1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class IMP_Spam
29: {
30:
31: const INNOCENT = 1;
32: const SPAM = 2;
33:
34: 35: 36: 37: 38:
39: protected $_action;
40:
41: 42: 43: 44: 45:
46: protected $_drivers;
47:
48: 49: 50: 51: 52: 53:
54: public function __construct($action, array $drivers = array())
55: {
56: $this->_action = $action;
57: $this->_drivers = $drivers;
58: }
59:
60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70:
71: public function report(IMP_Indices $indices, array $opts = array())
72: {
73: global $injector, $notification, $prefs;
74:
75: 76:
77: if (empty($this->_drivers) || !count($indices)) {
78: return 0;
79: }
80:
81: $imp_contents = $injector->getInstance('IMP_Factory_Contents');
82: $report_count = 0;
83:
84: foreach ($indices as $ob) {
85: try {
86: $ob->mbox->uidvalid;
87: } catch (IMP_Exception $e) {
88: continue;
89: }
90:
91: foreach ($ob->uids as $idx) {
92: try {
93: $contents = $imp_contents->create($ob->mbox->getIndicesOb($idx));
94: } catch (IMP_Exception $e) {
95: continue;
96: }
97:
98: $report_flag = false;
99:
100: foreach ($this->_drivers as $val) {
101: if ($val->report($contents, $this->_action)) {
102: $report_flag = true;
103: }
104: }
105:
106: if ($report_flag) {
107: ++$report_count;
108: }
109: }
110: }
111:
112: if (!$report_count) {
113: return 0;
114: }
115:
116:
117: if ($report_count == 1) {
118: $hdrs = $contents->getHeader();
119: if ($subject = $hdrs->getValue('subject')) {
120: $subject = Horde_String::truncate($subject, 30);
121: } elseif ($from = $hdrs->getValue('from')) {
122: $from = Horde_String::truncate($from, 30);
123: } else {
124: $subject = '[' . _("No Subject") . ']';
125: }
126:
127: switch ($this->_action) {
128: case self::INNOCENT:
129: $msg = $subject
130: ? sprintf(_("The message \"%s\" has been reported as innocent."), $subject)
131: : sprintf(_("The message from \"%s\" has been reported as innocent."), $from);
132: break;
133:
134: case self::SPAM:
135: $msg = $subject
136: ? sprintf(_("The message \"%s\" has been reported as spam."), $subject)
137: : sprintf(_("The message from \"%s\" has been reported as spam."), $from);
138: break;
139: }
140: } else {
141: switch ($this->_action) {
142: case self::INNOCENT:
143: $msg = sprintf(_("%d messages have been reported as innocent."), $report_count);
144: break;
145:
146: case self::SPAM:
147: $msg = sprintf(_("%d messages have been reported as spam."), $report_count);
148: break;
149: }
150: }
151: $notification->push($msg, 'horde.message');
152:
153: $mbox_args = array();
154: if (isset($opts['mailboxob'])) {
155: $mbox_args['mailboxob'] = $opts['mailboxob'];
156: }
157:
158:
159: try {
160: $injector->getInstance('Horde_Core_Hooks')->callHook(
161: 'post_spam',
162: 'imp',
163: array(
164: ($this->_action == self::SPAM) ? 'spam' : 'innocent',
165: $indices
166: )
167: );
168: } catch (Horde_Exception_HookNotSet $e) {}
169:
170:
171: switch ($this->_action) {
172: case self::INNOCENT:
173:
174: $imp_message = $injector->getInstance('IMP_Message');
175: $imp_message->flag(array(
176: 'add' => array(Horde_Imap_Client::FLAG_NOTJUNK),
177: 'remove' => array(Horde_Imap_Client::FLAG_JUNK)
178: ), $indices);
179:
180: if (($result = $prefs->getValue('move_innocent_after_report')) &&
181: !$imp_message->copy('INBOX', 'move', $indices, $mbox_args)) {
182: $result = 0;
183: }
184: break;
185:
186: case self::SPAM:
187:
188: $imp_message = $injector->getInstance('IMP_Message');
189: $imp_message->flag(array(
190: 'add' => array(Horde_Imap_Client::FLAG_JUNK),
191: 'remove' => array(Horde_Imap_Client::FLAG_NOTJUNK)
192: ), $indices);
193:
194: switch ($result = $prefs->getValue('delete_spam_after_report')) {
195: case 1:
196: $msg_count = $imp_message->delete($indices, $mbox_args);
197: if ($msg_count === false) {
198: $result = 0;
199: } else {
200: if ($msg_count == 1) {
201: $notification->push(_("The message has been deleted."), 'horde.message');
202: } else {
203: $notification->push(sprintf(_("%d messages have been deleted."), $msg_count), 'horde.message');
204: }
205: }
206: break;
207:
208: case 2:
209: if ($targetMbox = IMP_Mailbox::getPref(IMP_Mailbox::MBOX_SPAM)) {
210: if (!$imp_message->copy($targetMbox, 'move', $indices, array_merge($mbox_args, array('create' => true)))) {
211: $result = 0;
212: }
213: } else {
214: $notification->push(_("Could not move message to spam mailbox - no spam mailbox defined in preferences."), 'horde.error');
215: $result = 0;
216: }
217: break;
218: }
219: break;
220: }
221:
222: return $result;
223: }
224:
225: }
226: