| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| 5 | | program/include/rcube_imap.inc | |
|---|
| 6 | | | |
|---|
| 7 | | This file is part of the RoundCube Webmail client | |
|---|
| 8 | | Copyright (C) 2005, RoundCube Dev. - Switzerland | |
|---|
| 9 | | Licensed under the GNU GPL | |
|---|
| 10 | | | |
|---|
| 11 | | PURPOSE: | |
|---|
| 12 | | IMAP wrapper that implements the Iloha IMAP Library (IIL) | |
|---|
| 13 | | See http://ilohamail.org/ for details | |
|---|
| 14 | | | |
|---|
| 15 | +-----------------------------------------------------------------------+ |
|---|
| 16 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 17 | +-----------------------------------------------------------------------+ |
|---|
| 18 | |
|---|
| 19 | $Id$ |
|---|
| 20 | |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | require_once('lib/imap.inc'); |
|---|
| 25 | require_once('lib/mime.inc'); |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | class rcube_imap |
|---|
| 29 | { |
|---|
| 30 | var $conn; |
|---|
| 31 | var $root_ns = ''; |
|---|
| 32 | var $root_dir = ''; |
|---|
| 33 | var $mailbox = 'INBOX'; |
|---|
| 34 | var $list_page = 1; |
|---|
| 35 | var $page_size = 10; |
|---|
| 36 | var $delimiter = NULL; |
|---|
| 37 | var $caching_enabled = FALSE; |
|---|
| 38 | var $default_folders = array('inbox', 'drafts', 'sent', 'junk', 'trash'); |
|---|
| 39 | var $cache = array(); |
|---|
| 40 | var $cache_changes = array(); |
|---|
| 41 | var $uid_id_map = array(); |
|---|
| 42 | var $msg_headers = array(); |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | // PHP 5 constructor |
|---|
| 46 | function __construct() |
|---|
| 47 | { |
|---|
| 48 | |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | // PHP 4 compatibility |
|---|
| 52 | function rcube_imap() |
|---|
| 53 | { |
|---|
| 54 | $this->__construct(); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | function connect($host, $user, $pass, $port=143, $use_ssl=FALSE) |
|---|
| 59 | { |
|---|
| 60 | global $ICL_PORT, $CONFIG; |
|---|
| 61 | |
|---|
| 62 | // check for Open-SSL support in PHP build |
|---|
| 63 | if ($use_ssl && in_array('openssl', get_loaded_extensions())) |
|---|
| 64 | $ICL_SSL = TRUE; |
|---|
| 65 | else if ($use_ssl) |
|---|
| 66 | { |
|---|
| 67 | raise_error(array('code' => 403, |
|---|
| 68 | 'type' => 'imap', |
|---|
| 69 | 'message' => 'Open SSL not available;'), TRUE, FALSE); |
|---|
| 70 | $port = 143; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | $ICL_PORT = $port; |
|---|
| 74 | $this->conn = iil_Connect($host, $user, $pass, array('imap' => 'check')); |
|---|
| 75 | $this->host = $host; |
|---|
| 76 | $this->user = $user; |
|---|
| 77 | $this->pass = $pass; |
|---|
| 78 | $this->port = $port; |
|---|
| 79 | $this->ssl = $use_ssl; |
|---|
| 80 | |
|---|
| 81 | // print trace mesages |
|---|
| 82 | if ($this->conn && ($CONFIG['debug_level'] & 8)) |
|---|
| 83 | console($this->conn->message); |
|---|
| 84 | |
|---|
| 85 | // write error log |
|---|
| 86 | else if (!$this->conn && $GLOBALS['iil_error']) |
|---|
| 87 | { |
|---|
| 88 | raise_error(array('code' => 403, |
|---|
| 89 | 'type' => 'imap', |
|---|
| 90 | 'message' => $GLOBALS['iil_error']), TRUE, FALSE); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | // get account namespace |
|---|
| 94 | if ($this->conn) |
|---|
| 95 | { |
|---|
| 96 | iil_C_NameSpace($this->conn); |
|---|
| 97 | |
|---|
| 98 | if (!empty($this->conn->delimiter)) |
|---|
| 99 | $this->delimiter = $this->conn->delimiter; |
|---|
| 100 | if (!empty($this->conn->rootdir)) |
|---|
| 101 | $this->root_ns = $this->conn->rootdir; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | return $this->conn ? TRUE : FALSE; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | function close() |
|---|
| 109 | { |
|---|
| 110 | if ($this->conn) |
|---|
| 111 | iil_Close($this->conn); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | function reconnect() |
|---|
| 116 | { |
|---|
| 117 | $this->close(); |
|---|
| 118 | $this->connect($this->host, $this->user, $this->pass, $this->port, $this->ssl); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | function set_rootdir($root) |
|---|
| 123 | { |
|---|
| 124 | if (ereg('[\.\/]$', $root)) //(substr($root, -1, 1)==='/') |
|---|
| 125 | $root = substr($root, 0, -1); |
|---|
| 126 | |
|---|
| 127 | $this->root_dir = $root; |
|---|
| 128 | |
|---|
| 129 | if (empty($this->delimiter)) |
|---|
| 130 | $this->get_hierarchy_delimiter(); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | function set_default_mailboxes($arr) |
|---|
| 135 | { |
|---|
| 136 | if (is_array($arr)) |
|---|
| 137 | { |
|---|
| 138 | $this->default_folders = array(); |
|---|
| 139 | |
|---|
| 140 | // add mailbox names lower case |
|---|
| 141 | foreach ($arr as $mbox) |
|---|
| 142 | $this->default_folders[] = strtolower($mbox); |
|---|
| 143 | |
|---|
| 144 | // add inbox if not included |
|---|
| 145 | if (!in_array('inbox', $this->default_folders)) |
|---|
| 146 | array_unshift($arr, 'inbox'); |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | function set_mailbox($mbox) |
|---|
| 152 | { |
|---|
| 153 | $mailbox = $this->_mod_mailbox($mbox); |
|---|
| 154 | |
|---|
| 155 | if ($this->mailbox == $mailbox) |
|---|
| 156 | return; |
|---|
| 157 | |
|---|
| 158 | $this->mailbox = $mailbox; |
|---|
| 159 | |
|---|
| 160 | // clear messagecount cache for this mailbox |
|---|
| 161 | $this->_clear_messagecount($mailbox); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | function set_page($page) |
|---|
| 166 | { |
|---|
| 167 | $this->list_page = (int)$page; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | function set_pagesize($size) |
|---|
| 172 | { |
|---|
| 173 | $this->page_size = (int)$size; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | function get_mailbox_name() |
|---|
| 178 | { |
|---|
| 179 | return $this->conn ? $this->_mod_mailbox($this->mailbox, 'out') : ''; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | function get_hierarchy_delimiter() |
|---|
| 184 | { |
|---|
| 185 | if ($this->conn && empty($this->delimiter)) |
|---|
| 186 | $this->delimiter = iil_C_GetHierarchyDelimiter($this->conn); |
|---|
| 187 | |
|---|
| 188 | return $this->delimiter; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | // public method for mailbox listing |
|---|
| 192 | // convert mailbox name with root dir first |
|---|
| 193 | function list_mailboxes($root='', $filter='*') |
|---|
| 194 | { |
|---|
| 195 | $a_out = array(); |
|---|
| 196 | $a_mboxes = $this->_list_mailboxes($root, $filter); |
|---|
| 197 | |
|---|
| 198 | foreach ($a_mboxes as $mbox) |
|---|
| 199 | { |
|---|
| 200 | $name = $this->_mod_mailbox($mbox, 'out'); |
|---|
| 201 | if (strlen($name)) |
|---|
| 202 | $a_out[] = $name; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | // sort mailboxes |
|---|
| 206 | $a_out = $this->_sort_mailbox_list($a_out); |
|---|
| 207 | |
|---|
| 208 | return $a_out; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | // private method for mailbox listing |
|---|
| 212 | function _list_mailboxes($root='', $filter='*') |
|---|
| 213 | { |
|---|
| 214 | $a_defaults = $a_out = array(); |
|---|
| 215 | |
|---|
| 216 | // get cached folder list |
|---|
| 217 | $a_mboxes = $this->get_cache('mailboxes'); |
|---|
| 218 | if (is_array($a_mboxes)) |
|---|
| 219 | return $a_mboxes; |
|---|
| 220 | |
|---|
| 221 | // retrieve list of folders from IMAP server |
|---|
| 222 | $a_folders = iil_C_ListSubscribed($this->conn, $this->_mod_mailbox($root), $filter); |
|---|
| 223 | |
|---|
| 224 | if (!is_array($a_folders) || !sizeof($a_folders)) |
|---|
| 225 | $a_folders = array(); |
|---|
| 226 | |
|---|
| 227 | // create INBOX if it does not exist |
|---|
| 228 | if (!in_array_nocase('INBOX', $a_folders)) |
|---|
| 229 | { |
|---|
| 230 | $this->create_mailbox('INBOX', TRUE); |
|---|
| 231 | array_unshift($a_folders, 'INBOX'); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | $a_mailbox_cache = array(); |
|---|
| 235 | |
|---|
| 236 | // write mailboxlist to cache |
|---|
| 237 | $this->update_cache('mailboxes', $a_folders); |
|---|
| 238 | |
|---|
| 239 | return $a_folders; |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | // get message count for a specific mailbox; acceptes modes are: ALL, UNSEEN |
|---|
| 244 | function messagecount($mbox='', $mode='ALL', $force=FALSE) |
|---|
| 245 | { |
|---|
| 246 | $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox; |
|---|
| 247 | return $this->_messagecount($mailbox, $mode, $force); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | // private method for getting nr of mesages |
|---|
| 251 | function _messagecount($mailbox='', $mode='ALL', $force=FALSE) |
|---|
| 252 | { |
|---|
| 253 | $a_mailbox_cache = FALSE; |
|---|
| 254 | $mode = strtoupper($mode); |
|---|
| 255 | |
|---|
| 256 | if (!$mailbox) |
|---|
| 257 | $mailbox = $this->mailbox; |
|---|
| 258 | |
|---|
| 259 | $a_mailbox_cache = $this->get_cache('messagecount'); |
|---|
| 260 | |
|---|
| 261 | // return cached value |
|---|
| 262 | if (!$force && is_array($a_mailbox_cache[$mailbox]) && isset($a_mailbox_cache[$mailbox][$mode])) |
|---|
| 263 | return $a_mailbox_cache[$mailbox][$mode]; |
|---|
| 264 | |
|---|
| 265 | // get message count and store in cache |
|---|
| 266 | if ($mode == 'UNSEEN') |
|---|
| 267 | $count = iil_C_CountUnseen($this->conn, $mailbox); |
|---|
| 268 | else |
|---|
| 269 | $count = iil_C_CountMessages($this->conn, $mailbox); |
|---|
| 270 | |
|---|
| 271 | // print "/**** get messagecount for $mailbox ($mode): $count ****/\n"; |
|---|
| 272 | |
|---|
| 273 | if (is_array($a_mailbox_cache[$mailbox])) |
|---|
| 274 | $a_mailbox_cache[$mailbox] = array(); |
|---|
| 275 | |
|---|
| 276 | $a_mailbox_cache[$mailbox][$mode] = (int)$count; |
|---|
| 277 | |
|---|
| 278 | // write back to cache |
|---|
| 279 | $this->update_cache('messagecount', $a_mailbox_cache); |
|---|
| 280 | |
|---|
| 281 | //var_dump($a_mailbox_cache); |
|---|
| 282 | |
|---|
| 283 | return (int)$count; |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | // public method for listing headers |
|---|
| 288 | // convert mailbox name with root dir first |
|---|
| 289 | function list_headers($mbox='', $page=NULL, $sort_field='date', $sort_order='DESC') |
|---|
| 290 | { |
|---|
| 291 | $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox; |
|---|
| 292 | return $this->_list_headers($mailbox, $page, $sort_field, $sort_order); |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | // private method for listing message header |
|---|
| 297 | function _list_headers($mailbox='', $page=NULL, $sort_field='date', $sort_order='DESC') |
|---|
| 298 | { |
|---|
| 299 | $max = $this->_messagecount($mailbox /*, 'ALL', TRUE*/); |
|---|
| 300 | |
|---|
| 301 | if (!strlen($mailbox)) |
|---|
| 302 | return array(); |
|---|
| 303 | |
|---|
| 304 | // get cached headers |
|---|
| 305 | $a_msg_headers = $this->get_cache($mailbox.'.msg'); |
|---|
| 306 | |
|---|
| 307 | // print "/**** count = $max; headers = ".sizeof($a_msg_headers)." ****/\n"; |
|---|
| 308 | |
|---|
| 309 | // retrieve headers from IMAP |
|---|
| 310 | if (!is_array($a_msg_headers) || sizeof($a_msg_headers) != $max) |
|---|
| 311 | { |
|---|
| 312 | $a_header_index = iil_C_FetchHeaders($this->conn, $mailbox, "1:$max"); |
|---|
| 313 | $a_msg_headers = array(); |
|---|
| 314 | |
|---|
| 315 | if (!empty($a_header_index)) |
|---|
| 316 | foreach ($a_header_index as $i => $headers) |
|---|
| 317 | if (!$headers->deleted) |
|---|
| 318 | $a_msg_headers[$headers->uid] = $headers; |
|---|
| 319 | |
|---|
| 320 | // print "/**** fetch headers ****/\n"; |
|---|
| 321 | } |
|---|
| 322 | else |
|---|
| 323 | $headers_cached = TRUE; |
|---|
| 324 | |
|---|
| 325 | if (!is_array($a_msg_headers)) |
|---|
| 326 | return array(); |
|---|
| 327 | |
|---|
| 328 | // sort headers by a specific col |
|---|
| 329 | $a_headers = iil_SortHeaders($a_msg_headers, $sort_field, $sort_order); |
|---|
| 330 | |
|---|
| 331 | // free memory |
|---|
| 332 | unset($a_msg_headers); |
|---|
| 333 | |
|---|
| 334 | // write headers list to cache |
|---|
| 335 | if (!$headers_cached) |
|---|
| 336 | $this->update_cache($mailbox.'.msg', $a_headers); |
|---|
| 337 | |
|---|
| 338 | if (empty($a_headers)) |
|---|
| 339 | return array(); |
|---|
| 340 | |
|---|
| 341 | // return complete list of messages |
|---|
| 342 | if (strtolower($page)=='all') |
|---|
| 343 | return $a_headers; |
|---|
| 344 | |
|---|
| 345 | $start_msg = ($this->list_page-1) * $this->page_size; |
|---|
| 346 | return array_slice($a_headers, $start_msg, $this->page_size); |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | // return sorted array of message UIDs |
|---|
| 351 | function message_index($mbox='', $sort_field='date', $sort_order='DESC') |
|---|
| 352 | { |
|---|
| 353 | $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox; |
|---|
| 354 | $a_out = array(); |
|---|
| 355 | |
|---|
| 356 | // get array of message headers |
|---|
| 357 | $a_headers = $this->_list_headers($mailbox, 'all', $sort_field, $sort_order); |
|---|
| 358 | |
|---|
| 359 | if (is_array($a_headers)) |
|---|
| 360 | foreach ($a_headers as $header) |
|---|
| 361 | $a_out[] = $header->uid; |
|---|
| 362 | |
|---|
| 363 | return $a_out; |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | function sync_header_index($mbox=NULL) |
|---|
| 368 | { |
|---|
| 369 | |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | function search($mbox='', $criteria='ALL') |
|---|
| 374 | { |
|---|
| 375 | $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox; |
|---|
| 376 | $a_messages = iil_C_Search($this->conn, $mailbox, $criteria); |
|---|
| 377 | return $a_messages; |
|---|
| 378 | } |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | function get_headers($uid, $mbox=NULL) |
|---|
| 382 | { |
|---|
| 383 | $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox; |
|---|
| 384 | |
|---|
| 385 | // get cached headers |
|---|
| 386 | $a_msg_headers = $this->get_cache($mailbox.'.msg'); |
|---|
| 387 | |
|---|
| 388 | // return cached header |
|---|
| 389 | if ($a_msg_headers[$uid]) |
|---|
| 390 | return $a_msg_headers[$uid]; |
|---|
| 391 | |
|---|
| 392 | $msg_id = $this->_uid2id($uid); |
|---|
| 393 | $header = iil_C_FetchHeader($this->conn, $mailbox, $msg_id); |
|---|
| 394 | |
|---|
| 395 | // write headers cache |
|---|
| 396 | $a_msg_headers[$uid] = $header; |
|---|
| 397 | $this->update_cache($mailbox.'.msg', $a_msg_headers); |
|---|
| 398 | |
|---|
| 399 | return $header; |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | |
|---|
| 403 | function get_body($uid, $part=1) |
|---|
| 404 | { |
|---|
| 405 | if (!($msg_id = $this->_uid2id($uid))) |
|---|
| 406 | return FALSE; |
|---|
| 407 | |
|---|
| 408 | $structure_str = iil_C_FetchStructureString($this->conn, $this->mailbox, $msg_id); |
|---|
| 409 | $structure = iml_GetRawStructureArray($structure_str); |
|---|
| 410 | $body = iil_C_FetchPartBody($this->conn, $this->mailbox, $msg_id, $part); |
|---|
| 411 | |
|---|
| 412 | $encoding = iml_GetPartEncodingCode($structure, $part); |
|---|
| 413 | |
|---|
| 414 | if ($encoding==3) $body = $this->mime_decode($body, 'base64'); |
|---|
| 415 | else if ($encoding==4) $body = $this->mime_decode($body, 'quoted-printable'); |
|---|
| 416 | |
|---|
| 417 | return $body; |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | function get_raw_body($uid) |
|---|
| 422 | { |
|---|
| 423 | if (!($msg_id = $this->_uid2id($uid))) |
|---|
| 424 | return FALSE; |
|---|
| 425 | |
|---|
| 426 | $body = iil_C_FetchPartHeader($this->conn, $this->mailbox, $msg_id, NULL); |
|---|
| 427 | $body .= iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, NULL, 1); |
|---|
| 428 | |
|---|
| 429 | return $body; |
|---|
| 430 | } |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | // set message flag to one or several messages |
|---|
| 434 | // possible flgs are: SEEN, DELETED, RECENT, ANSWERED, DRAFT |
|---|
| 435 | function set_flag($uids, $flag) |
|---|
| 436 | { |
|---|
| 437 | $flag = strtoupper($flag); |
|---|
| 438 | $msg_ids = array(); |
|---|
| 439 | if (!is_array($uids)) |
|---|
| 440 | $uids = array($uids); |
|---|
| 441 | |
|---|
| 442 | foreach ($uids as $uid) |
|---|
| 443 | $msg_ids[] = $this->_uid2id($uid); |
|---|
| 444 | |
|---|
| 445 | if ($flag=='UNSEEN') |
|---|
| 446 | $result = iil_C_Unseen($this->conn, $this->mailbox, join(',', $msg_ids)); |
|---|
| 447 | else |
|---|
| 448 | $result = iil_C_Flag($this->conn, $this->mailbox, join(',', $msg_ids), $flag); |
|---|
| 449 | |
|---|
| 450 | // reload message headers if cached |
|---|
| 451 | $cache_key = $this->mailbox.'.msg'; |
|---|
| 452 | if ($this->caching_enabled && $result && ($a_cached_headers = $this->get_cache($cache_key))) |
|---|
| 453 | { |
|---|
| 454 | // close and re-open connection |
|---|
| 455 | $this->reconnect(); |
|---|
| 456 | |
|---|
| 457 | foreach ($uids as $uid) |
|---|
| 458 | { |
|---|
| 459 | if (isset($a_cached_headers[$uid])) |
|---|
| 460 | { |
|---|
| 461 | unset($this->cache[$cache_key][$uid]); |
|---|
| 462 | $this->get_headers($uid); |
|---|
| 463 | } |
|---|
| 464 | } |
|---|
| 465 | } |
|---|
| 466 | |
|---|
| 467 | // set nr of messages that were flaged |
|---|
| 468 | $count = sizeof($msg_ids); |
|---|
| 469 | |
|---|
| 470 | // clear message count cache |
|---|
| 471 | if ($result && $flag=='SEEN') |
|---|
| 472 | $this->_set_messagecount($this->mailbox, 'UNSEEN', $count*(-1)); |
|---|
| 473 | else if ($result && $flag=='UNSEEN') |
|---|
| 474 | $this->_set_messagecount($this->mailbox, 'UNSEEN', $count); |
|---|
| 475 | else if ($result && $flag=='DELETED') |
|---|
| 476 | $this->_set_messagecount($this->mailbox, 'ALL', $count*(-1)); |
|---|
| 477 | |
|---|
| 478 | return $result; |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | |
|---|
| 482 | // append a mail message (source) to a specific mailbox |
|---|
| 483 | function save_message($mbox, $message) |
|---|
| 484 | { |
|---|
| 485 | $mailbox = $this->_mod_mailbox($mbox); |
|---|
| 486 | |
|---|
| 487 | // make shure mailbox exists |
|---|
| 488 | if (in_array($mailbox, $this->_list_mailboxes())) |
|---|
| 489 | $saved = iil_C_Append($this->conn, $mailbox, $message); |
|---|
| 490 | |
|---|
| 491 | if ($saved) |
|---|
| 492 | { |
|---|
| 493 | // increase messagecount of the target mailbox |
|---|
| 494 | $this->_set_messagecount($mailbox, 'ALL', 1); |
|---|
| 495 | } |
|---|
| 496 | |
|---|
| 497 | return $saved; |
|---|
| 498 | } |
|---|
| 499 | |
|---|
| 500 | |
|---|
| 501 | // move a message from one mailbox to another |
|---|
| 502 | function move_message($uids, $to_mbox, $from_mbox='') |
|---|
| 503 | { |
|---|
| 504 | $to_mbox = $this->_mod_mailbox($to_mbox); |
|---|
| 505 | $from_mbox = $from_mbox ? $this->_mod_mailbox($from_mbox) : $this->mailbox; |
|---|
| 506 | |
|---|
| 507 | // make shure mailbox exists |
|---|
| 508 | if (!in_array($to_mbox, $this->_list_mailboxes())) |
|---|
| 509 | return FALSE; |
|---|
| 510 | |
|---|
| 511 | // convert the list of uids to array |
|---|
| 512 | $a_uids = is_string($uids) ? explode(',', $uids) : (is_array($uids) ? $uids : NULL); |
|---|
| 513 | |
|---|
| 514 | // exit if no message uids are specified |
|---|
| 515 | if (!is_array($a_uids)) |
|---|
| 516 | return false; |
|---|
| 517 | |
|---|
| 518 | // convert uids to message ids |
|---|
| 519 | $a_mids = array(); |
|---|
| 520 | foreach ($a_uids as $uid) |
|---|
| 521 | $a_mids[] = $this->_uid2id($uid, $from_mbox); |
|---|
| 522 | |
|---|
| 523 | $moved = iil_C_Move($this->conn, join(',', $a_mids), $from_mbox, $to_mbox); |
|---|
| 524 | |
|---|
| 525 | // send expunge command in order to have the moved message |
|---|
| 526 | // really deleted from the source mailbox |
|---|
| 527 | if ($moved) |
|---|
| 528 | { |
|---|
| 529 | $this->expunge($from_mbox, FALSE); |
|---|
| 530 | $this->clear_cache($to_mbox.'.msg'); |
|---|
| 531 | $this->_clear_messagecount($from_mbox); |
|---|
| 532 | $this->_clear_messagecount($to_mbox); |
|---|
| 533 | } |
|---|
| 534 | |
|---|
| 535 | // update cached message headers |
|---|
| 536 | $cache_key = $from_mbox.'.msg'; |
|---|
| 537 | if ($moved && ($a_cached_headers = $this->get_cache($cache_key))) |
|---|
| 538 | { |
|---|
| 539 | foreach ($a_uids as $uid) |
|---|
| 540 | unset($a_cached_headers[$uid]); |
|---|
| 541 | |
|---|
| 542 | $this->update_cache($cache_key, $a_cached_headers); |
|---|
| 543 | } |
|---|
| 544 | |
|---|
| 545 | return $moved; |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | |
|---|
| 549 | // mark messages as deleted and expunge mailbox |
|---|
| 550 | function delete_message($uids, $mbox='') |
|---|
| 551 | { |
|---|
| 552 | $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox; |
|---|
| 553 | |
|---|
| 554 | // convert the list of uids to array |
|---|
| 555 | $a_uids = is_string($uids) ? explode(',', $uids) : (is_array($uids) ? $uids : NULL); |
|---|
| 556 | |
|---|
| 557 | // exit if no message uids are specified |
|---|
| 558 | if (!is_array($a_uids)) |
|---|
| 559 | return false; |
|---|
| 560 | |
|---|
| 561 | |
|---|
| 562 | // convert uids to message ids |
|---|
| 563 | $a_mids = array(); |
|---|
| 564 | foreach ($a_uids as $uid) |
|---|
| 565 | $a_mids[] = $this->_uid2id($uid, $mailbox); |
|---|
| 566 | |
|---|
| 567 | $deleted = iil_C_Delete($this->conn, $mailbox, join(',', $a_mids)); |
|---|
| 568 | |
|---|
| 569 | // send expunge command in order to have the deleted message |
|---|
| 570 | // really deleted from the mailbox |
|---|
| 571 | if ($deleted) |
|---|
| 572 | { |
|---|
| 573 | $this->expunge($mailbox, FALSE); |
|---|
| 574 | $this->_clear_messagecount($mailbox); |
|---|
| 575 | } |
|---|
| 576 | |
|---|
| 577 | // remove deleted messages from cache |
|---|
| 578 | if ($deleted && ($a_cached_headers = $this->get_cache($mailbox.'.msg'))) |
|---|
| 579 | { |
|---|
| 580 | foreach ($a_uids as $uid) |
|---|
| 581 | unset($a_cached_headers[$uid]); |
|---|
| 582 | |
|---|
| 583 | $this->update_cache($mailbox.'.msg', $a_cached_headers); |
|---|
| 584 | } |
|---|
| 585 | |
|---|
| 586 | return $deleted; |
|---|
| 587 | } |
|---|
| 588 | |
|---|
| 589 | |
|---|
| 590 | // send IMAP expunge command and clear cache |
|---|
| 591 | function expunge($mbox='', $clear_cache=TRUE) |
|---|
| 592 | { |
|---|
| 593 | $mailbox = $mbox ? $this->_mod_mailbox($mbox) : $this->mailbox; |
|---|
| 594 | |
|---|
| 595 | $result = iil_C_Expunge($this->conn, $mailbox); |
|---|
| 596 | |
|---|
| 597 | if ($result>=0 && $clear_cache) |
|---|
| 598 | { |
|---|
| 599 | $this->clear_cache($mailbox.'.msg'); |
|---|
| 600 | $this->_clear_messagecount($mailbox); |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | return $result; |
|---|
| 604 | } |
|---|
| 605 | |
|---|
| 606 | |
|---|
| 607 | |
|---|
| 608 | /* -------------------------------- |
|---|
| 609 | * folder managment |
|---|
| 610 | * --------------------------------*/ |
|---|
| 611 | |
|---|
| 612 | |
|---|
| 613 | // return an array with all folders available in IMAP server |
|---|
| 614 | function list_unsubscribed($root='') |
|---|
| 615 | { |
|---|
| 616 | static $sa_unsubscribed; |
|---|
| 617 | |
|---|
| 618 | if (is_array($sa_unsubscribed)) |
|---|
| 619 | return $sa_unsubscribed; |
|---|
| 620 | |
|---|
| 621 | // retrieve list of folders from IMAP server |
|---|
| 622 | $a_mboxes = iil_C_ListMailboxes($this->conn, $this->_mod_mailbox($root), '*'); |
|---|
| 623 | |
|---|
| 624 | // modify names with root dir |
|---|
| 625 | foreach ($a_mboxes as $mbox) |
|---|
| 626 | { |
|---|
| 627 | $name = $this->_mod_mailbox($mbox, 'out'); |
|---|
| 628 | if (strlen($name)) |
|---|
| 629 | $a_folders[] = $name; |
|---|
| 630 | } |
|---|
| 631 | |
|---|
| 632 | // filter folders and sort them |
|---|
| 633 | $sa_unsubscribed = $this->_sort_mailbox_list($a_folders); |
|---|
| 634 | return $sa_unsubscribed; |
|---|
| 635 | } |
|---|
| 636 | |
|---|
| 637 | |
|---|
| 638 | // subscribe to a specific mailbox(es) |
|---|
| 639 | function subscribe($mbox, $mode='subscribe') |
|---|
| 640 | { |
|---|
| 641 | if (is_array($mbox)) |
|---|
| 642 | $a_mboxes = $mbox; |
|---|
| 643 | else if (is_string($mbox) && strlen($mbox)) |
|---|
| 644 | $a_mboxes = explode(',', $mbox); |
|---|
| 645 | |
|---|
| 646 | // let this common function do the main work |
|---|
| 647 | return $this->_change_subscription($a_mboxes, 'subscribe'); |
|---|
| 648 | } |
|---|
| 649 | |
|---|
| 650 | |
|---|
| 651 | // unsubscribe mailboxes |
|---|
| 652 | function unsubscribe($mbox) |
|---|
| 653 | { |
|---|
| 654 | if (is_array($mbox)) |
|---|
| 655 | $a_mboxes = $mbox; |
|---|
| 656 | else if (is_string($mbox) && strlen($mbox)) |
|---|
| 657 | $a_mboxes = explode(',', $mbox); |
|---|
| 658 | |
|---|
| 659 | // let this common function do the main work |
|---|
| 660 | return $this->_change_subscription($a_mboxes, 'unsubscribe'); |
|---|
| 661 | } |
|---|
| 662 | |
|---|
| 663 | |
|---|
| 664 | // create a new mailbox on the server and register it in local cache |
|---|
| 665 | function create_mailbox($name, $subscribe=FALSE) |
|---|
| 666 | { |
|---|
| 667 | $result = FALSE; |
|---|
| 668 | $abs_name = $this->_mod_mailbox($name); |
|---|
| 669 | $a_mailbox_cache = $this->get_cache('mailboxes'); |
|---|
| 670 | |
|---|
| 671 | if (strlen($this->root_ns)) |
|---|
| 672 | $abs_name = $this->root_ns.$abs_name; |
|---|
| 673 | |
|---|
| 674 | if (strlen($abs_name) && (!is_array($a_mailbox_cache) || !in_array($abs_name, $a_mailbox_cache))) |
|---|
| 675 | $result = iil_C_CreateFolder($this->conn, iil_utf7_encode($abs_name)); |
|---|
| 676 | |
|---|
| 677 | // update mailboxlist cache |
|---|
| 678 | if ($result && $subscribe) |
|---|
| 679 | $this->subscribe($this->root_ns.$name); |
|---|
| 680 | |
|---|
| 681 | return $result ? $this->root_ns.$name : FALSE; |
|---|
| 682 | } |
|---|
| 683 | |
|---|
| 684 | |
|---|
| 685 | // set a new name to an existing mailbox |
|---|
| 686 | function rename_mailbox($mbox, $new_name) |
|---|
| 687 | { |
|---|
| 688 | // not implemented yet |
|---|
| 689 | } |
|---|
| 690 | |
|---|
| 691 | |
|---|
| 692 | // remove mailboxes from server |
|---|
| 693 | function delete_mailbox($mbox) |
|---|
| 694 | { |
|---|
| 695 | $deleted = FALSE; |
|---|
| 696 | |
|---|
| 697 | if (is_array($mbox)) |
|---|
| 698 | $a_mboxes = $mbox; |
|---|
| 699 | else if (is_string($mbox) && strlen($mbox)) |
|---|
| 700 | $a_mboxes = explode(',', $mbox); |
|---|
| 701 | |
|---|
| 702 | if (is_array($a_mboxes)) |
|---|
| 703 | foreach ($a_mboxes as $mbox) |
|---|
| 704 | { |
|---|
| 705 | $mailbox = $this->_mod_mailbox($mbox); |
|---|
| 706 | |
|---|
| 707 | // unsubscribe mailbox before deleting |
|---|
| 708 | iil_C_UnSubscribe($this->conn, $mailbox); |
|---|
| 709 | |
|---|
| 710 | // send delete command to server |
|---|
| 711 | $result = iil_C_DeleteFolder($this->conn, $mailbox); |
|---|
| 712 | if ($result>=0) |
|---|
| 713 | $deleted = TRUE; |
|---|
| 714 | } |
|---|
| 715 | |
|---|
| 716 | // clear mailboxlist cache |
|---|
| 717 | if ($deleted) |
|---|
| 718 | $this->clear_cache('mailboxes'); |
|---|
| 719 | |
|---|
| 720 | return $updated; |
|---|
| 721 | } |
|---|
| 722 | |
|---|
| 723 | |
|---|
| 724 | |
|---|
| 725 | |
|---|
| 726 | /* -------------------------------- |
|---|
| 727 | * internal caching functions |
|---|
| 728 | * --------------------------------*/ |
|---|
| 729 | |
|---|
| 730 | |
|---|
| 731 | function set_caching($set) |
|---|
| 732 | { |
|---|
| 733 | if ($set && function_exists('rcube_read_cache')) |
|---|
| 734 | $this->caching_enabled = TRUE; |
|---|
| 735 | else |
|---|
| 736 | $this->caching_enabled = FALSE; |
|---|
| 737 | } |
|---|
| 738 | |
|---|
| 739 | function get_cache($key) |
|---|
| 740 | { |
|---|
| 741 | // read cache |
|---|
| 742 | if (!isset($this->cache[$key]) && $this->caching_enabled) |
|---|
| 743 | { |
|---|
| 744 | $cache_data = rcube_read_cache('IMAP.'.$key); |
|---|
| 745 | $this->cache[$key] = strlen($cache_data) ? unserialize($cache_data) : FALSE; |
|---|
| 746 | } |
|---|
| 747 | |
|---|
| 748 | return $this->cache[$key]; |
|---|
| 749 | } |
|---|
| 750 | |
|---|
| 751 | |
|---|
| 752 | function update_cache($key, $data) |
|---|
| 753 | { |
|---|
| 754 | $this->cache[$key] = $data; |
|---|
| 755 | $this->cache_changed = TRUE; |
|---|
| 756 | $this->cache_changes[$key] = TRUE; |
|---|
| 757 | } |
|---|
| 758 | |
|---|
| 759 | |
|---|
| 760 | function write_cache() |
|---|
| 761 | { |
|---|
| 762 | if ($this->caching_enabled && $this->cache_changed) |
|---|
| 763 | { |
|---|
| 764 | foreach ($this->cache as $key => $data) |
|---|
| 765 | { |
|---|
| 766 | if ($this->cache_changes[$key]) |
|---|
| 767 | rcube_write_cache('IMAP.'.$key, serialize($data)); |
|---|
| 768 | } |
|---|
| 769 | } |
|---|
| 770 | } |
|---|
| 771 | |
|---|
| 772 | |
|---|
| 773 | function clear_cache($key=NULL) |
|---|
| 774 | { |
|---|
| 775 | if ($key===NULL) |
|---|
| 776 | { |
|---|
| 777 | foreach ($this->cache as $key => $data) |
|---|
| 778 | rcube_clear_cache('IMAP.'.$key); |
|---|
| 779 | |
|---|
| 780 | $this->cache = array(); |
|---|
| 781 | $this->cache_changed = FALSE; |
|---|
| 782 | $this->cache_changes = array(); |
|---|
| 783 | } |
|---|
| 784 | else |
|---|
| 785 | { |
|---|
| 786 | rcube_clear_cache('IMAP.'.$key); |
|---|
| 787 | $this->cache_changes[$key] = FALSE; |
|---|
| 788 | unset($this->cache[$key]); |
|---|
| 789 | } |
|---|
| 790 | } |
|---|
| 791 | |
|---|
| 792 | |
|---|
| 793 | |
|---|
| 794 | /* -------------------------------- |
|---|
| 795 | * encoding/decoding functions |
|---|
| 796 | * --------------------------------*/ |
|---|
| 797 | |
|---|
| 798 | |
|---|
| 799 | function decode_address_list($input, $max=NULL) |
|---|
| 800 | { |
|---|
| 801 | $a = $this->_parse_address_list($input); |
|---|
| 802 | $out = array(); |
|---|
| 803 | |
|---|
| 804 | if (!is_array($a)) |
|---|
| 805 | return $out; |
|---|
| 806 | |
|---|
| 807 | $c = count($a); |
|---|
| 808 | $j = 0; |
|---|
| 809 | |
|---|
| 810 | foreach ($a as $val) |
|---|
| 811 | { |
|---|
| 812 | $j++; |
|---|
| 813 | $address = $val['address']; |
|---|
| 814 | $name = preg_replace(array('/^[\'"]/', '/[\'"]$/'), '', trim($val['name'])); |
|---|
| 815 | $string = $name!==$address ? sprintf('%s <%s>', strpos($name, ',')!==FALSE ? '"'.$name.'"' : $name, $address) : $address; |
|---|
| 816 | |
|---|
| 817 | $out[$j] = array('name' => $name, |
|---|
| 818 | 'mailto' => $address, |
|---|
| 819 | 'string' => $string); |
|---|
| 820 | |
|---|
| 821 | if ($max && $j==$max) |
|---|
| 822 | break; |
|---|
| 823 | } |
|---|
| 824 | |
|---|
| 825 | return $out; |
|---|
| 826 | } |
|---|
| 827 | |
|---|
| 828 | |
|---|
| 829 | function decode_header($input) |
|---|
| 830 | { |
|---|
| 831 | $out = ''; |
|---|
| 832 | |
|---|
| 833 | $pos = strpos($input, '=?'); |
|---|
| 834 | if ($pos !== false) |
|---|
| 835 | { |
|---|
| 836 | $out = substr($input, 0, $pos); |
|---|
| 837 | |
|---|
| 838 | $end_cs_pos = strpos($input, "?", $pos+2); |
|---|
| 839 | $end_en_pos = strpos($input, "?", $end_cs_pos+1); |
|---|
| 840 | $end_pos = strpos($input, "?=", $end_en_pos+1); |
|---|
| 841 | |
|---|
| 842 | $encstr = substr($input, $pos+2, ($end_pos-$pos-2)); |
|---|
| 843 | $rest = substr($input, $end_pos+2); |
|---|
| 844 | |
|---|
| 845 | $out .= $this->decode_mime_string($encstr); |
|---|
| 846 | $out .= $this->decode_header($rest); |
|---|
| 847 | |
|---|
| 848 | return $out; |
|---|
| 849 | } |
|---|
| 850 | else |
|---|
| 851 | return $input; |
|---|
| 852 | } |
|---|
| 853 | |
|---|
| 854 | |
|---|
| 855 | function decode_mime_string($str) |
|---|
| 856 | { |
|---|
| 857 | $a = explode('?', $str); |
|---|
| 858 | $count = count($a); |
|---|
| 859 | |
|---|
| 860 | // should be in format "charset?encoding?base64_string" |
|---|
| 861 | if ($count >= 3) |
|---|
| 862 | { |
|---|
| 863 | for ($i=2; $i<$count; $i++) |
|---|
| 864 | $rest.=$a[$i]; |
|---|
| 865 | |
|---|
| 866 | if (($a[1]=="B")||($a[1]=="b")) |
|---|
| 867 | $rest = base64_decode($rest); |
|---|
| 868 | else if (($a[1]=="Q")||($a[1]=="q")) |
|---|
| 869 | { |
|---|
| 870 | $rest = str_replace("_", " ", $rest); |
|---|
| 871 | $rest = quoted_printable_decode($rest); |
|---|
| 872 | } |
|---|
| 873 | |
|---|
| 874 | return decode_specialchars($rest, $a[0]); |
|---|
| 875 | } |
|---|
| 876 | else |
|---|
| 877 | return $str; //we dont' know what to do with this |
|---|
| 878 | } |
|---|
| 879 | |
|---|
| 880 | |
|---|
| 881 | function mime_decode($input, $encoding='7bit') |
|---|
| 882 | { |
|---|
| 883 | switch (strtolower($encoding)) |
|---|
| 884 | { |
|---|
| 885 | case '7bit': |
|---|
| 886 | return $input; |
|---|
| 887 | break; |
|---|
| 888 | |
|---|
| 889 | case 'quoted-printable': |
|---|
| 890 | return quoted_printable_decode($input); |
|---|
| 891 | break; |
|---|
| 892 | |
|---|
| 893 | case 'base64': |
|---|
| 894 | return base64_decode($input); |
|---|
| 895 | break; |
|---|
| 896 | |
|---|
| 897 | default: |
|---|
| 898 | return $input; |
|---|
| 899 | } |
|---|
| 900 | } |
|---|
| 901 | |
|---|
| 902 | |
|---|
| 903 | function mime_encode($input, $encoding='7bit') |
|---|
| 904 | { |
|---|
| 905 | switch ($encoding) |
|---|
| 906 | { |
|---|
| 907 | case 'quoted-printable': |
|---|
| 908 | return quoted_printable_encode($input); |
|---|
| 909 | break; |
|---|
| 910 | |
|---|
| 911 | case 'base64': |
|---|
| 912 | return base64_encode($input); |
|---|
| 913 | break; |
|---|
| 914 | |
|---|
| 915 | default: |
|---|
| 916 | return $input; |
|---|
| 917 | } |
|---|
| 918 | } |
|---|
| 919 | |
|---|
| 920 | |
|---|
| 921 | // convert body chars according to the ctype_parameters |
|---|
| 922 | function charset_decode($body, $ctype_param) |
|---|
| 923 | { |
|---|
| 924 | if (is_array($ctype_param) && strlen($ctype_param['charset'])) |
|---|
| 925 | return decode_specialchars($body, $ctype_param['charset']); |
|---|
| 926 | |
|---|
| 927 | return $body; |
|---|
| 928 | } |
|---|
| 929 | |
|---|
| 930 | |
|---|
| 931 | /* -------------------------------- |
|---|
| 932 | * private methods |
|---|
| 933 | * --------------------------------*/ |
|---|
| 934 | |
|---|
| 935 | |
|---|
| 936 | function _mod_mailbox($mbox, $mode='in') |
|---|
| 937 | { |
|---|
| 938 | if (!empty($this->root_dir) && $mode=='in') |
|---|
| 939 | $mbox = $this->root_dir.$this->delimiter.$mbox; |
|---|
| 940 | else if (strlen($this->root_dir) && $mode=='out') |
|---|
| 941 | $mbox = substr($mbox, strlen($this->root_dir)+1); |
|---|
| 942 | |
|---|
| 943 | return $mbox; |
|---|
| 944 | } |
|---|
| 945 | |
|---|
| 946 | |
|---|
| 947 | // sort mailboxes first by default folders and then in alphabethical order |
|---|
| 948 | function _sort_mailbox_list($a_folders) |
|---|
| 949 | { |
|---|
| 950 | $a_out = $a_defaults = array(); |
|---|
| 951 | |
|---|
| 952 | // find default folders and skip folders starting with '.' |
|---|
| 953 | foreach($a_folders as $i => $folder) |
|---|
| 954 | { |
|---|
| 955 | if ($folder{0}=='.') |
|---|
| 956 | continue; |
|---|
| 957 | |
|---|
| 958 | if (($p = array_search(strtolower($folder), $this->default_folders))!==FALSE) |
|---|
| 959 | $a_defaults[$p] = $folder; |
|---|
| 960 | else |
|---|
| 961 | $a_out[] = $folder; |
|---|
| 962 | } |
|---|
| 963 | |
|---|
| 964 | sort($a_out); |
|---|
| 965 | ksort($a_defaults); |
|---|
| 966 | |
|---|
| 967 | return array_merge($a_defaults, $a_out); |
|---|
| 968 | } |
|---|
| 969 | |
|---|
| 970 | |
|---|
| 971 | function _uid2id($uid, $mbox=NULL) |
|---|
| 972 | { |
|---|
| 973 | if (!$mbox) |
|---|
| 974 | $mbox = $this->mailbox; |
|---|
| 975 | |
|---|
| 976 | if (!isset($this->uid_id_map[$mbox][$uid])) |
|---|
| 977 | $this->uid_id_map[$mbox][$uid] = iil_C_UID2ID($this->conn, $mbox, $uid); |
|---|
| 978 | |
|---|
| 979 | return $this->uid_id_map[$mbox][$uid]; |
|---|
| 980 | } |
|---|
| 981 | |
|---|
| 982 | |
|---|
| 983 | // subscribe/unsubscribe a list of mailboxes and update local cache |
|---|
| 984 | function _change_subscription($a_mboxes, $mode) |
|---|
| 985 | { |
|---|
| 986 | $updated = FALSE; |
|---|
| 987 | |
|---|
| 988 | if (is_array($a_mboxes)) |
|---|
| 989 | foreach ($a_mboxes as $i => $mbox) |
|---|
| 990 | { |
|---|
| 991 | $mailbox = $this->_mod_mailbox($mbox); |
|---|
| 992 | $a_mboxes[$i] = $mailbox; |
|---|
| 993 | |
|---|
| 994 | if ($mode=='subscribe') |
|---|
| 995 | $result = iil_C_Subscribe($this->conn, $mailbox); |
|---|
| 996 | else if ($mode=='unsubscribe') |
|---|
| 997 | $result = iil_C_UnSubscribe($this->conn, $mailbox); |
|---|
| 998 | |
|---|
| 999 | if ($result>=0) |
|---|
| 1000 | $updated = TRUE; |
|---|
| 1001 | } |
|---|
| 1002 | |
|---|
| 1003 | // get cached mailbox list |
|---|
| 1004 | if ($updated) |
|---|
| 1005 | { |
|---|
| 1006 | $a_mailbox_cache = $this->get_cache('mailboxes'); |
|---|
| 1007 | if (!is_array($a_mailbox_cache)) |
|---|
| 1008 | return $updated; |
|---|
| 1009 | |
|---|
| 1010 | // modify cached list |
|---|
| 1011 | if ($mode=='subscribe') |
|---|
| 1012 | $a_mailbox_cache = array_merge($a_mailbox_cache, $a_mboxes); |
|---|
| 1013 | else if ($mode=='unsubscribe') |
|---|
| 1014 | $a_mailbox_cache = array_diff($a_mailbox_cache, $a_mboxes); |
|---|
| 1015 | |
|---|
| 1016 | // write mailboxlist to cache |
|---|
| 1017 | $this->update_cache('mailboxes', $this->_sort_mailbox_list($a_mailbox_cache)); |
|---|
| 1018 | } |
|---|
| 1019 | |
|---|
| 1020 | return $updated; |
|---|
| 1021 | } |
|---|
| 1022 | |
|---|
| 1023 | |
|---|
| 1024 | // increde/decrese messagecount for a specific mailbox |
|---|
| 1025 | function _set_messagecount($mbox, $mode, $increment) |
|---|
| 1026 | { |
|---|
| 1027 | $a_mailbox_cache = FALSE; |
|---|
| 1028 | $mailbox = $mbox ? $mbox : $this->mailbox; |
|---|
| 1029 | $mode = strtoupper($mode); |
|---|
| 1030 | |
|---|
| 1031 | $a_mailbox_cache = $this->get_cache('messagecount'); |
|---|
| 1032 | |
|---|
| 1033 | if (!is_array($a_mailbox_cache[$mailbox]) || !isset($a_mailbox_cache[$mailbox][$mode]) || !is_numeric($increment)) |
|---|
| 1034 | return FALSE; |
|---|
| 1035 | |
|---|
| 1036 | // add incremental value to messagecount |
|---|
| 1037 | $a_mailbox_cache[$mailbox][$mode] += $increment; |
|---|
| 1038 | |
|---|
| 1039 | // write back to cache |
|---|
| 1040 | $this->update_cache('messagecount', $a_mailbox_cache); |
|---|
| 1041 | |
|---|
| 1042 | return TRUE; |
|---|
| 1043 | } |
|---|
| 1044 | |
|---|
| 1045 | |
|---|
| 1046 | // remove messagecount of a specific mailbox from cache |
|---|
| 1047 | function _clear_messagecount($mbox='') |
|---|
| 1048 | { |
|---|
| 1049 | $a_mailbox_cache = FALSE; |
|---|
| 1050 | $mailbox = $mbox ? $mbox : $this->mailbox; |
|---|
| 1051 | |
|---|
| 1052 | $a_mailbox_cache = $this->get_cache('messagecount'); |
|---|
| 1053 | |
|---|
| 1054 | if (is_array($a_mailbox_cache[$mailbox])) |
|---|
| 1055 | { |
|---|
| 1056 | unset($a_mailbox_cache[$mailbox]); |
|---|
| 1057 | $this->update_cache('messagecount', $a_mailbox_cache); |
|---|
| 1058 | } |
|---|
| 1059 | } |
|---|
| 1060 | |
|---|
| 1061 | |
|---|
| 1062 | function _parse_address_list($str) |
|---|
| 1063 | { |
|---|
| 1064 | $a = $this->_explode_quoted_string(',', $str); |
|---|
| 1065 | $result = array(); |
|---|
| 1066 | |
|---|
| 1067 | foreach ($a as $key => $val) |
|---|
| 1068 | { |
|---|
| 1069 | $val = str_replace("\"<", "\" <", $val); |
|---|
| 1070 | $sub_a = $this->_explode_quoted_string(' ', $val); |
|---|
| 1071 | |
|---|
| 1072 | foreach ($sub_a as $k => $v) |
|---|
| 1073 | { |
|---|
| 1074 | if ((strpos($v, '@') > 0) && (strpos($v, '.') > 0)) |
|---|
| 1075 | $result[$key]['address'] = str_replace('<', '', str_replace('>', '', $v)); |
|---|
| 1076 | else |
|---|
| 1077 | $result[$key]['name'] .= (empty($result[$key]['name'])?'':' ').str_replace("\"",'',stripslashes($v)); |
|---|
| 1078 | } |
|---|
| 1079 | |
|---|
| 1080 | if (empty($result[$key]['name'])) |
|---|
| 1081 | $result[$key]['name'] = $result[$key]['address']; |
|---|
| 1082 | |
|---|
| 1083 | $result[$key]['name'] = $this->decode_header($result[$key]['name']); |
|---|
| 1084 | } |
|---|
| 1085 | |
|---|
| 1086 | return $result; |
|---|
| 1087 | } |
|---|
| 1088 | |
|---|
| 1089 | |
|---|
| 1090 | function _explode_quoted_string($delimiter, $string) |
|---|
| 1091 | { |
|---|
| 1092 | $quotes = explode("\"", $string); |
|---|
| 1093 | foreach ($quotes as $key => $val) |
|---|
| 1094 | if (($key % 2) == 1) |
|---|
| 1095 | $quotes[$key] = str_replace($delimiter, "_!@!_", $quotes[$key]); |
|---|
| 1096 | |
|---|
| 1097 | $string = implode("\"", $quotes); |
|---|
| 1098 | |
|---|
| 1099 | $result = explode($delimiter, $string); |
|---|
| 1100 | foreach ($result as $key => $val) |
|---|
| 1101 | $result[$key] = str_replace("_!@!_", $delimiter, $result[$key]); |
|---|
| 1102 | |
|---|
| 1103 | return $result; |
|---|
| 1104 | } |
|---|
| 1105 | } |
|---|
| 1106 | |
|---|
| 1107 | |
|---|
| 1108 | |
|---|
| 1109 | |
|---|
| 1110 | |
|---|
| 1111 | function quoted_printable_encode($input="", $line_max=76, $space_conv=false) |
|---|
| 1112 | { |
|---|
| 1113 | $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'); |
|---|
| 1114 | $lines = preg_split("/(?:\r\n|\r|\n)/", $input); |
|---|
| 1115 | $eol = "\r\n"; |
|---|
| 1116 | $escape = "="; |
|---|
| 1117 | $output = ""; |
|---|
| 1118 | |
|---|
| 1119 | while( list(, $line) = each($lines)) |
|---|
| 1120 | { |
|---|
| 1121 | //$line = rtrim($line); // remove trailing white space -> no =20\r\n necessary |
|---|
| 1122 | $linlen = strlen($line); |
|---|
| 1123 | $newline = ""; |
|---|
| 1124 | for($i = 0; $i < $linlen; $i++) |
|---|
| 1125 | { |
|---|
| 1126 | $c = substr( $line, $i, 1 ); |
|---|
| 1127 | $dec = ord( $c ); |
|---|
| 1128 | if ( ( $i == 0 ) && ( $dec == 46 ) ) // convert first point in the line into =2E |
|---|
| 1129 | { |
|---|
| 1130 | $c = "=2E"; |
|---|
| 1131 | } |
|---|
| 1132 | if ( $dec == 32 ) |
|---|
| 1133 | { |
|---|
| 1134 | if ( $i == ( $linlen - 1 ) ) // convert space at eol only |
|---|
| 1135 | { |
|---|
| 1136 | $c = "=20"; |
|---|
| 1137 | } |
|---|
| 1138 | else if ( $space_conv ) |
|---|
| 1139 | { |
|---|
| 1140 | $c = "=20"; |
|---|
| 1141 | } |
|---|
| 1142 | } |
|---|
| 1143 | else if ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) // always encode "\t", which is *not* required |
|---|
| 1144 | { |
|---|
| 1145 | $h2 = floor($dec/16); |
|---|
| 1146 | $h1 = floor($dec%16); |
|---|
| 1147 | $c = $escape.$hex["$h2"].$hex["$h1"]; |
|---|
| 1148 | } |
|---|
| 1149 | |
|---|
| 1150 | if ( (strlen($newline) + strlen($c)) >= $line_max ) // CRLF is not counted |
|---|
| 1151 | { |
|---|
| 1152 | $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay |
|---|
| 1153 | $newline = ""; |
|---|
| 1154 | // check if newline first character will be point or not |
|---|
| 1155 | if ( $dec == 46 ) |
|---|
| 1156 | { |
|---|
| 1157 | $c = "=2E"; |
|---|
| 1158 | } |
|---|
| 1159 | } |
|---|
| 1160 | $newline .= $c; |
|---|
| 1161 | } // end of for |
|---|
| 1162 | $output .= $newline.$eol; |
|---|
| 1163 | } // end of while |
|---|
| 1164 | |
|---|
| 1165 | return trim($output); |
|---|
| 1166 | } |
|---|
| 1167 | |
|---|
| 1168 | ?> |
|---|