drupal/includes/comment.inc

332 lines
13 KiB
PHP
Raw Normal View History

<?php
// Security check:
if (strstr($id, " ") || strstr($pid, " ") || strstr($lid, " ") || strstr($mode, " ") || strstr($order, " ") || strstr($threshold, " ")) {
2001-01-20 12:58:47 +00:00
watchdog("error", "comment: attempt to provide malicious input through URI");
exit();
}
$cmodes = array(1 => "List - min", 2 => "List - max", 3 => "Threaded - min", 4 => "Threaded - max");
$corder = array(1 => "Date - new", 2 => "Date - old", 3 => "Rate - high", 4 => "Rate - low");
class Comment {
function Comment($userid, $subject, $comment, $timestamp, $url, $fake_email, $score, $votes, $cid, $lid) {
$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;
$this->lid = $lid;
}
}
function comment_moderate($moderate) {
global $user, $comment_votes;
if ($user->id && $moderate) {
$none = $comment_votes[key($comment_votes)];
foreach ($moderate as $id=>$vote) {
if ($vote != $comment_votes[$none]) {
$id = check_output($id);
$vote = check_output($vote);
$comment = db_fetch_object(db_query("SELECT * FROM comments WHERE cid = '$id'"));
if ($comment && !field_get($comment->users, $user->userid)) {
$result = db_query("UPDATE comments SET score = score $vote, votes = votes + 1, users = '". field_set($comment->users, $user->userid, $vote) ."' WHERE cid = '$id'");
}
}
}
}
}
function comment_settings($mode, $order, $threshold) {
global $user;
if ($user->id) $user = user_save($user, array("mode" => $mode, "sort" => $order, "threshold" => $threshold));
}
function comment_form($edit) {
global $REQUEST_URI, $user;
// name field:
$form .= form_item(t("Your name"), format_username($user->userid));
// subject field:
$form .= form_textfield(t("Subject"), "subject", $edit[subject], 50, 64);
// comment field:
$form .= form_textarea(t("Comment"), "comment", $edit[comment] ? $edit[comment] : $user->signature, 70, 10, t("Allowed HTML tags") .": ". htmlspecialchars(variable_get("allowed_html", "")));
// preview button:
$form .= form_hidden("pid", $edit[pid]);
$form .= form_hidden("id", $edit[id]);
if (!$edit[comment]) {
$form .= form_submit(t("Preview comment"));
}
else {
$form .= form_submit(t("Preview comment"));
$form .= form_submit(t("Post comment"));
}
Oops, a rather large commit: - Changed meta.module, node.module and index.php to use comma-seperated lists of attributes rather then "foo=a,bar=b" lists. This makes it a a lot easier to use both modules. In addition, error handling can be discarded as it can't be made any simpler, really ... It fits rather nicely in Drupal's design so I'm getting more and more happy with this meta.module (but we are not 100% there yet). - node.module, node.inc: + Improved the node-related admin interface so that navigating back and forth the administrative menus is made both easier and faster. + Removed some redundant database fields from the node table. See 2.00-to-x.xx.sql! + Added 2 news hooks called "node_insert" and "node_update". Just like this is the case with the existing hook "node_delete" these new hooks will automatically get called when a node has been inserted or udpated. Note that this is an optional call-back that only needs to be implemented when required. With the addition of these two hooks, the node mechanism (version 1) is pretty well completed. - watchdog.module: + Fixed bug whit the 'regular messages' query in the watchdog.module. - book.module: + Fixed bug in book.module: the 'parent' was not set properly when updating a book page. + Made it so that older versions of a book page are automatically reactived upon deletion of the most recent version, i.e. when doing a version roll-back. - comment.inc: + Undid Remco's patch to comment.inc; it does not work in some cases. - conf.module: + Fine-tuned some of the options in conf.module a bit. - marvin.theme: + Visual changes to make it look better on Windows browsers. Mind to give some feedback on this? + Fixed 3 HTML typos/bugs. + XHTML-ified the theme at a best effort basis; I didn't carry the XHTML specification with me. + Made use of the theme_slogan variable to display the site's slogan. + As soon we have at least one valid XHTML theme we can wonder on how to integrate other XML namespaces (cfr. MathML story at drop.org). - database.mysql: + Updated database.mysql so that it contains all the latest "database patches".
2001-06-17 18:31:25 +00:00
return form($REQUEST_URI, $form);
}
function comment_reply($pid, $id) {
global $theme;
if ($pid) {
$item = db_fetch_object(db_query("SELECT comments.*, users.userid FROM comments LEFT JOIN users ON comments.author = users.id WHERE comments.cid = '$pid'"));
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"));
}
This a rather large commit that needs a lot of fine-tuning. If you update, you'll break your site as you need switching from structure to index.module: so this can be considered an intermediate commit. If you upgrade, and you are welcome to, just create a collection called "section" (for now) and assign your nodes some attributes in the described format. Feedback and bugreports are welcomed. Questions will be answered. CHANGES: - comment system: + when replying to a node (rather then to a comment), that node is displayed above the reply form. + when replying to a comment (rather then to a node), that comment is displayd above the reply form. - removed structure.inc, removed structure.module. - node.inc: + added 2 new node functions called 'node_attribute_edit()' and 'node_attribute_save()' used to 'hook in' any indexing system including your home-brewed stuff if you'd want to. Currently, index.module is the facto default index system. See story.module for usage. - book.module, story.module, poll.module, page.module, forum.module: + added preview functionality to administration section (via node module). + removed all references to structure.inc (category, topic). - moderate.module: + removed all references to structure.inc (category, topic). - book.module, story.module, page.module, forum.module: + increased the sizes of some textareas. - submit.php: + removed all references to structure.inc (category, topic). - marvin.theme: + removed dead code: function story() was depricated. - unconed.theme: + removed hardcoded references to drop.org. - marvin.theme, unconed.theme, jeroen.theme, yaroon.theme, example.theme: + removed all references to structure.inc (category, topic). TODO: - file.module, trip_link.module: + update preview functionality: see story.module for example. + remove references to 'cid' and 'tid', use 'attribute' instead: see story.module for example. - extend and build upon index.module as well as making it configurable
2001-06-10 15:01:20 +00:00
else {
node_view(node_get_object(array("nid" => $id)));
$pid = 0;
}
if (user_access("post comments")) {
$theme->box(t("Reply"), comment_form(array(pid=>$pid, id=>$id)));
}
else {
$theme->box(t("Reply"), t("You are not authorized to post comments."));
}
}
function comment_preview($edit) {
global $REQUEST_URI, $theme, $user;
// Preview comment:
comment_view(new Comment($user->userid, check_preview($edit[subject]), check_preview($edit[comment]), time(), check_preview($user->url), check_preview($user->fake_email), 0, 0, 0, 0), t("reply to this comment"));
$theme->box(t("Reply"), comment_form($edit));
}
function comment_post($edit) {
global $theme, $user;
if (user_access("post comments")) {
// check comment submission rate:
throttle("post comment", variable_get(max_comment_rate, 60));
// check for duplicate comments:
$duplicate = db_result(db_query("SELECT COUNT(cid) FROM comments WHERE pid = '". check_input($edit[pid]) ."' AND lid = '". check_input($edit[id]) ."' AND subject = '". check_input($edit[subject]) ."' AND comment = '". check_input($edit[comment]) ."'"), 0);
if ($duplicate != 0) {
watchdog("warning", "comment: duplicate '$edit[subject]'");
}
else {
// validate subject:
$edit[subject] = $edit[subject] ? $edit[subject] : substr($edit[comment], 0, 29);
// add watchdog entry:
watchdog("special", "comment: added '$edit[subject]'");
// add comment to database:
db_query("INSERT INTO comments (lid, pid, author, subject, comment, hostname, timestamp, score) VALUES ('". check_input($edit[id]) ."', '". check_input($edit[pid]) ."', '$user->id', '". check_input($edit[subject]) ."', '". check_input($edit[comment]) ."', '". getenv("REMOTE_ADDR") ."', '". time() ."', '". ($user->userid ? 1 : 0) ."')");
2001-06-30 20:23:33 +00:00
// clear cache:
cache_clear();
}
}
}
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) {
$result = db_query("SELECT COUNT(cid) FROM comments WHERE pid = '$id'");
return ($result) ? db_result($result, 0) : 0;
}
function comment_moderation($comment) {
global $comment_votes, $op, $user;
if ($op == "reply") {
// preview comment:
$output .= "&nbsp;";
}
else if ($user->id && $user->userid != $comment->userid && !field_get($comment->users, $user->userid)) {
// 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";
}
else {
// comment has already been moderated:
$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) ."</TD></TR></TABLE>\n";
}
return $output;
}
function comment_controls($threshold = 1, $mode = 3, $order = 1) {
global $REQUEST_URI, $user;
$output .= "<DIV ALIGN=\"CENTER\">\n";
$output .= "<FORM METHOD=\"post\" ACTION=\"$REQUEST_URI\">\n";
$output .= comment_mode(($user->id ? $user->mode : $mode));
$output .= comment_order(($user->id ? $user->sort : $order));
$output .= comment_threshold(($user->id ? $user->threshold : $threshold));
$output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Update settings") ."\">\n";
$output .= "<INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Add comment") ."\">\n";
$output .= "</FORM>\n";
$output .= "</DIV>\n";
return $output;
}
function comment_threshold($threshold) {
for ($i = -1; $i < 6; $i++) $options .= " <OPTION VALUE=\"$i\"". ($threshold == $i ? " SELECTED" : "") .">". t("Filter") ." - $i</OPTION>";
return "<SELECT NAME=\"threshold\">$options</SELECT>\n";
}
function comment_mode($mode) {
global $cmodes;
foreach ($cmodes as $key=>$value) $options .= " <OPTION VALUE=\"$key\"". ($mode == $key ? " SELECTED" : "") .">$value</OPTION>\n";
return "<SELECT NAME=\"mode\">$options</SELECT>\n";
}
function comment_order($order) {
global $corder;
foreach ($corder as $key=>$value) $options .= " <OPTION VALUE=\"$key\"". ($order == $key ? " SELECTED" : "") .">$value</OPTION>\n";
return "<SELECT NAME=\"order\">$options</SELECT>\n";
}
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'";
if ($pid >= 0) $query .= " AND pid = '$pid'";
if ($order == 1) $query .= " ORDER BY c.timestamp DESC";
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";
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) {
global $mod;
if ($args) return ($mod) ? "module.php?mod=$mod;$args" : "node.php?$args";
else return ($mod) ? "module.php?mod=$mod" : "node.php";
}
function comment_links($comment, $return = 1) {
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>";
}
function comment_view($comment, $folded = 0) {
global $theme;
// calculate comment's score:
$comment->score = comment_score($comment);
// display comment:
if ($folded) $theme->comment($comment, $folded);
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>";
}
function comment_thread_min($cid, $threshold) {
global $user;
$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");
while ($comment = db_fetch_object($result)) {
print "<UL>";
Oops, a rather large commit: - Changed meta.module, node.module and index.php to use comma-seperated lists of attributes rather then "foo=a,bar=b" lists. This makes it a a lot easier to use both modules. In addition, error handling can be discarded as it can't be made any simpler, really ... It fits rather nicely in Drupal's design so I'm getting more and more happy with this meta.module (but we are not 100% there yet). - node.module, node.inc: + Improved the node-related admin interface so that navigating back and forth the administrative menus is made both easier and faster. + Removed some redundant database fields from the node table. See 2.00-to-x.xx.sql! + Added 2 news hooks called "node_insert" and "node_update". Just like this is the case with the existing hook "node_delete" these new hooks will automatically get called when a node has been inserted or udpated. Note that this is an optional call-back that only needs to be implemented when required. With the addition of these two hooks, the node mechanism (version 1) is pretty well completed. - watchdog.module: + Fixed bug whit the 'regular messages' query in the watchdog.module. - book.module: + Fixed bug in book.module: the 'parent' was not set properly when updating a book page. + Made it so that older versions of a book page are automatically reactived upon deletion of the most recent version, i.e. when doing a version roll-back. - comment.inc: + Undid Remco's patch to comment.inc; it does not work in some cases. - conf.module: + Fine-tuned some of the options in conf.module a bit. - marvin.theme: + Visual changes to make it look better on Windows browsers. Mind to give some feedback on this? + Fixed 3 HTML typos/bugs. + XHTML-ified the theme at a best effort basis; I didn't carry the XHTML specification with me. + Made use of the theme_slogan variable to display the site's slogan. + As soon we have at least one valid XHTML theme we can wonder on how to integrate other XML namespaces (cfr. MathML story at drop.org). - database.mysql: + Updated database.mysql so that it contains all the latest "database patches".
2001-06-17 18:31:25 +00:00
print comment_view($comment);
comment_thread_min($comment->cid, $threshold);
print "</UL>";
}
}
function comment_thread_max($cid, $mode, $threshold, $level = 0, $dummy = 0) {
global $user;
$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");
while ($comment = db_fetch_object($result)) {
print "<UL>";
comment_view($comment, (comment_visible($comment, $threshold) ? comment_links($comment, 0) : 0));
comment_thread_max($comment->cid, $mode, $threshold, $level + 1, $dummy + 1);
print "</UL>";
}
}
function comment_render($lid, $cid) {
global $user, $theme, $REQUEST_URI;
if (user_access("access comments")) {
// Pre-process variables:
$lid = empty($lid) ? 0 : $lid;
$cid = empty($cid) ? 0 : $cid;
$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);
if ($user->id) {
// Comment control:
$theme->box(t("Comment control"), comment_controls($threshold, $mode, $order));
// Print moderation form:
print "<FORM METHOD=\"post\" ACTION=\"$REQUEST_URI\">\n";
}
if ($cid > 0) {
$result = db_query("SELECT c.*, u.* FROM comments c LEFT JOIN users u ON c.author = u.id WHERE cid = '$cid'");
if ($comment = db_fetch_object($result)) {
comment_view($comment, comment_links($comment));
}
}
else {
if ($mode == 1) {
$result = comment_query($lid, $order);
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)) {
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";
}
}
print "</TABLE>\n";
}
else if ($mode == 2) {
$result = comment_query($lid, $order);
while ($comment = db_fetch_object($result)) {
comment_view($comment, (comment_visible($comment, $threshold) ? comment_links($comment, 0) : 0));
}
}
else if ($mode == 3) {
$result = comment_query($lid, $order, 0);
while ($comment = db_fetch_object($result)) {
comment_view($comment);
comment_thread_min($comment->cid, $threshold);
}
}
else {
$result = comment_query($lid, $order, 0);
while ($comment = db_fetch_object($result)) {
comment_view($comment, (comment_visible($comment, $threshold) ? comment_links($comment, 0) : 0));
comment_thread_max($comment->cid, $mode, $threshold, $level + 1);
}
}
}
if ($user->id) {
// Print moderation form:
print " <INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"$lid\">\n";
print " <INPUT TYPE=\"submit\" NAME=\"op\" VALUE=\"". t("Moderate comments") ."\">\n";
print "</FORM>\n";
}
}
}
?>