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 561 2007-05-17 15:18:12Z 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)
36 * @package RoundCube Webmail
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 = '';
73 * @param object Database connection
75 function __construct($db_conn)
82 * PHP 4 object constructor
84 * @see rcube_imap::__construct
86 function rcube_imap($db_conn)
88 $this->__construct($db_conn);
93 * Connect to an IMAP server
95 * @param string Host to connect
96 * @param string Username for IMAP account
97 * @param string Password for IMAP account
98 * @param number Port to connect to
99 * @param boolean Use SSL connection
100 * @return boolean TRUE on success, FALSE on failure
103 function connect($host, $user, $pass, $port=143, $use_ssl=FALSE)
105 global $ICL_SSL, $ICL_PORT, $IMAP_USE_INTERNAL_DATE;
107 // check for Open-SSL support in PHP build
108 if ($use_ssl && in_array('openssl', get_loaded_extensions()))
112 raise_error(array('code' => 403, 'type' => 'imap', 'file' => __FILE__,
113 'message' => 'Open SSL not available;'), TRUE, FALSE);
118 $IMAP_USE_INTERNAL_DATE = false;
120 $this->conn = iil_Connect($host, $user, $pass, array('imap' => 'check'));
125 $this->ssl = $use_ssl;
127 // print trace mesages
128 if ($this->conn && ($this->debug_level & 8))
129 console($this->conn->message);
132 else if (!$this->conn && $GLOBALS['iil_error'])
134 raise_error(array('code' => 403,
136 'message' => $GLOBALS['iil_error']), TRUE, FALSE);
139 // get server properties
142 $this->_parse_capability($this->conn->capability);
144 if (!empty($this->conn->delimiter))
145 $this->delimiter = $this->conn->delimiter;
146 if (!empty($this->conn->rootdir))
148 $this->set_rootdir($this->conn->rootdir);
149 $this->root_ns = ereg_replace('[\.\/]$', '', $this->conn->rootdir);
153 return $this->conn ? TRUE : FALSE;
158 * Close IMAP connection
159 * Usually done on script shutdown
166 iil_Close($this->conn);
171 * Close IMAP connection and re-connect
172 * This is used to avoid some strange socket errors when talking to Courier IMAP
179 $this->connect($this->host, $this->user, $this->pass, $this->port, $this->ssl);
184 * Set a root folder for the IMAP connection.
186 * Only folders within this root folder will be displayed
187 * and all folder paths will be translated using this folder name
189 * @param string Root folder
192 function set_rootdir($root)
194 if (ereg('[\.\/]$', $root)) //(substr($root, -1, 1)==='/')
195 $root = substr($root, 0, -1);
197 $this->root_dir = $root;
199 if (empty($this->delimiter))
200 $this->get_hierarchy_delimiter();
205 * This list of folders will be listed above all other folders
207 * @param array Indexed list of folder names
210 function set_default_mailboxes($arr)
214 $this->default_folders = $arr;
215 $this->default_folders_lc = array();
217 // add inbox if not included
218 if (!in_array_nocase('INBOX', $this->default_folders))
219 array_unshift($this->default_folders, 'INBOX');
221 // create a second list with lower cased names
222 foreach ($this->default_folders as $mbox)
223 $this->default_folders_lc[] = strtolower($mbox);
229 * Set internal mailbox reference.
231 * All operations will be perfomed on this mailbox/folder
233 * @param string Mailbox/Folder name
236 function set_mailbox($new_mbox)
238 $mailbox = $this->_mod_mailbox($new_mbox);
240 if ($this->mailbox == $mailbox)
243 $this->mailbox = $mailbox;
245 // clear messagecount cache for this mailbox
246 $this->_clear_messagecount($mailbox);
251 * Set internal list page
253 * @param number Page number to list
256 function set_page($page)
258 $this->list_page = (int)$page;
263 * Set internal page size
265 * @param number Number of messages to display on one page
268 function set_pagesize($size)
270 $this->page_size = (int)$size;
275 * Save a set of message ids for future message listing methods
277 * @param array List of IMAP fields to search in
278 * @param string Search string
279 * @param array List of message ids or NULL if empty
281 function set_search_set($subject, $str=null, $msgs=null, $charset=null)
283 if (is_array($subject) && $str == null && $msgs == null)
284 list($subject, $str, $msgs, $charset) = $subject;
285 if ($msgs != null && !is_array($msgs))
286 $msgs = split(',', $msgs);
288 $this->search_subject = $subject;
289 $this->search_string = $str;
290 $this->search_set = is_array($msgs) ? $msgs : NULL;
291 $this->search_charset = $charset;
296 * Return the saved search set as hash array
298 function get_search_set()
300 return array($this->search_subject, $this->search_string, $this->search_set, $this->search_charset);
305 * Returns the currently used mailbox name
307 * @return string Name of the mailbox/folder
310 function get_mailbox_name()
312 return $this->conn ? $this->_mod_mailbox($this->mailbox, 'out') : '';
317 * Returns the IMAP server's capability
319 * @param string Capability name
320 * @return mixed Capability value or TRUE if supported, FALSE if not
323 function get_capability($cap)
325 $cap = strtoupper($cap);
326 return $this->capabilities[$cap];
331 * Returns the delimiter that is used by the IMAP server for folder separation
333 * @return string Delimiter string
336 function get_hierarchy_delimiter()
338 if ($this->conn && empty($this->delimiter))
339 $this->delimiter = iil_C_GetHierarchyDelimiter($this->conn);
341 if (empty($this->delimiter))
342 $this->delimiter = '/';
344 return $this->delimiter;
349 * Public method for mailbox listing.
351 * Converts mailbox name with root dir first
353 * @param string Optional root folder
354 * @param string Optional filter for mailbox listing
355 * @return array List of mailboxes/folders
358 function list_mailboxes($root='', $filter='*')
361 $a_mboxes = $this->_list_mailboxes($root, $filter);
363 foreach ($a_mboxes as $mbox_row)
365 $name = $this->_mod_mailbox($mbox_row, 'out');
370 // INBOX should always be available
371 if (!in_array_nocase('INBOX', $a_out))
372 array_unshift($a_out, 'INBOX');
375 $a_out = $this->_sort_mailbox_list($a_out);
382 * Private method for mailbox listing
384 * @return array List of mailboxes/folders
386 * @see rcube_imap::list_mailboxes
388 function _list_mailboxes($root='', $filter='*')
390 $a_defaults = $a_out = array();
392 // get cached folder list
393 $a_mboxes = $this->get_cache('mailboxes');
394 if (is_array($a_mboxes))
397 // retrieve list of folders from IMAP server
398 $a_folders = iil_C_ListSubscribed($this->conn, $this->_mod_mailbox($root), $filter);
400 if (!is_array($a_folders) || !sizeof($a_folders))
401 $a_folders = array();
403 // write mailboxlist to cache
404 $this->update_cache('mailboxes', $a_folders);
411 * Get message count for a specific mailbox
413 * @param string Mailbox/folder name
414 * @param string Mode for count [ALL|UNSEEN|RECENT]
415 * @param boolean Force reading from server and update cache
416 * @return number Number of messages
419 function messagecount($mbox_name='', $mode='ALL', $force=FALSE)
421 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
422 return $this->_messagecount($mailbox, $mode, $force);
427 * Private method for getting nr of messages
430 * @see rcube_imap::messagecount
432 function _messagecount($mailbox='', $mode='ALL', $force=FALSE)
434 $a_mailbox_cache = FALSE;
435 $mode = strtoupper($mode);
438 $mailbox = $this->mailbox;
441 if ($this->search_set && $mailbox == $this->mailbox && $mode == 'ALL')
442 return count($this->search_set);
444 $a_mailbox_cache = $this->get_cache('messagecount');
446 // return cached value
447 if (!$force && is_array($a_mailbox_cache[$mailbox]) && isset($a_mailbox_cache[$mailbox][$mode]))
448 return $a_mailbox_cache[$mailbox][$mode];
450 // RECENT count is fetched abit different
451 if ($mode == 'RECENT')
452 $count = iil_C_CheckForRecent($this->conn, $mailbox);
454 // use SEARCH for message counting
455 else if ($this->skip_deleted)
457 $search_str = "ALL UNDELETED";
459 // get message count and store in cache
460 if ($mode == 'UNSEEN')
461 $search_str .= " UNSEEN";
463 // get message count using SEARCH
464 // not very performant but more precise (using UNDELETED)
466 $index = $this->_search_index($mailbox, $search_str);
467 if (is_array($index))
469 $str = implode(",", $index);
471 $count = count($index);
476 if ($mode == 'UNSEEN')
477 $count = iil_C_CountUnseen($this->conn, $mailbox);
479 $count = iil_C_CountMessages($this->conn, $mailbox);
482 if (!is_array($a_mailbox_cache[$mailbox]))
483 $a_mailbox_cache[$mailbox] = array();
485 $a_mailbox_cache[$mailbox][$mode] = (int)$count;
487 // write back to cache
488 $this->update_cache('messagecount', $a_mailbox_cache);
495 * Public method for listing headers
496 * convert mailbox name with root dir first
498 * @param string Mailbox/folder name
499 * @param number Current page to list
500 * @param string Header field to sort by
501 * @param string Sort order [ASC|DESC]
502 * @return array Indexed array with message header objects
505 function list_headers($mbox_name='', $page=NULL, $sort_field=NULL, $sort_order=NULL)
507 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
508 return $this->_list_headers($mailbox, $page, $sort_field, $sort_order);
513 * Private method for listing message headers
516 * @see rcube_imap::list_headers
518 function _list_headers($mailbox='', $page=NULL, $sort_field=NULL, $sort_order=NULL, $recursive=FALSE)
520 if (!strlen($mailbox))
523 // use saved message set
524 if ($this->search_set && $mailbox == $this->mailbox)
525 return $this->_list_header_set($mailbox, $this->search_set, $page, $sort_field, $sort_order);
527 if ($sort_field!=NULL)
528 $this->sort_field = $sort_field;
529 if ($sort_order!=NULL)
530 $this->sort_order = strtoupper($sort_order);
532 $max = $this->_messagecount($mailbox);
533 $start_msg = ($this->list_page-1) * $this->page_size;
535 list($begin, $end) = $this->_get_message_range($max, $page);
541 $headers_sorted = FALSE;
542 $cache_key = $mailbox.'.msg';
543 $cache_status = $this->check_cache_status($mailbox, $cache_key);
545 // cache is OK, we can get all messages from local cache
548 $a_msg_headers = $this->get_message_cache($cache_key, $start_msg, $start_msg+$this->page_size, $this->sort_field, $this->sort_order);
549 $headers_sorted = TRUE;
551 // cache is dirty, sync it
552 else if ($this->caching_enabled && $cache_status==-1 && !$recursive)
554 $this->sync_header_index($mailbox);
555 return $this->_list_headers($mailbox, $page, $this->sort_field, $this->sort_order, TRUE);
559 // retrieve headers from IMAP
560 if ($this->get_capability('sort') && ($msg_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, $this->skip_deleted ? 'UNDELETED' : '')))
562 $msgs = $msg_index[$begin];
563 for ($i=$begin+1; $i < $end; $i++)
564 $msgs = $msgs.','.$msg_index[$i];
568 $msgs = sprintf("%d:%d", $begin+1, $end);
571 for ($msg_seqnum = $begin; $msg_seqnum <= $end; $msg_seqnum++)
572 $msg_index[$i++] = $msg_seqnum;
575 // use this class for message sorting
576 $sorter = new rcube_header_sorter();
577 $sorter->set_sequence_numbers($msg_index);
579 // fetch reuested headers from server
580 $a_msg_headers = array();
581 $deleted_count = $this->_fetch_headers($mailbox, $msgs, $a_msg_headers, $cache_key);
583 // delete cached messages with a higher index than $max+1
584 // Changed $max to $max+1 to fix this bug : #1484295
585 $this->clear_message_cache($cache_key, $max + 1);
588 // kick child process to sync cache
594 // return empty array if no messages found
595 if (!is_array($a_msg_headers) || empty($a_msg_headers))
599 // if not already sorted
600 if (!$headers_sorted)
602 $sorter->sort_headers($a_msg_headers);
604 if ($this->sort_order == 'DESC')
605 $a_msg_headers = array_reverse($a_msg_headers);
608 return array_values($a_msg_headers);
614 * Public method for listing a specific set of headers
615 * convert mailbox name with root dir first
617 * @param string Mailbox/folder name
618 * @param array List of message ids to list
619 * @param number Current page to list
620 * @param string Header field to sort by
621 * @param string Sort order [ASC|DESC]
622 * @return array Indexed array with message header objects
625 function list_header_set($mbox_name='', $msgs, $page=NULL, $sort_field=NULL, $sort_order=NULL)
627 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
628 return $this->_list_header_set($mailbox, $msgs, $page, $sort_field, $sort_order);
633 * Private method for listing a set of message headers
636 * @see rcube_imap::list_header_set
638 function _list_header_set($mailbox, $msgs, $page=NULL, $sort_field=NULL, $sort_order=NULL)
640 // also accept a comma-separated list of message ids
641 if (is_string($msgs))
642 $msgs = split(',', $msgs);
644 if (!strlen($mailbox) || empty($msgs))
647 if ($sort_field!=NULL)
648 $this->sort_field = $sort_field;
649 if ($sort_order!=NULL)
650 $this->sort_order = strtoupper($sort_order);
653 $start_msg = ($this->list_page-1) * $this->page_size;
655 // fetch reuested headers from server
656 $a_msg_headers = array();
657 $this->_fetch_headers($mailbox, join(',', $msgs), $a_msg_headers, NULL);
659 // return empty array if no messages found
660 if (!is_array($a_msg_headers) || empty($a_msg_headers))
663 // if not already sorted
664 $a_msg_headers = iil_SortHeaders($a_msg_headers, $this->sort_field, $this->sort_order);
666 // only return the requested part of the set
667 return array_slice(array_values($a_msg_headers), $start_msg, min($max-$start_msg, $this->page_size));
672 * Helper function to get first and last index of the requested set
674 * @param number message count
675 * @param mixed page number to show, or string 'all'
676 * @return array array with two values: first index, last index
679 function _get_message_range($max, $page)
681 $start_msg = ($this->list_page-1) * $this->page_size;
688 else if ($this->sort_order=='DESC')
690 $begin = $max - $this->page_size - $start_msg;
691 $end = $max - $start_msg;
696 $end = $start_msg + $this->page_size;
699 if ($begin < 0) $begin = 0;
700 if ($end < 0) $end = $max;
701 if ($end > $max) $end = $max;
703 return array($begin, $end);
709 * Fetches message headers
712 * @param string Mailbox name
713 * @param string Message index to fetch
714 * @param array Reference to message headers array
715 * @param array Array with cache index
716 * @return number Number of deleted messages
719 function _fetch_headers($mailbox, $msgs, &$a_msg_headers, $cache_key)
721 // cache is incomplete
722 $cache_index = $this->get_message_cache_index($cache_key);
724 // fetch reuested headers from server
725 $a_header_index = iil_C_FetchHeaders($this->conn, $mailbox, $msgs);
728 if (!empty($a_header_index))
730 foreach ($a_header_index as $i => $headers)
732 if ($headers->deleted && $this->skip_deleted)
735 if ($cache_index[$headers->id] && $cache_index[$headers->id] == $headers->uid)
736 $this->remove_message_cache($cache_key, $headers->id);
742 // add message to cache
743 if ($this->caching_enabled && $cache_index[$headers->id] != $headers->uid)
744 $this->add_message_cache($cache_key, $headers->id, $headers);
746 $a_msg_headers[$headers->uid] = $headers;
750 return $deleted_count;
755 * Return sorted array of message UIDs
757 * @param string Mailbox to get index from
758 * @param string Sort column
759 * @param string Sort order [ASC, DESC]
760 * @return array Indexed array with message ids
762 function message_index($mbox_name='', $sort_field=NULL, $sort_order=NULL)
764 if ($sort_field!=NULL)
765 $this->sort_field = $sort_field;
766 if ($sort_order!=NULL)
767 $this->sort_order = strtoupper($sort_order);
769 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
770 $key = "$mbox:".$this->sort_field.":".$this->sort_order.".msgi";
772 // have stored it in RAM
773 if (isset($this->cache[$key]))
774 return $this->cache[$key];
777 $cache_key = $mailbox.'.msg';
778 $cache_status = $this->check_cache_status($mailbox, $cache_key);
783 $a_index = $this->get_message_cache_index($cache_key, TRUE, $this->sort_field, $this->sort_order);
784 return array_values($a_index);
788 // fetch complete message index
789 $msg_count = $this->_messagecount($mailbox);
790 if ($this->get_capability('sort') && ($a_index = iil_C_Sort($this->conn, $mailbox, $this->sort_field, '', TRUE)))
792 if ($this->sort_order == 'DESC')
793 $a_index = array_reverse($a_index);
795 $this->cache[$key] = $a_index;
800 $a_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, "1:$msg_count", $this->sort_field);
801 $a_uids = iil_C_FetchUIDs($this->conn, $mailbox);
803 if ($this->sort_order=="ASC")
805 else if ($this->sort_order=="DESC")
809 $this->cache[$key] = array();
810 foreach ($a_index as $index => $value)
811 $this->cache[$key][$i++] = $a_uids[$index];
814 return $this->cache[$key];
818 function sync_header_index($mailbox)
820 $cache_key = $mailbox.'.msg';
821 $cache_index = $this->get_message_cache_index($cache_key);
822 $msg_count = $this->_messagecount($mailbox);
824 // fetch complete message index
825 $a_message_index = iil_C_FetchHeaderIndex($this->conn, $mailbox, "1:$msg_count", 'UID');
827 foreach ($a_message_index as $id => $uid)
829 // message in cache at correct position
830 if ($cache_index[$id] == $uid)
832 unset($cache_index[$id]);
836 // message in cache but in wrong position
837 if (in_array((string)$uid, $cache_index, TRUE))
839 unset($cache_index[$id]);
842 // other message at this position
843 if (isset($cache_index[$id]))
845 $this->remove_message_cache($cache_key, $id);
846 unset($cache_index[$id]);
850 // fetch complete headers and add to cache
851 $headers = iil_C_FetchHeader($this->conn, $mailbox, $id);
852 $this->add_message_cache($cache_key, $headers->id, $headers);
855 // those ids that are still in cache_index have been deleted
856 if (!empty($cache_index))
858 foreach ($cache_index as $id => $uid)
859 $this->remove_message_cache($cache_key, $id);
865 * Invoke search request to IMAP server
867 * @param string mailbox name to search in
868 * @param string search criteria (ALL, TO, FROM, SUBJECT, etc)
869 * @param string search string
870 * @return array search results as list of message ids
873 function search($mbox_name='', $criteria='ALL', $str=NULL, $charset=NULL)
875 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
877 // have an array of criterias => execute multiple searches
878 if (is_array($criteria) && $str)
881 foreach ($criteria as $crit)
882 if ($search_result = $this->search($mbox_name, $crit, $str, $charset))
883 $results = array_merge($results, $search_result);
885 $results = array_unique($results);
886 $this->set_search_set($criteria, $str, $results, $charset);
889 else if ($str && $criteria)
891 $search = (!empty($charset) ? "CHARSET $charset " : '') . sprintf("%s {%d}\r\n%s", $criteria, strlen($str), $str);
892 $results = $this->_search_index($mailbox, $search);
894 // try search with ISO charset (should be supported by server)
895 if (empty($results) && !empty($charset) && $charset!='ISO-8859-1')
896 $results = $this->search($mbox_name, $criteria, rcube_charset_convert($str, $charset, 'ISO-8859-1'), 'ISO-8859-1');
898 $this->set_search_set($criteria, $str, $results, $charset);
902 return $this->_search_index($mailbox, $criteria);
907 * Private search method
909 * @return array search results as list of message ids
911 * @see rcube_imap::search()
913 function _search_index($mailbox, $criteria='ALL')
915 $a_messages = iil_C_Search($this->conn, $mailbox, $criteria);
916 // clean message list (there might be some empty entries)
917 if (is_array($a_messages))
919 foreach ($a_messages as $i => $val)
921 unset($a_messages[$i]);
929 * Refresh saved search set
931 function refresh_search()
933 if (!empty($this->search_subject) && !empty($this->search_string))
934 $this->search_set = $this->search('', $this->search_subject, $this->search_string, $this->search_charset);
936 return $this->get_search_set();
941 * Return message headers object of a specific message
943 * @param int Message ID
944 * @param string Mailbox to read from
945 * @param boolean True if $id is the message UID
946 * @return object Message headers representation
948 function get_headers($id, $mbox_name=NULL, $is_uid=TRUE)
950 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
951 $uid = $is_uid ? $id : $this->_id2uid($id);
953 // get cached headers
954 if ($uid && ($headers = &$this->get_cached_message($mailbox.'.msg', $uid)))
957 $headers = iil_C_FetchHeader($this->conn, $mailbox, $id, $is_uid);
959 // write headers cache
963 $this->uid_id_map[$mbox_name][$uid] = $headers->id;
965 $this->add_message_cache($mailbox.'.msg', $headers->id, $headers);
973 * Fetch body structure from the IMAP server and build
974 * an object structure similar to the one generated by PEAR::Mail_mimeDecode
976 * @param Int Message UID to fetch
977 * @return object Standard object tree or False on failure
979 function &get_structure($uid)
981 $cache_key = $this->mailbox.'.msg';
982 $headers = &$this->get_cached_message($cache_key, $uid, true);
984 // return cached message structure
985 if (is_object($headers) && is_object($headers->structure))
986 return $headers->structure;
988 // resolve message sequence number
989 if (!($msg_id = $this->_uid2id($uid)))
992 $structure_str = iil_C_FetchStructureString($this->conn, $this->mailbox, $msg_id);
993 $structure = iml_GetRawStructureArray($structure_str);
996 // parse structure and add headers
997 if (!empty($structure))
999 $this->_msg_id = $msg_id;
1000 $headers = $this->get_headers($msg_id, NULL, FALSE);
1002 $struct = &$this->_structure_part($structure);
1003 $struct->headers = get_object_vars($headers);
1005 // don't trust given content-type
1006 if (empty($struct->parts) && !empty($struct->headers['ctype']))
1008 $struct->mime_id = '1';
1009 $struct->mimetype = strtolower($struct->headers['ctype']);
1010 list($struct->ctype_primary, $struct->ctype_secondary) = explode('/', $struct->mimetype);
1013 // write structure to cache
1014 if ($this->caching_enabled)
1015 $this->add_message_cache($cache_key, $msg_id, $headers, $struct);
1023 * Build message part object
1027 function &_structure_part($part, $count=0, $parent='')
1029 $struct = new rcube_message_part;
1030 $struct->mime_id = empty($parent) ? (string)$count : "$parent.$count";
1033 if (is_array($part[0]))
1035 $struct->ctype_primary = 'multipart';
1037 // find first non-array entry
1038 for ($i=1; count($part); $i++)
1039 if (!is_array($part[$i]))
1041 $struct->ctype_secondary = strtolower($part[$i]);
1045 $struct->mimetype = 'multipart/'.$struct->ctype_secondary;
1047 $struct->parts = array();
1048 for ($i=0, $count=0; $i<count($part); $i++)
1049 if (is_array($part[$i]) && count($part[$i]) > 5)
1050 $struct->parts[] = $this->_structure_part($part[$i], ++$count, $struct->mime_id);
1057 $struct->ctype_primary = strtolower($part[0]);
1058 $struct->ctype_secondary = strtolower($part[1]);
1059 $struct->mimetype = $struct->ctype_primary.'/'.$struct->ctype_secondary;
1061 // read content type parameters
1062 if (is_array($part[2]))
1064 $struct->ctype_parameters = array();
1065 for ($i=0; $i<count($part[2]); $i+=2)
1066 $struct->ctype_parameters[strtolower($part[2][$i])] = $part[2][$i+1];
1068 if (isset($struct->ctype_parameters['charset']))
1069 $struct->charset = $struct->ctype_parameters['charset'];
1072 // read content encoding
1073 if (!empty($part[5]) && $part[5]!='NIL')
1075 $struct->encoding = strtolower($part[5]);
1076 $struct->headers['content-transfer-encoding'] = $struct->encoding;
1080 if (!empty($part[6]) && $part[6]!='NIL')
1081 $struct->size = intval($part[6]);
1083 // read part disposition
1084 $di = count($part) - 2;
1085 if ((is_array($part[$di]) && count($part[$di]) == 2 && is_array($part[$di][1])) ||
1086 (is_array($part[--$di]) && count($part[$di]) == 2))
1088 $struct->disposition = strtolower($part[$di][0]);
1090 if (is_array($part[$di][1]))
1091 for ($n=0; $n<count($part[$di][1]); $n+=2)
1092 $struct->d_parameters[strtolower($part[$di][1][$n])] = $part[$di][1][$n+1];
1096 if (is_array($part[8]) && $di != 8)
1098 $struct->parts = array();
1099 for ($i=0, $count=0; $i<count($part[8]); $i++)
1100 if (is_array($part[8][$i]) && count($part[8][$i]) > 5)
1101 $struct->parts[] = $this->_structure_part($part[8][$i], ++$count, $struct->mime_id);
1105 if (!empty($part[3]) && $part[3]!='NIL')
1107 $struct->content_id = $part[3];
1108 $struct->headers['content-id'] = $part[3];
1110 if (empty($struct->disposition))
1111 $struct->disposition = 'inline';
1114 // fetch message headers if message/rfc822
1115 if ($struct->ctype_primary=='message')
1117 $headers = iil_C_FetchPartBody($this->conn, $this->mailbox, $this->_msg_id, $struct->mime_id.'.HEADER');
1118 $struct->headers = $this->_parse_headers($headers);
1120 if (is_array($part[8]) && empty($struct->parts))
1121 $struct->parts[] = $this->_structure_part($part[8], ++$count, $struct->mime_id);
1124 // normalize filename property
1125 if (!empty($struct->d_parameters['filename']))
1126 $struct->filename = $this->decode_mime_string($struct->d_parameters['filename']);
1127 else if (!empty($struct->ctype_parameters['name']))
1128 $struct->filename = $this->decode_mime_string($struct->ctype_parameters['name']);
1129 else if (!empty($struct->headers['content-description']))
1130 $struct->filename = $this->decode_mime_string($struct->headers['content-description']);
1137 * Return a flat array with references to all parts, indexed by part numbers
1139 * @param object Message body structure
1140 * @return Array with part number -> object pairs
1142 function get_mime_numbers(&$structure)
1145 $this->_get_part_numbers($structure, $a_parts);
1151 * Helper method for recursive calls
1155 function _get_part_numbers(&$part, &$a_parts)
1158 $a_parts[$part->mime_id] = &$part;
1160 if (is_array($part->parts))
1161 for ($i=0; $i<count($part->parts); $i++)
1162 $this->_get_part_numbers($part->parts[$i], $a_parts);
1167 * Fetch message body of a specific message from the server
1169 * @param int Message UID
1170 * @param string Part number
1171 * @param object Part object created by get_structure()
1172 * @param mixed True to print part, ressource to write part contents in
1173 * @return Message/part body if not printed
1175 function &get_message_part($uid, $part=1, $o_part=NULL, $print=NULL)
1177 if (!($msg_id = $this->_uid2id($uid)))
1180 // get part encoding if not provided
1181 if (!is_object($o_part))
1183 $structure_str = iil_C_FetchStructureString($this->conn, $this->mailbox, $msg_id);
1184 $structure = iml_GetRawStructureArray($structure_str);
1185 $part_type = iml_GetPartTypeCode($structure, $part);
1186 $o_part = new rcube_message_part;
1187 $o_part->ctype_primary = $part_type==0 ? 'text' : ($part_type==2 ? 'message' : 'other');
1188 $o_part->encoding = strtolower(iml_GetPartEncodingString($structure, $part));
1189 $o_part->charset = iml_GetPartCharset($structure, $part);
1192 // TODO: Add caching for message parts
1196 iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, $part, ($o_part->encoding=='base64'?3:2));
1201 $body = iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, $part, 1);
1204 if ($o_part->encoding=='base64' || $o_part->encoding=='quoted-printable')
1205 $body = $this->mime_decode($body, $o_part->encoding);
1207 // convert charset (if text or message part)
1208 if ($o_part->ctype_primary=='text' || $o_part->ctype_primary=='message')
1210 // assume ISO-8859-1 if no charset specified
1211 if (empty($o_part->charset))
1212 $o_part->charset = 'ISO-8859-1';
1214 $body = rcube_charset_convert($body, $o_part->charset);
1223 * Fetch message body of a specific message from the server
1225 * @param int Message UID
1226 * @return Message/part body
1227 * @see ::get_message_part()
1229 function &get_body($uid, $part=1)
1231 return $this->get_message_part($uid, $part);
1236 * Returns the whole message source as string
1238 * @param int Message UID
1239 * @return Message source string
1241 function &get_raw_body($uid)
1243 if (!($msg_id = $this->_uid2id($uid)))
1246 $body = iil_C_FetchPartHeader($this->conn, $this->mailbox, $msg_id, NULL);
1247 $body .= iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, NULL, 1);
1254 * Sends the whole message source to stdout
1256 * @param int Message UID
1258 function print_raw_body($uid)
1260 if (!($msg_id = $this->_uid2id($uid)))
1263 print iil_C_FetchPartHeader($this->conn, $this->mailbox, $msg_id, NULL);
1265 iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, NULL, 2);
1270 * Set message flag to one or several messages
1272 * @param mixed Message UIDs as array or as comma-separated string
1273 * @param string Flag to set: SEEN, UNDELETED, DELETED, RECENT, ANSWERED, DRAFT
1274 * @return True on success, False on failure
1276 function set_flag($uids, $flag)
1278 $flag = strtoupper($flag);
1280 if (!is_array($uids))
1281 $uids = explode(',',$uids);
1283 foreach ($uids as $uid) {
1284 $msg_ids[$uid] = $this->_uid2id($uid);
1287 if ($flag=='UNDELETED')
1288 $result = iil_C_Undelete($this->conn, $this->mailbox, join(',', array_values($msg_ids)));
1289 else if ($flag=='UNSEEN')
1290 $result = iil_C_Unseen($this->conn, $this->mailbox, join(',', array_values($msg_ids)));
1292 $result = iil_C_Flag($this->conn, $this->mailbox, join(',', array_values($msg_ids)), $flag);
1294 // reload message headers if cached
1295 $cache_key = $this->mailbox.'.msg';
1296 if ($this->caching_enabled)
1298 foreach ($msg_ids as $uid => $id)
1300 if ($cached_headers = $this->get_cached_message($cache_key, $uid))
1302 $this->remove_message_cache($cache_key, $id);
1303 //$this->get_headers($uid);
1307 // close and re-open connection
1308 // this prevents connection problems with Courier
1312 // set nr of messages that were flaged
1313 $count = count($msg_ids);
1315 // clear message count cache
1316 if ($result && $flag=='SEEN')
1317 $this->_set_messagecount($this->mailbox, 'UNSEEN', $count*(-1));
1318 else if ($result && $flag=='UNSEEN')
1319 $this->_set_messagecount($this->mailbox, 'UNSEEN', $count);
1320 else if ($result && $flag=='DELETED')
1321 $this->_set_messagecount($this->mailbox, 'ALL', $count*(-1));
1327 // append a mail message (source) to a specific mailbox
1328 function save_message($mbox_name, &$message)
1330 $mbox_name = stripslashes($mbox_name);
1331 $mailbox = $this->_mod_mailbox($mbox_name);
1333 // make sure mailbox exists
1334 if (in_array($mailbox, $this->_list_mailboxes()))
1335 $saved = iil_C_Append($this->conn, $mailbox, $message);
1339 // increase messagecount of the target mailbox
1340 $this->_set_messagecount($mailbox, 'ALL', 1);
1347 // move a message from one mailbox to another
1348 function move_message($uids, $to_mbox, $from_mbox='')
1350 $to_mbox = stripslashes($to_mbox);
1351 $from_mbox = stripslashes($from_mbox);
1352 $to_mbox = $this->_mod_mailbox($to_mbox);
1353 $from_mbox = $from_mbox ? $this->_mod_mailbox($from_mbox) : $this->mailbox;
1355 // make sure mailbox exists
1356 if (!in_array($to_mbox, $this->_list_mailboxes()))
1358 if (in_array(strtolower($to_mbox), $this->default_folders))
1359 $this->create_mailbox($to_mbox, TRUE);
1364 // convert the list of uids to array
1365 $a_uids = is_string($uids) ? explode(',', $uids) : (is_array($uids) ? $uids : NULL);
1367 // exit if no message uids are specified
1368 if (!is_array($a_uids))
1371 // convert uids to message ids
1373 foreach ($a_uids as $uid)
1374 $a_mids[] = $this->_uid2id($uid, $from_mbox);
1376 $iil_move = iil_C_Move($this->conn, join(',', $a_mids), $from_mbox, $to_mbox);
1377 $moved = !($iil_move === false || $iil_move < 0);
1379 // send expunge command in order to have the moved message
1380 // really deleted from the source mailbox
1383 $this->_expunge($from_mbox, FALSE);
1384 $this->_clear_messagecount($from_mbox);
1385 $this->_clear_messagecount($to_mbox);
1388 // remove message ids from search set
1389 if ($moved && $this->search_set && $from_mbox == $this->mailbox)
1390 $this->search_set = array_diff($this->search_set, $a_mids);
1392 // update cached message headers
1393 $cache_key = $from_mbox.'.msg';
1394 if ($moved && ($a_cache_index = $this->get_message_cache_index($cache_key)))
1396 $start_index = 100000;
1397 foreach ($a_uids as $uid)
1399 if (($index = array_search($uid, $a_cache_index)) !== FALSE)
1400 $start_index = min($index, $start_index);
1403 // clear cache from the lowest index on
1404 $this->clear_message_cache($cache_key, $start_index);
1411 // mark messages as deleted and expunge mailbox
1412 function delete_message($uids, $mbox_name='')
1414 $mbox_name = stripslashes($mbox_name);
1415 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
1417 // convert the list of uids to array
1418 $a_uids = is_string($uids) ? explode(',', $uids) : (is_array($uids) ? $uids : NULL);
1420 // exit if no message uids are specified
1421 if (!is_array($a_uids))
1425 // convert uids to message ids
1427 foreach ($a_uids as $uid)
1428 $a_mids[] = $this->_uid2id($uid, $mailbox);
1430 $deleted = iil_C_Delete($this->conn, $mailbox, join(',', $a_mids));
1432 // send expunge command in order to have the deleted message
1433 // really deleted from the mailbox
1436 $this->_expunge($mailbox, FALSE);
1437 $this->_clear_messagecount($mailbox);
1440 // remove message ids from search set
1441 if ($moved && $this->search_set && $mailbox == $this->mailbox)
1442 $this->search_set = array_diff($this->search_set, $a_mids);
1444 // remove deleted messages from cache
1445 $cache_key = $mailbox.'.msg';
1446 if ($deleted && ($a_cache_index = $this->get_message_cache_index($cache_key)))
1448 $start_index = 100000;
1449 foreach ($a_uids as $uid)
1451 if (($index = array_search($uid, $a_cache_index)) !== FALSE)
1452 $start_index = min($index, $start_index);
1455 // clear cache from the lowest index on
1456 $this->clear_message_cache($cache_key, $start_index);
1463 // clear all messages in a specific mailbox
1464 function clear_mailbox($mbox_name=NULL)
1466 $mbox_name = stripslashes($mbox_name);
1467 $mailbox = !empty($mbox_name) ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
1468 $msg_count = $this->_messagecount($mailbox, 'ALL');
1472 $cleared = iil_C_ClearFolder($this->conn, $mailbox);
1474 // make sure the message count cache is cleared as well
1477 $this->clear_message_cache($mailbox.'.msg');
1478 $a_mailbox_cache = $this->get_cache('messagecount');
1479 unset($a_mailbox_cache[$mailbox]);
1480 $this->update_cache('messagecount', $a_mailbox_cache);
1490 // send IMAP expunge command and clear cache
1491 function expunge($mbox_name='', $clear_cache=TRUE)
1493 $mbox_name = stripslashes($mbox_name);
1494 $mailbox = $mbox_name ? $this->_mod_mailbox($mbox_name) : $this->mailbox;
1495 return $this->_expunge($mailbox, $clear_cache);
1499 // send IMAP expunge command and clear cache
1500 function _expunge($mailbox, $clear_cache=TRUE)
1502 $result = iil_C_Expunge($this->conn, $mailbox);
1504 if ($result>=0 && $clear_cache)
1506 //$this->clear_message_cache($mailbox.'.msg');
1507 $this->_clear_messagecount($mailbox);
1514 /* --------------------------------
1516 * --------------------------------*/
1520 * Get a list of all folders available on the IMAP server
1522 * @param string IMAP root dir
1523 * @return array Inbdexed array with folder names
1525 function list_unsubscribed($root='')
1527 static $sa_unsubscribed;
1529 if (is_array($sa_unsubscribed))
1530 return $sa_unsubscribed;
1532 // retrieve list of folders from IMAP server
1533 $a_mboxes = iil_C_ListMailboxes($this->conn, $this->_mod_mailbox($root), '*');
1535 // modify names with root dir
1536 foreach ($a_mboxes as $mbox_name)
1538 $name = $this->_mod_mailbox($mbox_name, 'out');
1540 $a_folders[] = $name;
1543 // filter folders and sort them
1544 $sa_unsubscribed = $this->_sort_mailbox_list($a_folders);
1545 return $sa_unsubscribed;
1553 function get_quota()
1555 if ($this->get_capability('QUOTA'))
1556 return iil_C_GetQuota($this->conn);
1563 * subscribe to a specific mailbox(es)
1565 function subscribe($mbox_name, $mode='subscribe')
1567 if (is_array($mbox_name))
1568 $a_mboxes = $mbox_name;
1569 else if (is_string($mbox_name) && strlen($mbox_name))
1570 $a_mboxes = explode(',', $mbox_name);
1572 // let this common function do the main work
1573 return $this->_change_subscription($a_mboxes, 'subscribe');
1578 * unsubscribe mailboxes
1580 function unsubscribe($mbox_name)
1582 if (is_array($mbox_name))
1583 $a_mboxes = $mbox_name;
1584 else if (is_string($mbox_name) && strlen($mbox_name))
1585 $a_mboxes = explode(',', $mbox_name);
1587 // let this common function do the main work
1588 return $this->_change_subscription($a_mboxes, 'unsubscribe');
1593 * Create a new mailbox on the server and register it in local cache
1595 * @param string New mailbox name (as utf-7 string)
1596 * @param boolean True if the new mailbox should be subscribed
1597 * @param string Name of the created mailbox, false on error
1599 function create_mailbox($name, $subscribe=FALSE)
1603 // replace backslashes
1604 $name = preg_replace('/[\\\]+/', '-', $name);
1606 // reduce mailbox name to 100 chars
1607 $name = substr($name, 0, 100);
1609 $abs_name = $this->_mod_mailbox($name);
1610 $a_mailbox_cache = $this->get_cache('mailboxes');
1612 if (strlen($abs_name) && (!is_array($a_mailbox_cache) || !in_array_nocase($abs_name, $a_mailbox_cache)))
1613 $result = iil_C_CreateFolder($this->conn, $abs_name);
1615 // try to subscribe it
1617 $this->subscribe($name);
1619 return $result ? $name : FALSE;
1624 * Set a new name to an existing mailbox
1626 * @param string Mailbox to rename (as utf-7 string)
1627 * @param string New mailbox name (as utf-7 string)
1628 * @param string Name of the renames mailbox, false on error
1630 function rename_mailbox($mbox_name, $new_name)
1634 // replace backslashes
1635 $name = preg_replace('/[\\\]+/', '-', $new_name);
1637 // encode mailbox name and reduce it to 100 chars
1638 $name = substr($new_name, 0, 100);
1640 // make absolute path
1641 $mailbox = $this->_mod_mailbox($mbox_name);
1642 $abs_name = $this->_mod_mailbox($name);
1644 // check if mailbox is subscribed
1645 $a_subscribed = $this->_list_mailboxes();
1646 $subscribed = in_array($mailbox, $a_subscribed);
1648 // unsubscribe folder
1650 iil_C_UnSubscribe($this->conn, $mailbox);
1652 if (strlen($abs_name))
1653 $result = iil_C_RenameFolder($this->conn, $mailbox, $abs_name);
1658 $this->clear_message_cache($mailbox.'.msg');
1659 $this->clear_cache('mailboxes');
1662 // try to subscribe it
1663 if ($result && $subscribed)
1664 iil_C_Subscribe($this->conn, $abs_name);
1666 return $result ? $name : FALSE;
1671 * remove mailboxes from server
1673 function delete_mailbox($mbox_name)
1677 if (is_array($mbox_name))
1678 $a_mboxes = $mbox_name;
1679 else if (is_string($mbox_name) && strlen($mbox_name))
1680 $a_mboxes = explode(',', $mbox_name);
1682 if (is_array($a_mboxes))
1683 foreach ($a_mboxes as $mbox_name)
1685 $mailbox = $this->_mod_mailbox($mbox_name);
1687 // unsubscribe mailbox before deleting
1688 iil_C_UnSubscribe($this->conn, $mailbox);
1690 // send delete command to server
1691 $result = iil_C_DeleteFolder($this->conn, $mailbox);
1696 // clear mailboxlist cache
1699 $this->clear_message_cache($mailbox.'.msg');
1700 $this->clear_cache('mailboxes');
1708 * Create all folders specified as default
1710 function create_default_folders()
1712 $a_folders = iil_C_ListMailboxes($this->conn, $this->_mod_mailbox(''), '*');
1713 $a_subscribed = iil_C_ListSubscribed($this->conn, $this->_mod_mailbox(''), '*');
1715 // create default folders if they do not exist
1716 foreach ($this->default_folders as $folder)
1718 $abs_name = $this->_mod_mailbox($folder);
1719 if (!in_array_nocase($abs_name, $a_subscribed))
1721 if (!in_array_nocase($abs_name, $a_folders))
1722 $this->create_mailbox($folder, TRUE);
1724 $this->subscribe($folder);
1726 else if (!in_array_nocase($abs_name, $a_folders))
1728 $this->create_mailbox($folder, FALSE);
1735 /* --------------------------------
1736 * internal caching methods
1737 * --------------------------------*/
1740 function set_caching($set)
1742 if ($set && is_object($this->db))
1743 $this->caching_enabled = TRUE;
1745 $this->caching_enabled = FALSE;
1749 function get_cache($key)
1752 if (!isset($this->cache[$key]) && $this->caching_enabled)
1754 $cache_data = $this->_read_cache_record('IMAP.'.$key);
1755 $this->cache[$key] = strlen($cache_data) ? unserialize($cache_data) : FALSE;
1758 return $this->cache[$key];
1762 function update_cache($key, $data)
1764 $this->cache[$key] = $data;
1765 $this->cache_changed = TRUE;
1766 $this->cache_changes[$key] = TRUE;
1770 function write_cache()
1772 if ($this->caching_enabled && $this->cache_changed)
1774 foreach ($this->cache as $key => $data)
1776 if ($this->cache_changes[$key])
1777 $this->_write_cache_record('IMAP.'.$key, serialize($data));
1783 function clear_cache($key=NULL)
1787 foreach ($this->cache as $key => $data)
1788 $this->_clear_cache_record('IMAP.'.$key);
1790 $this->cache = array();
1791 $this->cache_changed = FALSE;
1792 $this->cache_changes = array();
1796 $this->_clear_cache_record('IMAP.'.$key);
1797 $this->cache_changes[$key] = FALSE;
1798 unset($this->cache[$key]);
1804 function _read_cache_record($key)
1806 $cache_data = FALSE;
1810 // get cached data from DB
1811 $sql_result = $this->db->query(
1812 "SELECT cache_id, data
1813 FROM ".get_table_name('cache')."
1816 $_SESSION['user_id'],
1819 if ($sql_arr = $this->db->fetch_assoc($sql_result))
1821 $cache_data = $sql_arr['data'];
1822 $this->cache_keys[$key] = $sql_arr['cache_id'];
1830 function _write_cache_record($key, $data)
1835 // check if we already have a cache entry for this key
1836 if (!isset($this->cache_keys[$key]))
1838 $sql_result = $this->db->query(
1840 FROM ".get_table_name('cache')."
1843 $_SESSION['user_id'],
1846 if ($sql_arr = $this->db->fetch_assoc($sql_result))
1847 $this->cache_keys[$key] = $sql_arr['cache_id'];
1849 $this->cache_keys[$key] = FALSE;
1852 // update existing cache record
1853 if ($this->cache_keys[$key])
1856 "UPDATE ".get_table_name('cache')."
1857 SET created=".$this->db->now().",
1862 $_SESSION['user_id'],
1865 // add new cache record
1869 "INSERT INTO ".get_table_name('cache')."
1870 (created, user_id, cache_key, data)
1871 VALUES (".$this->db->now().", ?, ?, ?)",
1872 $_SESSION['user_id'],
1879 function _clear_cache_record($key)
1882 "DELETE FROM ".get_table_name('cache')."
1885 $_SESSION['user_id'],
1891 /* --------------------------------
1892 * message caching methods
1893 * --------------------------------*/
1896 // checks if the cache is up-to-date
1897 // return: -3 = off, -2 = incomplete, -1 = dirty
1898 function check_cache_status($mailbox, $cache_key)
1900 if (!$this->caching_enabled)
1903 $cache_index = $this->get_message_cache_index($cache_key, TRUE);
1904 $msg_count = $this->_messagecount($mailbox);
1905 $cache_count = count($cache_index);
1907 // console("Cache check: $msg_count !== ".count($cache_index));
1909 if ($cache_count==$msg_count)
1911 // get highest index
1912 $header = iil_C_FetchHeader($this->conn, $mailbox, "$msg_count");
1913 $cache_uid = array_pop($cache_index);
1915 // uids of highest message matches -> cache seems OK
1916 if ($cache_uid == $header->uid)
1922 // if cache count differs less than 10% report as dirty
1923 else if (abs($msg_count - $cache_count) < $msg_count/10)
1931 function get_message_cache($key, $from, $to, $sort_field, $sort_order)
1933 $cache_key = "$key:$from:$to:$sort_field:$sort_order";
1934 $db_header_fields = array('idx', 'uid', 'subject', 'from', 'to', 'cc', 'date', 'size');
1936 if (!in_array($sort_field, $db_header_fields))
1937 $sort_field = 'idx';
1939 if ($this->caching_enabled && !isset($this->cache[$cache_key]))
1941 $this->cache[$cache_key] = array();
1942 $sql_result = $this->db->limitquery(
1943 "SELECT idx, uid, headers
1944 FROM ".get_table_name('messages')."
1947 ORDER BY ".$this->db->quoteIdentifier($sort_field)." ".
1948 strtoupper($sort_order),
1951 $_SESSION['user_id'],
1954 while ($sql_arr = $this->db->fetch_assoc($sql_result))
1956 $uid = $sql_arr['uid'];
1957 $this->cache[$cache_key][$uid] = unserialize($sql_arr['headers']);
1961 return $this->cache[$cache_key];
1965 function &get_cached_message($key, $uid, $struct=false)
1967 if (!$this->caching_enabled)
1970 $internal_key = '__single_msg';
1971 if ($this->caching_enabled && (!isset($this->cache[$internal_key][$uid]) ||
1972 ($struct && empty($this->cache[$internal_key][$uid]->structure))))
1974 $sql_select = "idx, uid, headers" . ($struct ? ", structure" : '');
1975 $sql_result = $this->db->query(
1977 FROM ".get_table_name('messages')."
1981 $_SESSION['user_id'],
1985 if ($sql_arr = $this->db->fetch_assoc($sql_result))
1987 $this->cache[$internal_key][$uid] = unserialize($sql_arr['headers']);
1988 if (is_object($this->cache[$internal_key][$uid]) && !empty($sql_arr['structure']))
1989 $this->cache[$internal_key][$uid]->structure = unserialize($sql_arr['structure']);
1993 return $this->cache[$internal_key][$uid];
1997 function get_message_cache_index($key, $force=FALSE, $sort_col='idx', $sort_order='ASC')
1999 static $sa_message_index = array();
2001 // empty key -> empty array
2002 if (!$this->caching_enabled || empty($key))
2005 if (!empty($sa_message_index[$key]) && !$force)
2006 return $sa_message_index[$key];
2008 $sa_message_index[$key] = array();
2009 $sql_result = $this->db->query(
2011 FROM ".get_table_name('messages')."
2014 ORDER BY ".$this->db->quote_identifier($sort_col)." ".$sort_order,
2015 $_SESSION['user_id'],
2018 while ($sql_arr = $this->db->fetch_assoc($sql_result))
2019 $sa_message_index[$key][$sql_arr['idx']] = $sql_arr['uid'];
2021 return $sa_message_index[$key];
2025 function add_message_cache($key, $index, $headers, $struct=null)
2027 if (!$this->caching_enabled || empty($key) || !is_object($headers) || empty($headers->uid))
2030 // check for an existing record (probly headers are cached but structure not)
2031 $sql_result = $this->db->query(
2033 FROM ".get_table_name('messages')."
2038 $_SESSION['user_id'],
2042 // update cache record
2043 if ($sql_arr = $this->db->fetch_assoc($sql_result))
2046 "UPDATE ".get_table_name('messages')."
2047 SET idx=?, headers=?, structure=?
2048 WHERE message_id=?",
2050 serialize($headers),
2051 is_object($struct) ? serialize($struct) : NULL,
2052 $sql_arr['message_id']
2055 else // insert new record
2058 "INSERT INTO ".get_table_name('messages')."
2059 (user_id, del, cache_key, created, idx, uid, subject, ".$this->db->quoteIdentifier('from').", ".$this->db->quoteIdentifier('to').", cc, date, size, headers, structure)
2060 VALUES (?, 0, ?, ".$this->db->now().", ?, ?, ?, ?, ?, ?, ".$this->db->fromunixtime($headers->timestamp).", ?, ?, ?)",
2061 $_SESSION['user_id'],
2065 (string)substr($this->decode_header($headers->subject, TRUE), 0, 128),
2066 (string)substr($this->decode_header($headers->from, TRUE), 0, 128),
2067 (string)substr($this->decode_header($headers->to, TRUE), 0, 128),
2068 (string)substr($this->decode_header($headers->cc, TRUE), 0, 128),
2069 (int)$headers->size,
2070 serialize($headers),
2071 is_object($struct) ? serialize($struct) : NULL
2077 function remove_message_cache($key, $index)
2080 "DELETE FROM ".get_table_name('messages')."
2084 $_SESSION['user_id'],
2090 function clear_message_cache($key, $start_index=1)
2093 "DELETE FROM ".get_table_name('messages')."
2097 $_SESSION['user_id'],
2105 /* --------------------------------
2106 * encoding/decoding methods
2107 * --------------------------------*/
2110 function decode_address_list($input, $max=null, $decode=true)
2112 $a = $this->_parse_address_list($input, $decode);
2121 foreach ($a as $val)
2124 $address = $val['address'];
2125 $name = preg_replace(array('/^[\'"]/', '/[\'"]$/'), '', trim($val['name']));
2126 if ($name && $address && $name != $address)
2127 $string = sprintf('%s <%s>', strpos($name, ',')!==FALSE ? '"'.$name.'"' : $name, $address);
2133 $out[$j] = array('name' => $name,
2134 'mailto' => $address,
2135 'string' => $string);
2137 if ($max && $j==$max)
2145 function decode_header($input, $remove_quotes=FALSE)
2147 $str = $this->decode_mime_string((string)$input);
2148 if ($str{0}=='"' && $remove_quotes)
2149 $str = str_replace('"', '', $str);
2156 * Decode a mime-encoded string to internal charset
2160 function decode_mime_string($input, $fallback=null)
2164 $pos = strpos($input, '=?');
2167 $out = substr($input, 0, $pos);
2169 $end_cs_pos = strpos($input, "?", $pos+2);
2170 $end_en_pos = strpos($input, "?", $end_cs_pos+1);
2171 $end_pos = strpos($input, "?=", $end_en_pos+1);
2173 $encstr = substr($input, $pos+2, ($end_pos-$pos-2));
2174 $rest = substr($input, $end_pos+2);
2176 $out .= rcube_imap::_decode_mime_string_part($encstr);
2177 $out .= rcube_imap::decode_mime_string($rest, $fallback);
2182 // no encoding information, use fallback
2183 return rcube_charset_convert($input, !empty($fallback) ? $fallback : 'ISO-8859-1');
2188 * Decode a part of a mime-encoded string
2192 function _decode_mime_string_part($str)
2194 $a = explode('?', $str);
2197 // should be in format "charset?encoding?base64_string"
2200 for ($i=2; $i<$count; $i++)
2203 if (($a[1]=="B")||($a[1]=="b"))
2204 $rest = base64_decode($rest);
2205 else if (($a[1]=="Q")||($a[1]=="q"))
2207 $rest = str_replace("_", " ", $rest);
2208 $rest = quoted_printable_decode($rest);
2211 return rcube_charset_convert($rest, $a[0]);
2214 return $str; // we dont' know what to do with this
2218 function mime_decode($input, $encoding='7bit')
2220 switch (strtolower($encoding))
2226 case 'quoted-printable':
2227 return quoted_printable_decode($input);
2231 return base64_decode($input);
2240 function mime_encode($input, $encoding='7bit')
2244 case 'quoted-printable':
2245 return quoted_printable_encode($input);
2249 return base64_encode($input);
2258 // convert body chars according to the ctype_parameters
2259 function charset_decode($body, $ctype_param)
2261 if (is_array($ctype_param) && !empty($ctype_param['charset']))
2262 return rcube_charset_convert($body, $ctype_param['charset']);
2264 // defaults to what is specified in the class header
2265 return rcube_charset_convert($body, 'ISO-8859-1');
2271 /* --------------------------------
2273 * --------------------------------*/
2276 function _mod_mailbox($mbox_name, $mode='in')
2278 if (empty($mbox_name) || (!empty($this->root_ns) && $this->root_ns == $mbox_name) || $mbox_name == 'INBOX')
2281 if (!empty($this->root_dir) && $mode=='in')
2282 $mbox_name = $this->root_dir.$this->delimiter.$mbox_name;
2283 else if (strlen($this->root_dir) && $mode=='out')
2284 $mbox_name = substr($mbox_name, strlen($this->root_dir)+1);
2290 // sort mailboxes first by default folders and then in alphabethical order
2291 function _sort_mailbox_list($a_folders)
2293 $a_out = $a_defaults = array();
2295 // find default folders and skip folders starting with '.'
2296 foreach($a_folders as $i => $folder)
2298 if ($folder{0}=='.')
2301 if (($p = array_search(strtolower($folder), $this->default_folders_lc))!==FALSE)
2302 $a_defaults[$p] = $folder;
2310 return array_merge($a_defaults, $a_out);
2313 function get_id($uid, $mbox_name=NULL)
2315 return $this->_uid2id($uid, $this->_mod_mailbox($mbox_name));
2318 function get_uid($id,$mbox_name=NULL)
2320 return $this->_id2uid($id, $this->_mod_mailbox($mbox_name));
2323 function _uid2id($uid, $mbox_name=NULL)
2326 $mbox_name = $this->mailbox;
2328 if (!isset($this->uid_id_map[$mbox_name][$uid]))
2329 $this->uid_id_map[$mbox_name][$uid] = iil_C_UID2ID($this->conn, $mbox_name, $uid);
2331 return $this->uid_id_map[$mbox_name][$uid];
2334 function _id2uid($id, $mbox_name=NULL)
2337 $mbox_name = $this->mailbox;
2339 return iil_C_ID2UID($this->conn, $mbox_name, $id);
2343 // parse string or array of server capabilities and put them in internal array
2344 function _parse_capability($caps)
2346 if (!is_array($caps))
2347 $cap_arr = explode(' ', $caps);
2351 foreach ($cap_arr as $cap)
2353 if ($cap=='CAPABILITY')
2356 if (strpos($cap, '=')>0)
2358 list($key, $value) = explode('=', $cap);
2359 if (!is_array($this->capabilities[$key]))
2360 $this->capabilities[$key] = array();
2362 $this->capabilities[$key][] = $value;
2365 $this->capabilities[$cap] = TRUE;
2370 // subscribe/unsubscribe a list of mailboxes and update local cache
2371 function _change_subscription($a_mboxes, $mode)
2375 if (is_array($a_mboxes))
2376 foreach ($a_mboxes as $i => $mbox_name)
2378 $mailbox = $this->_mod_mailbox($mbox_name);
2379 $a_mboxes[$i] = $mailbox;
2381 if ($mode=='subscribe')
2382 $result = iil_C_Subscribe($this->conn, $mailbox);
2383 else if ($mode=='unsubscribe')
2384 $result = iil_C_UnSubscribe($this->conn, $mailbox);
2390 // get cached mailbox list
2393 $a_mailbox_cache = $this->get_cache('mailboxes');
2394 if (!is_array($a_mailbox_cache))
2397 // modify cached list
2398 if ($mode=='subscribe')
2399 $a_mailbox_cache = array_merge($a_mailbox_cache, $a_mboxes);
2400 else if ($mode=='unsubscribe')
2401 $a_mailbox_cache = array_diff($a_mailbox_cache, $a_mboxes);
2403 // write mailboxlist to cache
2404 $this->update_cache('mailboxes', $this->_sort_mailbox_list($a_mailbox_cache));
2411 // increde/decrese messagecount for a specific mailbox
2412 function _set_messagecount($mbox_name, $mode, $increment)
2414 $a_mailbox_cache = FALSE;
2415 $mailbox = $mbox_name ? $mbox_name : $this->mailbox;
2416 $mode = strtoupper($mode);
2418 $a_mailbox_cache = $this->get_cache('messagecount');
2420 if (!is_array($a_mailbox_cache[$mailbox]) || !isset($a_mailbox_cache[$mailbox][$mode]) || !is_numeric($increment))
2423 // add incremental value to messagecount
2424 $a_mailbox_cache[$mailbox][$mode] += $increment;
2426 // there's something wrong, delete from cache
2427 if ($a_mailbox_cache[$mailbox][$mode] < 0)
2428 unset($a_mailbox_cache[$mailbox][$mode]);
2430 // write back to cache
2431 $this->update_cache('messagecount', $a_mailbox_cache);
2437 // remove messagecount of a specific mailbox from cache
2438 function _clear_messagecount($mbox_name='')
2440 $a_mailbox_cache = FALSE;
2441 $mailbox = $mbox_name ? $mbox_name : $this->mailbox;
2443 $a_mailbox_cache = $this->get_cache('messagecount');
2445 if (is_array($a_mailbox_cache[$mailbox]))
2447 unset($a_mailbox_cache[$mailbox]);
2448 $this->update_cache('messagecount', $a_mailbox_cache);
2453 // split RFC822 header string into an associative array
2454 function _parse_headers($headers)
2456 $a_headers = array();
2457 $lines = explode("\n", $headers);
2459 for ($i=0; $i<$c; $i++)
2461 if ($p = strpos($lines[$i], ': '))
2463 $field = strtolower(substr($lines[$i], 0, $p));
2464 $value = trim(substr($lines[$i], $p+1));
2466 $a_headers[$field] = $value;
2474 function _parse_address_list($str, $decode=true)
2476 // remove any newlines and carriage returns before
2477 $a = $this->_explode_quoted_string('[,;]', preg_replace( "/[\r\n]/", " ", $str));
2480 foreach ($a as $key => $val)
2482 $val = preg_replace("/([\"\w])</", "$1 <", $val);
2483 $sub_a = $this->_explode_quoted_string(' ', $decode ? $this->decode_header($val) : $val);
2484 $result[$key]['name'] = '';
2486 foreach ($sub_a as $k => $v)
2488 if (strpos($v, '@') > 0)
2489 $result[$key]['address'] = str_replace('<', '', str_replace('>', '', $v));
2491 $result[$key]['name'] .= (empty($result[$key]['name'])?'':' ').str_replace("\"",'',stripslashes($v));
2494 if (empty($result[$key]['name']))
2495 $result[$key]['name'] = $result[$key]['address'];
2502 function _explode_quoted_string($delimiter, $string)
2505 $strlen = strlen($string);
2506 for ($q=$p=$i=0; $i < $strlen; $i++)
2508 if ($string{$i} == "\"" && $string{$i-1} != "\\")
2509 $q = $q ? false : true;
2510 else if (!$q && preg_match("/$delimiter/", $string{$i}))
2512 $result[] = substr($string, $p, $i - $p);
2517 $result[] = substr($string, $p);
2524 * Class representing a message part
2526 class rcube_message_part
2529 var $ctype_primary = 'text';
2530 var $ctype_secondary = 'plain';
2531 var $mimetype = 'text/plain';
2532 var $disposition = '';
2534 var $encoding = '8bit';
2537 var $headers = array();
2538 var $d_parameters = array();
2539 var $ctype_parameters = array();
2545 * rcube_header_sorter
2547 * Class for sorting an array of iilBasicHeader objects in a predetermined order.
2549 * @author Eric Stadtherr
2551 class rcube_header_sorter
2553 var $sequence_numbers = array();
2556 * set the predetermined sort order.
2558 * @param array $seqnums numerically indexed array of IMAP message sequence numbers
2560 function set_sequence_numbers($seqnums)
2562 $this->sequence_numbers = $seqnums;
2566 * sort the array of header objects
2568 * @param array $headers array of iilBasicHeader objects indexed by UID
2570 function sort_headers(&$headers)
2573 * uksort would work if the keys were the sequence number, but unfortunately
2574 * the keys are the UIDs. We'll use uasort instead and dereference the value
2575 * to get the sequence number (in the "id" field).
2577 * uksort($headers, array($this, "compare_seqnums"));
2579 uasort($headers, array($this, "compare_seqnums"));
2583 * get the position of a message sequence number in my sequence_numbers array
2585 * @param integer $seqnum message sequence number contained in sequence_numbers
2587 function position_of($seqnum)
2589 $c = count($this->sequence_numbers);
2590 for ($pos = 0; $pos <= $c; $pos++)
2592 if ($this->sequence_numbers[$pos] == $seqnum)
2599 * Sort method called by uasort()
2601 function compare_seqnums($a, $b)
2603 // First get the sequence number from the header object (the 'id' field).
2607 // then find each sequence number in my ordered list
2608 $posa = $this->position_of($seqa);
2609 $posb = $this->position_of($seqb);
2611 // return the relative position as the comparison value
2612 $ret = $posa - $posb;
2619 * Add quoted-printable encoding to a given string
2621 * @param string $input string to encode
2622 * @param int $line_max add new line after this number of characters
2623 * @param boolena $space_conf true if spaces should be converted into =20
2624 * @return encoded string
2626 function quoted_printable_encode($input, $line_max=76, $space_conv=false)
2628 $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
2629 $lines = preg_split("/(?:\r\n|\r|\n)/", $input);
2634 while( list(, $line) = each($lines))
2636 //$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary
2637 $linlen = strlen($line);
2639 for($i = 0; $i < $linlen; $i++)
2641 $c = substr( $line, $i, 1 );
2643 if ( ( $i == 0 ) && ( $dec == 46 ) ) // convert first point in the line into =2E
2649 if ( $i == ( $linlen - 1 ) ) // convert space at eol only
2653 else if ( $space_conv )
2658 else if ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) // always encode "\t", which is *not* required
2660 $h2 = floor($dec/16);
2661 $h1 = floor($dec%16);
2662 $c = $escape.$hex["$h2"].$hex["$h1"];
2665 if ( (strlen($newline) + strlen($c)) >= $line_max ) // CRLF is not counted
2667 $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
2669 // check if newline first character will be point or not
2677 $output .= $newline.$eol;
2680 return trim($output);