comments.php 19.1 KB
Newer Older
Dries's avatar
Dries committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
<?

function moderate_1() {
  include "config.inc";
  global $admin;
  echo "<FORM ACTION=\"comments.php\" METHOD=\"post\">";
}

function moderate_2($tid, $reason) {
  include "config.inc";

  echo "<SELECT NAME=\"meta:$tid\">"; 
  for($i = 0; $i < sizeof($comments_meta_reasons); $i++) {
    echo "<OPTION VALUE=\"$i\">$comments_meta_reasons[$i]</OPTION>\n";
  }
  echo "</SELECT>";
}

function moderate_3($sid, $mode, $order, $thold = 0) {
  echo "<INPUT TYPE=\"hidden\" NAME=\"sid\" VALUE=\"$sid\"><INPUT TYPE=\"hidden\" NAME=\"mode\" VALUE=\"$mode\"><INPUT TYPE=\"hidden\" NAME=\"order\" VALUE=\"$order\"><INPUT TYPE=\"hidden\" NAME=\"thold\" VALUE=\"$thold\"><INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Moderate\"></FORM>";
}

function displayControlBlock($sid, $title, $thold, $mode, $order) {
  global $user, $theme;
  dbconnect();
  $query = mysql_query("SELECT sid FROM comments WHERE sid = $sid");

  if (!$query) $count = 0; else $count = mysql_num_rows($query);
  if (!isset($thold)) $thold = 0; 

 ?>
  <TABLE WIDTH="100%" BORDER="0" CELLSPACING="1" CELLPADDING="2">
   <TR>
    <TD ALIGN="center" BGCOLOR="<? echo "$theme->bgcolor2"; ?>"><FONT COLOR="<? echo "$theme->bgcolor1" ?>" SIZE="+1"><B>Comment control</B></FONT></TD>
   </TR>
   <TR>
    <TD ALIGN="center" BGCOLOR="<? echo "$theme->bgcolor1"; ?>">
     <FORM METHOD="get" ACTION="article.php">
      <FONT SIZE="2"> 
      <SELECT NAME="thold">
       <OPTION VALUE="-1" <? if ($thold == -1) { echo "SELECTED"; } ?>>Threshold: -1
       <OPTION VALUE="0" <? if ($thold == 0) { echo "SELECTED"; } ?>>Threshold: 0
       <OPTION VALUE="1" <? if ($thold == 1) { echo "SELECTED"; } ?>>Threshold: 1
       <OPTION VALUE="2" <? if ($thold == 2) { echo "SELECTED"; } ?>>Threshold: 2
       <OPTION VALUE="3" <? if ($thold == 3) { echo "SELECTED"; } ?>>Threshold: 3
       <OPTION VALUE="4" <? if ($thold == 4) { echo "SELECTED"; } ?>>Threshold: 4
       <OPTION VALUE="5" <? if ($thold == 5) { echo "SELECTED"; } ?>>Threshold: 5
      </SELECT> 
      <SELECT NAME="mode">
       <OPTION VALUE="nocomments" <? if ($mode == 'nocomments') { echo "SELECTED"; } ?>>No comments
       <OPTION VALUE="nested" <? if ($mode == 'nested') { echo "SELECTED"; } ?>>Nested
       <OPTION VALUE="flat" <? if ($mode == 'flat') { echo "SELECTED"; } ?>>Flat
       <OPTION VALUE="threaded" <? if (!isset($mode) || $mode=='threaded' || $mode=="") { echo "SELECTED"; } ?>>Threaded
      </SELECT> 
      <SELECT NAME="order">
       <OPTION VALUE="0" <? if (!$order) { echo "SELECTED"; } ?>>Oldest first
       <OPTION VALUE="1" <? if ($order==1) { echo "SELECTED"; } ?>>Newest first
       <OPTION VALUE="2" <? if ($order==2) { echo "SELECTED"; } ?>>Highest scoring first
      </SELECT> 
      <INPUT TYPE="hidden" NAME="sid" VALUE="<? echo "$sid"; ?>"> <INPUT TYPE="submit" VALUE="Refresh">
      <?
       if (isset($user)) echo "<BR><CENTER><INPUT TYPE=\"checkbox\" NAME=\"save\"> Save preferences</CENTER>"; 
      ?>
      </FONT>
     </FORM>
    </TD>
   </TR>
   <?
    $result = mysql_query("SELECT COUNT(tid) FROM comments WHERE sid = $sid AND score < $thold");
    if ($result && $number = mysql_result($result, 0)) {
   ?>
     <TR>
      <TD ALIGN="center" BGCOLOR="<? echo "$theme->bgcolor2"; ?>"><SMALL><FONT COLOR="<? echo "$theme->fgcolor2"; ?>">There are at least <? echo $number; ?> comments below your threshold.</FONT></SMALL></TD>
     </TR>
   <?
    }
   ?> 
  </TABLE>
<?
}

function displayKids ($tid, $mode, $order = 0, $thold = 0, $level = 0, $dummy = 0) {
  global $user, $cookie, $theme;
  include "config.inc";
  $comments = 0;
  cookiedecode($user);

  $result = mysql_query("SELECT tid, pid, sid, date, name, email, url, host_name, subject, comment, score, reason FROM comments WHERE pid = $tid ORDER BY date, tid");

  if ($mode == 'nested') {
    while (list($r_tid, $r_pid, $r_sid, $r_date, $r_name, $r_email, $r_url, $r_host_name, $r_subject, $r_comment, $r_score, $r_reason) = mysql_fetch_row($result)) {
      if ($r_score >= $thold) {
        if ($level && !$comments) {
           echo "<UL>";
           $tblwidth -= 5;
        }
        $comments++;

        $link = "<A HREF=\"comments.php?op=reply&pid=$r_tid&sid=$r_sid&mode=$mode&order=$order&thold=$thold\">reply to this comment</A>";

        $theme->comment($r_name, $r_subject, $r_tid, $r_date, $r_url, $r_email, $r_score, $r_reason, $r_comment, $link);
        
        displayKids($r_tid, $mode, $order, $thold, $level + 1, $dummy + 1);
      }
    }
  } elseif ($mode == 'flat') {
    while (list($r_tid, $r_pid, $r_sid, $r_date, $r_name, $r_email, $r_url, $r_host_name, $r_subject, $r_comment, $r_score, $r_reason) = mysql_fetch_row($result)) {
      if ($r_score >= $thold) {
        if (!eregi("[a-z0-9]",$r_name)) $r_name = $anonymous;
        if (!eregi("[a-z0-9]",$r_subject)) $r_subject = "[no subject]";

        $link = "<A HREF=\"comments.php?op=reply&pid=$r_tid&sid=$r_sid&mode=$mode&order=$order&thold=$thold\">reply to this comment</A>";

        $theme->comment($r_name, $r_subject, $r_tid, $r_date, $r_url, $r_email, $r_score, $r_reason, $r_comment, $link);
      } 
      displayKids($r_tid, $mode, $order, $thold);
    }
  } else {
    echo "ERROR: we should not get here!";
  }
  
  if ($level && $comments) {
    echo "</UL>";
  }
}

function displayBabies ($tid, $level = 0, $dummy = 0, $thread) {
  global $datetime, $theme, $user, $cookie;

  include "config.inc";

  $comments = 0;
  $result = mysql_query("SELECT tid, pid, sid, date, name, email, url, host_name, subject, comment, score, reason FROM comments WHERE pid = $tid ORDER BY date, tid");
  
  if ($level == 0) $thread = "";

  while (list($r_tid, $r_pid, $r_sid, $r_date, $r_name, $r_email, $r_url, $r_host_name, $r_subject, $r_comment, $r_score, $r_reason) = mysql_fetch_row($result)) {
    if ($level && !$comments) {
      $thread .= "<UL>";
    }
  
    $comments++;
    if (!eregi("[a-z0-9]",$r_name)) { $r_name = $anonymous; }
    if (!eregi("[a-z0-9]",$r_subject)) { $r_subject = "[no subject]"; }

    if ($user) {
      ### Make sure to respect the user preferences:
      $thread .= "<LI><A HREF=\"comments.php?op=showreply&tid=$r_tid&pid=$r_pid&sid=$r_sid";
      if (isset($cookie[4])) { $thread .= "&mode=$cookie[4]"; } else { $thread .= "&mode=threaded"; }
      if (isset($cookie[5])) { $thread .= "&order=$cookie[5]"; } else { $thread .= "&order=0"; }
      if (isset($cookie[6])) { $thread .= "&thold=$cookie[6]"; } else { $thread .= "&thold=0"; }
      $thread .= "\">$r_subject</A> by $r_name <FONT SIZE=\"2\">(". formatTimestamp($r_date) .")</FONT></LI>";
    }
    else {
      $thread .= "<LI><A HREF=\"comments.php?op=showreply&tid=$r_tid&pid=$r_pid&sid=$r_sid&mode=threaded&order=1&thold=0\">$r_subject</A> by $r_name <FONT SIZE=\"2\">(". formatTimestamp($r_date) .")</FONT></LI>";
    }   
    displayBabies($r_tid, $level + 1, $dummy + 1, &$thread);
  } 

  if ($level && $comments) {
    $thread .= "</UL>";
  }

  return $thread;
}

