annotate program/lib/Roundcube/rcube_imap_generic.php @ 9:0c9c57d9d4dd

maybe stragglers from 1.3.3, after another mistaken apt-get install of 1.2.3
author Charlie Root
date Fri, 04 Jan 2019 06:38:56 -0500
parents 3a5f959af5ae
children aff04b06b685
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;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
53 protected $cmd_tag;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
54 protected $cmd_num = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
55 protected $resourceid;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
56 protected $prefs = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
57 protected $logged = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
58 protected $capability = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
59 protected $capability_readed = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
60 protected $debug = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
61 protected $debug_handler = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
62
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
63 const ERROR_OK = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
64 const ERROR_NO = -1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
65 const ERROR_BAD = -2;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
66 const ERROR_BYE = -3;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
67 const ERROR_UNKNOWN = -4;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
68 const ERROR_COMMAND = -5;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
69 const ERROR_READONLY = -6;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
70
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
71 const COMMAND_NORESPONSE = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
72 const COMMAND_CAPABILITY = 2;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
73 const COMMAND_LASTLINE = 4;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
74 const COMMAND_ANONYMIZED = 8;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
75
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
76 const DEBUG_LINE_LENGTH = 4098; // 4KB + 2B for \r\n
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
77
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 * Send simple (one line) command to the connection stream
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
81 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
82 * @param string $string Command string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
83 * @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
84 * @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
85 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
86 * @param int Number of bytes sent, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
87 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
88 protected function putLine($string, $endln = true, $anonymized = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
89 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
90 if (!$this->fp) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
91 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
92 }
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 if ($this->debug) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
95 // anonymize the sent command for logging
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
96 $cut = $endln ? 2 : 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
97 if ($anonymized && preg_match('/^(A\d+ (?:[A-Z]+ )+)(.+)/', $string, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
98 $log = $m[1] . sprintf('****** [%d]', strlen($m[2]) - $cut);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
99 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
100 else if ($anonymized) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
101 $log = sprintf('****** [%d]', strlen($string) - $cut);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
102 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
103 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
104 $log = rtrim($string);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
105 }
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 $this->debug('C: ' . $log);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
108 }
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 if ($endln) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
111 $string .= "\r\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
112 }
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 $res = fwrite($this->fp, $string);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
115
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
116 if ($res === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
117 $this->closeSocket();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
118 }
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 return $res;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
121 }
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 * Send command to the connection stream with Command Continuation
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
125 * Requests (RFC3501 7.5) and LITERAL+ (RFC2088) support
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
126 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
127 * @param string $string Command string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
128 * @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
129 * @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
130 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
131 * @return int|bool Number of bytes sent, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
132 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
133 protected function putLineC($string, $endln=true, $anonymized=false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
134 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
135 if (!$this->fp) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
136 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
137 }
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 if ($endln) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
140 $string .= "\r\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
141 }
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 $res = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
144 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
145 for ($i=0, $cnt=count($parts); $i<$cnt; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
146 if (preg_match('/^\{([0-9]+)\}\r\n$/', $parts[$i+1], $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
147 // LITERAL+ support
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
148 if ($this->prefs['literal+']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
149 $parts[$i+1] = sprintf("{%d+}\r\n", $matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
150 }
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 $bytes = $this->putLine($parts[$i].$parts[$i+1], false, $anonymized);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
153 if ($bytes === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
154 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
155 }
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 $res += $bytes;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
158
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
159 // don't wait if server supports LITERAL+ capability
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
160 if (!$this->prefs['literal+']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
161 $line = $this->readLine(1000);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
162 // handle error in command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
163 if ($line[0] != '+') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
164 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
165 }
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 $i++;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
169 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
170 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
171 $bytes = $this->putLine($parts[$i], false, $anonymized);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
172 if ($bytes === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
173 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
174 }
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 $res += $bytes;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
177 }
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 return $res;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
182 }
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 * Reads line from the connection stream
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
186 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
187 * @param int $size Buffer size
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
188 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
189 * @return string Line of text response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
190 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
191 protected function readLine($size = 1024)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
192 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
193 $line = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
194
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
195 if (!$size) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
196 $size = 1024;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
197 }
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 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
200 if ($this->eof()) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
201 return $line ?: null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
202 }
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 $buffer = fgets($this->fp, $size);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
205
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
206 if ($buffer === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
207 $this->closeSocket();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
208 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
209 }
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 if ($this->debug) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
212 $this->debug('S: '. rtrim($buffer));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
213 }
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 $line .= $buffer;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
216 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
217 while (substr($buffer, -1) != "\n");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
218
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
219 return $line;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
220 }
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 * Reads more data from the connection stream when provided
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
224 * data contain string literal
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
225 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
226 * @param string $line Response text
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
227 * @param bool $escape Enables escaping
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
228 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
229 * @return string Line of text response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
230 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
231 protected function multLine($line, $escape = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
232 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
233 $line = rtrim($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
234 if (preg_match('/\{([0-9]+)\}$/', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
235 $out = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
236 $str = substr($line, 0, -strlen($m[0]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
237 $bytes = $m[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
238
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
239 while (strlen($out) < $bytes) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
240 $line = $this->readBytes($bytes);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
241 if ($line === null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
242 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
243 }
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 $out .= $line;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
246 }
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 $line = $str . ($escape ? $this->escape($out) : $out);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
249 }
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 return $line;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
252 }
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 * Reads specified number of bytes from the connection stream
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
256 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
257 * @param int $bytes Number of bytes to get
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
258 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
259 * @return string Response text
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
260 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
261 protected function readBytes($bytes)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
262 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
263 $data = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
264 $len = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
265
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
266 while ($len < $bytes && !$this->eof()) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
267 $d = fread($this->fp, $bytes-$len);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
268 if ($this->debug) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
269 $this->debug('S: '. $d);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
270 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
271 $data .= $d;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
272 $data_len = strlen($data);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
273 if ($len == $data_len) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
274 break; // nothing was read -> exit to avoid apache lockups
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
275 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
276 $len = $data_len;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
277 }
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 return $data;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
280 }
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 * Reads complete response to the IMAP command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
284 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
285 * @param array $untagged Will be filled with untagged response lines
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
286 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
287 * @return string Response text
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
288 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
289 protected function readReply(&$untagged = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
290 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
291 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
292 $line = trim($this->readLine(1024));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
293 // store untagged response lines
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
294 if ($line[0] == '*') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
295 $untagged[] = $line;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
296 }
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 while ($line[0] == '*');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
299
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
300 if ($untagged) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
301 $untagged = join("\n", $untagged);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
302 }
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 return $line;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
305 }
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 * Response parser.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
309 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
310 * @param string $string Response text
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
311 * @param string $err_prefix Error message prefix
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
312 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
313 * @return int Response status
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
314 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
315 protected function parseResult($string, $err_prefix = '')
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
316 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
317 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
318 $res = strtoupper($matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
319 $str = trim($matches[2]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
320
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
321 if ($res == 'OK') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
322 $this->errornum = self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
323 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
324 else if ($res == 'NO') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
325 $this->errornum = self::ERROR_NO;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
326 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
327 else if ($res == 'BAD') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
328 $this->errornum = self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
329 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
330 else if ($res == 'BYE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
331 $this->closeSocket();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
332 $this->errornum = self::ERROR_BYE;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
333 }
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 if ($str) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
336 $str = trim($str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
337 // get response string and code (RFC5530)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
338 if (preg_match("/^\[([a-z-]+)\]/i", $str, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
339 $this->resultcode = strtoupper($m[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
340 $str = trim(substr($str, strlen($m[1]) + 2));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
341 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
342 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
343 $this->resultcode = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
344 // parse response for [APPENDUID 1204196876 3456]
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
345 if (preg_match("/^\[APPENDUID [0-9]+ ([0-9]+)\]/i", $str, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
346 $this->data['APPENDUID'] = $m[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
347 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
348 // parse response for [COPYUID 1204196876 3456:3457 123:124]
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
349 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
350 $this->data['COPYUID'] = array($m[1], $m[2]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
351 }
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 $this->result = $str;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
355
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
356 if ($this->errornum != self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
357 $this->error = $err_prefix ? $err_prefix.$str : $str;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
358 }
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 return $this->errornum;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
362 }
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 return self::ERROR_UNKNOWN;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
365 }
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 * Checks connection stream state.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
369 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
370 * @return bool True if connection is closed
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
371 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
372 protected function eof()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
373 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
374 if (!is_resource($this->fp)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
375 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
376 }
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 // If a connection opened by fsockopen() wasn't closed
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
379 // by the server, feof() will hang.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
380 $start = microtime(true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
381
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
382 if (feof($this->fp) ||
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
383 ($this->prefs['timeout'] && (microtime(true) - $start > $this->prefs['timeout']))
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
384 ) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
385 $this->closeSocket();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
386 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
387 }
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 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
390 }
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 * Closes connection stream.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
394 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
395 protected function closeSocket()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
396 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
397 @fclose($this->fp);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
398 $this->fp = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
399 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
400
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
401 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
402 * Error code/message setter.
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 protected function setError($code, $msg = '')
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
405 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
406 $this->errornum = $code;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
407 $this->error = $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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
410 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
411 * Checks response status.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
412 * 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
413 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
414 * @param string $string Response text
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
415 * @param string $match Prefix to match with (case-sensitive)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
416 * @param bool $error Enables BYE/BAD checking
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
417 * @param bool $nonempty Enables empty response checking
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
418 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
419 * @return bool True any check is true or connection is closed.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
420 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
421 protected function startsWith($string, $match, $error = false, $nonempty = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
422 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
423 if (!$this->fp) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
424 return true;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
427 if (strncmp($string, $match, strlen($match)) == 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
428 return true;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
431 if ($error && preg_match('/^\* (BYE|BAD) /i', $string, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
432 if (strtoupper($m[1]) == 'BYE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
433 $this->closeSocket();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
434 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
435 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
436 }
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 if ($nonempty && !strlen($string)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
439 return true;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
442 return false;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
446 * Capabilities checker
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 protected function hasCapability($name)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
449 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
450 if (empty($this->capability) || $name == '') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
451 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
454 if (in_array($name, $this->capability)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
455 return true;
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 else if (strpos($name, '=')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
458 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
461 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
462 foreach ($this->capability as $cap) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
463 $entry = explode('=', $cap);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
464 if ($entry[0] == $name) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
465 $result[] = $entry[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
466 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
467 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
468
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
469 return $result ?: false;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
473 * Capabilities checker
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 * @param string $name Capability name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
476 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
477 * @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
478 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
479 public function getCapability($name)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
480 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
481 $result = $this->hasCapability($name);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
482
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
483 if (!empty($result)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
484 return $result;
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 else if ($this->capability_readed) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
487 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
490 // get capabilities (only once) because initial
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
491 // optional CAPABILITY response may differ
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
492 $result = $this->execute('CAPABILITY');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
493
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
494 if ($result[0] == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
495 $this->parseCapability($result[1]);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
498 $this->capability_readed = true;
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 return $this->hasCapability($name);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
501 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
504 * Clears detected server capabilities
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 public function clearCapability()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
507 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
508 $this->capability = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
509 $this->capability_readed = false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
512 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
513 * DIGEST-MD5/CRAM-MD5/PLAIN Authentication
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 * @param string $user Username
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
516 * @param string $pass Password
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
517 * @param string $type Authentication type (PLAIN/CRAM-MD5/DIGEST-MD5)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
518 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
519 * @return resource Connection resourse on success, error code on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
520 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
521 protected function authenticate($user, $pass, $type = 'PLAIN')
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
522 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
523 if ($type == 'CRAM-MD5' || $type == 'DIGEST-MD5') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
524 if ($type == 'DIGEST-MD5' && !class_exists('Auth_SASL')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
525 $this->setError(self::ERROR_BYE,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
526 "The Auth_SASL package is required for DIGEST-MD5 authentication");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
527 return self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
528 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
529
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
530 $this->putLine($this->nextTag() . " AUTHENTICATE $type");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
531 $line = trim($this->readReply());
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 if ($line[0] == '+') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
534 $challenge = substr($line, 2);
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 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
537 return $this->parseResult($line);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
540 if ($type == 'CRAM-MD5') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
541 // RFC2195: CRAM-MD5
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
542 $ipad = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
543 $opad = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
544 $xor = function($str1, $str2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
545 $result = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
546 $size = strlen($str1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
547 for ($i=0; $i<$size; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
548 $result .= chr(ord($str1[$i]) ^ ord($str2[$i]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
549 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
550 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
551 };
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 // initialize ipad, opad
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
554 for ($i=0; $i<64; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
555 $ipad .= chr(0x36);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
556 $opad .= chr(0x5C);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
557 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
558
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
559 // pad $pass so it's 64 bytes
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
560 $pass = str_pad($pass, 64, chr(0));
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 // generate hash
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
563 $hash = md5($xor($pass, $opad) . pack("H*",
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
564 md5($xor($pass, $ipad) . base64_decode($challenge))));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
565 $reply = base64_encode($user . ' ' . $hash);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
566
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
567 // send result
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
568 $this->putLine($reply, true, true);
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 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
571 // RFC2831: DIGEST-MD5
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
572 // proxy authorization
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
573 if (!empty($this->prefs['auth_cid'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
574 $authc = $this->prefs['auth_cid'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
575 $pass = $this->prefs['auth_pw'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
576 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
577 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
578 $authc = $user;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
579 $user = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
580 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
581
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
582 $auth_sasl = new Auth_SASL;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
583 $auth_sasl = $auth_sasl->factory('digestmd5');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
584 $reply = base64_encode($auth_sasl->getResponse($authc, $pass,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
585 base64_decode($challenge), $this->host, 'imap', $user));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
586
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
587 // send result
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
588 $this->putLine($reply, true, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
589 $line = trim($this->readReply());
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
590
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
591 if ($line[0] != '+') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
592 return $this->parseResult($line);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
595 // check response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
596 $challenge = substr($line, 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
597 $challenge = base64_decode($challenge);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
598 if (strpos($challenge, 'rspauth=') === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
599 $this->setError(self::ERROR_BAD,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
600 "Unexpected response from server to DIGEST-MD5 response");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
601 return self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
602 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
603
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
604 $this->putLine('');
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 $line = $this->readReply();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
608 $result = $this->parseResult($line);
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 else if ($type == 'GSSAPI') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
611 if (!extension_loaded('krb5')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
612 $this->setError(self::ERROR_BYE,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
613 "The krb5 extension is required for GSSAPI authentication");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
614 return self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
615 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
616
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
617 if (empty($this->prefs['gssapi_cn'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
618 $this->setError(self::ERROR_BYE,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
619 "The gssapi_cn parameter is required for GSSAPI authentication");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
620 return self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
621 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
622
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
623 if (empty($this->prefs['gssapi_context'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
624 $this->setError(self::ERROR_BYE,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
625 "The gssapi_context parameter is required for GSSAPI authentication");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
626 return self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
627 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
628
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
629 putenv('KRB5CCNAME=' . $this->prefs['gssapi_cn']);
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 try {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
632 $ccache = new KRB5CCache();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
633 $ccache->open($this->prefs['gssapi_cn']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
634 $gssapicontext = new GSSAPIContext();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
635 $gssapicontext->acquireCredentials($ccache);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
636
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
637 $token = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
638 $success = $gssapicontext->initSecContext($this->prefs['gssapi_context'], null, null, null, $token);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
639 $token = base64_encode($token);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
640 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
641 catch (Exception $e) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
642 trigger_error($e->getMessage(), E_USER_WARNING);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
643 $this->setError(self::ERROR_BYE, "GSSAPI authentication failed");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
644 return self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
645 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
646
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
647 $this->putLine($this->nextTag() . " AUTHENTICATE GSSAPI " . $token);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
648 $line = trim($this->readReply());
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 if ($line[0] != '+') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
651 return $this->parseResult($line);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
654 try {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
655 $challenge = base64_decode(substr($line, 2));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
656 $gssapicontext->unwrap($challenge, $challenge);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
657 $gssapicontext->wrap($challenge, $challenge, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
658 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
659 catch (Exception $e) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
660 trigger_error($e->getMessage(), E_USER_WARNING);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
661 $this->setError(self::ERROR_BYE, "GSSAPI authentication failed");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
662 return self::ERROR_BAD;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
663 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
664
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
665 $this->putLine(base64_encode($challenge));
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 $line = $this->readReply();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
668 $result = $this->parseResult($line);
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 else { // PLAIN
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
671 // proxy authorization
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
672 if (!empty($this->prefs['auth_cid'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
673 $authc = $this->prefs['auth_cid'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
674 $pass = $this->prefs['auth_pw'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
675 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
676 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
677 $authc = $user;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
678 $user = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
679 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
680
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
681 $reply = base64_encode($user . chr(0) . $authc . chr(0) . $pass);
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 // RFC 4959 (SASL-IR): save one round trip
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
684 if ($this->getCapability('SASL-IR')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
685 list($result, $line) = $this->execute("AUTHENTICATE PLAIN", array($reply),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
686 self::COMMAND_LASTLINE | self::COMMAND_CAPABILITY | self::COMMAND_ANONYMIZED);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
687 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
688 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
689 $this->putLine($this->nextTag() . " AUTHENTICATE PLAIN");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
690 $line = trim($this->readReply());
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
691
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
692 if ($line[0] != '+') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
693 return $this->parseResult($line);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
696 // send result, get reply and process it
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
697 $this->putLine($reply, true, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
698 $line = $this->readReply();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
699 $result = $this->parseResult($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
700 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
701 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
702
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
703 if ($result == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
704 // optional CAPABILITY response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
705 if ($line && preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
706 $this->parseCapability($matches[1], true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
707 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
708 return $this->fp;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
709 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
710 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
711 $this->setError($result, "AUTHENTICATE $type: $line");
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
714 return $result;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
718 * LOGIN Authentication
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 * @param string $user Username
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
721 * @param string $pass Password
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 * @return resource Connection resourse on success, error code on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
724 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
725 protected function login($user, $password)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
726 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
727 list($code, $response) = $this->execute('LOGIN', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
728 $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
729
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
730 // re-set capabilities list if untagged CAPABILITY response provided
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
731 if (preg_match('/\* CAPABILITY (.+)/i', $response, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
732 $this->parseCapability($matches[1], true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
733 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
734
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
735 if ($code == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
736 return $this->fp;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
739 return $code;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
743 * Detects hierarchy delimiter
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 * @return string The delimiter
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
746 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
747 public function getHierarchyDelimiter()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
748 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
749 if ($this->prefs['delimiter']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
750 return $this->prefs['delimiter'];
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
753 // try (LIST "" ""), should return delimiter (RFC2060 Sec 6.3.8)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
754 list($code, $response) = $this->execute('LIST',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
755 array($this->escape(''), $this->escape('')));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
756
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
757 if ($code == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
758 $args = $this->tokenizeResponse($response, 4);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
759 $delimiter = $args[3];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
760
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
761 if (strlen($delimiter) > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
762 return ($this->prefs['delimiter'] = $delimiter);
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
765 }
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 * NAMESPACE handler (RFC 2342)
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 * @return array Namespace data hash (personal, other, shared)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
771 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
772 public function getNamespace()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
773 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
774 if (array_key_exists('namespace', $this->prefs)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
775 return $this->prefs['namespace'];
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
778 if (!$this->getCapability('NAMESPACE')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
779 return self::ERROR_BAD;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
782 list($code, $response) = $this->execute('NAMESPACE');
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 if ($code == self::ERROR_OK && preg_match('/^\* NAMESPACE /', $response)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
785 $response = substr($response, 11);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
786 $data = $this->tokenizeResponse($response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
787 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
788
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
789 if (!is_array($data)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
790 return $code;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
793 $this->prefs['namespace'] = array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
794 'personal' => $data[0],
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
795 'other' => $data[1],
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
796 'shared' => $data[2],
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
797 );
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
798
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
799 return $this->prefs['namespace'];
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
803 * Connects to IMAP server and authenticates.
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 * @param string $host Server hostname or IP
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
806 * @param string $user User name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
807 * @param string $password Password
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
808 * @param array $options Connection and class options
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
809 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
810 * @return bool True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
811 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
812 public function connect($host, $user, $password, $options = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
813 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
814 // configure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
815 $this->set_prefs($options);
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 $this->host = $host;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
818 $this->user = $user;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
819 $this->logged = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
820 $this->selected = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
821
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
822 // check input
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
823 if (empty($host)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
824 $this->setError(self::ERROR_BAD, "Empty host");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
825 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
826 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
827
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
828 if (empty($user)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
829 $this->setError(self::ERROR_NO, "Empty user");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
830 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
831 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
832
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
833 if (empty($password) && empty($options['gssapi_cn'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
834 $this->setError(self::ERROR_NO, "Empty password");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
835 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
836 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
837
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
838 // Connect
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
839 if (!$this->_connect($host)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
840 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
841 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
842
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
843 // Send ID info
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
844 if (!empty($this->prefs['ident']) && $this->getCapability('ID')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
845 $this->data['ID'] = $this->id($this->prefs['ident']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
846 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
847
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
848 $auth_method = $this->prefs['auth_type'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
849 $auth_methods = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
850 $result = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
851
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
852 // check for supported auth methods
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
853 if ($auth_method == 'CHECK') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
854 if ($auth_caps = $this->getCapability('AUTH')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
855 $auth_methods = $auth_caps;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
856 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
857
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
858 // RFC 2595 (LOGINDISABLED) LOGIN disabled when connection is not secure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
859 $login_disabled = $this->getCapability('LOGINDISABLED');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
860 if (($key = array_search('LOGIN', $auth_methods)) !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
861 if ($login_disabled) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
862 unset($auth_methods[$key]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
863 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
864 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
865 else if (!$login_disabled) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
866 $auth_methods[] = 'LOGIN';
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
869 // Use best (for security) supported authentication method
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
870 $all_methods = array('DIGEST-MD5', 'CRAM-MD5', 'CRAM_MD5', 'PLAIN', 'LOGIN');
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 if (!empty($this->prefs['gssapi_cn'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
873 array_unshift($all_methods, 'GSSAPI');
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
876 foreach ($all_methods as $auth_method) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
877 if (in_array($auth_method, $auth_methods)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
878 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
879 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
880 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
881 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
882 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
883 // 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
884 if ($auth_method == 'LOGIN' && $this->getCapability('LOGINDISABLED')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
885 $this->setError(self::ERROR_BAD, "Login disabled by IMAP server");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
886 $this->closeConnection();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
887 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
888 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
889 // replace AUTH with CRAM-MD5 for backward compat.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
890 if ($auth_method == 'AUTH') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
891 $auth_method = 'CRAM-MD5';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
892 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
893 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
894
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
895 // pre-login capabilities can be not complete
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
896 $this->capability_readed = false;
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 // Authenticate
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
899 switch ($auth_method) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
900 case 'CRAM_MD5':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
901 $auth_method = 'CRAM-MD5';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
902 case 'CRAM-MD5':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
903 case 'DIGEST-MD5':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
904 case 'PLAIN':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
905 case 'GSSAPI':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
906 $result = $this->authenticate($user, $password, $auth_method);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
907 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
908 case 'LOGIN':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
909 $result = $this->login($user, $password);
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 default:
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
912 $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
913 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
914
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
915 // Connected and authenticated
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
916 if (is_resource($result)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
917 if ($this->prefs['force_caps']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
918 $this->clearCapability();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
919 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
920 $this->logged = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
921
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
922 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
923 }
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 $this->closeConnection();
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 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
928 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
931 * Connects to IMAP server.
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 * @param string $host Server hostname or IP
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
934 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
935 * @return bool True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
936 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
937 protected function _connect($host)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
938 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
939 // initialize connection
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
940 $this->error = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
941 $this->errornum = self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
942
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
943 if (!$this->prefs['port']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
944 $this->prefs['port'] = 143;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
947 // check for SSL
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
948 if ($this->prefs['ssl_mode'] && $this->prefs['ssl_mode'] != 'tls') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
949 $host = $this->prefs['ssl_mode'] . '://' . $host;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
950 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
951
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
952 if ($this->prefs['timeout'] <= 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
953 $this->prefs['timeout'] = max(0, intval(ini_get('default_socket_timeout')));
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
956 if (!empty($this->prefs['socket_options'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
957 $context = stream_context_create($this->prefs['socket_options']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
958 $this->fp = stream_socket_client($host . ':' . $this->prefs['port'], $errno, $errstr,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
959 $this->prefs['timeout'], STREAM_CLIENT_CONNECT, $context);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
960 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
961 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
962 $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
963 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
964
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
965 if (!$this->fp) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
966 $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
967 $host, $this->prefs['port'], $errstr ?: "Unknown reason"));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
968
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
969 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
970 }
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 if ($this->prefs['timeout'] > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
973 stream_set_timeout($this->fp, $this->prefs['timeout']);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
976 $line = trim(fgets($this->fp, 8192));
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 if ($this->debug) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
979 // set connection identifier for debug output
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
980 preg_match('/#([0-9]+)/', (string) $this->fp, $m);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
981 $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
982
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
983 if ($line) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
984 $this->debug('S: '. $line);
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
987
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
988 // Connected to wrong port or connection error?
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
989 if (!preg_match('/^\* (OK|PREAUTH)/i', $line)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
990 if ($line)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
991 $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
992 else
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
993 $error = sprintf("Empty startup greeting (%s:%d)", $host, $this->prefs['port']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
994
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
995 $this->setError(self::ERROR_BAD, $error);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
996 $this->closeConnection();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
997 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
998 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
999
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1000 $this->data['GREETING'] = trim(preg_replace('/\[[^\]]+\]\s*/', '', $line));
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 // RFC3501 [7.1] optional CAPABILITY response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1003 if (preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1004 $this->parseCapability($matches[1], true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1005 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1006
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1007 // TLS connection
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1008 if ($this->prefs['ssl_mode'] == 'tls' && $this->getCapability('STARTTLS')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1009 $res = $this->execute('STARTTLS');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1010
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1011 if ($res[0] != self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1012 $this->closeConnection();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1013 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1014 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1015
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1016 if (isset($this->prefs['socket_options']['ssl']['crypto_method'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1017 $crypto_method = $this->prefs['socket_options']['ssl']['crypto_method'];
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 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1020 // There is no flag to enable all TLS methods. Net_SMTP
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1021 // handles enabling TLS similarly.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1022 $crypto_method = STREAM_CRYPTO_METHOD_TLS_CLIENT
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1023 | @STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1024 | @STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1025 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1026
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1027 if (!stream_socket_enable_crypto($this->fp, true, $crypto_method)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1028 $this->setError(self::ERROR_BAD, "Unable to negotiate TLS");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1029 $this->closeConnection();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1030 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1031 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1032
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1033 // Now we're secure, capabilities need to be reread
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1034 $this->clearCapability();
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1037 return true;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1041 * Initializes environment
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 protected function set_prefs($prefs)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1044 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1045 // set preferences
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1046 if (is_array($prefs)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1047 $this->prefs = $prefs;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1048 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1049
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1050 // set auth method
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1051 if (!empty($this->prefs['auth_type'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1052 $this->prefs['auth_type'] = strtoupper($this->prefs['auth_type']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1053 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1054 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1055 $this->prefs['auth_type'] = 'CHECK';
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1058 // disabled capabilities
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1059 if (!empty($this->prefs['disabled_caps'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1060 $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
1061 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1062
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1063 // additional message flags
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1064 if (!empty($this->prefs['message_flags'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1065 $this->flags = array_merge($this->flags, $this->prefs['message_flags']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1066 unset($this->prefs['message_flags']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1067 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1068 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1069
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 * Checks connection status
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 * @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
1074 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1075 public function connected()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1076 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1077 return $this->fp && $this->logged;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1078 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1081 * Closes connection with logout.
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 public function closeConnection()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1084 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1085 if ($this->logged && $this->putLine($this->nextTag() . ' LOGOUT')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1086 $this->readReply();
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1089 $this->closeSocket();
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1093 * Executes SELECT command (if mailbox is already not in selected state)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1096 * @param array $qresync_data QRESYNC data (RFC5162)
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 * @return boolean True on success, false on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1099 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1100 public function select($mailbox, $qresync_data = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1101 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1102 if (!strlen($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1103 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1106 if ($this->selected === $mailbox) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1107 return true;
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 /*
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1110 Temporary commented out because Courier returns \Noselect for INBOX
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1111 Requires more investigation
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 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
1114 if (in_array('\\Noselect', $opts)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1115 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1116 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1117 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1118 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1119 $params = array($this->escape($mailbox));
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 // QRESYNC data items
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1122 // 0. the last known UIDVALIDITY,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1123 // 1. the last known modification sequence,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1124 // 2. the optional set of known UIDs, and
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1125 // 3. an optional parenthesized list of known sequence ranges and their
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1126 // corresponding UIDs.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1127 if (!empty($qresync_data)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1128 if (!empty($qresync_data[2])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1129 $qresync_data[2] = self::compressMessageSet($qresync_data[2]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1130 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1131
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1132 $params[] = array('QRESYNC', $qresync_data);
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 list($code, $response) = $this->execute('SELECT', $params);
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 if ($code == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1138 $this->clear_mailbox_cache();
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 $response = explode("\r\n", $response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1141 foreach ($response as $line) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1142 if (preg_match('/^\* OK \[/i', $line)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1143 $pos = strcspn($line, ' ]', 6);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1144 $token = strtoupper(substr($line, 6, $pos));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1145 $pos += 7;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1146
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1147 switch ($token) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1148 case 'UIDNEXT':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1149 case 'UIDVALIDITY':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1150 case 'UNSEEN':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1151 if ($len = strspn($line, '0123456789', $pos)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1152 $this->data[$token] = (int) substr($line, $pos, $len);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1153 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1154 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1155
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1156 case 'HIGHESTMODSEQ':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1157 if ($len = strspn($line, '0123456789', $pos)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1158 $this->data[$token] = (string) substr($line, $pos, $len);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1159 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1160 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1161
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1162 case 'NOMODSEQ':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1163 $this->data[$token] = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1164 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1165
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1166 case 'PERMANENTFLAGS':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1167 $start = strpos($line, '(', $pos);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1168 $end = strrpos($line, ')');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1169 if ($start && $end) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1170 $flags = substr($line, $start + 1, $end - $start - 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1171 $this->data[$token] = explode(' ', $flags);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1172 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1173 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1174 }
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 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
1177 $token = strtoupper($match[2]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1178 switch ($token) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1179 case 'EXISTS':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1180 case 'RECENT':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1181 $this->data[$token] = (int) $match[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1182 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1183
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1184 case 'FETCH':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1185 // QRESYNC FETCH response (RFC5162)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1186 $line = substr($line, strlen($match[0]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1187 $fetch_data = $this->tokenizeResponse($line, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1188 $data = array('id' => $match[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1189
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1190 for ($i=0, $size=count($fetch_data); $i<$size; $i+=2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1191 $data[strtolower($fetch_data[$i])] = $fetch_data[$i+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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1194 $this->data['QRESYNC'][$data['uid']] = $data;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1195 break;
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1198 // QRESYNC VANISHED response (RFC5162)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1199 else if (preg_match('/^\* VANISHED [()EARLIER]*/i', $line, $match)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1200 $line = substr($line, strlen($match[0]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1201 $v_data = $this->tokenizeResponse($line, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1202
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1203 $this->data['VANISHED'] = $v_data;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1204 }
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1207 $this->data['READ-WRITE'] = $this->resultcode != 'READ-ONLY';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1208 $this->selected = $mailbox;
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 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1211 }
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 false;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1217 * Executes STATUS command
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1220 * @param array $items Additional requested item names. By default
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1221 * MESSAGES and UNSEEN are requested. Other defined
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1222 * in RFC3501: UIDNEXT, UIDVALIDITY, RECENT
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1223 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1224 * @return array Status item-value hash
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1225 * @since 0.5-beta
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 public function status($mailbox, $items = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1228 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1229 if (!strlen($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1230 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1233 if (!in_array('MESSAGES', $items)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1234 $items[] = 'MESSAGES';
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('UNSEEN', $items)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1237 $items[] = 'UNSEEN';
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1240 list($code, $response) = $this->execute('STATUS', array($this->escape($mailbox),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1241 '(' . implode(' ', $items) . ')'));
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 if ($code == self::ERROR_OK && preg_match('/^\* STATUS /i', $response)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1244 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1245 $response = substr($response, 9); // remove prefix "* STATUS "
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1246
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1247 list($mbox, $items) = $this->tokenizeResponse($response, 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1248
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1249 // Fix for #1487859. Some buggy server returns not quoted
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1250 // folder name with spaces. Let's try to handle this situation
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1251 if (!is_array($items) && ($pos = strpos($response, '(')) !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1252 $response = substr($response, $pos);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1253 $items = $this->tokenizeResponse($response, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1254 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1255
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1256 if (!is_array($items)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1257 return $result;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1260 for ($i=0, $len=count($items); $i<$len; $i += 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1261 $result[$items[$i]] = $items[$i+1];
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1264 $this->data['STATUS:'.$mailbox] = $result;
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 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1267 }
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 false;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1273 * Executes EXPUNGE command
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1276 * @param string|array $messages Message UIDs to expunge
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 * @return boolean True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1279 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1280 public function expunge($mailbox, $messages = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1281 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1282 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1283 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1286 if (!$this->data['READ-WRITE']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1287 $this->setError(self::ERROR_READONLY, "Mailbox is read-only");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1288 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1289 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1290
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1291 // Clear internal status cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1292 $this->clear_status_cache($mailbox);
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 if (!empty($messages) && $messages != '*' && $this->hasCapability('UIDPLUS')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1295 $messages = self::compressMessageSet($messages);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1296 $result = $this->execute('UID EXPUNGE', array($messages), self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1297 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1298 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1299 $result = $this->execute('EXPUNGE', null, 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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1302 if ($result == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1303 $this->selected = null; // state has changed, need to reselect
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1304 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1305 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1306
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1307 return false;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1311 * Executes CLOSE command
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 * @return boolean True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1314 * @since 0.5
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 public function close()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1317 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1318 $result = $this->execute('CLOSE', null, self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1319
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1320 if ($result == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1321 $this->selected = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1322 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1323 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1324
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1325 return false;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1329 * Folder subscription (SUBSCRIBE)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1332 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1333 * @return boolean True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1334 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1335 public function subscribe($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1336 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1337 $result = $this->execute('SUBSCRIBE', array($this->escape($mailbox)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1338 self::COMMAND_NORESPONSE);
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 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1341 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1344 * Folder unsubscription (UNSUBSCRIBE)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1347 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1348 * @return boolean True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1349 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1350 public function unsubscribe($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1351 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1352 $result = $this->execute('UNSUBSCRIBE', array($this->escape($mailbox)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1353 self::COMMAND_NORESPONSE);
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 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1356 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1359 * Folder creation (CREATE)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1362 * @param array $types Optional folder types (RFC 6154)
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 * @return bool True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1365 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1366 public function createFolder($mailbox, $types = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1367 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1368 $args = array($this->escape($mailbox));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1369
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1370 // RFC 6154: CREATE-SPECIAL-USE
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1371 if (!empty($types) && $this->getCapability('CREATE-SPECIAL-USE')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1372 $args[] = '(USE (' . implode(' ', $types) . '))';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1373 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1374
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1375 $result = $this->execute('CREATE', $args, self::COMMAND_NORESPONSE);
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 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1378 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1381 * Folder renaming (RENAME)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1384 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1385 * @return bool True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1386 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1387 public function renameFolder($from, $to)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1388 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1389 $result = $this->execute('RENAME', array($this->escape($from), $this->escape($to)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1390 self::COMMAND_NORESPONSE);
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 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1393 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1396 * Executes DELETE command
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1399 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1400 * @return boolean True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1401 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1402 public function deleteFolder($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1403 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1404 $result = $this->execute('DELETE', array($this->escape($mailbox)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1405 self::COMMAND_NORESPONSE);
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 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1408 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1411 * Removes all messages in a folder
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1414 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1415 * @return boolean True on success, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1416 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1417 public function clearFolder($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1418 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1419 if ($this->countMessages($mailbox) > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1420 $res = $this->flag($mailbox, '1:*', 'DELETED');
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1423 if ($res) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1424 if ($this->selected === $mailbox) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1425 $res = $this->close();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1426 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1427 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1428 $res = $this->expunge($mailbox);
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1431
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1432 return $res;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1436 * Returns list of mailboxes
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 * @param string $ref Reference name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1439 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1440 * @param array $return_opts (see self::_listMailboxes)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1441 * @param array $select_opts (see self::_listMailboxes)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1442 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1443 * @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
1444 * is requested, False on error.
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 public function listMailboxes($ref, $mailbox, $return_opts = array(), $select_opts = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1447 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1448 return $this->_listMailboxes($ref, $mailbox, false, $return_opts, $select_opts);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1449 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1452 * Returns list of subscribed mailboxes
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 * @param string $ref Reference name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1455 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1456 * @param array $return_opts (see self::_listMailboxes)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1457 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1458 * @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
1459 * is requested, False on error.
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 public function listSubscribed($ref, $mailbox, $return_opts = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1462 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1463 return $this->_listMailboxes($ref, $mailbox, true, $return_opts, null);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1464 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1467 * IMAP LIST/LSUB command
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 * @param string $ref Reference name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1470 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1471 * @param bool $subscribed Enables returning subscribed mailboxes only
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1472 * @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
1473 * Possible: MESSAGES, RECENT, UIDNEXT, UIDVALIDITY, UNSEEN,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1474 * MYRIGHTS, SUBSCRIBED, CHILDREN
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1475 * @param array $select_opts List of selection options (RFC5258: LIST-EXTENDED)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1476 * Possible: SUBSCRIBED, RECURSIVEMATCH, REMOTE,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1477 * SPECIAL-USE (RFC6154)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1478 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1479 * @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
1480 * is requested, False on error.
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 protected function _listMailboxes($ref, $mailbox, $subscribed=false,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1483 $return_opts=array(), $select_opts=array())
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 if (!strlen($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1486 $mailbox = '*';
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1489 $args = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1490 $rets = array();
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 if (!empty($select_opts) && $this->getCapability('LIST-EXTENDED')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1493 $select_opts = (array) $select_opts;
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 $args[] = '(' . implode(' ', $select_opts) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1496 }
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[] = $this->escape($ref);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1499 $args[] = $this->escape($mailbox);
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 if (!empty($return_opts) && $this->getCapability('LIST-EXTENDED')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1502 $ext_opts = array('SUBSCRIBED', 'CHILDREN');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1503 $rets = array_intersect($return_opts, $ext_opts);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1504 $return_opts = array_diff($return_opts, $rets);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1505 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1506
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1507 if (!empty($return_opts) && $this->getCapability('LIST-STATUS')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1508 $lstatus = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1509 $status_opts = array('MESSAGES', 'RECENT', 'UIDNEXT', 'UIDVALIDITY', 'UNSEEN');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1510 $opts = array_diff($return_opts, $status_opts);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1511 $status_opts = array_diff($return_opts, $opts);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1512
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1513 if (!empty($status_opts)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1514 $rets[] = 'STATUS (' . implode(' ', $status_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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1517 if (!empty($opts)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1518 $rets = array_merge($rets, $opts);
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1521
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1522 if (!empty($rets)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1523 $args[] = 'RETURN (' . implode(' ', $rets) . ')';
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1526 list($code, $response) = $this->execute($subscribed ? 'LSUB' : 'LIST', $args);
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 if ($code == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1529 $folders = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1530 $last = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1531 $pos = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1532 $response .= "\r\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1533
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1534 while ($pos = strpos($response, "\r\n", $pos+1)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1535 // literal string, not real end-of-command-line
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1536 if ($response[$pos-1] == '}') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1537 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1538 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1539
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1540 $line = substr($response, $last, $pos - $last);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1541 $last = $pos + 2;
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 if (!preg_match('/^\* (LIST|LSUB|STATUS|MYRIGHTS) /i', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1544 continue;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1547 $cmd = strtoupper($m[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1548 $line = substr($line, strlen($m[0]));
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 // * LIST (<options>) <delimiter> <mailbox>
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1551 if ($cmd == 'LIST' || $cmd == 'LSUB') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1552 list($opts, $delim, $mailbox) = $this->tokenizeResponse($line, 3);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1553
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1554 // 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
1555 if ($delim) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1556 $mailbox = rtrim($mailbox, $delim);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1557 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1558
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1559 // Add to result array
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1560 if (!$lstatus) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1561 $folders[] = $mailbox;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1562 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1563 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1564 $folders[$mailbox] = array();
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1567 // store folder options
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1568 if ($cmd == 'LIST') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1569 // Add to options array
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1570 if (empty($this->data['LIST'][$mailbox])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1571 $this->data['LIST'][$mailbox] = $opts;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1572 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1573 else if (!empty($opts)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1574 $this->data['LIST'][$mailbox] = array_unique(array_merge(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1575 $this->data['LIST'][$mailbox], $opts));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1576 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1577 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1578 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1579 else if ($lstatus) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1580 // * STATUS <mailbox> (<result>)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1581 if ($cmd == 'STATUS') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1582 list($mailbox, $status) = $this->tokenizeResponse($line, 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1583
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1584 for ($i=0, $len=count($status); $i<$len; $i += 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1585 list($name, $value) = $this->tokenizeResponse($status, 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1586 $folders[$mailbox][$name] = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1587 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1588 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1589 // * MYRIGHTS <mailbox> <acl>
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1590 else if ($cmd == 'MYRIGHTS') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1591 list($mailbox, $acl) = $this->tokenizeResponse($line, 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1592 $folders[$mailbox]['MYRIGHTS'] = $acl;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1593 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1594 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1595 }
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 return $folders;
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 false;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1604 * Returns count of all messages in a folder
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1607 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1608 * @return int Number of messages, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1609 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1610 public function countMessages($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1611 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1612 if ($this->selected === $mailbox && isset($this->data['EXISTS'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1613 return $this->data['EXISTS'];
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1616 // Check internal cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1617 $cache = $this->data['STATUS:'.$mailbox];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1618 if (!empty($cache) && isset($cache['MESSAGES'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1619 return (int) $cache['MESSAGES'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1620 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1621
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1622 // Try STATUS (should be faster than SELECT)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1623 $counts = $this->status($mailbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1624 if (is_array($counts)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1625 return (int) $counts['MESSAGES'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1626 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1627
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1628 return false;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1632 * Returns count of messages with \Recent flag in a folder
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1635 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1636 * @return int Number of messages, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1637 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1638 public function countRecent($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1639 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1640 if ($this->selected === $mailbox && isset($this->data['RECENT'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1641 return $this->data['RECENT'];
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1644 // Check internal cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1645 $cache = $this->data['STATUS:'.$mailbox];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1646 if (!empty($cache) && isset($cache['RECENT'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1647 return (int) $cache['RECENT'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1648 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1649
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1650 // Try STATUS (should be faster than SELECT)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1651 $counts = $this->status($mailbox, array('RECENT'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1652 if (is_array($counts)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1653 return (int) $counts['RECENT'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1654 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1655
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1656 return false;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1660 * Returns count of messages without \Seen flag in a specified folder
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1663 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1664 * @return int Number of messages, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1665 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1666 public function countUnseen($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1667 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1668 // Check internal cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1669 $cache = $this->data['STATUS:'.$mailbox];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1670 if (!empty($cache) && isset($cache['UNSEEN'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1671 return (int) $cache['UNSEEN'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1672 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1673
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1674 // Try STATUS (should be faster than SELECT+SEARCH)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1675 $counts = $this->status($mailbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1676 if (is_array($counts)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1677 return (int) $counts['UNSEEN'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1678 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1679
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1680 // Invoke SEARCH as a fallback
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1681 $index = $this->search($mailbox, 'ALL UNSEEN', false, array('COUNT'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1682 if (!$index->is_error()) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1683 return $index->count();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1684 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1685
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1686 return false;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1690 * Executes ID command (RFC2971)
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 * @param array $items Client identification information key/value hash
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1693 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1694 * @return array Server identification information key/value hash
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1695 * @since 0.6
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 public function id($items = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1698 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1699 if (is_array($items) && !empty($items)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1700 foreach ($items as $key => $value) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1701 $args[] = $this->escape($key, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1702 $args[] = $this->escape($value, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1703 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1704 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1705
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1706 list($code, $response) = $this->execute('ID', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1707 !empty($args) ? '(' . implode(' ', (array) $args) . ')' : $this->escape(null)
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1710 if ($code == self::ERROR_OK && preg_match('/^\* ID /i', $response)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1711 $response = substr($response, 5); // remove prefix "* ID "
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1712 $items = $this->tokenizeResponse($response, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1713 $result = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1714
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1715 for ($i=0, $len=count($items); $i<$len; $i += 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1716 $result[$items[$i]] = $items[$i+1];
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1719 return $result;
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 false;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1726 * Executes ENABLE command (RFC5161)
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 * @param mixed $extension Extension name to enable (or array of names)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1729 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1730 * @return array|bool List of enabled extensions, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1731 * @since 0.6
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 public function enable($extension)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1734 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1735 if (empty($extension)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1736 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1739 if (!$this->hasCapability('ENABLE')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1740 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1743 if (!is_array($extension)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1744 $extension = array($extension);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1747 if (!empty($this->extensions_enabled)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1748 // check if all extensions are already enabled
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1749 $diff = array_diff($extension, $this->extensions_enabled);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1750
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1751 if (empty($diff)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1752 return $extension;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1755 // Make sure the mailbox isn't selected, before enabling extension(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1756 if ($this->selected !== null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1757 $this->close();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1758 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1759 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1760
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1761 list($code, $response) = $this->execute('ENABLE', $extension);
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 if ($code == self::ERROR_OK && preg_match('/^\* ENABLED /i', $response)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1764 $response = substr($response, 10); // remove prefix "* ENABLED "
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1765 $result = (array) $this->tokenizeResponse($response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1766
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1767 $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
1768
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1769 return $this->extensions_enabled;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1770 }
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 false;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1776 * Executes SORT command
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1779 * @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
1780 * @param string $criteria Searching criteria
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1781 * @param bool $return_uid Enables UID SORT usage
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1782 * @param string $encoding Character set
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1783 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1784 * @return rcube_result_index Response data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1785 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1786 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
1787 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1788 $old_sel = $this->selected;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1789 $supported = array('ARRIVAL', 'CC', 'DATE', 'FROM', 'SIZE', 'SUBJECT', 'TO');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1790 $field = strtoupper($field);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1791
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1792 if ($field == 'INTERNALDATE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1793 $field = 'ARRIVAL';
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1796 if (!in_array($field, $supported)) {
5
3a5f959af5ae debugging aids, all commented out
Charlie Root
parents: 0
diff changeset
1797 #rcube::write_log('mail',"$field not supported: $mailbox fallback");
0
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1798 return new rcube_result_index($mailbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1799 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1800
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1801 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1802 return new rcube_result_index($mailbox);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1805 // 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
1806 if ($old_sel != $mailbox && !$this->data['EXISTS']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1807 return new rcube_result_index($mailbox, '* SORT');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1808 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1809
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1810 // RFC 5957: SORT=DISPLAY
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1811 if (($field == 'FROM' || $field == 'TO') && $this->getCapability('SORT=DISPLAY')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1812 $field = 'DISPLAY' . $field;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1813 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1814
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1815 $encoding = $encoding ? trim($encoding) : 'US-ASCII';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1816 $criteria = $criteria ? 'ALL ' . trim($criteria) : 'ALL';
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 list($code, $response) = $this->execute($return_uid ? 'UID SORT' : 'SORT',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1819 array("($field)", $encoding, $criteria));
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 if ($code != self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1822 $response = null;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1825 return new rcube_result_index($mailbox, $response);
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1829 * Executes THREAD command
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1832 * @param string $algorithm Threading algorithm (ORDEREDSUBJECT, REFERENCES, REFS)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1833 * @param string $criteria Searching criteria
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1834 * @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
1835 * @param string $encoding Character set
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1836 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1837 * @return rcube_result_thread Thread data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1838 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1839 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
1840 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1841 $old_sel = $this->selected;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1842
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1843 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1844 return new rcube_result_thread($mailbox);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1847 // 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
1848 if ($old_sel != $mailbox && !$this->data['EXISTS']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1849 return new rcube_result_thread($mailbox, '* THREAD');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1850 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1851
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1852 $encoding = $encoding ? trim($encoding) : 'US-ASCII';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1853 $algorithm = $algorithm ? trim($algorithm) : 'REFERENCES';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1854 $criteria = $criteria ? 'ALL '.trim($criteria) : 'ALL';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1855
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1856 list($code, $response) = $this->execute($return_uid ? 'UID THREAD' : 'THREAD',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1857 array($algorithm, $encoding, $criteria));
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 if ($code != self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1860 $response = null;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1863 return new rcube_result_thread($mailbox, $response);
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1867 * Executes SEARCH command
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1870 * @param string $criteria Searching criteria
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1871 * @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
1872 * @param array $items Return items (MIN, MAX, COUNT, ALL)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1873 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1874 * @return rcube_result_index Result data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1875 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1876 public function search($mailbox, $criteria, $return_uid = false, $items = array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1877 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1878 $old_sel = $this->selected;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1879
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1880 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1881 return new rcube_result_index($mailbox);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1884 // 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
1885 if ($old_sel != $mailbox && !$this->data['EXISTS']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1886 return new rcube_result_index($mailbox, '* SEARCH');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1887 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1888
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1889 // If ESEARCH is supported always use ALL
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1890 // but not when items are specified or using simple id2uid search
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1891 if (empty($items) && preg_match('/[^0-9]/', $criteria)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1892 $items = array('ALL');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1893 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1894
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1895 $esearch = empty($items) ? false : $this->getCapability('ESEARCH');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1896 $criteria = trim($criteria);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1897 $params = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1898
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1899 // RFC4731: ESEARCH
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1900 if (!empty($items) && $esearch) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1901 $params .= 'RETURN (' . implode(' ', $items) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1902 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1903
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1904 if (!empty($criteria)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1905 $params .= ($params ? ' ' : '') . $criteria;
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 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1908 $params .= 'ALL';
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1911 list($code, $response) = $this->execute($return_uid ? 'UID SEARCH' : 'SEARCH',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1912 array($params));
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 if ($code != self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1915 $response = null;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1918 return new rcube_result_index($mailbox, $response);
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1922 * Simulates SORT command by using FETCH and sorting.
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1925 * @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
1926 * @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
1927 * @param bool $skip_deleted Makes that DELETED messages will be skipped
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1928 * @param bool $uidfetch Enables UID FETCH usage
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1929 * @param bool $return_uid Enables returning UIDs instead of IDs
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1930 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1931 * @return rcube_result_index Response data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1932 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1933 public function index($mailbox, $message_set, $index_field='', $skip_deleted=true,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1934 $uidfetch=false, $return_uid=false)
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 $msg_index = $this->fetchHeaderIndex($mailbox, $message_set,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1937 $index_field, $skip_deleted, $uidfetch, $return_uid);
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 if (!empty($msg_index)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1940 asort($msg_index); // ASC
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1941 $msg_index = array_keys($msg_index);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1942 $msg_index = '* SEARCH ' . implode(' ', $msg_index);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1943 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1944 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1945 $msg_index = is_array($msg_index) ? '* SEARCH' : null;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1948 return new rcube_result_index($mailbox, $msg_index);
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1952 * Fetches specified header/data value for a set of messages.
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1955 * @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
1956 * @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
1957 * @param bool $skip_deleted Makes that DELETED messages will be skipped
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1958 * @param bool $uidfetch Enables UID FETCH usage
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1959 * @param bool $return_uid Enables returning UIDs instead of IDs
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1960 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1961 * @return array|bool List of header values or False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1962 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1963 public function fetchHeaderIndex($mailbox, $message_set, $index_field = '', $skip_deleted = true,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1964 $uidfetch = false, $return_uid = false)
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 if (is_array($message_set)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1967 if (!($message_set = $this->compressMessageSet($message_set))) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1968 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1969 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1970 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1971 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1972 list($from_idx, $to_idx) = explode(':', $message_set);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1973 if (empty($message_set) ||
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1974 (isset($to_idx) && $to_idx != '*' && (int)$from_idx > (int)$to_idx)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1975 ) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1976 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1977 }
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1980 $index_field = empty($index_field) ? 'DATE' : strtoupper($index_field);
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 $fields_a['DATE'] = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1983 $fields_a['INTERNALDATE'] = 4;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1984 $fields_a['ARRIVAL'] = 4;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1985 $fields_a['FROM'] = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1986 $fields_a['REPLY-TO'] = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1987 $fields_a['SENDER'] = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1988 $fields_a['TO'] = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1989 $fields_a['CC'] = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1990 $fields_a['SUBJECT'] = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1991 $fields_a['UID'] = 2;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1992 $fields_a['SIZE'] = 2;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1993 $fields_a['SEEN'] = 3;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1994 $fields_a['RECENT'] = 3;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1995 $fields_a['DELETED'] = 3;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1996
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1997 if (!($mode = $fields_a[$index_field])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
1998 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2001 // Select the mailbox
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2002 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2003 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2004 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2005
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2006 // build FETCH command string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2007 $key = $this->nextTag();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2008 $cmd = $uidfetch ? 'UID FETCH' : 'FETCH';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2009 $fields = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2010
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2011 if ($return_uid) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2012 $fields[] = 'UID';
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 ($skip_deleted) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2015 $fields[] = 'FLAGS';
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2018 if ($mode == 1) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2019 if ($index_field == 'DATE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2020 $fields[] = 'INTERNALDATE';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2021 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2022 $fields[] = "BODY.PEEK[HEADER.FIELDS ($index_field)]";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2023 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2024 else if ($mode == 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2025 if ($index_field == 'SIZE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2026 $fields[] = 'RFC822.SIZE';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2027 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2028 else if (!$return_uid || $index_field != 'UID') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2029 $fields[] = $index_field;
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2032 else if ($mode == 3 && !$skip_deleted) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2033 $fields[] = 'FLAGS';
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 == 4) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2036 $fields[] = 'INTERNALDATE';
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2039 $request = "$key $cmd $message_set (" . implode(' ', $fields) . ")";
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 if (!$this->putLine($request)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2042 $this->setError(self::ERROR_COMMAND, "Failed to send $cmd command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2043 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2044 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2045
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2046 $result = array();
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 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2049 $line = rtrim($this->readLine(200));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2050 $line = $this->multLine($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2051
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2052 if (preg_match('/^\* ([0-9]+) FETCH/', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2053 $id = $m[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2054 $flags = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2055
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2056 if ($return_uid) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2057 if (preg_match('/UID ([0-9]+)/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2058 $id = (int) $matches[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2059 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2060 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2061 continue;
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2064 if ($skip_deleted && preg_match('/FLAGS \(([^)]+)\)/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2065 $flags = explode(' ', strtoupper($matches[1]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2066 if (in_array('\\DELETED', $flags)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2067 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2068 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2069 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2070
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2071 if ($mode == 1 && $index_field == 'DATE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2072 if (preg_match('/BODY\[HEADER\.FIELDS \("*DATE"*\)\] (.*)/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2073 $value = preg_replace(array('/^"*[a-z]+:/i'), '', $matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2074 $value = trim($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2075 $result[$id] = rcube_utils::strtotime($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2076 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2077 // non-existent/empty Date: header, use INTERNALDATE
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2078 if (empty($result[$id])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2079 if (preg_match('/INTERNALDATE "([^"]+)"/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2080 $result[$id] = rcube_utils::strtotime($matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2081 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2082 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2083 $result[$id] = 0;
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2086 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2087 else if ($mode == 1) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2088 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
2089 $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
2090 $result[$id] = trim($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2091 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2092 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2093 $result[$id] = '';
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2096 else if ($mode == 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2097 if (preg_match('/' . $index_field . ' ([0-9]+)/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2098 $result[$id] = trim($matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2099 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2100 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2101 $result[$id] = 0;
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2104 else if ($mode == 3) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2105 if (!$flags && preg_match('/FLAGS \(([^)]+)\)/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2106 $flags = explode(' ', $matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2107 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2108 $result[$id] = in_array("\\".$index_field, (array) $flags) ? 1 : 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2109 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2110 else if ($mode == 4) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2111 if (preg_match('/INTERNALDATE "([^"]+)"/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2112 $result[$id] = rcube_utils::strtotime($matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2113 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2114 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2115 $result[$id] = 0;
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2118 }
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 while (!$this->startsWith($line, $key, true, true));
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 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2123 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2126 * Returns message sequence identifier
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2129 * @param int $uid Message unique identifier (UID)
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 * @return int Message sequence identifier
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2132 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2133 public function UID2ID($mailbox, $uid)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2134 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2135 if ($uid > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2136 $index = $this->search($mailbox, "UID $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 ($index->count() == 1) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2139 $arr = $index->get();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2140 return (int) $arr[0];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2141 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2142 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2143 }
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 * Returns message unique identifier (UID)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2149 * @param int $uid Message sequence identifier
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 * @return int Message unique identifier
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2152 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2153 public function ID2UID($mailbox, $id)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2154 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2155 if (empty($id) || $id < 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2156 return null;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2159 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2160 return null;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2163 if ($uid = $this->data['UID-MAP'][$id]) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2164 return $uid;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2167 if (isset($this->data['EXISTS']) && $id > $this->data['EXISTS']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2168 return null;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2171 $index = $this->search($mailbox, $id, true);
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 if ($index->count() == 1) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2174 $arr = $index->get();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2175 return $this->data['UID-MAP'][$id] = (int) $arr[0];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2176 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2177 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2178
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 * Sets flag of the message(s)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2183 * @param string|array $messages Message UID(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2184 * @param string $flag Flag name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2185 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2186 * @return bool True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2187 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2188 public function flag($mailbox, $messages, $flag)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2189 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2190 return $this->modFlag($mailbox, $messages, $flag, '+');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2191 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2194 * Unsets flag of the message(s)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2197 * @param string|array $messages Message UID(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2198 * @param string $flag Flag name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2199 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2200 * @return bool True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2201 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2202 public function unflag($mailbox, $messages, $flag)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2203 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2204 return $this->modFlag($mailbox, $messages, $flag, '-');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2205 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2208 * Changes flag of the message(s)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2211 * @param string|array $messages Message UID(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2212 * @param string $flag Flag name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2213 * @param string $mod Modifier [+|-]. Default: "+".
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2214 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2215 * @return bool True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2216 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2217 protected function modFlag($mailbox, $messages, $flag, $mod = '+')
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2218 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2219 if (!$flag) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2220 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2223 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2224 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2227 if (!$this->data['READ-WRITE']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2228 $this->setError(self::ERROR_READONLY, "Mailbox is read-only");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2229 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2230 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2231
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2232 if ($this->flags[strtoupper($flag)]) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2233 $flag = $this->flags[strtoupper($flag)];
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2236 // if PERMANENTFLAGS is not specified all flags are allowed
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2237 if (!empty($this->data['PERMANENTFLAGS'])
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2238 && !in_array($flag, (array) $this->data['PERMANENTFLAGS'])
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2239 && !in_array('\\*', (array) $this->data['PERMANENTFLAGS'])
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2240 ) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2241 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2242 }
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 // Clear internal status cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2245 if ($flag == 'SEEN') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2246 unset($this->data['STATUS:'.$mailbox]['UNSEEN']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2247 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2248
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2249 if ($mod != '+' && $mod != '-') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2250 $mod = '+';
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2253 $result = $this->execute('UID STORE', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2254 $this->compressMessageSet($messages), $mod . 'FLAGS.SILENT', "($flag)"),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2255 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2256
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2257 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2258 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2261 * Copies message(s) from one folder to another
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 * @param string|array $messages Message UID(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2264 * @param string $from Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2265 * @param string $to Destination mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2266 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2267 * @return bool True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2268 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2269 public function copy($messages, $from, $to)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2270 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2271 // Clear last COPYUID data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2272 unset($this->data['COPYUID']);
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 if (!$this->select($from)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2275 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2278 // Clear internal status cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2279 unset($this->data['STATUS:'.$to]);
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 $result = $this->execute('UID COPY', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2282 $this->compressMessageSet($messages), $this->escape($to)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2283 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2284
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2285 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2286 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2289 * Moves message(s) from one folder to another.
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 * @param string|array $messages Message UID(s)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2292 * @param string $from Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2293 * @param string $to Destination mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2294 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2295 * @return bool True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2296 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2297 public function move($messages, $from, $to)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2298 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2299 if (!$this->select($from)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2300 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2303 if (!$this->data['READ-WRITE']) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2304 $this->setError(self::ERROR_READONLY, "Mailbox is read-only");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2305 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2306 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2307
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2308 // use MOVE command (RFC 6851)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2309 if ($this->hasCapability('MOVE')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2310 // Clear last COPYUID data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2311 unset($this->data['COPYUID']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2312
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2313 // Clear internal status cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2314 unset($this->data['STATUS:'.$to]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2315 $this->clear_status_cache($from);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2316
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2317 $result = $this->execute('UID MOVE', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2318 $this->compressMessageSet($messages), $this->escape($to)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2319 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2320
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2321 return $result == self::ERROR_OK;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2322 }
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 // use COPY + STORE +FLAGS.SILENT \Deleted + EXPUNGE
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2325 $result = $this->copy($messages, $from, $to);
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 if ($result) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2328 // Clear internal status cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2329 unset($this->data['STATUS:'.$from]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2330
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2331 $result = $this->flag($from, $messages, 'DELETED');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2332
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2333 if ($messages == '*') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2334 // CLOSE+SELECT should be faster than EXPUNGE
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2335 $this->close();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2336 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2337 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2338 $this->expunge($from, $messages);
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2341
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2342 return $result;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2346 * FETCH command (RFC3501)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2349 * @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
2350 * @param bool $is_uid True if $message_set contains UIDs
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2351 * @param array $query_items FETCH command data items
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2352 * @param string $mod_seq Modification sequence for CHANGEDSINCE (RFC4551) query
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2353 * @param bool $vanished Enables VANISHED parameter (RFC5162) for CHANGEDSINCE query
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2354 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2355 * @return array List of rcube_message_header elements, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2356 * @since 0.6
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 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
2359 $mod_seq = null, $vanished = false)
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 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2362 return 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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2365 $message_set = $this->compressMessageSet($message_set);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2366 $result = array();
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 $key = $this->nextTag();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2369 $cmd = ($is_uid ? 'UID ' : '') . 'FETCH';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2370 $request = "$key $cmd $message_set (" . implode(' ', $query_items) . ")";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2371
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2372 if ($mod_seq !== null && $this->hasCapability('CONDSTORE')) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2373 $request .= " (CHANGEDSINCE $mod_seq" . ($vanished ? " VANISHED" : '') .")";
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2376 if (!$this->putLine($request)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2377 $this->setError(self::ERROR_COMMAND, "Failed to send $cmd command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2378 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2379 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2380
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2381 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2382 $line = $this->readLine(4096);
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 if (!$line) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2385 break;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2388 // Sample reply line:
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2389 // * 321 FETCH (UID 2417 RFC822.SIZE 2730 FLAGS (\Seen)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2390 // INTERNALDATE "16-Nov-2008 21:08:46 +0100" BODYSTRUCTURE (...)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2391 // BODY[HEADER.FIELDS ...
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2392
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2393 if (preg_match('/^\* ([0-9]+) FETCH/', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2394 $id = intval($m[1]);
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 $result[$id] = new rcube_message_header;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2397 $result[$id]->id = $id;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2398 $result[$id]->subject = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2399 $result[$id]->messageID = 'mid:' . $id;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2400
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2401 $headers = null;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2402 $lines = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2403 $line = substr($line, strlen($m[0]) + 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2404 $ln = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2405
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2406 // get complete entry
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2407 while (preg_match('/\{([0-9]+)\}\r\n$/', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2408 $bytes = $m[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2409 $out = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2410
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2411 while (strlen($out) < $bytes) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2412 $out = $this->readBytes($bytes);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2413 if ($out === null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2414 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2415 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2416 $line .= $out;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2417 }
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 $str = $this->readLine(4096);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2420 if ($str === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2421 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2422 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2423
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2424 $line .= $str;
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 // Tokenize response and assign to object properties
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2428 while (list($name, $value) = $this->tokenizeResponse($line, 2)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2429 if ($name == 'UID') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2430 $result[$id]->uid = intval($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2431 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2432 else if ($name == 'RFC822.SIZE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2433 $result[$id]->size = 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.TEXT') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2436 $result[$id]->body = $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 == 'INTERNALDATE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2439 $result[$id]->internaldate = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2440 $result[$id]->date = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2441 $result[$id]->timestamp = rcube_utils::strtotime($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2442 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2443 else if ($name == 'FLAGS') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2444 if (!empty($value)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2445 foreach ((array)$value as $flag) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2446 $flag = str_replace(array('$', "\\"), '', $flag);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2447 $flag = strtoupper($flag);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2448
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2449 $result[$id]->flags[$flag] = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2450 }
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2453 else if ($name == 'MODSEQ') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2454 $result[$id]->modseq = $value[0];
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 == 'ENVELOPE') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2457 $result[$id]->envelope = $value;
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 == 'BODYSTRUCTURE' || ($name == 'BODY' && count($value) > 2)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2460 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
2461 $value = array($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2462 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2463 $result[$id]->bodystructure = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2464 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2465 else if ($name == 'RFC822') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2466 $result[$id]->body = $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 (stripos($name, 'BODY[') === 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2469 $name = str_replace(']', '', substr($name, 5));
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 if ($name == 'HEADER.FIELDS') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2472 // skip ']' after headers list
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2473 $this->tokenizeResponse($line, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2474 $headers = $this->tokenizeResponse($line, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2475 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2476 else if (strlen($name)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2477 $result[$id]->bodypart[$name] = $value;
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 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2480 $result[$id]->body = $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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2483 }
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 // create array with header field:data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2486 if (!empty($headers)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2487 $headers = explode("\n", trim($headers));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2488 foreach ($headers as $resln) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2489 if (ord($resln[0]) <= 32) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2490 $lines[$ln] .= (empty($lines[$ln]) ? '' : "\n") . trim($resln);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2491 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2492 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2493 $lines[++$ln] = 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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2496
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2497 foreach ($lines as $str) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2498 list($field, $string) = explode(':', $str, 2);
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 $field = strtolower($field);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2501 $string = preg_replace('/\n[\t\s]*/', ' ', trim($string));
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 switch ($field) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2504 case 'date';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2505 $result[$id]->date = $string;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2506 $result[$id]->timestamp = rcube_utils::strtotime($string);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2507 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2508 case 'to':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2509 $result[$id]->to = preg_replace('/undisclosed-recipients:[;,]*/', '', $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 'from':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2512 case 'subject':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2513 case 'cc':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2514 case 'bcc':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2515 case 'references':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2516 $result[$id]->{$field} = $string;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2517 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2518 case 'reply-to':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2519 $result[$id]->replyto = $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 'content-transfer-encoding':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2522 $result[$id]->encoding = $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-type':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2525 $ctype_parts = preg_split('/[; ]+/', $string);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2526 $result[$id]->ctype = strtolower(array_shift($ctype_parts));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2527 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
2528 $result[$id]->charset = $regs[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2529 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2530 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2531 case 'in-reply-to':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2532 $result[$id]->in_reply_to = str_replace(array("\n", '<', '>'), '', $string);
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 'return-receipt-to':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2535 case 'disposition-notification-to':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2536 case 'x-confirm-reading-to':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2537 $result[$id]->mdn_to = $string;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2538 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2539 case 'message-id':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2540 $result[$id]->messageID = $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 'x-priority':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2543 if (preg_match('/^(\d+)/', $string, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2544 $result[$id]->priority = intval($matches[1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2545 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2546 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2547 default:
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2548 if (strlen($field) < 3) {
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2551 if ($result[$id]->others[$field]) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2552 $string = array_merge((array)$result[$id]->others[$field], (array)$string);
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 $result[$id]->others[$field] = $string;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2555 }
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 }
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 // VANISHED response (QRESYNC RFC5162)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2560 // Sample: * VANISHED (EARLIER) 300:310,405,411
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2561 else if (preg_match('/^\* VANISHED [()EARLIER]*/i', $line, $match)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2562 $line = substr($line, strlen($match[0]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2563 $v_data = $this->tokenizeResponse($line, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2564
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2565 $this->data['VANISHED'] = $v_data;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2566 }
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 while (!$this->startsWith($line, $key, true));
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 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2571 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2574 * Returns message(s) data (flags, headers, etc.)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2577 * @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
2578 * @param bool $is_uid True if $message_set contains UIDs
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2579 * @param bool $bodystr Enable to add BODYSTRUCTURE data to the result
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2580 * @param array $add_headers List of additional headers
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2581 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2582 * @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
2583 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2584 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
2585 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2586 $query_items = array('UID', 'RFC822.SIZE', 'FLAGS', 'INTERNALDATE');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2587 $headers = array('DATE', 'FROM', 'TO', 'SUBJECT', 'CONTENT-TYPE', 'CC', 'REPLY-TO',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2588 'LIST-POST', 'DISPOSITION-NOTIFICATION-TO', 'X-PRIORITY');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2589
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2590 if (!empty($add_headers)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2591 $add_headers = array_map('strtoupper', $add_headers);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2592 $headers = array_unique(array_merge($headers, $add_headers));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2593 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2594
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2595 if ($bodystr) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2596 $query_items[] = 'BODYSTRUCTURE';
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2599 $query_items[] = 'BODY.PEEK[HEADER.FIELDS (' . implode(' ', $headers) . ')]';
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 return $this->fetch($mailbox, $message_set, $is_uid, $query_items);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2602 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2605 * Returns message data (flags, headers, etc.)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2608 * @param int $id Message sequence identifier or UID
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2609 * @param bool $is_uid True if $id is an UID
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2610 * @param bool $bodystr Enable to add BODYSTRUCTURE data to the result
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2611 * @param array $add_headers List of additional headers
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2612 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2613 * @return bool|rcube_message_header Message data, False on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2614 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2615 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
2616 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2617 $a = $this->fetchHeaders($mailbox, $id, $is_uid, $bodystr, $add_headers);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2618 if (is_array($a)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2619 return array_shift($a);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2620 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2621
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2622 return false;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2626 * Sort messages by specified header field
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 * @param array $messages Array of rcube_message_header objects
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2629 * @param string $field Name of the property to sort by
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2630 * @param string $flag Sorting order (ASC|DESC)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2631 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2632 * @return array Sorted input array
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2633 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2634 public static function sortHeaders($messages, $field, $flag)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2635 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2636 // Strategy: First, we'll create an "index" array.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2637 // 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
2638
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2639 $field = empty($field) ? 'uid' : strtolower($field);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2640 $flag = empty($flag) ? 'ASC' : strtoupper($flag);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2641 $index = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2642 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2643
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2644 reset($messages);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2645
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2646 foreach ($messages as $key => $headers) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2647 $value = null;
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 switch ($field) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2650 case 'arrival':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2651 $field = 'internaldate';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2652 case 'date':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2653 case 'internaldate':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2654 case 'timestamp':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2655 $value = rcube_utils::strtotime($headers->$field);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2656 if (!$value && $field != 'timestamp') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2657 $value = $headers->timestamp;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2658 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2659
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2660 break;
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 default:
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2663 // @TODO: decode header value, convert to UTF-8
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2664 $value = $headers->$field;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2665 if (is_string($value)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2666 $value = str_replace('"', '', $value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2667 if ($field == 'subject') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2668 $value = preg_replace('/^(Re:\s*|Fwd:\s*|Fw:\s*)+/i', '', $value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2669 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2670
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2671 $data = strtoupper($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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2675 $index[$key] = $value;
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 if (!empty($index)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2679 // sort index
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2680 if ($flag == 'ASC') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2681 asort($index);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2682 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2683 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2684 arsort($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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2687 // form new array based on index
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2688 foreach ($index as $key => $val) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2689 $result[$key] = $messages[$key];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2690 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2691 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2692
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2693 return $result;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2697 * Fetch MIME headers of specified message parts
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2700 * @param int $uid Message UID
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2701 * @param array $parts Message part identifiers
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2702 * @param bool $mime Use MIME instad of HEADER
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2703 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2704 * @return array|bool Array containing headers string for each specified body
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2705 * False on failure.
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 public function fetchMIMEHeaders($mailbox, $uid, $parts, $mime = true)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2708 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2709 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2710 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2713 $result = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2714 $parts = (array) $parts;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2715 $key = $this->nextTag();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2716 $peeks = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2717 $type = $mime ? 'MIME' : 'HEADER';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2718
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2719 // format request
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2720 foreach ($parts as $part) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2721 $peeks[] = "BODY.PEEK[$part.$type]";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2722 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2723
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2724 $request = "$key UID FETCH $uid (" . implode(' ', $peeks) . ')';
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 // send request
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2727 if (!$this->putLine($request)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2728 $this->setError(self::ERROR_COMMAND, "Failed to send UID FETCH command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2729 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2730 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2731
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2732 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2733 $line = $this->readLine(1024);
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 if (preg_match('/^\* [0-9]+ FETCH [0-9UID( ]+/', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2736 $line = ltrim(substr($line, strlen($m[0])));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2737 while (preg_match('/^BODY\[([0-9\.]+)\.'.$type.'\]/', $line, $matches)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2738 $line = substr($line, strlen($matches[0]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2739 $result[$matches[1]] = trim($this->multLine($line));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2740 $line = $this->readLine(1024);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2741 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2742 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2743 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2744 while (!$this->startsWith($line, $key, true));
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 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2747 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2750 * Fetches message part header
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 public function fetchPartHeader($mailbox, $id, $is_uid = false, $part = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2753 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2754 $part = empty($part) ? 'HEADER' : $part.'.MIME';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2755
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2756 return $this->handlePartBody($mailbox, $id, $is_uid, $part);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2757 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2760 * Fetches body of the specified message part
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 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
2763 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2764 if (!$this->select($mailbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2765 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2768 $binary = true;
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 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2771 if (!$initiated) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2772 switch ($encoding) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2773 case 'base64':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2774 $mode = 1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2775 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2776 case 'quoted-printable':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2777 $mode = 2;
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 'x-uuencode':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2780 case 'x-uue':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2781 case 'uue':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2782 case 'uuencode':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2783 $mode = 3;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2784 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2785 default:
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2786 $mode = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2787 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2788
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2789 // Use BINARY extension when possible (and safe)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2790 $binary = $binary && $mode && preg_match('/^[0-9.]+$/', $part) && $this->hasCapability('BINARY');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2791 $fetch_mode = $binary ? 'BINARY' : 'BODY';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2792 $partial = $max_bytes ? sprintf('<0.%d>', $max_bytes) : '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2793
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2794 // format request
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2795 $key = $this->nextTag();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2796 $cmd = ($is_uid ? 'UID ' : '') . 'FETCH';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2797 $request = "$key $cmd $id ($fetch_mode.PEEK[$part]$partial)";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2798 $result = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2799 $found = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2800 $initiated = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2801
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2802 // send request
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2803 if (!$this->putLine($request)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2804 $this->setError(self::ERROR_COMMAND, "Failed to send $cmd command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2805 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2806 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2807
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2808 if ($binary) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2809 // 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
2810 $mode = -1;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2811 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2812 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2813
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2814 $line = trim($this->readLine(1024));
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 if (!$line) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2817 break;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2820 // 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
2821 if ($binary && !$found && preg_match('/^' . $key . ' NO \[UNKNOWN-CTE\]/i', $line)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2822 $binary = $initiated = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2823 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2824 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2825
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2826 // skip irrelevant untagged responses (we have a result already)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2827 if ($found || !preg_match('/^\* ([0-9]+) FETCH (.*)$/', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2828 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2829 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2830
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2831 $line = $m[2];
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 // handle one line response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2834 if ($line[0] == '(' && substr($line, -1) == ')') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2835 // tokenize content inside brackets
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2836 // 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
2837 $tokens = $this->tokenizeResponse(preg_replace('/(^\(|\)$)/', '', $line));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2838
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2839 for ($i=0; $i<count($tokens); $i+=2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2840 if (preg_match('/^(BODY|BINARY)/i', $tokens[$i])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2841 $result = $tokens[$i+1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2842 $found = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2843 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2844 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2845 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2846
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2847 if ($result !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2848 if ($mode == 1) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2849 $result = base64_decode($result);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2850 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2851 else if ($mode == 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2852 $result = quoted_printable_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 == 3) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2855 $result = convert_uudecode($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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2858 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2859 // response with string literal
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2860 else if (preg_match('/\{([0-9]+)\}$/', $line, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2861 $bytes = (int) $m[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2862 $prev = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2863 $found = true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2864
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2865 // empty body
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2866 if (!$bytes) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2867 $result = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2868 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2869 else while ($bytes > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2870 $line = $this->readLine(8192);
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 if ($line === null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2873 break;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2876 $len = strlen($line);
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 if ($len > $bytes) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2879 $line = substr($line, 0, $bytes);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2880 $len = strlen($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2881 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2882 $bytes -= $len;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2883
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2884 // BASE64
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2885 if ($mode == 1) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2886 $line = preg_replace('|[^a-zA-Z0-9+=/]|', '', $line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2887 // create chunks with proper length for base64 decoding
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2888 $line = $prev.$line;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2889 $length = strlen($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2890 if ($length % 4) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2891 $length = floor($length / 4) * 4;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2892 $prev = substr($line, $length);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2893 $line = substr($line, 0, $length);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2894 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2895 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2896 $prev = '';
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 $line = base64_decode($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2899 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2900 // QUOTED-PRINTABLE
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2901 else if ($mode == 2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2902 $line = rtrim($line, "\t\r\0\x0B");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2903 $line = quoted_printable_decode($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2904 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2905 // UUENCODE
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2906 else if ($mode == 3) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2907 $line = rtrim($line, "\t\r\n\0\x0B");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2908 if ($line == 'end' || preg_match('/^begin\s+[0-7]+\s+.+$/', $line)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2909 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2910 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2911 $line = convert_uudecode($line);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2912 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2913 // default
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2914 else if ($formatted) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2915 $line = rtrim($line, "\t\r\n\0\x0B") . "\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2916 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2917
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2918 if ($file) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2919 if (fwrite($file, $line) === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2920 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2921 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2922 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2923 else if ($print) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2924 echo $line;
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 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2927 $result .= $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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2930 }
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 while (!$this->startsWith($line, $key, true) || !$initiated);
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 if ($result !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2935 if ($file) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2936 return fwrite($file, $result);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2937 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2938 else if ($print) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2939 echo $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2940 return true;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2941 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2942
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2943 return $result;
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 false;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2950 * Handler for IMAP APPEND command
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2953 * @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
2954 * @param array $flags Message flags
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2955 * @param string $date Message internal date
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2956 * @param bool $binary Enable BINARY append (RFC3516)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2957 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2958 * @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
2959 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2960 public function append($mailbox, &$message, $flags = array(), $date = null, $binary = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2961 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2962 unset($this->data['APPENDUID']);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2963
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2964 if ($mailbox === null || $mailbox === '') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2965 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2968 $binary = $binary && $this->getCapability('BINARY');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2969 $literal_plus = !$binary && $this->prefs['literal+'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2970 $len = 0;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2971 $msg = is_array($message) ? $message : array(&$message);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2972 $chunk_size = 512000;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2973
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2974 for ($i=0, $cnt=count($msg); $i<$cnt; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2975 if (is_resource($msg[$i])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2976 $stat = fstat($msg[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2977 if ($stat === false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2978 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2979 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2980 $len += $stat['size'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2981 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2982 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2983 if (!$binary) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2984 $msg[$i] = str_replace("\r", '', $msg[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2985 $msg[$i] = str_replace("\n", "\r\n", $msg[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2986 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2987
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2988 $len += strlen($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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2992 if (!$len) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2993 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2996 // build APPEND command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2997 $key = $this->nextTag();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2998 $request = "$key APPEND " . $this->escape($mailbox) . ' (' . $this->flagsToStr($flags) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
2999 if (!empty($date)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3000 $request .= ' ' . $this->escape($date);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3001 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3002 $request .= ' ' . ($binary ? '~' : '') . '{' . $len . ($literal_plus ? '+' : '') . '}';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3003
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3004 // send APPEND command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3005 if (!$this->putLine($request)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3006 $this->setError(self::ERROR_COMMAND, "Failed to send APPEND command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3007 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3008 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3009
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3010 // Do not wait when LITERAL+ is supported
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3011 if (!$literal_plus) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3012 $line = $this->readReply();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3013
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3014 if ($line[0] != '+') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3015 $this->parseResult($line, 'APPEND: ');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3016 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3017 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3018 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3019
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3020 foreach ($msg as $msg_part) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3021 // file pointer
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3022 if (is_resource($msg_part)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3023 rewind($msg_part);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3024 while (!feof($msg_part) && $this->fp) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3025 $buffer = fread($msg_part, $chunk_size);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3026 $this->putLine($buffer, false);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3027 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3028 fclose($msg_part);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3029 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3030 // string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3031 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3032 $size = strlen($msg_part);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3033
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3034 // 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
3035 // This approach reduces our peak memory usage
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3036 for ($offset = 0; $offset < $size; $offset += $chunk_size) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3037 $chunk = substr($msg_part, $offset, $chunk_size);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3038 if (!$this->putLine($chunk, false)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3039 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3040 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3041 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3042 }
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 if (!$this->putLine('')) { // \r\n
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3046 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3049 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3050 $line = $this->readLine();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3051 } while (!$this->startsWith($line, $key, true, true));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3052
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3053 // Clear internal status cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3054 unset($this->data['STATUS:'.$mailbox]);
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 if ($this->parseResult($line, 'APPEND: ') != self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3057 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3060 if (!empty($this->data['APPENDUID'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3061 return $this->data['APPENDUID'];
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3064 return true;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3068 * Handler for IMAP APPEND command.
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3071 * @param string $path Path to the file with message body
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3072 * @param string $headers Message headers
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3073 * @param array $flags Message flags
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3074 * @param string $date Message internal date
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3075 * @param bool $binary Enable BINARY append (RFC3516)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3076 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3077 * @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
3078 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3079 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
3080 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3081 // open message file
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3082 if (file_exists(realpath($path))) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3083 $fp = fopen($path, 'r');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3084 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3085
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3086 if (!$fp) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3087 $this->setError(self::ERROR_UNKNOWN, "Couldn't open $path for reading");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3088 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3089 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3090
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3091 $message = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3092 if ($headers) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3093 $message[] = trim($headers, "\r\n") . "\r\n\r\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3094 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3095 $message[] = $fp;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3096
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3097 return $this->append($mailbox, $message, $flags, $date, $binary);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3098 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3101 * Returns QUOTA information
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3104 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3105 * @return array Quota information
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3106 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3107 public function getQuota($mailbox = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3108 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3109 if ($mailbox === null || $mailbox === '') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3110 $mailbox = 'INBOX';
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3113 // a0001 GETQUOTAROOT INBOX
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3114 // * QUOTAROOT INBOX user/sample
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3115 // * QUOTA user/sample (STORAGE 654 9765)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3116 // a0001 OK Completed
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3117
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3118 list($code, $response) = $this->execute('GETQUOTAROOT', array($this->escape($mailbox)));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3119
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3120 $result = false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3121 $min_free = PHP_INT_MAX;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3122 $all = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3123
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3124 if ($code == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3125 foreach (explode("\n", $response) as $line) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3126 if (preg_match('/^\* QUOTA /', $line)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3127 list(, , $quota_root) = $this->tokenizeResponse($line, 3);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3128
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3129 while ($line) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3130 list($type, $used, $total) = $this->tokenizeResponse($line, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3131 $type = strtolower($type);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3132
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3133 if ($type && $total) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3134 $all[$quota_root][$type]['used'] = intval($used);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3135 $all[$quota_root][$type]['total'] = intval($total);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3136 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3137 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3138
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3139 if (empty($all[$quota_root]['storage'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3140 continue;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3143 $used = $all[$quota_root]['storage']['used'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3144 $total = $all[$quota_root]['storage']['total'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3145 $free = $total - $used;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3146
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3147 // calculate lowest available space from all storage quotas
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3148 if ($free < $min_free) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3149 $min_free = $free;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3150 $result['used'] = $used;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3151 $result['total'] = $total;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3152 $result['percent'] = min(100, round(($used/max(1,$total))*100));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3153 $result['free'] = 100 - $result['percent'];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3154 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3155 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3156 }
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 if (!empty($result)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3160 $result['all'] = $all;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3163 return $result;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3167 * Send the SETACL command (RFC4314)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3170 * @param string $user User name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3171 * @param mixed $acl ACL string or array
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3172 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3173 * @return boolean True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3174 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3175 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3176 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3177 public function setACL($mailbox, $user, $acl)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3178 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3179 if (is_array($acl)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3180 $acl = implode('', $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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3183 $result = $this->execute('SETACL', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3184 $this->escape($mailbox), $this->escape($user), strtolower($acl)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3185 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3186
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3187 return ($result == self::ERROR_OK);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3188 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3191 * Send the DELETEACL command (RFC4314)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3194 * @param string $user User name
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 * @return boolean True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3197 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3198 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3199 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3200 public function deleteACL($mailbox, $user)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3201 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3202 $result = $this->execute('DELETEACL', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3203 $this->escape($mailbox), $this->escape($user)),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3204 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3205
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3206 return ($result == self::ERROR_OK);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3207 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3210 * Send the GETACL command (RFC4314)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3213 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3214 * @return array User-rights array on success, NULL on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3215 * @since 0.5-beta
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 public function getACL($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3218 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3219 list($code, $response) = $this->execute('GETACL', array($this->escape($mailbox)));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3220
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3221 if ($code == self::ERROR_OK && preg_match('/^\* ACL /i', $response)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3222 // Parse server response (remove "* ACL ")
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3223 $response = substr($response, 6);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3224 $ret = $this->tokenizeResponse($response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3225 $mbox = array_shift($ret);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3226 $size = count($ret);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3227
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3228 // Create user-rights hash array
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3229 // @TODO: consider implementing fixACL() method according to RFC4314.2.1.1
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3230 // so we could return only standard rights defined in RFC4314,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3231 // excluding 'c' and 'd' defined in RFC2086.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3232 if ($size % 2 == 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3233 for ($i=0; $i<$size; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3234 $ret[$ret[$i]] = str_split($ret[++$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3235 unset($ret[$i-1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3236 unset($ret[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3237 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3238 return $ret;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3239 }
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 $this->setError(self::ERROR_COMMAND, "Incomplete ACL response");
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
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 * Send the LISTRIGHTS command (RFC4314)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3249 * @param string $user User name
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 * @return array List of user rights
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3252 * @since 0.5-beta
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 public function listRights($mailbox, $user)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3255 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3256 list($code, $response) = $this->execute('LISTRIGHTS', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3257 $this->escape($mailbox), $this->escape($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 if ($code == self::ERROR_OK && preg_match('/^\* LISTRIGHTS /i', $response)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3260 // Parse server response (remove "* LISTRIGHTS ")
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3261 $response = substr($response, 13);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3262
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3263 $ret_mbox = $this->tokenizeResponse($response, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3264 $ret_user = $this->tokenizeResponse($response, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3265 $granted = $this->tokenizeResponse($response, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3266 $optional = trim($response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3267
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3268 return array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3269 'granted' => str_split($granted),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3270 'optional' => explode(' ', $optional),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3271 );
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3272 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3273 }
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 * Send the MYRIGHTS command (RFC4314)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3279 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3280 * @return array MYRIGHTS response on success, NULL on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3281 * @since 0.5-beta
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 public function myRights($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3284 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3285 list($code, $response) = $this->execute('MYRIGHTS', array($this->escape($mailbox)));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3286
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3287 if ($code == self::ERROR_OK && preg_match('/^\* MYRIGHTS /i', $response)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3288 // Parse server response (remove "* MYRIGHTS ")
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3289 $response = substr($response, 11);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3290
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3291 $ret_mbox = $this->tokenizeResponse($response, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3292 $rights = $this->tokenizeResponse($response, 1);
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 return str_split($rights);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3295 }
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
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 * Send the SETMETADATA command (RFC5464)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3302 * @param array $entries Entry-value array (use NULL value as NIL)
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 * @return boolean True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3305 * @since 0.5-beta
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 public function setMetadata($mailbox, $entries)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3308 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3309 if (!is_array($entries) || empty($entries)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3310 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETMETADATA command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3311 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3312 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3313
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3314 foreach ($entries as $name => $value) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3315 $entries[$name] = $this->escape($name) . ' ' . $this->escape($value, true);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3318 $entries = implode(' ', $entries);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3319 $result = $this->execute('SETMETADATA', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3320 $this->escape($mailbox), '(' . $entries . ')'),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3321 self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3322
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3323 return ($result == self::ERROR_OK);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3324 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3327 * Send the SETMETADATA command with NIL values (RFC5464)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3330 * @param array $entries Entry names array
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 * @return boolean True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3333 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3334 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3335 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3336 public function deleteMetadata($mailbox, $entries)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3337 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3338 if (!is_array($entries) && !empty($entries)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3339 $entries = explode(' ', $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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3342 if (empty($entries)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3343 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETMETADATA command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3344 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3345 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3346
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3347 foreach ($entries as $entry) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3348 $data[$entry] = null;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3351 return $this->setMetadata($mailbox, $data);
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3355 * Send the GETMETADATA command (RFC5464)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3358 * @param array $entries Entries
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3359 * @param array $options Command options (with MAXSIZE and DEPTH keys)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3360 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3361 * @return array GETMETADATA result on success, NULL on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3362 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3363 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3364 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3365 public function getMetadata($mailbox, $entries, $options=array())
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3366 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3367 if (!is_array($entries)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3368 $entries = array($entries);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3371 // create entries string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3372 foreach ($entries as $idx => $name) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3373 $entries[$idx] = $this->escape($name);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3374 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3375
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3376 $optlist = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3377 $entlist = '(' . implode(' ', $entries) . ')';
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 // create options string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3380 if (is_array($options)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3381 $options = array_change_key_case($options, CASE_UPPER);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3382 $opts = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3383
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3384 if (!empty($options['MAXSIZE'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3385 $opts[] = 'MAXSIZE '.intval($options['MAXSIZE']);
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['DEPTH'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3388 $opts[] = 'DEPTH '.intval($options['DEPTH']);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3391 if ($opts) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3392 $optlist = '(' . implode(' ', $opts) . ')';
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3395
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3396 $optlist .= ($optlist ? ' ' : '') . $entlist;
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 list($code, $response) = $this->execute('GETMETADATA', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3399 $this->escape($mailbox), $optlist));
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 if ($code == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3402 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3403 $data = $this->tokenizeResponse($response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3404
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3405 // The METADATA response can contain multiple entries in a single
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3406 // response or multiple responses for each entry or group of entries
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3407 if (!empty($data) && ($size = count($data))) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3408 for ($i=0; $i<$size; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3409 if (isset($mbox) && is_array($data[$i])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3410 $size_sub = count($data[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3411 for ($x=0; $x<$size_sub; $x+=2) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3412 if ($data[$i][$x+1] !== null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3413 $result[$mbox][$data[$i][$x]] = $data[$i][$x+1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3414 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3415 unset($data[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3416 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3417 else if ($data[$i] == '*') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3418 if ($data[$i+1] == 'METADATA') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3419 $mbox = $data[$i+2];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3420 unset($data[$i]); // "*"
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3421 unset($data[++$i]); // "METADATA"
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3422 unset($data[++$i]); // Mailbox
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3423 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3424 // get rid of other untagged responses
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3425 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3426 unset($mbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3427 unset($data[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3428 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3429 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3430 else if (isset($mbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3431 if ($data[++$i] !== null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3432 $result[$mbox][$data[$i-1]] = $data[$i];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3433 unset($data[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3434 unset($data[$i-1]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3435 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3436 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3437 unset($data[$i]);
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3440 }
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 return $result;
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
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 * Send the SETANNOTATION command (draft-daboo-imap-annotatemore)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3450 * @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
3451 * three elements: entry name, attribute name, value
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3452 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3453 * @return boolean True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3454 * @since 0.5-beta
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 public function setAnnotation($mailbox, $data)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3457 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3458 if (!is_array($data) || empty($data)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3459 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETANNOTATION command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3460 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3461 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3462
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3463 foreach ($data as $entry) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3464 // ANNOTATEMORE drafts before version 08 require quoted parameters
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3465 $entries[] = sprintf('%s (%s %s)', $this->escape($entry[0], true),
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3466 $this->escape($entry[1], true), $this->escape($entry[2], true));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3467 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3468
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3469 $entries = implode(' ', $entries);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3470 $result = $this->execute('SETANNOTATION', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3471 $this->escape($mailbox), $entries), self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3472
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3473 return ($result == self::ERROR_OK);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3474 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3477 * Send the SETANNOTATION command with NIL values (draft-daboo-imap-annotatemore)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3480 * @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
3481 * two elements: entry name and attribute name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3482 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3483 * @return boolean True on success, False on failure
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3484 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3485 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3486 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3487 public function deleteAnnotation($mailbox, $data)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3488 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3489 if (!is_array($data) || empty($data)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3490 $this->setError(self::ERROR_COMMAND, "Wrong argument for SETANNOTATION command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3491 return false;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3492 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3493
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3494 return $this->setAnnotation($mailbox, $data);
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3498 * Send the GETANNOTATION command (draft-daboo-imap-annotatemore)
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 * @param string $mailbox Mailbox name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3501 * @param array $entries Entries names
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3502 * @param array $attribs Attribs names
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3503 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3504 * @return array Annotations result on success, NULL on error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3505 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3506 * @since 0.5-beta
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3507 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3508 public function getAnnotation($mailbox, $entries, $attribs)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3509 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3510 if (!is_array($entries)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3511 $entries = array($entries);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3514 // create entries string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3515 // ANNOTATEMORE drafts before version 08 require quoted parameters
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3516 foreach ($entries as $idx => $name) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3517 $entries[$idx] = $this->escape($name, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3518 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3519 $entries = '(' . implode(' ', $entries) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3520
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3521 if (!is_array($attribs)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3522 $attribs = array($attribs);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3525 // create attributes string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3526 foreach ($attribs as $idx => $name) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3527 $attribs[$idx] = $this->escape($name, true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3528 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3529 $attribs = '(' . implode(' ', $attribs) . ')';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3530
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3531 list($code, $response) = $this->execute('GETANNOTATION', array(
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3532 $this->escape($mailbox), $entries, $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 if ($code == self::ERROR_OK) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3535 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3536 $data = $this->tokenizeResponse($response);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3537
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3538 // Here we returns only data compatible with METADATA result format
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3539 if (!empty($data) && ($size = count($data))) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3540 for ($i=0; $i<$size; $i++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3541 $entry = $data[$i];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3542 if (isset($mbox) && is_array($entry)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3543 $attribs = $entry;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3544 $entry = $last_entry;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3545 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3546 else if ($entry == '*') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3547 if ($data[$i+1] == 'ANNOTATION') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3548 $mbox = $data[$i+2];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3549 unset($data[$i]); // "*"
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3550 unset($data[++$i]); // "ANNOTATION"
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3551 unset($data[++$i]); // Mailbox
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3552 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3553 // get rid of other untagged responses
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3554 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3555 unset($mbox);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3556 unset($data[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3557 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3558 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3559 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3560 else if (isset($mbox)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3561 $attribs = $data[++$i];
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 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3564 unset($data[$i]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3565 continue;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3566 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3567
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3568 if (!empty($attribs)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3569 for ($x=0, $len=count($attribs); $x<$len;) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3570 $attr = $attribs[$x++];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3571 $value = $attribs[$x++];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3572 if ($attr == 'value.priv' && $value !== null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3573 $result[$mbox]['/private' . $entry] = $value;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3574 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3575 else if ($attr == 'value.shared' && $value !== null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3576 $result[$mbox]['/shared' . $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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3579 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3580 $last_entry = $entry;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3581 unset($data[$i]);
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3584
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3585 return $result;
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
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 * Returns BODYSTRUCTURE for the specified message.
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 * @param string $mailbox Folder name
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3593 * @param int $id Message sequence number or UID
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3594 * @param bool $is_uid True if $id is an UID
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3595 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3596 * @return array/bool Body structure array or False on error.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3597 * @since 0.6
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 public function getStructure($mailbox, $id, $is_uid = false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3600 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3601 $result = $this->fetch($mailbox, $id, $is_uid, array('BODYSTRUCTURE'));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3602
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3603 if (is_array($result)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3604 $result = array_shift($result);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3605 return $result->bodystructure;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3606 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3607
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3608 return false;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3612 * Returns data of a message part according to specified structure.
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 * @param array $structure Message structure (getStructure() result)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3615 * @param string $part Message part identifier
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 * @return array Part data as hash array (type, encoding, charset, size)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3618 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3619 public static function getStructurePartData($structure, $part)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3620 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3621 $part_a = self::getStructurePartArray($structure, $part);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3622 $data = array();
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 if (empty($part_a)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3625 return $data;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3628 // content-type
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3629 if (is_array($part_a[0])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3630 $data['type'] = 'multipart';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3631 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3632 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3633 $data['type'] = strtolower($part_a[0]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3634 $data['encoding'] = strtolower($part_a[5]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3635
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3636 // charset
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3637 if (is_array($part_a[2])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3638 foreach ($part_a[2] as $key => $val) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3639 if (strcasecmp($val, 'charset') == 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3640 $data['charset'] = $part_a[2][$key+1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3641 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3642 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3643 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3644 }
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 // size
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3648 $data['size'] = intval($part_a[6]);
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 return $data;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3651 }
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 public static function getStructurePartArray($a, $part)
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 if (!is_array($a)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3656 return false;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3659 if (empty($part)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3660 return $a;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3663 $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
3664
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3665 if (strcasecmp($ctype, 'message/rfc822') == 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3666 $a = $a[8];
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3669 if (strpos($part, '.') > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3670 $orig_part = $part;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3671 $pos = strpos($part, '.');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3672 $rest = substr($orig_part, $pos+1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3673 $part = substr($orig_part, 0, $pos);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3674
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3675 return self::getStructurePartArray($a[$part-1], $rest);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3676 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3677 else if ($part > 0) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3678 return (is_array($a[$part-1])) ? $a[$part-1] : $a;
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 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3681
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 * Creates next command identifier (tag)
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 * @return string Command identifier
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3686 * @since 0.5-beta
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 public function nextTag()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3689 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3690 $this->cmd_num++;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3691 $this->cmd_tag = sprintf('A%04d', $this->cmd_num);
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 return $this->cmd_tag;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3694 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3697 * Sends IMAP command and parses result
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 * @param string $command IMAP command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3700 * @param array $arguments Command arguments
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3701 * @param int $options Execution options
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3702 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3703 * @return mixed Response code or list of response code and data
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3704 * @since 0.5-beta
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 public function execute($command, $arguments=array(), $options=0)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3707 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3708 $tag = $this->nextTag();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3709 $query = $tag . ' ' . $command;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3710 $noresp = ($options & self::COMMAND_NORESPONSE);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3711 $response = $noresp ? null : '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3712
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3713 if (!empty($arguments)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3714 foreach ($arguments as $arg) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3715 $query .= ' ' . self::r_implode($arg);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3716 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3717 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3718
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3719 // Send command
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3720 if (!$this->putLineC($query, true, ($options & self::COMMAND_ANONYMIZED))) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3721 preg_match('/^[A-Z0-9]+ ((UID )?[A-Z]+)/', $query, $matches);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3722 $cmd = $matches[1] ?: 'UNKNOWN';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3723 $this->setError(self::ERROR_COMMAND, "Failed to send $cmd command");
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3724
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3725 return $noresp ? self::ERROR_COMMAND : array(self::ERROR_COMMAND, '');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3726 }
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 // Parse response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3729 do {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3730 $line = $this->readLine(4096);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3731
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3732 if ($response !== null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3733 $response .= $line;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3736 // parse untagged response for [COPYUID 1204196876 3456:3457 123:124] (RFC6851)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3737 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
3738 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
3739 $this->data['COPYUID'] = array($m[1], $m[2]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3740 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3741 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3742 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3743 while (!$this->startsWith($line, $tag . ' ', true, true));
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 $code = $this->parseResult($line, $command . ': ');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3746
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3747 // Remove last line from response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3748 if ($response) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3749 $line_len = min(strlen($response), strlen($line) + 2);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3750 $response = substr($response, 0, -$line_len);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3751 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3752
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3753 // optional CAPABILITY response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3754 if (($options & self::COMMAND_CAPABILITY) && $code == self::ERROR_OK
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3755 && preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3756 ) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3757 $this->parseCapability($matches[1], true);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3758 }
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 // return last line only (without command tag, result and response code)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3761 if ($line && ($options & self::COMMAND_LASTLINE)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3762 $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
3763 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3764
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3765 return $noresp ? $code : array($code, $response);
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3769 * Splits IMAP response into string tokens
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 * @param string &$str The IMAP's server response
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3772 * @param int $num Number of tokens to return
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 * @return mixed Tokens array or string if $num=1
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3775 * @since 0.5-beta
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 public static function tokenizeResponse(&$str, $num=0)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3778 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3779 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3780
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3781 while (!$num || count($result) < $num) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3782 // remove spaces from the beginning of the string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3783 $str = ltrim($str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3784
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3785 switch ($str[0]) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3786
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3787 // String literal
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3788 case '{':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3789 if (($epos = strpos($str, "}\r\n", 1)) == false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3790 // error
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3791 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3792 if (!is_numeric(($bytes = substr($str, 1, $epos - 1)))) {
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3796 $result[] = $bytes ? substr($str, $epos + 3, $bytes) : '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3797 $str = substr($str, $epos + 3 + $bytes);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3798 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3799
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3800 // Quoted string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3801 case '"':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3802 $len = strlen($str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3803
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3804 for ($pos=1; $pos<$len; $pos++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3805 if ($str[$pos] == '"') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3806 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3807 }
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 if ($str[$pos + 1] == '"' || $str[$pos + 1] == "\\") {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3810 $pos++;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3811 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3812 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3813 }
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 // we need to strip slashes for a quoted string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3816 $result[] = stripslashes(substr($str, 1, $pos - 1));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3817 $str = substr($str, $pos + 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3818 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3819
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3820 // Parenthesized list
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3821 case '(':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3822 $str = substr($str, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3823 $result[] = self::tokenizeResponse($str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3824 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3825
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3826 case ')':
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3827 $str = substr($str, 1);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3828 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3829
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3830 // String atom, number, astring, NIL, *, %
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3831 default:
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3832 // empty string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3833 if ($str === '' || $str === null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3834 break 2;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3835 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3836
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3837 // excluded chars: SP, CTL, ), DEL
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3838 // we do not exclude [ and ] (#1489223)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3839 if (preg_match('/^([^\x00-\x20\x29\x7F]+)/', $str, $m)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3840 $result[] = $m[1] == 'NIL' ? null : $m[1];
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3841 $str = substr($str, strlen($m[1]));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3842 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3843 break;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3844 }
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3847 return $num == 1 ? $result[0] : $result;
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3851 * Joins IMAP command line elements (recursively)
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 protected static function r_implode($element)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3854 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3855 $string = '';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3856
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3857 if (is_array($element)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3858 reset($element);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3859 foreach ($element as $value) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3860 $string .= ' ' . self::r_implode($value);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3861 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3862 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3863 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3864 return $element;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3867 return '(' . trim($string) . ')';
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3871 * Converts message identifiers array into sequence-set syntax
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 * @param array $messages Message identifiers
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3874 * @param bool $force Forces compression of any size
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 * @return string Compressed sequence-set
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3877 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3878 public static function compressMessageSet($messages, $force=false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3879 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3880 // given a comma delimited list of independent mid's,
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3881 // compresses by grouping sequences together
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 if (!is_array($messages)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3884 // if less than 255 bytes long, let's not bother
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3885 if (!$force && strlen($messages)<255) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3886 return $messages;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3887 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3888
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3889 // see if it's already been compressed
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3890 if (strpos($messages, ':') !== false) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3891 return $messages;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3892 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3893
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3894 // separate, then sort
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3895 $messages = explode(',', $messages);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3898 sort($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 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3901 $start = $prev = $messages[0];
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 foreach ($messages as $id) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3904 $incr = $id - $prev;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3905 if ($incr > 1) { // found a gap
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3906 if ($start == $prev) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3907 $result[] = $prev; // push single id
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3908 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3909 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3910 $result[] = $start . ':' . $prev; // push sequence as start_id:end_id
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3911 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3912 $start = $id; // start of new sequence
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3913 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3914 $prev = $id;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3915 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3916
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3917 // handle the last sequence/id
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3918 if ($start == $prev) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3919 $result[] = $prev;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3920 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3921 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3922 $result[] = $start.':'.$prev;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3923 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3924
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3925 // return as comma separated string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3926 return implode(',', $result);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3927 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3930 * Converts message sequence-set into array
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3931 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3932 * @param string $messages Message identifiers
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 * @return array List of message identifiers
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3935 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3936 public static function uncompressMessageSet($messages)
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 if (empty($messages)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3939 return array();
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 $result = array();
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3943 $messages = explode(',', $messages);
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 foreach ($messages as $idx => $part) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3946 $items = explode(':', $part);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3947 $max = max($items[0], $items[1]);
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 for ($x=$items[0]; $x<=$max; $x++) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3950 $result[] = (int)$x;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3951 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3952 unset($messages[$idx]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3953 }
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 return $result;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3956 }
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 * Clear internal status cache
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3960 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3961 protected function clear_status_cache($mailbox)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3962 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3963 unset($this->data['STATUS:' . $mailbox]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3964
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3965 $keys = array('EXISTS', 'RECENT', 'UNSEEN', 'UID-MAP');
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3966
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3967 foreach ($keys as $key) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3968 unset($this->data[$key]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3969 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3973 * Clear internal cache of the current mailbox
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 protected function clear_mailbox_cache()
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3976 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3977 $this->clear_status_cache($this->selected);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3978
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3979 $keys = array('UIDNEXT', 'UIDVALIDITY', 'HIGHESTMODSEQ', 'NOMODSEQ',
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3980 'PERMANENTFLAGS', 'QRESYNC', 'VANISHED', 'READ-WRITE');
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 foreach ($keys as $key) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3983 unset($this->data[$key]);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3984 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3985 }
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 * Converts flags array into string for inclusion in IMAP command
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 * @param array $flags Flags (see self::flags)
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 * @return string Space-separated list of flags
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 protected function flagsToStr($flags)
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 foreach ((array)$flags as $idx => $flag) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3997 if ($flag = $this->flags[strtoupper($flag)]) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3998 $flags[$idx] = $flag;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
3999 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4000 }
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 return implode(' ', (array)$flags);
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4006 * CAPABILITY response parser
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4007 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4008 protected function parseCapability($str, $trusted=false)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4009 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4010 $str = preg_replace('/^\* CAPABILITY /i', '', $str);
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4011
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4012 $this->capability = explode(' ', strtoupper($str));
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4013
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4014 if (!empty($this->prefs['disabled_caps'])) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4015 $this->capability = array_diff($this->capability, $this->prefs['disabled_caps']);
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 if (!isset($this->prefs['literal+']) && in_array('LITERAL+', $this->capability)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4019 $this->prefs['literal+'] = true;
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 if ($trusted) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4023 $this->capability_readed = true;
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 }
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 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4028 * Escapes a string when it contains special characters (RFC3501)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4029 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4030 * @param string $string IMAP string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4031 * @param boolean $force_quotes Forces string quoting (for atoms)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4032 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4033 * @return string String atom, quoted-string or string literal
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4034 * @todo lists
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4035 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4036 public static function escape($string, $force_quotes=false)
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 if ($string === null) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4039 return 'NIL';
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4040 }
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 if ($string === '') {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4043 return '""';
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4046 // atom-string (only safe characters)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4047 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
4048 return $string;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4051 // quoted-string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4052 if (!preg_match('/[\r\n\x00\x80-\xFF]/', $string)) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4053 return '"' . addcslashes($string, '\\"') . '"';
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4056 // literal-string
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4057 return sprintf("{%d}\r\n%s", strlen($string), $string);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4060 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4061 * Set the value of the debugging flag.
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 * @param boolean $debug New value for the debugging flag.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4064 * @param callback $handler Logging handler function
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4065 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4066 * @since 0.5-stable
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 public function setDebug($debug, $handler = null)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4069 {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4070 $this->debug = $debug;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4071 $this->debug_handler = $handler;
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4074 /**
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4075 * Write the given debug text to the current debug output handler.
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 * @param string $message Debug message text.
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4078 *
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4079 * @since 0.5-stable
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4080 */
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4081 protected function debug($message)
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 if (($len = strlen($message)) > self::DEBUG_LINE_LENGTH) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4084 $diff = $len - self::DEBUG_LINE_LENGTH;
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4085 $message = substr($message, 0, self::DEBUG_LINE_LENGTH)
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4086 . "... [truncated $diff bytes]";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4087 }
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4088
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4089 if ($this->resourceid) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4090 $message = sprintf('[%s] %s', $this->resourceid, $message);
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
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4093 if ($this->debug_handler) {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4094 call_user_func_array($this->debug_handler, array(&$this, $message));
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 else {
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4097 echo "DEBUG: $message\n";
4681f974d28b vanilla 1.3.3 distro, I hope
Charlie Root
parents:
diff changeset
4098 }
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 }