You are here

function spam_page in Spam 5

Drupal _page hook. Provides various spam actions based on the URL that is currently being accessed.

1 string reference to 'spam_page'
spam_menu in ./spam.module
Implementation of hook_menu().

File

./spam.module, line 836

Code

function spam_page() {
  global $ID, $SOURCE;

  /* URL looks something like  spam/comment/12345/notspam
   *                           0    1       2     3
   * 0 has to be spam for us to be here.
   */
  $source = arg(1);
  $id = arg(2);
  $op = arg(3);
  if ($op) {

    // set spam to TRUE (spam) or FALSE (not spam)
    $is_spam = $op == 'spam';
  }
  else {

    // invalid URL, let Drupal core generate a 404
    return drupal_not_found();
  }
  $ID = $id;
  $SOURCE = $source;
  switch ($source) {
    case 'comment':
      $comment = spam_load_comment($id);
      $old = spam_load('comment', $id);
      $goto = "node/{$comment->nid}";
      $goto_fragment = "comment-{$comment->cid}";
      $header = $comment->subject;
      $body = "{$comment->comment} {$comment->name} {$comment->mail} {$comment->homepage}";
      watchdog('spam', t('Spam: marked comment "%subject" as @spam', array(
        '%subject' => $comment->subject,
        '@spam' => $is_spam ? 'spam' : 'not spam',
      )));
      break;
    case 'node':
      $node = node_load(array(
        'nid' => $id,
      ));
      $old = spam_load('node', $id);
      $goto = "node/{$node->nid}";
      $goto_fragment = "";
      $header = $node->title;
      $body = $node->body;
      watchdog('spam', t('Spam: marked node "%title" as @spam', array(
        '%title' => $node->title,
        '@spam' => $is_spam ? 'spam' : 'not spam',
      )));
      break;
    default:

      /* Allow external modules the ability to handle custom content types.
       * Adds _spam_page($id) hook.
       */
      $hook = spam_invoke_hook('page', $id);
      if ($hook['old']) {
        $old = $hook['old'];
      }
      if ($hook['goto']) {
        $goto = $hook['goto'];
      }
      if ($hook['goto_fragment']) {
        $goto_fragment = $hook['goto_fragment'];
      }
      if ($hook['header']) {
        $header = $hook['header'];
      }
      if ($hook['body']) {
        $body = $hook['body'];
      }
      break;
  }
  $hash = md5($header . $body);
  $tokens = spam_tokenize($header, 'header*');
  $tokens = array_merge($tokens, spam_tokenize($body));
  if ($old->id) {

    // we've filtered this content before, now we're updating it
    spam_tokens_unsave($tokens, $is_spam);
    spam_tokens_save($tokens, $is_spam);
    db_query("UPDATE {spam_tracker} SET probability = %d, hash = '%s', timestamp = %d WHERE source = '%s' AND id = %d", $is_spam ? 99 : 1, $hash, time(), $source, $id);
  }
  else {

    // this is the first time we've filtered this content
    spam_tokens_save($tokens, $is_spam);
    db_query("INSERT INTO {spam_tracker} (source,id,probability,hostname,hash,timestamp) VALUES('%s',%d,%d,'%s','%s',%d)", $source, $id, $is_spam ? 99 : 1, $_SERVER['REMOTE_ADDR'], $hash, time());
  }
  spam_default_actions($source, $id, $header, $body, $is_spam ? 99 : 1, $old, FALSE);
  spam_log(SPAM_LOG, t('spam_page: @source manually marked as @spam', array(
    '@source' => $source,
    '@spam' => $is_spam ? 'spam' : 'not spam',
  )), $source, $id);
  drupal_goto($goto, NULL, $goto_fragment);
}