具有树状结构的文本论坛(七)——回复主题

    回复主题的实现文件reply.php,它的代码如下:
<?    
  
if($submit == '提交回复')
  {
    if(
$content != "")
    {
      if(
$username != "" && $password != "")
      {
        if(
checkuser($username$password) == true)
        {
          
session_register("username");
          
reply($theme$username$face$oicq$email$homepage$content$REMOTE_ADDR$titleid$noteid);
          
$notes get_notes($titleid);
          echo 
"<meta http-equiv='Refresh' content='0; url=forum.php?action=read&id=$titleid&title=$title&notes=$notes'>";
          exit;
      }
        else
        {
          
$url="forum.php?action=reply&theme=$theme&email=$email&homepage=$homepage&oicq=$oicq&content=$content";
          include(
"loginerr.php");
        }
      }
      else if(!
session_is_registered("username"))
      {
        echo 
"<h3><font color=#FF0000>您还没有登录,必须输入名字和密码</font></h3>";
      }
      else
      {
        
reply($theme$username$face$oicq$email$homepage$content$REMOTE_ADDR$titleid$noteid);
        
$notes get_notes($titleid);
        echo 
"<meta http-equiv='Refresh' content='0; url=forum.php?action=read&id=$titleid&title=$title&notes=$notes'>";
        exit;
      }
    }
    else
    {
      echo 
"<h3><font color=#FF0000>您的输入不完整,回复内容是必须填的</font></h3>";
    }
  }     
      
  echo 
"<body bgcolor='#DDFFDD'>";
  echo 
"<div style='position:absolute; left:5%' ><div align='center'><h2><font color='#00AA00'>";
  echo 
"回复主题";
  echo 
"</font></h2></div>";
  echo 
"<form name='input' method='post'";  
  if(
session_is_registered("username"))
  {
    echo  
" action='forum.php?action=reply&username=$username&theme=$theme&email=$email&homepage=$homepage&oicq=$oicq&content=$content&titleid=$titleid&noteid=$noteid&title=$title'>";
    echo 
"名字:".$username."<br><br>";
  }
  else
  {  
    echo 
">名字:";
    echo 
"<input type='text' name='username' maxlength='16' size='15'>&nbsp;";
    echo 
"密码:";
    echo 
"<input type='password' name='password' maxlength='16' size='15'>(必填)<br><br>";
  }

  echo 
"OICQ:<input type='text' name='oicq' size='8' maxlength=10 value=$oicq>&nbsp;";
  echo 
"E-mail地址:<input type='text' name='email' size='20' value=$email>";
  echo 
"&nbsp;个人主页:<input type='text' name='homepage' size='22' value=$homepage>(可不填)<br><br>";
  
$theme "RE:$theme";
  echo 
"回复标题:<input type='text' name='theme' size='77' maxlength=MAX_TITLE_WIDTH value=$theme>(可不填)<br><br>";
      
  for(
$i 1$i <= 66$i ++)
  {
    
$str strval($i);
    if(
$i 10)
      
$str "0".$str;
    
$str "img/face".$str.".png";
    echo 
"<img src=$str width=20>";
    if(
$i != 1)
      echo 
"<input type='radio' name='face' value=$i>";
    else
      echo 
"<input type='radio' name='face' value=$i checked>";
    if(
$i%17 == 0)
      echo 
"<br>";
  }
      
  echo 
"<textarea name='content' cols=95 rows=5>$content</textarea>";
      
  echo 
"<div align=center><br><input type='submit' name='submit' value='提交回复'>&nbsp;";  
  echo 
"<input type='reset' name='reset' value='重新填写'></div><br>";  
?>
    可以发现这部分和新建主题是很相似的,不同之处是标题是可以不输入的,而用reply()函数代替addnew()函数,在funcdef.php文件中,reply()函数定义如下:
<?
  
//将回复帖子写入文件
  
function reply($title$maker$face$oicq$email$homepage$content$ip$titleid$noteid)
  {
    
$newid get_noteid($titleid$noteid);
    
//改写?主题文件
    
$fp fopen("rec/".$titleid"a+");
    
fwrite($fp"$newidt");
    
fwrite($fp"$titlet");
    
fwrite($fp"$facet");
    
fwrite($fp"$makert");
    
$timestr date("Y.m.d,H:i");    
    
fwrite($fp"$timestrt");
    
fwrite($fp"$ipt");
    
fwrite($fp"$oicqt");    
    
fwrite($fp"$emailt");
    
fwrite($fp"$homepaget");
    
$content ascii2html($content);
    
fwrite($fp"$contentn");  
    
fclose($fp);
    
    
//改写themelist文件,更新帖子数目
    
$fp fopen("rec/themelist""r");
    
$newfp fopen("rec/themelist.bak""w+");
    
    
$rows fgets($fpMAX_READ_BYTES);
    
fwrite($newfp$rows);
    
$rows fgets($fpMAX_READ_BYTES);
    
fwrite($newfpstrval(intval($rows) + 1));
    
fwrite($newfp"n");
    while(!
feof($fp))
    {
      
$rows fgets($fpMAX_READ_BYTES);
      
$recid intval(strtok($rows"t"));
      if(
$recid != $titleid)
      {
        
fwrite($newfp$rows);
      }
      else    
//如果是要修改的行
      
{
        
$rectitle strtok("t");
        
$recmaker strtok("t");        
        
$rectime strtok("t");        
        
$newnum intval(strtok("t")) + 1;
        
fwrite($newfp$recid."t");
        
fwrite($newfp$rectitle."t");
        
fwrite($newfp$recmaker."t");
        
fwrite($newfp$rectime."t");
        
fwrite($newfp"$newnumn");
      }
    }
    
fclose($newfp);
    
fclose($fp);
    
copy("rec/themelist.bak""rec/themelist");
    
unlink("rec/themelist.bak");
  }
?>
    在reply.php代码中有一个get_notes($titleid)函数被调用了,这个函数是根据标题ID来获得该标题下帖子总数目,其定义是:
<?
  
//获取某主题的总帖子数目
  
function get_notes($titleid)
  {
    if(!
file_exists("rec/themelist"))
      return 
0;
    
$fp fopen("rec/themelist""r");
    
fgets($fpMAX_READ_BYTES);
    
fgets($fpMAX_READ_BYTES);
    while(!
feof($fp))
    {
      
$rows fgets($fpMAX_READ_BYTES);
      
$recid intval(strtok($rows"t"));
      if(
$recid == $titleid)
      {
        
$notes intval(substr($rowsstrrpos($rows"t")));
        break;
      }
    }
    
fclose($fp);   
    return 
$notes;
  }
?>
   reply.php中还用到一个get_noteid()函数,是用来获得新帖子ID的,其定义如下:
<?
  
//获取新帖子应该具有的ID
  
function get_noteid($titleid$noteid)
  {
    
$fp fopen("rec/$titleid""r");
    for(
$i 1; ; $i ++)
    {
      if(
$i 10)
        
$curid $noteid."0".strval($i);
      else
        
$curid $noteid.strval($i);
      
$found 0;
      
fseek($fp0);
      while(!
feof($fp))
      {
        
$recid strtok(fgets($fpMAX_READ_BYTES), "t");
        if(
$curid == $recid)
          
$found 1;
      }
      if(!
$found)
        break;
    }
    
fclose($fp);
    return 
$curid;
  }
?>
    下次将介绍论坛的主题搜索部分。(未完待续)