function displayTopic ($sid, $pid = 0, $tid = 0, $mode = "threaded", $order = 0, $thold = 0, $level = 0, $nokids = 0) {
  global $user, $cookie, $theme, $functions;

  ### include required files:
  if ($functions) {
    include "config.inc";
  }
  else {
    include "functions.inc";
    include "theme.inc";
    $theme->header();
  }

  ### ensure default value:
  if (!isset($pid)) $pid = 0;

  ### connect to database:
  dbconnect();

  $count_times = 0;
  cookiedecode($user);

  $q = "SELECT tid, pid, sid, date, name, email, url, host_name, subject, comment, score, reason FROM comments WHERE sid = $sid AND pid = $pid";

  if ($mode == 'threaded' || mode == 'nested') {
    if ($thold != "") {
      $q .= " AND score >= $thold";
    } else {
      $q .= " AND score >= 0";
    }
  }

  if ($order == 1) $q .= " ORDER BY date DESC";
  if ($order == 2) $q .= " ORDER BY score DESC";

  $res = mysql_query("$q");
  
  $num_tid = mysql_num_rows($res);

  displayControlBlock($sid, $title, $thold, $mode, $order);

  moderate_1();

  while ($count_times < $num_tid) {
    list($tid, $pid, $sid, $date, $name, $email, $url, $host_name, $subject, $comment, $score, $reason) = mysql_fetch_row($res);
    if ($name == "") { $name = $anonymous; }
    if ($subject == "") { $subject = "[no subject]"; }	

    ### Dynamically generate the link:
    if ($pid != 0) {
      list($erin) = mysql_fetch_row(mysql_query("SELECT pid FROM comments WHERE tid=$pid"));
      $link = "<A HREF=\"comments.php?sid=$sid&pid=$erin&mode=$mode&order=$order&thold=$thold\">return to parent</A> | <A HREF=\"comments.php?op=reply&pid=$tid&sid=$sid&mode=$mode&order=$order&thold=$thold\">reply to this comment</A>";
    }
    else {
      $link = "<A HREF=\"comments.php?op=reply&pid=$tid&sid=$sid&mode=$mode&order=$order&thold=$thold\">reply to this comment</A> ";
    }

    if ($mode == "threaded") {
      $thread = displayBabies($tid, $mode, $order, $thold, $level);
      $theme->comment($name, $subject, $tid, $date, $url, $email, $score, $reason, $comment, $link, $thread);
    }
    else {
      $theme->comment($name, $subject, $tid, $date, $url, $email, $score, $reason, $comment, $link);
      displayKids($tid, $mode, $order, $thold, $level);
    }

    echo "</UL>";
    echo "</P>";
    $count_times += 1;
  }

  moderate_3($sid, $mode, $order, $thold);

  if ($pid == 0) return array($sid, $pid, $subject);
  else $theme->footer();
}


function reply($pid, $sid, $mode, $order, $thold) {
  include "functions.inc";
  include "theme.inc";
  
  global $user, $cookie;
  dbconnect();

  $theme->header();
  
  if ($pid != 0) {
    list($date, $name, $email, $url, $subject, $comment, $score) = mysql_fetch_row(mysql_query("SELECT date, name, email, url, subject, comment, score FROM comments WHERE tid = $pid"));
  } else {
    list($date, $subject, $comment, $name) = mysql_fetch_row(mysql_query("SELECT time, subject, abstract, informant FROM stories WHERE sid = $sid"));
  }

  ### Pre-process the variables:
  if ($comment == "") $comment = $comment;
  if ($subject == "") $subject = "[no subject]";
  if ($name == "") $name = $anonymous;

  ### Display parent comment:
  echo "<TABLE WIDTH=\"100%\" BORDER=\"0\">";
  if ($email) {
    echo " <TR BGCOLOR=\"$theme->bgcolor1\"><TD><FONT COLOR=\"$theme->hlcolor1\"><B>$subject</B><BR>by <A HREF=\"mailto:$email\">$name</A> <B>($email)</B> on ". formatTimestamp($date) ."</FONT></TD></TR>";
  } 
  else {
    echo " <TR BGCOLOR=\"$theme->bgcolor1\"><TD><FONT COLOR=\"$theme->hlcolor1\"><B>$subject</B><BR>by $name on ". formatTimestamp($date) ."</FONT></TD></TR>";
  }
  echo " <TR BGCOLOR=\"$theme->bgcolor2\"><TD>$comment</TD></TR>";
  echo "</TABLE>";

  if (!isset($pid) || !isset($sid)) { exit(); }
  if ($pid == 0) {
    list($subject) = mysql_fetch_row(mysql_query("SELECT subject FROM stories WHERE sid = $sid"));
  } 
  else {
    list($subject) = mysql_fetch_row(mysql_query("SELECT subject FROM comments WHERE tid = $pid"));
  }

  ### Build reply form:
  echo "<FORM ACTION=\"comments.php\" METHOD=\"post\">";

  echo "<B>Your name:</B><BR> ";
  if ($user) {
    cookiedecode($user);
    echo "<A HREF=\"account.php\">$cookie[1]</A> &nbsp; &nbsp; <FONT SIZE=\"2\">[ <A HREF=\"account.php?op=logout\">logout</A> ]</FONT>";
  } 
  else {
    echo "$anonymous"; 
    $postanon = 2;
  }
  echo "<BR><BR>";

  echo "<B>Subject:</B><BR>";
  if (!eregi("Re:",$subject)) $subject = "Re: $subject"; 
    // Only one 'Re:' will just do fine. ;)
  echo "<INPUT TYPE=\"text\" NAME=\"subject\" SIZE=\"60\" MAXLENGTH=\"60\" VALUE=\"$subject\">";
  echo "<BR><BR>";

  if ($user) { 
    $userinfo = getusrinfo($user);
    echo "<TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"10\" NAME=\"comment\">$userinfo[signature]</TEXTAREA><BR>";
    echo "<INPUT TYPE=\"checkbox\" NAME=\"postanon\"> Post this comment anonymously.";
    echo "<BR><BR>";
  }
  else {
    echo "<TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"10\" NAME=\"comment\"></TEXTAREA>";
    echo "<BR><BR>";
  }

  echo "<INPUT TYPE=\"hidden\" NAME=\"pid\" VALUE=\"$pid\">";
  echo "<INPUT TYPE=\"hidden\" NAME=\"sid\" VALUE=\"$sid\"><INPUT TYPE=\"hidden\" NAME=\"mode\" VALUE=\"$mode\">";
  echo "<INPUT TYPE=\"hidden\" NAME=\"order\" VALUE=\"$order\"><INPUT TYPE=\"hidden\" NAME=\"thold\" VALUE=\"$thold\">";
  echo "<INPUT TYPE=submit NAME=op VALUE=\"Preview comment\"> <INPUT TYPE=submit NAME=op VALUE=\"Post comment\"> <SELECT NAME=\"posttype\"><OPTION VALUE=\"exttrans\">HTML to text<OPTION VALUE=\"html\">HTML-formatted<OPTION VALUE=\"plaintext\" SELECTED>Plain text</SELECT></FORM>";
 
  echo "<FONT SIZE=\"2\">Allowed HTML-tags:<BR>";
  for ($i=0; $i < sizeof($AllowableHTML); $i++) {
    if (!eregi("/",$AllowableHTML[$i])) echo " &lt;$AllowableHTML[$i]&gt;";
  }
  
  $theme->footer();
}

