[ Index ]
 

Code source de IMP H3 (4.1.5)

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/lib/ -> Spam.php (source)

   1  <?php
   2  
   3  require_once 'Horde/Identity.php';
   4  require_once  IMP_BASE . '/lib/Compose.php';
   5  
   6  /**
   7   * The IMP_Spam:: class contains functions related to reporting spam
   8   * messages in IMP.
   9   *
  10   * $Horde: imp/lib/Spam.php,v 1.3.4.14 2007/01/02 13:54:56 jan Exp $
  11   *
  12   * Copyright 2004-2007 Michael Slusarz <slusarz@curecanti.org>
  13   *
  14   * See the enclosed file COPYING for license information (GPL). If you
  15   * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
  16   *
  17   * @author  Michael Slusarz <slusarz@curecanti.org>
  18   * @since   IMP 4.0
  19   * @package IMP
  20   */
  21  class IMP_Spam {
  22  
  23      /**
  24       * The IMP_Compose:: object used by the class.
  25       *
  26       * @var IMP_Compose
  27       */
  28      var $_imp_compose;
  29  
  30      /**
  31       * The IMP_Identity:: object used by the class.
  32       *
  33       * @var IMP_Identity
  34       */
  35      var $_identity;
  36  
  37      /**
  38       * Constructor.
  39       */
  40      function IMP_Spam()
  41      {
  42          $this->_imp_compose = &new IMP_Compose();
  43          $this->_identity = &Identity::singleton(array('imp', 'imp'));
  44      }
  45  
  46      /**
  47       * Report a list of messages as spam, based on the local configuration
  48       * parameters.
  49       *
  50       * @param mixed &$indices  See IMP::parseIndicesList().
  51       * @param string $action   Either 'spam' or 'notspam'.
  52       */
  53      function reportSpam(&$indices, $action)
  54      {
  55          /* Abort immediately if spam reporting has not been enabled. */
  56          if (empty($GLOBALS['conf'][$action]['reporting'])) {
  57              return;
  58          }
  59  
  60          /* Exit if there are no messages. */
  61          if (!($msgList = IMP::parseIndicesList($indices))) {
  62              return;
  63          }
  64  
  65          require_once  IMP_BASE . '/lib/MIME/Contents.php';
  66          require_once  IMP_BASE . '/lib/IMAP.php';
  67          $imp_imap = &IMP_IMAP::singleton();
  68  
  69          /* We can report 'program' and 'bounce' messages as the same since
  70           * they are both meant to indicate that the message has been reported
  71           * to some program for analysis. */
  72          $email_msg_count = $report_msg_count = 0;
  73  
  74          foreach ($msgList as $folder => $msgIndices) {
  75              /* Switch folders, if necessary (only valid for IMAP). */
  76              $imp_imap->changeMbox($folder);
  77  
  78              foreach ($msgIndices as $msgnum) {
  79                  /* Fetch the raw message contents (headers and complete
  80                   * body). */
  81                  $imp_contents = &IMP_Contents::singleton($msgnum . IMP_IDX_SEP . $folder);
  82  
  83                  $to = null;
  84                  $report_flag = false;
  85                  $raw_msg = null;
  86  
  87                  /* If a (not)spam reporting program has been provided, use
  88                   * it. */
  89                  if (!empty($GLOBALS['conf'][$action]['program'])) {
  90                      $raw_msg = $imp_contents->fullMessageText();
  91                      /* Use a pipe to write the message contents. This should
  92                       * be secure. */
  93                      $prog = str_replace('%u', escapeshellarg(Auth::getAuth()), $GLOBALS['conf'][$action]['program']);
  94                      $proc = proc_open($prog,
  95                                        array(0 => array('pipe', 'r'),
  96                                              1 => array('pipe', 'w'),
  97                                              2 => array('pipe', 'w')),
  98                                        $pipes);
  99                      if (!is_resource($proc)) {
 100                          Horde::logMessage('Cannot open process ' . $prog, __FILE__, __LINE__, PEAR_LOG_ERR);
 101                          return;
 102                      }
 103                      fwrite($pipes[0], $raw_msg);
 104                      fclose($pipes[0]);
 105                      $stderr = '';
 106                      while (!feof($pipes[2])) {
 107                          $stderr .= fgets($pipes[2]);
 108                      }
 109                      fclose($pipes[2]);
 110                      if (!empty($stderr)) {
 111                          Horde::logMessage('Error reporting spam: ' . $stderr, __FILE__, __LINE__, PEAR_LOG_ERR);
 112                      }
 113                      proc_close($proc);
 114                      $report_msg_count++;
 115                      $report_flag = true;
 116                  }
 117  
 118                  /* If a (not)spam reporting email address has been provided,
 119                   * use it. */
 120                  if (!empty($GLOBALS['conf'][$action]['email'])) {
 121                      if (!isset($raw_msg)) {
 122                          $raw_msg = $imp_contents->fullMessageText();
 123                      }
 124                      $this->_sendSpamReportMessage($action, $raw_msg);
 125                      $email_msg_count++;
 126                 }
 127  
 128                  /* If a (not)spam bounce email address has been provided, use
 129                   * it. */
 130                  if (!empty($GLOBALS['conf'][$action]['bounce'])) {
 131                      $to = $GLOBALS['conf'][$action]['bounce'];
 132                  } elseif (!empty($GLOBALS['conf']['hooks']['spam_bounce'])) {
 133                      /* Call the bounce email generation hook, if requested. */
 134                      require_once HORDE_BASE . '/config/hooks.php';
 135                      if (function_exists('_imp_hook_spam_bounce')) {
 136                          $to = call_user_func('_imp_hook_spam_bounce', $action);
 137                      }
 138                  }
 139  
 140                  if ($to) {
 141                      require_once  IMP_BASE . '/lib/MIME/Headers.php';
 142                      $imp_headers = &new IMP_Headers($msgnum);
 143                      $imp_headers->buildHeaders();
 144  
 145                      $from_addr = $this->_identity->getFromAddress();
 146                      $imp_headers->addResentHeaders($from_addr, $to);
 147  
 148                      /* We need to set the Return-Path header to the current
 149                       * user - see RFC 2821 [4.4]. */
 150                      $imp_headers->removeHeader('return-path');
 151                      $imp_headers->addHeader('Return-Path', $from_addr);
 152  
 153                      $bodytext = $imp_contents->getBody();
 154  
 155                      $this->_imp_compose->sendMessage($to, $imp_headers, $bodytext, NLS::getCharset());
 156                      if (!$report_flag) {
 157                          $report_msg_count++;
 158                      }
 159                  }
 160              }
 161          }
 162  
 163          /* Report what we've done. */
 164          if ($report_msg_count) {
 165              switch ($action) {
 166              case 'spam':
 167                  if ($report_msg_count > 1) {
 168                      $GLOBALS['notification']->push(sprintf(_("%d messages have been reported as spam."), $report_msg_count), 'horde.message');
 169                  } else {
 170                      $GLOBALS['notification']->push(_("1 message has been reported as spam."), 'horde.message');
 171                  }
 172                  break;
 173  
 174              case 'notspam':
 175                  if ($report_msg_count > 1) {
 176                      $GLOBALS['notification']->push(sprintf(_("%d messages have been reported as not spam."), $report_msg_count), 'horde.message');
 177                  } else {
 178                      $GLOBALS['notification']->push(_("1 message has been reported as not spam."), 'horde.message');
 179                  }
 180                  break;
 181              }
 182          }
 183  
 184          if ($email_msg_count) {
 185              switch ($action) {
 186              case 'spam':
 187                  if ($email_msg_count > 1) {
 188                      $GLOBALS['notification']->push(sprintf(_("%d messages have been reported as spam to your system administrator."), $email_msg_count), 'horde.message');
 189                  } else {
 190                      $GLOBALS['notification']->push(_("1 message has been reported as spam to your system administrator."), 'horde.message');
 191                  }
 192                  break;
 193  
 194              case 'notspam':
 195                  if ($email_msg_count > 1) {
 196                      $GLOBALS['notification']->push(sprintf(_("%d messages have been reported as not spam to your system administrator."), $email_msg_count), 'horde.message');
 197                  } else {
 198                      $GLOBALS['notification']->push(_("1 message has been reported as not spam to your system administrator."), 'horde.message');
 199                  }
 200                  break;
 201              }
 202          }
 203      }
 204  
 205      /**
 206       * Send a (not)spam message to the sysadmin.
 207       *
 208       * @access private
 209       *
 210       * @param string $action  The action type.
 211       * @param string $data    The message data.
 212       */
 213      function _sendSpamReportMessage($action, $data)
 214      {
 215          require_once 'Horde/MIME/Message.php';
 216  
 217          /* Build the MIME structure. */
 218          $mime = &new MIME_Message();
 219          $mime->setType('multipart/digest');
 220          $mime->addPart(new MIME_Part('message/rfc822', $data));
 221  
 222          $spam_headers = &new IMP_Headers();
 223          $spam_headers->addMessageIdHeader();
 224          $spam_headers->addHeader('Date', date('r'));
 225          $spam_headers->addHeader('To', $GLOBALS['conf'][$action]['email']);
 226          $spam_headers->addHeader('From', $this->_identity->getFromLine());
 227          $spam_headers->addHeader('Subject', _("$action Report from") . ' ' . $GLOBALS['imp']['user']);
 228          $spam_headers->addMIMEHeaders($mime);
 229  
 230          /* Send the message. */
 231          $this->_imp_compose->sendMessage($GLOBALS['conf'][$action]['email'], $spam_headers, $mime, NLS::getCharset());
 232      }
 233  
 234  }


Généré le : Thu Nov 29 12:30:07 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics