<?php
// $Id: commentmail.module,v 1.10.2.2 2006/10/07 22:21:59 jjeff Exp $

/**
 * Implementation of hook_help()
 */

function commentmail_help($section){
  switch($section){
    case 'admin/modules#description':
      return t('Sends an email message when comments are posted to the site.');
  }
}

/**
 * Implementation of hook_settings()
 */

function commentmail_settings(){
  drupal_set_title(t('comment mail'));
  $form['commentmail_to'] = array('#type' => 'textfield', '#title' => t('Send to'), '#default_value' => variable_get('commentmail_to', variable_get('site_mail', '')), '#description' => t('A mail message will be sent here when new comments are posted to the site. Separate multiple addresses with a comma.'));
  $form['commentmail_opt'] = array(
    '#type' => 'radios',
    '#title' => t('Send mail for'),
    '#default_value' => variable_get('commentmail_opt', 'approval'),
    '#options' => array(
      'all' => t('all new comments'),
      'approval' => t('just comments needing approval'),
      'disable' => t('none (disabled)'),      
    ),
  );
  return $form;
}

/**
 * Implementation of hook_comment()
 *
 */

function commentmail_comment($comment, $op){
  switch($op){
    case 'insert':
      $comment_obj = db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $comment['cid']));
      $opt = variable_get('commentmail_opt', 'approval');
      switch($opt){
        case 'approval':
          if($comment_obj->status == 0){
            break;
          }
        case 'all':
          $mailto = variable_get('commentmail_to', variable_get('site_mail', FALSE));
          $from = variable_get('site_mail', ini_get('sendmail_from'));
          if($mailto){
            $node = node_load($comment_obj->nid);
            $site = variable_get('site_name', t('Drupal'));
            user_mail(
              $mailto, t(
                "[%site] New Comment posted on '%title'",
                array('%title' => check_plain($node->title), '%site' => $site)), 
                theme(
                  ($comment_obj->status == COMMENT_NOT_PUBLISHED ? 'commentmail_approve' : 'commentmail_notify'), 
                  $comment_obj,
                  $node
                ),
              "From: $from\nX-Mailer: Drupal"
            );
          }
          else {
            watchdog('commentmail', t("Site mail address is not configured."), WATCHDOG_ERROR);
          }
      }
  }
}

function commentmail_menu($may_cache){
  $items = array();
  $access = user_access('administer comments');
  if($may_cache){
    $items[] = array(
      'title' => t('quick edit'), 
      'path' => 'comment/qa', 
      'access' => TRUE, 
      'callback' => 'commentmail_quick_approve', 
      'type' => MENU_CALLBACK
    );
    $items[] = array(
      'title' => t('quick delete'), 
      'path' => 'comment/quickdelete', 
      'access' => $access, 
      'callback' => 'commentmail_quick_delete', 
      'type' => MENU_CALLBACK
    );
    $items[] = array(
      'title' => t('quick delete and ban'), 
      'path' => 'comment/quickdeleteban', 
      'access' => $access, 
      'callback' => 'commentmail_quick_deleteban', 
      'type' => MENU_CALLBACK
    );
  }
  return $items;
}

function commentmail_quick_approve($cid = NULL, $op = FALSE){
  global $user;
  if(user_access('administer comments')){

      $comment = db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $cid));
      if ($comment){
        if ($op == 'approve'){
          $comment->status = 0;
          if(comment_save((array)$comment)){
            // link to comment on page
            drupal_goto('node/'.$comment->nid, NULL, 'comment-'.$comment->cid);
          }
          else {
            drupal_set_message(t('Comment not saved'), 'error');
          }
        }
        $comment->comment = check_markup($comment->comment, $comment->format, FALSE);
        $node = node_load($comment->nid);
        $output = "<h2>".t('Comment on <em>%title</em>', array('%title' => $node->title))."</h2>";
        $confirm_delete = "return confirm('". t('Are you sure you want to delete this comment?') ."');";
        $confirm_deleteban = "return confirm('". t('Are you sure you want to delete this comment and ban the computer that posted it?') ."');";
        if ($comment->status) {
          $links[] = l(t('approve'), 'comment/qa/'.$cid.'/approve');
        }
        else {
          $links[] = t('approved');
        }
        $links[] = l(t('edit'), 'comment/edit/'.$cid);
        $links[] = l(t('delete'), 'comment/quickdelete/'.$cid, array('onclick' => $confirm_delete));
        $links[] = l(t('delete and ban ip'), 'comment/quickdeleteban/'.$cid, array('onclick' => $confirm_deleteban));
        $output .= theme('comment', $comment, $links);
      }
      else {
        $output = t("Comment not found.");
      }
    }
    elseif ($user->uid > 0) {
      drupal_access_denied();
    }
    else {
      // user is not logged in... goto the user login page, then come back here
      drupal_goto('user/login', 'destination=comment/qa/'.$cid);
    }
  return $output;
}

function commentmail_quick_delete($cid = NULL){
  if(is_numeric($cid) && $comment = db_fetch_object(db_query('SELECT c.*, u.name AS registered_name, u.uid FROM {comments} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.cid = %d', $cid))){
    
    commentmail_delete($comment);
    
    drupal_goto("node/$comment->nid");
  }
  else {
    drupal_not_found();
  }
}

function commentmail_quick_deleteban($cid = NULL){
    if(is_numeric($cid) && $comment = db_fetch_object(db_query('SELECT * FROM {comments} WHERE cid = %d', $cid))){
    
    commentmail_delete($comment);
    
    $aid = db_next_id('{access}_aid');
    db_query("INSERT INTO {access} (aid, mask, type, status) VALUES ('%s', '%s', 'host', 0)", $aid, $comment->hostname);
    
    drupal_set_message(t('The address <em>%ip</em> rule has been banned.', array('%ip' => $comment->hostname)));
    
    drupal_goto("node/$comment->nid");
  }
  else {
    drupal_not_found();
  }
}

function commentmail_delete($comment){
  $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
    
    drupal_set_message(t('The comment and all its replies have been deleted.'));

    // Delete comment and its replies.
    _comment_delete_thread($comment);

    _comment_update_node_statistics($comment->nid);

    // Clear the cache so an anonymous user sees that his comment was deleted.
    cache_clear_all();
}


/**
 * Theme function for comments needing approval
 *
 * @param object $comment
 *  The comment object
 * @param object $node
 *  The object of the node being commented upon
 * @return string
 *  body of email
 */

function theme_commentmail_approve($comment, $node){
  $site = variable_get('site_name', t('your Drupal site'));
  $approval_url = url('comment/qa/'. $comment->cid, NULL, NULL, TRUE);
  //$delete_url = url('comment/delete/'. $comment->cid, $dest, NULL, TRUE);
  //$queue_url = url('admin/comment/list/approval', NULL, NULL, TRUE);
  $output = t(<<<EOT
An unapproved comment has been posted on %site for the entry entitled '%node->title'. You need to publish this comment before it will appear on your site.

Approve/edit/delete this comment:
  <%approval_url>

IP Address: %comment->hostname\n 
Name: %comment->name\n
Email Address: %comment->mail\n 
URL: %comment->homepage\n
Comment:

%comment->comment

EOT
  , array(
    '%site' => $site,
    '%node->title' => $node->title,
    '%approval_url' => $approval_url,
    '%delete_url' => $delete_url,
    '%queue_url' => $queue_url,
    '%comment->hostname' => $comment->hostname,
    '%comment->name' => $comment->name,
    '%comment->mail' => $comment->mail,
    '%comment->homepage' => $comment->homepage,
    '%comment->comment' => $comment->comment,
  ));
  return $output;
}

/**
 * Theme function for comments not needing approval
 *
 * @param object $comment
 *  The comment object
 * @param object $node
 *  The object of the node being commented upon
 * @return string
 *  body of email
 */

function theme_commentmail_notify($comment, $node){
  $site = variable_get('site_name', t('your Drupal site'));
  $view_url = url('node/'. $node->nid, NULL, 'comment-'. $comment->cid, TRUE);
  $delete_url = url('comment/delete/'. $comment->cid, $dest, NULL, TRUE);
  $admin_url = url('admin/comment', NULL, NULL, TRUE);
  $output = t(<<<EOT
A new comment has been posted on %site for the entry entitled '%node->title'.

View this comment:
  <%view_url>
Delete this comment:
  <%delete_url>
Comment administration:
  <%admin_url>

IP Address: %comment->hostname\n
Name: %comment->name\n
Email Address: %comment->mail\n
URL: %comment->homepage\n
Comments:

%comment->comment

EOT
  , array(
    '%site' => $site,
    '%node->title' => $node->title,
    '%view_url' => $view_url,
    '%delete_url' => $delete_url,
    '%admin_url' => $admin_url,
    '%comment->hostname' => $comment->hostname,
    '%comment->name' => $comment->name,
    '%comment->mail' => $comment->mail,
    '%comment->homepage' => $comment->homepage,
    '%comment->comment' => $comment->comment,
  ));
  return $output;
}
