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: class Horde_Kolab_Storage_Driver_Pear
29: extends Horde_Kolab_Storage_Driver_Base
30: {
31: 32: 33: 34: 35:
36: public function createBackend()
37: {
38: $config = $this->getParams();
39: if (isset($config['secure']) && $config['secure'] == 'ssl') {
40: $prefix = 'ssl://';
41: } else {
42: $prefix = '';
43: }
44: $client = new Net_IMAP(
45: $prefix . $config['host'],
46: $config['port'],
47: isset($config['secure']) && $config['secure'] == 'tls'
48: );
49: $client->_useUTF_7 = false;
50: if (isset($config['debug'])) {
51: if ($config['debug'] == 'STDOUT') {
52: $client->setDebug(true);
53: } else {
54: throw new Horde_Kolab_Storage_Exception('This driver does not support debug logging into a file.');
55: }
56: }
57: Horde_Kolab_Storage_Exception_Pear::catchError(
58: $client->login($config['username'], $config['password'], true, false)
59: );
60: return $client;
61: }
62:
63: 64: 65: 66: 67:
68: public function listFolders()
69: {
70: $list = Horde_Kolab_Storage_Exception_Pear::catchError(
71: $this->getBackend()->getMailboxes()
72: );
73: return $this->decodeList($list);
74: }
75:
76: 77: 78: 79: 80: 81: 82:
83: public function create($folder)
84: {
85: Horde_Kolab_Storage_Exception_Pear::catchError(
86: $this->getBackend()->createMailbox($this->encodePath($folder))
87: );
88: }
89:
90: 91: 92: 93: 94: 95: 96:
97: public function delete($folder)
98: {
99: Horde_Kolab_Storage_Exception_Pear::catchError(
100: $this->getBackend()->deleteMailbox($this->encodePath($folder))
101: );
102: }
103:
104: 105: 106: 107: 108: 109: 110: 111:
112: public function rename($old, $new)
113: {
114: Horde_Kolab_Storage_Exception_Pear::catchError(
115: $this->getBackend()->renameMailbox(
116: $this->encodePath($old),
117: $this->encodePath($new)
118: )
119: );
120: }
121:
122: 123: 124: 125: 126:
127: public function hasAclSupport()
128: {
129: return $this->getBackend()->hasCapability('ACL');
130: }
131:
132: 133: 134: 135: 136: 137: 138:
139: public function getAcl($folder)
140: {
141: $result = Horde_Kolab_Storage_Exception_Pear::catchError(
142: $this->getBackend()->getACL($this->encodePath($folder))
143: );
144: $acl = array();
145: foreach ($result as $user) {
146: $acl[$user['USER']] = $user['RIGHTS'];
147: }
148: return $acl;
149: }
150:
151: 152: 153: 154: 155: 156: 157:
158: public function getMyAcl($folder)
159: {
160: return Horde_Kolab_Storage_Exception_Pear::catchError(
161: $this->getBackend()->getMyRights($this->encodePath($folder))
162: );
163: }
164:
165: 166: 167: 168: 169: 170: 171: 172: 173:
174: public function setAcl($folder, $user, $acl)
175: {
176: Horde_Kolab_Storage_Exception_Pear::catchError(
177: $this->getBackend()->setACL($this->encodePath($folder), $user, $acl)
178: );
179: }
180:
181: 182: 183: 184: 185: 186: 187: 188:
189: public function deleteAcl($folder, $user)
190: {
191: Horde_Kolab_Storage_Exception_Pear::catchError(
192: $this->getBackend()->deleteACL($this->encodePath($folder), $user)
193: );
194: }
195:
196: 197: 198: 199: 200: 201: 202: 203:
204: public function listAnnotation($annotation)
205: {
206: list($entry, $value) = $this->_getAnnotateMoreEntry($annotation);
207: $list = array();
208: $result = Horde_Kolab_Storage_Exception_Pear::catchError(
209: $this->getBackend()->getAnnotation($entry, $value, '*')
210: );
211: foreach ($result as $element) {
212: if (isset($element['ATTRIBUTES'][$value])) {
213: $list[$element['MAILBOX']] = $element['ATTRIBUTES'][$value];
214: }
215: }
216: return $this->decodeListKeys($list);
217: }
218:
219: 220: 221: 222: 223: 224: 225: 226:
227: public function getAnnotation($folder, $annotation)
228: {
229: list($entry, $type) = $this->_getAnnotateMoreEntry($annotation);
230: $result = Horde_Kolab_Storage_Exception_Pear::catchError(
231: $this->getBackend()->getAnnotation(
232: $entry, $type, $this->encodePath($folder)
233: )
234: );var_dump($result);
235: foreach ($result as $element) {
236: if (isset($element['ATTRIBUTES'][$type])) {
237: return $element['ATTRIBUTES'][$type];
238: }
239: }
240: return '';
241: }
242:
243: 244: 245: 246: 247: 248: 249: 250: 251:
252: public function setAnnotation($folder, $annotation, $value)
253: {
254: list($entry, $type) = $this->_getAnnotateMoreEntry($annotation);
255: Horde_Kolab_Storage_Exception_Pear::catchError(
256: $this->getBackend()->setAnnotation(
257: $entry, array($type => $value), $this->encodePath($folder)
258: )
259: );
260: }
261:
262: 263: 264: 265: 266:
267: public function getNamespace()
268: {
269: if ($this->getBackend()->hasCapability('NAMESPACE') === true) {
270: $namespaces = array();
271: foreach ($this->getBackend()->getNamespace() as $type => $elements) {
272: foreach ($elements as $namespace) {
273: switch ($type) {
274: case 'others':
275: $namespace['type'] = 'other';
276: break;
277: default:
278: $namespace['type'] = $type;
279: break;
280: }
281: $namespace['delimiter'] = $namespace['delimter'];
282: $namespaces[] = $namespace;
283: }
284: }
285: return new Horde_Kolab_Storage_Folder_Namespace_Imap(
286: $this->getAuth(),
287: $namespaces,
288: $this->getParam('namespaces', array())
289: );
290: }
291: return parent::getNamespace();
292: }
293:
294: 295: 296: 297: 298: 299: 300:
301: public function select($folder)
302: {
303: Horde_Kolab_Storage_Exception_Pear::catchError(
304: $this->getBackend()->selectMailbox($this->encodePath($folder))
305: );
306: }
307:
308: 309: 310: 311: 312: 313: 314:
315: public function status($folder)
316: {
317: $result = Horde_Kolab_Storage_Exception_Pear::catchError(
318: $this->getBackend()->getStatus($this->encodePath($folder))
319: );
320: return array(
321: 'uidvalidity' => $result['UIDVALIDITY'],
322: 'uidnext' => $result['UIDNEXT']
323: );
324: }
325:
326: 327: 328: 329: 330: 331: 332:
333: public function getUids($folder)
334: {
335: $this->select($folder);
336: $uids = Horde_Kolab_Storage_Exception_Pear::catchError(
337: $this->getBackend()->search('UNDELETED', true)
338: );
339: if (!is_array($uids)) {
340: $uids = array();
341: }
342: return $uids;
343: }
344:
345: 346: 347: 348: 349: 350: 351: 352: 353:
354: public function fetchComplete($folder, $uid)
355: {
356: $this->select($folder);
357: return array(
358: Horde_Mime_Headers::parseHeaders(
359: Horde_Kolab_Storage_Exception_Pear::catchError(
360: $this->getBackend()->getRawHeaders($uid, '', true)
361: )
362: ),
363: Horde_Mime_Part::parseMessage(
364: Horde_Kolab_Storage_Exception_Pear::catchError(
365: $this->getBackend()->getBody($uid, true)
366: )
367: )
368: );
369: }
370:
371: 372: 373: 374: 375: 376: 377: 378: 379:
380: public function fetchStructure($folder, $uids)
381: {
382: $result = array();
383:
384: $this->select($folder);
385: foreach ($uids as $uid) {
386: $structure = Horde_Kolab_Storage_Exception_Pear::catchError(
387: $this->getBackend()->getStructure($uid, true)
388: );
389: $ob = $this->_parseStructure($structure);
390: $ob->buildMimeIds();
391: $result[$uid]['structure'] = $ob;
392: }
393: return $result;
394: }
395:
396: 397: 398: 399: 400: 401: 402: 403: 404:
405: public function fetchBodypart($folder, $uid, $id)
406: {
407: $this->select($folder);
408: return Horde_Kolab_Storage_Exception_Pear::catchError(
409: $this->getBackend()->getBodyPart($uid, $id, true)
410: );
411: }
412:
413: 414: 415: 416: 417: 418: 419: 420: 421:
422: public function appendMessage($folder, $msg)
423: {
424: rewind($msg);
425: $this->select($folder);
426: return Horde_Kolab_Storage_Exception_Pear::catchError(
427: $this->getBackend()->appendMessage(stream_get_contents($msg))
428: );
429: }
430:
431: 432: 433: 434: 435: 436: 437: 438:
439: public function deleteMessages($folder, $uids)
440: {
441: $this->select($folder);
442: return Horde_Kolab_Storage_Exception_Pear::catchError(
443: $this->getBackend()->deleteMessages($uids, true)
444: );
445: }
446:
447: 448: 449: 450: 451: 452: 453: 454: 455:
456: public function moveMessage($uid, $old_folder, $new_folder)
457: {
458: $this->select($old_folder);
459: Horde_Kolab_Storage_Exception_Pear::catchError(
460: $this->getBackend()->copyMessage($uid, $new_folder)
461: );
462: $this->deleteMessages($old_folder, array($uid));
463: $this->expunge($old_folder);
464: }
465:
466: 467: 468: 469: 470:
471: public function expunge($folder)
472: {
473: $this->select($folder);
474: return Horde_Kolab_Storage_Exception_Pear::catchError(
475: $this->getBackend()->expunge()
476: );
477: }
478:
479: 480: 481: 482: 483: 484: 485:
486: protected function _parseStructure($data)
487: {
488: $ob = new Horde_Mime_Part();
489:
490: $ob->setType(strtolower($data->type) . '/' . strtolower($data->subType));
491:
492:
493: if (isset($data->parameters)) {
494: $params = array();
495: foreach ($data->parameters as $key => $value) {
496: $params[strtolower($key)] = $value;
497: }
498:
499: $params = Horde_Mime::decodeParam('content-type', $params, 'UTF-8');
500: foreach ($params['params'] as $key => $value) {
501: $ob->setContentTypeParameter($key, $value);
502: }
503: }
504:
505:
506: if (isset($data->disposition)) {
507: $ob->setDisposition($data->disposition);
508: if (isset($data->dparameters)) {
509: $dparams = array();
510: foreach ($data->dparameters as $key => $value) {
511: $dparams[strtolower($key)] = $value;
512: }
513:
514: $dparams = Horde_Mime::decodeParam('content-disposition', $dparams, 'UTF-8');
515: foreach ($dparams['params'] as $key => $value) {
516: $ob->setDispositionParameter($key, $value);
517: }
518: }
519: }
520:
521: if ($ob->getPrimaryType() == 'multipart') {
522:
523: foreach ($data->subParts as $val) {
524: $ob->addPart($this->_parseStructure($val));
525: }
526: } else {
527:
528: if (isset($data->partID)) {
529: $ob->setContentId($data->partID);
530: }
531:
532: $ob->setTransferEncoding(strtolower($data->encoding));
533: $ob->setBytes($data->bytes);
534:
535: if ($ob->getType() == 'message/rfc822') {
536: $ob->addPart($this->_parseStructure(reset($data->subParts)));
537: }
538: }
539:
540: return $ob;
541: }
542: }
543: