| Total Complexity | 183 |
| Total Lines | 1044 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 2 | Features | 2 |
Complex classes like ImapProtocol often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ImapProtocol, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class ImapProtocol extends Protocol { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Request noun |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $noun = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Imap constructor. |
||
| 38 | * @param bool $cert_validation set to false to skip SSL certificate validation |
||
| 39 | * @param mixed $encryption Connection encryption method |
||
| 40 | */ |
||
| 41 | public function __construct($cert_validation = true, $encryption = false) { |
||
| 42 | $this->setCertValidation($cert_validation); |
||
| 43 | $this->encryption = $encryption; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Public destructor |
||
| 48 | */ |
||
| 49 | public function __destruct() { |
||
| 50 | $this->logout(); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Open connection to IMAP server |
||
| 55 | * @param string $host hostname or IP address of IMAP server |
||
| 56 | * @param int|null $port of IMAP server, default is 143 and 993 for ssl |
||
| 57 | * |
||
| 58 | * @throws ConnectionFailedException |
||
| 59 | */ |
||
| 60 | public function connect($host, $port = null) { |
||
| 61 | $transport = 'tcp'; |
||
| 62 | $encryption = ""; |
||
| 63 | |||
| 64 | if ($this->encryption) { |
||
| 65 | $encryption = strtolower($this->encryption); |
||
| 66 | if ($encryption == "ssl") { |
||
| 67 | $transport = 'ssl'; |
||
| 68 | $port = $port === null ? 993 : $port; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | $port = $port === null ? 143 : $port; |
||
| 72 | try { |
||
| 73 | $this->stream = $this->createStream($transport, $host, $port, $this->connection_timeout); |
||
| 74 | if (!$this->assumedNextLine('* OK')) { |
||
| 75 | throw new ConnectionFailedException('connection refused'); |
||
| 76 | } |
||
| 77 | if ($encryption == "tls") { |
||
| 78 | $this->enableTls(); |
||
| 79 | } |
||
| 80 | } catch (Exception $e) { |
||
| 81 | throw new ConnectionFailedException('connection failed', 0, $e); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Enable tls on the current connection |
||
| 87 | * |
||
| 88 | * @throws ConnectionFailedException |
||
| 89 | * @throws RuntimeException |
||
| 90 | */ |
||
| 91 | protected function enableTls(){ |
||
| 92 | $response = $this->requestAndResponse('STARTTLS'); |
||
| 93 | $result = $response && stream_socket_enable_crypto($this->stream, true, $this->getCryptoMethod()); |
||
|
|
|||
| 94 | if (!$result) { |
||
| 95 | throw new ConnectionFailedException('failed to enable TLS'); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get the next line from stream |
||
| 101 | * |
||
| 102 | * @return string next line |
||
| 103 | * @throws RuntimeException |
||
| 104 | */ |
||
| 105 | public function nextLine() { |
||
| 106 | $line = fgets($this->stream); |
||
| 107 | |||
| 108 | if ($line === false) { |
||
| 109 | throw new RuntimeException('failed to read - connection closed?'); |
||
| 110 | } |
||
| 111 | |||
| 112 | return $line; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get the next line and check if it starts with a given string |
||
| 117 | * @param string $start |
||
| 118 | * |
||
| 119 | * @return bool |
||
| 120 | * @throws RuntimeException |
||
| 121 | */ |
||
| 122 | protected function assumedNextLine($start) { |
||
| 123 | $line = $this->nextLine(); |
||
| 124 | return strpos($line, $start) === 0; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get the next line and split the tag |
||
| 129 | * @param string $tag reference tag |
||
| 130 | * |
||
| 131 | * @return string next line |
||
| 132 | * @throws RuntimeException |
||
| 133 | */ |
||
| 134 | protected function nextTaggedLine(&$tag) { |
||
| 135 | $line = $this->nextLine(); |
||
| 136 | list($tag, $line) = explode(' ', $line, 2); |
||
| 137 | |||
| 138 | return $line; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Split a given line in values. A value is literal of any form or a list |
||
| 143 | * @param string $line |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | * @throws RuntimeException |
||
| 147 | */ |
||
| 148 | protected function decodeLine($line) { |
||
| 149 | $tokens = []; |
||
| 150 | $stack = []; |
||
| 151 | |||
| 152 | // replace any trailing <NL> including spaces with a single space |
||
| 153 | $line = rtrim($line) . ' '; |
||
| 154 | while (($pos = strpos($line, ' ')) !== false) { |
||
| 155 | $token = substr($line, 0, $pos); |
||
| 156 | if (!strlen($token)) { |
||
| 157 | continue; |
||
| 158 | } |
||
| 159 | while ($token[0] == '(') { |
||
| 160 | array_push($stack, $tokens); |
||
| 161 | $tokens = []; |
||
| 162 | $token = substr($token, 1); |
||
| 163 | } |
||
| 164 | if ($token[0] == '"') { |
||
| 165 | if (preg_match('%^\(*"((.|\\\\|\\")*?)" *%', $line, $matches)) { |
||
| 166 | $tokens[] = $matches[1]; |
||
| 167 | $line = substr($line, strlen($matches[0])); |
||
| 168 | continue; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | if ($token[0] == '{') { |
||
| 172 | $endPos = strpos($token, '}'); |
||
| 173 | $chars = substr($token, 1, $endPos - 1); |
||
| 174 | if (is_numeric($chars)) { |
||
| 175 | $token = ''; |
||
| 176 | while (strlen($token) < $chars) { |
||
| 177 | $token .= $this->nextLine(); |
||
| 178 | } |
||
| 179 | $line = ''; |
||
| 180 | if (strlen($token) > $chars) { |
||
| 181 | $line = substr($token, $chars); |
||
| 182 | $token = substr($token, 0, $chars); |
||
| 183 | } else { |
||
| 184 | $line .= $this->nextLine(); |
||
| 185 | } |
||
| 186 | $tokens[] = $token; |
||
| 187 | $line = trim($line) . ' '; |
||
| 188 | continue; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | if ($stack && $token[strlen($token) - 1] == ')') { |
||
| 192 | // closing braces are not separated by spaces, so we need to count them |
||
| 193 | $braces = strlen($token); |
||
| 194 | $token = rtrim($token, ')'); |
||
| 195 | // only count braces if more than one |
||
| 196 | $braces -= strlen($token) + 1; |
||
| 197 | // only add if token had more than just closing braces |
||
| 198 | if (rtrim($token) != '') { |
||
| 199 | $tokens[] = rtrim($token); |
||
| 200 | } |
||
| 201 | $token = $tokens; |
||
| 202 | $tokens = array_pop($stack); |
||
| 203 | // special handline if more than one closing brace |
||
| 204 | while ($braces-- > 0) { |
||
| 205 | $tokens[] = $token; |
||
| 206 | $token = $tokens; |
||
| 207 | $tokens = array_pop($stack); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | $tokens[] = $token; |
||
| 211 | $line = substr($line, $pos + 1); |
||
| 212 | } |
||
| 213 | |||
| 214 | // maybe the server forgot to send some closing braces |
||
| 215 | while ($stack) { |
||
| 216 | $child = $tokens; |
||
| 217 | $tokens = array_pop($stack); |
||
| 218 | $tokens[] = $child; |
||
| 219 | } |
||
| 220 | |||
| 221 | return $tokens; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Read abd decode a response "line" |
||
| 226 | * @param array|string $tokens to decode |
||
| 227 | * @param string $wantedTag targeted tag |
||
| 228 | * @param bool $dontParse if true only the unparsed line is returned in $tokens |
||
| 229 | * |
||
| 230 | * @return bool |
||
| 231 | * @throws RuntimeException |
||
| 232 | */ |
||
| 233 | public function readLine(&$tokens = [], $wantedTag = '*', $dontParse = false) { |
||
| 234 | $line = $this->nextTaggedLine($tag); // get next tag |
||
| 235 | if (!$dontParse) { |
||
| 236 | $tokens = $this->decodeLine($line); |
||
| 237 | } else { |
||
| 238 | $tokens = $line; |
||
| 239 | } |
||
| 240 | if ($this->debug) echo "<< ".$line."\n"; |
||
| 241 | |||
| 242 | // if tag is wanted tag we might be at the end of a multiline response |
||
| 243 | return $tag == $wantedTag; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Read all lines of response until given tag is found |
||
| 248 | * @param string $tag request tag |
||
| 249 | * @param bool $dontParse if true every line is returned unparsed instead of the decoded tokens |
||
| 250 | * |
||
| 251 | * @return void|null|bool|array tokens if success, false if error, null if bad request |
||
| 252 | * @throws RuntimeException |
||
| 253 | */ |
||
| 254 | public function readResponse($tag, $dontParse = false) { |
||
| 255 | $lines = []; |
||
| 256 | $tokens = null; // define $tokens variable before first use |
||
| 257 | while (!$this->readLine($tokens, $tag, $dontParse)) { |
||
| 258 | $lines[] = $tokens; |
||
| 259 | } |
||
| 260 | |||
| 261 | if ($dontParse) { |
||
| 262 | // First two chars are still needed for the response code |
||
| 263 | $tokens = [substr($tokens, 0, 2)]; |
||
| 264 | } |
||
| 265 | |||
| 266 | // last line has response code |
||
| 267 | if ($tokens[0] == 'OK') { |
||
| 268 | return $lines ? $lines : true; |
||
| 269 | } elseif ($tokens[0] == 'NO') { |
||
| 270 | return false; |
||
| 271 | } |
||
| 272 | |||
| 273 | return; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Send a new request |
||
| 278 | * @param string $command |
||
| 279 | * @param array $tokens additional parameters to command, use escapeString() to prepare |
||
| 280 | * @param string $tag provide a tag otherwise an autogenerated is returned |
||
| 281 | * |
||
| 282 | * @throws RuntimeException |
||
| 283 | */ |
||
| 284 | public function sendRequest($command, $tokens = [], &$tag = null) { |
||
| 285 | if (!$tag) { |
||
| 286 | $this->noun++; |
||
| 287 | $tag = 'TAG' . $this->noun; |
||
| 288 | } |
||
| 289 | |||
| 290 | $line = $tag . ' ' . $command; |
||
| 291 | |||
| 292 | foreach ($tokens as $token) { |
||
| 293 | if (is_array($token)) { |
||
| 294 | if (fwrite($this->stream, $line . ' ' . $token[0] . "\r\n") === false) { |
||
| 295 | throw new RuntimeException('failed to write - connection closed?'); |
||
| 296 | } |
||
| 297 | if (!$this->assumedNextLine('+ ')) { |
||
| 298 | throw new RuntimeException('failed to send literal string'); |
||
| 299 | } |
||
| 300 | $line = $token[1]; |
||
| 301 | } else { |
||
| 302 | $line .= ' ' . $token; |
||
| 303 | } |
||
| 304 | } |
||
| 305 | if ($this->debug) echo ">> ".$line."\n"; |
||
| 306 | |||
| 307 | if (fwrite($this->stream, $line . "\r\n") === false) { |
||
| 308 | throw new RuntimeException('failed to write - connection closed?'); |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Send a request and get response at once |
||
| 314 | * @param string $command |
||
| 315 | * @param array $tokens parameters as in sendRequest() |
||
| 316 | * @param bool $dontParse if true unparsed lines are returned instead of tokens |
||
| 317 | * |
||
| 318 | * @return void|null|bool|array response as in readResponse() |
||
| 319 | * @throws RuntimeException |
||
| 320 | */ |
||
| 321 | public function requestAndResponse($command, $tokens = [], $dontParse = false) { |
||
| 322 | $this->sendRequest($command, $tokens, $tag); |
||
| 323 | |||
| 324 | return $this->readResponse($tag, $dontParse); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Escape one or more literals i.e. for sendRequest |
||
| 329 | * @param string|array $string the literal/-s |
||
| 330 | * |
||
| 331 | * @return string|array escape literals, literals with newline ar returned |
||
| 332 | * as array('{size}', 'string'); |
||
| 333 | */ |
||
| 334 | public function escapeString($string) { |
||
| 335 | if (func_num_args() < 2) { |
||
| 336 | if (strpos($string, "\n") !== false) { |
||
| 337 | return ['{' . strlen($string) . '}', $string]; |
||
| 338 | } else { |
||
| 339 | return '"' . str_replace(['\\', '"'], ['\\\\', '\\"'], $string) . '"'; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | $result = []; |
||
| 343 | foreach (func_get_args() as $string) { |
||
| 344 | $result[] = $this->escapeString($string); |
||
| 345 | } |
||
| 346 | return $result; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Escape a list with literals or lists |
||
| 351 | * @param array $list list with literals or lists as PHP array |
||
| 352 | * |
||
| 353 | * @return string escaped list for imap |
||
| 354 | */ |
||
| 355 | public function escapeList($list) { |
||
| 356 | $result = []; |
||
| 357 | foreach ($list as $v) { |
||
| 358 | if (!is_array($v)) { |
||
| 359 | $result[] = $v; |
||
| 360 | continue; |
||
| 361 | } |
||
| 362 | $result[] = $this->escapeList($v); |
||
| 363 | } |
||
| 364 | return '(' . implode(' ', $result) . ')'; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Login to a new session. |
||
| 369 | * @param string $user username |
||
| 370 | * @param string $password password |
||
| 371 | * |
||
| 372 | * @return bool|mixed |
||
| 373 | * @throws AuthFailedException |
||
| 374 | */ |
||
| 375 | public function login($user, $password) { |
||
| 376 | try { |
||
| 377 | return $this->requestAndResponse('LOGIN', $this->escapeString($user, $password), true); |
||
| 378 | } catch (RuntimeException $e) { |
||
| 379 | throw new AuthFailedException("failed to authenticate", 0, $e); |
||
| 380 | } |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Authenticate your current IMAP session. |
||
| 385 | * @param string $user username |
||
| 386 | * @param string $token access token |
||
| 387 | * |
||
| 388 | * @return bool|mixed |
||
| 389 | * @throws AuthFailedException |
||
| 390 | */ |
||
| 391 | public function authenticate($user, $token) { |
||
| 392 | try { |
||
| 393 | $authenticateParams = ['XOAUTH2', base64_encode("user=$user\1auth=Bearer $token\1\1")]; |
||
| 394 | $this->sendRequest('AUTHENTICATE', $authenticateParams); |
||
| 395 | |||
| 396 | while (true) { |
||
| 397 | $response = ""; |
||
| 398 | $is_plus = $this->readLine($response, '+', true); |
||
| 399 | if ($is_plus) { |
||
| 400 | // try to log the challenge somewhere where it can be found |
||
| 401 | error_log("got an extra server challenge: $response"); |
||
| 402 | // respond with an empty response. |
||
| 403 | $this->sendRequest(''); |
||
| 404 | } else { |
||
| 405 | if (preg_match('/^NO /i', $response) || |
||
| 406 | preg_match('/^BAD /i', $response)) { |
||
| 407 | error_log("got failure response: $response"); |
||
| 408 | return false; |
||
| 409 | } else if (preg_match("/^OK /i", $response)) { |
||
| 410 | return true; |
||
| 411 | } |
||
| 412 | } |
||
| 413 | } |
||
| 414 | } catch (RuntimeException $e) { |
||
| 415 | throw new AuthFailedException("failed to authenticate", 0, $e); |
||
| 416 | } |
||
| 417 | return false; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Logout of imap server |
||
| 422 | * |
||
| 423 | * @return bool success |
||
| 424 | */ |
||
| 425 | public function logout() { |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Check if the current session is connected |
||
| 439 | * |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | public function connected(){ |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Get an array of available capabilities |
||
| 448 | * |
||
| 449 | * @return array list of capabilities |
||
| 450 | * @throws RuntimeException |
||
| 451 | */ |
||
| 452 | public function getCapabilities() { |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Examine and select have the same response. |
||
| 466 | * @param string $command can be 'EXAMINE' or 'SELECT' |
||
| 467 | * @param string $folder target folder |
||
| 468 | * |
||
| 469 | * @return bool|array |
||
| 470 | * @throws RuntimeException |
||
| 471 | */ |
||
| 472 | public function examineOrSelect($command = 'EXAMINE', $folder = 'INBOX') { |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Change the current folder |
||
| 508 | * @param string $folder change to this folder |
||
| 509 | * |
||
| 510 | * @return bool|array see examineOrselect() |
||
| 511 | * @throws RuntimeException |
||
| 512 | */ |
||
| 513 | public function selectFolder($folder = 'INBOX') { |
||
| 514 | return $this->examineOrSelect('SELECT', $folder); |
||
| 515 | } |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Examine a given folder |
||
| 519 | * @param string $folder examine this folder |
||
| 520 | * |
||
| 521 | * @return bool|array see examineOrselect() |
||
| 522 | * @throws RuntimeException |
||
| 523 | */ |
||
| 524 | public function examineFolder($folder = 'INBOX') { |
||
| 526 | } |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Fetch one or more items of one or more messages |
||
| 530 | * @param string|array $items items to fetch [RFC822.HEADER, FLAGS, RFC822.TEXT, etc] |
||
| 531 | * @param int|array $from message for items or start message if $to !== null |
||
| 532 | * @param int|null $to if null only one message ($from) is fetched, else it's the |
||
| 533 | * last message, INF means last message available |
||
| 534 | * @param bool $uid set to true if passing a unique id |
||
| 535 | * |
||
| 536 | * @return string|array if only one item of one message is fetched it's returned as string |
||
| 537 | * if items of one message are fetched it's returned as (name => value) |
||
| 538 | * if one items of messages are fetched it's returned as (msgno => value) |
||
| 539 | * if items of messages are fetched it's returned as (msgno => (name => value)) |
||
| 540 | * @throws RuntimeException |
||
| 541 | */ |
||
| 542 | protected function fetch($items, $from, $to = null, $uid = false) { |
||
| 628 | } |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Fetch message headers |
||
| 632 | * @param array|int $uids |
||
| 633 | * @param string $rfc |
||
| 634 | * @param bool $uid set to true if passing a unique id |
||
| 635 | * |
||
| 636 | * @return array |
||
| 637 | * @throws RuntimeException |
||
| 638 | */ |
||
| 639 | public function content($uids, $rfc = "RFC822", $uid = false) { |
||
| 640 | return $this->fetch(["$rfc.TEXT"], $uids, null, $uid); |
||
| 641 | } |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Fetch message headers |
||
| 645 | * @param array|int $uids |
||
| 646 | * @param string $rfc |
||
| 647 | * @param bool $uid set to true if passing a unique id |
||
| 648 | * |
||
| 649 | * @return array |
||
| 650 | * @throws RuntimeException |
||
| 651 | */ |
||
| 652 | public function headers($uids, $rfc = "RFC822", $uid = false){ |
||
| 654 | } |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Fetch message flags |
||
| 658 | * @param array|int $uids |
||
| 659 | * @param bool $uid set to true if passing a unique id |
||
| 660 | * |
||
| 661 | * @return array |
||
| 662 | * @throws RuntimeException |
||
| 663 | */ |
||
| 664 | public function flags($uids, $uid = false){ |
||
| 666 | } |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Get uid for a given id |
||
| 670 | * @param int|null $id message number |
||
| 671 | * |
||
| 672 | * @return array|string message number for given message or all messages as array |
||
| 673 | * @throws MessageNotFoundException |
||
| 674 | */ |
||
| 675 | public function getUid($id = null) { |
||
| 690 | } |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Get a message number for a uid |
||
| 694 | * @param string $id uid |
||
| 695 | * |
||
| 696 | * @return int message number |
||
| 697 | * @throws MessageNotFoundException |
||
| 698 | */ |
||
| 699 | public function getMessageNumber($id) { |
||
| 700 | $ids = $this->getUid(); |
||
| 701 | foreach ($ids as $k => $v) { |
||
| 702 | if ($v == $id) { |
||
| 703 | return $k; |
||
| 704 | } |
||
| 705 | } |
||
| 706 | |||
| 707 | throw new MessageNotFoundException('message number not found'); |
||
| 708 | } |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Get a list of available folders |
||
| 712 | * @param string $reference mailbox reference for list |
||
| 713 | * @param string $folder mailbox name match with wildcards |
||
| 714 | * |
||
| 715 | * @return array folders that matched $folder as array(name => array('delimiter' => .., 'flags' => ..)) |
||
| 716 | * @throws RuntimeException |
||
| 717 | */ |
||
| 718 | public function folders($reference = '', $folder = '*') { |
||
| 733 | } |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Manage flags |
||
| 737 | * @param array $flags flags to set, add or remove - see $mode |
||
| 738 | * @param int $from message for items or start message if $to !== null |
||
| 739 | * @param int|null $to if null only one message ($from) is fetched, else it's the |
||
| 740 | * last message, INF means last message available |
||
| 741 | * @param string|null $mode '+' to add flags, '-' to remove flags, everything else sets the flags as given |
||
| 742 | * @param bool $silent if false the return values are the new flags for the wanted messages |
||
| 743 | * @param bool $uid set to true if passing a unique id |
||
| 744 | * |
||
| 745 | * @return bool|array new flags if $silent is false, else true or false depending on success |
||
| 746 | * @throws RuntimeException |
||
| 747 | */ |
||
| 748 | public function store(array $flags, $from, $to = null, $mode = null, $silent = true, $uid = false) { |
||
| 780 | } |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Append a new message to given folder |
||
| 784 | * @param string $folder name of target folder |
||
| 785 | * @param string $message full message content |
||
| 786 | * @param array $flags flags for new message |
||
| 787 | * @param string $date date for new message |
||
| 788 | * |
||
| 789 | * @return bool success |
||
| 790 | * @throws RuntimeException |
||
| 791 | */ |
||
| 792 | public function appendMessage($folder, $message, $flags = null, $date = null) { |
||
| 804 | } |
||
| 805 | |||
| 806 | /** |
||
| 807 | * Copy a message set from current folder to an other folder |
||
| 808 | * @param string $folder destination folder |
||
| 809 | * @param $from |
||
| 810 | * @param int|null $to if null only one message ($from) is fetched, else it's the |
||
| 811 | * last message, INF means last message available |
||
| 812 | * @param bool $uid set to true if passing a unique id |
||
| 813 | * |
||
| 814 | * @return bool success |
||
| 815 | * @throws RuntimeException |
||
| 816 | */ |
||
| 817 | public function copyMessage($folder, $from, $to = null, $uid = false) { |
||
| 825 | } |
||
| 826 | |||
| 827 | /** |
||
| 828 | * Copy multiple messages to the target folder |
||
| 829 | * |
||
| 830 | * @param array<string> $messages List of message identifiers |
||
| 831 | * @param string $folder Destination folder |
||
| 832 | * @param bool $uid Set to true if you pass message unique identifiers instead of numbers |
||
| 833 | * @return array|bool Tokens if operation successful, false if an error occurred |
||
| 834 | * |
||
| 835 | * @throws RuntimeException |
||
| 836 | */ |
||
| 837 | public function copyManyMessages($messages, $folder, $uid = false) { |
||
| 838 | $command = $uid ? 'UID COPY' : 'COPY'; |
||
| 839 | |||
| 840 | $set = implode(',', $messages); |
||
| 841 | $tokens = [$set, $this->escapeString($folder)]; |
||
| 842 | |||
| 843 | return $this->requestAndResponse($command, $tokens, true); |
||
| 844 | } |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Move a message set from current folder to an other folder |
||
| 848 | * @param string $folder destination folder |
||
| 849 | * @param $from |
||
| 850 | * @param int|null $to if null only one message ($from) is fetched, else it's the |
||
| 851 | * last message, INF means last message available |
||
| 852 | * @param bool $uid set to true if passing a unique id |
||
| 853 | * |
||
| 854 | * @return bool success |
||
| 855 | * @throws RuntimeException |
||
| 856 | */ |
||
| 857 | public function moveMessage($folder, $from, $to = null, $uid = false) { |
||
| 858 | $set = (int)$from; |
||
| 859 | if ($to !== null) { |
||
| 860 | $set .= ':' . ($to == INF ? '*' : (int)$to); |
||
| 861 | } |
||
| 862 | $command = ($uid ? "UID " : "")."MOVE"; |
||
| 863 | |||
| 864 | return $this->requestAndResponse($command, [$set, $this->escapeString($folder)], true); |
||
| 865 | } |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Move multiple messages to the target folder |
||
| 869 | * |
||
| 870 | * @param array<string> $messages List of message identifiers |
||
| 871 | * @param string $folder Destination folder |
||
| 872 | * @param bool $uid Set to true if you pass message unique identifiers instead of numbers |
||
| 873 | * @return array|bool Tokens if operation successful, false if an error occurred |
||
| 874 | * |
||
| 875 | * @throws RuntimeException |
||
| 876 | */ |
||
| 877 | public function moveManyMessages($messages, $folder, $uid = false) { |
||
| 878 | $command = $uid ? 'UID MOVE' : 'MOVE'; |
||
| 879 | |||
| 880 | $set = implode(',', $messages); |
||
| 881 | $tokens = [$set, $this->escapeString($folder)]; |
||
| 882 | |||
| 883 | return $this->requestAndResponse($command, $tokens, true); |
||
| 884 | } |
||
| 885 | |||
| 886 | /** |
||
| 887 | * Create a new folder (and parent folders if needed) |
||
| 888 | * @param string $folder folder name |
||
| 889 | * |
||
| 890 | * @return bool success |
||
| 891 | * @throws RuntimeException |
||
| 892 | */ |
||
| 893 | public function createFolder($folder) { |
||
| 894 | return $this->requestAndResponse('CREATE', [$this->escapeString($folder)], true); |
||
| 895 | } |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Rename an existing folder |
||
| 899 | * @param string $old old name |
||
| 900 | * @param string $new new name |
||
| 901 | * |
||
| 902 | * @return bool success |
||
| 903 | * @throws RuntimeException |
||
| 904 | */ |
||
| 905 | public function renameFolder($old, $new) { |
||
| 906 | return $this->requestAndResponse('RENAME', $this->escapeString($old, $new), true); |
||
| 907 | } |
||
| 908 | |||
| 909 | /** |
||
| 910 | * Delete a folder |
||
| 911 | * @param string $folder folder name |
||
| 912 | * |
||
| 913 | * @return bool success |
||
| 914 | * @throws RuntimeException |
||
| 915 | */ |
||
| 916 | public function deleteFolder($folder) { |
||
| 917 | return $this->requestAndResponse('DELETE', [$this->escapeString($folder)], true); |
||
| 918 | } |
||
| 919 | |||
| 920 | /** |
||
| 921 | * Subscribe to a folder |
||
| 922 | * @param string $folder folder name |
||
| 923 | * |
||
| 924 | * @return bool success |
||
| 925 | * @throws RuntimeException |
||
| 926 | */ |
||
| 927 | public function subscribeFolder($folder) { |
||
| 928 | return $this->requestAndResponse('SUBSCRIBE', [$this->escapeString($folder)], true); |
||
| 929 | } |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Unsubscribe from a folder |
||
| 933 | * @param string $folder folder name |
||
| 934 | * |
||
| 935 | * @return bool success |
||
| 936 | * @throws RuntimeException |
||
| 937 | */ |
||
| 938 | public function unsubscribeFolder($folder) { |
||
| 939 | return $this->requestAndResponse('UNSUBSCRIBE', [$this->escapeString($folder)], true); |
||
| 940 | } |
||
| 941 | |||
| 942 | /** |
||
| 943 | * Apply session saved changes to the server |
||
| 944 | * |
||
| 945 | * @return bool success |
||
| 946 | * @throws RuntimeException |
||
| 947 | */ |
||
| 948 | public function expunge() { |
||
| 949 | return $this->requestAndResponse('EXPUNGE'); |
||
| 950 | } |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Send noop command |
||
| 954 | * |
||
| 955 | * @return bool success |
||
| 956 | * @throws RuntimeException |
||
| 957 | */ |
||
| 958 | public function noop() { |
||
| 960 | } |
||
| 961 | |||
| 962 | /** |
||
| 963 | * Retrieve the quota level settings, and usage statics per mailbox |
||
| 964 | * @param $username |
||
| 965 | * |
||
| 966 | * @return array |
||
| 967 | * @throws RuntimeException |
||
| 968 | */ |
||
| 969 | public function getQuota($username) { |
||
| 970 | return $this->requestAndResponse("GETQUOTA", ['"#user/'.$username.'"']); |
||
| 971 | } |
||
| 972 | |||
| 973 | /** |
||
| 974 | * Retrieve the quota settings per user |
||
| 975 | * @param string $quota_root |
||
| 976 | * |
||
| 977 | * @return array |
||
| 978 | * @throws RuntimeException |
||
| 979 | */ |
||
| 980 | public function getQuotaRoot($quota_root = 'INBOX') { |
||
| 981 | return $this->requestAndResponse("QUOTA", [$quota_root]); |
||
| 982 | } |
||
| 983 | |||
| 984 | /** |
||
| 985 | * Send idle command |
||
| 986 | * |
||
| 987 | * @throws RuntimeException |
||
| 988 | */ |
||
| 989 | public function idle() { |
||
| 990 | $this->sendRequest("IDLE"); |
||
| 991 | if (!$this->assumedNextLine('+ ')) { |
||
| 992 | throw new RuntimeException('idle failed'); |
||
| 993 | } |
||
| 994 | } |
||
| 995 | |||
| 996 | /** |
||
| 997 | * Send done command |
||
| 998 | * @throws RuntimeException |
||
| 999 | */ |
||
| 1000 | public function done() { |
||
| 1005 | } |
||
| 1006 | |||
| 1007 | /** |
||
| 1008 | * Search for matching messages |
||
| 1009 | * @param array $params |
||
| 1010 | * @param bool $uid set to true if passing a unique id |
||
| 1011 | * |
||
| 1012 | * @return array message ids |
||
| 1013 | * @throws RuntimeException |
||
| 1014 | */ |
||
| 1015 | public function search(array $params, $uid = false) { |
||
| 1029 | } |
||
| 1030 | |||
| 1031 | /** |
||
| 1032 | * Get a message overview |
||
| 1033 | * @param string $sequence |
||
| 1034 | * @param bool $uid set to true if passing a unique id |
||
| 1035 | * |
||
| 1036 | * @return array |
||
| 1037 | * @throws RuntimeException |
||
| 1038 | * @throws MessageNotFoundException |
||
| 1039 | * @throws InvalidMessageDateException |
||
| 1040 | */ |
||
| 1041 | public function overview($sequence, $uid = false) { |
||
| 1042 | $result = []; |
||
| 1043 | list($from, $to) = explode(":", $sequence); |
||
| 1044 | |||
| 1045 | $uids = $this->getUid(); |
||
| 1046 | $ids = []; |
||
| 1047 | foreach ($uids as $msgn => $v) { |
||
| 1048 | $id = $uid ? $v : $msgn; |
||
| 1049 | if ( ($to >= $id && $from <= $id) || ($to === "*" && $from <= $id) ){ |
||
| 1050 | $ids[] = $id; |
||
| 1051 | } |
||
| 1052 | } |
||
| 1053 | $headers = $this->headers($ids, $rfc = "RFC822", $uid); |
||
| 1054 | foreach ($headers as $id => $raw_header) { |
||
| 1055 | $result[$id] = (new Header($raw_header, false))->getAttributes(); |
||
| 1056 | } |
||
| 1057 | return $result; |
||
| 1058 | } |
||
| 1059 | |||
| 1060 | /** |
||
| 1061 | * Enable the debug mode |
||
| 1062 | */ |
||
| 1063 | public function enableDebug(){ |
||
| 1065 | } |
||
| 1066 | |||
| 1067 | /** |
||
| 1068 | * Disable the debug mode |
||
| 1069 | */ |
||
| 1070 | public function disableDebug(){ |
||
| 1072 | } |
||
| 1073 | } |
||
| 1074 |