1: <?php
2: /**
3: * Copyright 2010-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 2010-2014 Horde LLC
10: * @license http://www.horde.org/licenses/gpl GPL
11: * @package IMP
12: */
13:
14: /**
15: * A Horde_Injector based IMP_Imap factory.
16: *
17: * @author Michael Slusarz <slusarz@horde.org>
18: * @category Horde
19: * @copyright 2010-2014 Horde LLC
20: * @license http://www.horde.org/licenses/gpl GPL
21: * @package IMP
22: */
23: class IMP_Factory_Imap extends Horde_Core_Factory_Base implements Horde_Shutdown_Task
24: {
25: const BASE_OB = "base\0";
26:
27: /**
28: * List of IMP_Imap instances.
29: *
30: * @var array
31: */
32: private $_instance = array();
33:
34: /**
35: */
36: public function __construct(Horde_Injector $injector)
37: {
38: parent::__construct($injector);
39:
40: Horde_Shutdown::add($this);
41: }
42:
43: /**
44: * Return the IMP_Imap instance for a given mailbox/identifier.
45: *
46: * @param string $id Mailbox/identifier.
47: *
48: * @return IMP_Imap IMP_Imap object.
49: */
50: public function create($id = null)
51: {
52: global $registry, $session;
53:
54: if (!is_null($id) &&
55: ($registry->getAuth() !== false) &&
56: ($base = $this->_injector->getInstance('IMP_Remote')->getRemoteById($id))) {
57: $id = strval($base);
58: } else {
59: $base = null;
60: $id = self::BASE_OB;
61: }
62:
63: if (!isset($this->_instance[$id])) {
64: $ob = null;
65: try {
66: $ob = $session->get('imp', 'imap_ob/' . $id);
67: } catch (Exception $e) {
68: // This indicates an unserialize() error. This is fatal, so
69: // logout.
70: throw new Horde_Exception_AuthenticationFailure(
71: 'Cached IMP session data has become invalid; expiring session.',
72: Horde_Auth::REASON_SESSION
73: );
74: }
75:
76: if (!is_object($ob)) {
77: $ob = (is_null($base))
78: ? new IMP_Imap($id)
79: : new IMP_Imap_Remote($id);
80: }
81:
82: $this->_instance[$id] = $ob;
83: }
84:
85: return $this->_instance[$id];
86: }
87:
88: /**
89: * Destroys an IMP_Imap instance.
90: *
91: * @param string $id Mailbox/identifier.
92: */
93: public function destroy($id)
94: {
95: global $session;
96:
97: $id = strval($id);
98:
99: if ($id != self::BASE_OB) {
100: $session->remove('imp', 'imap_ob/' . $id);
101: unset($this->_instance[$id]);
102: }
103: }
104:
105: /**
106: * Saves IMP_Imap instance to the session on shutdown.
107: */
108: public function shutdown()
109: {
110: global $registry, $session;
111:
112: if ($registry->getAuth() !== false) {
113: foreach ($this->_instance as $key => $val) {
114: if ($val->changed) {
115: $session->set('imp', 'imap_ob/' . $key, $val);
116: }
117: }
118: }
119: }
120:
121: /**
122: * Returns IMAP alerts from all servers contacted this access.
123: *
124: * @return array IMAP alerts.
125: */
126: public function alerts()
127: {
128: $out = array();
129:
130: foreach ($this->_instance as $val) {
131: if ($val->init) {
132: $out = array_merge($out, $val->alerts());
133: }
134: }
135:
136: return $out;
137: }
138:
139: }
140: