| | |
| | | $IMAP_USE_INTERNAL_DATE = true; |
| | | } |
| | | |
| | | /** |
| | | * @todo Maybe use date() to generate this. |
| | | */ |
| | | $GLOBALS['IMAP_MONTHS'] = array("Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, |
| | | "May" => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, |
| | | "Nov" => 11, "Dec" => 12); |
| | | |
| | | $GLOBALS['IMAP_SERVER_TZ'] = date('Z'); |
| | | |
| | | $GLOBALS['IMAP_FLAGS'] = array( |
| | | 'SEEN' => '\\Seen', |
| | | 'DELETED' => '\\Deleted', |
| | |
| | | } |
| | | |
| | | function iil_PutLine($fp, $string, $endln=true) { |
| | | // console('C: '. rtrim($string)); |
| | | global $my_prefs; |
| | | |
| | | if (!empty($my_prefs['debug_mode'])) |
| | | write_log('imap', 'C: '. rtrim($string)); |
| | | |
| | | return fputs($fp, $string . ($endln ? "\r\n" : '')); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | function iil_ReadLine($fp, $size=1024) { |
| | | global $my_prefs; |
| | | |
| | | $line = ''; |
| | | |
| | | if (!$fp) { |
| | |
| | | |
| | | do { |
| | | $buffer = fgets($fp, $size); |
| | | |
| | | if ($buffer === false) { |
| | | break; |
| | | } |
| | | // console('S: '. chop($buffer)); |
| | | if (!empty($my_prefs['debug_mode'])) |
| | | write_log('imap', 'S: '. chop($buffer)); |
| | | $line .= $buffer; |
| | | } while ($buffer[strlen($buffer)-1] != "\n"); |
| | | |
| | | |
| | | return $line; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | $line = $a[1][0] . '"' . ($escape ? iil_Escape($out) : $out) . '"'; |
| | | // console('[...] '. $out); |
| | | } |
| | | return $line; |
| | | } |
| | | |
| | | function iil_ReadBytes($fp, $bytes) { |
| | | global $my_prefs; |
| | | $data = ''; |
| | | $len = 0; |
| | | do { |
| | | $data .= fread($fp, $bytes-$len); |
| | | if ($len == strlen($data)) { |
| | | $d = fread($fp, $bytes-$len); |
| | | if (!empty($my_prefs['debug_mode'])) |
| | | write_log('imap', 'S: '. $d); |
| | | $data .= $d; |
| | | $data_len = strlen($data); |
| | | if ($len == $data_len) { |
| | | break; //nothing was read -> exit to avoid apache lockups |
| | | } |
| | | $len = strlen($data); |
| | | $len = $data_len; |
| | | } while ($len < $bytes); |
| | | |
| | | return $data; |
| | | } |
| | | |
| | | // don't use it in loops, until you exactly know what you're doing |
| | | function iil_ReadReply($fp) { |
| | | do { |
| | | $line = trim(iil_ReadLine($fp, 1024)); |
| | | } while ($line[0] == '*'); |
| | | |
| | | |
| | | return $line; |
| | | } |
| | | |
| | |
| | | return false; |
| | | } |
| | | |
| | | function iil_StartsWithI($string, $match, $bye=false) { |
| | | function iil_StartsWithI($string, $match, $error=false) { |
| | | $len = strlen($match); |
| | | if ($len == 0) { |
| | | return false; |
| | |
| | | if (strncasecmp($string, $match, $len) == 0) { |
| | | return true; |
| | | } |
| | | if ($bye && strncmp($string, '* BYE ', 6) == 0) { |
| | | if ($error && preg_match('/^\* (BYE|BAD) /i', $string)) { |
| | | return true; |
| | | |
| | | } |
| | |
| | | |
| | | iil_PutLine($conn->fp, 'a001 LOGIN "'.iil_Escape($user).'" "'.iil_Escape($password).'"'); |
| | | |
| | | do { |
| | | $line = iil_ReadReply($conn->fp); |
| | | if ($line === false) { |
| | | break; |
| | | } |
| | | } while (!iil_StartsWith($line, 'a001 ', true)); |
| | | |
| | | $line = iil_ReadReply($conn->fp); |
| | | |
| | | // process result |
| | | $result = iil_ParseResult($line); |
| | | |
| | |
| | | $my_prefs['rootdir'] = $optval; |
| | | } else if ($optkey == 'delimiter') { |
| | | $my_prefs['delimiter'] = $optval; |
| | | } else if ($optkey == 'debug_mode') { |
| | | $my_prefs['debug_mode'] = $optval; |
| | | } |
| | | } |
| | | } |
| | |
| | | } else if ($my_prefs['sort_field'] == 'DATE') { |
| | | $IMAP_USE_INTERNAL_DATE = false; |
| | | } |
| | | //echo '<!-- conn sort_field: '.$my_prefs['sort_field'].' //-->'; |
| | | |
| | | //check input |
| | | if (empty($host)) { |
| | |
| | | stream_set_timeout($conn->fp, 10); |
| | | $line = stream_get_line($conn->fp, 8192, "\r\n"); |
| | | |
| | | if ($my_prefs['debug_mode'] && $line) |
| | | write_log('imap', 'S: '. $line); |
| | | |
| | | // Connected to wrong port or connection error? |
| | | if (!preg_match('/^\* (OK|PREAUTH)/i', $line)) { |
| | | if ($line) |
| | |
| | | $iil_errornum = -2; |
| | | return false; |
| | | } |
| | | |
| | | |
| | | // RFC3501 [7.1] optional CAPABILITY response |
| | | if (preg_match('/\[CAPABILITY ([^]]+)\]/i', $line, $matches)) { |
| | | $conn->capability = explode(' ', strtoupper($matches[1])); |
| | |
| | | if (strcasecmp($a[2], 'EXISTS') == 0) { |
| | | $conn->exists = (int) $a[1]; |
| | | } |
| | | if (strcasecmp($a[2], 'RECENT') == 0) { |
| | | else if (strcasecmp($a[2], 'RECENT') == 0) { |
| | | $conn->recent = (int) $a[1]; |
| | | } |
| | | } |
| | |
| | | return $string; |
| | | } |
| | | |
| | | function iil_StrToTime($str) { |
| | | $IMAP_MONTHS = $GLOBALS['IMAP_MONTHS']; |
| | | $IMAP_SERVER_TZ = $GLOBALS['IMAP_SERVER_TZ']; |
| | | |
| | | if ($str) { |
| | | $time1 = strtotime($str); |
| | | } |
| | | if ($time1 && $time1 != -1) { |
| | | return $time1-$IMAP_SERVER_TZ; |
| | | } |
| | | //echo '<!--'.$str.'//-->'; |
| | | |
| | | //replace double spaces with single space |
| | | $str = trim($str); |
| | | $str = str_replace(' ', ' ', $str); |
| | | |
| | | //strip off day of week |
| | | $pos = strpos($str, ' '); |
| | | if (!is_numeric(substr($str, 0, $pos))) { |
| | | $str = substr($str, $pos+1); |
| | | } |
| | | //explode, take good parts |
| | | $a = explode(' ', $str); |
| | | function iil_StrToTime($date) { |
| | | |
| | | $month_str = $a[1]; |
| | | $month = $IMAP_MONTHS[$month_str]; |
| | | $day = (int)$a[0]; |
| | | $year = (int)$a[2]; |
| | | $time = $a[3]; |
| | | $tz_str = $a[4]; |
| | | $tz = substr($tz_str, 0, 3); |
| | | $ta = explode(':', $time); |
| | | $hour = (int)$ta[0]-(int)$tz; |
| | | $minute = (int)$ta[1]; |
| | | $second = (int)$ta[2]; |
| | | |
| | | //make UNIX timestamp |
| | | $time2 = mktime($hour, $minute, $second, $month, $day, $year); |
| | | //echo '<!--'.$time1.' '.$time2.' //-->'."\n"; |
| | | return $time2; |
| | | // support non-standard "GMTXXXX" literal |
| | | $date = preg_replace('/GMT\s*([+-][0-9]+)/', '\\1', $date); |
| | | // if date parsing fails, we have a date in non-rfc format. |
| | | // remove token from the end and try again |
| | | while ((($ts = @strtotime($date))===false) || ($ts < 0)) |
| | | { |
| | | $d = explode(' ', $date); |
| | | array_pop($d); |
| | | if (!$d) break; |
| | | $date = implode(' ', $d); |
| | | } |
| | | |
| | | $ts = (int) $ts; |
| | | |
| | | return $ts < 0 ? 0 : $ts; |
| | | } |
| | | |
| | | function iil_C_Sort(&$conn, $mailbox, $field, $add='', $is_uid=FALSE, |
| | |
| | | $command = 's ' . $is_uid . 'SORT (' . $field . ') '; |
| | | $command .= $encoding . ' ALL' . $add; |
| | | $line = $data = ''; |
| | | |
| | | |
| | | if (!iil_PutLineC($conn->fp, $command)) { |
| | | return false; |
| | | } |
| | | do { |
| | | $line = chop(iil_ReadLine($conn->fp, 1024)); |
| | | $line = chop(iil_ReadLine($conn->fp)); |
| | | if (iil_StartsWith($line, '* SORT')) { |
| | | $data .= ($data ? ' ' : '') . substr($line, 7); |
| | | $data .= substr($line, 7); |
| | | } else if (preg_match('/^[0-9 ]+$/', $line)) { |
| | | $data .= $line; |
| | | } |
| | |
| | | return false; |
| | | } |
| | | |
| | | $out = explode(' ',$data); |
| | | return $out; |
| | | return preg_split('/\s+/', $data, -1, PREG_SPLIT_NO_EMPTY); |
| | | } |
| | | |
| | | function iil_C_FetchHeaderIndex(&$conn, $mailbox, $message_set, $index_field='', $skip_deleted=true) { |
| | |
| | | $result[$id] = in_array('\\'.$index_field, $flags) ? 1 : 0; |
| | | } else if ($mode == 4) { |
| | | if (preg_match('/INTERNALDATE "([^"]+)"/', $line, $matches)) { |
| | | $result[$id] = strtotime($matches[1]); |
| | | $result[$id] = iil_StrToTime($matches[1]); |
| | | } else { |
| | | $result[$id] = 0; |
| | | } |
| | |
| | | // if time is gmt... |
| | | $time_str = str_replace('GMT','+0000',$time_str); |
| | | |
| | | //get timezone |
| | | $time_str = substr($time_str, 0, -1); |
| | | $time_zone_str = substr($time_str, -5); // extract timezone |
| | | $time_str = substr($time_str, 0, -5); // remove timezone |
| | | $time_zone = (float)substr($time_zone_str, 1, 2); // get first two digits |
| | | |
| | | if ($time_zone_str[3] != '0') { |
| | | $time_zone += 0.5; //handle half hour offset |
| | | } |
| | | if ($time_zone_str[0] == '-') { |
| | | $time_zone = $time_zone * -1.0; //minus? |
| | | } |
| | | |
| | | //calculate timestamp |
| | | $timestamp = strtotime($time_str); //return's server's time |
| | | $timestamp -= $time_zone * 3600; //compensate for tz, get GMT |
| | | |
| | | $result[$id]->internaldate = $time_str; |
| | | $result[$id]->timestamp = $timestamp; |
| | | $result[$id]->timestamp = iil_StrToTime($time_str); |
| | | $result[$id]->date = $time_str; |
| | | } |
| | | |
| | |
| | | while (list($key, $val)=each($a)) { |
| | | |
| | | if ($field == 'timestamp') { |
| | | $data = @strtotime($val->date); |
| | | if ($data == false) { |
| | | $data = iil_StrToTime($val->date); |
| | | if (!$data) { |
| | | $data = $val->timestamp; |
| | | } |
| | | } else { |
| | |
| | | return iil_C_ModFlag($conn, $mailbox, $messages, 'DELETED', '+'); |
| | | } |
| | | |
| | | function iil_C_Undelete(&$conn, $mailbox, $messages) { |
| | | return iil_C_ModFlag($conn, $mailbox, $messages, 'DELETED', '-'); |
| | | } |
| | | |
| | | function iil_C_Unseen(&$conn, $mailbox, $messages) { |
| | | return iil_C_ModFlag($conn, $mailbox, $messages, 'SEEN', '-'); |
| | | } |
| | | |
| | | function iil_C_Copy(&$conn, $messages, $from, $to) { |
| | | $fp = $conn->fp; |
| | | |
| | |
| | | $c=0; |
| | | |
| | | iil_PutLine($fp, "cpy1 UID COPY $messages \"".iil_Escape($to)."\""); |
| | | $line=iil_ReadReply($fp); |
| | | $line = iil_ReadReply($fp); |
| | | return iil_ParseResult($line); |
| | | } else { |
| | | return -1; |
| | | } |
| | | } |
| | | |
| | | function iil_FormatSearchDate($month, $day, $year) { |
| | | $month = (int) $month; |
| | | $months = $GLOBALS['IMAP_MONTHS']; |
| | | return $day . '-' . $months[$month] . '-' . $year; |
| | | } |
| | | |
| | | function iil_C_CountUnseen(&$conn, $folder) { |
| | | $index = iil_C_Search($conn, $folder, 'ALL UNSEEN'); |
| | | if (is_array($index)) { |
| | | if (($cnt = count($index)) && $index[0] != '') { |
| | | return $cnt; |
| | | } |
| | | } |
| | | return false; |
| | | $index = iil_C_Search($conn, $folder, 'ALL UNSEEN'); |
| | | if (is_array($index)) |
| | | return count($index); |
| | | return false; |
| | | } |
| | | |
| | | function iil_C_UID2ID(&$conn, $folder, $uid) { |
| | |
| | | } |
| | | |
| | | function iil_C_ID2UID(&$conn, $folder, $id) { |
| | | $fp = $conn->fp; |
| | | |
| | | if ($id == 0) { |
| | | return -1; |
| | | } |
| | | $result = -1; |
| | | if (iil_C_Select($conn, $folder)) { |
| | | $key = 'FUID'; |
| | | if (iil_PutLine($fp, "$key FETCH $id (UID)")) { |
| | | $key = 'fuid'; |
| | | if (iil_PutLine($conn->fp, "$key FETCH $id (UID)")) { |
| | | do { |
| | | $line=chop(iil_ReadLine($fp, 1024)); |
| | | $line = chop(iil_ReadLine($conn->fp, 1024)); |
| | | if (preg_match("/^\* $id FETCH \(UID (.*)\)/i", $line, $r)) { |
| | | $result = $r[1]; |
| | | } |
| | | } while (!preg_match("/^$key/", $line)); |
| | | } while (!iil_StartsWith($line, $key, true)); |
| | | } |
| | | } |
| | | return $result; |
| | | } |
| | | |
| | | function iil_C_Search(&$conn, $folder, $criteria) { |
| | | $fp = $conn->fp; |
| | | |
| | | if (iil_C_Select($conn, $folder)) { |
| | | $c = 0; |
| | | $data = ''; |
| | | |
| | | $query = 'srch1 SEARCH ' . chop($criteria); |
| | | if (!iil_PutLineC($fp, $query)) { |
| | | if (!iil_PutLineC($conn->fp, $query)) { |
| | | return false; |
| | | } |
| | | do { |
| | | $line=trim(iil_ReadLine($fp, 10000)); |
| | | if (preg_match('/^\* SEARCH/i', $line)) { |
| | | $str = trim(substr($line, 8)); |
| | | $messages = explode(' ', $str); |
| | | $line = trim(iil_ReadLine($conn->fp)); |
| | | if (iil_StartsWith($line, '* SEARCH')) { |
| | | $data .= substr($line, 8); |
| | | } else if (preg_match('/^[0-9 ]+$/', $line)) { |
| | | $data .= $line; |
| | | } |
| | | } while (!iil_StartsWith($line, 'srch1', true)); |
| | | |
| | | $result_code = iil_ParseResult($line); |
| | | if ($result_code == 0) { |
| | | return $messages; |
| | | return preg_split('/\s+/', $data, -1, PREG_SPLIT_NO_EMPTY); |
| | | } |
| | | $conn->error = 'iil_C_Search: ' . $line . "\n"; |
| | | return false; |
| | |
| | | return $result; |
| | | } |
| | | |
| | | function iil_C_FetchPartHeader(&$conn, $mailbox, $id, $part) { |
| | | function iil_C_FetchPartHeader(&$conn, $mailbox, $id, $is_uid=false, $part=NULL) { |
| | | |
| | | $part = empty($part) ? 'HEADER' : $part.'.MIME'; |
| | | |
| | | return iil_C_HandlePartBody($conn, $mailbox, $id, $part); |
| | | return iil_C_HandlePartBody($conn, $mailbox, $id, $is_uid, $part); |
| | | } |
| | | |
| | | function iil_C_HandlePartBody(&$conn, $mailbox, $id, $part='', $encoding=NULL, $print=NULL, $file=NULL) { |
| | | function iil_C_HandlePartBody(&$conn, $mailbox, $id, $is_uid=false, $part='', $encoding=NULL, $print=NULL, $file=NULL) { |
| | | |
| | | $fp = $conn->fp; |
| | | $result = false; |
| | |
| | | |
| | | // format request |
| | | $key = 'ftch0'; |
| | | $request = $key . " FETCH $id (BODY.PEEK[$part])"; |
| | | $request = $key . ($is_uid ? ' UID' : '') . " FETCH $id (BODY.PEEK[$part])"; |
| | | // send request |
| | | if (!iil_PutLine($fp, $request)) { |
| | | return false; |
| | |
| | | $len = $to - $from; |
| | | $result = substr($line, $from, $len); |
| | | } |
| | | |
| | | |
| | | if ($mode == 1) |
| | | $result = base64_decode($result); |
| | | else if ($mode == 2) |
| | |
| | | |
| | | if ($len > $bytes) { |
| | | $line = substr($line, 0, $bytes); |
| | | $len = strlen($line); |
| | | } |
| | | $bytes -= strlen($line); |
| | | $bytes -= $len; |
| | | |
| | | if ($mode == 1) { |
| | | $line = rtrim($line, "\t\r\n\0\x0B"); |
| | |
| | | do { |
| | | $line = iil_ReadLine($fp, 1024); |
| | | } while (!iil_StartsWith($line, $key, true)); |
| | | |
| | | |
| | | if ($result) { |
| | | $result = rtrim($result, "\t\r\n\0\x0B"); |
| | | if ($file) { |
| | |
| | | echo $result; |
| | | } else |
| | | return $result; // substr($result, 0, strlen($result)-1); |
| | | |
| | | return true; |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | function iil_C_FetchPartBody(&$conn, $mailbox, $id, $part, $file=NULL) { |
| | | return iil_C_HandlePartBody($conn, $mailbox, $id, $part, NULL, NULL, $file); |
| | | } |
| | | |
| | | function iil_C_PrintPartBody(&$conn, $mailbox, $id, $part) { |
| | | iil_C_HandlePartBody($conn, $mailbox, $id, $part, NULL, true, NULL); |
| | | } |
| | | |
| | | function iil_C_CreateFolder(&$conn, $folder) { |
| | |
| | | if (iil_PutLine($fp, 'c CREATE "' . iil_Escape($folder) . '"')) { |
| | | do { |
| | | $line=iil_ReadLine($fp, 300); |
| | | } while ($line[0] != 'c'); |
| | | $conn->error = $line; |
| | | } while (!iil_StartsWith($line, 'c ', true)); |
| | | return (iil_ParseResult($line) == 0); |
| | | } |
| | | return false; |
| | |
| | | if (iil_PutLine($fp, 'r RENAME "' . iil_Escape($from) . '" "' . iil_Escape($to) . '"')) { |
| | | do { |
| | | $line = iil_ReadLine($fp, 300); |
| | | } while ($line[0] != 'r'); |
| | | } while (!iil_StartsWith($line, 'r ', true)); |
| | | return (iil_ParseResult($line) == 0); |
| | | } |
| | | return false; |
| | |
| | | if (iil_PutLine($fp, 'd DELETE "' . iil_Escape($folder). '"')) { |
| | | do { |
| | | $line=iil_ReadLine($fp, 300); |
| | | } while ($line[0] != 'd'); |
| | | } while (!iil_StartsWith($line, 'd ', true)); |
| | | return (iil_ParseResult($line) == 0); |
| | | } |
| | | $conn->error = "Couldn't send command\n"; |
| | | return false; |
| | | } |
| | | |
| | |
| | | return false; |
| | | } |
| | | |
| | | $request = 'A APPEND "' . iil_Escape($folder) .'" (\\Seen) {' . $len . '}'; |
| | | $request = 'a APPEND "' . iil_Escape($folder) .'" (\\Seen) {' . $len . '}'; |
| | | |
| | | if (iil_PutLine($fp, $request)) { |
| | | $line=iil_ReadLine($fp, 100); |
| | | $sent = fwrite($fp, $message."\r\n"); |
| | | $line = iil_ReadLine($fp, 100); |
| | | $sent = iil_PutLine($fp, $message); |
| | | do { |
| | | $line=iil_ReadLine($fp, 1000); |
| | | } while ($line[0] != 'A'); |
| | | $line=iil_ReadLine($fp); |
| | | } while (!iil_StartsWith($line, 'a ', true)); |
| | | |
| | | $result = (iil_ParseResult($line) == 0); |
| | | if (!$result) { |
| | |
| | | } |
| | | |
| | | //send APPEND command |
| | | $request = 'A APPEND "' . iil_Escape($folder) . '" (\\Seen) {' . $len . '}'; |
| | | $request = 'a APPEND "' . iil_Escape($folder) . '" (\\Seen) {' . $len . '}'; |
| | | $bytes_sent = 0; |
| | | if (iil_PutLine($fp, $request)) { |
| | | $line = iil_ReadLine($fp, 100); |
| | |
| | | } |
| | | fclose($in_fp); |
| | | |
| | | iil_PutLine($fp, ''); |
| | | iil_PutLine($fp, ''); // \r\n |
| | | |
| | | //read response |
| | | do { |
| | | $line = iil_ReadLine($fp, 1000); |
| | | } while ($line[0] != 'A'); |
| | | $line = iil_ReadLine($fp); |
| | | } while (!iil_StartsWith($line, 'a ', true)); |
| | | |
| | | $result = (iil_ParseResult($line) == 0); |
| | | if (!$result) { |
| | |
| | | return false; |
| | | } |
| | | |
| | | function iil_C_FetchStructureString(&$conn, $folder, $id) { |
| | | function iil_C_FetchStructureString(&$conn, $folder, $id, $is_uid=false) { |
| | | $fp = $conn->fp; |
| | | $result = false; |
| | | |
| | | if (iil_C_Select($conn, $folder)) { |
| | | $key = 'F1247'; |
| | | |
| | | if (iil_PutLine($fp, "$key FETCH $id (BODYSTRUCTURE)")) { |
| | | if (iil_PutLine($fp, $key . ($is_uid ? ' UID' : '') ." FETCH $id (BODYSTRUCTURE)")) { |
| | | do { |
| | | $line = iil_ReadLine($fp, 5000); |
| | | $line = iil_MultLine($fp, $line); |
| | | list(, $index, $cmd, $rest) = explode(' ', $line); |
| | | if ($cmd != 'FETCH' || $index == $id || preg_match("/^$key/", $line)) |
| | | if (!preg_match("/^$key/", $line)) |
| | | $result .= $line; |
| | | } while (!preg_match("/^$key/", $line)); |
| | | } while (!iil_StartsWith($line, $key, true)); |
| | | |
| | | $result = trim(substr($result, strpos($result, 'BODYSTRUCTURE')+13, -(strlen($result)-strrpos($result, $key)+1))); |
| | | $result = trim(substr($result, strpos($result, 'BODYSTRUCTURE')+13, -1)); |
| | | } |
| | | } |
| | | return $result; |