annotate program/lib/Roundcube/rcube_imap_generic.php @ 15:85a746e95663

slowly working through deprecations/warnings from 8.3
author Charlie Root
date Sat, 20 Sep 2025 12:16:50 -0400
parents abddb304201f
children 1866b439e6d3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1 <?php
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4 +-----------------------------------------------------------------------+
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
5 | This file is part of the Roundcube Webmail client |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
6 | Copyright (C) 2005-2015, The Roundcube Dev Team |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
7 | Copyright (C) 2011-2012, Kolab Systems AG |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
8 | |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
9 | Licensed under the GNU General Public License version 3 or |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
10 | any later version with exceptions for skins & plugins. |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
11 | See the README file for a full license statement. |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
12 | |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
13 | PURPOSE: |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
14 | Provide alternative IMAP library that doesn't rely on the standard |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
15 | C-Client based version. This allows to function regardless |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
16 | of whether or not the PHP build it's running on has IMAP |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
17 | functionality built-in. |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
18 | |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
19 | Based on Iloha IMAP Library. See http://ilohamail.org/ for details |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
20 +-----------------------------------------------------------------------+
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
21 | Author: Aleksander Machniak <alec@alec.pl> |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
22 | Author: Ryo Chijiiwa <Ryo@IlohaMail.org> |
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
23 +-----------------------------------------------------------------------+
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
24 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
25
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
26 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
27 * PHP based wrapper class to connect to an IMAP server
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
28 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
29 * @package Framework
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
30 * @subpackage Storage
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
31 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
32 class rcube_imap_generic
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
33 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
34 public $error;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
35 public $errornum;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
36 public $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
37 public $resultcode;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
38 public $selected;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
39 public $data = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
40 public $flags = array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
41 'SEEN' => '\\Seen',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
42 'DELETED' => '\\Deleted',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
43 'ANSWERED' => '\\Answered',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
44 'DRAFT' => '\\Draft',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
45 'FLAGGED' => '\\Flagged',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
46 'FORWARDED' => '$Forwarded',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
47 'MDNSENT' => '$MDNSent',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
48 '*' => '\\*',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
49 );
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
50
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
51 protected $fp;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
52 protected $host;
15
85a746e95663 slowly working through deprecations/warnings from 8.3
Charlie Root
parents: 14
diff changeset
53 protected $user;
0
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
54 protected $cmd_tag;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
55 protected $cmd_num = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
56 protected $resourceid;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
57 protected $prefs = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
58 protected $logged = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
59 protected $capability = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
60 protected $capability_readed = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
61 protected $debug = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
62 protected $debug_handler = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
63
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
64 const ERROR_OK = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
65 const ERROR_NO = -1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
66 const ERROR_BAD = -2;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
67 const ERROR_BYE = -3;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
68 const ERROR_UNKNOWN = -4;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
69 const ERROR_COMMAND = -5;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
70 const ERROR_READONLY = -6;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
71
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
72 const COMMAND_NORESPONSE = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
73 const COMMAND_CAPABILITY = 2;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
74 const COMMAND_LASTLINE = 4;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
75 const COMMAND_ANONYMIZED = 8;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
76
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
77 const DEBUG_LINE_LENGTH = 4098; // 4KB + 2B for \r\n
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
78
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
79
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
80 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
81 * Send simple (one line) command to the connection stream
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
82 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
83 * @param string $string Command string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
84 * @param bool $endln True if CRLF need to be added at the end of command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
85 * @param bool $anonymized Don't write the given data to log but a placeholder
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
86 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
87 * @param int Number of bytes sent, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
88 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
89 protected function putLine($string, $endln = true, $anonymized = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
90 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
91 if (!$this->fp) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
92 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
93 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
94
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
95 if ($this->debug) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
96 // anonymize the sent command for logging
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
97 $cut = $endln ? 2 : 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
98 if ($anonymized && preg_match('/^(A\d+ (?:[A-Z]+ )+)(.+)/', $string, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
99 $log = $m[1] . sprintf('****** [%d]', strlen($m[2]) - $cut);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
100 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
101 else if ($anonymized) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
102 $log = sprintf('****** [%d]', strlen($string) - $cut);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
103 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
104 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
105 $log = rtrim($string);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
106 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
107
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
108 $this->debug('C: ' . $log);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
109 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
110
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
111 if ($endln) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
112 $string .= "\r\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
113 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
114
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
115 $res = fwrite($this->fp, $string);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
116
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
117 if ($res === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
118 $this->closeSocket();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
119 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
120
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
121 return $res;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
122 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
123
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
124 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
125 * Send command to the connection stream with Command Continuation
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
126 * Requests (RFC3501 7.5) and LITERAL+ (RFC2088) support
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
127 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
128 * @param string $string Command string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
129 * @param bool $endln True if CRLF need to be added at the end of command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
130 * @param bool $anonymized Don't write the given data to log but a placeholder
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
131 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
132 * @return int|bool Number of bytes sent, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
133 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
134 protected function putLineC($string, $endln=true, $anonymized=false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
135 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
136 if (!$this->fp) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
137 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
138 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
139
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
140 if ($endln) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
141 $string .= "\r\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
142 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
143
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
144 $res = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
145 if ($parts = preg_split('/(\{[0-9]+\}\r\n)/m', $string, -1, PREG_SPLIT_DELIM_CAPTURE)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
146 for ($i=0, $cnt=count($parts); $i<$cnt; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
147 if (preg_match('/^\{([0-9]+)\}\r\n$/', $parts[$i+1], $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
148 // LITERAL+ support
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
149 if ($this->prefs['literal+']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
150 $parts[$i+1] = sprintf("{%d+}\r\n", $matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
151 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
152
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
153 $bytes = $this->putLine($parts[$i].$parts[$i+1], false, $anonymized);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
154 if ($bytes === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
155 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
156 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
157
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
158 $res += $bytes;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
159
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
160 // don't wait if server supports LITERAL+ capability
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
161 if (!$this->prefs['literal+']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
162 $line = $this->readLine(1000);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
163 // handle error in command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
164 if ($line[0] != '+') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
165 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
166 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
167 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
168
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
169 $i++;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
170 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
171 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
172 $bytes = $this->putLine($parts[$i], false, $anonymized);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
173 if ($bytes === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
174 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
175 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
176
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
177 $res += $bytes;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
178 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
179 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
180 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
181
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
182 return $res;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
183 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
184
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
185 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
186 * Reads line from the connection stream
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
187 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
188 * @param int $size Buffer size
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
189 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
190 * @return string Line of text response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
191 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
192 protected function readLine($size = 1024)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
193 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
194 $line = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
195
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
196 if (!$size) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
197 $size = 1024;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
198 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
199
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
200 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
201 if ($this->eof()) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
202 return $line ?: null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
203 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
204
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
205 $buffer = fgets($this->fp, $size);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
206
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
207 if ($buffer === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
208 $this->closeSocket();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
209 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
210 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
211
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
212 if ($this->debug) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
213 $this->debug('S: '. rtrim($buffer));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
214 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
215
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
216 $line .= $buffer;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
217 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
218 while (substr($buffer, -1) != "\n");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
219
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
220 return $line;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
221 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
222
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
223 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
224 * Reads more data from the connection stream when provided
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
225 * data contain string literal
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
226 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
227 * @param string $line Response text
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
228 * @param bool $escape Enables escaping
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
229 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
230 * @return string Line of text response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
231 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
232 protected function multLine($line, $escape = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
233 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
234 $line = rtrim($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
235 if (preg_match('/\{([0-9]+)\}$/', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
236 $out = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
237 $str = substr($line, 0, -strlen($m[0]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
238 $bytes = $m[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
239
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
240 while (strlen($out) < $bytes) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
241 $line = $this->readBytes($bytes);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
242 if ($line === null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
243 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
244 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
245
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
246 $out .= $line;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
247 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
248
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
249 $line = $str . ($escape ? $this->escape($out) : $out);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
250 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
251
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
252 return $line;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
253 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
254
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
255 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
256 * Reads specified number of bytes from the connection stream
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
257 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
258 * @param int $bytes Number of bytes to get
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
259 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
260 * @return string Response text
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
261 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
262 protected function readBytes($bytes)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
263 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
264 $data = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
265 $len = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
266
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
267 while ($len < $bytes && !$this->eof()) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
268 $d = fread($this->fp, $bytes-$len);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
269 if ($this->debug) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
270 $this->debug('S: '. $d);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
271 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
272 $data .= $d;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
273 $data_len = strlen($data);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
274 if ($len == $data_len) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
275 break; // nothing was read -> exit to avoid apache lockups
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
276 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
277 $len = $data_len;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
278 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
279
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
280 return $data;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
281 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
282
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
283 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
284 * Reads complete response to the IMAP command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
285 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
286 * @param array $untagged Will be filled with untagged response lines
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
287 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
288 * @return string Response text
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
289 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
290 protected function readReply(&$untagged = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
291 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
292 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
293 $line = trim($this->readLine(1024));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
294 // store untagged response lines
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
295 if ($line[0] == '*') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
296 $untagged[] = $line;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
297 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
298 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
299 while ($line[0] == '*');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
300
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
301 if ($untagged) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
302 $untagged = join("\n", $untagged);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
303 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
304
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
305 return $line;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
306 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
307
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
308 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
309 * Response parser.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
310 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
311 * @param string $string Response text
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
312 * @param string $err_prefix Error message prefix
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
313 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
314 * @return int Response status
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
315 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
316 protected function parseResult($string, $err_prefix = '')
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
317 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
318 if (preg_match('/^[a-z0-9*]+ (OK|NO|BAD|BYE)(.*)$/i', trim($string), $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
319 $res = strtoupper($matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
320 $str = trim($matches[2]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
321
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
322 if ($res == 'OK') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
323 $this->errornum = self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
324 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
325 else if ($res == 'NO') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
326 $this->errornum = self::ERROR_NO;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
327 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
328 else if ($res == 'BAD') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
329 $this->errornum = self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
330 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
331 else if ($res == 'BYE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
332 $this->closeSocket();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
333 $this->errornum = self::ERROR_BYE;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
334 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
335
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
336 if ($str) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
337 $str = trim($str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
338 // get response string and code (RFC5530)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
339 if (preg_match("/^\[([a-z-]+)\]/i", $str, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
340 $this->resultcode = strtoupper($m[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
341 $str = trim(substr($str, strlen($m[1]) + 2));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
342 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
343 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
344 $this->resultcode = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
345 // parse response for [APPENDUID 1204196876 3456]
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
346 if (preg_match("/^\[APPENDUID [0-9]+ ([0-9]+)\]/i", $str, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
347 $this->data['APPENDUID'] = $m[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
348 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
349 // parse response for [COPYUID 1204196876 3456:3457 123:124]
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
350 else if (preg_match("/^\[COPYUID [0-9]+ ([0-9,:]+) ([0-9,:]+)\]/i", $str, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
351 $this->data['COPYUID'] = array($m[1], $m[2]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
352 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
353 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
354
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
355 $this->result = $str;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
356
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
357 if ($this->errornum != self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
358 $this->error = $err_prefix ? $err_prefix.$str : $str;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
359 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
360 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
361
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
362 return $this->errornum;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
363 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
364
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
365 return self::ERROR_UNKNOWN;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
366 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
367
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
368 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
369 * Checks connection stream state.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
370 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
371 * @return bool True if connection is closed
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
372 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
373 protected function eof()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
374 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
375 if (!is_resource($this->fp)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
376 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
377 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
378
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
379 // If a connection opened by fsockopen() wasn't closed
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
380 // by the server, feof() will hang.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
381 $start = microtime(true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
382
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
383 if (feof($this->fp) ||
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
384 ($this->prefs['timeout'] && (microtime(true) - $start > $this->prefs['timeout']))
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
385 ) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
386 $this->closeSocket();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
387 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
388 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
389
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
390 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
391 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
392
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
393 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
394 * Closes connection stream.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
395 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
396 protected function closeSocket()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
397 {
12
5039cc34571f some old, mostly new 7.3->8.4 changes needed
Charlie Root
parents: 11
diff changeset
398 if ($this->fp) {
5039cc34571f some old, mostly new 7.3->8.4 changes needed
Charlie Root
parents: 11
diff changeset
399 fclose($this->fp);
5039cc34571f some old, mostly new 7.3->8.4 changes needed
Charlie Root
parents: 11
diff changeset
400 $this->fp = null;
5039cc34571f some old, mostly new 7.3->8.4 changes needed
Charlie Root
parents: 11
diff changeset
401 }
0
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
402 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
403
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
404 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
405 * Error code/message setter.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
406 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
407 protected function setError($code, $msg = '')
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
408 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
409 $this->errornum = $code;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
410 $this->error = $msg;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
411 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
412
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
413 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
414 * Checks response status.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
415 * Checks if command response line starts with specified prefix (or * BYE/BAD)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
416 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
417 * @param string $string Response text
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
418 * @param string $match Prefix to match with (case-sensitive)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
419 * @param bool $error Enables BYE/BAD checking
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
420 * @param bool $nonempty Enables empty response checking
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
421 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
422 * @return bool True any check is true or connection is closed.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
423 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
424 protected function startsWith($string, $match, $error = false, $nonempty = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
425 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
426 if (!$this->fp) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
427 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
428 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
429
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
430 if (strncmp($string, $match, strlen($match)) == 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
431 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
432 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
433
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
434 if ($error && preg_match('/^\* (BYE|BAD) /i', $string, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
435 if (strtoupper($m[1]) == 'BYE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
436 $this->closeSocket();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
437 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
438 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
439 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
440
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
441 if ($nonempty && !strlen($string)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
442 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
443 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
444
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
445 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
446 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
447
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
448 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
449 * Capabilities checker
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
450 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
451 protected function hasCapability($name)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
452 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
453 if (empty($this->capability) || $name == '') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
454 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
455 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
456
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
457 if (in_array($name, $this->capability)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
458 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
459 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
460 else if (strpos($name, '=')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
461 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
462 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
463
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
464 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
465 foreach ($this->capability as $cap) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
466 $entry = explode('=', $cap);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
467 if ($entry[0] == $name) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
468 $result[] = $entry[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
469 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
470 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
471
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
472 return $result ?: false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
473 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
474
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
475 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
476 * Capabilities checker
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
477 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
478 * @param string $name Capability name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
479 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
480 * @return mixed Capability values array for key=value pairs, true/false for others
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
481 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
482 public function getCapability($name)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
483 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
484 $result = $this->hasCapability($name);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
485
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
486 if (!empty($result)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
487 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
488 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
489 else if ($this->capability_readed) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
490 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
491 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
492
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
493 // get capabilities (only once) because initial
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
494 // optional CAPABILITY response may differ
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
495 $result = $this->execute('CAPABILITY');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
496
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
497 if ($result[0] == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
498 $this->parseCapability($result[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
499 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
500
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
501 $this->capability_readed = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
502
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
503 return $this->hasCapability($name);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
504 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
505
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
506 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
507 * Clears detected server capabilities
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
508 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
509 public function clearCapability()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
510 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
511 $this->capability = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
512 $this->capability_readed = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
513 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
514
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
515 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
516 * DIGEST-MD5/CRAM-MD5/PLAIN Authentication
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
517 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
518 * @param string $user Username
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
519 * @param string $pass Password
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
520 * @param string $type Authentication type (PLAIN/CRAM-MD5/DIGEST-MD5)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
521 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
522 * @return resource Connection resourse on success, error code on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
523 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
524 protected function authenticate($user, $pass, $type = 'PLAIN')
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
525 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
526 if ($type == 'CRAM-MD5' || $type == 'DIGEST-MD5') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
527 if ($type == 'DIGEST-MD5' && !class_exists('Auth_SASL')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
528 $this->setError(self::ERROR_BYE,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
529 "The Auth_SASL package is required for DIGEST-MD5 authentication");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
530 return self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
531 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
532
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
533 $this->putLine($this->nextTag() . " AUTHENTICATE $type");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
534 $line = trim($this->readReply());
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
535
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
536 if ($line[0] == '+') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
537 $challenge = substr($line, 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
538 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
539 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
540 return $this->parseResult($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
541 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
542
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
543 if ($type == 'CRAM-MD5') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
544 // RFC2195: CRAM-MD5
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
545 $ipad = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
546 $opad = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
547 $xor = function($str1, $str2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
548 $result = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
549 $size = strlen($str1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
550 for ($i=0; $i<$size; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
551 $result .= chr(ord($str1[$i]) ^ ord($str2[$i]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
552 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
553 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
554 };
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
555
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
556 // initialize ipad, opad
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
557 for ($i=0; $i<64; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
558 $ipad .= chr(0x36);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
559 $opad .= chr(0x5C);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
560 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
561
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
562 // pad $pass so it's 64 bytes
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
563 $pass = str_pad($pass, 64, chr(0));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
564
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
565 // generate hash
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
566 $hash = md5($xor($pass, $opad) . pack("H*",
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
567 md5($xor($pass, $ipad) . base64_decode($challenge))));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
568 $reply = base64_encode($user . ' ' . $hash);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
569
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
570 // send result
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
571 $this->putLine($reply, true, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
572 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
573 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
574 // RFC2831: DIGEST-MD5
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
575 // proxy authorization
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
576 if (!empty($this->prefs['auth_cid'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
577 $authc = $this->prefs['auth_cid'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
578 $pass = $this->prefs['auth_pw'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
579 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
580 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
581 $authc = $user;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
582 $user = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
583 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
584
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
585 $auth_sasl = new Auth_SASL;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
586 $auth_sasl = $auth_sasl->factory('digestmd5');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
587 $reply = base64_encode($auth_sasl->getResponse($authc, $pass,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
588 base64_decode($challenge), $this->host, 'imap', $user));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
589
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
590 // send result
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
591 $this->putLine($reply, true, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
592 $line = trim($this->readReply());
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
593
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
594 if ($line[0] != '+') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
595 return $this->parseResult($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
596 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
597
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
598 // check response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
599 $challenge = substr($line, 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
600 $challenge = base64_decode($challenge);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
601 if (strpos($challenge, 'rspauth=') === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
602 $this->setError(self::ERROR_BAD,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
603 "Unexpected response from server to DIGEST-MD5 response");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
604 return self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
605 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
606
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
607 $this->putLine('');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
608 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
609
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
610 $line = $this->readReply();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
611 $result = $this->parseResult($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
612 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
613 else if ($type == 'GSSAPI') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
614 if (!extension_loaded('krb5')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
615 $this->setError(self::ERROR_BYE,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
616 "The krb5 extension is required for GSSAPI authentication");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
617 return self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
618 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
619
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
620 if (empty($this->prefs['gssapi_cn'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
621 $this->setError(self::ERROR_BYE,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
622 "The gssapi_cn parameter is required for GSSAPI authentication");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
623 return self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
624 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
625
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
626 if (empty($this->prefs['gssapi_context'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
627 $this->setError(self::ERROR_BYE,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
628 "The gssapi_context parameter is required for GSSAPI authentication");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
629 return self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
630 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
631
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
632 putenv('KRB5CCNAME=' . $this->prefs['gssapi_cn']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
633
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
634 try {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
635 $ccache = new KRB5CCache();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
636 $ccache->open($this->prefs['gssapi_cn']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
637 $gssapicontext = new GSSAPIContext();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
638 $gssapicontext->acquireCredentials($ccache);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
639
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
640 $token = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
641 $success = $gssapicontext->initSecContext($this->prefs['gssapi_context'], null, null, null, $token);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
642 $token = base64_encode($token);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
643 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
644 catch (Exception $e) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
645 trigger_error($e->getMessage(), E_USER_WARNING);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
646 $this->setError(self::ERROR_BYE, "GSSAPI authentication failed");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
647 return self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
648 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
649
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
650 $this->putLine($this->nextTag() . " AUTHENTICATE GSSAPI " . $token);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
651 $line = trim($this->readReply());
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
652
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
653 if ($line[0] != '+') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
654 return $this->parseResult($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
655 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
656
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
657 try {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
658 $challenge = base64_decode(substr($line, 2));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
659 $gssapicontext->unwrap($challenge, $challenge);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
660 $gssapicontext->wrap($challenge, $challenge, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
661 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
662 catch (Exception $e) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
663 trigger_error($e->getMessage(), E_USER_WARNING);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
664 $this->setError(self::ERROR_BYE, "GSSAPI authentication failed");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
665 return self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
666 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
667
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
668 $this->putLine(base64_encode($challenge));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
669
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
670 $line = $this->readReply();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
671 $result = $this->parseResult($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
672 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
673 else { // PLAIN
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
674 // proxy authorization
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
675 if (!empty($this->prefs['auth_cid'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
676 $authc = $this->prefs['auth_cid'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
677 $pass = $this->prefs['auth_pw'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
678 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
679 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
680 $authc = $user;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
681 $user = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
682 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
683
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
684 $reply = base64_encode($user . chr(0) . $authc . chr(0) . $pass);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
685
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
686 // RFC 4959 (SASL-IR): save one round trip
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
687 if ($this->getCapability('SASL-IR')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
688 list($result, $line) = $this->execute("AUTHENTICATE PLAIN", array($reply),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
689 self::COMMAND_LASTLINE | self::COMMAND_CAPABILITY | self::COMMAND_ANONYMIZED);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
690 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
691 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
692 $this->putLine($this->nextTag() . " AUTHENTICATE PLAIN");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
693 $line = trim($this->readReply());
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
694
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
695 if ($line[0] != '+') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
696 return $this->parseResult($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
697 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
698
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
699 // send result, get reply and process it
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
700 $this->putLine($reply, true, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
701 $line = $this->readReply();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
702 $result = $this->parseResult($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
703 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
704 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
705
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
706 if ($result == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
707 // optional CAPABILITY response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
708 if ($line && preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
709 $this->parseCapability($matches[1], true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
710 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
711 return $this->fp;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
712 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
713 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
714 $this->setError($result, "AUTHENTICATE $type: $line");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
715 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
716
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
717 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
718 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
719
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
720 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
721 * LOGIN Authentication
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
722 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
723 * @param string $user Username
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
724 * @param string $pass Password
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
725 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
726 * @return resource Connection resourse on success, error code on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
727 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
728 protected function login($user, $password)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
729 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
730 list($code, $response) = $this->execute('LOGIN', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
731 $this->escape($user), $this->escape($password)), self::COMMAND_CAPABILITY | self::COMMAND_ANONYMIZED);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
732
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
733 // re-set capabilities list if untagged CAPABILITY response provided
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
734 if (preg_match('/\* CAPABILITY (.+)/i', $response, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
735 $this->parseCapability($matches[1], true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
736 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
737
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
738 if ($code == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
739 return $this->fp;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
740 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
741
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
742 return $code;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
743 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
744
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
745 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
746 * Detects hierarchy delimiter
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
747 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
748 * @return string The delimiter
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
749 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
750 public function getHierarchyDelimiter()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
751 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
752 if ($this->prefs['delimiter']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
753 return $this->prefs['delimiter'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
754 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
755
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
756 // try (LIST "" ""), should return delimiter (RFC2060 Sec 6.3.8)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
757 list($code, $response) = $this->execute('LIST',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
758 array($this->escape(''), $this->escape('')));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
759
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
760 if ($code == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
761 $args = $this->tokenizeResponse($response, 4);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
762 $delimiter = $args[3];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
763
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
764 if (strlen($delimiter) > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
765 return ($this->prefs['delimiter'] = $delimiter);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
766 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
767 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
768 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
769
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
770 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
771 * NAMESPACE handler (RFC 2342)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
772 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
773 * @return array Namespace data hash (personal, other, shared)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
774 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
775 public function getNamespace()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
776 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
777 if (array_key_exists('namespace', $this->prefs)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
778 return $this->prefs['namespace'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
779 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
780
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
781 if (!$this->getCapability('NAMESPACE')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
782 return self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
783 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
784
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
785 list($code, $response) = $this->execute('NAMESPACE');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
786
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
787 if ($code == self::ERROR_OK && preg_match('/^\* NAMESPACE /', $response)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
788 $response = substr($response, 11);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
789 $data = $this->tokenizeResponse($response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
790 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
791
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
792 if (!is_array($data)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
793 return $code;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
794 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
795
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
796 $this->prefs['namespace'] = array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
797 'personal' => $data[0],
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
798 'other' => $data[1],
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
799 'shared' => $data[2],
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
800 );
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
801
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
802 return $this->prefs['namespace'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
803 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
804
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
805 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
806 * Connects to IMAP server and authenticates.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
807 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
808 * @param string $host Server hostname or IP
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
809 * @param string $user User name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
810 * @param string $password Password
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
811 * @param array $options Connection and class options
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
812 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
813 * @return bool True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
814 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
815 public function connect($host, $user, $password, $options = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
816 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
817 // configure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
818 $this->set_prefs($options);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
819
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
820 $this->host = $host;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
821 $this->user = $user;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
822 $this->logged = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
823 $this->selected = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
824
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
825 // check input
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
826 if (empty($host)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
827 $this->setError(self::ERROR_BAD, "Empty host");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
828 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
829 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
830
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
831 if (empty($user)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
832 $this->setError(self::ERROR_NO, "Empty user");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
833 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
834 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
835
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
836 if (empty($password) && empty($options['gssapi_cn'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
837 $this->setError(self::ERROR_NO, "Empty password");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
838 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
839 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
840
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
841 // Connect
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
842 if (!$this->_connect($host)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
843 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
844 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
845
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
846 // Send ID info
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
847 if (!empty($this->prefs['ident']) && $this->getCapability('ID')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
848 $this->data['ID'] = $this->id($this->prefs['ident']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
849 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
850
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
851 $auth_method = $this->prefs['auth_type'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
852 $auth_methods = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
853 $result = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
854
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
855 // check for supported auth methods
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
856 if ($auth_method == 'CHECK') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
857 if ($auth_caps = $this->getCapability('AUTH')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
858 $auth_methods = $auth_caps;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
859 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
860
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
861 // RFC 2595 (LOGINDISABLED) LOGIN disabled when connection is not secure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
862 $login_disabled = $this->getCapability('LOGINDISABLED');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
863 if (($key = array_search('LOGIN', $auth_methods)) !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
864 if ($login_disabled) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
865 unset($auth_methods[$key]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
866 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
867 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
868 else if (!$login_disabled) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
869 $auth_methods[] = 'LOGIN';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
870 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
871
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
872 // Use best (for security) supported authentication method
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
873 $all_methods = array('DIGEST-MD5', 'CRAM-MD5', 'CRAM_MD5', 'PLAIN', 'LOGIN');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
874
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
875 if (!empty($this->prefs['gssapi_cn'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
876 array_unshift($all_methods, 'GSSAPI');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
877 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
878
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
879 foreach ($all_methods as $auth_method) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
880 if (in_array($auth_method, $auth_methods)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
881 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
882 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
883 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
884 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
885 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
886 // Prevent from sending credentials in plain text when connection is not secure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
887 if ($auth_method == 'LOGIN' && $this->getCapability('LOGINDISABLED')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
888 $this->setError(self::ERROR_BAD, "Login disabled by IMAP server");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
889 $this->closeConnection();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
890 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
891 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
892 // replace AUTH with CRAM-MD5 for backward compat.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
893 if ($auth_method == 'AUTH') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
894 $auth_method = 'CRAM-MD5';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
895 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
896 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
897
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
898 // pre-login capabilities can be not complete
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
899 $this->capability_readed = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
900
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
901 // Authenticate
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
902 switch ($auth_method) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
903 case 'CRAM_MD5':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
904 $auth_method = 'CRAM-MD5';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
905 case 'CRAM-MD5':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
906 case 'DIGEST-MD5':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
907 case 'PLAIN':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
908 case 'GSSAPI':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
909 $result = $this->authenticate($user, $password, $auth_method);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
910 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
911 case 'LOGIN':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
912 $result = $this->login($user, $password);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
913 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
914 default:
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
915 $this->setError(self::ERROR_BAD, "Configuration error. Unknown auth method: $auth_method");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
916 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
917
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
918 // Connected and authenticated
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
919 if (is_resource($result)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
920 if ($this->prefs['force_caps']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
921 $this->clearCapability();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
922 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
923 $this->logged = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
924
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
925 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
926 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
927
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
928 $this->closeConnection();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
929
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
930 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
931 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
932
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
933 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
934 * Connects to IMAP server.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
935 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
936 * @param string $host Server hostname or IP
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
937 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
938 * @return bool True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
939 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
940 protected function _connect($host)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
941 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
942 // initialize connection
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
943 $this->error = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
944 $this->errornum = self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
945
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
946 if (!$this->prefs['port']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
947 $this->prefs['port'] = 143;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
948 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
949
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
950 // check for SSL
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
951 if ($this->prefs['ssl_mode'] && $this->prefs['ssl_mode'] != 'tls') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
952 $host = $this->prefs['ssl_mode'] . '://' . $host;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
953 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
954
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
955 if ($this->prefs['timeout'] <= 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
956 $this->prefs['timeout'] = max(0, intval(ini_get('default_socket_timeout')));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
957 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
958
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
959 if (!empty($this->prefs['socket_options'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
960 $context = stream_context_create($this->prefs['socket_options']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
961 $this->fp = stream_socket_client($host . ':' . $this->prefs['port'], $errno, $errstr,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
962 $this->prefs['timeout'], STREAM_CLIENT_CONNECT, $context);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
963 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
964 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
965 $this->fp = @fsockopen($host, $this->prefs['port'], $errno, $errstr, $this->prefs['timeout']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
966 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
967
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
968 if (!$this->fp) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
969 $this->setError(self::ERROR_BAD, sprintf("Could not connect to %s:%d: %s",
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
970 $host, $this->prefs['port'], $errstr ?: "Unknown reason"));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
971
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
972 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
973 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
974
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
975 if ($this->prefs['timeout'] > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
976 stream_set_timeout($this->fp, $this->prefs['timeout']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
977 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
978
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
979 $line = trim(fgets($this->fp, 8192));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
980
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
981 if ($this->debug) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
982 // set connection identifier for debug output
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
983 preg_match('/#([0-9]+)/', (string) $this->fp, $m);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
984 $this->resourceid = strtoupper(substr(md5($m[1].$this->user.microtime()), 0, 4));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
985
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
986 if ($line) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
987 $this->debug('S: '. $line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
988 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
989 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
990
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
991 // Connected to wrong port or connection error?
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
992 if (!preg_match('/^\* (OK|PREAUTH)/i', $line)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
993 if ($line)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
994 $error = sprintf("Wrong startup greeting (%s:%d): %s", $host, $this->prefs['port'], $line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
995 else
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
996 $error = sprintf("Empty startup greeting (%s:%d)", $host, $this->prefs['port']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
997
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
998 $this->setError(self::ERROR_BAD, $error);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
999 $this->closeConnection();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1000 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1001 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1002
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1003 $this->data['GREETING'] = trim(preg_replace('/\[[^\]]+\]\s*/', '', $line));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1004
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1005 // RFC3501 [7.1] optional CAPABILITY response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1006 if (preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1007 $this->parseCapability($matches[1], true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1008 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1009
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1010 // TLS connection
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1011 if ($this->prefs['ssl_mode'] == 'tls' && $this->getCapability('STARTTLS')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1012 $res = $this->execute('STARTTLS');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1013
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1014 if ($res[0] != self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1015 $this->closeConnection();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1016 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1017 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1018
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1019 if (isset($this->prefs['socket_options']['ssl']['crypto_method'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1020 $crypto_method = $this->prefs['socket_options']['ssl']['crypto_method'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1021 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1022 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1023 // There is no flag to enable all TLS methods. Net_SMTP
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1024 // handles enabling TLS similarly.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1025 $crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1026 | @STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1027 | @STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1028 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1029
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1030 if (!stream_socket_enable_crypto($this->fp, true, $crypto_method)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1031 $this->setError(self::ERROR_BAD, "Unable to negotiate TLS");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1032 $this->closeConnection();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1033 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1034 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1035
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1036 // Now we're secure, capabilities need to be reread
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1037 $this->clearCapability();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1038 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1039
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1040 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1041 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1042
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1043 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1044 * Initializes environment
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1045 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1046 protected function set_prefs($prefs)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1047 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1048 // set preferences
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1049 if (is_array($prefs)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1050 $this->prefs = $prefs;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1051 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1052
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1053 // set auth method
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1054 if (!empty($this->prefs['auth_type'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1055 $this->prefs['auth_type'] = strtoupper($this->prefs['auth_type']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1056 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1057 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1058 $this->prefs['auth_type'] = 'CHECK';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1059 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1060
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1061 // disabled capabilities
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1062 if (!empty($this->prefs['disabled_caps'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1063 $this->prefs['disabled_caps'] = array_map('strtoupper', (array)$this->prefs['disabled_caps']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1064 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1065
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1066 // additional message flags
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1067 if (!empty($this->prefs['message_flags'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1068 $this->flags = array_merge($this->flags, $this->prefs['message_flags']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1069 unset($this->prefs['message_flags']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1070 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1071 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1072
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1073 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1074 * Checks connection status
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1075 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1076 * @return bool True if connection is active and user is logged in, False otherwise.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1077 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1078 public function connected()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1079 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1080 return $this->fp && $this->logged;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1081 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1082
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1083 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1084 * Closes connection with logout.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1085 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1086 public function closeConnection()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1087 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1088 if ($this->logged && $this->putLine($this->nextTag() . ' LOGOUT')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1089 $this->readReply();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1090 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1091
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1092 $this->closeSocket();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1093 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1094
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1095 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1096 * Executes SELECT command (if mailbox is already not in selected state)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1097 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1098 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1099 * @param array $qresync_data QRESYNC data (RFC5162)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1100 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1101 * @return boolean True on success, false on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1102 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1103 public function select($mailbox, $qresync_data = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1104 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1105 if (!strlen($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1106 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1107 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1108
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1109 if ($this->selected === $mailbox) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1110 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1111 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1112 /*
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1113 Temporary commented out because Courier returns \Noselect for INBOX
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1114 Requires more investigation
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1115
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1116 if (is_array($this->data['LIST']) && is_array($opts = $this->data['LIST'][$mailbox])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1117 if (in_array('\\Noselect', $opts)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1118 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1119 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1120 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1121 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1122 $params = array($this->escape($mailbox));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1123
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1124 // QRESYNC data items
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1125 // 0. the last known UIDVALIDITY,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1126 // 1. the last known modification sequence,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1127 // 2. the optional set of known UIDs, and
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1128 // 3. an optional parenthesized list of known sequence ranges and their
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1129 // corresponding UIDs.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1130 if (!empty($qresync_data)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1131 if (!empty($qresync_data[2])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1132 $qresync_data[2] = self::compressMessageSet($qresync_data[2]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1133 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1134
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1135 $params[] = array('QRESYNC', $qresync_data);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1136 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1137
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1138 list($code, $response) = $this->execute('SELECT', $params);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1139
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1140 if ($code == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1141 $this->clear_mailbox_cache();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1142
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1143 $response = explode("\r\n", $response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1144 foreach ($response as $line) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1145 if (preg_match('/^\* OK \[/i', $line)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1146 $pos = strcspn($line, ' ]', 6);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1147 $token = strtoupper(substr($line, 6, $pos));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1148 $pos += 7;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1149
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1150 switch ($token) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1151 case 'UIDNEXT':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1152 case 'UIDVALIDITY':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1153 case 'UNSEEN':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1154 if ($len = strspn($line, '0123456789', $pos)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1155 $this->data[$token] = (int) substr($line, $pos, $len);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1156 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1157 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1158
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1159 case 'HIGHESTMODSEQ':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1160 if ($len = strspn($line, '0123456789', $pos)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1161 $this->data[$token] = (string) substr($line, $pos, $len);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1162 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1163 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1164
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1165 case 'NOMODSEQ':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1166 $this->data[$token] = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1167 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1168
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1169 case 'PERMANENTFLAGS':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1170 $start = strpos($line, '(', $pos);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1171 $end = strrpos($line, ')');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1172 if ($start && $end) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1173 $flags = substr($line, $start + 1, $end - $start - 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1174 $this->data[$token] = explode(' ', $flags);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1175 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1176 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1177 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1178 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1179 else if (preg_match('/^\* ([0-9]+) (EXISTS|RECENT|FETCH)/i', $line, $match)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1180 $token = strtoupper($match[2]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1181 switch ($token) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1182 case 'EXISTS':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1183 case 'RECENT':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1184 $this->data[$token] = (int) $match[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1185 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1186
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1187 case 'FETCH':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1188 // QRESYNC FETCH response (RFC5162)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1189 $line = substr($line, strlen($match[0]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1190 $fetch_data = $this->tokenizeResponse($line, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1191 $data = array('id' => $match[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1192
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1193 for ($i=0, $size=count($fetch_data); $i<$size; $i+=2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1194 $data[strtolower($fetch_data[$i])] = $fetch_data[$i+1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1195 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1196
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1197 $this->data['QRESYNC'][$data['uid']] = $data;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1198 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1199 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1200 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1201 // QRESYNC VANISHED response (RFC5162)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1202 else if (preg_match('/^\* VANISHED [()EARLIER]*/i', $line, $match)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1203 $line = substr($line, strlen($match[0]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1204 $v_data = $this->tokenizeResponse($line, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1205
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1206 $this->data['VANISHED'] = $v_data;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1207 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1208 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1209
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1210 $this->data['READ-WRITE'] = $this->resultcode != 'READ-ONLY';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1211 $this->selected = $mailbox;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1212
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1213 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1214 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1215
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1216 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1217 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1218
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1219 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1220 * Executes STATUS command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1221 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1222 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1223 * @param array $items Additional requested item names. By default
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1224 * MESSAGES and UNSEEN are requested. Other defined
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1225 * in RFC3501: UIDNEXT, UIDVALIDITY, RECENT
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1226 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1227 * @return array Status item-value hash
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1228 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1229 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1230 public function status($mailbox, $items = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1231 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1232 if (!strlen($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1233 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1234 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1235
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1236 if (!in_array('MESSAGES', $items)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1237 $items[] = 'MESSAGES';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1238 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1239 if (!in_array('UNSEEN', $items)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1240 $items[] = 'UNSEEN';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1241 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1242
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1243 list($code, $response) = $this->execute('STATUS', array($this->escape($mailbox),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1244 '(' . implode(' ', $items) . ')'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1245
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1246 if ($code == self::ERROR_OK && preg_match('/^\* STATUS /i', $response)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1247 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1248 $response = substr($response, 9); // remove prefix "* STATUS "
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1249
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1250 list($mbox, $items) = $this->tokenizeResponse($response, 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1251
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1252 // Fix for #1487859. Some buggy server returns not quoted
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1253 // folder name with spaces. Let's try to handle this situation
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1254 if (!is_array($items) && ($pos = strpos($response, '(')) !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1255 $response = substr($response, $pos);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1256 $items = $this->tokenizeResponse($response, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1257 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1258
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1259 if (!is_array($items)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1260 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1261 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1262
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1263 for ($i=0, $len=count($items); $i<$len; $i += 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1264 $result[$items[$i]] = $items[$i+1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1265 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1266
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1267 $this->data['STATUS:'.$mailbox] = $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1268
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1269 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1270 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1271
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1272 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1273 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1274
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1275 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1276 * Executes EXPUNGE command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1277 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1278 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1279 * @param string|array $messages Message UIDs to expunge
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1280 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1281 * @return boolean True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1282 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1283 public function expunge($mailbox, $messages = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1284 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1285 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1286 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1287 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1288
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1289 if (!$this->data['READ-WRITE']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1290 $this->setError(self::ERROR_READONLY, "Mailbox is read-only");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1291 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1292 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1293
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1294 // Clear internal status cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1295 $this->clear_status_cache($mailbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1296
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1297 if (!empty($messages) && $messages != '*' && $this->hasCapability('UIDPLUS')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1298 $messages = self::compressMessageSet($messages);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1299 $result = $this->execute('UID EXPUNGE', array($messages), self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1300 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1301 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1302 $result = $this->execute('EXPUNGE', null, self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1303 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1304
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1305 if ($result == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1306 $this->selected = null; // state has changed, need to reselect
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1307 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1308 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1309
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1310 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1311 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1312
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1313 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1314 * Executes CLOSE command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1315 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1316 * @return boolean True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1317 * @since 0.5
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1318 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1319 public function close()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1320 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1321 $result = $this->execute('CLOSE', null, self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1322
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1323 if ($result == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1324 $this->selected = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1325 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1326 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1327
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1328 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1329 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1330
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1331 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1332 * Folder subscription (SUBSCRIBE)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1333 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1334 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1335 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1336 * @return boolean True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1337 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1338 public function subscribe($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1339 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1340 $result = $this->execute('SUBSCRIBE', array($this->escape($mailbox)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1341 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1342
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1343 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1344 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1345
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1346 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1347 * Folder unsubscription (UNSUBSCRIBE)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1348 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1349 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1350 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1351 * @return boolean True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1352 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1353 public function unsubscribe($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1354 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1355 $result = $this->execute('UNSUBSCRIBE', array($this->escape($mailbox)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1356 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1357
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1358 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1359 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1360
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1361 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1362 * Folder creation (CREATE)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1363 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1364 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1365 * @param array $types Optional folder types (RFC 6154)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1366 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1367 * @return bool True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1368 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1369 public function createFolder($mailbox, $types = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1370 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1371 $args = array($this->escape($mailbox));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1372
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1373 // RFC 6154: CREATE-SPECIAL-USE
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1374 if (!empty($types) && $this->getCapability('CREATE-SPECIAL-USE')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1375 $args[] = '(USE (' . implode(' ', $types) . '))';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1376 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1377
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1378 $result = $this->execute('CREATE', $args, self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1379
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1380 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1381 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1382
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1383 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1384 * Folder renaming (RENAME)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1385 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1386 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1387 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1388 * @return bool True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1389 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1390 public function renameFolder($from, $to)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1391 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1392 $result = $this->execute('RENAME', array($this->escape($from), $this->escape($to)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1393 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1394
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1395 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1396 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1397
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1398 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1399 * Executes DELETE command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1400 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1401 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1402 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1403 * @return boolean True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1404 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1405 public function deleteFolder($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1406 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1407 $result = $this->execute('DELETE', array($this->escape($mailbox)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1408 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1409
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1410 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1411 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1412
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1413 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1414 * Removes all messages in a folder
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1415 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1416 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1417 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1418 * @return boolean True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1419 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1420 public function clearFolder($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1421 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1422 if ($this->countMessages($mailbox) > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1423 $res = $this->flag($mailbox, '1:*', 'DELETED');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1424 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1425
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1426 if ($res) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1427 if ($this->selected === $mailbox) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1428 $res = $this->close();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1429 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1430 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1431 $res = $this->expunge($mailbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1432 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1433 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1434
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1435 return $res;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1436 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1437
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1438 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1439 * Returns list of mailboxes
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1440 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1441 * @param string $ref Reference name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1442 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1443 * @param array $return_opts (see self::_listMailboxes)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1444 * @param array $select_opts (see self::_listMailboxes)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1445 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1446 * @return array|bool List of mailboxes or hash of options if STATUS/MYROGHTS response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1447 * is requested, False on error.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1448 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1449 public function listMailboxes($ref, $mailbox, $return_opts = array(), $select_opts = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1450 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1451 return $this->_listMailboxes($ref, $mailbox, false, $return_opts, $select_opts);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1452 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1453
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1454 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1455 * Returns list of subscribed mailboxes
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1456 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1457 * @param string $ref Reference name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1458 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1459 * @param array $return_opts (see self::_listMailboxes)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1460 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1461 * @return array|bool List of mailboxes or hash of options if STATUS/MYROGHTS response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1462 * is requested, False on error.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1463 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1464 public function listSubscribed($ref, $mailbox, $return_opts = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1465 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1466 return $this->_listMailboxes($ref, $mailbox, true, $return_opts, null);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1467 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1468
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1469 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1470 * IMAP LIST/LSUB command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1471 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1472 * @param string $ref Reference name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1473 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1474 * @param bool $subscribed Enables returning subscribed mailboxes only
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1475 * @param array $return_opts List of RETURN options (RFC5819: LIST-STATUS, RFC5258: LIST-EXTENDED)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1476 * Possible: MESSAGES, RECENT, UIDNEXT, UIDVALIDITY, UNSEEN,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1477 * MYRIGHTS, SUBSCRIBED, CHILDREN
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1478 * @param array $select_opts List of selection options (RFC5258: LIST-EXTENDED)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1479 * Possible: SUBSCRIBED, RECURSIVEMATCH, REMOTE,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1480 * SPECIAL-USE (RFC6154)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1481 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1482 * @return array|bool List of mailboxes or hash of options if STATUS/MYROGHTS response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1483 * is requested, False on error.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1484 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1485 protected function _listMailboxes($ref, $mailbox, $subscribed=false,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1486 $return_opts=array(), $select_opts=array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1487 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1488 if (!strlen($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1489 $mailbox = '*';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1490 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1491
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1492 $args = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1493 $rets = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1494
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1495 if (!empty($select_opts) && $this->getCapability('LIST-EXTENDED')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1496 $select_opts = (array) $select_opts;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1497
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1498 $args[] = '(' . implode(' ', $select_opts) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1499 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1500
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1501 $args[] = $this->escape($ref);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1502 $args[] = $this->escape($mailbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1503
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1504 if (!empty($return_opts) && $this->getCapability('LIST-EXTENDED')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1505 $ext_opts = array('SUBSCRIBED', 'CHILDREN');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1506 $rets = array_intersect($return_opts, $ext_opts);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1507 $return_opts = array_diff($return_opts, $rets);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1508 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1509
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1510 if (!empty($return_opts) && $this->getCapability('LIST-STATUS')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1511 $lstatus = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1512 $status_opts = array('MESSAGES', 'RECENT', 'UIDNEXT', 'UIDVALIDITY', 'UNSEEN');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1513 $opts = array_diff($return_opts, $status_opts);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1514 $status_opts = array_diff($return_opts, $opts);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1515
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1516 if (!empty($status_opts)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1517 $rets[] = 'STATUS (' . implode(' ', $status_opts) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1518 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1519
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1520 if (!empty($opts)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1521 $rets = array_merge($rets, $opts);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1522 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1523 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1524
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1525 if (!empty($rets)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1526 $args[] = 'RETURN (' . implode(' ', $rets) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1527 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1528
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1529 list($code, $response) = $this->execute($subscribed ? 'LSUB' : 'LIST', $args);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1530
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1531 if ($code == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1532 $folders = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1533 $last = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1534 $pos = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1535 $response .= "\r\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1536
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1537 while ($pos = strpos($response, "\r\n", $pos+1)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1538 // literal string, not real end-of-command-line
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1539 if ($response[$pos-1] == '}') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1540 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1541 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1542
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1543 $line = substr($response, $last, $pos - $last);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1544 $last = $pos + 2;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1545
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1546 if (!preg_match('/^\* (LIST|LSUB|STATUS|MYRIGHTS) /i', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1547 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1548 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1549
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1550 $cmd = strtoupper($m[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1551 $line = substr($line, strlen($m[0]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1552
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1553 // * LIST (<options>) <delimiter> <mailbox>
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1554 if ($cmd == 'LIST' || $cmd == 'LSUB') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1555 list($opts, $delim, $mailbox) = $this->tokenizeResponse($line, 3);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1556
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1557 // Remove redundant separator at the end of folder name, UW-IMAP bug? (#1488879)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1558 if ($delim) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1559 $mailbox = rtrim($mailbox, $delim);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1560 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1561
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1562 // Add to result array
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1563 if (!$lstatus) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1564 $folders[] = $mailbox;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1565 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1566 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1567 $folders[$mailbox] = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1568 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1569
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1570 // store folder options
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1571 if ($cmd == 'LIST') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1572 // Add to options array
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1573 if (empty($this->data['LIST'][$mailbox])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1574 $this->data['LIST'][$mailbox] = $opts;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1575 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1576 else if (!empty($opts)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1577 $this->data['LIST'][$mailbox] = array_unique(array_merge(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1578 $this->data['LIST'][$mailbox], $opts));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1579 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1580 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1581 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1582 else if ($lstatus) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1583 // * STATUS <mailbox> (<result>)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1584 if ($cmd == 'STATUS') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1585 list($mailbox, $status) = $this->tokenizeResponse($line, 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1586
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1587 for ($i=0, $len=count($status); $i<$len; $i += 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1588 list($name, $value) = $this->tokenizeResponse($status, 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1589 $folders[$mailbox][$name] = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1590 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1591 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1592 // * MYRIGHTS <mailbox> <acl>
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1593 else if ($cmd == 'MYRIGHTS') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1594 list($mailbox, $acl) = $this->tokenizeResponse($line, 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1595 $folders[$mailbox]['MYRIGHTS'] = $acl;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1596 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1597 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1598 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1599
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1600 return $folders;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1601 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1602
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1603 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1604 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1605
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1606 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1607 * Returns count of all messages in a folder
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1608 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1609 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1610 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1611 * @return int Number of messages, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1612 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1613 public function countMessages($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1614 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1615 if ($this->selected === $mailbox && isset($this->data['EXISTS'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1616 return $this->data['EXISTS'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1617 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1618
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1619 // Check internal cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1620 $cache = $this->data['STATUS:'.$mailbox];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1621 if (!empty($cache) && isset($cache['MESSAGES'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1622 return (int) $cache['MESSAGES'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1623 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1624
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1625 // Try STATUS (should be faster than SELECT)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1626 $counts = $this->status($mailbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1627 if (is_array($counts)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1628 return (int) $counts['MESSAGES'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1629 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1630
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1631 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1632 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1633
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1634 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1635 * Returns count of messages with \Recent flag in a folder
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1636 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1637 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1638 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1639 * @return int Number of messages, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1640 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1641 public function countRecent($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1642 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1643 if ($this->selected === $mailbox && isset($this->data['RECENT'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1644 return $this->data['RECENT'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1645 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1646
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1647 // Check internal cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1648 $cache = $this->data['STATUS:'.$mailbox];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1649 if (!empty($cache) && isset($cache['RECENT'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1650 return (int) $cache['RECENT'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1651 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1652
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1653 // Try STATUS (should be faster than SELECT)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1654 $counts = $this->status($mailbox, array('RECENT'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1655 if (is_array($counts)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1656 return (int) $counts['RECENT'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1657 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1658
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1659 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1660 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1661
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1662 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1663 * Returns count of messages without \Seen flag in a specified folder
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1664 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1665 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1666 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1667 * @return int Number of messages, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1668 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1669 public function countUnseen($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1670 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1671 // Check internal cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1672 $cache = $this->data['STATUS:'.$mailbox];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1673 if (!empty($cache) && isset($cache['UNSEEN'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1674 return (int) $cache['UNSEEN'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1675 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1676
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1677 // Try STATUS (should be faster than SELECT+SEARCH)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1678 $counts = $this->status($mailbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1679 if (is_array($counts)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1680 return (int) $counts['UNSEEN'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1681 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1682
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1683 // Invoke SEARCH as a fallback
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1684 $index = $this->search($mailbox, 'ALL UNSEEN', false, array('COUNT'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1685 if (!$index->is_error()) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1686 return $index->count();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1687 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1688
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1689 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1690 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1691
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1692 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1693 * Executes ID command (RFC2971)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1694 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1695 * @param array $items Client identification information key/value hash
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1696 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1697 * @return array Server identification information key/value hash
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1698 * @since 0.6
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1699 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1700 public function id($items = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1701 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1702 if (is_array($items) && !empty($items)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1703 foreach ($items as $key => $value) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1704 $args[] = $this->escape($key, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1705 $args[] = $this->escape($value, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1706 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1707 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1708
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1709 list($code, $response) = $this->execute('ID', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1710 !empty($args) ? '(' . implode(' ', (array) $args) . ')' : $this->escape(null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1711 ));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1712
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1713 if ($code == self::ERROR_OK && preg_match('/^\* ID /i', $response)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1714 $response = substr($response, 5); // remove prefix "* ID "
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1715 $items = $this->tokenizeResponse($response, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1716 $result = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1717
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1718 for ($i=0, $len=count($items); $i<$len; $i += 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1719 $result[$items[$i]] = $items[$i+1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1720 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1721
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1722 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1723 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1724
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1725 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1726 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1727
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1728 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1729 * Executes ENABLE command (RFC5161)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1730 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1731 * @param mixed $extension Extension name to enable (or array of names)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1732 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1733 * @return array|bool List of enabled extensions, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1734 * @since 0.6
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1735 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1736 public function enable($extension)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1737 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1738 if (empty($extension)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1739 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1740 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1741
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1742 if (!$this->hasCapability('ENABLE')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1743 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1744 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1745
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1746 if (!is_array($extension)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1747 $extension = array($extension);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1748 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1749
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1750 if (!empty($this->extensions_enabled)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1751 // check if all extensions are already enabled
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1752 $diff = array_diff($extension, $this->extensions_enabled);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1753
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1754 if (empty($diff)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1755 return $extension;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1756 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1757
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1758 // Make sure the mailbox isn't selected, before enabling extension(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1759 if ($this->selected !== null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1760 $this->close();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1761 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1762 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1763
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1764 list($code, $response) = $this->execute('ENABLE', $extension);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1765
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1766 if ($code == self::ERROR_OK && preg_match('/^\* ENABLED /i', $response)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1767 $response = substr($response, 10); // remove prefix "* ENABLED "
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1768 $result = (array) $this->tokenizeResponse($response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1769
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1770 $this->extensions_enabled = array_unique(array_merge((array)$this->extensions_enabled, $result));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1771
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1772 return $this->extensions_enabled;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1773 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1774
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1775 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1776 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1777
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1778 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1779 * Executes SORT command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1780 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1781 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1782 * @param string $field Field to sort by (ARRIVAL, CC, DATE, FROM, SIZE, SUBJECT, TO)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1783 * @param string $criteria Searching criteria
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1784 * @param bool $return_uid Enables UID SORT usage
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1785 * @param string $encoding Character set
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1786 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1787 * @return rcube_result_index Response data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1788 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1789 public function sort($mailbox, $field = 'ARRIVAL', $criteria = '', $return_uid = false, $encoding = 'US-ASCII')
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1790 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1791 $old_sel = $this->selected;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1792 $supported = array('ARRIVAL', 'CC', 'DATE', 'FROM', 'SIZE', 'SUBJECT', 'TO');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1793 $field = strtoupper($field);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1794
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1795 if ($field == 'INTERNALDATE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1796 $field = 'ARRIVAL';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1797 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1798
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1799 if (!in_array($field, $supported)) {
5
3a5f959af5ae debugging aids, all commented out
Charlie Root
parents: 0
diff changeset
1800 #rcube::write_log('mail',"$field not supported: $mailbox fallback");
0
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1801 return new rcube_result_index($mailbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1802 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1803
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1804 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1805 return new rcube_result_index($mailbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1806 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1807
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1808 // return empty result when folder is empty and we're just after SELECT
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1809 if ($old_sel != $mailbox && !$this->data['EXISTS']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1810 return new rcube_result_index($mailbox, '* SORT');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1811 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1812
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1813 // RFC 5957: SORT=DISPLAY
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1814 if (($field == 'FROM' || $field == 'TO') && $this->getCapability('SORT=DISPLAY')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1815 $field = 'DISPLAY' . $field;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1816 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1817
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1818 $encoding = $encoding ? trim($encoding) : 'US-ASCII';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1819 $criteria = $criteria ? 'ALL ' . trim($criteria) : 'ALL';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1820
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1821 list($code, $response) = $this->execute($return_uid ? 'UID SORT' : 'SORT',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1822 array("($field)", $encoding, $criteria));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1823
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1824 if ($code != self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1825 $response = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1826 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1827
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1828 return new rcube_result_index($mailbox, $response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1829 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1830
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1831 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1832 * Executes THREAD command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1833 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1834 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1835 * @param string $algorithm Threading algorithm (ORDEREDSUBJECT, REFERENCES, REFS)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1836 * @param string $criteria Searching criteria
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1837 * @param bool $return_uid Enables UIDs in result instead of sequence numbers
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1838 * @param string $encoding Character set
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1839 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1840 * @return rcube_result_thread Thread data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1841 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1842 public function thread($mailbox, $algorithm = 'REFERENCES', $criteria = '', $return_uid = false, $encoding = 'US-ASCII')
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1843 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1844 $old_sel = $this->selected;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1845
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1846 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1847 return new rcube_result_thread($mailbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1848 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1849
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1850 // return empty result when folder is empty and we're just after SELECT
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1851 if ($old_sel != $mailbox && !$this->data['EXISTS']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1852 return new rcube_result_thread($mailbox, '* THREAD');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1853 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1854
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1855 $encoding = $encoding ? trim($encoding) : 'US-ASCII';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1856 $algorithm = $algorithm ? trim($algorithm) : 'REFERENCES';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1857 $criteria = $criteria ? 'ALL '.trim($criteria) : 'ALL';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1858
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1859 list($code, $response) = $this->execute($return_uid ? 'UID THREAD' : 'THREAD',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1860 array($algorithm, $encoding, $criteria));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1861
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1862 if ($code != self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1863 $response = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1864 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1865
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1866 return new rcube_result_thread($mailbox, $response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1867 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1868
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1869 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1870 * Executes SEARCH command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1871 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1872 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1873 * @param string $criteria Searching criteria
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1874 * @param bool $return_uid Enable UID in result instead of sequence ID
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1875 * @param array $items Return items (MIN, MAX, COUNT, ALL)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1876 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1877 * @return rcube_result_index Result data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1878 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1879 public function search($mailbox, $criteria, $return_uid = false, $items = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1880 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1881 $old_sel = $this->selected;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1882
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1883 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1884 return new rcube_result_index($mailbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1885 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1886
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1887 // return empty result when folder is empty and we're just after SELECT
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1888 if ($old_sel != $mailbox && !$this->data['EXISTS']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1889 return new rcube_result_index($mailbox, '* SEARCH');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1890 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1891
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1892 // If ESEARCH is supported always use ALL
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1893 // but not when items are specified or using simple id2uid search
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1894 if (empty($items) && preg_match('/[^0-9]/', $criteria)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1895 $items = array('ALL');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1896 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1897
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1898 $esearch = empty($items) ? false : $this->getCapability('ESEARCH');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1899 $criteria = trim($criteria);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1900 $params = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1901
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1902 // RFC4731: ESEARCH
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1903 if (!empty($items) && $esearch) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1904 $params .= 'RETURN (' . implode(' ', $items) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1905 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1906
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1907 if (!empty($criteria)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1908 $params .= ($params ? ' ' : '') . $criteria;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1909 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1910 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1911 $params .= 'ALL';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1912 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1913
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1914 list($code, $response) = $this->execute($return_uid ? 'UID SEARCH' : 'SEARCH',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1915 array($params));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1916
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1917 if ($code != self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1918 $response = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1919 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1920
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1921 return new rcube_result_index($mailbox, $response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1922 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1923
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1924 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1925 * Simulates SORT command by using FETCH and sorting.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1926 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1927 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1928 * @param string|array $message_set Searching criteria (list of messages to return)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1929 * @param string $index_field Field to sort by (ARRIVAL, CC, DATE, FROM, SIZE, SUBJECT, TO)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1930 * @param bool $skip_deleted Makes that DELETED messages will be skipped
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1931 * @param bool $uidfetch Enables UID FETCH usage
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1932 * @param bool $return_uid Enables returning UIDs instead of IDs
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1933 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1934 * @return rcube_result_index Response data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1935 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1936 public function index($mailbox, $message_set, $index_field='', $skip_deleted=true,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1937 $uidfetch=false, $return_uid=false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1938 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1939 $msg_index = $this->fetchHeaderIndex($mailbox, $message_set,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1940 $index_field, $skip_deleted, $uidfetch, $return_uid);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1941
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1942 if (!empty($msg_index)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1943 asort($msg_index); // ASC
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1944 $msg_index = array_keys($msg_index);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1945 $msg_index = '* SEARCH ' . implode(' ', $msg_index);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1946 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1947 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1948 $msg_index = is_array($msg_index) ? '* SEARCH' : null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1949 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1950
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1951 return new rcube_result_index($mailbox, $msg_index);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1952 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1953
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1954 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1955 * Fetches specified header/data value for a set of messages.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1956 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1957 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1958 * @param string|array $message_set Searching criteria (list of messages to return)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1959 * @param string $index_field Field to sort by (ARRIVAL, CC, DATE, FROM, SIZE, SUBJECT, TO)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1960 * @param bool $skip_deleted Makes that DELETED messages will be skipped
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1961 * @param bool $uidfetch Enables UID FETCH usage
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1962 * @param bool $return_uid Enables returning UIDs instead of IDs
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1963 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1964 * @return array|bool List of header values or False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1965 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1966 public function fetchHeaderIndex($mailbox, $message_set, $index_field = '', $skip_deleted = true,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1967 $uidfetch = false, $return_uid = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1968 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1969 if (is_array($message_set)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1970 if (!($message_set = $this->compressMessageSet($message_set))) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1971 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1972 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1973 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1974 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1975 list($from_idx, $to_idx) = explode(':', $message_set);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1976 if (empty($message_set) ||
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1977 (isset($to_idx) && $to_idx != '*' && (int)$from_idx > (int)$to_idx)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1978 ) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1979 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1980 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1981 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1982
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1983 $index_field = empty($index_field) ? 'DATE' : strtoupper($index_field);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1984
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1985 $fields_a['DATE'] = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1986 $fields_a['INTERNALDATE'] = 4;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1987 $fields_a['ARRIVAL'] = 4;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1988 $fields_a['FROM'] = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1989 $fields_a['REPLY-TO'] = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1990 $fields_a['SENDER'] = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1991 $fields_a['TO'] = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1992 $fields_a['CC'] = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1993 $fields_a['SUBJECT'] = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1994 $fields_a['UID'] = 2;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1995 $fields_a['SIZE'] = 2;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1996 $fields_a['SEEN'] = 3;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1997 $fields_a['RECENT'] = 3;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1998 $fields_a['DELETED'] = 3;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1999
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2000 if (!($mode = $fields_a[$index_field])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2001 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2002 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2003
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2004 // Select the mailbox
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2005 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2006 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2007 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2008
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2009 // build FETCH command string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2010 $key = $this->nextTag();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2011 $cmd = $uidfetch ? 'UID FETCH' : 'FETCH';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2012 $fields = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2013
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2014 if ($return_uid) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2015 $fields[] = 'UID';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2016 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2017 if ($skip_deleted) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2018 $fields[] = 'FLAGS';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2019 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2020
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2021 if ($mode == 1) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2022 if ($index_field == 'DATE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2023 $fields[] = 'INTERNALDATE';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2024 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2025 $fields[] = "BODY.PEEK[HEADER.FIELDS ($index_field)]";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2026 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2027 else if ($mode == 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2028 if ($index_field == 'SIZE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2029 $fields[] = 'RFC822.SIZE';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2030 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2031 else if (!$return_uid || $index_field != 'UID') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2032 $fields[] = $index_field;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2033 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2034 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2035 else if ($mode == 3 && !$skip_deleted) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2036 $fields[] = 'FLAGS';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2037 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2038 else if ($mode == 4) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2039 $fields[] = 'INTERNALDATE';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2040 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2041
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2042 $request = "$key $cmd $message_set (" . implode(' ', $fields) . ")";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2043
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2044 if (!$this->putLine($request)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2045 $this->setError(self::ERROR_COMMAND, "Failed to send $cmd command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2046 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2047 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2048
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2049 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2050
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2051 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2052 $line = rtrim($this->readLine(200));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2053 $line = $this->multLine($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2054
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2055 if (preg_match('/^\* ([0-9]+) FETCH/', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2056 $id = $m[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2057 $flags = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2058
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2059 if ($return_uid) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2060 if (preg_match('/UID ([0-9]+)/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2061 $id = (int) $matches[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2062 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2063 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2064 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2065 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2066 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2067 if ($skip_deleted && preg_match('/FLAGS \(([^)]+)\)/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2068 $flags = explode(' ', strtoupper($matches[1]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2069 if (in_array('\\DELETED', $flags)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2070 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2071 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2072 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2073
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2074 if ($mode == 1 && $index_field == 'DATE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2075 if (preg_match('/BODY\[HEADER\.FIELDS \("*DATE"*\)\] (.*)/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2076 $value = preg_replace(array('/^"*[a-z]+:/i'), '', $matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2077 $value = trim($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2078 $result[$id] = rcube_utils::strtotime($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2079 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2080 // non-existent/empty Date: header, use INTERNALDATE
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2081 if (empty($result[$id])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2082 if (preg_match('/INTERNALDATE "([^"]+)"/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2083 $result[$id] = rcube_utils::strtotime($matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2084 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2085 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2086 $result[$id] = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2087 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2088 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2089 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2090 else if ($mode == 1) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2091 if (preg_match('/BODY\[HEADER\.FIELDS \("?(FROM|REPLY-TO|SENDER|TO|SUBJECT)"?\)\] (.*)/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2092 $value = preg_replace(array('/^"*[a-z]+:/i', '/\s+$/sm'), array('', ''), $matches[2]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2093 $result[$id] = trim($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2094 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2095 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2096 $result[$id] = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2097 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2098 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2099 else if ($mode == 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2100 if (preg_match('/' . $index_field . ' ([0-9]+)/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2101 $result[$id] = trim($matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2102 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2103 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2104 $result[$id] = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2105 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2106 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2107 else if ($mode == 3) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2108 if (!$flags && preg_match('/FLAGS \(([^)]+)\)/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2109 $flags = explode(' ', $matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2110 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2111 $result[$id] = in_array("\\".$index_field, (array) $flags) ? 1 : 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2112 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2113 else if ($mode == 4) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2114 if (preg_match('/INTERNALDATE "([^"]+)"/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2115 $result[$id] = rcube_utils::strtotime($matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2116 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2117 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2118 $result[$id] = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2119 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2120 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2121 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2122 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2123 while (!$this->startsWith($line, $key, true, true));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2124
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2125 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2126 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2127
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2128 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2129 * Returns message sequence identifier
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2130 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2131 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2132 * @param int $uid Message unique identifier (UID)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2133 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2134 * @return int Message sequence identifier
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2135 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2136 public function UID2ID($mailbox, $uid)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2137 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2138 if ($uid > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2139 $index = $this->search($mailbox, "UID $uid");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2140
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2141 if ($index->count() == 1) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2142 $arr = $index->get();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2143 return (int) $arr[0];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2144 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2145 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2146 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2147
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2148 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2149 * Returns message unique identifier (UID)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2150 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2151 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2152 * @param int $uid Message sequence identifier
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2153 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2154 * @return int Message unique identifier
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2155 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2156 public function ID2UID($mailbox, $id)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2157 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2158 if (empty($id) || $id < 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2159 return null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2160 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2161
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2162 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2163 return null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2164 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2165
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2166 if ($uid = $this->data['UID-MAP'][$id]) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2167 return $uid;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2168 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2169
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2170 if (isset($this->data['EXISTS']) && $id > $this->data['EXISTS']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2171 return null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2172 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2173
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2174 $index = $this->search($mailbox, $id, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2175
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2176 if ($index->count() == 1) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2177 $arr = $index->get();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2178 return $this->data['UID-MAP'][$id] = (int) $arr[0];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2179 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2180 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2181
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2182 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2183 * Sets flag of the message(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2184 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2185 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2186 * @param string|array $messages Message UID(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2187 * @param string $flag Flag name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2188 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2189 * @return bool True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2190 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2191 public function flag($mailbox, $messages, $flag)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2192 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2193 return $this->modFlag($mailbox, $messages, $flag, '+');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2194 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2195
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2196 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2197 * Unsets flag of the message(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2198 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2199 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2200 * @param string|array $messages Message UID(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2201 * @param string $flag Flag name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2202 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2203 * @return bool True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2204 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2205 public function unflag($mailbox, $messages, $flag)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2206 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2207 return $this->modFlag($mailbox, $messages, $flag, '-');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2208 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2209
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2210 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2211 * Changes flag of the message(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2212 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2213 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2214 * @param string|array $messages Message UID(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2215 * @param string $flag Flag name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2216 * @param string $mod Modifier [+|-]. Default: "+".
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2217 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2218 * @return bool True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2219 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2220 protected function modFlag($mailbox, $messages, $flag, $mod = '+')
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2221 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2222 if (!$flag) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2223 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2224 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2225
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2226 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2227 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2228 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2229
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2230 if (!$this->data['READ-WRITE']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2231 $this->setError(self::ERROR_READONLY, "Mailbox is read-only");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2232 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2233 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2234
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2235 if ($this->flags[strtoupper($flag)]) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2236 $flag = $this->flags[strtoupper($flag)];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2237 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2238
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2239 // if PERMANENTFLAGS is not specified all flags are allowed
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2240 if (!empty($this->data['PERMANENTFLAGS'])
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2241 && !in_array($flag, (array) $this->data['PERMANENTFLAGS'])
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2242 && !in_array('\\*', (array) $this->data['PERMANENTFLAGS'])
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2243 ) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2244 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2245 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2246
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2247 // Clear internal status cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2248 if ($flag == 'SEEN') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2249 unset($this->data['STATUS:'.$mailbox]['UNSEEN']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2250 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2251
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2252 if ($mod != '+' && $mod != '-') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2253 $mod = '+';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2254 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2255
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2256 $result = $this->execute('UID STORE', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2257 $this->compressMessageSet($messages), $mod . 'FLAGS.SILENT', "($flag)"),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2258 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2259
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2260 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2261 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2262
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2263 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2264 * Copies message(s) from one folder to another
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2265 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2266 * @param string|array $messages Message UID(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2267 * @param string $from Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2268 * @param string $to Destination mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2269 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2270 * @return bool True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2271 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2272 public function copy($messages, $from, $to)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2273 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2274 // Clear last COPYUID data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2275 unset($this->data['COPYUID']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2276
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2277 if (!$this->select($from)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2278 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2279 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2280
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2281 // Clear internal status cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2282 unset($this->data['STATUS:'.$to]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2283
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2284 $result = $this->execute('UID COPY', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2285 $this->compressMessageSet($messages), $this->escape($to)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2286 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2287
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2288 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2289 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2290
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2291 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2292 * Moves message(s) from one folder to another.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2293 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2294 * @param string|array $messages Message UID(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2295 * @param string $from Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2296 * @param string $to Destination mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2297 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2298 * @return bool True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2299 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2300 public function move($messages, $from, $to)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2301 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2302 if (!$this->select($from)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2303 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2304 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2305
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2306 if (!$this->data['READ-WRITE']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2307 $this->setError(self::ERROR_READONLY, "Mailbox is read-only");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2308 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2309 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2310
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2311 // use MOVE command (RFC 6851)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2312 if ($this->hasCapability('MOVE')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2313 // Clear last COPYUID data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2314 unset($this->data['COPYUID']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2315
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2316 // Clear internal status cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2317 unset($this->data['STATUS:'.$to]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2318 $this->clear_status_cache($from);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2319
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2320 $result = $this->execute('UID MOVE', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2321 $this->compressMessageSet($messages), $this->escape($to)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2322 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2323
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2324 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2325 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2326
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2327 // use COPY + STORE +FLAGS.SILENT \Deleted + EXPUNGE
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2328 $result = $this->copy($messages, $from, $to);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2329
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2330 if ($result) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2331 // Clear internal status cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2332 unset($this->data['STATUS:'.$from]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2333
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2334 $result = $this->flag($from, $messages, 'DELETED');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2335
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2336 if ($messages == '*') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2337 // CLOSE+SELECT should be faster than EXPUNGE
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2338 $this->close();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2339 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2340 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2341 $this->expunge($from, $messages);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2342 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2343 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2344
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2345 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2346 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2347
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2348 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2349 * FETCH command (RFC3501)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2350 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2351 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2352 * @param mixed $message_set Message(s) sequence identifier(s) or UID(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2353 * @param bool $is_uid True if $message_set contains UIDs
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2354 * @param array $query_items FETCH command data items
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2355 * @param string $mod_seq Modification sequence for CHANGEDSINCE (RFC4551) query
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2356 * @param bool $vanished Enables VANISHED parameter (RFC5162) for CHANGEDSINCE query
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2357 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2358 * @return array List of rcube_message_header elements, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2359 * @since 0.6
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2360 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2361 public function fetch($mailbox, $message_set, $is_uid = false, $query_items = array(),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2362 $mod_seq = null, $vanished = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2363 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2364 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2365 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2366 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2367
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2368 $message_set = $this->compressMessageSet($message_set);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2369 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2370
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2371 $key = $this->nextTag();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2372 $cmd = ($is_uid ? 'UID ' : '') . 'FETCH';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2373 $request = "$key $cmd $message_set (" . implode(' ', $query_items) . ")";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2374
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2375 if ($mod_seq !== null && $this->hasCapability('CONDSTORE')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2376 $request .= " (CHANGEDSINCE $mod_seq" . ($vanished ? " VANISHED" : '') .")";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2377 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2378
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2379 if (!$this->putLine($request)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2380 $this->setError(self::ERROR_COMMAND, "Failed to send $cmd command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2381 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2382 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2383
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2384 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2385 $line = $this->readLine(4096);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2386
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2387 if (!$line) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2388 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2389 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2390
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2391 // Sample reply line:
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2392 // * 321 FETCH (UID 2417 RFC822.SIZE 2730 FLAGS (\Seen)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2393 // INTERNALDATE "16-Nov-2008 21:08:46 +0100" BODYSTRUCTURE (...)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2394 // BODY[HEADER.FIELDS ...
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2395
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2396 if (preg_match('/^\* ([0-9]+) FETCH/', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2397 $id = intval($m[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2398
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2399 $result[$id] = new rcube_message_header;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2400 $result[$id]->id = $id;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2401 $result[$id]->subject = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2402 $result[$id]->messageID = 'mid:' . $id;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2403
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2404 $headers = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2405 $lines = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2406 $line = substr($line, strlen($m[0]) + 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2407 $ln = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2408
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2409 // get complete entry
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2410 while (preg_match('/\{([0-9]+)\}\r\n$/', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2411 $bytes = $m[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2412 $out = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2413
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2414 while (strlen($out) < $bytes) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2415 $out = $this->readBytes($bytes);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2416 if ($out === null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2417 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2418 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2419 $line .= $out;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2420 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2421
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2422 $str = $this->readLine(4096);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2423 if ($str === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2424 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2425 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2426
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2427 $line .= $str;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2428 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2429
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2430 // Tokenize response and assign to object properties
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2431 while (list($name, $value) = $this->tokenizeResponse($line, 2)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2432 if ($name == 'UID') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2433 $result[$id]->uid = intval($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2434 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2435 else if ($name == 'RFC822.SIZE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2436 $result[$id]->size = intval($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2437 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2438 else if ($name == 'RFC822.TEXT') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2439 $result[$id]->body = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2440 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2441 else if ($name == 'INTERNALDATE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2442 $result[$id]->internaldate = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2443 $result[$id]->date = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2444 $result[$id]->timestamp = rcube_utils::strtotime($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2445 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2446 else if ($name == 'FLAGS') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2447 if (!empty($value)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2448 foreach ((array)$value as $flag) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2449 $flag = str_replace(array('$', "\\"), '', $flag);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2450 $flag = strtoupper($flag);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2451
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2452 $result[$id]->flags[$flag] = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2453 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2454 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2455 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2456 else if ($name == 'MODSEQ') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2457 $result[$id]->modseq = $value[0];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2458 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2459 else if ($name == 'ENVELOPE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2460 $result[$id]->envelope = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2461 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2462 else if ($name == 'BODYSTRUCTURE' || ($name == 'BODY' && count($value) > 2)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2463 if (!is_array($value[0]) && (strtolower($value[0]) == 'message' && strtolower($value[1]) == 'rfc822')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2464 $value = array($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2465 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2466 $result[$id]->bodystructure = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2467 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2468 else if ($name == 'RFC822') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2469 $result[$id]->body = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2470 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2471 else if (stripos($name, 'BODY[') === 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2472 $name = str_replace(']', '', substr($name, 5));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2473
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2474 if ($name == 'HEADER.FIELDS') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2475 // skip ']' after headers list
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2476 $this->tokenizeResponse($line, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2477 $headers = $this->tokenizeResponse($line, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2478 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2479 else if (strlen($name)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2480 $result[$id]->bodypart[$name] = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2481 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2482 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2483 $result[$id]->body = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2484 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2485 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2486 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2487
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2488 // create array with header field:data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2489 if (!empty($headers)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2490 $headers = explode("\n", trim($headers));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2491 foreach ($headers as $resln) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2492 if (ord($resln[0]) <= 32) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2493 $lines[$ln] .= (empty($lines[$ln]) ? '' : "\n") . trim($resln);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2494 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2495 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2496 $lines[++$ln] = trim($resln);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2497 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2498 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2499
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2500 foreach ($lines as $str) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2501 list($field, $string) = explode(':', $str, 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2502
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2503 $field = strtolower($field);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2504 $string = preg_replace('/\n[\t\s]*/', ' ', trim($string));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2505
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2506 switch ($field) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2507 case 'date';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2508 $result[$id]->date = $string;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2509 $result[$id]->timestamp = rcube_utils::strtotime($string);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2510 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2511 case 'to':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2512 $result[$id]->to = preg_replace('/undisclosed-recipients:[;,]*/', '', $string);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2513 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2514 case 'from':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2515 case 'subject':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2516 case 'cc':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2517 case 'bcc':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2518 case 'references':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2519 $result[$id]->{$field} = $string;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2520 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2521 case 'reply-to':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2522 $result[$id]->replyto = $string;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2523 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2524 case 'content-transfer-encoding':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2525 $result[$id]->encoding = $string;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2526 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2527 case 'content-type':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2528 $ctype_parts = preg_split('/[; ]+/', $string);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2529 $result[$id]->ctype = strtolower(array_shift($ctype_parts));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2530 if (preg_match('/charset\s*=\s*"?([a-z0-9\-\.\_]+)"?/i', $string, $regs)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2531 $result[$id]->charset = $regs[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2532 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2533 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2534 case 'in-reply-to':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2535 $result[$id]->in_reply_to = str_replace(array("\n", '<', '>'), '', $string);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2536 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2537 case 'return-receipt-to':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2538 case 'disposition-notification-to':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2539 case 'x-confirm-reading-to':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2540 $result[$id]->mdn_to = $string;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2541 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2542 case 'message-id':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2543 $result[$id]->messageID = $string;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2544 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2545 case 'x-priority':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2546 if (preg_match('/^(\d+)/', $string, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2547 $result[$id]->priority = intval($matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2548 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2549 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2550 default:
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2551 if (strlen($field) < 3) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2552 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2553 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2554 if ($result[$id]->others[$field]) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2555 $string = array_merge((array)$result[$id]->others[$field], (array)$string);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2556 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2557 $result[$id]->others[$field] = $string;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2558 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2559 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2560 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2561 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2562 // VANISHED response (QRESYNC RFC5162)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2563 // Sample: * VANISHED (EARLIER) 300:310,405,411
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2564 else if (preg_match('/^\* VANISHED [()EARLIER]*/i', $line, $match)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2565 $line = substr($line, strlen($match[0]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2566 $v_data = $this->tokenizeResponse($line, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2567
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2568 $this->data['VANISHED'] = $v_data;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2569 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2570 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2571 while (!$this->startsWith($line, $key, true));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2572
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2573 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2574 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2575
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2576 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2577 * Returns message(s) data (flags, headers, etc.)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2578 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2579 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2580 * @param mixed $message_set Message(s) sequence identifier(s) or UID(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2581 * @param bool $is_uid True if $message_set contains UIDs
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2582 * @param bool $bodystr Enable to add BODYSTRUCTURE data to the result
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2583 * @param array $add_headers List of additional headers
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2584 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2585 * @return bool|array List of rcube_message_header elements, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2586 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2587 public function fetchHeaders($mailbox, $message_set, $is_uid = false, $bodystr = false, $add_headers = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2588 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2589 $query_items = array('UID', 'RFC822.SIZE', 'FLAGS', 'INTERNALDATE');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2590 $headers = array('DATE', 'FROM', 'TO', 'SUBJECT', 'CONTENT-TYPE', 'CC', 'REPLY-TO',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2591 'LIST-POST', 'DISPOSITION-NOTIFICATION-TO', 'X-PRIORITY');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2592
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2593 if (!empty($add_headers)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2594 $add_headers = array_map('strtoupper', $add_headers);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2595 $headers = array_unique(array_merge($headers, $add_headers));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2596 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2597
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2598 if ($bodystr) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2599 $query_items[] = 'BODYSTRUCTURE';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2600 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2601
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2602 $query_items[] = 'BODY.PEEK[HEADER.FIELDS (' . implode(' ', $headers) . ')]';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2603
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2604 return $this->fetch($mailbox, $message_set, $is_uid, $query_items);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2605 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2606
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2607 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2608 * Returns message data (flags, headers, etc.)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2609 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2610 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2611 * @param int $id Message sequence identifier or UID
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2612 * @param bool $is_uid True if $id is an UID
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2613 * @param bool $bodystr Enable to add BODYSTRUCTURE data to the result
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2614 * @param array $add_headers List of additional headers
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2615 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2616 * @return bool|rcube_message_header Message data, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2617 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2618 public function fetchHeader($mailbox, $id, $is_uid = false, $bodystr = false, $add_headers = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2619 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2620 $a = $this->fetchHeaders($mailbox, $id, $is_uid, $bodystr, $add_headers);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2621 if (is_array($a)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2622 return array_shift($a);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2623 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2624
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2625 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2626 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2627
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2628 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2629 * Sort messages by specified header field
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2630 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2631 * @param array $messages Array of rcube_message_header objects
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2632 * @param string $field Name of the property to sort by
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2633 * @param string $flag Sorting order (ASC|DESC)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2634 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2635 * @return array Sorted input array
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2636 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2637 public static function sortHeaders($messages, $field, $flag)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2638 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2639 // Strategy: First, we'll create an "index" array.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2640 // Then, we'll use sort() on that array, and use that to sort the main array.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2641
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2642 $field = empty($field) ? 'uid' : strtolower($field);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2643 $flag = empty($flag) ? 'ASC' : strtoupper($flag);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2644 $index = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2645 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2646
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2647 reset($messages);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2648
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2649 foreach ($messages as $key => $headers) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2650 $value = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2651
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2652 switch ($field) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2653 case 'arrival':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2654 $field = 'internaldate';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2655 case 'date':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2656 case 'internaldate':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2657 case 'timestamp':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2658 $value = rcube_utils::strtotime($headers->$field);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2659 if (!$value && $field != 'timestamp') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2660 $value = $headers->timestamp;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2661 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2662
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2663 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2664
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2665 default:
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2666 // @TODO: decode header value, convert to UTF-8
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2667 $value = $headers->$field;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2668 if (is_string($value)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2669 $value = str_replace('"', '', $value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2670 if ($field == 'subject') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2671 $value = preg_replace('/^(Re:\s*|Fwd:\s*|Fw:\s*)+/i', '', $value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2672 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2673
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2674 $data = strtoupper($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2675 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2676 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2677
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2678 $index[$key] = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2679 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2680
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2681 if (!empty($index)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2682 // sort index
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2683 if ($flag == 'ASC') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2684 asort($index);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2685 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2686 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2687 arsort($index);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2688 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2689
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2690 // form new array based on index
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2691 foreach ($index as $key => $val) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2692 $result[$key] = $messages[$key];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2693 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2694 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2695
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2696 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2697 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2698
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2699 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2700 * Fetch MIME headers of specified message parts
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2701 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2702 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2703 * @param int $uid Message UID
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2704 * @param array $parts Message part identifiers
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2705 * @param bool $mime Use MIME instad of HEADER
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2706 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2707 * @return array|bool Array containing headers string for each specified body
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2708 * False on failure.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2709 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2710 public function fetchMIMEHeaders($mailbox, $uid, $parts, $mime = true)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2711 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2712 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2713 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2714 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2715
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2716 $result = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2717 $parts = (array) $parts;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2718 $key = $this->nextTag();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2719 $peeks = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2720 $type = $mime ? 'MIME' : 'HEADER';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2721
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2722 // format request
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2723 foreach ($parts as $part) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2724 $peeks[] = "BODY.PEEK[$part.$type]";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2725 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2726
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2727 $request = "$key UID FETCH $uid (" . implode(' ', $peeks) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2728
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2729 // send request
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2730 if (!$this->putLine($request)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2731 $this->setError(self::ERROR_COMMAND, "Failed to send UID FETCH command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2732 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2733 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2734
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2735 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2736 $line = $this->readLine(1024);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2737
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2738 if (preg_match('/^\* [0-9]+ FETCH [0-9UID( ]+/', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2739 $line = ltrim(substr($line, strlen($m[0])));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2740 while (preg_match('/^BODY\[([0-9\.]+)\.'.$type.'\]/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2741 $line = substr($line, strlen($matches[0]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2742 $result[$matches[1]] = trim($this->multLine($line));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2743 $line = $this->readLine(1024);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2744 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2745 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2746 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2747 while (!$this->startsWith($line, $key, true));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2748
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2749 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2750 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2751
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2752 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2753 * Fetches message part header
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2754 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2755 public function fetchPartHeader($mailbox, $id, $is_uid = false, $part = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2756 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2757 $part = empty($part) ? 'HEADER' : $part.'.MIME';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2758
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2759 return $this->handlePartBody($mailbox, $id, $is_uid, $part);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2760 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2761
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2762 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2763 * Fetches body of the specified message part
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2764 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2765 public function handlePartBody($mailbox, $id, $is_uid=false, $part='', $encoding=null, $print=null, $file=null, $formatted=false, $max_bytes=0)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2766 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2767 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2768 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2769 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2770
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2771 $binary = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2772
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2773 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2774 if (!$initiated) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2775 switch ($encoding) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2776 case 'base64':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2777 $mode = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2778 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2779 case 'quoted-printable':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2780 $mode = 2;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2781 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2782 case 'x-uuencode':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2783 case 'x-uue':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2784 case 'uue':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2785 case 'uuencode':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2786 $mode = 3;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2787 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2788 default:
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2789 $mode = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2790 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2791
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2792 // Use BINARY extension when possible (and safe)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2793 $binary = $binary && $mode && preg_match('/^[0-9.]+$/', $part) && $this->hasCapability('BINARY');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2794 $fetch_mode = $binary ? 'BINARY' : 'BODY';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2795 $partial = $max_bytes ? sprintf('<0.%d>', $max_bytes) : '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2796
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2797 // format request
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2798 $key = $this->nextTag();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2799 $cmd = ($is_uid ? 'UID ' : '') . 'FETCH';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2800 $request = "$key $cmd $id ($fetch_mode.PEEK[$part]$partial)";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2801 $result = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2802 $found = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2803 $initiated = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2804
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2805 // send request
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2806 if (!$this->putLine($request)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2807 $this->setError(self::ERROR_COMMAND, "Failed to send $cmd command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2808 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2809 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2810
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2811 if ($binary) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2812 // WARNING: Use $formatted argument with care, this may break binary data stream
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2813 $mode = -1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2814 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2815 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2816
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2817 $line = trim($this->readLine(1024));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2818
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2819 if (!$line) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2820 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2821 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2822
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2823 // handle UNKNOWN-CTE response - RFC 3516, try again with standard BODY request
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2824 if ($binary && !$found && preg_match('/^' . $key . ' NO \[UNKNOWN-CTE\]/i', $line)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2825 $binary = $initiated = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2826 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2827 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2828
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2829 // skip irrelevant untagged responses (we have a result already)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2830 if ($found || !preg_match('/^\* ([0-9]+) FETCH (.*)$/', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2831 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2832 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2833
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2834 $line = $m[2];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2835
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2836 // handle one line response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2837 if ($line[0] == '(' && substr($line, -1) == ')') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2838 // tokenize content inside brackets
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2839 // the content can be e.g.: (UID 9844 BODY[2.4] NIL)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2840 $tokens = $this->tokenizeResponse(preg_replace('/(^\(|\)$)/', '', $line));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2841
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2842 for ($i=0; $i<count($tokens); $i+=2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2843 if (preg_match('/^(BODY|BINARY)/i', $tokens[$i])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2844 $result = $tokens[$i+1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2845 $found = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2846 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2847 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2848 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2849
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2850 if ($result !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2851 if ($mode == 1) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2852 $result = base64_decode($result);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2853 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2854 else if ($mode == 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2855 $result = quoted_printable_decode($result);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2856 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2857 else if ($mode == 3) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2858 $result = convert_uudecode($result);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2859 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2860 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2861 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2862 // response with string literal
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2863 else if (preg_match('/\{([0-9]+)\}$/', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2864 $bytes = (int) $m[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2865 $prev = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2866 $found = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2867
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2868 // empty body
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2869 if (!$bytes) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2870 $result = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2871 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2872 else while ($bytes > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2873 $line = $this->readLine(8192);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2874
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2875 if ($line === null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2876 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2877 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2878
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2879 $len = strlen($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2880
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2881 if ($len > $bytes) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2882 $line = substr($line, 0, $bytes);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2883 $len = strlen($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2884 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2885 $bytes -= $len;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2886
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2887 // BASE64
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2888 if ($mode == 1) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2889 $line = preg_replace('|[^a-zA-Z0-9+=/]|', '', $line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2890 // create chunks with proper length for base64 decoding
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2891 $line = $prev.$line;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2892 $length = strlen($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2893 if ($length % 4) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2894 $length = floor($length / 4) * 4;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2895 $prev = substr($line, $length);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2896 $line = substr($line, 0, $length);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2897 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2898 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2899 $prev = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2900 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2901 $line = base64_decode($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2902 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2903 // QUOTED-PRINTABLE
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2904 else if ($mode == 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2905 $line = rtrim($line, "\t\r\0\x0B");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2906 $line = quoted_printable_decode($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2907 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2908 // UUENCODE
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2909 else if ($mode == 3) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2910 $line = rtrim($line, "\t\r\n\0\x0B");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2911 if ($line == 'end' || preg_match('/^begin\s+[0-7]+\s+.+$/', $line)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2912 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2913 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2914 $line = convert_uudecode($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2915 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2916 // default
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2917 else if ($formatted) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2918 $line = rtrim($line, "\t\r\n\0\x0B") . "\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2919 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2920
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2921 if ($file) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2922 if (fwrite($file, $line) === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2923 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2924 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2925 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2926 else if ($print) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2927 echo $line;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2928 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2929 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2930 $result .= $line;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2931 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2932 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2933 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2934 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2935 while (!$this->startsWith($line, $key, true) || !$initiated);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2936
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2937 if ($result !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2938 if ($file) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2939 return fwrite($file, $result);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2940 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2941 else if ($print) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2942 echo $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2943 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2944 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2945
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2946 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2947 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2948
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2949 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2950 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2951
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2952 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2953 * Handler for IMAP APPEND command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2954 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2955 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2956 * @param string|array $message The message source string or array (of strings and file pointers)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2957 * @param array $flags Message flags
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2958 * @param string $date Message internal date
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2959 * @param bool $binary Enable BINARY append (RFC3516)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2960 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2961 * @return string|bool On success APPENDUID response (if available) or True, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2962 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2963 public function append($mailbox, &$message, $flags = array(), $date = null, $binary = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2964 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2965 unset($this->data['APPENDUID']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2966
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2967 if ($mailbox === null || $mailbox === '') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2968 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2969 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2970
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2971 $binary = $binary && $this->getCapability('BINARY');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2972 $literal_plus = !$binary && $this->prefs['literal+'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2973 $len = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2974 $msg = is_array($message) ? $message : array(&$message);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2975 $chunk_size = 512000;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2976
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2977 for ($i=0, $cnt=count($msg); $i<$cnt; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2978 if (is_resource($msg[$i])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2979 $stat = fstat($msg[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2980 if ($stat === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2981 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2982 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2983 $len += $stat['size'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2984 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2985 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2986 if (!$binary) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2987 $msg[$i] = str_replace("\r", '', $msg[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2988 $msg[$i] = str_replace("\n", "\r\n", $msg[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2989 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2990
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2991 $len += strlen($msg[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2992 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2993 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2994
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2995 if (!$len) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2996 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2997 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2998
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2999 // build APPEND command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3000 $key = $this->nextTag();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3001 $request = "$key APPEND " . $this->escape($mailbox) . ' (' . $this->flagsToStr($flags) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3002 if (!empty($date)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3003 $request .= ' ' . $this->escape($date);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3004 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3005 $request .= ' ' . ($binary ? '~' : '') . '{' . $len . ($literal_plus ? '+' : '') . '}';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3006
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3007 // send APPEND command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3008 if (!$this->putLine($request)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3009 $this->setError(self::ERROR_COMMAND, "Failed to send APPEND command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3010 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3011 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3012
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3013 // Do not wait when LITERAL+ is supported
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3014 if (!$literal_plus) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3015 $line = $this->readReply();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3016
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3017 if ($line[0] != '+') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3018 $this->parseResult($line, 'APPEND: ');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3019 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3020 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3021 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3022
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3023 foreach ($msg as $msg_part) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3024 // file pointer
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3025 if (is_resource($msg_part)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3026 rewind($msg_part);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3027 while (!feof($msg_part) && $this->fp) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3028 $buffer = fread($msg_part, $chunk_size);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3029 $this->putLine($buffer, false);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3030 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3031 fclose($msg_part);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3032 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3033 // string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3034 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3035 $size = strlen($msg_part);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3036
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3037 // Break up the data by sending one chunk (up to 512k) at a time.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3038 // This approach reduces our peak memory usage
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3039 for ($offset = 0; $offset < $size; $offset += $chunk_size) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3040 $chunk = substr($msg_part, $offset, $chunk_size);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3041 if (!$this->putLine($chunk, false)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3042 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3043 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3044 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3045 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3046 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3047
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3048 if (!$this->putLine('')) { // \r\n
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3049 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3050 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3051
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3052 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3053 $line = $this->readLine();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3054 } while (!$this->startsWith($line, $key, true, true));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3055
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3056 // Clear internal status cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3057 unset($this->data['STATUS:'.$mailbox]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3058
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3059 if ($this->parseResult($line, 'APPEND: ') != self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3060 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3061 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3062
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3063 if (!empty($this->data['APPENDUID'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3064 return $this->data['APPENDUID'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3065 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3066
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3067 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3068 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3069
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3070 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3071 * Handler for IMAP APPEND command.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3072 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3073 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3074 * @param string $path Path to the file with message body
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3075 * @param string $headers Message headers
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3076 * @param array $flags Message flags
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3077 * @param string $date Message internal date
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3078 * @param bool $binary Enable BINARY append (RFC3516)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3079 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3080 * @return string|bool On success APPENDUID response (if available) or True, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3081 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3082 public function appendFromFile($mailbox, $path, $headers=null, $flags = array(), $date = null, $binary = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3083 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3084 // open message file
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3085 if (file_exists(realpath($path))) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3086 $fp = fopen($path, 'r');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3087 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3088
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3089 if (!$fp) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3090 $this->setError(self::ERROR_UNKNOWN, "Couldn't open $path for reading");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3091 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3092 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3093
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3094 $message = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3095 if ($headers) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3096 $message[] = trim($headers, "\r\n") . "\r\n\r\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3097 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3098 $message[] = $fp;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3099
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3100 return $this->append($mailbox, $message, $flags, $date, $binary);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3101 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3102
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3103 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3104 * Returns QUOTA information
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3105 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3106 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3107 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3108 * @return array Quota information
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3109 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3110 public function getQuota($mailbox = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3111 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3112 if ($mailbox === null || $mailbox === '') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3113 $mailbox = 'INBOX';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3114 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3115
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3116 // a0001 GETQUOTAROOT INBOX
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3117 // * QUOTAROOT INBOX user/sample
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3118 // * QUOTA user/sample (STORAGE 654 9765)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3119 // a0001 OK Completed
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3120
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3121 list($code, $response) = $this->execute('GETQUOTAROOT', array($this->escape($mailbox)));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3122
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3123 $result = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3124 $min_free = PHP_INT_MAX;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3125 $all = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3126
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3127 if ($code == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3128 foreach (explode("\n", $response) as $line) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3129 if (preg_match('/^\* QUOTA /', $line)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3130 list(, , $quota_root) = $this->tokenizeResponse($line, 3);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3131
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3132 while ($line) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3133 list($type, $used, $total) = $this->tokenizeResponse($line, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3134 $type = strtolower($type);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3135
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3136 if ($type && $total) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3137 $all[$quota_root][$type]['used'] = intval($used);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3138 $all[$quota_root][$type]['total'] = intval($total);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3139 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3140 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3141
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3142 if (empty($all[$quota_root]['storage'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3143 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3144 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3145
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3146 $used = $all[$quota_root]['storage']['used'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3147 $total = $all[$quota_root]['storage']['total'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3148 $free = $total - $used;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3149
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3150 // calculate lowest available space from all storage quotas
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3151 if ($free < $min_free) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3152 $min_free = $free;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3153 $result['used'] = $used;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3154 $result['total'] = $total;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3155 $result['percent'] = min(100, round(($used/max(1,$total))*100));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3156 $result['free'] = 100 - $result['percent'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3157 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3158 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3159 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3160 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3161
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3162 if (!empty($result)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3163 $result['all'] = $all;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3164 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3165
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3166 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3167 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3168
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3169 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3170 * Send the SETACL command (RFC4314)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3171 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3172 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3173 * @param string $user User name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3174 * @param mixed $acl ACL string or array
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3175 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3176 * @return boolean True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3177 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3178 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3179 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3180 public function setACL($mailbox, $user, $acl)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3181 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3182 if (is_array($acl)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3183 $acl = implode('', $acl);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3184 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3185
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3186 $result = $this->execute('SETACL', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3187 $this->escape($mailbox), $this->escape($user), strtolower($acl)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3188 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3189
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3190 return ($result == self::ERROR_OK);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3191 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3192
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3193 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3194 * Send the DELETEACL command (RFC4314)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3195 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3196 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3197 * @param string $user User name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3198 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3199 * @return boolean True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3200 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3201 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3202 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3203 public function deleteACL($mailbox, $user)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3204 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3205 $result = $this->execute('DELETEACL', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3206 $this->escape($mailbox), $this->escape($user)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3207 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3208
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3209 return ($result == self::ERROR_OK);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3210 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3211
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3212 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3213 * Send the GETACL command (RFC4314)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3214 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3215 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3216 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3217 * @return array User-rights array on success, NULL on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3218 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3219 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3220 public function getACL($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3221 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3222 list($code, $response) = $this->execute('GETACL', array($this->escape($mailbox)));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3223
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3224 if ($code == self::ERROR_OK && preg_match('/^\* ACL /i', $response)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3225 // Parse server response (remove "* ACL ")
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3226 $response = substr($response, 6);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3227 $ret = $this->tokenizeResponse($response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3228 $mbox = array_shift($ret);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3229 $size = count($ret);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3230
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3231 // Create user-rights hash array
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3232 // @TODO: consider implementing fixACL() method according to RFC4314.2.1.1
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3233 // so we could return only standard rights defined in RFC4314,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3234 // excluding 'c' and 'd' defined in RFC2086.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3235 if ($size % 2 == 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3236 for ($i=0; $i<$size; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3237 $ret[$ret[$i]] = str_split($ret[++$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3238 unset($ret[$i-1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3239 unset($ret[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3240 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3241 return $ret;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3242 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3243
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3244 $this->setError(self::ERROR_COMMAND, "Incomplete ACL response");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3245 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3246 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3247
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3248 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3249 * Send the LISTRIGHTS command (RFC4314)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3250 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3251 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3252 * @param string $user User name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3253 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3254 * @return array List of user rights
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3255 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3256 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3257 public function listRights($mailbox, $user)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3258 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3259 list($code, $response) = $this->execute('LISTRIGHTS', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3260 $this->escape($mailbox), $this->escape($user)));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3261
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3262 if ($code == self::ERROR_OK && preg_match('/^\* LISTRIGHTS /i', $response)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3263 // Parse server response (remove "* LISTRIGHTS ")
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3264 $response = substr($response, 13);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3265
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3266 $ret_mbox = $this->tokenizeResponse($response, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3267 $ret_user = $this->tokenizeResponse($response, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3268 $granted = $this->tokenizeResponse($response, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3269 $optional = trim($response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3270
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3271 return array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3272 'granted' => str_split($granted),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3273 'optional' => explode(' ', $optional),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3274 );
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3275 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3276 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3277
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3278 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3279 * Send the MYRIGHTS command (RFC4314)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3280 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3281 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3282 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3283 * @return array MYRIGHTS response on success, NULL on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3284 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3285 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3286 public function myRights($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3287 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3288 list($code, $response) = $this->execute('MYRIGHTS', array($this->escape($mailbox)));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3289
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3290 if ($code == self::ERROR_OK && preg_match('/^\* MYRIGHTS /i', $response)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3291 // Parse server response (remove "* MYRIGHTS ")
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3292 $response = substr($response, 11);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3293
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3294 $ret_mbox = $this->tokenizeResponse($response, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3295 $rights = $this->tokenizeResponse($response, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3296
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3297 return str_split($rights);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3298 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3299 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3300
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3301 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3302 * Send the SETMETADATA command (RFC5464)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3303 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3304 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3305 * @param array $entries Entry-value array (use NULL value as NIL)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3306 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3307 * @return boolean True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3308 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3309 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3310 public function setMetadata($mailbox, $entries)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3311 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3312 if (!is_array($entries) || empty($entries)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3313 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETMETADATA command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3314 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3315 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3316
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3317 foreach ($entries as $name => $value) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3318 $entries[$name] = $this->escape($name) . ' ' . $this->escape($value, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3319 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3320
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3321 $entries = implode(' ', $entries);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3322 $result = $this->execute('SETMETADATA', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3323 $this->escape($mailbox), '(' . $entries . ')'),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3324 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3325
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3326 return ($result == self::ERROR_OK);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3327 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3328
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3329 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3330 * Send the SETMETADATA command with NIL values (RFC5464)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3331 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3332 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3333 * @param array $entries Entry names array
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3334 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3335 * @return boolean True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3336 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3337 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3338 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3339 public function deleteMetadata($mailbox, $entries)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3340 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3341 if (!is_array($entries) && !empty($entries)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3342 $entries = explode(' ', $entries);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3343 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3344
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3345 if (empty($entries)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3346 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETMETADATA command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3347 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3348 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3349
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3350 foreach ($entries as $entry) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3351 $data[$entry] = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3352 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3353
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3354 return $this->setMetadata($mailbox, $data);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3355 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3356
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3357 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3358 * Send the GETMETADATA command (RFC5464)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3359 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3360 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3361 * @param array $entries Entries
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3362 * @param array $options Command options (with MAXSIZE and DEPTH keys)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3363 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3364 * @return array GETMETADATA result on success, NULL on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3365 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3366 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3367 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3368 public function getMetadata($mailbox, $entries, $options=array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3369 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3370 if (!is_array($entries)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3371 $entries = array($entries);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3372 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3373
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3374 // create entries string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3375 foreach ($entries as $idx => $name) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3376 $entries[$idx] = $this->escape($name);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3377 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3378
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3379 $optlist = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3380 $entlist = '(' . implode(' ', $entries) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3381
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3382 // create options string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3383 if (is_array($options)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3384 $options = array_change_key_case($options, CASE_UPPER);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3385 $opts = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3386
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3387 if (!empty($options['MAXSIZE'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3388 $opts[] = 'MAXSIZE '.intval($options['MAXSIZE']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3389 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3390 if (!empty($options['DEPTH'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3391 $opts[] = 'DEPTH '.intval($options['DEPTH']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3392 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3393
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3394 if ($opts) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3395 $optlist = '(' . implode(' ', $opts) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3396 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3397 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3398
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3399 $optlist .= ($optlist ? ' ' : '') . $entlist;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3400
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3401 list($code, $response) = $this->execute('GETMETADATA', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3402 $this->escape($mailbox), $optlist));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3403
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3404 if ($code == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3405 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3406 $data = $this->tokenizeResponse($response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3407
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3408 // The METADATA response can contain multiple entries in a single
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3409 // response or multiple responses for each entry or group of entries
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3410 if (!empty($data) && ($size = count($data))) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3411 for ($i=0; $i<$size; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3412 if (isset($mbox) && is_array($data[$i])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3413 $size_sub = count($data[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3414 for ($x=0; $x<$size_sub; $x+=2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3415 if ($data[$i][$x+1] !== null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3416 $result[$mbox][$data[$i][$x]] = $data[$i][$x+1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3417 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3418 unset($data[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3419 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3420 else if ($data[$i] == '*') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3421 if ($data[$i+1] == 'METADATA') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3422 $mbox = $data[$i+2];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3423 unset($data[$i]); // "*"
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3424 unset($data[++$i]); // "METADATA"
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3425 unset($data[++$i]); // Mailbox
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3426 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3427 // get rid of other untagged responses
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3428 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3429 unset($mbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3430 unset($data[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3431 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3432 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3433 else if (isset($mbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3434 if ($data[++$i] !== null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3435 $result[$mbox][$data[$i-1]] = $data[$i];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3436 unset($data[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3437 unset($data[$i-1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3438 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3439 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3440 unset($data[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3441 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3442 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3443 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3444
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3445 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3446 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3447 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3448
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3449 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3450 * Send the SETANNOTATION command (draft-daboo-imap-annotatemore)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3451 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3452 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3453 * @param array $data Data array where each item is an array with
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3454 * three elements: entry name, attribute name, value
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3455 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3456 * @return boolean True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3457 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3458 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3459 public function setAnnotation($mailbox, $data)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3460 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3461 if (!is_array($data) || empty($data)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3462 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETANNOTATION command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3463 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3464 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3465
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3466 foreach ($data as $entry) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3467 // ANNOTATEMORE drafts before version 08 require quoted parameters
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3468 $entries[] = sprintf('%s (%s %s)', $this->escape($entry[0], true),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3469 $this->escape($entry[1], true), $this->escape($entry[2], true));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3470 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3471
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3472 $entries = implode(' ', $entries);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3473 $result = $this->execute('SETANNOTATION', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3474 $this->escape($mailbox), $entries), self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3475
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3476 return ($result == self::ERROR_OK);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3477 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3478
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3479 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3480 * Send the SETANNOTATION command with NIL values (draft-daboo-imap-annotatemore)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3481 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3482 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3483 * @param array $data Data array where each item is an array with
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3484 * two elements: entry name and attribute name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3485 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3486 * @return boolean True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3487 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3488 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3489 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3490 public function deleteAnnotation($mailbox, $data)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3491 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3492 if (!is_array($data) || empty($data)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3493 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETANNOTATION command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3494 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3495 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3496
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3497 return $this->setAnnotation($mailbox, $data);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3498 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3499
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3500 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3501 * Send the GETANNOTATION command (draft-daboo-imap-annotatemore)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3502 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3503 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3504 * @param array $entries Entries names
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3505 * @param array $attribs Attribs names
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3506 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3507 * @return array Annotations result on success, NULL on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3508 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3509 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3510 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3511 public function getAnnotation($mailbox, $entries, $attribs)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3512 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3513 if (!is_array($entries)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3514 $entries = array($entries);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3515 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3516
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3517 // create entries string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3518 // ANNOTATEMORE drafts before version 08 require quoted parameters
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3519 foreach ($entries as $idx => $name) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3520 $entries[$idx] = $this->escape($name, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3521 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3522 $entries = '(' . implode(' ', $entries) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3523
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3524 if (!is_array($attribs)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3525 $attribs = array($attribs);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3526 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3527
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3528 // create attributes string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3529 foreach ($attribs as $idx => $name) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3530 $attribs[$idx] = $this->escape($name, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3531 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3532 $attribs = '(' . implode(' ', $attribs) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3533
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3534 list($code, $response) = $this->execute('GETANNOTATION', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3535 $this->escape($mailbox), $entries, $attribs));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3536
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3537 if ($code == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3538 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3539 $data = $this->tokenizeResponse($response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3540
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3541 // Here we returns only data compatible with METADATA result format
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3542 if (!empty($data) && ($size = count($data))) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3543 for ($i=0; $i<$size; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3544 $entry = $data[$i];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3545 if (isset($mbox) && is_array($entry)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3546 $attribs = $entry;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3547 $entry = $last_entry;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3548 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3549 else if ($entry == '*') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3550 if ($data[$i+1] == 'ANNOTATION') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3551 $mbox = $data[$i+2];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3552 unset($data[$i]); // "*"
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3553 unset($data[++$i]); // "ANNOTATION"
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3554 unset($data[++$i]); // Mailbox
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3555 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3556 // get rid of other untagged responses
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3557 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3558 unset($mbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3559 unset($data[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3560 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3561 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3562 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3563 else if (isset($mbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3564 $attribs = $data[++$i];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3565 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3566 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3567 unset($data[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3568 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3569 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3570
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3571 if (!empty($attribs)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3572 for ($x=0, $len=count($attribs); $x<$len;) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3573 $attr = $attribs[$x++];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3574 $value = $attribs[$x++];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3575 if ($attr == 'value.priv' && $value !== null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3576 $result[$mbox]['/private' . $entry] = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3577 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3578 else if ($attr == 'value.shared' && $value !== null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3579 $result[$mbox]['/shared' . $entry] = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3580 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3581 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3582 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3583 $last_entry = $entry;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3584 unset($data[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3585 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3586 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3587
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3588 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3589 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3590 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3591
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3592 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3593 * Returns BODYSTRUCTURE for the specified message.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3594 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3595 * @param string $mailbox Folder name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3596 * @param int $id Message sequence number or UID
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3597 * @param bool $is_uid True if $id is an UID
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3598 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3599 * @return array/bool Body structure array or False on error.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3600 * @since 0.6
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3601 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3602 public function getStructure($mailbox, $id, $is_uid = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3603 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3604 $result = $this->fetch($mailbox, $id, $is_uid, array('BODYSTRUCTURE'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3605
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3606 if (is_array($result)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3607 $result = array_shift($result);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3608 return $result->bodystructure;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3609 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3610
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3611 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3612 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3613
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3614 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3615 * Returns data of a message part according to specified structure.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3616 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3617 * @param array $structure Message structure (getStructure() result)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3618 * @param string $part Message part identifier
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3619 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3620 * @return array Part data as hash array (type, encoding, charset, size)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3621 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3622 public static function getStructurePartData($structure, $part)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3623 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3624 $part_a = self::getStructurePartArray($structure, $part);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3625 $data = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3626
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3627 if (empty($part_a)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3628 return $data;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3629 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3630
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3631 // content-type
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3632 if (is_array($part_a[0])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3633 $data['type'] = 'multipart';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3634 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3635 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3636 $data['type'] = strtolower($part_a[0]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3637 $data['encoding'] = strtolower($part_a[5]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3638
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3639 // charset
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3640 if (is_array($part_a[2])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3641 foreach ($part_a[2] as $key => $val) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3642 if (strcasecmp($val, 'charset') == 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3643 $data['charset'] = $part_a[2][$key+1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3644 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3645 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3646 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3647 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3648 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3649
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3650 // size
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3651 $data['size'] = intval($part_a[6]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3652
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3653 return $data;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3654 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3655
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3656 public static function getStructurePartArray($a, $part)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3657 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3658 if (!is_array($a)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3659 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3660 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3661
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3662 if (empty($part)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3663 return $a;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3664 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3665
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3666 $ctype = is_string($a[0]) && is_string($a[1]) ? $a[0] . '/' . $a[1] : '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3667
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3668 if (strcasecmp($ctype, 'message/rfc822') == 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3669 $a = $a[8];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3670 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3671
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3672 if (strpos($part, '.') > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3673 $orig_part = $part;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3674 $pos = strpos($part, '.');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3675 $rest = substr($orig_part, $pos+1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3676 $part = substr($orig_part, 0, $pos);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3677
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3678 return self::getStructurePartArray($a[$part-1], $rest);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3679 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3680 else if ($part > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3681 return (is_array($a[$part-1])) ? $a[$part-1] : $a;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3682 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3683 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3684
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3685 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3686 * Creates next command identifier (tag)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3687 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3688 * @return string Command identifier
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3689 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3690 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3691 public function nextTag()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3692 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3693 $this->cmd_num++;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3694 $this->cmd_tag = sprintf('A%04d', $this->cmd_num);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3695
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3696 return $this->cmd_tag;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3697 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3698
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3699 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3700 * Sends IMAP command and parses result
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3701 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3702 * @param string $command IMAP command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3703 * @param array $arguments Command arguments
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3704 * @param int $options Execution options
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3705 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3706 * @return mixed Response code or list of response code and data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3707 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3708 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3709 public function execute($command, $arguments=array(), $options=0)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3710 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3711 $tag = $this->nextTag();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3712 $query = $tag . ' ' . $command;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3713 $noresp = ($options & self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3714 $response = $noresp ? null : '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3715
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3716 if (!empty($arguments)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3717 foreach ($arguments as $arg) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3718 $query .= ' ' . self::r_implode($arg);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3719 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3720 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3721
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3722 // Send command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3723 if (!$this->putLineC($query, true, ($options & self::COMMAND_ANONYMIZED))) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3724 preg_match('/^[A-Z0-9]+ ((UID )?[A-Z]+)/', $query, $matches);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3725 $cmd = $matches[1] ?: 'UNKNOWN';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3726 $this->setError(self::ERROR_COMMAND, "Failed to send $cmd command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3727
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3728 return $noresp ? self::ERROR_COMMAND : array(self::ERROR_COMMAND, '');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3729 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3730
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3731 // Parse response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3732 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3733 $line = $this->readLine(4096);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3734
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3735 if ($response !== null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3736 $response .= $line;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3737 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3738
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3739 // parse untagged response for [COPYUID 1204196876 3456:3457 123:124] (RFC6851)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3740 if ($line && $command == 'UID MOVE' && substr_compare($line, '* OK', 0, 4, true)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3741 if (preg_match("/^\* OK \[COPYUID [0-9]+ ([0-9,:]+) ([0-9,:]+)\]/i", $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3742 $this->data['COPYUID'] = array($m[1], $m[2]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3743 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3744 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3745 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3746 while (!$this->startsWith($line, $tag . ' ', true, true));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3747
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3748 $code = $this->parseResult($line, $command . ': ');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3749
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3750 // Remove last line from response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3751 if ($response) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3752 $line_len = min(strlen($response), strlen($line) + 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3753 $response = substr($response, 0, -$line_len);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3754 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3755
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3756 // optional CAPABILITY response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3757 if (($options & self::COMMAND_CAPABILITY) && $code == self::ERROR_OK
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3758 && preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3759 ) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3760 $this->parseCapability($matches[1], true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3761 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3762
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3763 // return last line only (without command tag, result and response code)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3764 if ($line && ($options & self::COMMAND_LASTLINE)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3765 $response = preg_replace("/^$tag (OK|NO|BAD|BYE|PREAUTH)?\s*(\[[a-z-]+\])?\s*/i", '', trim($line));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3766 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3767
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3768 return $noresp ? $code : array($code, $response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3769 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3770
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3771 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3772 * Splits IMAP response into string tokens
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3773 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3774 * @param string &$str The IMAP's server response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3775 * @param int $num Number of tokens to return
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3776 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3777 * @return mixed Tokens array or string if $num=1
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3778 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3779 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3780 public static function tokenizeResponse(&$str, $num=0)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3781 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3782 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3783
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3784 while (!$num || count($result) < $num) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3785 // remove spaces from the beginning of the string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3786 $str = ltrim($str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3787
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3788 switch ($str[0]) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3789
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3790 // String literal
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3791 case '{':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3792 if (($epos = strpos($str, "}\r\n", 1)) == false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3793 // error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3794 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3795 if (!is_numeric(($bytes = substr($str, 1, $epos - 1)))) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3796 // error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3797 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3798
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3799 $result[] = $bytes ? substr($str, $epos + 3, $bytes) : '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3800 $str = substr($str, $epos + 3 + $bytes);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3801 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3802
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3803 // Quoted string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3804 case '"':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3805 $len = strlen($str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3806
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3807 for ($pos=1; $pos<$len; $pos++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3808 if ($str[$pos] == '"') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3809 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3810 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3811 if ($str[$pos] == "\\") {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3812 if ($str[$pos + 1] == '"' || $str[$pos + 1] == "\\") {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3813 $pos++;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3814 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3815 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3816 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3817
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3818 // we need to strip slashes for a quoted string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3819 $result[] = stripslashes(substr($str, 1, $pos - 1));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3820 $str = substr($str, $pos + 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3821 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3822
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3823 // Parenthesized list
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3824 case '(':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3825 $str = substr($str, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3826 $result[] = self::tokenizeResponse($str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3827 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3828
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3829 case ')':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3830 $str = substr($str, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3831 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3832
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3833 // String atom, number, astring, NIL, *, %
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3834 default:
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3835 // empty string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3836 if ($str === '' || $str === null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3837 break 2;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3838 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3839
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3840 // excluded chars: SP, CTL, ), DEL
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3841 // we do not exclude [ and ] (#1489223)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3842 if (preg_match('/^([^\x00-\x20\x29\x7F]+)/', $str, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3843 $result[] = $m[1] == 'NIL' ? null : $m[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3844 $str = substr($str, strlen($m[1]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3845 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3846 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3847 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3848 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3849
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3850 return $num == 1 ? $result[0] : $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3851 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3852
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3853 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3854 * Joins IMAP command line elements (recursively)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3855 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3856 protected static function r_implode($element)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3857 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3858 $string = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3859
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3860 if (is_array($element)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3861 reset($element);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3862 foreach ($element as $value) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3863 $string .= ' ' . self::r_implode($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3864 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3865 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3866 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3867 return $element;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3868 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3869
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3870 return '(' . trim($string) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3871 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3872
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3873 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3874 * Converts message identifiers array into sequence-set syntax
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3875 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3876 * @param array $messages Message identifiers
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3877 * @param bool $force Forces compression of any size
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3878 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3879 * @return string Compressed sequence-set
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3880 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3881 public static function compressMessageSet($messages, $force=false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3882 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3883 // given a comma delimited list of independent mid's,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3884 // compresses by grouping sequences together
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3885
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3886 if (!is_array($messages)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3887 // if less than 255 bytes long, let's not bother
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3888 if (!$force && strlen($messages)<255) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3889 return $messages;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3890 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3891
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3892 // see if it's already been compressed
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3893 if (strpos($messages, ':') !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3894 return $messages;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3895 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3896
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3897 // separate, then sort
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3898 $messages = explode(',', $messages);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3899 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3900
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3901 sort($messages);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3902
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3903 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3904 $start = $prev = $messages[0];
12
5039cc34571f some old, mostly new 7.3->8.4 changes needed
Charlie Root
parents: 11
diff changeset
3905 $needStrip = (! is_numeric($start)) || (! is_numeric($prev));
0
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3906
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3907 foreach ($messages as $id) {
11
aff04b06b685 various small fixes from upgrades to PHP and/or hangover from fix to apt-get overwrite at beginning of the year somehow
Charlie Root
parents: 5
diff changeset
3908 #Advanced search calls with pseudo-message-ids? Non-numeric, anyway, e.g.
aff04b06b685 various small fixes from upgrades to PHP and/or hangover from fix to apt-get overwrite at beginning of the year somehow
Charlie Root
parents: 5
diff changeset
3909 # 420__MB__97ce7451bd364b47894f71ba7eb8ceb1
aff04b06b685 various small fixes from upgrades to PHP and/or hangover from fix to apt-get overwrite at beginning of the year somehow
Charlie Root
parents: 5
diff changeset
3910 if ($needStrip) {
12
5039cc34571f some old, mostly new 7.3->8.4 changes needed
Charlie Root
parents: 11
diff changeset
3911 set_error_handler(function($e1, $e2) {
14
abddb304201f continue slowly cleaning up after move to 8.3
Charlie Root
parents: 12
diff changeset
3912 rcube::write_log('mail',
12
5039cc34571f some old, mostly new 7.3->8.4 changes needed
Charlie Root
parents: 11
diff changeset
3913 "non-num? id: |$id|, prev: |$prev|");
5039cc34571f some old, mostly new 7.3->8.4 changes needed
Charlie Root
parents: 11
diff changeset
3914 },
14
abddb304201f continue slowly cleaning up after move to 8.3
Charlie Root
parents: 12
diff changeset
3915 E_ALL);
abddb304201f continue slowly cleaning up after move to 8.3
Charlie Root
parents: 12
diff changeset
3916 $incr = intval(substr($id,0,strpos($id,'_'))) - intval(substr($prev,0,strpos($prev,'_')));
12
5039cc34571f some old, mostly new 7.3->8.4 changes needed
Charlie Root
parents: 11
diff changeset
3917 restore_error_handler();
11
aff04b06b685 various small fixes from upgrades to PHP and/or hangover from fix to apt-get overwrite at beginning of the year somehow
Charlie Root
parents: 5
diff changeset
3918 }
aff04b06b685 various small fixes from upgrades to PHP and/or hangover from fix to apt-get overwrite at beginning of the year somehow
Charlie Root
parents: 5
diff changeset
3919 else {
aff04b06b685 various small fixes from upgrades to PHP and/or hangover from fix to apt-get overwrite at beginning of the year somehow
Charlie Root
parents: 5
diff changeset
3920 $incr = $id - $prev;
aff04b06b685 various small fixes from upgrades to PHP and/or hangover from fix to apt-get overwrite at beginning of the year somehow
Charlie Root
parents: 5
diff changeset
3921 }
0
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3922 if ($incr > 1) { // found a gap
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3923 if ($start == $prev) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3924 $result[] = $prev; // push single id
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3925 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3926 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3927 $result[] = $start . ':' . $prev; // push sequence as start_id:end_id
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3928 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3929 $start = $id; // start of new sequence
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3930 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3931 $prev = $id;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3932 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3933
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3934 // handle the last sequence/id
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3935 if ($start == $prev) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3936 $result[] = $prev;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3937 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3938 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3939 $result[] = $start.':'.$prev;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3940 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3941
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3942 // return as comma separated string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3943 return implode(',', $result);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3944 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3945
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3946 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3947 * Converts message sequence-set into array
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3948 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3949 * @param string $messages Message identifiers
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3950 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3951 * @return array List of message identifiers
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3952 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3953 public static function uncompressMessageSet($messages)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3954 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3955 if (empty($messages)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3956 return array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3957 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3958
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3959 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3960 $messages = explode(',', $messages);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3961
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3962 foreach ($messages as $idx => $part) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3963 $items = explode(':', $part);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3964 $max = max($items[0], $items[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3965
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3966 for ($x=$items[0]; $x<=$max; $x++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3967 $result[] = (int)$x;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3968 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3969 unset($messages[$idx]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3970 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3971
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3972 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3973 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3974
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3975 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3976 * Clear internal status cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3977 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3978 protected function clear_status_cache($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3979 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3980 unset($this->data['STATUS:' . $mailbox]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3981
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3982 $keys = array('EXISTS', 'RECENT', 'UNSEEN', 'UID-MAP');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3983
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3984 foreach ($keys as $key) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3985 unset($this->data[$key]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3986 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3987 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3988
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3989 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3990 * Clear internal cache of the current mailbox
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3991 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3992 protected function clear_mailbox_cache()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3993 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3994 $this->clear_status_cache($this->selected);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3995
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3996 $keys = array('UIDNEXT', 'UIDVALIDITY', 'HIGHESTMODSEQ', 'NOMODSEQ',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3997 'PERMANENTFLAGS', 'QRESYNC', 'VANISHED', 'READ-WRITE');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3998
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3999 foreach ($keys as $key) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4000 unset($this->data[$key]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4001 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4002 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4003
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4004 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4005 * Converts flags array into string for inclusion in IMAP command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4006 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4007 * @param array $flags Flags (see self::flags)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4008 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4009 * @return string Space-separated list of flags
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4010 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4011 protected function flagsToStr($flags)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4012 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4013 foreach ((array)$flags as $idx => $flag) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4014 if ($flag = $this->flags[strtoupper($flag)]) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4015 $flags[$idx] = $flag;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4016 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4017 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4018
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4019 return implode(' ', (array)$flags);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4020 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4021
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4022 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4023 * CAPABILITY response parser
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4024 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4025 protected function parseCapability($str, $trusted=false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4026 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4027 $str = preg_replace('/^\* CAPABILITY /i', '', $str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4028
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4029 $this->capability = explode(' ', strtoupper($str));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4030
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4031 if (!empty($this->prefs['disabled_caps'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4032 $this->capability = array_diff($this->capability, $this->prefs['disabled_caps']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4033 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4034
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4035 if (!isset($this->prefs['literal+']) && in_array('LITERAL+', $this->capability)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4036 $this->prefs['literal+'] = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4037 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4038
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4039 if ($trusted) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4040 $this->capability_readed = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4041 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4042 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4043
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4044 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4045 * Escapes a string when it contains special characters (RFC3501)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4046 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4047 * @param string $string IMAP string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4048 * @param boolean $force_quotes Forces string quoting (for atoms)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4049 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4050 * @return string String atom, quoted-string or string literal
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4051 * @todo lists
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4052 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4053 public static function escape($string, $force_quotes=false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4054 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4055 if ($string === null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4056 return 'NIL';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4057 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4058
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4059 if ($string === '') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4060 return '""';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4061 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4062
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4063 // atom-string (only safe characters)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4064 if (!$force_quotes && !preg_match('/[\x00-\x20\x22\x25\x28-\x2A\x5B-\x5D\x7B\x7D\x80-\xFF]/', $string)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4065 return $string;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4066 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4067
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4068 // quoted-string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4069 if (!preg_match('/[\r\n\x00\x80-\xFF]/', $string)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4070 return '"' . addcslashes($string, '\\"') . '"';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4071 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4072
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4073 // literal-string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4074 return sprintf("{%d}\r\n%s", strlen($string), $string);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4075 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4076
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4077 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4078 * Set the value of the debugging flag.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4079 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4080 * @param boolean $debug New value for the debugging flag.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4081 * @param callback $handler Logging handler function
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4082 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4083 * @since 0.5-stable
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4084 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4085 public function setDebug($debug, $handler = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4086 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4087 $this->debug = $debug;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4088 $this->debug_handler = $handler;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4089 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4090
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4091 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4092 * Write the given debug text to the current debug output handler.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4093 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4094 * @param string $message Debug message text.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4095 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4096 * @since 0.5-stable
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4097 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4098 protected function debug($message)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4099 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4100 if (($len = strlen($message)) > self::DEBUG_LINE_LENGTH) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4101 $diff = $len - self::DEBUG_LINE_LENGTH;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4102 $message = substr($message, 0, self::DEBUG_LINE_LENGTH)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4103 . "... [truncated $diff bytes]";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4104 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4105
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4106 if ($this->resourceid) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4107 $message = sprintf('[%s] %s', $this->resourceid, $message);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4108 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4109
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4110 if ($this->debug_handler) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4111 call_user_func_array($this->debug_handler, array(&$this, $message));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4112 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4113 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4114 echo "DEBUG: $message\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4115 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4116 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4117 }