| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | class Hm_IMAP_Base { |
| 17: | |
| 18: | public $cached_response = false; |
| 19: | public $supported_extensions = array(); |
| 20: | protected $handle = false; |
| 21: | protected $debug = array(); |
| 22: | protected $commands = array(); |
| 23: | protected $responses = array(); |
| 24: | protected $current_command = false; |
| 25: | protected $max_read = false; |
| 26: | protected $command_count = 0; |
| 27: | protected $cache_data = array(); |
| 28: | protected $enabled_extensions = array(); |
| 29: | protected $capability = false; |
| 30: | protected $server_id = array(); |
| 31: | protected $literal_overflow = false; |
| 32: | public $struct_object = false; |
| 33: | |
| 34: | |
| 35: | |
| 36: | protected $config = array('server', 'port', 'tls', 'read_only', |
| 37: | 'utf7_folders', 'auth', 'search_charset', 'sort_speedup', 'folder_max', |
| 38: | 'use_cache', 'max_history', 'blacklisted_extensions', 'app_name', 'app_version', |
| 39: | 'app_vendor', 'app_support_url', 'cache_limit', 'no_caps'); |
| 40: | |
| 41: | |
| 42: | protected $client_extensions = array('SORT', 'COMPRESS', 'NAMESPACE', 'CONDSTORE', |
| 43: | 'ENABLE', 'QRESYNC', 'MOVE', 'SPECIAL-USE', 'LIST-STATUS', 'UNSELECT', 'ID', 'X-GM-EXT-1', |
| 44: | 'ESEARCH', 'ESORT', 'QUOTA', 'LIST-EXTENDED'); |
| 45: | |
| 46: | |
| 47: | protected $declared_extensions = array('CONDSTORE', 'QRESYNC'); |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | private function command_number() { |
| 55: | $this->command_count += 1; |
| 56: | return $this->command_count; |
| 57: | } |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | private function read_literal($size, $max, $current, $line_length) { |
| 72: | $left_over = false; |
| 73: | $literal_data = $this->fgets($line_length); |
| 74: | $lit_size = strlen($literal_data); |
| 75: | $current += $lit_size; |
| 76: | while ($lit_size < $size) { |
| 77: | $chunk = $this->fgets($line_length); |
| 78: | $chunk_size = strlen($chunk); |
| 79: | $lit_size += $chunk_size; |
| 80: | $current += $chunk_size; |
| 81: | $literal_data .= $chunk; |
| 82: | if ($max && $current > $max) { |
| 83: | $this->max_read = true; |
| 84: | break; |
| 85: | } |
| 86: | } |
| 87: | if ($this->max_read) { |
| 88: | while ($lit_size < $size) { |
| 89: | $temp = $this->fgets($line_length); |
| 90: | $lit_size += strlen($temp); |
| 91: | } |
| 92: | } |
| 93: | elseif ($size < strlen($literal_data)) { |
| 94: | $left_over = substr($literal_data, $size); |
| 95: | $literal_data = substr($literal_data, 0, $size); |
| 96: | } |
| 97: | return array($literal_data, $left_over); |
| 98: | } |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | protected function parse_line($line, $current_size, $max, $line_length) { |
| 111: | |
| 112: | $line = str_replace(')(', ') (', $line); |
| 113: | $this->literal_overflow = false; |
| 114: | |
| 115: | |
| 116: | $parts = array(); |
| 117: | |
| 118: | |
| 119: | $line_cont = false; |
| 120: | |
| 121: | |
| 122: | $len = mb_strlen($line); |
| 123: | |
| 124: | |
| 125: | for ($i=0;$i<$len;$i++) { |
| 126: | |
| 127: | $char = mb_substr($line, $i, 1); |
| 128: | |
| 129: | |
| 130: | $chunk = ''; |
| 131: | |
| 132: | |
| 133: | if ($char == "\r" || $char == "\n") { |
| 134: | $line_cont = false; |
| 135: | break; |
| 136: | } |
| 137: | |
| 138: | |
| 139: | if ($char == ' ') { |
| 140: | continue; |
| 141: | } |
| 142: | |
| 143: | |
| 144: | elseif ($char == '*' || $char == '[' || $char == ']' || $char == '(' || $char == ')') { |
| 145: | $chunk = $char; |
| 146: | } |
| 147: | |
| 148: | |
| 149: | elseif ($char == '"') { |
| 150: | if (preg_match("/^(\"[^\"\\\]*(?:\\\.[^\"\\\]*)*\")/", mb_substr($line, $i), $matches)) { |
| 151: | $chunk = mb_substr($matches[1], 1, -1); |
| 152: | } |
| 153: | $i += mb_strlen($chunk) + 1; |
| 154: | } |
| 155: | |
| 156: | |
| 157: | elseif ($char == '{') { |
| 158: | $end = mb_strpos($line, '}'); |
| 159: | if ($end !== false) { |
| 160: | $literal_size = mb_substr($line, ($i + 1), ($end - $i - 1)); |
| 161: | } |
| 162: | $lit_result = $this->read_literal($literal_size, $max, $current_size, $line_length); |
| 163: | $chunk = $lit_result[0]; |
| 164: | if (!isset($lit_result[1]) || $lit_result[1] != "\r\n") { |
| 165: | $line_cont = true; |
| 166: | } |
| 167: | if (isset($lit_result[1]) && $lit_result[1] != "\r\n" && mb_strlen($lit_result[1]) > 0) { |
| 168: | $this->literal_overflow = $lit_result[1]; |
| 169: | } |
| 170: | $i = $len; |
| 171: | } |
| 172: | |
| 173: | |
| 174: | else { |
| 175: | $marker = -1; |
| 176: | |
| 177: | |
| 178: | foreach (array(' ', ')', ']') as $v) { |
| 179: | $tmp_marker = mb_strpos($line, $v, $i); |
| 180: | if ($tmp_marker !== false && ($marker == -1 || $tmp_marker < $marker)) { |
| 181: | $marker = $tmp_marker; |
| 182: | } |
| 183: | } |
| 184: | |
| 185: | |
| 186: | if ($marker !== false && $marker !== -1) { |
| 187: | $chunk = mb_substr($line, $i, ($marker - $i)); |
| 188: | $i += mb_strlen($chunk) - 1; |
| 189: | } |
| 190: | else { |
| 191: | $chunk = rtrim(mb_substr($line, $i)); |
| 192: | $i += mb_strlen($chunk); |
| 193: | } |
| 194: | } |
| 195: | |
| 196: | |
| 197: | if ($chunk) { |
| 198: | $parts[] = $chunk; |
| 199: | } |
| 200: | } |
| 201: | return array($line_cont, $parts); |
| 202: | } |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | |
| 209: | protected function fgets($len=false) { |
| 210: | if (is_resource($this->handle) && !feof($this->handle)) { |
| 211: | if ($len) { |
| 212: | return fgets($this->handle, $len); |
| 213: | } |
| 214: | else { |
| 215: | return fgets($this->handle); |
| 216: | } |
| 217: | } |
| 218: | return ''; |
| 219: | } |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | |
| 226: | |
| 227: | |
| 228: | |
| 229: | |
| 230: | |
| 231: | |
| 232: | public function get_response($max=false, $chunked=false, $line_length=8192, $sort=false) { |
| 233: | |
| 234: | $result = array(); |
| 235: | $current_size = 0; |
| 236: | $chunked_result = array(); |
| 237: | $last_line_cont = false; |
| 238: | $line_cont = false; |
| 239: | $c = -1; |
| 240: | $n = -1; |
| 241: | |
| 242: | |
| 243: | do { |
| 244: | $n++; |
| 245: | |
| 246: | |
| 247: | if (Hm_Functions::stream_ended($this->handle)) { |
| 248: | break; |
| 249: | } |
| 250: | |
| 251: | |
| 252: | $result[$n] = $this->fgets($line_length); |
| 253: | |
| 254: | |
| 255: | |
| 256: | |
| 257: | $current_size += mb_strlen($result[$n]); |
| 258: | if ($max && $current_size > $max) { |
| 259: | $this->max_read = true; |
| 260: | break; |
| 261: | } |
| 262: | |
| 263: | |
| 264: | |
| 265: | while(mb_substr($result[$n], -2) != "\r\n" && mb_substr($result[$n], -1) != "\n") { |
| 266: | if (!is_resource($this->handle) || feof($this->handle)) { |
| 267: | break; |
| 268: | } |
| 269: | $result[$n] .= $this->fgets($line_length); |
| 270: | if ($result[$n] === false) { |
| 271: | break; |
| 272: | } |
| 273: | $current_size += mb_strlen($result[$n]); |
| 274: | if ($max && $current_size > $max) { |
| 275: | $this->max_read = true; |
| 276: | break 2; |
| 277: | } |
| 278: | } |
| 279: | |
| 280: | |
| 281: | if ($line_cont) { |
| 282: | $last_line_cont = true; |
| 283: | $pres = $n - 1; |
| 284: | if ($chunks) { |
| 285: | $pchunk = $c; |
| 286: | } |
| 287: | } |
| 288: | |
| 289: | |
| 290: | |
| 291: | |
| 292: | if ($sort) { |
| 293: | $line_cont = false; |
| 294: | $chunks = explode(' ', trim($result[$n])); |
| 295: | } |
| 296: | |
| 297: | |
| 298: | else { |
| 299: | list($line_cont, $chunks) = $this->parse_line($result[$n], $current_size, $max, $line_length); |
| 300: | while ($this->literal_overflow) { |
| 301: | $lit_text = $this->literal_overflow; |
| 302: | $this->literal_overflow = false; |
| 303: | $current_size += mb_strlen($lit_text); |
| 304: | list($line_cont, $new_chunks) = $this->parse_line($lit_text, $current_size, $max, $line_length); |
| 305: | $chunks = array_merge($chunks, $new_chunks); |
| 306: | } |
| 307: | } |
| 308: | |
| 309: | |
| 310: | if ($chunks && !$last_line_cont) { |
| 311: | $c++; |
| 312: | } |
| 313: | if ($last_line_cont) { |
| 314: | $result[$pres] .= ' '.implode(' ', $chunks); |
| 315: | if ($chunks) { |
| 316: | $line_bits = array_merge($chunked_result[$pchunk], $chunks); |
| 317: | $chunked_result[$pchunk] = $line_bits; |
| 318: | } |
| 319: | $last_line_cont = false; |
| 320: | } |
| 321: | |
| 322: | |
| 323: | else { |
| 324: | $result[$n] = implode(' ', $chunks); |
| 325: | if ($chunked) { |
| 326: | $chunked_result[$c] = $chunks; |
| 327: | } |
| 328: | } |
| 329: | |
| 330: | |
| 331: | |
| 332: | if (mb_substr(mb_strtoupper($result[$n]), 0, 6) == '* BYE ') { |
| 333: | break; |
| 334: | } |
| 335: | |
| 336: | |
| 337: | if (mb_substr($result[$n], 0, 1) == '+') { |
| 338: | if (preg_match("/^\+ ([a-zA-Z0-9=]+)$/", $result[$n], $matches)) { |
| 339: | break; |
| 340: | } |
| 341: | } |
| 342: | |
| 343: | |
| 344: | } while (mb_substr($result[$n], 0, mb_strlen('A'.$this->command_count)) != 'A'.$this->command_count); |
| 345: | |
| 346: | |
| 347: | $this->responses[] = $result; |
| 348: | if ($chunked) { |
| 349: | $result = $chunked_result; |
| 350: | } |
| 351: | if ($this->current_command && isset($this->commands[$this->current_command])) { |
| 352: | $start_time = $this->commands[$this->current_command]; |
| 353: | $this->commands[$this->current_command] = microtime(true) - $start_time; |
| 354: | if (count($this->commands) >= $this->max_history) { |
| 355: | array_shift($this->commands); |
| 356: | array_shift($this->responses); |
| 357: | } |
| 358: | } |
| 359: | return $result; |
| 360: | } |
| 361: | |
| 362: | |
| 363: | |
| 364: | |
| 365: | |
| 366: | |
| 367: | |
| 368: | |
| 369: | public function send_command($command, $no_prefix=false) { |
| 370: | $this->cached_response = false; |
| 371: | if (!$no_prefix) { |
| 372: | $command = 'A'.$this->command_number().' '.$command; |
| 373: | } |
| 374: | |
| 375: | |
| 376: | if (is_resource($this->handle)) { |
| 377: | $res = @fputs($this->handle, $command); |
| 378: | if (!$res) { |
| 379: | $this->debug[] = 'Error communicating with IMAP server: '.trim($command); |
| 380: | } |
| 381: | } |
| 382: | |
| 383: | |
| 384: | if (mb_strstr($command, 'LOGIN')) { |
| 385: | $command = 'LOGIN'; |
| 386: | } |
| 387: | $this->commands[trim($command)] = microtime( true ); |
| 388: | $this->current_command = trim($command); |
| 389: | } |
| 390: | |
| 391: | |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | |
| 397: | protected function check_response($data, $chunked=false, $log_failures=true) { |
| 398: | $result = false; |
| 399: | |
| 400: | |
| 401: | if ($chunked) { |
| 402: | if (!empty($data) && isset($data[(count($data) - 1)])) { |
| 403: | $vals = $data[(count($data) - 1)]; |
| 404: | if ($vals[0] == 'A'.$this->command_count) { |
| 405: | if (mb_strtoupper($vals[1]) == 'OK') { |
| 406: | $result = true; |
| 407: | } |
| 408: | } |
| 409: | } |
| 410: | } |
| 411: | |
| 412: | |
| 413: | else { |
| 414: | $line = array_pop($data); |
| 415: | if (preg_match("/^A".$this->command_count." OK/i", $line)) { |
| 416: | $result = true; |
| 417: | } |
| 418: | } |
| 419: | if (!$result && $log_failures) { |
| 420: | $this->debug[] = 'Command FAILED: '.$this->current_command; |
| 421: | } |
| 422: | return $result; |
| 423: | } |
| 424: | |
| 425: | |
| 426: | |
| 427: | |
| 428: | |
| 429: | |
| 430: | protected function utf7_decode($string) { |
| 431: | if ($this->utf7_folders) { |
| 432: | $string = mb_convert_encoding($string, "UTF-8", "UTF7-IMAP" ); |
| 433: | } |
| 434: | return $string; |
| 435: | } |
| 436: | |
| 437: | |
| 438: | |
| 439: | |
| 440: | |
| 441: | |
| 442: | protected function utf7_encode($string) { |
| 443: | if ($this->utf7_folders) { |
| 444: | $string = mb_convert_encoding($string, "UTF7-IMAP", "UTF-8" ); |
| 445: | } |
| 446: | return $string; |
| 447: | } |
| 448: | |
| 449: | |
| 450: | |
| 451: | |
| 452: | |
| 453: | |
| 454: | |
| 455: | protected function input_validate($val, $type) { |
| 456: | $imap_search_charsets = array( |
| 457: | 'UTF-8', |
| 458: | 'US-ASCII', |
| 459: | '', |
| 460: | ); |
| 461: | $imap_keywords = array( |
| 462: | 'ARRIVAL', 'DATE', 'FROM', 'SUBJECT', |
| 463: | 'CC', 'TO', 'SIZE', 'UNSEEN', |
| 464: | 'SEEN', 'FLAGGED', 'UNFLAGGED', 'ANSWERED', |
| 465: | 'UNANSWERED', 'DELETED', 'UNDELETED', 'TEXT', |
| 466: | 'ALL', 'DRAFT', 'NEW', 'RECENT', 'OLD', 'UNDRAFT', |
| 467: | 'BODY' |
| 468: | ); |
| 469: | $valid = false; |
| 470: | switch ($type) { |
| 471: | case 'search_str': |
| 472: | if (preg_match("/^[^\r\n]+$/", $val)) { |
| 473: | $valid = true; |
| 474: | } |
| 475: | break; |
| 476: | case 'msg_part': |
| 477: | if (preg_match("/^[\d\.]+$/", $val)) { |
| 478: | $valid = true; |
| 479: | } |
| 480: | break; |
| 481: | case 'charset': |
| 482: | if (!$val || in_array(mb_strtoupper($val), $imap_search_charsets)) { |
| 483: | $valid = true; |
| 484: | } |
| 485: | break; |
| 486: | case 'uid': |
| 487: | if (ctype_digit((string) $val)) { |
| 488: | $valid = true; |
| 489: | } |
| 490: | break; |
| 491: | case 'uid_list'; |
| 492: | if (preg_match("/^[0-9,*:]+$/", $val)) { |
| 493: | $valid = true; |
| 494: | } |
| 495: | break; |
| 496: | case 'mailbox'; |
| 497: | if (preg_match("/^[^\r\n]+$/", $val)) { |
| 498: | $valid = true; |
| 499: | } |
| 500: | break; |
| 501: | case 'keyword'; |
| 502: | if (in_array(mb_strtoupper($val), $imap_keywords)) { |
| 503: | $valid = true; |
| 504: | } |
| 505: | break; |
| 506: | } |
| 507: | return $valid; |
| 508: | } |
| 509: | |
| 510: | |
| 511: | |
| 512: | |
| 513: | |
| 514: | |
| 515: | |
| 516: | protected function is_clean($val, $type) { |
| 517: | if (!$this->input_validate($val, $type)) { |
| 518: | $this->debug[] = 'INVALID IMAP INPUT DETECTED: '.$type.' : '.$val; |
| 519: | return false; |
| 520: | } |
| 521: | return true; |
| 522: | } |
| 523: | |
| 524: | |
| 525: | |
| 526: | |
| 527: | |
| 528: | |
| 529: | protected function apply_config( $config ) { |
| 530: | foreach($config as $key => $val) { |
| 531: | if (in_array($key, $this->config)) { |
| 532: | $this->{$key} = $val; |
| 533: | } |
| 534: | } |
| 535: | } |
| 536: | } |
| 537: | |