comments.php 16.6 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
<?

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 displayKids ($tid, $mode, $order = 0, $thold = 0, $level = 0, $dummy = 0) {
24
  global $user, $theme;
Dries's avatar
Dries committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  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 ($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++;

Dries's avatar
Dries committed
39
        $link = "<A HREF=\"comments.php?op=reply&pid=$r_tid&sid=$r_sid&mode=$mode&order=$order&thold=$thold\"><FONT COLOR=\"$theme->hlcolor2\">reply to this comment</FONT></A>";
Dries's avatar
Dries committed
40
41
42
43
44
45
46
47
48
49
50
51

        $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]";

Dries's avatar
Dries committed
52
        $link = "<A HREF=\"comments.php?op=reply&pid=$r_tid&sid=$r_sid&mode=$mode&order=$order&thold=$thold\"><FONT COLOR=\"$theme->hlcolor2\">reply to this comment</FONT></A>";
Dries's avatar
Dries committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

        $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) {
68
  global $datetime, $theme, $user;
Dries's avatar
Dries committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

  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";
89
90
91
      if (isset($user->umode)) { $thread .= "&mode=$user->umode"; } else { $thread .= "&mode=threaded"; }
      if (isset($user->uorder)) { $thread .= "&order=$user->uorder"; } else { $thread .= "&order=0"; }
      if (isset($user->thold)) { $thread .= "&thold=$user->thold"; } else { $thread .= "&thold=0"; }
Dries's avatar
Dries committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
      $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) {
108
  global $user, $theme, $functions;
Dries's avatar
Dries committed
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

  ### 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;

  $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);

145
  $theme->commentControl($sid, $title, $thold, $mode, $order);
Dries's avatar
Dries committed
146
147
148
149
150
151
152
153
154
155
156

  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"));
Dries's avatar
Dries committed
157
      $link = "<A HREF=\"comments.php?sid=$sid&pid=$erin&mode=$mode&order=$order&thold=$thold\"><FONT COLOR=\"$theme->hlcolor2\">return to parent</FONT></A> | <A HREF=\"comments.php?op=reply&pid=$tid&sid=$sid&mode=$mode&order=$order&thold=$thold\"><FONT COLOR=\"$theme->hlcolor2\">reply to this comment</FONT></A>";
Dries's avatar
Dries committed
158
159
    }
    else {
Dries's avatar
Dries committed
160
      $link = "<A HREF=\"comments.php?op=reply&pid=$tid&sid=$sid&mode=$mode&order=$order&thold=$thold\"><FONT COLOR=\"$theme->hlcolor2\">reply to this comment</FONT></A> ";
Dries's avatar
Dries committed
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
    }

    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";
  
188
  global $user;
Dries's avatar
Dries committed
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
  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) {
228
    echo "<A HREF=\"account.php\">$user->userid</A> &nbsp; &nbsp; <FONT SIZE=\"2\">[ <A HREF=\"account.php?op=logout\">logout</A> ]</FONT>";
Dries's avatar
Dries committed
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
  } 
  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" ;

270
  global $user, $bgcolor1, $bgcolor2;
Dries's avatar
Dries committed
271
272
273
274
275
276
277
278
279

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

  $theme->header();

  ### Display preview:
  echo "<TABLE WIDTH=\"100%\" BORDER=\"0\">";
  if ($user) {
280
    echo " <TR BGCOLOR=\"$bgcolor1\"><TD><B>$subject</B><BR>by $user->userid.</TD></TR>";
Dries's avatar
Dries committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
  } 
  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) {
302
    echo "<A HREF=\"account.php\">$user->userid</A> &nbsp; &nbsp; <FONT SIZE=\"2\">[ <A HREF=\"account.php?op=logout\">logout</A> ]</FONT>";
Dries's avatar
Dries committed
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
  } 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) {
346
  global $user, $userinfo;
Dries's avatar
Dries committed
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
  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";
399
400
401
      if (isset($user->umode)) { $header .= "&mode=$user->umode"; } else { $header .= "&mode=threaded"; }
      if (isset($user->uorder)) { $header .= "&order=$user->uorder"; } else { $header .= "&order=0"; }    
      if (isset($user->thold)) { $header .= "&thold=$user->thold"; } else { $header .= "&thold=1"; }
Dries's avatar
Dries committed
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
    }
    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);
}

?>