"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_input($id); $vote = check_input($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_reply($pid, $id) {
global $REQUEST_URI, $theme, $user;
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"));
}
else $pid = 0;
// Build reply form:
$output .= "
\n";
$theme->box(t("Reply"), $output);
}
function comment_preview($pid, $id, $subject, $comment) {
global $REQUEST_URI, $theme, $user;
// Preview comment:
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"));
// Build reply form:
$output .= "\n";
// Name field:
$output .= "". t("Your name") .": \n";
$output .= format_username($user->userid) ."\n";
// Subject field:
$output .= "". t("Subject") .": \n";
$output .= "
\n";
// Comment field:
$output .= "". t("Comment") .": \n";
$output .= "". check_form($comment) ." \n";
$output .= "". t("Allowed HTML tags") .": ". htmlspecialchars(variable_get("allowed_html", "")) .".
\n";
// Hidden fields:
$output .= " \n";
$output .= " \n";
if (empty($subject)) {
$output .= "". t("Warning: you did not supply a subject.") ."
\n";
}
// Preview and submit button:
$output .= " \n";
$output .= " \n";
$output .= "
\n";
$theme->box(t("Reply"), $output);
}
function comment_post($pid, $id, $subject, $comment) {
global $theme, $user;
// 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 = '$pid' AND lid = '$id' AND subject = '$subject' AND comment = '$comment'"), 0);
if ($duplicate != 0) {
watchdog("warning", "comment: duplicate '$subject'");
}
else {
// validate subject:
$subject = ($subject) ? $subject : substr($comment, 0, 29);
// add watchdog entry:
watchdog("special", "comment: added '$subject'");
// add comment to database:
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) ."')");
}
}
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 .= " ";
}
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 .= " $key \n";
$output .= "cid]\">$options \n";
}
else {
// comment has already been moderated:
$output .= "". t("score") .": ". check_output($comment->score) ." ". t("votes") .": ". check_output($comment->votes) ."
\n";
}
return $output;
}
function comment_controls($threshold = 1, $mode = 3, $order = 1) {
global $REQUEST_URI, $user;
$output .= "\n";
$output .= "
\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 .= " \n";
$output .= " \n";
$output .= " \n";
$output .= "\n";
return $output;
}
function comment_threshold($threshold) {
for ($i = -1; $i < 6; $i++) $options .= " ". t("Filter") ." - $i ";
return "$options \n";
}
function comment_mode($mode) {
global $cmodes;
foreach ($cmodes as $key=>$value) $options .= " $value \n";
return "$options \n";
}
function comment_order($order) {
global $corder;
foreach ($corder as $key=>$value) $options .= " $value \n";
return "$options \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_link($comment, $return = 1) {
global $theme;
if ($return) return "lid#$comment->cid") ."\">type\">". t("return") ." | lid&pid=$comment->cid") ."\">type\">". t("reply to this comment") ." ";
else return "lid&pid=$comment->cid") ."\">type\">". t("reply to this comment") ." ";
}
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 "lid&cid=$comment->cid#$comment->cid") ."\">". check_output($comment->subject) ." by ". format_username($comment->userid) ." ($comment->score) ";
}
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");
print "
";
while ($comment = db_fetch_object($result)) {
comment_view($comment);
comment_thread_min($comment->cid, $threshold);
}
print " ";
}
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");
print "";
while ($comment = db_fetch_object($result)) {
comment_view($comment, (comment_visible($comment, $threshold) ? comment_link($comment, 0) : 0));
comment_thread_max($comment->cid, $mode, $threshold, $level + 1, $dummy + 1);
}
print " ";
}
function comment_render($lid, $cid) {
global $theme, $REQUEST_URI, $user;
// 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 "\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_link($comment));
}
}
else {
if ($mode == 1) {
$result = comment_query($lid, $order);
print "\n";
}
else if ($mode == 2) {
$result = comment_query($lid, $order);
while ($comment = db_fetch_object($result)) {
comment_view($comment, (comment_visible($comment, $threshold) ? comment_link($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_link($comment, 0) : 0));
comment_thread_max($comment->cid, $mode, $threshold, $level + 1);
}
}
}
if ($user->id) {
// Print moderation form:
print " \n";
print " \n";
print " \n";
}
}
?>