1: <?php
2: /**
3: * Utility functions for the Horde IMAP client - POP3 specific methods.
4: *
5: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
6: *
7: * See the enclosed file COPYING for license information (LGPL). If you
8: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
9: *
10: * @author Michael Slusarz <slusarz@horde.org>
11: * @category Horde
12: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
13: * @package Imap_Client
14: */
15: class Horde_Imap_Client_Utils_Pop3 extends Horde_Imap_Client_Utils
16: {
17: /**
18: * Create a POP3 message sequence string from a list of UIDs.
19: *
20: * Index Format: {P[length]}UID1{P[length]}UID2...
21: *
22: * @param mixed $in An array of UIDs (or a single UID).
23: * @param array $options Additional options. 'nosort' is not used in
24: * this class.
25: *
26: * @return string The POP3 message sequence string.
27: */
28: public function toSequenceString($in, $options = array())
29: {
30: if (!is_array($in)) {
31: if (!strlen($in)) {
32: return '';
33: }
34: $in = array($in);
35: } elseif (!empty($options['mailbox'])) {
36: $tmp = $in;
37: $in = array();
38: foreach ($tmp as $val) {
39: $in = array_merge($in, $val);
40: }
41: }
42:
43: $str = '';
44:
45: // Make sure IDs are unique
46: foreach (array_keys(array_flip($in)) as $val) {
47: $str .= '{P' . strlen($val) . '}' . $val;
48: }
49:
50: return $str;
51: }
52:
53: /**
54: * Parse a POP3 message sequence string into a list of indices.
55: *
56: * @param string $str The POP3 message sequence string.
57: *
58: * @return array An array of UIDs.
59: */
60: public function fromSequenceString($str)
61: {
62: $ids = array();
63: $str = trim($str);
64:
65: while (strlen($str)) {
66: /* Check for valid beginning of UID. */
67: if (substr($str, 0, 2) != '{P') {
68: /* Assume this is the entire UID, if there is no other
69: * data. Otherwise, ignore garbage data. */
70: if (empty($ids)) {
71: $ids[] = $str;
72: }
73: break;
74: }
75:
76: $i = strpos($str, '}', 2);
77: $size = intval(substr($str, 2, $i - 2));
78: $ids[] = substr($str, $i + 1, $size);
79:
80: $str = substr($str, $i + 1 + $size);
81: }
82:
83: return $ids;
84: }
85:
86: }
87: