1: <?php
2: /**
3: * A Horde_Injector:: based IMP_Imap:: factory.
4: *
5: * PHP version 5
6: *
7: * @author Michael Slusarz <slusarz@horde.org>
8: * @category Horde
9: * @license http://www.horde.org/licenses/gpl GPL
10: * @link http://pear.horde.org/index.php?package=IMP
11: * @package IMP
12: */
13:
14: /**
15: * A Horde_Injector:: based IMP_Imap:: factory.
16: *
17: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
18: *
19: * See the enclosed file COPYING for license information (GPL). If you
20: * did not receive this file, see http://www.horde.org/licenses/gpl.
21: *
22: * @author Michael Slusarz <slusarz@horde.org>
23: * @category Horde
24: * @license http://www.horde.org/licenses/gpl GPL
25: * @link http://pear.horde.org/index.php?package=IMP
26: * @package IMP
27: */
28: class IMP_Factory_Imap extends Horde_Core_Factory_Base
29: {
30: /**
31: * Name of the instance used for the initial authentication.
32: * Needed because the session may not be setup yet to indicate the
33: * default instance to use.
34: *
35: * @var string
36: */
37: private $_authInstance;
38:
39: /**
40: * Instances.
41: *
42: * @var array
43: */
44: private $_instances = array();
45:
46: /**
47: * The list of instances to save.
48: *
49: * @var array
50: */
51: private $_save = array();
52:
53: /**
54: * Return the IMP_Imap:: instance.
55: *
56: * @param string $id The server ID.
57: * @param boolean $save Save the instance in the session?
58: *
59: * @return IMP_Imap The singleton instance.
60: * @throws IMP_Exception
61: */
62: public function create($id = null, $save = false)
63: {
64: global $injector, $registry, $session;
65:
66: if (is_null($id) &&
67: !($id = $session->get('imp', 'server_key')) &&
68: !($id = $this->_authInstance)) {
69: $id = 'default';
70: }
71:
72: if (!isset($this->_instances[$id])) {
73: if (empty($this->_instances)) {
74: register_shutdown_function(array($this, 'shutdown'));
75: }
76:
77: try {
78: $ob = $session->get('imp', 'imap_ob/' . $id);
79: } catch (Exception $e) {
80: // This indicates an unserialize() error. This is fatal, so
81: // logout.
82: $injector->getInstance('Horde_Core_Factory_Auth')->create()->setError(Horde_Auth::REASON_SESSION);
83: $registry->authenticateFailure('imp');
84: }
85:
86: if ($ob) {
87: /* If retrieved from session, we know $save should implicitly
88: * be true. */
89: $this->_save[] = $id;
90: } else {
91: $ob = new IMP_Imap();
92: $this->_authInstance = $id;
93: }
94:
95: $this->_instances[$id] = $ob;
96: }
97:
98: if ($save) {
99: $this->_save[] = $id;
100: /* Explicitly save object when first creating. Prevents losing
101: * authentication information in case a misconfigured server
102: * crashes before shutdown operations can occur. */
103: $session->set('imp', 'imap_ob/' . $id, $this->_instances[$id]);
104: }
105:
106: return $this->_instances[$id];
107: }
108:
109: /**
110: * Saves IMP_Imap instances to the session on shutdown.
111: */
112: public function shutdown()
113: {
114: if ($GLOBALS['registry']->getAuth() !== false) {
115: foreach (array_unique($this->_save) as $id) {
116: if ($this->_instances[$id]->changed) {
117: $GLOBALS['session']->set('imp', 'imap_ob/' . $id, $this->_instances[$id]);
118: }
119: }
120: }
121: }
122:
123: }
124: