1: <?php
2: /**
3: * Copyright 2002-2014 Horde LLC (http://www.horde.org/)
4: *
5: * See the enclosed file COPYING for license information (GPL). If you
6: * did not receive this file, see http://www.horde.org/licenses/gpl.
7: *
8: * @category Horde
9: * @copyright 2002-2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * The IMP_Filter class contains all functions related to handling filtering
16: * messages in IMP.
17: *
18: * For full use, the following Horde API calls should be defined
19: * (These API methods are not defined in IMP):
20: * - mail/applyFilters
21: * - mail/canApplyFilters
22: * - mail/showFilters
23: * - mail/blacklistFrom
24: * - mail/showBlacklist
25: * - mail/whitelistFrom
26: * - mail/showWhitelist
27: *
28: * @author Michael Slusarz <slusarz@horde.org>
29: * @category Horde
30: * @copyright 2002-2014 Horde LLC
31: * @license http://www.horde.org/licenses/gpl GPL
32: * @package IMP
33: */
34: class IMP_Filter
35: {
36: /**
37: * Runs the filters if they are able to be applied manually.
38: *
39: * @param string $mbox The mailbox to apply the filters to.
40: * @throws Horde_Exception
41: */
42: public function filter($mbox)
43: {
44: if (!self::canApplyFilters()) {
45: return;
46: }
47:
48: $imp_search = $GLOBALS['injector']->getInstance('IMP_Search');
49: $mbox_list = $imp_search->isSearchMbox($mbox)
50: ? $imp_search[$mbox]->mboxes
51: : array($mbox);
52:
53: foreach ($mbox_list as $val) {
54: $GLOBALS['registry']->call('mail/applyFilters', array(array('mailbox' => strval($val))));
55: }
56: }
57:
58: /**
59: * Adds the From address from the message(s) to the blacklist and deletes
60: * the message(s).
61: *
62: * @param IMP_Indices $indices An indices object.
63: * @param boolean $show_link Show link to the blacklist management in
64: * the notification message?
65: *
66: * @return boolean True if the messages(s) were deleted.
67: * @throws Horde_Exception
68: */
69: public function blacklistMessage($indices, $show_link = true)
70: {
71: if (!$this->_processBWlist($indices, _("your blacklist"), 'blacklistFrom', 'showBlacklist', $show_link) ||
72: !($msg_count = $GLOBALS['injector']->getInstance('IMP_Message')->delete($indices))) {
73: return false;
74: }
75:
76: $GLOBALS['notification']->push(ngettext("The message has been deleted.", "The messages have been deleted.", $msg_count), 'horde.message');
77:
78: return true;
79: }
80:
81: /**
82: * Adds the From address from the message(s) to the whitelist.
83: *
84: * @param IMP_Indices $indices An indices object.
85: * @param boolean $show_link Show link to the whitelist management in
86: * the notification message?
87: *
88: * @return boolean True if the messages(s) were whitelisted.
89: * @throws Horde_Exception
90: */
91: public function whitelistMessage($indices, $show_link = true)
92: {
93: return $this->_processBWlist($indices, _("your whitelist"), 'whitelistFrom', 'showWhitelist', $show_link);
94: }
95:
96: /**
97: * Internal function to handle adding addresses to [black|white]list.
98: *
99: * @param IMP_Indices $indices An indices object.
100: * @param string $descrip The textual description to use.
101: * @param string $reg1 The name of the mail/ registry call to use
102: * for adding the addresses.
103: * @param string $reg2 The name of the mail/ registry call to use
104: * for linking to the filter management page.
105: * @param boolean $link Show link to the whitelist management in
106: * the notification message?
107: *
108: * @return boolean True on success.
109: * @throws IMP_Exception
110: */
111: protected function _processBWlist($indices, $descrip, $reg1, $reg2, $link)
112: {
113: if (!count($indices)) {
114: return false;
115: }
116:
117: $addr = new Horde_Mail_Rfc822_List();
118:
119: foreach ($indices as $ob) {
120: $ob->mbox->uidvalid;
121:
122: foreach ($ob->uids as $idx) {
123: /* Get the list of from addresses. */
124: $addr->add($GLOBALS['injector']->getInstance('IMP_Factory_Contents')->create($ob->mbox->getIndicesOb($idx))->getHeader()->getOb('from'));
125: }
126: }
127:
128: $GLOBALS['registry']->call('mail/' . $reg1, array($addr->bare_addresses));
129:
130: /* Add link to filter management page. */
131: if ($link && $GLOBALS['registry']->hasMethod('mail/' . $reg2)) {
132: $manage_link = Horde::link(Horde::url($GLOBALS['registry']->link('mail/' . $reg2)), sprintf(_("Filters: %s management page"), $descrip)) . _("HERE") . '</a>';
133: $GLOBALS['notification']->push(sprintf(_("Click %s to go to %s management page."), $manage_link, $descrip), 'horde.message', array('content.raw'));
134: }
135:
136: return true;
137: }
138:
139: /* Static methods. */
140:
141: /**
142: * Are appliable filters available?
143: *
144: * @return voolean True if appliable filters are available.
145: */
146: public static function canApplyFilters()
147: {
148: global $registry, $session;
149:
150: if (!$session->exists('imp', 'filteravail')) {
151: $apply = false;
152: try {
153: $apply = $registry->call('mail/canApplyFilters');
154: } catch (Horde_Exception $e) {}
155: $session->set('imp', 'filteravail', $apply);
156: }
157:
158: return $session->get('imp', 'filteravail');
159: }
160:
161: }
162: