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: class IMP_Mailbox_SessionCache implements Serializable
26: {
27:
28: const CHANGED_NO = 0;
29: const CHANGED_YES = 1;
30:
31:
32: const CACHE_ACL = 'a';
33: const CACHE_DISPLAY = 'd';
34: const CACHE_ICONS = 'i';
35: const CACHE_LABEL = 'l';
36: const CACHE_UIDVALIDITY = 'v';
37:
38:
39: const CACHE_EXISTS = 'e';
40: const CACHE_ICONHOOK = 'ic';
41: const CACHE_PREFTO = 'pt';
42: const CACHE_SPECIALMBOXES = 's';
43:
44: 45: 46: 47: 48:
49: protected $_cache = array();
50:
51: 52: 53: 54: 55:
56: protected $_changed = self::CHANGED_NO;
57:
58: 59: 60: 61: 62:
63: protected $_temp = array();
64:
65: 66:
67: public function __get($name)
68: {
69: switch ($name) {
70: case 'changed':
71: return $this->_changed;
72: }
73: }
74:
75: 76: 77: 78: 79: 80: 81: 82:
83: public function getAcl($mbox)
84: {
85: if (!isset($this->_cache[$mbox]) ||
86: !array_key_exists(self::CACHE_ACL, $this->_cache[$mbox])) {
87: return false;
88: }
89:
90: return is_null($this->_cache[$mbox][self::CACHE_ACL])
91: ? null
92: : new Horde_Imap_Client_Data_Acl($this->_cache[$mbox][self::CACHE_ACL]);
93: }
94:
95: 96: 97: 98: 99: 100:
101: public function setAcl($mbox, $acl)
102: {
103: 104:
105: $this->_cache[$mbox][self::CACHE_ACL] = is_null($acl)
106: ? $acl
107: : strval($acl);
108: $this->_changed = self::CHANGED_YES;
109: }
110:
111: 112: 113: 114: 115: 116: 117:
118: public function getLabel($mbox)
119: {
120: if (!isset($this->_cache[$mbox][self::CACHE_LABEL])) {
121: return false;
122: }
123:
124: return ($this->_cache[$mbox][self::CACHE_LABEL] === true)
125: ? $this->_cache[$mbox][self::CACHE_DISPLAY]
126: : $this->_cache[$mbox][self::CACHE_LABEL];
127: }
128:
129: 130: 131: 132: 133: 134:
135: public function setLabel($mbox, $label)
136: {
137: $this->_cache[$mbox][self::CACHE_LABEL] = (isset($this->_cache[$mbox][self::CACHE_DISPLAY]) && ($this->_cache[$mbox][self::CACHE_DISPLAY] == $label))
138: ? true
139: : $label;
140: $this->_changed = self::CHANGED_YES;
141: }
142:
143: 144: 145: 146: 147: 148: 149:
150: public function getPrefTo($mbox)
151: {
152: return isset($this->_temp[$mbox][self::CACHE_PREFTO])
153: ? $this->_temp[$mbox][self::CACHE_PREFTO]
154: : false;
155: }
156:
157: 158: 159: 160: 161: 162:
163: public function setPrefTo($mbox, $pref_to)
164: {
165: $this->_temp[$mbox][self::CACHE_PREFTO] = $pref_to;
166: $this->_changed = self::CHANGED_YES;
167: }
168:
169: 170: 171: 172: 173: 174: 175:
176: public function getUidvalidity($mbox)
177: {
178: return isset($this->_cache[$mbox][self::CACHE_UIDVALIDITY])
179: ? $this->_cache[$mbox][self::CACHE_UIDVALIDITY]
180: : false;
181: }
182:
183: 184: 185: 186: 187: 188:
189: public function setUidvalidity($mbox, $uidvalid)
190: {
191: $this->_cache[$mbox][self::CACHE_UIDVALIDITY] = $uidvalid;
192: $this->_changed = self::CHANGED_YES;
193: }
194:
195: 196: 197: 198: 199: 200: 201:
202: public function getDisplay($mbox)
203: {
204: return isset($this->_cache[$mbox][self::CACHE_DISPLAY])
205: ? $this->_cache[$mbox][self::CACHE_DISPLAY]
206: : false;
207: }
208:
209: 210: 211: 212: 213: 214:
215: public function setDisplay($mbox, $display)
216: {
217: $this->_cache[$mbox][self::CACHE_DISPLAY] = $display;
218: $this->_changed = self::CHANGED_YES;
219: }
220:
221: 222: 223: 224: 225: 226: 227:
228: public function getIcons($mbox)
229: {
230: global $injector;
231:
232: if (isset($this->_cache[$mbox][self::CACHE_ICONS])) {
233: return $this->_cache[$mbox][self::CACHE_ICONS];
234: }
235:
236: if (!isset($this->_temp[self::CACHE_ICONHOOK])) {
237: try {
238: $this->_temp[self::CACHE_ICONHOOK] = $injector->getInstance('Horde_Core_Hooks')->callHook('mbox_icons', 'imp');
239: } catch (Horde_Exception_HookNotSet $e) {
240: $this->_temp[self::CACHE_ICONHOOK] = array();
241: }
242: }
243:
244: $icons = isset($this->_temp[self::CACHE_ICONHOOK][$mbox])
245: ? $this->_temp[self::CACHE_ICONHOOK][$mbox]
246: : false;
247:
248: $this->_cache[$mbox][self::CACHE_ICONS] = $icons;
249: $this->_changed = self::CHANGED_YES;
250:
251: return $icons;
252: }
253:
254: 255: 256: 257: 258: 259: 260: 261:
262: public function getSpecialMailboxes()
263: {
264: global $injector, $prefs;
265:
266: if (!isset($this->_temp[self::CACHE_SPECIALMBOXES])) {
267: $sm = array(
268: IMP_Mailbox::SPECIAL_COMPOSETEMPLATES => IMP_Mailbox::getPref(IMP_Mailbox::MBOX_TEMPLATES),
269: IMP_Mailbox::SPECIAL_DRAFTS => IMP_Mailbox::getPref(IMP_Mailbox::MBOX_DRAFTS),
270: IMP_Mailbox::SPECIAL_SENT => $injector->getInstance('IMP_Identity')->getAllSentmail(),
271: IMP_Mailbox::SPECIAL_SPAM => IMP_Mailbox::getPref(IMP_Mailbox::MBOX_SPAM),
272: IMP_Mailbox::SPECIAL_TRASH => $prefs->getValue('use_trash') ? IMP_Mailbox::getPref(IMP_Mailbox::MBOX_TRASH) : null,
273: IMP_Mailbox::SPECIAL_USER => array()
274: );
275:
276: foreach ($injector->getInstance('IMP_Factory_Imap')->create()->config->user_special_mboxes as $key => $val) {
277: $ob = IMP_Mailbox::get($key);
278: $ob->display = $val;
279: $sm[IMP_Mailbox::SPECIAL_USER][strval($key)] = $ob;
280: }
281:
282: $this->_temp[self::CACHE_SPECIALMBOXES] = $sm;
283: }
284:
285: return $this->_temp[self::CACHE_SPECIALMBOXES];
286: }
287:
288: 289: 290: 291: 292: 293: 294:
295: public function exists($mbox)
296: {
297: $s_mbox = strval($mbox);
298:
299: if (!isset($this->_temp[$s_mbox][self::CACHE_EXISTS])) {
300: $mbox = IMP_Mailbox::get($mbox);
301:
302: if ($mbox->search) {
303: $exists = (($ob = $mbox->getSearchOb()) && $ob->enabled);
304: } elseif ($elt = $mbox->tree_elt) {
305: $exists = !$elt->container;
306: } else {
307: try {
308: $mbox_list = $mbox->imp_imap->listMailboxes(
309: $mbox->imap_mbox_ob,
310: null,
311: array('attributes' => true)
312: );
313: $exists = (isset($mbox_list[strval($mbox)]) &&
314: !in_array('\\noselect', $mbox_list[strval($mbox)]['attributes']));
315: } catch (IMP_Imap_Exception $e) {
316: $exists = false;
317: }
318: }
319:
320: $this->_temp[$s_mbox][self::CACHE_EXISTS] = $exists;
321: }
322:
323: return $this->_temp[$s_mbox][self::CACHE_EXISTS];
324: }
325:
326: 327: 328: 329: 330: 331: 332: 333:
334: public function expire($entries, $mbox = null)
335: {
336: if (is_null($entries)) {
337: $entries = array(
338: self::CACHE_ACL,
339: self::CACHE_DISPLAY,
340: self::CACHE_EXISTS,
341: self::CACHE_ICONS,
342: self::CACHE_LABEL,
343: self::CACHE_PREFTO,
344: self::CACHE_UIDVALIDITY
345: );
346: } elseif (!is_array($entries)) {
347: $entries = array($entries);
348: }
349:
350: if (in_array(self::CACHE_DISPLAY, $entries)) {
351: $entries[] = self::CACHE_LABEL;
352: }
353:
354: foreach ($entries as $val) {
355: switch ($val) {
356: case self::CACHE_ACL:
357: case self::CACHE_DISPLAY:
358: case self::CACHE_EXISTS:
359: case self::CACHE_ICONS:
360: case self::CACHE_LABEL:
361: case self::CACHE_PREFTO:
362: case self::CACHE_UIDVALIDITY:
363: if (!isset($mbox_list)) {
364: $mbox_list = $mbox
365: ? array(strval($mbox))
366: : array_merge(array_keys($this->_cache), array_keys($this->_temp));
367: }
368:
369: foreach ($mbox_list as $val2) {
370: if (isset($this->_cache[$val2][$val])) {
371: $this->_changed = self::CHANGED_YES;
372: }
373: unset(
374: $this->_cache[$val2][$val],
375: $this->_temp[$val2][$val]
376: );
377: }
378: break;
379:
380: case self::CACHE_ICONHOOK:
381: case self::CACHE_SPECIALMBOXES:
382: if (($c = isset($this->_cache[$val])) ||
383: isset($this->_temp[$val])) {
384: if ($c) {
385: $this->_changed = self::CHANGED_YES;
386: }
387: unset($this->_cache[$val], $this->_temp[$val]);
388: }
389: break;
390: }
391: }
392: }
393:
394:
395:
396: 397:
398: public function serialize()
399: {
400: return $GLOBALS['injector']->getInstance('Horde_Pack')->pack(
401: $this->_cache,
402: array(
403: 'compression' => false,
404: 'phpob' => false
405: )
406: );
407: }
408:
409: 410:
411: public function unserialize($data)
412: {
413: $this->_cache = $GLOBALS['injector']->getInstance('Horde_Pack')->unpack($data);
414: }
415:
416: }
417: