4 +-----------------------------------------------------------------------+
5 | program/include/rcube_imap.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2005-2006, RoundCube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | IMAP wrapper that implements the Iloha IMAP Library (IIL) |
13 | See http://ilohamail.org/ for details |
15 +-----------------------------------------------------------------------+
16 | Author: Thomas Bruederli <roundcube@gmail.com> |
17 +-----------------------------------------------------------------------+
19 $Id: rcube_imap.inc 883 2007-10-17 21:27:20Z thomasb $
25 * Obtain classes from the Iloha IMAP library
27 require_once('lib/imap.inc');
28 require_once('lib/mime.inc');
32 * Interface class for accessing an IMAP server
34 * This is a wrapper that implements the Iloha IMAP Library (IIL)
37 * @author Thomas Bruederli <roundcube@gmail.com>
39 * @link http://ilohamail.org
47 var $mailbox = 'INBOX';
50 var $sort_field = 'date';
51 var $sort_order = 'DESC';
52 var $delimiter = NULL;
53 var $caching_enabled = FALSE;
54 var $default_folders = array('INBOX');
55 var $default_folders_lc = array('inbox');
57 var $cache_keys = array();
58 var $cache_changes = array();
59 var $uid_id_map = array();
60 var $msg_headers = array();
61 var $capabilities = array();
62 var $skip_deleted = FALSE;
63 var $search_set = NULL;
64 var $search_subject = '';
65 var $search_string = '';
66 var $search_charset = '';
74 * @param object DB Database connection
76 function __construct($db_conn)
83 * PHP 4 object constructor
85 * @see rcube_imap::__construct
87 function rcube_imap($db_conn)
89 $this->__construct($db_conn);
94 * Connect to an IMAP server
96 * @param string Host to connect
97 * @param string Username for IMAP account
98 * @param string Password for IMAP account
99 * @param number Port to connect to
100 * @param boolean Use SSL connection
101 * @return boolean TRUE on success, FALSE on failure
104 function connect($host, $user, $pass, $port=143, $use_ssl=FALSE)
106 global $ICL_SSL, $ICL_PORT, $IMAP_USE_INTERNAL_DATE;
108 // check for Open-SSL support in PHP build
109 if ($use_ssl && in_array('openssl', get_loaded_extensions()))
113 raise_error(array('code' => 403, 'type' => 'imap', 'file' => __FILE__,
114 'message' => 'Open SSL not available;'), TRUE, FALSE);
119 $IMAP_USE_INTERNAL_DATE = false;
121 $this->conn = iil_Connect($host, $user, $pass, array('imap' => 'check'));
126 $this->ssl = $use_ssl;
128 // print trace mesages
129 if ($this->conn && ($this->debug_level & 8))
130 console($this->conn->message);
133 else if (!$this->conn && $GLOBALS['iil_error'])
135 $this->error_code = $GLOBALS['iil_errornum'];
136 raise_error(array('code' => 403,
138 'message' => $GLOBALS['iil_error']), TRUE, FALSE);
141 // get server properties
144 $this->_parse_capability($this->conn->capability);
146 if (!empty($this->conn->delimiter))
147 $this->delimiter = $this->conn->delimiter;
148 if (!empty($this->conn->rootdir))
150 $this->set_rootdir($this->conn->rootdir);
151 $this->root_ns = ereg_replace('[\.\/]$', '', $this->conn->rootdir);
155 return $this->conn ? TRUE : FALSE;
160 * Close IMAP connection
161 * Usually done on script shutdown
168 iil_Close($this->conn);
173 * Close IMAP connection and re-connect
174 * This is used to avoid some strange socket errors when talking to Courier IMAP
181 $this->connect($this->host, $this->user, $this->pass, $this->port, $this->ssl);
186 * Set a root folder for the IMAP connection.
188 * Only folders within this root folder will be displayed
189 * and all folder paths will be translated using this folder name
191 * @param string Root folder
194 function set_rootdir($root)
196 if (ereg('[\.\/]$', $root)) //(substr($root, -1, 1)==='/')
197 $root = substr($root, 0, -1);
199 $this->root_dir = $root;
201 if (empty($this->delimiter))
202 $this->get_hierarchy_delimiter();
207 * This list of folders will be listed above all other folders
209 * @param array Indexed list of folder names
212 function set_default_mailboxes($arr)
216 $this->default_folders = $arr;
217 $this->default_folders_lc = array();
219 // add inbox if not included
220 if (!in_array_nocase('INBOX', $this->default_folders))
221 array_unshift($this->default_folders, 'INBOX');
223 // create a second list with lower cased names
224 foreach ($this->default_folders as $mbox)
225 $this->default_folders_lc[] = strtolower($mbox);
231 * Set internal mailbox reference.
233 * All operations will be perfomed on this mailbox/folder
235 * @param string Mailbox/Folder name
238 function set_mailbox($new_mbox)
240 $mailbox = $this->_mod_mailbox($new_mbox);
242 if ($this->mailbox == $mailbox)
245 $this->mailbox = $mailbox;
247 // clear messagecount cache for this mailbox
248 $this->_clear_messagecount($mailbox);
253 * Set internal list page
255 * @param number Page number to list
258 function set_page($page)
260 $this->list_page = (int)$page;
265 * Set internal page size
267 * @param number Number of messages to display on one page
270 function set_pagesize($size)
272 $this->page_size = (int)$size;
277 * Save a set of message ids for future message listing methods
279 * @param array List of IMAP fields to search in
280 * @param string Search string
281 * @param array List of message ids or NULL if empty
283 function set_search_set($subject, $str=null, $msgs=null, $charset=null)
285 if (is_array($subject) && $str == null && $msgs == null)
286 list($subject, $str, $msgs, $charset) = $subject;
287 if ($msgs != null && !is_array($msgs))
288 $msgs = split(',', $msgs);
290 $this->search_subject = $subject;
291 $this->search_string = $str;
292 $this->search_set = (array)$msgs;
293 $this->search_charset = $charset;
298 * Return the saved search set as hash array
299 * @return array Search set
301 function get_search_set()
303 return array($this->search_subject, $this->search_string, $this->search_set, $this->search_charset);
308 * Returns the currently used mailbox name
310 * @return string Name of the mailbox/folder
313 function get_mailbox_name()
315 return $this->conn ? $this->_mod_mailbox($this->mailbox, 'out') : '';
320 * Returns the IMAP server's capability
322 * @param string Capability name
323 * @return mixed Capability value or TRUE if supported, FALSE if not
326 function get_capability($cap)
328 $cap = strtoupper($cap);
329 return $this->capabilities[$cap];
334 * Returns the delimiter that is used by the IMAP server for folder separation
336 * @return string Delimiter string
339 function get_hierarchy_delimiter()
341 if ($this->conn && empty($this->delimiter))
342 $this->delimiter = iil_C_GetHierarchyDelimiter($this->conn);
344 if (empty($this->delimiter))
345 $this->delimiter = '/';
347 return $this->delimiter;
352 * Public method for mailbox listing.
354 * Converts mailbox name with root dir first
356 * @param string Optional root folder
357 * @param string Optional filter for mailbox listing
358 * @return array List of mailboxes/folders
361 function list_mailboxes($root='', $filter='*')
364 $a_mboxes = $this->_list_mailboxes($root, $filter);
366 foreach ($a_mboxes as $mbox_row)
368 $name = $this->_mod_mailbox($mbox_row, 'out');
373 // INBOX should always be available
374 if (!in_array_nocase('INBOX', $a_out))
375 array_unshift($a_out, 'INBOX');
378 $a_out = $this->_sort_mailbox_list($a_out);
385 * Private method for mailbox listing
387 * @return array List of mailboxes/folders
388 * @see rcube_imap::list_mailboxes()
391 function _list_mailboxes($root='', $filter='*')
393 $a_defaults = $a_out = array();
395 // get cached folder list
396 $a_mboxes = $this->get_cache('mailboxes');
397 if (is_array($a_mboxes))
400 // retrieve list of folders from IMAP server
401 $a_folders = iil_C_ListSubscribed($this->conn, $this->_mod_mailbox($root), $filter);
403 if (!is_array($a_folders) || !sizeof($a_folders))
404 $a_folders = array();
406 // write mailboxlist to cache
407 $this->update_cache('mailboxes', $a_folders);
414 * Get message count for a specific mailbox
416 * @param string Mailbox/folder name
417 * @param string Mode for count [ALL|UNSEEN|RECENT]
418 * @param boolean Force reading from server and update cache
419 * @return int Number of messages
422 function messagecount($mbox_name='', $mode='ALL', $force=FALSE)
424 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
425 return $this->_messagecount($mailbox, $mode, $force);
430 * Private method for getting nr of messages
433 * @see rcube_imap::messagecount()
435 function _messagecount($mailbox='', $mode='ALL', $force=FALSE)
437 $a_mailbox_cache = FALSE;
438 $mode = strtoupper($mode);
441 $mailbox = $this->mailbox;
444 if ($this->search_string && $mailbox == $this->mailbox && $mode == 'ALL')
445 return count((array)$this->search_set);
447 $a_mailbox_cache = $this->get_cache('messagecount');
449 // return cached value
450 if (!$force && is_array($a_mailbox_cache[$mailbox]) && isset($a_mailbox_cache[$mailbox][$mode]))
451 return $a_mailbox_cache[$mailbox][$mode];
453 // RECENT count is fetched abit different
454 if ($mode == 'RECENT')
455 $count = iil_C_CheckForRecent($this->conn, $mailbox);
457 // use SEARCH for message counting
458 else if ($this->skip_deleted)
460 $search_str = "ALL UNDELETED";
462 // get message count and store in cache
463 if ($mode == 'UNSEEN')
464 $search_str .= " UNSEEN";
466 // get message count using SEARCH
467 // not very performant but more precise (using UNDELETED)
469 $index = $this->_search_index($mailbox, $search_str);
470 if (is_array($index))
472 $str = implode(",", $index);
474 $count = count($index);
479 if ($mode == 'UNSEEN')
480 $count = iil_C_CountUnseen($this->conn, $mailbox);
482 $count = iil_C_CountMessages($this->conn, $mailbox);
485 if (!is_array($a_mailbox_cache[$mailbox]))
486 $a_mailbox_cache[$mailbox] = array();
488 $a_mailbox_cache[$mailbox][$mode] = (int)$count;
490 // write back to cache
491 $this->update_cache('messagecount', $a_mailbox_cache);
498 * Public method for listing headers
499 * convert mailbox name with root dir first
501 * @param string Mailbox/folder name
502 * @param int Current page to list
503 * @param string Header field to sort by
504 * @param string Sort order [ASC|DESC]
505 * @return array Indexed array with message header objects
508 function list_headers($mbox_name='', $page=NULL, $sort_field=NULL, $sort_order=NULL)
510 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
511 return $this->_list_headers($mailbox, $page, $sort_field, $sort_order);
516 * Private method for listing message headers
519 * @see rcube_imap::list_headers
521 function _list_headers($mailbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $recursive=FALSE)
523 if (!strlen($mailbox))
526 // use saved message set
527 if ($this->search_string && $mailbox == $this->mailbox)
528 return $this->_list_header_set($mailbox, $this->search_set, $page, $sort_field, $sort_order);
530 if ($sort_field!=NULL)
531 $this->sort_field = $sort_field;
532 if ($sort_order!=NULL)
533 $this->sort_order = strtoupper($sort_order);
535 $max = $this->_messagecount($mailbox);
536 $start_msg = ($this->list_page-1) * $this->page_size;
538 list($begin, $end) = $this->_get_message_range($max, $page);
544 $headers_sorted = FALSE;
545 $cache_key = $mailbox.'.msg';
546 $cache_status = $this->check_cache_status($mailbox, $cache_key);
548 // cache is OK, we can get all messages from local cache
551 $a_msg_headers = $this->get_message_cache($cache_key, $start_msg, $start_msg+$this->page_size, $this->sort_field, $this->sort_order);
552 $headers_sorted = TRUE;
554 // cache is dirty, sync it
555 else if ($this->caching_enabled && $cache_status==-1 && !$recursive)
557 $this->sync_header_index($mailbox);
558 return $this->_list_headers($mailbox, $page, $this->sort_field, $this->sort_order, TRUE);
562 // retrieve headers from IMAP
563 if ($this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : '')))
565 $msgs = $msg_index[$begin];
566 for ($i=$begin+1; $i < $end; $i++)
567 $msgs = $msgs.','.$msg_index[$i];
571 $msgs = sprintf("%d:%d", $begin+1, $end);
574 for ($msg_seqnum = $begin; $msg_seqnum <= $end; $msg_seqnum++)
575 $msg_index[$i++] = $msg_seqnum;
578 // use this class for message sorting
579 $sorter = new rcube_header_sorter();
580 $sorter->set_sequence_numbers($msg_index);
582 // fetch reuested headers from server
583 $a_msg_headers = array();
584 $deleted_count = $this->_fetch_headers($mailbox, $msgs, $a_msg_headers, $cache_key);
586 // delete cached messages with a higher index than $max+1
587 // Changed $max to $max+1 to fix this bug : #1484295
588 $this->clear_message_cache($cache_key, $max + 1);
591 // kick child process to sync cache
597 // return empty array if no messages found
598 if (!is_array($a_msg_headers) || empty($a_msg_headers))
602 // if not already sorted
603 if (!$headers_sorted)
605 $sorter->sort_headers($a_msg_headers);
607 if ($this->sort_order == 'DESC')
608 $a_msg_headers = array_reverse($a_msg_headers);
611 return array_values($a_msg_headers);
617 * Public method for listing a specific set of headers
618 * convert mailbox name with root dir first
620 * @param string Mailbox/folder name
621 * @param array List of message ids to list
622 * @param int Current page to list
623 * @param string Header field to sort by
624 * @param string Sort order [ASC|DESC]
625 * @return array Indexed array with message header objects
628 function list_header_set($mbox_name='', $msgs, $page=NULL, $sort_field=NULL, $sort_order=NULL)
630 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
631 return $this->_list_header_set($mailbox, $msgs, $page, $sort_field, $sort_order);
636 * Private method for listing a set of message headers
639 * @see rcube_imap::list_header_set()
641 function _list_header_set($mailbox, $msgs, $page=NULL, $sort_field=NULL, $sort_order=NULL)
643 // also accept a comma-separated list of message ids
644 if (is_string($msgs))
645 $msgs = split(',', $msgs);
647 if (!strlen($mailbox) || empty($msgs))
650 if ($sort_field!=NULL)
651 $this->sort_field = $sort_field;
652 if ($sort_order!=NULL)
653 $this->sort_order = strtoupper($sort_order);
656 $start_msg = ($this->list_page-1) * $this->page_size;
658 // fetch reuested headers from server
659 $a_msg_headers = array();
660 $this->_fetch_headers($mailbox, join(',', $msgs), $a_msg_headers, NULL);
662 // return empty array if no messages found
663 if (!is_array($a_msg_headers) || empty($a_msg_headers))
666 // if not already sorted
667 $a_msg_headers = iil_SortHeaders($a_msg_headers, $this->sort_field, $this->sort_order);
669 // only return the requested part of the set
670 return array_slice(array_values($a_msg_headers), $start_msg, min($max-$start_msg, $this->page_size));
675 * Helper function to get first and last index of the requested set
677 * @param int message count
678 * @param mixed page number to show, or string 'all'
679 * @return array array with two values: first index, last index
682 function _get_message_range($max, $page)
684 $start_msg = ($this->list_page-1) * $this->page_size;
691 else if ($this->sort_order=='DESC')
693 $begin = $max - $this->page_size - $start_msg;
694 $end = $max - $start_msg;
699 $end = $start_msg + $this->page_size;
702 if ($begin < 0) $begin = 0;
703 if ($end < 0) $end = $max;
704 if ($end > $max) $end = $max;
706 return array($begin, $end);
712 * Fetches message headers
715 * @param string Mailbox name
716 * @param string Message index to fetch
717 * @param array Reference to message headers array
718 * @param array Array with cache index
719 * @return int Number of deleted messages
722 function _fetch_headers($mailbox, $msgs, &$a_msg_headers, $cache_key)
724 // cache is incomplete
725 $cache_index = $this->get_message_cache_index($cache_key);
727 // fetch reuested headers from server
728 $a_header_index = iil_C_FetchHeaders($this->conn, $mailbox, $msgs);
731 if (!empty($a_header_index))
733 foreach ($a_header_index as $i => $headers)
735 if ($headers->deleted && $this->skip_deleted)
738 if ($cache_index[$headers->id] && $cache_index[$headers->id] == $headers->uid)
739 $this->remove_message_cache($cache_key, $headers->id);
745 // add message to cache
746 if ($this->caching_enabled && $cache_index[$headers->id] != $headers->uid)
747 $this->add_message_cache($cache_key, $headers->id, $headers);
749 $a_msg_headers[$headers->uid] = $headers;
753 return $deleted_count;
758 * Return sorted array of message UIDs
760 * @param string Mailbox to get index from
761 * @param string Sort column
762 * @param string Sort order [ASC, DESC]
763 * @return array Indexed array with message ids
765 function message_index($mbox_name='', $sort_field=NULL, $sort_order=NULL)
767 if ($sort_field!=NULL)
768 $this->sort_field = $sort_field;
769 if ($sort_order!=NULL)
770 $this->sort_order = strtoupper($sort_order);
772 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
773 $key = "$mbox:".$this->sort_field.":".$this->sort_order.".msgi";
775 // have stored it in RAM
776 if (isset($this->cache[$key]))
777 return $this->cache[$key];
780 $cache_key = $mailbox.'.msg';
781 $cache_status = $this->check_cache_status($mailbox, $cache_key);
786 $a_index = $this->get_message_cache_index($cache_key, TRUE, $this->sort_field, $this->sort_order);
787 return array_values($a_index);
791 // fetch complete message index
792 $msg_count = $this->_messagecount($mailbox);
793 if ($this->get_capability('sort') && ($a_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, '', TRUE)))
795 if ($this->sort_order == 'DESC')
796 $a_index = array_reverse($a_index);
798 $this->cache[$key] = $a_index;
803 $a_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, "1:$msg_count", $this->sort_field);
804 $a_uids = iil_C_FetchUIDs($this->conn, $mailbox);
806 if ($this->sort_order=="ASC")
808 else if ($this->sort_order=="DESC")
812 $this->cache[$key] = array();
813 foreach ($a_index as $index => $value)
814 $this->cache[$key][$i++] = $a_uids[$index];
817 return $this->cache[$key];
824 function sync_header_index($mailbox)
826 $cache_key = $mailbox.'.msg';
827 $cache_index = $this->get_message_cache_index($cache_key);
828 $msg_count = $this->_messagecount($mailbox);
830 // fetch complete message index
831 $a_message_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, "1:$msg_count", 'UID');
833 foreach ($a_message_index as $id => $uid)
835 // message in cache at correct position
836 if ($cache_index[$id] == $uid)
838 unset($cache_index[$id]);
842 // message in cache but in wrong position
843 if (in_array((string)$uid, $cache_index, TRUE))
845 unset($cache_index[$id]);
848 // other message at this position
849 if (isset($cache_index[$id]))
851 $this->remove_message_cache($cache_key, $id);
852 unset($cache_index[$id]);
856 // fetch complete headers and add to cache
857 $headers = iil_C_FetchHeader($this->conn, $mailbox, $id);
858 $this->add_message_cache($cache_key, $headers->id, $headers);
861 // those ids that are still in cache_index have been deleted
862 if (!empty($cache_index))
864 foreach ($cache_index as $id => $uid)
865 $this->remove_message_cache($cache_key, $id);
871 * Invoke search request to IMAP server
873 * @param string mailbox name to search in
874 * @param string search criteria (ALL, TO, FROM, SUBJECT, etc)
875 * @param string search string
876 * @return array search results as list of message ids
879 function search($mbox_name='', $criteria='ALL', $str=NULL, $charset=NULL)
881 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
883 // have an array of criterias => execute multiple searches
884 if (is_array($criteria) && $str)
887 foreach ($criteria as $crit)
888 if ($search_result = $this->search($mbox_name, $crit, $str, $charset))
889 $results = array_merge($results, $search_result);
891 $results = array_unique($results);
892 $this->set_search_set($criteria, $str, $results, $charset);
895 else if ($str && $criteria)
897 $search = (!empty($charset) ? "CHARSET $charset " : '') . sprintf("%s {%d}\r\n%s", $criteria, strlen($str), $str);
898 $results = $this->_search_index($mailbox, $search);
900 // try search with ISO charset (should be supported by server)
901 if (empty($results) && !empty($charset) && $charset!='ISO-8859-1')
902 $results = $this->search($mbox_name, $criteria, rcube_charset_convert($str, $charset, 'ISO-8859-1'), 'ISO-8859-1');
904 $this->set_search_set($criteria, $str, $results, $charset);
908 return $this->_search_index($mailbox, $criteria);
913 * Private search method
915 * @return array search results as list of message ids
917 * @see rcube_imap::search()
919 function _search_index($mailbox, $criteria='ALL')
921 $a_messages = iil_C_Search($this->conn, $mailbox, $criteria);
922 // clean message list (there might be some empty entries)
923 if (is_array($a_messages))
925 foreach ($a_messages as $i => $val)
927 unset($a_messages[$i]);
935 * Refresh saved search set
937 * @return array Current search set
939 function refresh_search()
941 if (!empty($this->search_subject) && !empty($this->search_string))
942 $this->search_set = $this->search('', $this->search_subject, $this->search_string, $this->search_charset);
944 return $this->get_search_set();
949 * Return message headers object of a specific message
951 * @param int Message ID
952 * @param string Mailbox to read from
953 * @param boolean True if $id is the message UID
954 * @return object Message headers representation
956 function get_headers($id, $mbox_name=NULL, $is_uid=TRUE)
958 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
959 $uid = $is_uid ? $id : $this->_id2uid($id);
961 // get cached headers
962 if ($uid && ($headers = &$this->get_cached_message($mailbox.'.msg', $uid)))
965 $headers = iil_C_FetchHeader($this->conn, $mailbox, $id, $is_uid);
967 // write headers cache
971 $this->uid_id_map[$mbox_name][$uid] = $headers->id;
973 $this->add_message_cache($mailbox.'.msg', $headers->id, $headers);
981 * Fetch body structure from the IMAP server and build
982 * an object structure similar to the one generated by PEAR::Mail_mimeDecode
984 * @param int Message UID to fetch
985 * @return object stdClass Message part tree or False on failure
987 function &get_structure($uid)
989 $cache_key = $this->mailbox.'.msg';
990 $headers = &$this->get_cached_message($cache_key, $uid, true);
992 // return cached message structure
993 if (is_object($headers) && is_object($headers->structure))
994 return $headers->structure;
996 // resolve message sequence number
997 if (!($msg_id = $this->_uid2id($uid)))
1000 $structure_str = iil_C_FetchStructureString($this->conn, $this->mailbox, $msg_id);
1001 $structure = iml_GetRawStructureArray($structure_str);
1004 // parse structure and add headers
1005 if (!empty($structure))
1007 $this->_msg_id = $msg_id;
1008 $headers = $this->get_headers($msg_id, NULL, FALSE);
1010 $struct = &$this->_structure_part($structure);
1011 $struct->headers = get_object_vars($headers);
1013 // don't trust given content-type
1014 if (empty($struct->parts) && !empty($struct->headers['ctype']))
1016 $struct->mime_id = '1';
1017 $struct->mimetype = strtolower($struct->headers['ctype']);
1018 list($struct->ctype_primary, $struct->ctype_secondary) = explode('/', $struct->mimetype);
1021 // write structure to cache
1022 if ($this->caching_enabled)
1023 $this->add_message_cache($cache_key, $msg_id, $headers, $struct);
1031 * Build message part object
1035 function &_structure_part($part, $count=0, $parent='')
1037 $struct = new rcube_message_part;
1038 $struct->mime_id = empty($parent) ? (string)$count : "$parent.$count";
1041 if (is_array($part[0]))
1043 $struct->ctype_primary = 'multipart';
1045 // find first non-array entry
1046 for ($i=1; count($part); $i++)
1047 if (!is_array($part[$i]))
1049 $struct->ctype_secondary = strtolower($part[$i]);
1053 $struct->mimetype = 'multipart/'.$struct->ctype_secondary;
1055 $struct->parts = array();
1056 for ($i=0, $count=0; $i<count($part); $i++)
1057 if (is_array($part[$i]) && count($part[$i]) > 5)
1058 $struct->parts[] = $this->_structure_part($part[$i], ++$count, $struct->mime_id);
1065 $struct->ctype_primary = strtolower($part[0]);
1066 $struct->ctype_secondary = strtolower($part[1]);
1067 $struct->mimetype = $struct->ctype_primary.'/'.$struct->ctype_secondary;
1069 // read content type parameters
1070 if (is_array($part[2]))
1072 $struct->ctype_parameters = array();
1073 for ($i=0; $i<count($part[2]); $i+=2)
1074 $struct->ctype_parameters[strtolower($part[2][$i])] = $part[2][$i+1];
1076 if (isset($struct->ctype_parameters['charset']))
1077 $struct->charset = $struct->ctype_parameters['charset'];
1080 // read content encoding
1081 if (!empty($part[5]) && $part[5]!='NIL')
1083 $struct->encoding = strtolower($part[5]);
1084 $struct->headers['content-transfer-encoding'] = $struct->encoding;
1088 if (!empty($part[6]) && $part[6]!='NIL')
1089 $struct->size = intval($part[6]);
1091 // read part disposition
1092 $di = count($part) - 2;
1093 if ((is_array($part[$di]) && count($part[$di]) == 2 && is_array($part[$di][1])) ||
1094 (is_array($part[--$di]) && count($part[$di]) == 2))
1096 $struct->disposition = strtolower($part[$di][0]);
1098 if (is_array($part[$di][1]))
1099 for ($n=0; $n<count($part[$di][1]); $n+=2)
1100 $struct->d_parameters[strtolower($part[$di][1][$n])] = $part[$di][1][$n+1];
1104 if (is_array($part[8]) && $di != 8)
1106 $struct->parts = array();
1107 for ($i=0, $count=0; $i<count($part[8]); $i++)
1108 if (is_array($part[8][$i]) && count($part[8][$i]) > 5)
1109 $struct->parts[] = $this->_structure_part($part[8][$i], ++$count, $struct->mime_id);
1113 if (!empty($part[3]) && $part[3]!='NIL')
1115 $struct->content_id = $part[3];
1116 $struct->headers['content-id'] = $part[3];
1118 if (empty($struct->disposition))
1119 $struct->disposition = 'inline';
1122 // fetch message headers if message/rfc822
1123 if ($struct->ctype_primary=='message')
1125 $headers = iil_C_FetchPartBody($this->conn, $this->mailbox, $this->_msg_id, $struct->mime_id.'.HEADER');
1126 $struct->headers = $this->_parse_headers($headers);
1128 if (is_array($part[8]) && empty($struct->parts))
1129 $struct->parts[] = $this->_structure_part($part[8], ++$count, $struct->mime_id);
1132 // normalize filename property
1133 if (!empty($struct->d_parameters['filename']))
1134 $struct->filename = $this->decode_mime_string($struct->d_parameters['filename']);
1135 else if (!empty($struct->ctype_parameters['name']))
1136 $struct->filename = $this->decode_mime_string($struct->ctype_parameters['name']);
1137 else if (!empty($struct->headers['content-description']))
1138 $struct->filename = $this->decode_mime_string($struct->headers['content-description']);
1145 * Return a flat array with references to all parts, indexed by part numbers
1147 * @param object rcube_message_part Message body structure
1148 * @return Array with part number -> object pairs
1150 function get_mime_numbers(&$structure)
1153 $this->_get_part_numbers($structure, $a_parts);
1159 * Helper method for recursive calls
1163 function _get_part_numbers(&$part, &$a_parts)
1166 $a_parts[$part->mime_id] = &$part;
1168 if (is_array($part->parts))
1169 for ($i=0; $i<count($part->parts); $i++)
1170 $this->_get_part_numbers($part->parts[$i], $a_parts);
1175 * Fetch message body of a specific message from the server
1177 * @param int Message UID
1178 * @param string Part number
1179 * @param object rcube_message_part Part object created by get_structure()
1180 * @param mixed True to print part, ressource to write part contents in
1181 * @return string Message/part body if not printed
1183 function &get_message_part($uid, $part=1, $o_part=NULL, $print=NULL)
1185 if (!($msg_id = $this->_uid2id($uid)))
1188 // get part encoding if not provided
1189 if (!is_object($o_part))
1191 $structure_str = iil_C_FetchStructureString($this->conn, $this->mailbox, $msg_id);
1192 $structure = iml_GetRawStructureArray($structure_str);
1193 $part_type = iml_GetPartTypeCode($structure, $part);
1194 $o_part = new rcube_message_part;
1195 $o_part->ctype_primary = $part_type==0 ? 'text' : ($part_type==2 ? 'message' : 'other');
1196 $o_part->encoding = strtolower(iml_GetPartEncodingString($structure, $part));
1197 $o_part->charset = iml_GetPartCharset($structure, $part);
1200 // TODO: Add caching for message parts
1204 iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, $part, ($o_part->encoding=='base64'?3:2));
1209 $body = iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, $part, 1);
1212 if ($o_part->encoding=='base64' || $o_part->encoding=='quoted-printable')
1213 $body = $this->mime_decode($body, $o_part->encoding);
1215 // convert charset (if text or message part)
1216 if ($o_part->ctype_primary=='text' || $o_part->ctype_primary=='message')
1218 // assume ISO-8859-1 if no charset specified
1219 if (empty($o_part->charset))
1220 $o_part->charset = 'ISO-8859-1';
1222 $body = rcube_charset_convert($body, $o_part->charset);
1231 * Fetch message body of a specific message from the server
1233 * @param int Message UID
1234 * @return string Message/part body
1235 * @see rcube_imap::get_message_part()
1237 function &get_body($uid, $part=1)
1239 return $this->get_message_part($uid, $part);
1244 * Returns the whole message source as string
1246 * @param int Message UID
1247 * @return string Message source string
1249 function &get_raw_body($uid)
1251 if (!($msg_id = $this->_uid2id($uid)))
1254 $body = iil_C_FetchPartHeader($this->conn, $this->mailbox, $msg_id, NULL);
1255 $body .= iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, NULL, 1);
1262 * Sends the whole message source to stdout
1264 * @param int Message UID
1266 function print_raw_body($uid)
1268 if (!($msg_id = $this->_uid2id($uid)))
1271 print iil_C_FetchPartHeader($this->conn, $this->mailbox, $msg_id, NULL);
1273 iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, NULL, 2);
1278 * Set message flag to one or several messages
1280 * @param mixed Message UIDs as array or as comma-separated string
1281 * @param string Flag to set: SEEN, UNDELETED, DELETED, RECENT, ANSWERED, DRAFT
1282 * @return boolean True on success, False on failure
1284 function set_flag($uids, $flag)
1286 $flag = strtoupper($flag);
1288 if (!is_array($uids))
1289 $uids = explode(',',$uids);
1291 foreach ($uids as $uid) {
1292 $msg_ids[$uid] = $this->_uid2id($uid);
1295 if ($flag=='UNDELETED')
1296 $result = iil_C_Undelete($this->conn, $this->mailbox, join(',', array_values($msg_ids)));
1297 else if ($flag=='UNSEEN')
1298 $result = iil_C_Unseen($this->conn, $this->mailbox, join(',', array_values($msg_ids)));
1300 $result = iil_C_Flag($this->conn, $this->mailbox, join(',', array_values($msg_ids)), $flag);
1302 // reload message headers if cached
1303 $cache_key = $this->mailbox.'.msg';
1304 if ($this->caching_enabled)
1306 foreach ($msg_ids as $uid => $id)
1308 if ($cached_headers = $this->get_cached_message($cache_key, $uid))
1310 $this->remove_message_cache($cache_key, $id);
1311 //$this->get_headers($uid);
1315 // close and re-open connection
1316 // this prevents connection problems with Courier
1320 // set nr of messages that were flaged
1321 $count = count($msg_ids);
1323 // clear message count cache
1324 if ($result && $flag=='SEEN')
1325 $this->_set_messagecount($this->mailbox, 'UNSEEN', $count*(-1));
1326 else if ($result && $flag=='UNSEEN')
1327 $this->_set_messagecount($this->mailbox, 'UNSEEN', $count);
1328 else if ($result && $flag=='DELETED')
1329 $this->_set_messagecount($this->mailbox, 'ALL', $count*(-1));
1336 * Append a mail message (source) to a specific mailbox
1338 * @param string Target mailbox
1339 * @param string Message source
1340 * @return boolean True on success, False on error
1342 function save_message($mbox_name, &$message)
1344 $mbox_name = stripslashes($mbox_name);
1345 $mailbox = $this->_mod_mailbox($mbox_name);
1347 // make sure mailbox exists
1348 if (in_array($mailbox, $this->_list_mailboxes()))
1349 $saved = iil_C_Append($this->conn, $mailbox, $message);
1353 // increase messagecount of the target mailbox
1354 $this->_set_messagecount($mailbox, 'ALL', 1);
1362 * Move a message from one mailbox to another
1364 * @param string List of UIDs to move, separated by comma
1365 * @param string Target mailbox
1366 * @param string Source mailbox
1367 * @return boolean True on success, False on error
1369 function move_message($uids, $to_mbox, $from_mbox='')
1371 $to_mbox = stripslashes($to_mbox);
1372 $from_mbox = stripslashes($from_mbox);
1373 $to_mbox = $this->_mod_mailbox($to_mbox);
1374 $from_mbox = $from_mbox ? $this->_mod_mailbox($from_mbox) : $this->mailbox;
1376 // make sure mailbox exists
1377 if (!in_array($to_mbox, $this->_list_mailboxes()))
1379 if (in_array($to_mbox, $this->default_folders))
1380 $this->create_mailbox($to_mbox, TRUE);
1385 // convert the list of uids to array
1386 $a_uids = is_string($uids) ? explode(',', $uids) : (is_array($uids) ? $uids : NULL);
1388 // exit if no message uids are specified
1389 if (!is_array($a_uids))
1392 // convert uids to message ids
1394 foreach ($a_uids as $uid)
1395 $a_mids[] = $this->_uid2id($uid, $from_mbox);
1397 $iil_move = iil_C_Move($this->conn, join(',', $a_mids), $from_mbox, $to_mbox);
1398 $moved = !($iil_move === false || $iil_move < 0);
1400 // send expunge command in order to have the moved message
1401 // really deleted from the source mailbox
1404 $this->_expunge($from_mbox, FALSE);
1405 $this->_clear_messagecount($from_mbox);
1406 $this->_clear_messagecount($to_mbox);
1409 // remove message ids from search set
1410 if ($moved && $this->search_set && $from_mbox == $this->mailbox)
1411 $this->search_set = array_diff($this->search_set, $a_mids);
1413 // update cached message headers
1414 $cache_key = $from_mbox.'.msg';
1415 if ($moved && ($a_cache_index = $this->get_message_cache_index($cache_key)))
1417 $start_index = 100000;
1418 foreach ($a_uids as $uid)
1420 if (($index = array_search($uid, $a_cache_index)) !== FALSE)
1421 $start_index = min($index, $start_index);
1424 // clear cache from the lowest index on
1425 $this->clear_message_cache($cache_key, $start_index);
1433 * Mark messages as deleted and expunge mailbox
1435 * @param string List of UIDs to move, separated by comma
1436 * @param string Source mailbox
1437 * @return boolean True on success, False on error
1439 function delete_message($uids, $mbox_name='')
1441 $mbox_name = stripslashes($mbox_name);
1442 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
1444 // convert the list of uids to array
1445 $a_uids = is_string($uids) ? explode(',', $uids) : (is_array($uids) ? $uids : NULL);
1447 // exit if no message uids are specified
1448 if (!is_array($a_uids))
1452 // convert uids to message ids
1454 foreach ($a_uids as $uid)
1455 $a_mids[] = $this->_uid2id($uid, $mailbox);
1457 $deleted = iil_C_Delete($this->conn, $mailbox, join(',', $a_mids));
1459 // send expunge command in order to have the deleted message
1460 // really deleted from the mailbox
1463 $this->_expunge($mailbox, FALSE);
1464 $this->_clear_messagecount($mailbox);
1467 // remove message ids from search set
1468 if ($moved && $this->search_set && $mailbox == $this->mailbox)
1469 $this->search_set = array_diff($this->search_set, $a_mids);
1471 // remove deleted messages from cache
1472 $cache_key = $mailbox.'.msg';
1473 if ($deleted && ($a_cache_index = $this->get_message_cache_index($cache_key)))
1475 $start_index = 100000;
1476 foreach ($a_uids as $uid)
1478 if (($index = array_search($uid, $a_cache_index)) !== FALSE)
1479 $start_index = min($index, $start_index);
1482 // clear cache from the lowest index on
1483 $this->clear_message_cache($cache_key, $start_index);
1491 * Clear all messages in a specific mailbox
1493 * @param string Mailbox name
1494 * @return int Above 0 on success
1496 function clear_mailbox($mbox_name=NULL)
1498 $mbox_name = stripslashes($mbox_name);
1499 $mailbox = !empty($mbox_name) ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
1500 $msg_count = $this->_messagecount($mailbox, 'ALL');
1504 $cleared = iil_C_ClearFolder($this->conn, $mailbox);
1506 // make sure the message count cache is cleared as well
1509 $this->clear_message_cache($mailbox.'.msg');
1510 $a_mailbox_cache = $this->get_cache('messagecount');
1511 unset($a_mailbox_cache[$mailbox]);
1512 $this->update_cache('messagecount', $a_mailbox_cache);
1523 * Send IMAP expunge command and clear cache
1525 * @param string Mailbox name
1526 * @param boolean False if cache should not be cleared
1527 * @return boolean True on success
1529 function expunge($mbox_name='', $clear_cache=TRUE)
1531 $mbox_name = stripslashes($mbox_name);
1532 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
1533 return $this->_expunge($mailbox, $clear_cache);
1538 * Send IMAP expunge command and clear cache
1540 * @see rcube_imap::expunge()
1543 function _expunge($mailbox, $clear_cache=TRUE)
1545 $result = iil_C_Expunge($this->conn, $mailbox);
1547 if ($result>=0 && $clear_cache)
1549 $this->clear_message_cache($mailbox.'.msg');
1550 $this->_clear_messagecount($mailbox);
1557 /* --------------------------------
1559 * --------------------------------*/
1563 * Get a list of all folders available on the IMAP server
1565 * @param string IMAP root dir
1566 * @return array Indexed array with folder names
1568 function list_unsubscribed($root='')
1570 static $sa_unsubscribed;
1572 if (is_array($sa_unsubscribed))
1573 return $sa_unsubscribed;
1575 // retrieve list of folders from IMAP server
1576 $a_mboxes = iil_C_ListMailboxes($this->conn, $this->_mod_mailbox($root), '*');
1578 // modify names with root dir
1579 foreach ($a_mboxes as $mbox_name)
1581 $name = $this->_mod_mailbox($mbox_name, 'out');
1583 $a_folders[] = $name;
1586 // filter folders and sort them
1587 $sa_unsubscribed = $this->_sort_mailbox_list($a_folders);
1588 return $sa_unsubscribed;
1593 * Get mailbox quota information
1596 * @return mixed Quota info or False if not supported
1598 function get_quota()
1600 if ($this->get_capability('QUOTA'))
1601 return iil_C_GetQuota($this->conn);
1608 * Subscribe to a specific mailbox(es)
1610 * @param string Mailbox name(s)
1611 * @return boolean True on success
1613 function subscribe($mbox_name)
1615 if (is_array($mbox_name))
1616 $a_mboxes = $mbox_name;
1617 else if (is_string($mbox_name) && strlen($mbox_name))
1618 $a_mboxes = explode(',', $mbox_name);
1620 // let this common function do the main work
1621 return $this->_change_subscription($a_mboxes, 'subscribe');
1626 * Unsubscribe mailboxes
1628 * @param string Mailbox name(s)
1629 * @return boolean True on success
1631 function unsubscribe($mbox_name)
1633 if (is_array($mbox_name))
1634 $a_mboxes = $mbox_name;
1635 else if (is_string($mbox_name) && strlen($mbox_name))
1636 $a_mboxes = explode(',', $mbox_name);
1638 // let this common function do the main work
1639 return $this->_change_subscription($a_mboxes, 'unsubscribe');
1644 * Create a new mailbox on the server and register it in local cache
1646 * @param string New mailbox name (as utf-7 string)
1647 * @param boolean True if the new mailbox should be subscribed
1648 * @param string Name of the created mailbox, false on error
1650 function create_mailbox($name, $subscribe=FALSE)
1654 // replace backslashes
1655 $name = preg_replace('/[\\\]+/', '-', $name);
1657 // reduce mailbox name to 100 chars
1658 $name = substr($name, 0, 100);
1660 $abs_name = $this->_mod_mailbox($name);
1661 $a_mailbox_cache = $this->get_cache('mailboxes');
1663 if (strlen($abs_name) && (!is_array($a_mailbox_cache) || !in_array($abs_name, $a_mailbox_cache)))
1664 $result = iil_C_CreateFolder($this->conn, $abs_name);
1666 // try to subscribe it
1667 if ($result && $subscribe)
1668 $this->subscribe($name);
1670 return $result ? $name : FALSE;
1675 * Set a new name to an existing mailbox
1677 * @param string Mailbox to rename (as utf-7 string)
1678 * @param string New mailbox name (as utf-7 string)
1679 * @return string Name of the renames mailbox, False on error
1681 function rename_mailbox($mbox_name, $new_name)
1685 // replace backslashes
1686 $name = preg_replace('/[\\\]+/', '-', $new_name);
1688 // encode mailbox name and reduce it to 100 chars
1689 $name = substr($new_name, 0, 100);
1691 // make absolute path
1692 $mailbox = $this->_mod_mailbox($mbox_name);
1693 $abs_name = $this->_mod_mailbox($name);
1695 // check if mailbox is subscribed
1696 $a_subscribed = $this->_list_mailboxes();
1697 $subscribed = in_array($mailbox, $a_subscribed);
1699 // unsubscribe folder
1701 iil_C_UnSubscribe($this->conn, $mailbox);
1703 if (strlen($abs_name))
1704 $result = iil_C_RenameFolder($this->conn, $mailbox, $abs_name);
1708 $delm = $this->get_hierarchy_delimiter();
1710 // check if mailbox children are subscribed
1711 foreach ($a_subscribed as $c_subscribed)
1712 if (preg_match('/^'.preg_quote($mailbox.$delm, '/').'/', $c_subscribed))
1714 iil_C_UnSubscribe($this->conn, $c_subscribed);
1715 iil_C_Subscribe($this->conn, preg_replace('/^'.preg_quote($mailbox, '/').'/', $abs_name, $c_subscribed));
1719 $this->clear_message_cache($mailbox.'.msg');
1720 $this->clear_cache('mailboxes');
1723 // try to subscribe it
1724 if ($result && $subscribed)
1725 iil_C_Subscribe($this->conn, $abs_name);
1727 return $result ? $name : FALSE;
1732 * Remove mailboxes from server
1734 * @param string Mailbox name
1735 * @return boolean True on success
1737 function delete_mailbox($mbox_name)
1741 if (is_array($mbox_name))
1742 $a_mboxes = $mbox_name;
1743 else if (is_string($mbox_name) && strlen($mbox_name))
1744 $a_mboxes = explode(',', $mbox_name);
1746 $all_mboxes = iil_C_ListMailboxes($this->conn, $this->_mod_mailbox($root), '*');
1748 if (is_array($a_mboxes))
1749 foreach ($a_mboxes as $mbox_name)
1751 $mailbox = $this->_mod_mailbox($mbox_name);
1753 // unsubscribe mailbox before deleting
1754 iil_C_UnSubscribe($this->conn, $mailbox);
1756 // send delete command to server
1757 $result = iil_C_DeleteFolder($this->conn, $mailbox);
1761 foreach ($all_mboxes as $c_mbox)
1762 if (preg_match('/^'.preg_quote($mailbox.$this->delimiter).'/', $c_mbox))
1764 iil_C_UnSubscribe($this->conn, $c_mbox);
1765 $result = iil_C_DeleteFolder($this->conn, $c_mbox);
1771 // clear mailboxlist cache
1774 $this->clear_message_cache($mailbox.'.msg');
1775 $this->clear_cache('mailboxes');
1783 * Create all folders specified as default
1785 function create_default_folders()
1787 $a_folders = iil_C_ListMailboxes($this->conn, $this->_mod_mailbox(''), '*');
1788 $a_subscribed = iil_C_ListSubscribed($this->conn, $this->_mod_mailbox(''), '*');
1790 // create default folders if they do not exist
1791 foreach ($this->default_folders as $folder)
1793 $abs_name = $this->_mod_mailbox($folder);
1794 if (!in_array_nocase($abs_name, $a_folders))
1795 $this->create_mailbox($folder, TRUE);
1796 else if (!in_array_nocase($abs_name, $a_subscribed))
1797 $this->subscribe($folder);
1803 /* --------------------------------
1804 * internal caching methods
1805 * --------------------------------*/
1810 function set_caching($set)
1812 if ($set && is_object($this->db))
1813 $this->caching_enabled = TRUE;
1815 $this->caching_enabled = FALSE;
1821 function get_cache($key)
1824 if (!isset($this->cache[$key]) && $this->caching_enabled)
1826 $cache_data = $this->_read_cache_record('IMAP.'.$key);
1827 $this->cache[$key] = strlen($cache_data) ? unserialize($cache_data) : FALSE;
1830 return $this->cache[$key];
1836 function update_cache($key, $data)
1838 $this->cache[$key] = $data;
1839 $this->cache_changed = TRUE;
1840 $this->cache_changes[$key] = TRUE;
1846 function write_cache()
1848 if ($this->caching_enabled && $this->cache_changed)
1850 foreach ($this->cache as $key => $data)
1852 if ($this->cache_changes[$key])
1853 $this->_write_cache_record('IMAP.'.$key, serialize($data));
1861 function clear_cache($key=NULL)
1865 foreach ($this->cache as $key => $data)
1866 $this->_clear_cache_record('IMAP.'.$key);
1868 $this->cache = array();
1869 $this->cache_changed = FALSE;
1870 $this->cache_changes = array();
1874 $this->_clear_cache_record('IMAP.'.$key);
1875 $this->cache_changes[$key] = FALSE;
1876 unset($this->cache[$key]);
1883 function _read_cache_record($key)
1885 $cache_data = FALSE;
1889 // get cached data from DB
1890 $sql_result = $this->db->query(
1891 "SELECT cache_id, data
1892 FROM ".get_table_name('cache')."
1895 $_SESSION['user_id'],
1898 if ($sql_arr = $this->db->fetch_assoc($sql_result))
1900 $cache_data = $sql_arr['data'];
1901 $this->cache_keys[$key] = $sql_arr['cache_id'];
1911 function _write_cache_record($key, $data)
1916 // check if we already have a cache entry for this key
1917 if (!isset($this->cache_keys[$key]))
1919 $sql_result = $this->db->query(
1921 FROM ".get_table_name('cache')."
1924 $_SESSION['user_id'],
1927 if ($sql_arr = $this->db->fetch_assoc($sql_result))
1928 $this->cache_keys[$key] = $sql_arr['cache_id'];
1930 $this->cache_keys[$key] = FALSE;
1933 // update existing cache record
1934 if ($this->cache_keys[$key])
1937 "UPDATE ".get_table_name('cache')."
1938 SET created=".$this->db->now().",
1943 $_SESSION['user_id'],
1946 // add new cache record
1950 "INSERT INTO ".get_table_name('cache')."
1951 (created, user_id, cache_key, data)
1952 VALUES (".$this->db->now().", ?, ?, ?)",
1953 $_SESSION['user_id'],
1962 function _clear_cache_record($key)
1965 "DELETE FROM ".get_table_name('cache')."
1968 $_SESSION['user_id'],
1974 /* --------------------------------
1975 * message caching methods
1976 * --------------------------------*/
1980 * Checks if the cache is up-to-date
1982 * @param string Mailbox name
1983 * @param string Internal cache key
1984 * @return int -3 = off, -2 = incomplete, -1 = dirty
1986 function check_cache_status($mailbox, $cache_key)
1988 if (!$this->caching_enabled)
1991 $cache_index = $this->get_message_cache_index($cache_key, TRUE);
1992 $msg_count = $this->_messagecount($mailbox);
1993 $cache_count = count($cache_index);
1995 // console("Cache check: $msg_count !== ".count($cache_index));
1997 if ($cache_count==$msg_count)
1999 // get highest index
2000 $header = iil_C_FetchHeader($this->conn, $mailbox, "$msg_count");
2001 $cache_uid = array_pop($cache_index);
2003 // uids of highest message matches -> cache seems OK
2004 if ($cache_uid == $header->uid)
2010 // if cache count differs less than 10% report as dirty
2011 else if (abs($msg_count - $cache_count) < $msg_count/10)
2020 function get_message_cache($key, $from, $to, $sort_field, $sort_order)
2022 $cache_key = "$key:$from:$to:$sort_field:$sort_order";
2023 $db_header_fields = array('idx', 'uid', 'subject', 'from', 'to', 'cc', 'date', 'size');
2025 if (!in_array($sort_field, $db_header_fields))
2026 $sort_field = 'idx';
2028 if ($this->caching_enabled && !isset($this->cache[$cache_key]))
2030 $this->cache[$cache_key] = array();
2031 $sql_result = $this->db->limitquery(
2032 "SELECT idx, uid, headers
2033 FROM ".get_table_name('messages')."
2036 ORDER BY ".$this->db->quoteIdentifier($sort_field)." ".
2037 strtoupper($sort_order),
2040 $_SESSION['user_id'],
2043 while ($sql_arr = $this->db->fetch_assoc($sql_result))
2045 $uid = $sql_arr['uid'];
2046 $this->cache[$cache_key][$uid] = unserialize($sql_arr['headers']);
2048 // featch headers if unserialize failed
2049 if (empty($this->cache[$cache_key][$uid]))
2050 $this->cache[$cache_key][$uid] = iil_C_FetchHeader($this->conn, preg_replace('/.msg$/', '', $key), $uid, true);
2054 return $this->cache[$cache_key];
2060 function &get_cached_message($key, $uid, $struct=false)
2062 if (!$this->caching_enabled)
2065 $internal_key = '__single_msg';
2066 if ($this->caching_enabled && (!isset($this->cache[$internal_key][$uid]) ||
2067 ($struct && empty($this->cache[$internal_key][$uid]->structure))))
2069 $sql_select = "idx, uid, headers" . ($struct ? ", structure" : '');
2070 $sql_result = $this->db->query(
2072 FROM ".get_table_name('messages')."
2076 $_SESSION['user_id'],
2080 if ($sql_arr = $this->db->fetch_assoc($sql_result))
2082 $this->cache[$internal_key][$uid] = unserialize($sql_arr['headers']);
2083 if (is_object($this->cache[$internal_key][$uid]) && !empty($sql_arr['structure']))
2084 $this->cache[$internal_key][$uid]->structure = unserialize($sql_arr['structure']);
2088 return $this->cache[$internal_key][$uid];
2094 function get_message_cache_index($key, $force=FALSE, $sort_col='idx', $sort_order='ASC')
2096 static $sa_message_index = array();
2098 // empty key -> empty array
2099 if (!$this->caching_enabled || empty($key))
2102 if (!empty($sa_message_index[$key]) && !$force)
2103 return $sa_message_index[$key];
2105 $sa_message_index[$key] = array();
2106 $sql_result = $this->db->query(
2108 FROM ".get_table_name('messages')."
2111 ORDER BY ".$this->db->quote_identifier($sort_col)." ".$sort_order,
2112 $_SESSION['user_id'],
2115 while ($sql_arr = $this->db->fetch_assoc($sql_result))
2116 $sa_message_index[$key][$sql_arr['idx']] = $sql_arr['uid'];
2118 return $sa_message_index[$key];
2124 function add_message_cache($key, $index, $headers, $struct=null)
2126 if (!$this->caching_enabled || empty($key) || !is_object($headers) || empty($headers->uid))
2129 // check for an existing record (probly headers are cached but structure not)
2130 $sql_result = $this->db->query(
2132 FROM ".get_table_name('messages')."
2137 $_SESSION['user_id'],
2141 // update cache record
2142 if ($sql_arr = $this->db->fetch_assoc($sql_result))
2145 "UPDATE ".get_table_name('messages')."
2146 SET idx=?, headers=?, structure=?
2147 WHERE message_id=?",
2149 serialize($headers),
2150 is_object($struct) ? serialize($struct) : NULL,
2151 $sql_arr['message_id']
2154 else // insert new record
2157 "INSERT INTO ".get_table_name('messages')."
2158 (user_id, del, cache_key, created, idx, uid, subject, ".$this->db->quoteIdentifier('from').", ".$this->db->quoteIdentifier('to').", cc, date, size, headers, structure)
2159 VALUES (?, 0, ?, ".$this->db->now().", ?, ?, ?, ?, ?, ?, ".$this->db->fromunixtime($headers->timestamp).", ?, ?, ?)",
2160 $_SESSION['user_id'],
2164 (string)substr($this->decode_header($headers->subject, TRUE), 0, 128),
2165 (string)substr($this->decode_header($headers->from, TRUE), 0, 128),
2166 (string)substr($this->decode_header($headers->to, TRUE), 0, 128),
2167 (string)substr($this->decode_header($headers->cc, TRUE), 0, 128),
2168 (int)$headers->size,
2169 serialize($headers),
2170 is_object($struct) ? serialize($struct) : NULL
2178 function remove_message_cache($key, $index)
2181 "DELETE FROM ".get_table_name('messages')."
2185 $_SESSION['user_id'],
2193 function clear_message_cache($key, $start_index=1)
2196 "DELETE FROM ".get_table_name('messages')."
2200 $_SESSION['user_id'],
2208 /* --------------------------------
2209 * encoding/decoding methods
2210 * --------------------------------*/
2213 * Split an address list into a structured array list
2215 * @param string Input string
2216 * @param int List only this number of addresses
2217 * @param boolean Decode address strings
2218 * @return array Indexed list of addresses
2220 function decode_address_list($input, $max=null, $decode=true)
2222 $a = $this->_parse_address_list($input, $decode);
2231 foreach ($a as $val)
2234 $address = $val['address'];
2235 $name = preg_replace(array('/^[\'"]/', '/[\'"]$/'), '', trim($val['name']));
2236 if ($name && $address && $name != $address)
2237 $string = sprintf('%s <%s>', strpos($name, ',')!==FALSE ? '"'.$name.'"' : $name, $address);
2243 $out[$j] = array('name' => $name,
2244 'mailto' => $address,
2245 'string' => $string);
2247 if ($max && $j==$max)
2256 * Decode a message header value
2258 * @param string Header value
2259 * @param boolean Remove quotes if necessary
2260 * @return string Decoded string
2262 function decode_header($input, $remove_quotes=FALSE)
2264 $str = $this->decode_mime_string((string)$input);
2265 if ($str{0}=='"' && $remove_quotes)
2266 $str = str_replace('"', '', $str);
2273 * Decode a mime-encoded string to internal charset
2275 * @param string Header value
2276 * @param string Fallback charset if none specified
2277 * @return string Decoded string
2280 function decode_mime_string($input, $fallback=null)
2284 $pos = strpos($input, '=?');
2287 $out = substr($input, 0, $pos);
2289 $end_cs_pos = strpos($input, "?", $pos+2);
2290 $end_en_pos = strpos($input, "?", $end_cs_pos+1);
2291 $end_pos = strpos($input, "?=", $end_en_pos+1);
2293 $encstr = substr($input, $pos+2, ($end_pos-$pos-2));
2294 $rest = substr($input, $end_pos+2);
2296 $out .= rcube_imap::_decode_mime_string_part($encstr);
2297 $out .= rcube_imap::decode_mime_string($rest, $fallback);
2302 // no encoding information, use fallback
2303 return rcube_charset_convert($input, !empty($fallback) ? $fallback : 'ISO-8859-1');
2308 * Decode a part of a mime-encoded string
2312 function _decode_mime_string_part($str)
2314 $a = explode('?', $str);
2317 // should be in format "charset?encoding?base64_string"
2320 for ($i=2; $i<$count; $i++)
2323 if (($a[1]=="B")||($a[1]=="b"))
2324 $rest = base64_decode($rest);
2325 else if (($a[1]=="Q")||($a[1]=="q"))
2327 $rest = str_replace("_", " ", $rest);
2328 $rest = quoted_printable_decode($rest);
2331 return rcube_charset_convert($rest, $a[0]);
2334 return $str; // we dont' know what to do with this
2339 * Decode a mime part
2341 * @param string Input string
2342 * @param string Part encoding
2343 * @return string Decoded string
2346 function mime_decode($input, $encoding='7bit')
2348 switch (strtolower($encoding))
2354 case 'quoted-printable':
2355 return quoted_printable_decode($input);
2359 return base64_decode($input);
2369 * Convert body charset to UTF-8 according to the ctype_parameters
2371 * @param string Part body to decode
2372 * @param string Charset to convert from
2373 * @return string Content converted to internal charset
2375 function charset_decode($body, $ctype_param)
2377 if (is_array($ctype_param) && !empty($ctype_param['charset']))
2378 return rcube_charset_convert($body, $ctype_param['charset']);
2380 // defaults to what is specified in the class header
2381 return rcube_charset_convert($body, 'ISO-8859-1');
2386 * Translate UID to message ID
2388 * @param int Message UID
2389 * @param string Mailbox name
2390 * @return int Message ID
2392 function get_id($uid, $mbox_name=NULL)
2394 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
2395 return $this->_uid2id($uid, $mailbox);
2400 * Translate message number to UID
2402 * @param int Message ID
2403 * @param string Mailbox name
2404 * @return int Message UID
2406 function get_uid($id,$mbox_name=NULL)
2408 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
2409 return $this->_id2uid($id, $mailbox);
2414 /* --------------------------------
2416 * --------------------------------*/
2422 function _mod_mailbox($mbox_name, $mode='in')
2424 if ((!empty($this->root_ns) && $this->root_ns == $mbox_name) || $mbox_name == 'INBOX')
2427 if (!empty($this->root_dir) && $mode=='in')
2428 $mbox_name = $this->root_dir.$this->delimiter.$mbox_name;
2429 else if (strlen($this->root_dir) && $mode=='out')
2430 $mbox_name = substr($mbox_name, strlen($this->root_dir)+1);
2437 * Sort mailboxes first by default folders and then in alphabethical order
2440 function _sort_mailbox_list($a_folders)
2442 $a_out = $a_defaults = array();
2444 // find default folders and skip folders starting with '.'
2445 foreach($a_folders as $i => $folder)
2447 if ($folder{0}=='.')
2450 if (($p = array_search(strtolower($folder), $this->default_folders_lc))!==FALSE)
2451 $a_defaults[$p] = $folder;
2456 natcasesort($a_out);
2459 return array_merge($a_defaults, $a_out);
2465 function _uid2id($uid, $mbox_name=NULL)
2468 $mbox_name = $this->mailbox;
2470 if (!isset($this->uid_id_map[$mbox_name][$uid]))
2471 $this->uid_id_map[$mbox_name][$uid] = iil_C_UID2ID($this->conn, $mbox_name, $uid);
2473 return $this->uid_id_map[$mbox_name][$uid];
2479 function _id2uid($id, $mbox_name=NULL)
2482 $mbox_name = $this->mailbox;
2484 return iil_C_ID2UID($this->conn, $mbox_name, $id);
2489 * Parse string or array of server capabilities and put them in internal array
2492 function _parse_capability($caps)
2494 if (!is_array($caps))
2495 $cap_arr = explode(' ', $caps);
2499 foreach ($cap_arr as $cap)
2501 if ($cap=='CAPABILITY')
2504 if (strpos($cap, '=')>0)
2506 list($key, $value) = explode('=', $cap);
2507 if (!is_array($this->capabilities[$key]))
2508 $this->capabilities[$key] = array();
2510 $this->capabilities[$key][] = $value;
2513 $this->capabilities[$cap] = TRUE;
2519 * Subscribe/unsubscribe a list of mailboxes and update local cache
2522 function _change_subscription($a_mboxes, $mode)
2526 if (is_array($a_mboxes))
2527 foreach ($a_mboxes as $i => $mbox_name)
2529 $mailbox = $this->_mod_mailbox($mbox_name);
2530 $a_mboxes[$i] = $mailbox;
2532 if ($mode=='subscribe')
2533 $result = iil_C_Subscribe($this->conn, $mailbox);
2534 else if ($mode=='unsubscribe')
2535 $result = iil_C_UnSubscribe($this->conn, $mailbox);
2541 // get cached mailbox list
2544 $a_mailbox_cache = $this->get_cache('mailboxes');
2545 if (!is_array($a_mailbox_cache))
2548 // modify cached list
2549 if ($mode=='subscribe')
2550 $a_mailbox_cache = array_merge($a_mailbox_cache, $a_mboxes);
2551 else if ($mode=='unsubscribe')
2552 $a_mailbox_cache = array_diff($a_mailbox_cache, $a_mboxes);
2554 // write mailboxlist to cache
2555 $this->update_cache('mailboxes', $this->_sort_mailbox_list($a_mailbox_cache));
2563 * Increde/decrese messagecount for a specific mailbox
2566 function _set_messagecount($mbox_name, $mode, $increment)
2568 $a_mailbox_cache = FALSE;
2569 $mailbox = $mbox_name ? $mbox_name : $this->mailbox;
2570 $mode = strtoupper($mode);
2572 $a_mailbox_cache = $this->get_cache('messagecount');
2574 if (!is_array($a_mailbox_cache[$mailbox]) || !isset($a_mailbox_cache[$mailbox][$mode]) || !is_numeric($increment))
2577 // add incremental value to messagecount
2578 $a_mailbox_cache[$mailbox][$mode] += $increment;
2580 // there's something wrong, delete from cache
2581 if ($a_mailbox_cache[$mailbox][$mode] < 0)
2582 unset($a_mailbox_cache[$mailbox][$mode]);
2584 // write back to cache
2585 $this->update_cache('messagecount', $a_mailbox_cache);
2592 * Remove messagecount of a specific mailbox from cache
2595 function _clear_messagecount($mbox_name='')
2597 $a_mailbox_cache = FALSE;
2598 $mailbox = $mbox_name ? $mbox_name : $this->mailbox;
2600 $a_mailbox_cache = $this->get_cache('messagecount');
2602 if (is_array($a_mailbox_cache[$mailbox]))
2604 unset($a_mailbox_cache[$mailbox]);
2605 $this->update_cache('messagecount', $a_mailbox_cache);
2611 * Split RFC822 header string into an associative array
2614 function _parse_headers($headers)
2616 $a_headers = array();
2617 $lines = explode("\n", $headers);
2619 for ($i=0; $i<$c; $i++)
2621 if ($p = strpos($lines[$i], ': '))
2623 $field = strtolower(substr($lines[$i], 0, $p));
2624 $value = trim(substr($lines[$i], $p+1));
2626 $a_headers[$field] = $value;
2637 function _parse_address_list($str, $decode=true)
2639 // remove any newlines and carriage returns before
2640 $a = $this->_explode_quoted_string('[,;]', preg_replace( "/[\r\n]/", " ", $str));
2643 foreach ($a as $key => $val)
2645 $val = preg_replace("/([\"\w])</", "$1 <", $val);
2646 $sub_a = $this->_explode_quoted_string(' ', $decode ? $this->decode_header($val) : $val);
2647 $result[$key]['name'] = '';
2649 foreach ($sub_a as $k => $v)
2651 if (strpos($v, '@') > 0)
2652 $result[$key]['address'] = str_replace('<', '', str_replace('>', '', $v));
2654 $result[$key]['name'] .= (empty($result[$key]['name'])?'':' ').str_replace("\"",'',stripslashes($v));
2657 if (empty($result[$key]['name']))
2658 $result[$key]['name'] = $result[$key]['address'];
2668 function _explode_quoted_string($delimiter, $string)
2671 $strlen = strlen($string);
2672 for ($q=$p=$i=0; $i < $strlen; $i++)
2674 if ($string{$i} == "\"" && $string{$i-1} != "\\")
2675 $q = $q ? false : true;
2676 else if (!$q && preg_match("/$delimiter/", $string{$i}))
2678 $result[] = substr($string, $p, $i - $p);
2683 $result[] = substr($string, $p);
2687 } // end class rcube_imap
2691 * Class representing a message part
2695 class rcube_message_part
2698 var $ctype_primary = 'text';
2699 var $ctype_secondary = 'plain';
2700 var $mimetype = 'text/plain';
2701 var $disposition = '';
2703 var $encoding = '8bit';
2706 var $headers = array();
2707 var $d_parameters = array();
2708 var $ctype_parameters = array();
2714 * Class for sorting an array of iilBasicHeader objects in a predetermined order.
2717 * @author Eric Stadtherr
2719 class rcube_header_sorter
2721 var $sequence_numbers = array();
2724 * Set the predetermined sort order.
2726 * @param array Numerically indexed array of IMAP message sequence numbers
2728 function set_sequence_numbers($seqnums)
2730 $this->sequence_numbers = $seqnums;
2734 * Sort the array of header objects
2736 * @param array Array of iilBasicHeader objects indexed by UID
2738 function sort_headers(&$headers)
2741 * uksort would work if the keys were the sequence number, but unfortunately
2742 * the keys are the UIDs. We'll use uasort instead and dereference the value
2743 * to get the sequence number (in the "id" field).
2745 * uksort($headers, array($this, "compare_seqnums"));
2747 uasort($headers, array($this, "compare_seqnums"));
2751 * Get the position of a message sequence number in my sequence_numbers array
2753 * @param int Message sequence number contained in sequence_numbers
2754 * @return int Position, -1 if not found
2756 function position_of($seqnum)
2758 $c = count($this->sequence_numbers);
2759 for ($pos = 0; $pos <= $c; $pos++)
2761 if ($this->sequence_numbers[$pos] == $seqnum)
2768 * Sort method called by uasort()
2770 function compare_seqnums($a, $b)
2772 // First get the sequence number from the header object (the 'id' field).
2776 // then find each sequence number in my ordered list
2777 $posa = $this->position_of($seqa);
2778 $posb = $this->position_of($seqb);
2780 // return the relative position as the comparison value
2781 $ret = $posa - $posb;
2788 * Add quoted-printable encoding to a given string
2790 * @param string String to encode
2791 * @param int Add new line after this number of characters
2792 * @param boolean True if spaces should be converted into =20
2793 * @return string Encoded string
2795 function quoted_printable_encode($input, $line_max=76, $space_conv=false)
2797 $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
2798 $lines = preg_split("/(?:\r\n|\r|\n)/", $input);
2803 while( list(, $line) = each($lines))
2805 //$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary
2806 $linlen = strlen($line);
2808 for($i = 0; $i < $linlen; $i++)
2810 $c = substr( $line, $i, 1 );
2812 if ( ( $i == 0 ) && ( $dec == 46 ) ) // convert first point in the line into =2E
2818 if ( $i == ( $linlen - 1 ) ) // convert space at eol only
2822 else if ( $space_conv )
2827 else if ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) // always encode "\t", which is *not* required
2829 $h2 = floor($dec/16);
2830 $h1 = floor($dec%16);
2831 $c = $escape.$hex["$h2"].$hex["$h1"];
2834 if ( (strlen($newline) + strlen($c)) >= $line_max ) // CRLF is not counted
2836 $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
2838 // check if newline first character will be point or not
2846 $output .= $newline.$eol;
2849 return trim($output);