function replyPreview ($pid, $sid, $subject, $comment, $postanon, $mode, $order, $thold, $posttype) {
  include "functions.inc";
  include "theme.inc" ;

  global $user, $cookie, $bgcolor1, $bgcolor2;

  cookiedecode($user);
  $subject = stripslashes($subject);
  $comment = stripslashes($comment);

  $theme->header();

  ### Display preview:
  echo "<TABLE WIDTH=\"100%\" BORDER=\"0\">";
  if ($user) {
    echo " <TR BGCOLOR=\"$bgcolor1\"><TD><B>$subject</B><BR>by $cookie[1].</TD></TR>";
  } 
  else {
    echo " <TR BGCOLOR=\"$bgcolor1\"><TD><B>$subject</B><BR>by $anonymous.</TD></TR>";
  }

  if ($posttype == "exttrans") {
    echo " <TR BGCOLOR=\"$bgcolor2\"><TD>". nl2br(htmlspecialchars($comment)) ."</TD></TR>";
  }
  elseif ($posttype == "plaintext") {
    echo " <TR BGCOLOR=\"$bgcolor2\"><TD>". nl2br($comment) ."</TD></TR>";
  }
  else {
    echo " <TR BGCOLOR=\"$bgcolor2\"><TD>$comment</TD></TR>";
  }
  echo "</TABLE>";

  ### Build reply form:
  echo "<FORM ACTION=\"comments.php\" METHOD=\"post\">";

  echo "<B>Your name:</B><BR> ";
  if ($user) {
    cookiedecode($user);
    echo "<A HREF=\"account.php\">$cookie[1]</A> &nbsp; &nbsp; <FONT SIZE=\"2\">[ <A HREF=\"account.php?op=logout\">logout</A> ]</FONT>";
  } else {
    echo "$anonymous"; 
    $postanon = 2;
  }
  echo "<BR><BR>";

  echo "<B>Subject:</B><BR>";
  if (!eregi("Re:",$subject)) $subject = "Re: $subject"; // one Re: will do ;)
  echo "<INPUT TYPE=\"text\" NAME=\"subject\" SIZE=\"60\" MAXLENGTH=\"60\" VALUE=\"$subject\">";
  echo "<BR><BR>";

  $userinfo = getusrinfo($user);
  echo "<TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"10\" NAME=\"comment\">$comment</TEXTAREA>";
  if ($user) { 
   if ($postanon) echo "<BR><INPUT TYPE=\"checkbox\" NAME=\"postanon\" CHECKED> Post this comment anonymously.";
   else echo "<BR><INPUT TYPE=\"checkbox\" NAME=\"postanon\"> Post this comment anonymously.";
  }
  echo "<BR><BR>";

  echo "<INPUT TYPE=\"hidden\" NAME=\"pid\" VALUE=\"$pid\">";
  echo "<INPUT TYPE=\"hidden\" NAME=\"sid\" VALUE=\"$sid\"><INPUT TYPE=\"hidden\" NAME=\"mode\" VALUE=\"$mode\">";
  echo "<INPUT TYPE=\"hidden\" NAME=\"order\" VALUE=\"$order\"><INPUT TYPE=\"hidden\" NAME=\"thold\" VALUE=\"$thold\">";
  echo "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Preview comment\"> ";
  echo "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"Post comment\"> ";
  echo "<SELECT NAME=\"posttype\">";
  if ($posttype == "exttrans") echo " <OPTION VALUE=\"exttrans\" SELECTED>HTML to text";
  else echo " <OPTION VALUE=\"exttrans\">HTML to text";
  if ($posttype == "html") echo " <OPTION VALUE=\"html\" SELECTED>HTML-formatted";
  else echo " <OPTION VALUE=\"html\">HTML-formatted";
  if ($posttype == "plaintext") echo " <OPTION VALUE=\"plaintext\" SELECTED>Plain text";
  else echo " <OPTION VALUE=\"plaintext\">Plain text";
  echo "</SELECT>";
  echo "</FORM>";
 
  echo "<FONT SIZE=\"2\">Allowed HTML-tags:<BR>";
  for ($i=0; $i < sizeof($AllowableHTML); $i++) {
    if (!eregi("/",$AllowableHTML[$i])) echo " &lt;$AllowableHTML[$i]&gt;";
  }

  $theme->footer();
}

function postComment($postanon, $subject, $comment, $pid, $sid, $host_name, $mode, $order, $thold, $posttype) {
  global $user, $userinfo, $cookie;
  include "functions.inc";
  include "config.inc";
  dbconnect();

  $subject = FixQuotes($subject);
  $comment = FixQuotes($comment);
  $author = FixQuotes($author);

  if ($posttype == "exttrans") $comment = nl2br(htmlspecialchars($comment));
  elseif($posttype == "plaintext") $comment = nl2br($comment);
  else $comment = $comment;

  if (($user) && (!$postanon)) {
    getusrinfo($user);
    $name = $userinfo[uname];
    $email = $userinfo[femail];
    $url = $userinfo[url];
    $score = 1;
  } else {
    $name = ""; 
    $email = ""; 
    $url = "";
    $score = 0;
  }
  $ip = getenv("REMOTE_ADDR");

  ### Check for fake threads:
  $fake = mysql_result(mysql_query("SELECT COUNT(*) FROM stories WHERE sid = $sid"), 0);

  ### Check for duplicate comments:
  $duplicate = mysql_result(mysql_query("SELECT COUNT(*) FROM comments WHERE pid = '$pid' AND sid = '$sid' AND subject = '$subject' AND comment = '$comment'"), 0);
  
  if ($fake != 1) {
    include "theme.inc";
    $theme->header();
    $theme->box("fake comment", "fake comment: $fake");
    $theme->footer();
  }
  elseif ($duplicate != 0) {
    include "theme.inc";
    $theme->header();
    $theme->box("duplicate comment", "duplicate comment: $duplicate");
    $theme->footer();
  }
  else {
    ### Add comment to table:
    $reason = (int) sizeof($comments_meta_reasons) / 2;
    mysql_query("INSERT INTO comments (tid, pid, sid, date, name, email, url, host_name, subject, comment, score, reason) VALUES (NULL, '$pid', '$sid', now(), '$name', '$email', '$url', '$ip', '$subject', '$comment', '$score', '$reason')");
  
    ### Compose header:
    if ($user) {
      $header = "article.php?sid=$sid";
      if (isset($cookie[4])) { $header .= "&mode=$cookie[4]"; } else { $header .= "&mode=threaded"; }
      if (isset($cookie[5])) { $header .= "&order=$cookie[5]"; } else { $header .= "&order=0"; }    
      if (isset($cookie[6])) { $header .= "&thold=$cookie[6]"; } else { $header .= "&thold=1"; }
    }
    else {
      $header .= "article.php?sid=$sid&mode=threaded&order=1&thold=0";
    }
    header("Location: $header");
  }
}

function moderate($tid, $meta_value = 0) {
  include "config.inc";
  if ($meta_value != -1) {
    ### Compose query:
    $query = "UPDATE comments SET";
    if ($meta_value > (sizeof($comments_meta_reasons) / 2)) {
      $query .= " score = score + 1, reason = $meta_value WHERE tid = $tid";
    } 
    elseif ($meta_value < ((sizeof($comments_meta_reasons) / 2) - 1)) {
      $query .= " score = score - 1, reason = $meta_value WHERE tid = $tid";
    }
    else {
      $query .= " reason = $meta_value WHERE tid = $tid";
    }

    ### Perform query:
    mysql_query("$query");
  }
}

switch($op) {
  case "reply":
    reply($pid, $sid, $mode, $order, $thold);
    break;
  case "Preview comment":
    replyPreview($pid, $sid, $subject, $comment, $postanon, $mode, $order, $thold, $posttype);
    break;
  case "Post comment":
    postComment($postanon, $subject, $comment, $pid, $sid, $host_name, $mode, $order, $thold, $posttype);
    break;
  case "Moderate":
    include "functions.inc";	
    dbconnect();

    while (list($name, $value) = each($HTTP_POST_VARS)) {
      if (eregi("meta", $name)) {
        ### extract comment id (tid):
        $info = explode(":", $name);
        moderate($info[1], $value);
      }
    }

    Header("Location: article.php?sid=$sid&mode=$mode&order=$order&thold=$thold");
    break;
  case "showreply":
    displayTopic($sid, $pid, $tid, $mode, $order, $thold);
    break;
  default:
    displayTopic($sid, $pid, $tid, $mode, $order, $thold);
}

?>