* When enabled, the Drupal comment module creates a discussion
* board for each Drupal node. Users can post comments to discuss
* a forum topic, weblog post, story, collaborative book page, etc.
*/
/**
* Comment is published.
*/
define('COMMENT_PUBLISHED', 0);
/**
* Comment is awaiting approval.
*/
define('COMMENT_NOT_PUBLISHED', 1);
/**
* Comments are displayed in a flat list - collapsed.
*/
define('COMMENT_MODE_FLAT_COLLAPSED', 1);
/**
* Comments are displayed in a flat list - expanded.
*/
define('COMMENT_MODE_FLAT_EXPANDED', 2);
/**
* Comments are displayed as a threaded list - collapsed.
*/
define('COMMENT_MODE_THREADED_COLLAPSED', 3);
/**
* Comments are displayed as a threaded list - expanded.
*/
define('COMMENT_MODE_THREADED_EXPANDED', 4);
/**
* Comments are ordered by date - newest first.
*/
define('COMMENT_ORDER_NEWEST_FIRST', 1);
/**
* Comments are ordered by date - oldest first.
*/
define('COMMENT_ORDER_OLDEST_FIRST', 2);
/**
* Comment controls should be shown above the comment list.
*/
define('COMMENT_CONTROLS_ABOVE', 0);
/**
* Comment controls should be shown below the comment list.
*/
define('COMMENT_CONTROLS_BELOW', 1);
/**
* Comment controls should be shown both above and below the comment list.
*/
define('COMMENT_CONTROLS_ABOVE_BELOW', 2);
/**
* Comment controls are hidden.
*/
define('COMMENT_CONTROLS_HIDDEN', 3);
/**
* Anonymous posters may not enter their contact information.
*/
define('COMMENT_ANONYMOUS_MAYNOT_CONTACT', 0);
/**
* Anonymous posters may leave their contact information.
*/
define('COMMENT_ANONYMOUS_MAY_CONTACT', 1);
/**
* Anonymous posters must leave their contact information.
*/
define('COMMENT_ANONYMOUS_MUST_CONTACT', 2);
/**
* Comment form should be displayed on a separate page.
*/
define('COMMENT_FORM_SEPARATE_PAGE', 0);
/**
* Comment form should be shown below post or list of comments.
*/
define('COMMENT_FORM_BELOW', 1);
/**
* Comments for this node are disabled.
*/
define('COMMENT_NODE_DISABLED', 0);
/**
* Comments for this node are locked.
*/
define('COMMENT_NODE_READ_ONLY', 1);
/**
* Comments are enabled on this node.
*/
define('COMMENT_NODE_READ_WRITE', 2);
/**
* Comment preview is optional.
*/
define('COMMENT_PREVIEW_OPTIONAL', 0);
/**
* Comment preview is required.
*/
define('COMMENT_PREVIEW_REQUIRED', 1);
/**
* Implementation of hook_help().
*/
function comment_help($path, $arg) {
switch ($path) {
case 'admin/help#comment':
$output = '<p>'. t('The comment module allows visitors to comment on your posts, creating ad hoc discussion boards. Any <a href="@content-type">content type</a> may have its <em>Default comment setting</em> set to <em>Read/Write</em> to allow comments, or <em>Disabled</em>, to prevent comments. Comment display settings and other controls may also be customized for each content type (some display settings are customizable by individual users).', array('@content-type' => url('admin/content/types'))) .'</p>';
$output .= '<p>'. t('Comment permissions are assigned to user roles, and are used to determine whether anonymous users (or other roles) are allowed to comment on posts. If anonymous users are allowed to comment, their individual contact information may be retained in cookies stored on their local computer for use in later comment submissions. When a comment has no replies, it may be (optionally) edited by its author. The comment module uses the same input formats and HTML tags available when creating other forms of content.') .'</p>';
$output .= '<p>'. t('For more information, see the online handbook entry for <a href="@comment">Comment module</a>.', array('@comment' => 'http://drupal.org/handbook/modules/comment/')) .'</p>';
return $output;
case 'admin/content/comment':
return '<p>'. t("Below is a list of the latest comments posted to your site. Click on a subject to see the comment, the author's name to edit the author's user information, 'edit' to modify the text, and 'delete' to remove their submission.") .'</p>';
case 'admin/content/comment/approval':
return '<p>'. t("Below is a list of the comments posted to your site that need approval. To approve a comment, click on 'edit' and then change its 'moderation status' to Approved. Click on a subject to see the comment, the author's name to edit the author's user information, 'edit' to modify the text, and 'delete' to remove their submission.") .'</p>';
* Generates a block with the most recent comments.
*/
function comment_block($op = 'list', $delta = 0) {
if ($op == 'list') {
$blocks[0]['info'] = t('Recent comments');
return $blocks;
}
else if ($op == 'view' && user_access('access comments')) {
$block['subject'] = t('Recent comments');
$block['content'] = theme('comment_block');
return $block;
}
}
/**
* Find a number of recent comments. This is done in two steps.
* 1. Find the n (specified by $number) nodes that have the most recent
* comments. This is done by querying node_comment_statistics which has
* an index on last_comment_timestamp, and is thus a fast query.
* 2. Loading the information from the comments table based on the nids found
* in step 1.
*
* @param $number
* (optional) The maximum number of comments to find.
* @return
* An array of comment objects each containing a nid,
* subject, cid, and timestamp, or an empty array if there are no recent
* comments visible to the current user.
*/
function comment_get_recent($number = 10) {
// Select the $number nodes (visible to the current user) with the most
// recent comments. This is efficient due to the index on
// last_comment_timestamp.
$result = db_query_range(db_rewrite_sql("SELECT nc.nid FROM {node_comment_statistics} nc WHERE nc.comment_count > 0 ORDER BY nc.last_comment_timestamp DESC", 'nc'), 0, $number);
$nids = array();
while ($row = db_fetch_object($result)) {
$nids[] = $row->nid;
}
$comments = array();
if (!empty($nids)) {
// From among the comments on the nodes selected in the first query,
// find the $number most recent comments.
$result = db_query_range('SELECT c.nid, c.subject, c.cid, c.timestamp FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE c.nid IN ('. implode(',', $nids) .') AND n.status = 1 AND c.status = %d ORDER BY c.cid DESC', COMMENT_PUBLISHED, 0, $number);
while ($comment = db_fetch_object($result)) {
$comments[] = $comment;
}
}
return $comments;
}
/**
* Calculate page number for first new comment.
*
* @param $num_comments
* Number of comments.
* @param $new_replies
* Number of new replies.
* @param $node
* The first new comment node.
* @return
* "page=X" if the page number is greater than zero; empty string otherwise.
*/
function comment_new_page_count($num_comments, $new_replies, $node) {
// Only one page of comments or flat forum and newest first.
// First new comment will always be on first page.
$pageno = 0;
}
else {
if ($flat) {
// Flat comments and oldest first.
$count = $num_comments - $new_replies;
}
else {
// Threaded comments. See the documentation for comment_render().
if ($order == COMMENT_ORDER_NEWEST_FIRST) {
// Newest first: find the last thread with new comment
$result = db_query('(SELECT thread FROM {comments} WHERE nid = %d AND status = 0 ORDER BY timestamp DESC LIMIT %d) ORDER BY thread DESC LIMIT 1', $node->nid, $new_replies);
$thread = db_result($result);
$result_count = db_query("SELECT COUNT(*) FROM {comments} WHERE nid = %d AND status = 0 AND thread > '". $thread ."'", $node->nid);
}
else {
// Oldest first: find the first thread with new comment
$result = db_query('(SELECT thread FROM {comments} WHERE nid = %d AND status = 0 ORDER BY timestamp DESC LIMIT %d) ORDER BY SUBSTRING(thread, 1, (LENGTH(thread) - 1)) LIMIT 1', $node->nid, $new_replies);
$thread = substr(db_result($result), 0, -1);
$result_count = db_query("SELECT COUNT(*) FROM {comments} WHERE nid = %d AND status = 0 AND SUBSTRING(thread, 1, (LENGTH(thread) - 1)) < '". $thread ."'", $node->nid);
}
$count = db_result($result_count);
}
$pageno = $count / $comments_per_page;
}
if ($pageno >= 1) {
$pagenum = "page=". intval($pageno);
}
return $pagenum;
}
/**
* Returns a formatted list of recent comments to be displayed in the comment block.
'#description' => t('The default sorting for new users and anonymous users while viewing comments. These users may change their view using the comment control panel. For registered users, this change is remembered as a persistent user preference.'),
'#description' => t('Position of the comment controls box. The comment controls let the user change the default display mode and display order of comments.'),
COMMENT_ANONYMOUS_MAYNOT_CONTACT => t('Anonymous posters may not enter their contact information'),
COMMENT_ANONYMOUS_MAY_CONTACT => t('Anonymous posters may leave their contact information'),
COMMENT_ANONYMOUS_MUST_CONTACT => t('Anonymous posters must leave their contact information')),
'#description' => t('This option is enabled when anonymous users have permission to post comments on the <a href="@url">permissions page</a>.', array('@url' => url('admin/user/permissions', array('fragment' => 'module-comment')))),
);
if (!user_access('post comments', drupal_anonymous_user())) {
return db_fetch_array(db_query("SELECT last_comment_timestamp, last_comment_name, comment_count FROM {node_comment_statistics} WHERE nid = %d", $node->nid));
// Clear the cache so an anonymous user can see his comment being added.
cache_clear_all();
// Explain the approval queue if necessary, and then
// redirect the user to the node he's commenting on.
if ($edit['status'] == COMMENT_NOT_PUBLISHED) {
drupal_set_message(t('Your comment has been queued for moderation by site administrators and will be published after approval.'));
}
else {
comment_invoke_comment($edit, 'publish');
}
return $edit['cid'];
}
else {
return FALSE;
}
}
else {
watchdog('content', 'Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $edit['subject']), WATCHDOG_WARNING);
drupal_set_message(t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $edit['subject'])), 'error');
return FALSE;
}
}
/**
* Build command links for a comment (e.g.\ edit, reply, delete) with respect to the current user's access permissions.
*
* @param $comment
* The comment to which the links will be related.
* @param $return
* Not used.
* @return
* An associative array containing the links.
*/
function comment_links($comment, $return = 1) {
global $user;
$links = array();
// If we are viewing just this comment, we link back to the node.
if ($return) {
$links['comment_parent'] = array(
'title' => t('parent'),
'href' => comment_node_url(),
'fragment' => "comment-$comment->cid"
);
}
if (node_comment_mode($comment->nid) == COMMENT_NODE_READ_WRITE) {
if (user_access('administer comments') && user_access('post comments')) {
// Use the timestamp to retrieve the number of new comments.
$result = db_result(db_query('SELECT COUNT(c.cid) FROM {node} n INNER JOIN {comments} c ON n.nid = c.nid WHERE n.nid = %d AND timestamp > %d AND c.status = %d', $nid, $timestamp, COMMENT_PUBLISHED));
return $result;
}
else {
return 0;
}
}
/**
* Validate comment data.
*
* @param $edit
* An associative array containig the comment data.
* @return
* The original $edit.
*/
function comment_validate($edit) {
global $user;
// Invoke other validation handlers
comment_invoke_comment($edit, 'validate');
if (isset($edit['date'])) {
// As of PHP 5.1.0, strtotime returns FALSE upon failure instead of -1.
if (strtotime($edit['date']) <= 0) {
form_set_error('date', t('You have to specify a valid date.'));
}
}
if (isset($edit['author']) && !$account = user_load(array('name' => $edit['author']))) {
form_set_error('author', t('You have to specify a valid author.'));
}
// Check validity of name, mail and homepage (if given)
if (!$user->uid || isset($edit['is_anonymous'])) {
$node = node_load($edit['nid']);
if (variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) > COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
if ($edit['name']) {
$taken = db_result(db_query("SELECT COUNT(uid) FROM {users} WHERE LOWER(name) = '%s'", $edit['name']));
if ($taken != 0) {
form_set_error('name', t('The name you used belongs to a registered user.'));
}
}
else if (variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
form_set_error('name', t('You have to leave your name.'));
}
if ($edit['mail']) {
if (!valid_email_address($edit['mail'])) {
form_set_error('mail', t('The e-mail address you specified is not valid.'));
}
}
else if (variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
form_set_error('mail', t('You have to leave an e-mail address.'));
}
if ($edit['homepage']) {
if (!valid_url($edit['homepage'], TRUE)) {
form_set_error('homepage', t('The URL of your homepage is not valid. Remember that it must be fully qualified, i.e. of the form <code>http://example.com/directory</code>.'));
}
}
}
}
return $edit;
}
/**
* Generate the basic commenting form, for appending to a node or display on a separate page.
*
* @param $title
* Not used.
* @ingroup forms
* @see comment_form_validate()
* @see comment_form_submit()
*/
function comment_form(&$form_state, $edit, $title = NULL) {
global $user;
$op = isset($_POST['op']) ? $_POST['op'] : '';
$node = node_load($edit['nid']);
if (!$user->uid && variable_get('comment_anonymous_'. $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) != COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
$form['mail'] = array('#type' => 'textfield', '#title' => t('E-mail'), '#maxlength' => 64, '#size' => 30, '#default_value' => $edit['mail'], '#description' => t('The content of this field is kept private and will not be shown publicly.')
$form['mail'] = array('#type' => 'textfield', '#title' => t('E-mail'), '#maxlength' => 64, '#size' => 30, '#default_value' => $edit['mail'], '#description' => t('The content of this field is kept private and will not be shown publicly.'), '#required' => TRUE);
$comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.signature, u.picture, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = %d', $edit['pid'], COMMENT_PUBLISHED));
* Theme comment controls box where the user can change the default display mode and display order of comments.
*
* @param $form
* The form structure.
* @ingroup themeable
*/
function theme_comment_controls($form) {
$output = '<div class="container-inline">';
$output .= drupal_render($form);
$output .= '</div>';
$output .= '<div class="description">'. t('Select your preferred way to display the comments and click "Save settings" to activate your changes.') .'</div>';
if (variable_get('comment_controls_'. $node->type, COMMENT_CONTROLS_HIDDEN) == COMMENT_CONTROLS_HIDDEN) {
// if comment controls are disabled use site default
$value = $default;
}
else {
// otherwise use the user's setting if set
if (isset($user->$setting) && $user->$setting) {
$value = $user->$setting;
}
else if (isset($_SESSION['comment_'. $setting]) && $_SESSION['comment_'. $setting]) {
$value = $_SESSION['comment_'. $setting];
}
else {
$value = $default;
}
}
}
return $value;
}
/**
* Updates the comment statistics for a given node. This should be called any
* time a comment is added, deleted, or updated.
*
* The following fields are contained in the node_comment_statistics table.
* - last_comment_timestamp: the timestamp of the last comment for this node or the node create stamp if no comments exist for the node.
* - last_comment_name: the name of the anonymous poster for the last comment
* - last_comment_uid: the uid of the poster for the last comment for this node or the node authors uid if no comments exists for the node.
* - comment_count: the total number of approved/published comments on this node.
*/
function _comment_update_node_statistics($nid) {
$count = db_result(db_query('SELECT COUNT(cid) FROM {comments} WHERE nid = %d AND status = %d', $nid, COMMENT_PUBLISHED));
// comments exist
if ($count > 0) {
$last_reply = db_fetch_object(db_query_range('SELECT cid, name, timestamp, uid FROM {comments} WHERE nid = %d AND status = %d ORDER BY cid DESC', $nid, COMMENT_PUBLISHED, 0, 1));
db_query("UPDATE {node_comment_statistics} SET comment_count = %d, last_comment_timestamp = %d, last_comment_name = '%s', last_comment_uid = %d WHERE nid = %d", $count, $last_reply->timestamp, $last_reply->uid ? '' : $last_reply->name, $last_reply->uid, $nid);
}
// no comments
else {
$node = db_fetch_object(db_query("SELECT uid, created FROM {node} WHERE nid = %d", $nid));
db_query("UPDATE {node_comment_statistics} SET comment_count = 0, last_comment_timestamp = %d, last_comment_name = '', last_comment_uid = %d WHERE nid = %d", $node->created, $node->uid, $nid);
}
}
/**
* Invoke a hook_comment() operation in all modules.
*
* @param &$comment
* A comment object.
* @param $op
* A string containing the name of the comment operation.
* @return
* The returned value of the invoked hooks.
*/
function comment_invoke_comment(&$comment, $op) {
$return = array();
foreach (module_implements('comment') as $name) {
$function = $name .'_comment';
$result = $function($comment, $op);
if (isset($result) && is_array($result)) {
$return = array_merge($return, $result);
}
else if (isset($result)) {
$return[] = $result;
}
}
return $return;
}
/**
* Generate vancode.
*
* Consists of a leading character indicating length, followed by N digits
* with a numerical value in base 36. Vancodes can be sorted as strings
* without messing up numerical order.
*
* It goes:
* 00, 01, 02, ..., 0y, 0z,
* 110, 111, ... , 1zy, 1zz,
* 2100, 2101, ..., 2zzy, 2zzz,
* 31000, 31001, ...
*/
function int2vancode($i = 0) {
$num = base_convert((int)$i, 10, 36);
$length = strlen($num);
return chr($length + ord('0') - 1) . $num;
}
/**
* Decode vancode back to an integer.
*/
function vancode2int($c = '00') {
return base_convert(substr($c, 1), 36, 10);
}
/**
* Implementation of hook_hook_info().
*/
function comment_hook_info() {
return array(
'comment' => array(
'comment' => array(
'insert' => array(
'runs when' => t('After saving a new comment'),
),
'update' => array(
'runs when' => t('After saving an updated comment'),
),
'delete' => array(
'runs when' => t('After deleting a comment')
),
'view' => array(
'runs when' => t('When a comment is being viewed by an authenticated user')
* Form builder; Prepare a form for blacklisted keywords.
*
* @ingroup forms
*/
function comment_unpublish_by_keyword_action_form($context) {
$form['keywords'] = array(
'#title' => t('Keywords'),
'#type' => 'textarea',
'#description' => t('The comment will be unpublished if it contains any of the character sequences above. Use a comma-separated list of character sequences. Example: funny, bungee jumping, "Company, Inc.". Character sequences are case-sensitive.'),