1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14: class Horde_Imsp_Book
15: {
16: 17: 18: 19:
20: const ACL_RIGHTS = 'lrwcda';
21:
22: 23: 24: 25: 26:
27: public $sort = 'ascend';
28:
29: 30: 31: 32: 33:
34: protected $_imsp;
35:
36: 37: 38: 39: 40:
41: protected $_params;
42:
43: 44: 45: 46: 47:
48: public function __construct(Horde_Imsp_Client_Base $client, array $params)
49: {
50: $this->_params = $params;
51: $this->_imsp = $client;
52: }
53:
54: 55: 56: 57: 58: 59:
60: public function getAddressBookList()
61: {
62: $command_string = 'ADDRESSBOOK *';
63:
64: $this->_imsp->send($command_string);
65:
66: 67:
68: $server_response = $this->_imsp->receive();
69: $abooks = array();
70: while (preg_match("/^\* ADDRESSBOOK/", $server_response)) {
71: 72: 73: 74: 75: 76:
77:
78:
79: if (preg_match(Horde_Imsp_Client_Socket::OCTET_COUNT, $server_response, $tempArray)) {
80: $abooks[] = $this->_imsp->receiveStringLiteral($tempArray[2]);
81: 82:
83: $this->_imsp->receiveStringLiteral(2);
84: } else {
85: $parts = explode(' ', $server_response);
86: $numParts = count($parts);
87: $name = $parts[4];
88: $firstChar = substr($name, 0, 1);
89: if ($firstChar =="\"") {
90: $name = ltrim($name, "\"");
91: for ($i = 5; $i < $numParts; $i++) {
92: $name .= ' ' . $parts[$i];
93: $lastChar = substr($parts[$i], strlen($parts[$i]) - 1, 1);
94: if ($lastChar == "\"") {
95: $name = rtrim($name, "\"");
96: break;
97: }
98: }
99: }
100: $abooks[] = $name;
101: }
102: $server_response = $this->_imsp->receive();
103: }
104:
105: if ($server_response != 'OK') {
106: $this->_imsp->_logger->err('Did not receive expected response frm server.');
107: throw new Horde_Imsp_Exception('Did not receive the expected response from the server.');
108: }
109: $this->_imsp->_logger->debug('ADDRESSBOOK command OK.');
110:
111: return $abooks;
112: }
113:
114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124:
125: public function search($abook, $search)
126: {
127:
128: $criteria = array();
129: if (!is_array($search)) {
130: $criteria['name'] = $search;
131: } else {
132: $criteria = $search;
133: }
134:
135: $this->_imsp->send('SEARCHADDRESS ', true, false);
136:
137:
138: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $abook)) {
139: $biBook = sprintf("{%d}", strlen($abook));
140: $this->_imsp->send($biBook, false, true, true);
141: }
142:
143:
144: $this->_imsp->send("$abook", false, false);
145: $count = count($criteria);
146: $current = 1;
147: foreach ($criteria as $search_field => $search) {
148: $this->_imsp->send(" $search_field ", false, false);
149:
150: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $search)) {
151: $biSearch = sprintf("{%d}", strlen($search));
152: $this->_imsp->send($biSearch, false, true, true);
153: $this->_imsp->send($search, false, $current == $count);
154: $current++;
155: } else {
156:
157: $this->_imsp->send('"' . $search . '"', false, $current == $count);
158: $current++;
159: }
160: }
161:
162:
163: $server_response = $this->_imsp->receive();
164: $abookNames = Array();
165:
166: while (preg_match("/^\* SEARCHADDRESS/", $server_response)) {
167: $chopped_response = preg_replace("/^\* SEARCHADDRESS/", '', $server_response);
168:
169:
170: $chopped_response = ltrim($chopped_response);
171:
172:
173: $temp = preg_replace("/\"/", '', $chopped_response);
174:
175: if (preg_match("/({)([0-9]{1,})(\}$)/", $temp, $tempArray)) {
176: $dataSize = $tempArray[2];
177: $temp = $this->_imsp->receiveStringLiteral($dataSize);
178:
179:
180: $this->_imsp->receiveStringLiteral(2);
181: }
182:
183: $abookNames[] = $temp;
184:
185:
186: $server_response = $this->_imsp->receive();
187: }
188:
189:
190: switch ($server_response) {
191: case 'BAD':
192: $this->_imsp->_logger->err('The IMSP server did not understand your request:' . $command_text);
193: throw new Horde_Imsp_Exception('The IMSP server did not understand your request: ' . $command_text);
194: case 'NO':
195: $this->_imsp->_logger->err('IMSP server is unable to perform your request: ' . $this->_imsp->lastRawError);
196: throw new Horde_Imsp_Exception('IMSP server is unable to perform your request: ' . $this->_imsp->lastRawError);
197: }
198:
199:
200: if (count($abookNames) < 1) {
201: return $abookNames;
202: }
203:
204: $this->_imsp->_logger->debug('SEARCHADDRESS command OK');
205:
206:
207: switch ($this->sort) {
208: case 'ascend':
209: sort($abookNames);
210: break;
211:
212: case 'descend':
213: rsort($abookNames);
214: break;
215: }
216:
217: return $abookNames;
218: }
219:
220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230:
231: public function getEntry($abook, $entryName)
232: {
233: $this->_imsp->send('FETCHADDRESS ', true, false);
234: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $abook)) {
235: $biBook = sprintf("{%d}", strlen($abook));
236: $this->_imsp->send($biBook, false, true, true);
237: }
238: $this->_imsp->send("$abook ", false, false);
239: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $entryName)) {
240: $biName = sprintf("{%d}", strlen($entryName));
241: $this->_imsp->send($biName, false, true, true);
242: $this->_imsp->send($entryName, false, true);
243: } else {
244: $this->_imsp->send("\"$entryName\"", false, true);
245: }
246:
247: $server_response = $this->_imsp->receive();
248: switch ($server_response) {
249: case 'BAD':
250: $this->_imsp->_logger->err('The IMSP server did not understand your request.');
251: throw new Horde_Imsp_Exception('The IMSP server did not understand your request');
252: case 'NO':
253: throw new Horde_Exception_NotFound('No entry in this address book matches your query.');
254: }
255:
256:
257: $entry = $this->_parseFetchAddressResponse($server_response);
258:
259:
260: $server_response = $this->_imsp->receive();
261: if ($server_response != 'OK') {
262:
263: $this->_imsp->_logger->err('Did not receive the expected response from the server.');
264: }
265: $this->_imsp->_logger->debug('FETCHADDRESS completed OK');
266:
267: return $entry;
268: }
269:
270: 271: 272: 273: 274: 275: 276:
277: public function createAddressBook($abookName)
278: {
279: $command_text = 'CREATEADDRESSBOOK ';
280:
281: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $abookName)) {
282: $biBook = sprintf("{%d}", strlen($abookName));
283: $this->_imsp->send($command_text . $biBook, true, true, true);
284: $this->_imsp->send($abookName, false, true);
285: } else {
286: $this->_imsp->send($command_text . $abookName, true, true);
287: }
288:
289: $server_response = $this->_imsp->receive();
290: switch ($server_response) {
291: case 'OK':
292: $this->_imsp->_logger->debug('CREATEADDRESSBOOK completed OK');
293: break;
294: case 'NO':
295:
296: $this->_imsp->_logger->err('IMSP server is unable to perform your request.');
297: throw new Horde_Imsp_Exception('IMSP server is unable to perform your request.');
298: case 'BAD':
299: $this->_imsp->_logger->err('The IMSP server did not understand your request.');
300: throw new Horde_Imsp_Exception('The IMSP server did not understand your request.');
301: default:
302:
303: $this->_imsp->_logger->err('Did not receive the expected response from the server.');
304: throw new Horde_Imsp_Exception('Did not receive the expected response from the server.');
305: }
306: }
307:
308: 309: 310: 311: 312: 313: 314:
315: public function deleteAddressBook($abookName)
316: {
317: $command_text = 'DELETEADDRESSBOOK ';
318:
319:
320: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $abookName)) {
321: $biBook = sprintf("{%d}", strlen($abookName));
322: $this->_imsp->send($command_text . $biBook, true, true, true);
323: $this->_imsp->send($abookName, false, true);
324: } else {
325: $this->_imsp->send($command_text . $abookName, true, true);
326: }
327: $server_response = $this->_imsp->receive();
328: switch ($server_response) {
329: case 'OK':
330: $this->_imsp->_logger->debug('DELETEADDRESSBOOK completed OK');
331: break;
332: case 'NO':
333:
334: $this->_imsp->_logger->err('IMSP server is unable to perform your request.');
335: throw new Horde_Imsp_Exception('IMSP server is unable to perform your request.');
336: case 'BAD':
337: $this->_imsp->_logger->err('The IMSP server did not understand your request.');
338: throw new Horde_Imsp_Exception('The IMSP server did not understand your request.');
339: default:
340:
341: $this->_imsp->_logger->err('Did not receive the expected response from the server.');
342: throw new Horde_Imsp_Exception('Did not receive the expected response from the server.');
343: }
344: }
345:
346: 347: 348: 349: 350: 351: 352: 353:
354: public function renameAddressBook($abookOldName, $abookNewName)
355: {
356: $this->_imsp->send('RENAMEADDRESSBOOK ', true, false);
357: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $abookOldName)) {
358: $biOldName = sprintf("{%d}", strlen($abookOldName));
359: $this->_imsp->send($biOldName, false, true);
360: $this->_imsp->receive();
361: }
362:
363: $this->_imsp->send("$abookOldName ", false, false);
364: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $abookNewName)) {
365: $biNewName = sprintf("{%d}", strlen($abookNewName));
366: $this->_imsp->send($biNewName, false, true);
367: $this->_imsp->receive();
368: }
369: $this->_imsp->send($abookNewName, false, true);
370:
371:
372: $server_response = $this->_imsp->receive();
373: switch ($server_response) {
374: case 'NO':
375:
376: $this->_imsp->_logger->err('IMSP server is unable to perform your request.');
377: throw new Horde_Imsp_Exception('IMSP server is unable to perform your request.');
378: case 'BAD':
379: $this->_imsp->_logger->err('The IMSP server did not understand your request.');
380: throw new Horde_Imsp_Exception('The IMSP server did not understand your request.');
381: case 'OK':
382: $this->_imsp->_logger->debug("Address book $abookOldName successfully changed to $abookNewName");
383: break;
384: default:
385:
386: $this->_imsp->_logger->err('Did not receive the expected response from the server.');
387: throw new Horde_Imsp_Exception('Did not receive the expected response from the server.');
388: }
389: }
390:
391: 392: 393: 394: 395: 396: 397: 398: 399: 400:
401: public function addEntry($abook, array $entryInfo)
402: {
403: $command_text = '';
404:
405:
406: $this->lockEntry($abook, $entryInfo['name']);
407: $this->_imsp->send('STOREADDRESS ', true, false);
408:
409:
410: $entryName = $entryInfo['name'];
411:
412:
413: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $abook)) {
414: $biBook = sprintf("{%d}", strlen($abook));
415: $this->_imsp->send($biBook, false, true);
416: $this->_imsp->receive();
417: }
418: $this->_imsp->send("$abook ", false, false);
419:
420:
421: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $entryName)) {
422: $biname = sprintf("{%d}", strlen($entryName));
423: $this->_imsp->send($biname, false, true);
424: $this->_imsp->receive();
425: $this->_imsp->send($entryName, false, false);
426: } else {
427: $this->_imsp->send("\"$entryName\" ", false, false);
428: }
429:
430: while (list($key, $value) = each($entryInfo)) {
431:
432: if ($key != 'name') {
433:
434: $value = trim($value);
435:
436:
437: $value = preg_replace("/\t/", "\n\r", $value);
438:
439:
440: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $value)) {
441: $command_text .= $key . sprintf(" {%d}", strlen($value));
442: $this->_imsp->send($command_text, false, true);
443: $server_response = $this->_imsp->receive();
444: $command_text = '';
445: if (!preg_match("/^\+/", $server_response)) {
446: $this->_imsp->_logger->err('Did not receive the expected response from the server.');
447: throw new Horde_Imsp_Exception('Did not receive the expected response from the server.');
448: }
449: $this->_imsp->send($value, false, false);
450: } else {
451:
452: $value = "\"" . $value . "\"";
453: $command_text .= $key . ' ' . $value . ' ';
454: }
455: }
456: }
457:
458:
459: $this->_imsp->send($command_text, false, true);
460: $server_response = $this->_imsp->receive();
461:
462: switch ($server_response) {
463: case 'NO':
464:
465: $this->_imsp->_logger->err('IMSP server is unable to perform your request.');
466: throw new Horde_Imsp_Exception('IMSP server is unable to perform your request.');
467: case 'BAD':
468: $this->_imsp->_logger->err('The IMSP server did not understand your request.');
469: throw new Horde_Imsp_Exception('The IMSP server did not understand your request.');
470: }
471:
472: if ($server_response != 'OK') {
473:
474:
475: $dummy_array = $this->_parseFetchAddressResponse($server_response);
476: $server_response = $this->_imsp->receive();
477: switch ($server_response) {
478: case 'NO':
479:
480: $this->_imsp->_logger->err('IMSP server is unable to perform your request.');
481: throw new Horde_Imsp_Exception('IMSP server is unable to perform your request.');
482: case 'BAD':
483: $this->_imsp->_logger->err('The IMSP server did not understand your request.');
484: throw new Horde_Imsp_Exception('The IMSP server did not understand your request.');
485: case 'OK':
486: $this->_imsp->_logger->debug('STOREADDRESS Completed successfully.');
487:
488:
489: $this->unlockEntry($abook, $entryInfo['name']);
490: }
491: }
492: }
493:
494: 495: 496: 497: 498: 499: 500: 501:
502: public function deleteEntry($abook, $bookEntry)
503: {
504:
505: $this->_imsp->send('DELETEADDRESS ', true, false);
506:
507: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $abook)) {
508: $biBook = sprintf("{%d}", strlen($abook));
509: $this->_imsp->send($biBook, false, true, true);
510: }
511: $this->_imsp->send("$abook ", false, false);
512:
513:
514: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $bookEntry)) {
515: $biEntry = sprintf("{%d}", strlen($bookEntry));
516: $this->_imsp->send($biEntry, false, true, true);
517: } else {
518: $bookEntry = $this->_imsp->quoteSpacedString($bookEntry);
519: }
520: $this->_imsp->send($bookEntry, false, true);
521: $server_response = $this->_imsp->receive();
522: switch ($server_response) {
523: case 'NO':
524:
525: $this->_imsp->_logger->err('IMSP server is unable to perform your request.');
526: throw new Horde_Imsp_Exception('IMSP server is unable to perform your request.');
527: case 'BAD':
528: $this->_imsp->_logger->err('The IMSP server did not understand your request.');
529: throw new Horde_Imsp_Exception('The IMSP server did not understand your request.');
530: case 'OK':
531: $this->_imsp->_logger->debug('DELETE Completed successfully.');
532: }
533: }
534:
535: 536: 537: 538: 539: 540: 541: 542:
543: public function lockEntry($abook, $bookEntry)
544: {
545: $this->_imsp->send('LOCK ADDRESSBOOK ', true, false);
546:
547:
548: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $abook)) {
549: $biBook = sprintf("{%d}", strlen($abook));
550: $this->_imsp->send($biBook, false, true, true);
551: }
552: $this->_imsp->send("$abook ", false, false);
553:
554: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $bookEntry)) {
555: $biEntry = sprintf("{%d}", strlen($bookEntry));
556: $this->_imsp->send($biEntry, false, true, true);
557: $this->_imsp->send($bookEntry, false, true);
558: } else {
559: $bookEntry = $this->_imsp->quoteSpacedString($bookEntry);
560: $this->_imsp->send("$bookEntry", false, true);
561: }
562:
563: $server_response = $this->_imsp->receive();
564: do {
565: switch ($server_response) {
566: case 'NO':
567:
568: $this->_imsp->_logger->err('IMSP server is unable to perform your request.');
569: throw new Horde_Imsp_Exception('IMSP server is unable to perform your request.');
570: case 'BAD':
571: $this->_imsp->_logger->err('The IMSP server did not understand your request.');
572: throw new Horde_Imsp_Exception('The IMSP server did not understand your request.');
573: }
574:
575:
576: $dummy = $this->_parseFetchAddressResponse($server_response);
577: if ($dummy) {
578: $server_response = $this->_imsp->receive();
579: }
580: } while ($server_response != 'OK');
581:
582: $this->_imsp->_logger->debug("LOCK ADDRESSBOOK on $abook $bookEntry OK");
583:
584:
585: if (!$dummy) {
586: return true;
587: } else {
588: return $dummy;
589: }
590: }
591:
592: 593: 594: 595: 596: 597: 598: 599:
600: public function unlockEntry($abook, $bookEntry)
601: {
602:
603: $this->_imsp->send('UNLOCK ADDRESSBOOK ', true, false);
604:
605:
606: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $abook)) {
607: $biBook = sprintf("{%d}", strlen($abook));
608: $this->_imsp->send($biBook, false, true, true);
609: }
610: $this->_imsp->send("$abook ", false, false);
611:
612: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $bookEntry)) {
613: $biEntry=sprintf("{%d}", strlen($bookEntry));
614: $this->_imsp->send($biEntry, false, true, true);
615: $this->_imsp->send($bookEntry, false, true);
616: } else {
617: $bookEntry = $this->_imsp->quoteSpacedString($bookEntry);
618: $this->_imsp->send("$bookEntry", false, true);
619: }
620: $response = $this->_imsp->receive();
621: switch ($response) {
622: case 'NO':
623:
624: $this->_imsp->_logger->err('IMSP server is unable to perform your request.');
625: throw new Horde_Imsp_Exception('IMSP server is unable to perform your request.');
626: case 'BAD':
627: $this->_imsp->_logger->err('The IMSP server did not understand your request.');
628: throw new Horde_Imsp_Exception('The IMSP server did not understand your request.');
629: case 'OK':
630: $this->_imsp->_logger->debug("UNLOCK ADDRESSBOOK on $abook $bookEntry OK");
631: }
632: }
633:
634: 635: 636: 637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649:
650:
651: 652: 653: 654: 655: 656: 657: 658: 659:
660: public function setACL($abook, $ident, $acl)
661: {
662:
663: if (preg_match("/[^" . self::ACL_RIGHTS . "]/", $acl)) {
664: $this->_imsp->_logger('Bad Argument');
665: throw new InvalidArgumentException();
666: }
667:
668:
669: $this->_imsp->send('SETACL ADDRESSBOOK ', true, false);
670:
671: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $abook)) {
672: $biBook = sprintf("{%d}", strlen($abook));
673: $this->_imsp->send($biBook, false, true, true);
674: }
675: $this->_imsp->send("$abook ", false, false);
676:
677:
678: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $ident)) {
679: $biIdent = sprintf("{%d}", strlen($ident));
680: $this->_imsp->send($biIdent, false, true, true);
681: }
682: $this->_imsp->send("$ident ", false, false);
683:
684:
685: $this->_imsp->send($acl, false, true);
686: $response = $this->_imsp->receive();
687: switch ($response) {
688: case 'NO':
689:
690: $this->_imsp->_logger->err('IMSP server is unable to perform your request.');
691: throw new Horde_Imsp_Exception('IMSP server is unable to perform your request.');
692: case 'BAD':
693: $this->_imsp->_logger->err('The IMSP server did not understand your request.');
694: throw new Horde_Imsp_Exception('The IMSP server did not understand your request.');
695: case 'OK':
696: $this->_imsp->_logger->debug("ACL set for $ident on $abook");
697: break;
698: default:
699:
700: $this->_imsp->_logger->err('Did not receive the expected response from the server.');
701: throw new Horde_Imsp_Exception('Did not receive the expected response from the server.');
702: }
703: }
704:
705: 706: 707: 708: 709: 710: 711: 712:
713: public function getACL($abook)
714: {
715: $this->_imsp->send('GETACL ADDRESSBOOK ', true, false);
716:
717:
718: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $abook)) {
719: $biName = sprintf("{%d}", strlen($abook));
720: $this->_imsp->send($biName, false, true, true);
721: }
722: $this->_imsp->send($abook, false, true);
723:
724:
725: $response = $this->_imsp->receive();
726: switch ($response) {
727: case 'NO':
728:
729: $this->_imsp->_logger->err('IMSP server is unable to perform your request.');
730: throw new Horde_Imsp_Exception('IMSP server is unable to perform your request.');
731: case 'BAD':
732: $this->_imsp->_logger->err('The IMSP server did not understand your request.');
733: throw new Horde_Imsp_Exception('The IMSP server did not understand your request.');
734: }
735:
736:
737: do {
738: 739: 740:
741:
742:
743: if (preg_match(Horde_Imsp_Client_Base::OCTET_COUNT, $response, $tempArray)) {
744: $data = $this->_imsp->receiveStringLiteral($tempArray[2]);
745: $response = $this->_imsp->receive();
746: }
747:
748: $parts = explode(' ', $response);
749:
750:
751: if ($data) {
752: array_unshift($parts, ' ', ' ', ' ', ' ');
753: }
754:
755: $numParts = count($parts);
756: $name = $parts[3];
757: $firstACLIdx = 4;
758: $firstChar = substr($name, 0, 1);
759: if ($firstChar == "\"") {
760: for ($i = 4; $i < $numParts; $i++) {
761: $lastChar = substr($parts[$i], strlen($parts[$i]) - 1, 1);
762: $firstACLIdx++;
763: if ($lastChar == "\"") {
764: break;
765: }
766: }
767: }
768:
769: for ($i = $firstACLIdx; $i < count($parts); $i += 2) {
770: $results[$parts[$i]] = $parts[$i+1];
771: }
772:
773: $response = $this->_imsp->receive();
774:
775: } while (preg_match("/^\* ACL ADDRESSBOOK/", $response));
776:
777:
778: if ($response != 'OK') {
779:
780: throw new Horde_Imsp_Exception('Did not receive the expected response from the server.');
781: }
782: $this->_imsp->_logger->debug("GETACL on $abook completed.");
783:
784: return $results;
785: }
786:
787: 788: 789: 790: 791: 792: 793: 794:
795: function deleteACL($abook, $ident)
796: {
797: $this->_imsp->send('DELETEACL ADDRESSBOOK ', true, false);
798:
799:
800: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $abook)) {
801: $biBook = sprintf("{%d}", strlen($abook));
802: $this->_imsp->send($biBook, false, true, true);
803: }
804: $this->_imsp->send("$abook ", false, false);
805:
806:
807: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $ident)) {
808: $biIdent = sprintf("{%d}", strlen($ident));
809: $this->_imsp->send($biIdent, false, true, true);
810: $this->_imsp->send($ident, false, true);
811: } else {
812: $this->_imsp->send("\"$ident\"", false, true);
813: }
814:
815:
816: $server_response = $this->_imsp->receive();
817: switch ($server_response) {
818: case 'NO':
819:
820: $this->_imsp->_logger->err('IMSP server is unable to perform your request.');
821: throw new Horde_Imsp_Exception('IMSP server is unable to perform your request.');
822: case 'BAD':
823: $this->_imsp->_logger->err('The IMSP server did not understand your request.');
824: throw new Horde_Imsp_Exception('The IMSP server did not understand your request.');
825: case 'OK':
826: $this->_imsp->_logger->debug("DELETED ACL for $ident on $abook");
827: default:
828: throw new Horde_Imsp_Exception('Did not receive the expected response from the server.');
829: }
830: }
831:
832: 833: 834: 835: 836: 837: 838:
839: public function myRights($abook)
840: {
841: $data = '';
842: $this->_imsp->send('MYRIGHTS ADDRESSBOOK ', true, false);
843: if (preg_match(Horde_Imsp_Client_Base::MUST_USE_LITERAL, $abook)) {
844: $biBook = sprintf("{%d}", strlen($abook));
845: $this->_imsp->send($biBook, false, true, true);
846: }
847: $this->_imsp->send($abook, false, true);
848: $server_response = $this->_imsp->receive();
849: switch ($server_response) {
850: case 'NO':
851:
852: $this->_imsp->_logger->err('IMSP server is unable to perform your request.');
853: throw new Horde_Imsp_Exception('IMSP server is unable to perform your request.');
854: case 'BAD':
855: $this->_imsp->_logger->err('The IMSP server did not understand your request.');
856: throw new Horde_Imsp_Exception('The IMSP server did not understand your request.');
857: }
858:
859: if (!preg_match("/^\* MYRIGHTS ADDRESSBOOK/", $server_response)) {
860: throw new Horde_Imsp_Exception('Did not receive the expected response from the server.');
861: }
862:
863:
864: if (preg_match(Horde_Imsp_Client_Base::OCTET_COUNT, $server_response, $tempArray)) {
865: $data = $this->_imsp->receiveStringLiteral($tempArray[2]);
866: $server_response = $this->_imsp->receive();
867: }
868:
869: $parts = explode(' ', $server_response);
870:
871:
872: if ($data) {
873: array_unshift($parts, ' ', ' ', ' ', ' ');
874: }
875:
876:
877: $numParts = count($parts);
878: $name = $parts[3];
879: $firstACLIdx = 4;
880: $firstChar = substr($name, 0, 1);
881: if ($firstChar == "\"") {
882: for ($i = 4; $i < $numParts; $i++) {
883: $lastChar = substr($parts[$i], strlen($parts[$i]) - 1, 1);
884: $firstACLIdx++;
885: if ($lastChar == "\"") {
886: break;
887: }
888: }
889: }
890:
891: $acl = $parts[$firstACLIdx];
892: $server_response = $this->_imsp->receive();
893:
894: if ($server_response != 'OK') {
895: throw new Horde_Imsp_Exception('Did not receive the expected response from the server.');
896: } else {
897: $this->_imsp->_logger->debug("MYRIGHTS on $abook completed.");
898: return $acl;
899: }
900: }
901:
902: 903: 904: 905: 906: 907: 908:
909: protected function _parseFetchAddressResponse($server_response)
910: {
911: $abook = '';
912: if (!preg_match("/^\* FETCHADDRESS /", $server_response)) {
913: $this->_imsp->_logger->err('Did not receive a FETCHADDRESS response from server.');
914: throw new Horde_Imsp_Exception('Did not receive the expected response from the server.');
915: }
916:
917: 918: 919: 920: 921: 922: 923: 924: 925: 926: 927: 928: 929: 930: 931: 932: 933: 934:
935:
936:
937: if (preg_match("/(^\* FETCHADDRESS )({)([0-9]{1,})(\}$)/",
938: $server_response, $tempArray)) {
939: $abook = $this->_imsp->receiveStringLiteral($tempArray[3]);
940: $chopped_response = trim($this->_imsp->receive());
941: } else {
942:
943: $chopped_response = trim(preg_replace("/^\* FETCHADDRESS /", '', $server_response));
944: }
945:
946: $parts = explode(' ', $chopped_response);
947: 948: 949:
950: if (!empty($abook)) {
951: array_unshift($parts, ' ');
952: }
953:
954:
955: $numOfParts = count($parts);
956: $name = $parts[0];
957: $firstNameIdx = 1;
958: $firstChar = substr($name, 0, 1);
959: if ($firstChar =="\"") {
960: for ($i = 1; $i < $numOfParts; $i++) {
961: $lastChar = substr($parts[$i], strlen($parts[$i]) - 1, 1);
962: $firstNameIdx++;
963: if ($lastChar == "\"") {
964: break;
965: }
966: }
967: }
968:
969:
970: $name = $parts[$firstNameIdx];
971: $firstChar = substr($name,0,1);
972:
973:
974:
975: if ($firstChar == "\"") {
976: $name = ltrim($name, "\"");
977: for ($i = $firstNameIdx + 1; $i < $numOfParts; $i++) {
978: $name .= ' ' . $parts[$i];
979: $lastChar = substr($parts[$i], strlen($parts[$i]) - 1,1);
980: if ($lastChar == "\"") {
981: $name = rtrim($name, "\"");
982: $nextKey = $i + 1;
983: break;
984: }
985: }
986:
987:
988: } elseif (preg_match('/\{(\d+)\}/', $name, $matches)) {
989: $name = $this->_imsp->receiveStringLiteral($matches[1]);
990: $response=$this->_imsp->receive();
991: $parts = explode(' ', $response);
992: $numOfParts = count($parts);
993: $nextKey = 0;
994: } else {
995:
996:
997:
998: $nextKey = $firstNameIdx + 1;
999: }
1000:
1001: $lastChar = '';
1002: $entry['name'] = $name;
1003:
1004:
1005: for ($i = $nextKey; $i < $numOfParts; $i += 2) {
1006: $key = $parts[$i];
1007:
1008: if (@preg_match(Horde_Imsp_Client_Base::OCTET_COUNT, $parts[$i+1], $tempArray)) {
1009: $server_data = $this->_imsp->receiveStringLiteral($tempArray[2]);
1010: $entry[$key] = $server_data;
1011:
1012: 1013: 1014: 1015:
1016: $parts = $this->_imsp->getServerResponseChunks();
1017: $i = -2;
1018: $numOfParts = count($parts);
1019: } else {
1020:
1021: @$entry[$key] = $parts[$i + 1];
1022:
1023:
1024:
1025:
1026: if ((@substr($parts[$i + 1], 0, 1) == '"') &&
1027: (substr($parts[$i + 1],
1028: strlen($parts[$i + 1]) - 1, 1) != '"')) {
1029:
1030: do {
1031: $nextElement = $parts[$i+2];
1032:
1033:
1034: $lastChar = substr($nextElement, strlen($nextElement) - 1, 1);
1035: $entry[$key] .= ' ' . $nextElement;
1036:
1037:
1038: if ($lastChar == '"') {
1039: $done = true;
1040: $i++;
1041: } else {
1042:
1043:
1044: $done = false;
1045: $lastChar = substr($parts[$i+3], strlen($parts[$i+3]) - 1,1);
1046: $i++;
1047: }
1048: } while ($lastChar != '"');
1049:
1050:
1051:
1052: if (!$done) {
1053: $nextElement = $parts[$i+2];
1054: $entry[$key] .= ' ' . $nextElement;
1055: $i++;
1056: }
1057:
1058:
1059: if (substr($entry[$key], 0, 1) == '"') {
1060: $entry[$key] = substr($entry[$key], 1, strlen($entry[$key]) - 2);
1061: }
1062:
1063: if (substr($entry[$key], strlen($entry[$key]) - 1, 1) == '"') {
1064: $entry[$key] = substr($entry[$key], 0, strlen($entry[$key]) - 2);
1065: }
1066: } elseif ((@substr($parts[$i + 1], 0, 1) == '"') &&
1067: (substr($parts[$i + 1], -1, 1) == '"')) {
1068:
1069: if (substr($entry[$key], 0, 1) == '"') {
1070: $entry[$key] = substr($entry[$key], 1, strlen($entry[$key]) - 2);
1071: }
1072:
1073: if (substr($entry[$key], -1, 1) == '"') {
1074: $entry[$key] = substr($entry[$key], 0, strlen($entry[$key]) - 2);
1075: }
1076: }
1077: }
1078: }
1079:
1080: return $entry;
1081: }
1082:
1083: }
1084: