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: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58:
59: class IMP_Imap_Config implements Serializable
60: {
61:
62: const PASSWORDS_KEY = 'imap_config_pass';
63:
64: 65: 66: 67: 68:
69: private $_aoptions = array(
70: 'admin', 'cache_params', 'capability_ignore', 'id', 'lang',
71: 'namespace', 'preferred', 'quota', 'smtp', 'spam', 'special_mboxes'
72: );
73:
74: 75: 76: 77: 78:
79: private $_boptions = array(
80: 'acl', 'autocreate_special', 'debug_raw', 'sort_force'
81: );
82:
83: 84: 85: 86: 87:
88: private $_config = array();
89:
90: 91: 92: 93: 94:
95: private $_moptions = array(
96: 'cache', 'hordeauth', 'secure'
97: );
98:
99: 100: 101: 102: 103:
104: private $_passwords = array();
105:
106: 107: 108: 109: 110:
111: private $_poptions = array(
112: 'admin', 'quota'
113: );
114:
115: 116: 117: 118: 119:
120: private $_soptions = array(
121: 'cache_lifetime', 'comparator', 'debug', 'hostspec', 'import_limit',
122: 'maildomain', 'name', 'port', 'protocol', 'thread', 'timeout'
123: );
124:
125: 126: 127: 128: 129:
130: public function __construct(array $c)
131: {
132: foreach ($c as $k => $v) {
133: $this->$k = $v;
134: }
135: }
136:
137: 138:
139: public function __set($name, $value)
140: {
141: if (in_array($name, $this->_poptions) && isset($value['password'])) {
142: $this->_passwords[$name] = $value['password'];
143: unset($value['password']);
144: }
145:
146:
147: switch ($name) {
148: case 'quota':
149: $value['params']['interval'] = isset($value['params']['interval'])
150: ? intval($value['params']['interval'])
151: : 900;
152: break;
153:
154: case 'preferred':
155: if (!is_array($value)) {
156: $value = array($value);
157: }
158: break;
159:
160: case 'protocol':
161: $value = (strcasecmp($value, 'pop') === 0)
162: ? 'pop'
163: : 'imap';
164: break;
165: }
166:
167: if (in_array($name, $this->_aoptions) ||
168: in_array($name, $this->_moptions)) {
169:
170: $this->_config[$name] = $value;
171: } elseif (in_array($name, $this->_boptions)) {
172:
173: $this->_config[$name] = (bool)$value;
174: } elseif (in_array($name, $this->_soptions)) {
175:
176: $this->_config[$name] = strval($value);
177: }
178: }
179:
180: 181:
182: public function __get($name)
183: {
184: global $injector;
185:
186: if (in_array($name, $this->_aoptions)) {
187:
188: $out = isset($this->_config[$name])
189: ? $this->_config[$name]
190: : array();
191:
192: if (isset($this->_passwords[$name])) {
193: $out['password'] = $this->_passwords[$name];
194: }
195: } elseif (in_array($name, $this->_boptions)) {
196:
197: $out = !empty($this->_config[$name]);
198: } elseif (in_array($name, $this->_soptions) ||
199: in_array($name, $this->_moptions)) {
200:
201: $out = isset($this->_config[$name])
202: ? $this->_config[$name]
203: : null;
204: } else {
205: $out = null;
206: }
207:
208: switch ($name) {
209: case 'autocreate_special':
210: $out = ($out && $injector->getInstance('IMP_Factory_Imap')->create()->access(IMP_Imap::ACCESS_FOLDERS));
211: break;
212:
213: case 'cache_params':
214: $ob = null;
215: if ($c = $this->cache) {
216: if ($c instanceof Horde_Imap_Client_Cache_Backend) {
217: $ob = $c;
218: } else {
219: switch ($driver = Horde_String::lower($c)) {
220: case 'hashtable':
221: case 'sql':
222:
223: break;
224:
225: case 'nosql':
226: $db = $injector->getInstance('Horde_Nosql_Adapter');
227: if (!$db instanceof Horde_Mongo_Client) {
228: $driver = null;
229: Horde::log(sprintf('IMAP client package does not support %s as a cache driver.', get_class($db)), 'ERR');
230: }
231: break;
232:
233: case 'cache':
234: 235:
236: default:
237: $driver = 'cache';
238: break;
239: }
240:
241: if (!is_null($driver)) {
242: $ob = new IMP_Imap_Cache_Wrapper($driver, $this->cache_lifetime);
243: }
244: }
245: }
246:
247: if (is_null($ob)) {
248: $ob = new Horde_Imap_Client_Cache_Backend_Cache(array(
249: 'cacheob' => new Horde_Cache(new Horde_Cache_Storage_Mock(), array(
250: 'compress' => true
251: ))
252: ));
253: }
254:
255: $out = array('backend' => $ob);
256: break;
257:
258: case 'import_limit':
259: $out = is_null($out)
260: ? 2500
261: : intval($out);
262: break;
263:
264: case 'innocent_params':
265: $p = $this->spam;
266: $out = isset($p['innocent'])
267: ? $p['innocent']
268: : array();
269: break;
270:
271: case 'maildomain':
272:
273: if (!strlen($out)) {
274: $out = null;
275: }
276: break;
277:
278: case 'port':
279: if (is_null($out)) {
280: if ($this->protocol === 'imap') {
281: $out = ($this->secure === 'ssl') ? 993 : 143;
282: } else {
283: $out = ($this->secure === 'ssl') ? 995 : 110;
284: }
285: } else {
286: $out = intval($out);
287: }
288: break;
289:
290: case 'protocol':
291: if (is_null($out)) {
292: $out = 'imap';
293: }
294: break;
295:
296: case 'smtp':
297: if (empty($out['horde_auth'])) {
298: if (!isset($out['username'])) {
299: $out['username'] = $injector->getInstance('IMP_Factory_Imap')->create()->getParam('username');
300: }
301: if (!isset($out['password'])) {
302: $out['password'] = $injector->getInstance('IMP_Factory_Imap')->create()->getParam('password');
303: }
304: }
305: break;
306:
307: case 'spam_params':
308: $p = $this->spam;
309: $out = isset($p['spam'])
310: ? $p['spam']
311: : array();
312: break;
313:
314: case 'thread':
315: if (is_null($out)) {
316: $out = 'REFERENCES';
317: }
318: break;
319:
320: case 'user_special_mboxes':
321: $out = (isset($out[IMP_Mailbox::MBOX_USERSPECIAL]) && is_array($out[IMP_Mailbox::MBOX_USERSPECIAL]))
322: ? $out[IMP_Mailbox::MBOX_USERSPECIAL]
323: : array();
324: break;
325: }
326:
327: return $out;
328: }
329:
330: 331:
332: public function __isset($name)
333: {
334: $tmp = $this->$name;
335: return !empty($tmp);
336: }
337:
338: 339:
340: public function __unset($name)
341: {
342: unset($this->_config[$name]);
343: }
344:
345:
346:
347: 348:
349: public function serialize()
350: {
351: global $injector, $session;
352:
353: if (!empty($this->_passwords)) {
354: $session->set('imp', self::PASSWORDS_KEY, $this->_passwords, $session::ENCRYPT);
355: }
356:
357: return $injector->getInstance('Horde_Pack')->pack(
358: array_filter($this->_config),
359: array(
360: 'compression' => false,
361: 'phpob' => false
362: )
363: );
364: }
365:
366: 367:
368: public function unserialize($data)
369: {
370: global $injector, $session;
371:
372: $this->_config = $injector->getInstance('Horde_Pack')->unpack($data);
373:
374: $this->_passwords = $session->get('imp', self::PASSWORDS_KEY, $session::TYPE_ARRAY);
375: }
376:
377: }
378: