1: <?php
2: /**
3: * Copyright 2013-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 2013-2014 Horde LLC
10: * @license http://www.fsf.org/copyleft/gpl.html GPL
11: * @package IMP
12: */
13:
14: /**
15: * Interface to deal with storing connection details of remote accounts.
16: *
17: * @author Michael Slusarz <slusarz@horde.org>
18: * @category Horde
19: * @copyright 2013-2014 Horde LLC
20: * @license http://www.horde.org/licenses/gpl GPL
21: * @package IMP
22: */
23: class IMP_Remote implements ArrayAccess, IteratorAggregate
24: {
25: /* The mailbox remote prefix. */
26: const MBOX_PREFIX = "remotembox\0";
27:
28: /**
29: * The list of remote accounts.
30: *
31: * @var array
32: */
33: protected $_accounts;
34:
35: /**
36: * Constructor.
37: */
38: public function __construct()
39: {
40: $this->_accounts = @unserialize($GLOBALS['prefs']->getValue('remote')) ?: array();
41: }
42:
43: /**
44: * Save the remote accounts list to the prefs backend.
45: */
46: protected function _save()
47: {
48: $GLOBALS['prefs']->setValue('remote', serialize($this->_accounts));
49: }
50:
51: /**
52: * Is the given mailbox a remote mailbox?
53: *
54: * @param string $id The mailbox name/identifier.
55: *
56: * @return boolean Whether the given mailbox name is a remote mailbox.
57: */
58: public function isRemoteMbox($id)
59: {
60: return (strpos($id, self::MBOX_PREFIX) === 0);
61: }
62:
63: /**
64: * Return the remote account for a valid remote mailbox/identifier.
65: *
66: * @param string $id The mailbox name/identifier.
67: *
68: * @return mixed Either a IMP_Remote_Account object or null.
69: */
70: public function getRemoteById($id)
71: {
72: return ($this->isRemoteMbox($id) && (count($parts = explode("\0", $id)) > 1))
73: ? $this[implode("\0", array_slice($parts, 0, 2))]
74: : null;
75: }
76:
77: /**
78: * Return the IMAP mailbox name for the given remote mailbox identifier.
79: *
80: * @param string $id The mailbox name/identifier.
81: *
82: * @return string The IMAP mailbox name.
83: */
84: public function getMailboxById($id)
85: {
86: return ($account = $this->getRemoteById($id))
87: ? substr($id, strlen($account) + 1)
88: : '';
89: }
90:
91: /**
92: * Return the label for the given mailbox.
93: *
94: * @param string $id The mailbox name/identifier.
95: *
96: * @return string The mailbox label.
97: */
98: public function label($id)
99: {
100: if (isset($this[$id])) {
101: return $this[$id]->label;
102: }
103:
104: $label = strval($this->getMailboxById($id));
105:
106: return (strcasecmp($label, 'INBOX') === 0)
107: ? _("Inbox")
108: : $label;
109: }
110:
111: /**
112: * Strip the identifying label from a mailbox ID.
113: *
114: * @param string $id The mailbox query ID.
115: *
116: * @return string The remote ID, with any IMP specific identifying
117: information stripped off.
118: */
119: protected function _strip($id)
120: {
121: return $this->isRemoteMbox($id)
122: ? substr($id, strlen(self::MBOX_PREFIX))
123: : strval($id);
124: }
125:
126: /* ArrayAccess methods. */
127:
128: /**
129: * Does the account ID exist?
130: *
131: * @param string $offset Account ID.
132: *
133: * @return boolean True if the account ID exists.
134: */
135: public function offsetExists($offset)
136: {
137: return isset($this->_accounts[$this->_strip($offset)]);
138: }
139:
140: /**
141: * Retrieve information on a single remote account.
142: *
143: * @param string $offset Account ID.
144: *
145: * @return array The configuration array, or false if ID not found.
146: */
147: public function offsetGet($offset)
148: {
149: $offset = $this->_strip($offset);
150:
151: return isset($this->_accounts[$offset])
152: ? $this->_accounts[$offset]
153: : false;
154: }
155:
156: /**
157: * Add a remote account.
158: *
159: * @param string $offset Account ID.
160: * @param IMP_Remote_Account $ob Account object.
161: */
162: public function offsetSet($offset, $value)
163: {
164: $this->_accounts[$this->_strip($offset)] = $value;
165: $this->_save();
166: }
167:
168: /**
169: * Delete a remote account.
170: *
171: * @param string $offset Account ID.
172: */
173: public function offsetUnset($offset)
174: {
175: $offset = $this->_strip($offset);
176:
177: if (isset($this->_accounts[$offset])) {
178: unset($this->_accounts[$offset]);
179: $this->_save();
180: }
181: }
182:
183: /* IteratorAggregate method. */
184:
185: /**
186: */
187: public function getIterator()
188: {
189: return new ArrayIterator($this->_accounts);
190: }
191:
192: }
193: