1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class IMP_Notification_Handler_Decorator_NewmailNotify
24: extends Horde_Core_Notification_Handler_Decorator_Base
25: {
26:
27: const RATELIMIT = 30;
28:
29:
30: const SESS_RATELIMIT = 'newmail_ratelimit';
31:
32: 33:
34: protected $_app = 'imp';
35:
36: 37:
38: protected function _notify(
39: Horde_Notification_Handler $handler,
40: Horde_Notification_Listener $listener
41: )
42: {
43: global $injector, $prefs, $session;
44:
45: if (!$prefs->getValue('newmail_notify') ||
46: !($listener instanceof Horde_Notification_Listener_Status)) {
47: return;
48: }
49:
50: 51:
52: $curr = time();
53: $ratelimit = $session->get('imp', self::SESS_RATELIMIT);
54: if ($ratelimit && (($ratelimit + self::RATELIMIT) > $curr)) {
55: return;
56: }
57: $session->set('imp', self::SESS_RATELIMIT, $curr);
58: if (!$ratelimit) {
59: return;
60: }
61:
62: $ajax_queue = $injector->getInstance('IMP_Ajax_Queue');
63: $imp_imap = $injector->getInstance('IMP_Factory_Imap')->create();
64: $recent = array();
65:
66: try {
67: foreach ($imp_imap->status($injector->getInstance('IMP_Ftree')->poll->getPollList(), Horde_Imap_Client::STATUS_RECENT_TOTAL, array('sort' => true)) as $key => $val) {
68: if (!empty($val['recent_total'])) {
69: 70:
71: $imp_imap->openMailbox($key, Horde_Imap_Client::OPEN_READWRITE);
72:
73: $mbox = IMP_Mailbox::get($key);
74: $recent[$mbox->display] = $val['recent_total'];
75: $ajax_queue->poll($mbox);
76: }
77: }
78: } catch (Exception $e) {}
79:
80: if (empty($recent)) {
81: return;
82: }
83:
84: $recent_sum = array_sum($recent);
85: reset($recent);
86:
87: switch (count($recent)) {
88: case 1:
89: $mbox_list = key($recent);
90: break;
91:
92: case 2:
93: $mbox_list = implode(_(" and "), array_keys($recent));
94: break;
95:
96: default:
97: $akeys = array_keys($recent);
98: $mbox_list = $akeys[0] . ', ' . $akeys[1] . ', ' . _("and") . ' ' . $akeys[2];
99: if ($addl_mbox = count($recent) - 3) {
100: $mbox_list .= ' (' . sprintf(ngettext("and %d more mailbox", "and %d more mailboxes", $addl_mbox), $addl_mbox) . ')';
101: }
102: break;
103: }
104:
105: $text = sprintf(
106: ngettext(
107: "You have %d new mail message in %s.",
108: "You have %d new mail messages in %s.",
109: $recent_sum
110: ),
111: $recent_sum,
112: $mbox_list
113: );
114:
115:
116: $handler->push($text, 'horde.message');
117:
118:
119: $handler->attach('webnotification', null, 'Horde_Core_Notification_Listener_Webnotification');
120: $handler->push(
121: Horde_Core_Notification_Event_Webnotification::createEvent(
122: $text,
123: array('icon' => strval(Horde_Themes::img('unseen.png')))
124: ),
125: 'webnotification'
126: );
127:
128: if ($audio = $prefs->getValue('newmail_audio')) {
129: $handler->attach('audio');
130: $handler->push(Horde_Themes::sound($audio), 'audio');
131: }
132: }
133:
134: }
135: