Overview

Packages

  • IMP
  • None

Classes

  • IMP
  • IMP_Ajax_Application
  • IMP_Ajax_Imple_ContactAutoCompleter
  • IMP_Ajax_Imple_PassphraseDialog
  • IMP_Ajax_Queue
  • IMP_Api
  • IMP_Auth
  • IMP_Block_Newmail
  • IMP_Block_Summary
  • IMP_Compose
  • IMP_Compose_Exception
  • IMP_Compose_Stationery
  • IMP_Contents
  • IMP_Crypt_Pgp
  • IMP_Crypt_Smime
  • IMP_Dimp
  • IMP_Exception
  • IMP_Factory_AuthImap
  • IMP_Factory_Compose
  • IMP_Factory_Contents
  • IMP_Factory_Flags
  • IMP_Factory_Identity
  • IMP_Factory_Imap
  • IMP_Factory_Imaptree
  • IMP_Factory_Mail
  • IMP_Factory_Mailbox
  • IMP_Factory_MailboxList
  • IMP_Factory_MimeViewer
  • IMP_Factory_Pgp
  • IMP_Factory_Quota
  • IMP_Factory_Search
  • IMP_Factory_Sentmail
  • IMP_Factory_Smime
  • IMP_Filter
  • IMP_Flag_Base
  • IMP_Flag_Imap
  • IMP_Flag_Imap_Answered
  • IMP_Flag_Imap_Deleted
  • IMP_Flag_Imap_Draft
  • IMP_Flag_Imap_Flagged
  • IMP_Flag_Imap_Forwarded
  • IMP_Flag_Imap_Junk
  • IMP_Flag_Imap_NotJunk
  • IMP_Flag_Imap_Seen
  • IMP_Flag_System_Attachment
  • IMP_Flag_System_Encrypted
  • IMP_Flag_System_HighPriority
  • IMP_Flag_System_List
  • IMP_Flag_System_LowPriority
  • IMP_Flag_System_Match_Address
  • IMP_Flag_System_Match_Flag
  • IMP_Flag_System_Match_Header
  • IMP_Flag_System_Personal
  • IMP_Flag_System_Signed
  • IMP_Flag_System_Unseen
  • IMP_Flag_User
  • IMP_Flags
  • IMP_Imap
  • IMP_Imap_Acl
  • IMP_Imap_Exception
  • IMP_Imap_PermanentFlags
  • IMP_Imap_Thread
  • IMP_Imap_Tree
  • IMP_Indices
  • IMP_Indices_Form
  • IMP_LoginTasks_SystemTask_GarbageCollection
  • IMP_LoginTasks_SystemTask_Upgrade
  • IMP_LoginTasks_SystemTask_UpgradeAuth
  • IMP_LoginTasks_Task_Autocreate
  • IMP_LoginTasks_Task_DeleteAttachmentsMonthly
  • IMP_LoginTasks_Task_DeleteSentmailMonthly
  • IMP_LoginTasks_Task_FilterOnLogin
  • IMP_LoginTasks_Task_PurgeSentmail
  • IMP_LoginTasks_Task_PurgeSpam
  • IMP_LoginTasks_Task_PurgeTrash
  • IMP_LoginTasks_Task_RecoverDraft
  • IMP_LoginTasks_Task_RenameSentmailMonthly
  • IMP_Mailbox
  • IMP_Mailbox_List
  • IMP_Mailbox_List_Track
  • IMP_Maillog
  • IMP_Menu_Dimp
  • IMP_Message
  • IMP_Mime_Status
  • IMP_Mime_Viewer_Alternative
  • IMP_Mime_Viewer_Appledouble
  • IMP_Mime_Viewer_Audio
  • IMP_Mime_Viewer_Enriched
  • IMP_Mime_Viewer_Externalbody
  • IMP_Mime_Viewer_Html
  • IMP_Mime_Viewer_Images
  • IMP_Mime_Viewer_Itip
  • IMP_Mime_Viewer_Mdn
  • IMP_Mime_Viewer_Partial
  • IMP_Mime_Viewer_Pdf
  • IMP_Mime_Viewer_Pgp
  • IMP_Mime_Viewer_Plain
  • IMP_Mime_Viewer_Related
  • IMP_Mime_Viewer_Rfc822
  • IMP_Mime_Viewer_Smil
  • IMP_Mime_Viewer_Smime
  • IMP_Mime_Viewer_Status
  • IMP_Mime_Viewer_Vcard
  • IMP_Mime_Viewer_Video
  • IMP_Mime_Viewer_Zip
  • IMP_Notification_Event_Status
  • IMP_Notification_Handler_Decorator_ImapAlerts
  • IMP_Notification_Handler_Decorator_NewmailNotify
  • IMP_Notification_Listener_AjaxStatus
  • Imp_Prefs_Identity
  • IMP_Prefs_Ui
  • IMP_Quota
  • IMP_Quota_Base
  • IMP_Quota_Command
  • IMP_Quota_Hook
  • IMP_Quota_Imap
  • IMP_Quota_Maildir
  • IMP_Quota_Mdaemon
  • IMP_Quota_Mercury32
  • IMP_Quota_Null
  • IMP_Quota_Sql
  • IMP_Search
  • IMP_Search_Element
  • IMP_Search_Element_Attachment
  • IMP_Search_Element_Autogenerated
  • IMP_Search_Element_Bulk
  • IMP_Search_Element_Contacts
  • IMP_Search_Element_Date
  • IMP_Search_Element_Flag
  • IMP_Search_Element_Header
  • IMP_Search_Element_Mailinglist
  • IMP_Search_Element_Or
  • IMP_Search_Element_Personal
  • IMP_Search_Element_Recipient
  • IMP_Search_Element_Size
  • IMP_Search_Element_Text
  • IMP_Search_Element_Within
  • IMP_Search_Filter
  • IMP_Search_Filter_Attachment
  • IMP_Search_Filter_Autogenerated
  • IMP_Search_Filter_Builtin
  • IMP_Search_Filter_Bulk
  • IMP_Search_Filter_Contacts
  • IMP_Search_Filter_Mailinglist
  • IMP_Search_Filter_Personal
  • IMP_Search_Query
  • IMP_Search_Vfolder
  • IMP_Search_Vfolder_Builtin
  • IMP_Search_Vfolder_Vinbox
  • IMP_Search_Vfolder_Vtrash
  • IMP_Sentmail
  • IMP_Sentmail_Base
  • IMP_Sentmail_Null
  • IMP_Sentmail_Sql
  • IMP_Spam
  • IMP_Test
  • IMP_Tree_Flist
  • IMP_Tree_Jquerymobile
  • IMP_Tree_Simplehtml
  • IMP_Ui_Compose
  • IMP_Ui_Editor
  • IMP_Ui_Folder
  • IMP_Ui_Headers
  • IMP_Ui_Imageview
  • IMP_Ui_Mailbox
  • IMP_Ui_Message
  • IMP_Ui_Mimp
  • IMP_Ui_Search
  • IMP_Views_Compose
  • IMP_Views_ListMessages
  • IMP_Views_ShowMessage
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * The IMP_Spam:: class contains functions related to reporting spam
  4:  * messages in IMP.
  5:  *
  6:  * Copyright 2004-2012 Horde LLC (http://www.horde.org/)
  7:  *
  8:  * See the enclosed file COPYING for license information (GPL). If you
  9:  * did not receive this file, see http://www.horde.org/licenses/gpl.
 10:  *
 11:  * @author   Michael Slusarz <slusarz@horde.org>
 12:  * @category Horde
 13:  * @license  http://www.horde.org/licenses/gpl GPL
 14:  * @package  IMP
 15:  */
 16: class IMP_Spam
 17: {
 18:     /**
 19:      * Reports a list of messages as spam, based on the local configuration
 20:      * parameters.
 21:      *
 22:      * @param IMP_Indices $indices  An indices object.
 23:      * @param string $action        Either 'spam' or 'notspam'.
 24:      * @param array $opts           Additional options:
 25:      *   - mailboxob: (IMP_Mailbox_List) Update this mailbox list object.
 26:      *                DEFAULT: No update.
 27:      *   - noaction: (boolean) Don't perform any action after reporting?
 28:      *               DEFAULT: false
 29:      *
 30:      * @return integer  1 if messages have been deleted, 2 if messages have
 31:      *                  been moved.
 32:      */
 33:     static public function reportSpam(IMP_Indices $indices, $action,
 34:                                       array $opts = array())
 35:     {
 36:         global $conf, $injector, $notification, $prefs, $registry;
 37: 
 38:         /* Abort immediately if spam reporting has not been enabled, or if
 39:          * there are no messages. */
 40:         if (empty($conf[$action]['reporting']) ||
 41:             !count($indices)) {
 42:             return 0;
 43:         }
 44: 
 45:         $report_count = $result = 0;
 46: 
 47:         foreach ($indices as $ob) {
 48:             try {
 49:                 $ob->mbox->uidvalid;
 50:             } catch (IMP_Exception $e) {
 51:                 continue;
 52:             }
 53: 
 54:             foreach ($ob->uids as $idx) {
 55:                 /* Fetch the raw message contents (headers and complete
 56:                  * body). */
 57:                 try {
 58:                     $imp_contents = $injector->getInstance('IMP_Factory_Contents')->create($ob->mbox->getIndicesOb($idx));
 59:                 } catch (IMP_Exception $e) {
 60:                     continue;
 61:                 }
 62: 
 63:                 $raw_msg = $to = null;
 64:                 $report_flag = false;
 65: 
 66:                 /* If a (not)spam reporting program has been provided, use
 67:                  * it. */
 68:                 if (!empty($conf[$action]['program'])) {
 69:                     $raw_msg = $imp_contents->fullMessageText(array('stream' => true));
 70: 
 71:                     /* Use a pipe to write the message contents. This should
 72:                      * be secure. */
 73:                     $prog = str_replace(array('%u','%l', '%d'),
 74:                         array(
 75:                             escapeshellarg($registry->getAuth()),
 76:                             escapeshellarg($registry->getAuth('bare')),
 77:                             escapeshellarg($registry->getAuth('domain'))
 78:                         ), $conf[$action]['program']);
 79:                     $proc = proc_open($prog,
 80:                         array(
 81:                             0 => array('pipe', 'r'),
 82:                             1 => array('pipe', 'w'),
 83:                             2 => array('pipe', 'w')
 84:                         ), $pipes);
 85:                     if (!is_resource($proc)) {
 86:                         Horde::logMessage('Cannot open process ' . $prog, 'ERR');
 87:                         return 0;
 88:                     }
 89:                     stream_copy_to_stream($raw_msg, $pipes[0]);
 90:                     fclose($pipes[0]);
 91:                     $stderr = '';
 92:                     while (!feof($pipes[2])) {
 93:                         $stderr .= fgets($pipes[2]);
 94:                     }
 95:                     fclose($pipes[2]);
 96:                     if (!empty($stderr)) {
 97:                         Horde::logMessage('Error reporting spam: ' . $stderr, 'ERR');
 98:                     }
 99:                     proc_close($proc);
100:                     $report_flag = true;
101:                 }
102: 
103:                 /* If a (not)spam reporting email address has been provided,
104:                  * use it. */
105:                 if (!empty($conf[$action]['email'])) {
106:                     $to = $conf[$action]['email'];
107:                 } else {
108:                     /* Call the email generation hook, if requested. */
109:                     try {
110:                         $to = Horde::callHook('spam_email', array($action), 'imp');
111:                     } catch (Horde_Exception_HookNotSet $e) {}
112:                 }
113: 
114:                 if ($to) {
115:                     if (!isset($imp_compose)) {
116:                         $imp_compose = $injector->getInstance('IMP_Factory_Compose')->create();
117:                     }
118: 
119:                     if (!isset($conf[$action]['email_format'])) {
120:                         $conf[$action]['email_format'] = 'digest';
121:                     }
122: 
123:                     switch ($conf[$action]['email_format']) {
124:                     case 'redirect':
125:                         $index = new IMP_Indices($ob->mbox, $idx);
126:                         $imp_compose->redirectMessage($index);
127: 
128:                         /* Send the message. */
129:                         try {
130:                             $imp_compose->sendRedirectMessage($to, false);
131:                             $report_flag = true;
132:                         } catch (IMP_Compose_Exception $e) {
133:                             $e->log();
134:                         }
135:                         break;
136: 
137:                     case 'digest':
138:                     default:
139:                         try {
140:                             $from_line = $injector->getInstance('IMP_Identity')->getFromLine();
141:                         } catch (Horde_Exception $e) {
142:                             $from_line = null;
143:                         }
144: 
145:                         if (!isset($raw_msg)) {
146:                             $raw_msg = $imp_contents->fullMessageText(array('stream' => true));
147:                         }
148: 
149:                         /* Build the MIME structure. */
150:                         $mime = new Horde_Mime_Part();
151:                         $mime->setType('multipart/digest');
152: 
153:                         $rfc822 = new Horde_Mime_Part();
154:                         $rfc822->setType('message/rfc822');
155:                         $rfc822->setContents($raw_msg);
156:                         $mime->addPart($rfc822);
157: 
158:                         $spam_headers = new Horde_Mime_Headers();
159:                         $spam_headers->addMessageIdHeader();
160:                         $spam_headers->addHeader('Date', date('r'));
161:                         $spam_headers->addHeader('To', $to);
162:                         if (!is_null($from_line)) {
163:                             $spam_headers->addHeader('From', $from_line);
164:                         }
165:                         $spam_headers->addHeader('Subject', sprintf(_("%s report from %s"), $action, $registry->getAuth()));
166: 
167:                         /* Send the message. */
168:                         try {
169:                             $recip_list = $imp_compose->recipientList(array('to' => $to));
170:                             $imp_compose->sendMessage($recip_list['list'], $spam_headers, $mime, 'UTF-8');
171:                             $report_flag = true;
172:                         } catch (IMP_Compose_Exception $e) {
173:                             $e->log();
174:                         }
175:                         break;
176:                     }
177:                 }
178: 
179:                 if ($report_flag) {
180:                     ++$report_count;
181:                 }
182:             }
183:         }
184: 
185:         if (!$report_count) {
186:             return 0;
187:         }
188: 
189:         /* Report what we've done. */
190:         if ($report_count == 1) {
191:             $hdrs = $imp_contents->getHeader();
192:             if ($subject = $hdrs->getValue('subject')) {
193:                 $subject = Horde_String::truncate($subject, 30);
194:             } elseif ($from = $hdrs->getValue('from')) {
195:                 $from = Horde_String::truncate($from, 30);
196:             } else {
197:                 $subject = '[' . _("No Subject") . ']';
198:             }
199: 
200:             switch ($action) {
201:             case 'spam':
202:                 $msg = $subject
203:                     ? sprintf(_("The message \"%s\" has been reported as spam."), $subject)
204:                     : sprintf(_("The message from \"%s\" has been reported as spam."), $from);
205:                 break;
206: 
207:             case 'notspam':
208:                 $msg = $subject
209:                     ? sprintf(_("The message \"%s\" has been reported as innocent."), $subject)
210:                     : sprintf(_("The message from \"%s\" has been reported as innocent."), $from);
211:                 break;
212:             }
213:         } elseif ($action == 'spam') {
214:             $msg = sprintf(_("%d messages have been reported as spam."), $report_count);
215:         } else {
216:             $msg = sprintf(_("%d messages have been reported as innocent."), $report_count);
217:         }
218:         $notification->push($msg, 'horde.message');
219: 
220:         $mbox_args = array();
221:         if (isset($opts['mailboxob'])) {
222:             $mbox_args['mailboxob'] = $opts['mailboxob'];
223:         }
224: 
225:         /* Run post-reporting hook. */
226:         try {
227:             Horde::callHook('post_spam', array($action, $indices), 'imp');
228:         } catch (Horde_Exception_HookNotSet $e) {}
229: 
230:         if (!empty($opts['noaction'])) {
231:             return $result;
232:         }
233: 
234:         /* Delete/move message after report. */
235:         switch ($action) {
236:         case 'spam':
237:             /* Always flag messages as Junk. */
238:             $imp_message = $injector->getInstance('IMP_Message');
239:             $imp_message->flag(array('$junk'), $indices, true);
240:             $imp_message->flag(array('$notjunk'), $indices, false);
241: 
242:             if ($result = $prefs->getValue('delete_spam_after_report')) {
243:                 switch ($result) {
244:                 case 1:
245:                     $msg_count = $imp_message->delete($indices, $mbox_args);
246:                     if ($msg_count === false) {
247:                         $result = 0;
248:                     } else {
249:                         if ($msg_count == 1) {
250:                             $notification->push(_("The message has been deleted."), 'horde.message');
251:                         } else {
252:                             $notification->push(sprintf(_("%d messages have been deleted."), $msg_count), 'horde.message');
253:                         }
254:                     }
255:                     break;
256: 
257:                 case 2:
258:                     if ($targetMbox = IMP_Mailbox::getPref('spam_folder')) {
259:                         if (!$imp_message->copy($targetMbox, 'move', $indices, array_merge($mbox_args, array('create' => true)))) {
260:                             $result = 0;
261:                         }
262:                     } else {
263:                         $notification->push(_("Could not move message to spam mailbox - no spam mailbox defined in preferences."), 'horde.error');
264:                         $result = 0;
265:                     }
266:                     break;
267:                 }
268:             }
269:             break;
270: 
271:         case 'notspam':
272:             /* Always flag messages as NotJunk. */
273:             $imp_message = $injector->getInstance('IMP_Message');
274:             $imp_message->flag(array('$notjunk'), $indices, true);
275:             $imp_message->flag(array('$junk'), $indices, false);
276: 
277:             if (($result = $prefs->getValue('move_ham_after_report')) &&
278:                 !$imp_message->copy('INBOX', 'move', $indices, $mbox_args)) {
279:                 $result = 0;
280:             }
281:             break;
282:         }
283: 
284:         return $result;
285:     }
286: 
287: }
288: 
API documentation generated by ApiGen