comment.inc 13.6 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
Dries's avatar
Dries committed
2

Dries's avatar
 
Dries committed
3 4
// Security check:
if (strstr($id, " ") || strstr($pid, " ") || strstr($lid, " ") || strstr($mode, " ") || strstr($order, " ") || strstr($threshold, " ")) {
Dries's avatar
 
Dries committed
5
  watchdog("error", "comment: attempt to provide malicious input through URI");
Dries's avatar
 
Dries committed
6 7 8 9
  exit();
}

$cmodes = array(1 => "List - min", 2 => "List - max", 3 => "Threaded - min", 4 => "Threaded - max");
Dries's avatar
 
Dries committed
10
$corder = array(1 => "Date - new", 2 => "Date - old", 3 => "Rate - high", 4 => "Rate - low");
Dries's avatar
 
Dries committed
11

Dries's avatar
Dries committed
12
class Comment {
Dries's avatar
 
Dries committed
13
  function Comment($userid, $subject, $comment, $timestamp, $url, $fake_email, $score, $votes, $cid, $lid) {
Dries's avatar
Dries committed
14 15 16 17 18 19 20 21 22
    $this->userid = $userid;
    $this->subject = $subject;
    $this->comment = $comment;
    $this->timestamp = $timestamp;
    $this->url = $url;
    $this->fake_email = $fake_email;
    $this->score = $score;
    $this->votes = $votes;
    $this->cid = $cid;
Dries's avatar
 
Dries committed
23
    $this->lid = $lid;
Dries's avatar
Dries committed
24 25 26
  }
}

Dries's avatar
 
Dries committed
27 28 29 30 31 32 33
function comment_moderate($moderate) {
  global $user, $comment_votes;

  if ($user->id && $moderate) {
    $none = $comment_votes[key($comment_votes)];

    foreach ($moderate as $id=>$vote) {
Dries's avatar
 
Dries committed
34 35 36
      if ($vote != $comment_votes[$none]) {
        $id = check_input($id); $vote = check_input($vote);
        $comment = db_fetch_object(db_query("SELECT * FROM comments WHERE cid = '$id'"));
Dries's avatar
 
Dries committed
37
        if ($comment && !field_get($comment->users, $user->userid)) {
Dries's avatar
 
Dries committed
38
          $result = db_query("UPDATE comments SET score = score $vote, votes = votes + 1, users = '". field_set($comment->users, $user->userid, $vote) ."' WHERE cid = '$id'");
Dries's avatar
 
Dries committed
39
        }
Dries's avatar
 
Dries committed
40 41 42 43 44 45 46
      }
    }
  }
}

function comment_settings($mode, $order, $threshold) {
  global $user;
Dries's avatar
 
Dries committed
47
  if ($user->id) $user = user_save($user, array("mode" => $mode, "sort" => $order, "threshold" => $threshold));
Dries's avatar
 
Dries committed
48 49 50
}

function comment_reply($pid, $id) {
Dries's avatar
CHANGES  
Dries committed
51
  global $REQUEST_URI, $theme, $user;
Dries's avatar
 
Dries committed
52 53

  if ($pid) {
Dries's avatar
 
Dries committed
54
    $item = db_fetch_object(db_query("SELECT comments.*, users.userid FROM comments LEFT JOIN users ON comments.author = users.id WHERE comments.cid = '$pid'"));
Dries's avatar
 
Dries committed
55
    comment_view(new Comment($item->userid, $item->subject, $item->comment, $item->timestamp, $item->url, $item->fake_email, comment_score($comment), $comment->votes, $item->cid, $item->lid), t("reply to this comment"));
Dries's avatar
 
Dries committed
56
  }
Dries's avatar
 
Dries committed
57 58 59 60
  else {
    node_view(node_get_object(array("nid" => $id)));
    $pid = 0;
  }
Dries's avatar
 
Dries committed
61 62

  // Build reply form:
63
  $output .= "<FORM ACTION=\"$REQUEST_URI\" METHOD=\"post\">\n";
Dries's avatar
 
Dries committed
64 65

  // Name field:
Dries's avatar
 
Dries committed
66 67
  $output .= "<B>". t("Your name") .":</B><BR>\n";
  $output .= format_username($user->userid) ."<P>\n";
Dries's avatar
 
Dries committed
68 69

  // Subject field:
Dries's avatar
 
Dries committed
70 71
  $output .= "<B>". t("Subject") .":</B><BR>\n";
  $output .= "<INPUT TYPE=\"text\" NAME=\"subject\" SIZE=\"50\" MAXLENGTH=\"60\"><P>\n";
Dries's avatar
 
Dries committed
72 73

  // Comment field:
Dries's avatar
 
Dries committed
74
  $output .= "<B>".t("Comment") .":</B><BR>\n";
Dries's avatar
CHANGES  
Dries committed
75 76
  $output .= "<TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"10\" NAME=\"comment\">". check_form($user->signature) ."</TEXTAREA><BR>\n";
  $output .= "<SMALL><I>". t("Allowed HTML tags") .": ". htmlspecialchars(variable_get("allowed_html", "")) .".</I></SMALL><P>\n";
Dries's avatar
 
Dries committed
77

Dries's avatar
 
Dries committed
78
  // Preview button:
Dries's avatar
 
Dries committed
79 80 81 82
  $output .= "<SMALL><I>". t("You must preview at least once before you can submit") .":</I></SMALL><BR>\n";
  $output .= "<INPUT TYPE=\"hidden\" NAME=\"pid\" VALUE=\"$pid\">\n";
  $output .= "<INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$id\">\n";
  $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Preview comment") ."\"><BR>\n";
Dries's avatar
 
Dries committed
83 84 85

  $output .= "</FORM>\n";

Dries's avatar
 
Dries committed
86
  $theme->box(t("Reply"), $output);
Dries's avatar
 
Dries committed
87 88 89
}

function comment_preview($pid, $id, $subject, $comment) {
Dries's avatar
CHANGES  
Dries committed
90
  global $REQUEST_URI, $theme, $user;
Dries's avatar
 
Dries committed
91 92

  // Preview comment:
Dries's avatar
CHANGES  
Dries committed
93
  comment_view(new Comment($user->userid, check_preview($subject), check_preview($comment), time(), check_preview($user->url), check_preview($user->fake_email), 0, 0, 0, 0), t("reply to this comment"));
Dries's avatar
 
Dries committed
94 95

  // Build reply form:
96
  $output .= "<FORM ACTION=\"$REQUEST_URI\" METHOD=\"post\">\n";
Dries's avatar
 
Dries committed
97 98

  // Name field:
Dries's avatar
 
Dries committed
99 100
  $output .= "<B>". t("Your name") .":</B><BR>\n";
  $output .= format_username($user->userid) ."<P>\n";
Dries's avatar
 
Dries committed
101 102

  // Subject field:
Dries's avatar
 
Dries committed
103
  $output .= "<B>". t("Subject") .":</B><BR>\n";
Dries's avatar
CHANGES  
Dries committed
104
  $output .= "<INPUT TYPE=\"text\" NAME=\"subject\" SIZE=\"50\" MAXLENGTH=\"60\" VALUE=\"". check_form($subject) ."\"><P>\n";
Dries's avatar
 
Dries committed
105 106

  // Comment field:
Dries's avatar
 
Dries committed
107
  $output .= "<B>". t("Comment") .":</B><BR>\n";
Dries's avatar
CHANGES  
Dries committed
108 109
  $output .= "<TEXTAREA WRAP=\"virtual\" COLS=\"50\" ROWS=\"10\" NAME=\"comment\">". check_form($comment) ."</TEXTAREA><BR>\n";
  $output .= "<SMALL><I>". t("Allowed HTML tags") .": ". htmlspecialchars(variable_get("allowed_html", "")) .".</I></SMALL><P>\n";
Dries's avatar
 
Dries committed
110

Dries's avatar
 
Dries committed
111 112 113 114 115
  // Hidden fields:
  $output .= "<INPUT TYPE=\"hidden\" NAME=\"pid\" VALUE=\"$pid\">\n";
  $output .= "<INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$id\">\n";

  if (empty($subject)) {
Dries's avatar
 
Dries committed
116
    $output .= "<FONT COLOR=\"red\">". t("Warning: you did not supply a subject.") ."</FONT><P>\n";
Dries's avatar
 
Dries committed
117 118 119
  }

  // Preview and submit button:
Dries's avatar
 
Dries committed
120 121 122
  $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Preview comment") ."\">\n";
  $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Post comment") ."\">\n";
  $output .= "</FORM>\n";
Dries's avatar
 
Dries committed
123

Dries's avatar
 
Dries committed
124
  $theme->box(t("Reply"), $output);
Dries's avatar
 
Dries committed
125 126 127
}

function comment_post($pid, $id, $subject, $comment) {
Dries's avatar
 
Dries committed
128
  global $theme, $user;
Dries's avatar
 
Dries committed
129

Dries's avatar
 
Dries committed
130
  // check comment submission rate:
Dries's avatar
 
Dries committed
131 132
  throttle("post comment", variable_get(max_comment_rate, 60));

Dries's avatar
 
Dries committed
133
  // check for duplicate comments:
Dries's avatar
 
Dries committed
134
  $duplicate = db_result(db_query("SELECT COUNT(cid) FROM comments WHERE pid = '$pid' AND lid = '$id' AND subject = '$subject' AND comment = '$comment'"), 0);
Dries's avatar
 
Dries committed
135 136

  if ($duplicate != 0) {
Dries's avatar
 
Dries committed
137
    watchdog("warning", "comment: duplicate '$subject'");
Dries's avatar
 
Dries committed
138
  }
Dries's avatar
 
Dries committed
139
  else {
Dries's avatar
 
Dries committed
140
    // validate subject:
Dries's avatar
 
Dries committed
141 142
    $subject = ($subject) ? $subject : substr($comment, 0, 29);

Dries's avatar
 
Dries committed
143
    // add watchdog entry:
Dries's avatar
 
Dries committed
144
    watchdog("special", "comment: added '$subject'");
Dries's avatar
 
Dries committed
145

Dries's avatar
 
Dries committed
146
    // add comment to database:
Dries's avatar
 
Dries committed
147
    db_query("INSERT INTO comments (lid, pid, author, subject, comment, hostname, timestamp, score) VALUES ('$id', '$pid', '$user->id', '$subject', '$comment', '". getenv("REMOTE_ADDR") ."', '". time() ."', '". ($user->userid ? 1 : 0) ."')");
Dries's avatar
 
Dries committed
148 149 150 151 152 153 154 155 156
  }
}

function comment_score($comment) {
  $value = ($comment->votes) ? ($comment->score / $comment->votes) : (($comment->score) ? $comment->score : 0);
  return ((strpos($value, ".")) ? substr($value ."00", 0, 4) : $value .".00");
}

function comment_num_replies($id, $count = 0) {
Dries's avatar
 
Dries committed
157
  $result = db_query("SELECT COUNT(cid) FROM comments WHERE pid = '$id'");
Dries's avatar
 
Dries committed
158 159
  return ($result) ? db_result($result, 0) : 0;
}
Dries's avatar
 
Dries committed
160

Dries's avatar
 
Dries committed
161 162 163 164
function comment_moderation($comment) {
  global $comment_votes, $op, $user;

  if ($op == "reply") {
Dries's avatar
 
Dries committed
165
    // preview comment:
Dries's avatar
 
Dries committed
166 167
    $output .= "&nbsp;";
  }
Dries's avatar
 
Dries committed
168
  else if ($user->id && $user->userid != $comment->userid && !field_get($comment->users, $user->userid)) {
Dries's avatar
 
Dries committed
169 170 171
    // comment hasn't been moderated yet:
    foreach ($comment_votes as $key=>$value) $options .= " <OPTION VALUE=\"$value\">$key</OPTION>\n";
    $output .= "<SELECT NAME=\"moderate[$comment->cid]\">$options</SELECT>\n";
Dries's avatar
 
Dries committed
172 173
  }
  else {
Dries's avatar
 
Dries committed
174
    // comment has already been moderated:
Dries's avatar
 
Dries committed
175
    $output .= "<TABLE BORDER=\"0\" CELLSPACING=\"1\" CELLPADDING=\"1\"><TR><TD ALIGN=\"right\">". t("score") .":</TD><TD>". check_output($comment->score) ."</TD></TR><TR><TD ALIGN=\"right\">". t("votes") .":</TD><TD>". check_output($comment->votes) ."</TR></TABLE>\n";
Dries's avatar
 
Dries committed
176 177 178 179 180
  }

  return $output;
}

Dries's avatar
 
Dries committed
181 182
function comment_controls($threshold = 1, $mode = 3, $order = 1) {
  global $REQUEST_URI, $user;
183
  $output .= "<DIV ALIGN=\"CENTER\">\n";
Dries's avatar
 
Dries committed
184
  $output .= "<FORM METHOD=\"post\" ACTION=\"$REQUEST_URI\">\n";
Dries's avatar
 
Dries committed
185 186 187
  $output .= comment_mode(($user->id ? $user->mode : $mode));
  $output .= comment_order(($user->id ? $user->sort : $order));
  $output .= comment_threshold(($user->id ? $user->threshold : $threshold));
Dries's avatar
 
Dries committed
188
  $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Update settings") ."\">\n";
Dries's avatar
 
Dries committed
189
  $output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Add comment") ."\">\n";
Dries's avatar
 
Dries committed
190
  $output .= "</FORM>\n";
191
  $output .= "</DIV>\n";
Dries's avatar
 
Dries committed
192 193 194 195
  return $output;
}

function comment_threshold($threshold) {
Dries's avatar
 
Dries committed
196 197
  for ($i = -1; $i < 6; $i++) $options .= " <OPTION VALUE=\"$i\"". ($threshold == $i ? " SELECTED" : "") .">". t("Filter") ." - $i</OPTION>";
  return "<SELECT NAME=\"threshold\">$options</SELECT>\n";
Dries's avatar
 
Dries committed
198 199 200 201
}

function comment_mode($mode) {
  global $cmodes;
Dries's avatar
 
Dries committed
202 203
  foreach ($cmodes as $key=>$value) $options .= " <OPTION VALUE=\"$key\"". ($mode == $key ? " SELECTED" : "") .">$value</OPTION>\n";
  return "<SELECT NAME=\"mode\">$options</SELECT>\n";
Dries's avatar
 
Dries committed
204 205 206 207
}

function comment_order($order) {
  global $corder;
Dries's avatar
 
Dries committed
208 209
  foreach ($corder as $key=>$value) $options .= " <OPTION VALUE=\"$key\"". ($order == $key ? " SELECTED" : "") .">$value</OPTION>\n";
  return "<SELECT NAME=\"order\">$options</SELECT>\n";
Dries's avatar
 
Dries committed
210 211
}

Dries's avatar
 
Dries committed
212 213
function comment_query($lid, $order, $pid = -1) {
  $query .= "SELECT c.*, u.* FROM comments c LEFT JOIN users u ON c.author = u.id WHERE c.lid = '$lid'";
Dries's avatar
 
Dries committed
214
  if ($pid >= 0) $query .= " AND pid = '$pid'";
Dries's avatar
 
Dries committed
215
  if ($order == 1) $query .= " ORDER BY c.timestamp DESC";
Dries's avatar
 
Dries committed
216 217 218
  else if ($order == 2) $query .= " ORDER BY c.timestamp";
  else if ($order == 3) $query .= " ORDER BY c.score DESC";
  else if ($order == 4) $query .= " ORDER BY c.score";
Dries's avatar
 
Dries committed
219 220 221 222 223 224 225 226 227 228
  return db_query($query);
}

function comment_visible($comment, $threshold = 0) {
  if ($comment->votes == 0 && $comment->score >= $threshold) return 1;
  else if ($comment->votes > 0 && $comment->score / $comment->votes >= $threshold) return 1;
  else return 0;
}

function comment_uri($args = 0) {
Dries's avatar
 
Dries committed
229 230 231
  global $mod;
  if ($args) return ($mod) ? "module.php?mod=$mod;$args" : "node.php?$args";
  else return ($mod) ? "module.php?mod=$mod" : "node.php";
Dries's avatar
 
Dries committed
232 233 234
}

function comment_link($comment, $return = 1) {
Dries's avatar
 
Dries committed
235 236 237
  global $theme;
  if ($return) return "<A HREF=\"". comment_uri("id=$comment->lid#$comment->cid") ."\"><FONT COLOR=\"$theme->type\">". t("return") ."</FONT></A> | <A HREF=\"". comment_uri("op=reply&id=$comment->lid&pid=$comment->cid") ."\"><FONT COLOR=\"$theme->type\">". t("reply to this comment") ."</FONT></A>";
  else return "<A HREF=\"". comment_uri("op=reply&id=$comment->lid&pid=$comment->cid") ."\"><FONT COLOR=\"$theme->type\">". t("reply to this comment") ."</FONT></A>";
Dries's avatar
 
Dries committed
238 239
}

Dries's avatar
 
Dries committed
240
function comment_view($comment, $folded = 0) {
Dries's avatar
 
Dries committed
241
  global $theme;
Dries's avatar
 
Dries committed
242 243 244 245 246

  // calculate comment's score:
  $comment->score = comment_score($comment);

  // display comment:
Dries's avatar
 
Dries committed
247
  if ($folded) $theme->comment($comment, $folded);
Dries's avatar
 
Dries committed
248
  else print "<A HREF=\"". comment_uri("id=$comment->lid&cid=$comment->cid#$comment->cid") ."\">". check_output($comment->subject) ."</A> by ". format_username($comment->userid) ." <SMALL>($comment->score)</SMALL><P>";
Dries's avatar
 
Dries committed
249 250 251
}

function comment_thread_min($cid, $threshold) {
Dries's avatar
 
Dries committed
252
  global $user;
Dries's avatar
 
Dries committed
253

Dries's avatar
 
Dries committed
254
  $result = db_query("SELECT c.*, u.* FROM comments c LEFT JOIN users u ON c.author = u.id WHERE c.pid = '$cid' ORDER BY c.timestamp, c.cid");
Dries's avatar
 
Dries committed
255 256 257

  print "<UL>";
  while ($comment = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
258
    comment_view($comment);
Dries's avatar
 
Dries committed
259 260 261 262 263 264
    comment_thread_min($comment->cid, $threshold);
  }
  print "</UL>";
}

function comment_thread_max($cid, $mode, $threshold, $level = 0, $dummy = 0) {
Dries's avatar
 
Dries committed
265
  global $user;
Dries's avatar
 
Dries committed
266

Dries's avatar
 
Dries committed
267
  $result = db_query("SELECT c.*, u.* FROM comments c LEFT JOIN users u ON c.author = u.id WHERE c.pid = '$cid' ORDER BY c.timestamp, c.cid");
Dries's avatar
 
Dries committed
268 269 270

  print "<UL>";
  while ($comment = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
271
    comment_view($comment, (comment_visible($comment, $threshold) ? comment_link($comment, 0) : 0));
Dries's avatar
 
Dries committed
272 273 274 275 276 277
    comment_thread_max($comment->cid, $mode, $threshold, $level + 1, $dummy + 1);
  }
  print "</UL>";
}

function comment_render($lid, $cid) {
Dries's avatar
 
Dries committed
278
  global $theme, $REQUEST_URI, $user;
Dries's avatar
 
Dries committed
279 280

  // Pre-process variables:
Dries's avatar
 
Dries committed
281
  $lid = empty($lid) ? 0 : $lid;
Dries's avatar
 
Dries committed
282
  $cid = empty($cid) ? 0 : $cid;
Dries's avatar
 
Dries committed
283 284 285
  $mode  = ($user->id) ? $user->mode : variable_get(default_comment_mode, 4);
  $order = ($user->id) ? $user->sort : variable_get(default_comment_order, 1);
  $threshold = ($user->id) ? $user->threshold : variable_get(default_comment_threshold, 3);
Dries's avatar
 
Dries committed
286 287

  if ($user->id) {
Dries's avatar
 
Dries committed
288
    // Comment control:
Dries's avatar
 
Dries committed
289
    $theme->box(t("Comment control"), comment_controls($threshold, $mode, $order));
Dries's avatar
 
Dries committed
290

Dries's avatar
 
Dries committed
291 292 293 294 295
    // Print moderation form:
    print "<FORM METHOD=\"post\" ACTION=\"$REQUEST_URI\">\n";
  }

  if ($cid > 0) {
Dries's avatar
 
Dries committed
296
    $result = db_query("SELECT c.*, u.* FROM comments c LEFT JOIN users u ON c.author = u.id WHERE cid = '$cid'");
Dries's avatar
 
Dries committed
297
    if ($comment = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
298
      comment_view($comment, comment_link($comment));
Dries's avatar
 
Dries committed
299 300 301 302
    }
  }
  else {
    if ($mode == 1) {
Dries's avatar
 
Dries committed
303
      $result = comment_query($lid, $order);
Dries's avatar
 
Dries committed
304 305 306 307
      print "<TABLE BORDER=\"0\" CELLPADDING=\"2\" CELLSPACING=\"2\">\n";
      print " <TR><TH>Subject</TH><TH>Author</TH><TH>Date</TH><TH>Score</TH></TR>\n";
      while ($comment = db_fetch_object($result)) {
        if (comment_visible($comment, $threshold)) {
Dries's avatar
Dries committed
308
          print " <TR><TD><A HREF=\"". comment_uri("id=$comment->lid&cid=$comment->cid#$comment->cid") ."\">". check_output($comment->subject) ."</A></TD><TD>". format_username($comment->userid) ."</TD><TD>". format_date($comment->timestamp, "small") ."</TD><TD>". comment_score($comment) ."</TD></TR>\n";
Dries's avatar
 
Dries committed
309 310 311 312 313
        }
      }
      print "</TABLE>\n";
    }
    else if ($mode == 2) {
Dries's avatar
 
Dries committed
314
      $result = comment_query($lid, $order);
Dries's avatar
 
Dries committed
315
      while ($comment = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
316
        comment_view($comment, (comment_visible($comment, $threshold) ? comment_link($comment, 0) : 0));
Dries's avatar
 
Dries committed
317 318 319
      }
    }
    else if ($mode == 3) {
Dries's avatar
 
Dries committed
320
      $result = comment_query($lid, $order, 0);
Dries's avatar
 
Dries committed
321
      while ($comment = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
322
        comment_view($comment);
Dries's avatar
 
Dries committed
323 324 325 326
        comment_thread_min($comment->cid, $threshold);
      }
    }
    else {
Dries's avatar
 
Dries committed
327
      $result = comment_query($lid, $order, 0);
Dries's avatar
 
Dries committed
328
      while ($comment = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
329
        comment_view($comment, (comment_visible($comment, $threshold) ? comment_link($comment, 0) : 0));
Dries's avatar
 
Dries committed
330 331 332 333
        comment_thread_max($comment->cid, $mode, $threshold, $level + 1);
      }
    }
  }
Dries's avatar
 
Dries committed
334

Dries's avatar
 
Dries committed
335 336
  if ($user->id) {
    // Print moderation form:
Dries's avatar
 
Dries committed
337 338
    print "  <INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$lid\">\n";
    print "  <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Moderate comments") ."\">\n";
Dries's avatar
 
Dries committed
339 340 341 342
    print "</FORM>\n";
  }
}

Dries's avatar
 
Dries committed
343
?>