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