--- mailhandler/mailhandler.module Wed Nov 24 17:03:43 2004 +++ mailhandler.module Wed Dec 8 14:27:13 2004 @@ -96,7 +96,7 @@ function mailhandler_comment_submit($nod if (!$node->subject) $node->subject = $node->title; if (!$node->comment) $node->comment = $node->body; - list($error_title, $error_body) = comment_post(object2array($node)); + list($error_title, $error_body) = mailhandler_comment_post(object2array($node)); if ($error_body && $mailbox["replies"]) { // $fromaddress really refers to the mail header which is authoritative for authentication $fromaddress = mailhandler_get_fromaddress($header, $mailbox); @@ -107,6 +107,185 @@ function mailhandler_comment_submit($nod } } +// This is the same as in comment.module, but with the drupal_gotos removed to allow for auto-functioning (e.g. run by cron) +function mailhandler_comment_post($edit) { + global $user; + + if (user_access('post comments') && node_comment_mode($edit['nid']) == 2) { + + // Validate the comment's subject. If not specified, extract + // one from the comment's body. + $edit['subject'] = strip_tags($edit['subject']); + + if ($edit['subject'] == '') { + $edit['subject'] = truncate_utf8(strip_tags($edit['comment']), 29); + } + + if (!form_get_errors()) { + // Check for duplicate comments. Note that we have to use the + // validated/filtered data to perform such check. + + $duplicate = db_result(db_query("SELECT COUNT(cid) FROM {comments} WHERE pid = %d AND nid = %d AND subject = '%s' AND comment = '%s'", $edit["pid"], $edit["nid"], $edit['subject'], $edit['comment']), 0); + if ($duplicate != 0) { + watchdog('warning', t('Comment: duplicate %subject.', array('%subject' => ''. $edit["subject"] .''))); + } + + if ($edit["cid"]) { + // Update the comment in the database. Note that the update + // query will fail if the comment isn't owned by the current + // user. + db_query("UPDATE {comments} SET subject = '%s', comment = '%s', format = '%s' WHERE cid = %d AND uid = '$user->uid'", $edit['subject'], $edit['comment'], $edit['format'], $edit["cid"]); + + _comment_update_node_statistics($edit['nid']); + + // Allow modules to respond to the updating of a comment. + module_invoke_all('comment', 'update', $edit); + + // Add entry to the watchdog log. + watchdog('special', t('Comment: updated %subject.', array('%subject' => ''. $edit['subject'] .'')), l(t('view'), 'node/'. $edit['nid'], NULL, NULL, 'comment-'. $edit['cid'])); + } + else { + // Add the comment to database. + $status = user_access('post comments without approval') ? 0 : 1; + $roles = variable_get('comment_roles', array()); + $score = 0; + + foreach (array_intersect(array_keys($roles), array_keys($user->roles)) as $rid) { + $score = max($roles[$rid], $score); + } + + $users = serialize(array(0 => $score)); + + /* + ** Here we are building the thread field. See the comment + ** in comment_render(). + */ + + if ($edit['pid'] == 0) { + /* + ** This is a comment with no parent comment (depth 0): we start + ** by retrieving the maximum thread level. + */ + + $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $edit['nid'])); + + // Strip the "/" from the end of the thread. + $max = rtrim($max, '/'); + + /* + ** Next, we increase this value by one. Note that we can't + ** use 1, 2, 3, ... 9, 10, 11 because we order by string and + ** 10 would be right after 1. We use 1, 2, 3, ..., 9, 91, + ** 92, 93, ... instead. Ugly but fast. + */ + + $decimals = (string)substr($max, 0, strlen($max) - 1); + $units = substr($max, -1, 1); + if ($units) { + $units++; + } + else { + $units = 1; + } + + if ($units == 10) { + $units = '90'; + } + + // Finally, build the thread field for this new comment. + $thread = "$decimals$units/"; + } + else { + /* + ** This is comment with a parent comment: we increase + ** the part of the thread value at the proper depth. + */ + + // Get the parent comment: + $parent = db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $edit['pid'])); + + // Strip the "/" from the end of the parent thread. + $parent->thread = (string)rtrim((string)$parent->thread, '/'); + + // Get the max value in _this_ thread. + $max = db_result(db_query("SELECT MAX(thread) FROM {comments} WHERE thread LIKE '%s.%%' AND nid = %d", $parent->thread, $edit['nid'])); + + if ($max == '') { + // First child of this parent. + $thread = "$parent->thread.1/"; + } + else { + // Strip the "/" at the end of the thread. + $max = rtrim($max, '/'); + + // We need to get the value at the correct depth. + $parts = explode('.', $max); + $parent_depth = count(explode('.', $parent->thread)); + $last = $parts[$parent_depth]; + + /* + ** Next, we increase this value by one. Note that we can't + ** use 1, 2, 3, ... 9, 10, 11 because we order by string and + ** 10 would be right after 1. We use 1, 2, 3, ..., 9, 91, + ** 92, 93, ... instead. Ugly but fast. + */ + + $decimals = (string)substr($last, 0, strlen($last) - 1); + $units = substr($last, -1, 1); + $units++; + if ($units == 10) { + $units = '90'; + } + + // Finally, build the thread field for this new comment. + $thread = "$parent->thread.". $decimals.$units .'/'; + } + } + + + $edit["cid"] = db_next_id("{comments}_cid"); + $edit['timestamp'] = time(); + + if ($edit['uid'] = $user->uid) { + $edit['name'] = $user->name; + } + + + db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (%d, %d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", $edit["cid"], $edit["nid"], $edit["pid"], $edit['uid'], $edit['subject'], $edit['comment'], $edit['format'], $_SERVER['REMOTE_ADDR'], $edit['timestamp'], $status, $score, $users, $thread, $edit["name"], $edit['mail'], $edit["homepage"]); + + _comment_update_node_statistics($edit['nid']); + + // Tell the other modules a new comment has been submitted. + module_invoke_all('comment', 'insert', $edit); + + // Add an entry to the watchdog log. + watchdog('special', t('Comment: added %subject.', array('%subject' => ''. $edit['subject'] .'')), l(t('view'), 'node/'. $edit['nid'], NULL, NULL, 'comment-'. $edit['cid'])); + } + + // Clear the cache so an anonymous user can see his comment being added. + cache_clear_all(); + + // Explain the moderation queue if necessary, and then + // redirect the user to the node he's commenting on. + if ($status == 1) { + drupal_set_message(t('Your comment has been queued for moderation by site administrators and will be published after approval.')); + //drupal_goto('node/'. $edit['nid']); + //scratched for auto-compatibility + } + else { + //drupal_goto('node/'. $edit['nid'] .'#comment-'. $edit['cid']); + //scratched for auto-compatibility + } + } + else { + print theme('page', comment_preview($edit)); + } + } + else { + watchdog('error', t('Comment: unauthorized comment submitted or comment submitted to a closed node %subject.', array('%subject' => ''. $edit['subject'] .''))); + } +} + /** * Create the node. */ @@ -532,7 +711,7 @@ function mailhandler_save_mailbox($edit) } else { if ($edit["mid"]) { - db_query("UPDATE {mailhandler} SET mail = '%s', mailto = '%s', domain = '%s', port = '%s', folder = '%s', name = '%s', pass = '%s', mime = '%s', imap = '%s', security = '%s', replies = '%s', fromheader = '%s', commands = '%s', sigseparator = '%s', enabled = '%s' delete_after_read = '%d' WHERE mid = '%s'", $edit["mail"], $edit["mailto"], $edit["domain"], $edit["port"], $edit["folder"], $edit["name"], $edit["pass"], $edit["mime"], $edit["imap"], $edit["security"], $edit["replies"], $edit["fromheader"], $edit["commands"], $edit["sigseparator"], $edit["enabled"], $edit['delete_after_read'], $edit["mid"]); + db_query("UPDATE {mailhandler} SET mail = '%s', mailto = '%s', domain = '%s', port = '%s', folder = '%s', name = '%s', pass = '%s', mime = '%s', imap = '%s', security = '%s', replies = '%s', fromheader = '%s', commands = '%s', sigseparator = '%s', enabled = '%s', delete_after_read = '%d' WHERE mid = '%s'", $edit["mail"], $edit["mailto"], $edit["domain"], $edit["port"], $edit["folder"], $edit["name"], $edit["pass"], $edit["mime"], $edit["imap"], $edit["security"], $edit["replies"], $edit["fromheader"], $edit["commands"], $edit["sigseparator"], $edit["enabled"], $edit['delete_after_read'], $edit["mid"]); drupal_set_message(t("Mailbox updated")); } else {