| | |
| | | * |
| | | * @package Mail |
| | | * @author Thomas Bruederli <roundcube@gmail.com> |
| | | * @version 1.40 |
| | | * @version 1.5 |
| | | * @link http://ilohamail.org |
| | | */ |
| | | class rcube_imap |
| | |
| | | var $debug_level = 1; |
| | | var $error_code = 0; |
| | | var $options = array('imap' => 'check'); |
| | | |
| | | private $host, $user, $pass, $port, $ssl; |
| | | |
| | | |
| | | /** |
| | |
| | | // check for Open-SSL support in PHP build |
| | | if ($use_ssl && extension_loaded('openssl')) |
| | | $ICL_SSL = $use_ssl == 'imaps' ? 'ssl' : $use_ssl; |
| | | else if ($use_ssl) |
| | | { |
| | | else if ($use_ssl) { |
| | | raise_error(array('code' => 403, 'type' => 'imap', 'file' => __FILE__, |
| | | 'message' => 'Open SSL not available;'), TRUE, FALSE); |
| | | $port = 143; |
| | | } |
| | | } |
| | | |
| | | $ICL_PORT = $port; |
| | | $IMAP_USE_INTERNAL_DATE = false; |
| | | |
| | | $attempt = 0; |
| | | do { |
| | | $data = rcmail::get_instance()->plugins->exec_hook('imap_connect', array('host' => $host, 'user' => $user, 'attempt' => ++$attempt)); |
| | | if (!empty($data['pass'])) |
| | | $pass = $data['pass']; |
| | | |
| | | $this->conn = iil_Connect($host, $user, $pass, $this->options); |
| | | $this->host = $host; |
| | | $this->user = $user; |
| | | $this->conn = iil_Connect($data['host'], $data['user'], $pass, $this->options); |
| | | } while(!$this->conn && $data['retry']); |
| | | |
| | | $this->host = $data['host']; |
| | | $this->user = $data['user']; |
| | | $this->pass = $pass; |
| | | $this->port = $port; |
| | | $this->ssl = $use_ssl; |
| | |
| | | * @param int Current page to list |
| | | * @param string Header field to sort by |
| | | * @param string Sort order [ASC|DESC] |
| | | * @param boolean Number of slice items to extract from result array |
| | | * @return array Indexed array with message header objects |
| | | * @access public |
| | | */ |
| | | function list_headers($mbox_name='', $page=NULL, $sort_field=NULL, $sort_order=NULL) |
| | | function list_headers($mbox_name='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $slice=0) |
| | | { |
| | | $mailbox = $mbox_name ? $this->mod_mailbox($mbox_name) : $this->mailbox; |
| | | return $this->_list_headers($mailbox, $page, $sort_field, $sort_order); |
| | | return $this->_list_headers($mailbox, $page, $sort_field, $sort_order, false, $slice); |
| | | } |
| | | |
| | | |
| | |
| | | * @access private |
| | | * @see rcube_imap::list_headers |
| | | */ |
| | | private function _list_headers($mailbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $recursive=FALSE) |
| | | private function _list_headers($mailbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $recursive=FALSE, $slice=0) |
| | | { |
| | | if (!strlen($mailbox)) |
| | | return array(); |
| | | |
| | | // use saved message set |
| | | if ($this->search_string && $mailbox == $this->mailbox) |
| | | return $this->_list_header_set($mailbox, $page, $sort_field, $sort_order); |
| | | return $this->_list_header_set($mailbox, $page, $sort_field, $sort_order, $slice); |
| | | |
| | | $this->_set_sort_order($sort_field, $sort_order); |
| | | |
| | |
| | | { |
| | | $start_msg = ($page-1) * $this->page_size; |
| | | $a_msg_headers = $this->get_message_cache($cache_key, $start_msg, $start_msg+$this->page_size, $this->sort_field, $this->sort_order); |
| | | return array_values($a_msg_headers); |
| | | $result = array_values($a_msg_headers); |
| | | if ($slice) |
| | | $result = array_slice($result, -$slice, $slice); |
| | | return $result; |
| | | } |
| | | // cache is dirty, sync it |
| | | else if ($this->caching_enabled && $cache_status==-1 && !$recursive) |
| | | { |
| | | $this->sync_header_index($mailbox); |
| | | return $this->_list_headers($mailbox, $page, $this->sort_field, $this->sort_order, TRUE); |
| | | return $this->_list_headers($mailbox, $page, $this->sort_field, $this->sort_order, TRUE, $slice); |
| | | } |
| | | |
| | | // retrieve headers from IMAP |
| | | $a_msg_headers = array(); |
| | | |
| | | if ($this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : ''))) |
| | | if (!$this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : ''))) |
| | | { |
| | | list($begin, $end) = $this->_get_message_range(count($msg_index), $page); |
| | | $max = max($msg_index); |
| | | $msg_index = array_slice($msg_index, $begin, $end-$begin); |
| | | |
| | | if ($slice) |
| | | $msg_index = array_slice($msg_index, ($this->sort_order == 'DESC' ? 0 : -$slice), $slice); |
| | | |
| | | // fetch reqested headers from server |
| | | $this->_fetch_headers($mailbox, join(',', $msg_index), $a_msg_headers, $cache_key); |
| | |
| | | else |
| | | { |
| | | $a_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, "1:*", $this->sort_field, $this->skip_deleted); |
| | | |
| | | |
| | | if (empty($a_index)) |
| | | return array(); |
| | | |
| | |
| | | $max = max($msg_index); |
| | | list($begin, $end) = $this->_get_message_range(count($msg_index), $page); |
| | | $msg_index = array_slice($msg_index, $begin, $end-$begin); |
| | | |
| | | if ($slice) |
| | | $msg_index = array_slice($msg_index, ($this->sort_order == 'DESC' ? 0 : -$slice), $slice); |
| | | |
| | | // fetch reqested headers from server |
| | | $this->_fetch_headers($mailbox, join(",", $msg_index), $a_msg_headers, $cache_key); |
| | |
| | | * @param int Current page to list |
| | | * @param string Header field to sort by |
| | | * @param string Sort order [ASC|DESC] |
| | | * @param boolean Number of slice items to extract from result array |
| | | * @return array Indexed array with message header objects |
| | | * @access private |
| | | * @see rcube_imap::list_header_set() |
| | | */ |
| | | private function _list_header_set($mailbox, $page=NULL, $sort_field=NULL, $sort_order=NULL) |
| | | private function _list_header_set($mailbox, $page=NULL, $sort_field=NULL, $sort_order=NULL, $slice=0) |
| | | { |
| | | if (!strlen($mailbox) || empty($this->search_set)) |
| | | return array(); |
| | |
| | | // get messages uids for one page |
| | | $msgs = array_slice(array_values($msgs), $start_msg, min(count($msgs)-$start_msg, $this->page_size)); |
| | | |
| | | if ($slice) |
| | | $msgs = array_slice($msgs, -$slice, $slice); |
| | | |
| | | // fetch headers |
| | | $this->_fetch_headers($mailbox, join(',',$msgs), $a_msg_headers, NULL); |
| | | |
| | |
| | | $a_index = $this->message_index('', $this->sort_field, $this->sort_order); |
| | | // get messages uids for one page... |
| | | $msgs = array_slice($a_index, $start_msg, min($cnt-$start_msg, $this->page_size)); |
| | | if ($slice) |
| | | $msgs = array_slice($msgs, -$slice, $slice); |
| | | // ...and fetch headers |
| | | $this->_fetch_headers($mailbox, join(',', $msgs), $a_msg_headers, NULL); |
| | | |
| | |
| | | $a_msg_headers = iil_SortHeaders($a_msg_headers, $this->sort_field, $this->sort_order); |
| | | |
| | | // only return the requested part of the set |
| | | return array_slice(array_values($a_msg_headers), $start_msg, min($cnt-$start_msg, $this->page_size)); |
| | | $a_msg_headers = array_slice(array_values($a_msg_headers), $start_msg, min($cnt-$start_msg, $this->page_size)); |
| | | if ($slice) |
| | | $a_msg_headers = array_slice($a_msg_headers, -$slice, $slice); |
| | | |
| | | return $a_msg_headers; |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | else |
| | | $a_messages = iil_C_Search($this->conn, $mailbox, ($charset ? "CHARSET $charset " : '') . $criteria); |
| | | |
| | | // clean message list (there might be some empty entries) |
| | | if (is_array($a_messages)) |
| | | { |
| | | foreach ($a_messages as $i => $val) |
| | | if (empty($val)) |
| | | unset($a_messages[$i]); |
| | | } |
| | | |
| | | // update messagecount cache ? |
| | | // $a_mailbox_cache = get_cache('messagecount'); |
| | |
| | | return $headers->structure; |
| | | } |
| | | |
| | | // resolve message sequence number |
| | | if (!($msg_id = $this->_uid2id($uid))) { |
| | | return FALSE; |
| | | } |
| | | |
| | | if (!$structure_str) |
| | | $structure_str = iil_C_FetchStructureString($this->conn, $this->mailbox, $msg_id); |
| | | $structure_str = iil_C_FetchStructureString($this->conn, $this->mailbox, $uid, true); |
| | | $structure = iml_GetRawStructureArray($structure_str); |
| | | $struct = false; |
| | | |
| | | // parse structure and add headers |
| | | if (!empty($structure)) |
| | | { |
| | | $this->_msg_id = $msg_id; |
| | | $headers = $this->get_headers($uid); |
| | | $this->_msg_id = $headers->id; |
| | | |
| | | // set message charset from message headers |
| | | if ($headers->charset) |
| | |
| | | |
| | | // write structure to cache |
| | | if ($this->caching_enabled) |
| | | $this->add_message_cache($cache_key, $msg_id, $headers, $struct); |
| | | $this->add_message_cache($cache_key, $this->_msg_id, $headers, $struct); |
| | | } |
| | | |
| | | return $struct; |
| | |
| | | // fetch message headers if message/rfc822 or named part (could contain Content-Location header) |
| | | if ($struct->ctype_primary == 'message' || ($struct->ctype_parameters['name'] && !$struct->content_id)) { |
| | | if (empty($raw_headers)) |
| | | $raw_headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, $struct->mime_id); |
| | | $raw_headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, false, $struct->mime_id); |
| | | $struct->headers = $this->_parse_headers($raw_headers) + $struct->headers; |
| | | } |
| | | |
| | |
| | | // we must fetch and parse headers "manually" |
| | | if ($i<2) { |
| | | if (!$headers) |
| | | $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, $part->mime_id); |
| | | $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, false, $part->mime_id); |
| | | $filename_mime = ''; |
| | | $i = 0; |
| | | while (preg_match('/filename\*'.$i.'\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) { |
| | |
| | | } |
| | | if ($i<2) { |
| | | if (!$headers) |
| | | $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, $part->mime_id); |
| | | $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, false, $part->mime_id); |
| | | $filename_encoded = ''; |
| | | $i = 0; $matches = array(); |
| | | while (preg_match('/filename\*'.$i.'\*\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) { |
| | |
| | | } |
| | | if ($i<2) { |
| | | if (!$headers) |
| | | $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, $part->mime_id); |
| | | $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, false, $part->mime_id); |
| | | $filename_mime = ''; |
| | | $i = 0; $matches = array(); |
| | | while (preg_match('/\s+name\*'.$i.'\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) { |
| | |
| | | } |
| | | if ($i<2) { |
| | | if (!$headers) |
| | | $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, $part->mime_id); |
| | | $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $this->_msg_id, false, $part->mime_id); |
| | | $filename_encoded = ''; |
| | | $i = 0; $matches = array(); |
| | | while (preg_match('/\s+name\*'.$i.'\*\s*=\s*"*([^"\n;]+)[";]*/', $headers, $matches)) { |
| | |
| | | */ |
| | | function &get_message_part($uid, $part=1, $o_part=NULL, $print=NULL, $fp=NULL) |
| | | { |
| | | if (!($msg_id = $this->_uid2id($uid))) |
| | | return FALSE; |
| | | |
| | | // get part encoding if not provided |
| | | if (!is_object($o_part)) |
| | | { |
| | | $structure_str = iil_C_FetchStructureString($this->conn, $this->mailbox, $msg_id); |
| | | $structure_str = iil_C_FetchStructureString($this->conn, $this->mailbox, $uid, true); |
| | | $structure = iml_GetRawStructureArray($structure_str); |
| | | // error or message not found |
| | | if (empty($structure)) |
| | | return false; |
| | | |
| | | $part_type = iml_GetPartTypeCode($structure, $part); |
| | | $o_part = new rcube_message_part; |
| | | $o_part->ctype_primary = $part_type==0 ? 'text' : ($part_type==2 ? 'message' : 'other'); |
| | |
| | | |
| | | if (!$part) $part = 'TEXT'; |
| | | |
| | | $body = iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, $part, |
| | | $body = iil_C_HandlePartBody($this->conn, $this->mailbox, $uid, true, $part, |
| | | $o_part->encoding, $print, $fp); |
| | | |
| | | |
| | | if ($fp || $print) |
| | | return true; |
| | | |
| | |
| | | */ |
| | | function &get_raw_body($uid) |
| | | { |
| | | if (!($msg_id = $this->_uid2id($uid))) |
| | | return FALSE; |
| | | |
| | | return iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id); |
| | | return iil_C_HandlePartBody($this->conn, $this->mailbox, $uid, true); |
| | | } |
| | | |
| | | |
| | |
| | | */ |
| | | function &get_raw_headers($uid) |
| | | { |
| | | if (!($msg_id = $this->_uid2id($uid))) |
| | | return FALSE; |
| | | |
| | | $headers = iil_C_FetchPartHeader($this->conn, $this->mailbox, $msg_id, NULL); |
| | | |
| | | return $headers; |
| | | return iil_C_FetchPartHeader($this->conn, $this->mailbox, $uid, true); |
| | | } |
| | | |
| | | |
| | |
| | | */ |
| | | function print_raw_body($uid) |
| | | { |
| | | if (!($msg_id = $this->_uid2id($uid))) |
| | | return FALSE; |
| | | |
| | | iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, NULL, NULL, true); |
| | | iil_C_HandlePartBody($this->conn, $this->mailbox, $uid, true, NULL, NULL, true); |
| | | } |
| | | |
| | | |
| | |
| | | if (!is_array($uids)) |
| | | $uids = explode(',',$uids); |
| | | |
| | | if ($flag=='UNDELETED') |
| | | $result = iil_C_Undelete($this->conn, $mailbox, join(',', $uids)); |
| | | else if ($flag=='UNSEEN') |
| | | $result = iil_C_Unseen($this->conn, $mailbox, join(',', $uids)); |
| | | else if ($flag=='UNFLAGGED') |
| | | $result = iil_C_UnFlag($this->conn, $mailbox, join(',', $uids), 'FLAGGED'); |
| | | if (strpos($flag, 'UN') === 0) |
| | | $result = iil_C_UnFlag($this->conn, $mailbox, join(',', $uids), substr($flag, 2)); |
| | | else |
| | | $result = iil_C_Flag($this->conn, $mailbox, join(',', $uids), $flag); |
| | | |
| | |
| | | $this->_set_messagecount($mailbox, 'ALL', $count*(-1)); |
| | | |
| | | return $result; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Remove message flag for one or several messages |
| | | * |
| | | * @param mixed Message UIDs as array or as comma-separated string |
| | | * @param string Flag to unset: SEEN, DELETED, RECENT, ANSWERED, DRAFT, MDNSENT |
| | | * @param string Folder name |
| | | * @return boolean True on success, False on failure |
| | | * @see set_flag |
| | | */ |
| | | function unset_flag($uids, $flag, $mbox_name=NULL) |
| | | { |
| | | return $this->set_flag($uids, 'UN'.$flag, $mbox_name); |
| | | } |
| | | |
| | | |
| | |
| | | $key, |
| | | $index, |
| | | $headers->uid, |
| | | (string)rc_substr($this->db->encode($this->decode_header($headers->subject, TRUE)), 0, 128), |
| | | (string)rc_substr($this->db->encode($this->decode_header($headers->from, TRUE)), 0, 128), |
| | | (string)rc_substr($this->db->encode($this->decode_header($headers->to, TRUE)), 0, 128), |
| | | (string)rc_substr($this->db->encode($this->decode_header($headers->cc, TRUE)), 0, 128), |
| | | (string)mb_substr($this->db->encode($this->decode_header($headers->subject, TRUE)), 0, 128), |
| | | (string)mb_substr($this->db->encode($this->decode_header($headers->from, TRUE)), 0, 128), |
| | | (string)mb_substr($this->db->encode($this->decode_header($headers->to, TRUE)), 0, 128), |
| | | (string)mb_substr($this->db->encode($this->decode_header($headers->cc, TRUE)), 0, 128), |
| | | (int)$headers->size, |
| | | serialize($this->db->encode(clone $headers)), |
| | | is_object($struct) ? serialize($this->db->encode(clone $struct)) : NULL |
| | |
| | | if (($p = array_search(strtolower($folder), $this->default_folders_lc)) !== false && !$a_defaults[$p]) |
| | | $a_defaults[$p] = $folder; |
| | | else |
| | | $folders[$folder] = rc_strtolower(rcube_charset_convert($folder, 'UTF7-IMAP')); |
| | | $folders[$folder] = mb_strtolower(rcube_charset_convert($folder, 'UTF7-IMAP')); |
| | | } |
| | | |
| | | // sort folders and place defaults on the top |
| | |
| | | private function _change_subscription($a_mboxes, $mode) |
| | | { |
| | | $updated = FALSE; |
| | | |
| | | |
| | | if (is_array($a_mboxes)) |
| | | foreach ($a_mboxes as $i => $mbox_name) |
| | | { |
| | |
| | | $a_mboxes[$i] = $mailbox; |
| | | |
| | | if ($mode=='subscribe') |
| | | $result = iil_C_Subscribe($this->conn, $mailbox); |
| | | $updated = iil_C_Subscribe($this->conn, $mailbox); |
| | | else if ($mode=='unsubscribe') |
| | | $result = iil_C_UnSubscribe($this->conn, $mailbox); |
| | | |
| | | if ($result>=0) |
| | | $updated = TRUE; |
| | | $updated = iil_C_UnSubscribe($this->conn, $mailbox); |
| | | } |
| | | |
| | | // get cached mailbox list |
| | | |
| | | // get cached mailbox list |
| | | if ($updated) |
| | | { |
| | | $a_mailbox_cache = $this->get_cache('mailboxes'); |
| | |
| | | $a_mailbox_cache = array_merge($a_mailbox_cache, $a_mboxes); |
| | | else if ($mode=='unsubscribe') |
| | | $a_mailbox_cache = array_diff($a_mailbox_cache, $a_mboxes); |
| | | |
| | | |
| | | // write mailboxlist to cache |
| | | $this->update_cache('mailboxes', $this->_sort_mailbox_list($a_mailbox_cache)); |
| | | } |