| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * imap_mailbox.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * This impliments all functions that manipulate mailboxes |
| 10 | * |
| 11 | * $Id$ |
| 12 | */ |
| 13 | require_once('../functions/imap_utf7_encode_local.php'); |
| 14 | require_once('../functions/imap_utf7_decode_local.php'); |
| 15 | global $boxesnew; |
| 16 | |
| 17 | class mailboxes { |
| 18 | var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false, |
| 19 | $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false, |
| 20 | $is_trash = false, $is_draft = false, $mbxs = array(), |
| 21 | $unseen = false, $total = false; |
| 22 | |
| 23 | function addMbx($mbx, $delimiter, $start, $specialfirst) { |
| 24 | $ary = explode($delimiter, $mbx->mailboxname_full); |
| 25 | $mbx_parent = &$this; |
| 26 | for ($i=$start; $i < (count($ary) -1); $i++) { |
| 27 | $mbx_childs = &$mbx_parent->mbxs; |
| 28 | $found = false; |
| 29 | if ($mbx_childs) { |
| 30 | foreach ($mbx_childs as $key => $parent) { |
| 31 | if ($parent->mailboxname_sub == $ary[$i]) { |
| 32 | $mbx_parent = &$mbx_parent->mbxs[$key]; |
| 33 | $found = true; |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | if (!$found) { |
| 38 | $no_select_mbx = new mailboxes(); |
| 39 | if (isset($mbx_parent->mailboxname_full) && $mbx_parent->mailboxname_full != '') { |
| 40 | $no_select_mbx->mailboxname_full = $mbx_parent->mailboxname_full.$delimiter.$ary[$i]; |
| 41 | } else { |
| 42 | $no_select_mbx->mailboxname_full = $ary[$i]; |
| 43 | } |
| 44 | $no_select_mbx->mailboxname_sub = $ary[$i]; |
| 45 | $no_select_mbx->is_noselect = true; |
| 46 | $mbx_parent->mbxs[] = $no_select_mbx; |
| 47 | $i--; |
| 48 | } |
| 49 | |
| 50 | } |
| 51 | $mbx_parent->mbxs[] = $mbx; |
| 52 | if ($mbx->is_special && $specialfirst) { |
| 53 | usort($mbx_parent->mbxs, 'sortSpecialMbx'); |
| 54 | } |
| 55 | |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | function sortSpecialMbx($a, $b) { |
| 60 | if ($a->is_inbox) { |
| 61 | $acmp = '0'. $a->mailboxname_full; |
| 62 | } else if ($a->is_special) { |
| 63 | $acmp = '1'. $a->mailboxname_full; |
| 64 | } else { |
| 65 | $acmp = '2' . $a->mailboxname_full; |
| 66 | } |
| 67 | if ($b->is_inbox) { |
| 68 | $bcmp = '0'. $b->mailboxname_full; |
| 69 | }else if ($b->is_special) { |
| 70 | $bcmp = '1' . $b->mailboxname_full; |
| 71 | } else { |
| 72 | $bcmp = '2' . $b->mailboxname_full; |
| 73 | } |
| 74 | if ($acmp == $bcmp) return 0; |
| 75 | return ($acmp>$bcmp) ? 1: -1; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | function find_mailbox_name ($mailbox) { |
| 80 | if (ereg(" *\"([^\r\n\"]*)\"[ \r\n]*$", $mailbox, $regs)) |
| 81 | return $regs[1]; |
| 82 | ereg(" *([^ \r\n\"]*)[ \r\n]*$",$mailbox,$regs); |
| 83 | return $regs[1]; |
| 84 | |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * If $haystack is a full mailbox name, and $needle is the mailbox |
| 89 | * separator character, returns the second last part of the full |
| 90 | * mailbox name (i.e. the mailbox's parent mailbox) |
| 91 | */ |
| 92 | function readMailboxParent($haystack, $needle) { |
| 93 | |
| 94 | if ($needle == '') { |
| 95 | $ret = ''; |
| 96 | } else { |
| 97 | $parts = explode($needle, $haystack); |
| 98 | $elem = array_pop($parts); |
| 99 | while ($elem == '' && count($parts)) { |
| 100 | $elem = array_pop($parts); |
| 101 | } |
| 102 | $ret = join($needle, $parts); |
| 103 | } |
| 104 | return( $ret ); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | function isBoxBelow( $box2, $box1 ) { |
| 109 | global $delimiter, $folder_prefix, $imap_server_type; |
| 110 | |
| 111 | if ( $imap_server_type == 'uw' ) { |
| 112 | $boxs = $box2; |
| 113 | $i = strpos( $box1, $delimiter, strlen( $folder_prefix ) ); |
| 114 | if ( $i === false ) { |
| 115 | $i = strlen( $box2 ); |
| 116 | } |
| 117 | } else { |
| 118 | $boxs = $box2 . $delimiter; |
| 119 | /* Skip next second delimiter */ |
| 120 | $i = strpos( $box1, $delimiter ); |
| 121 | $i = strpos( $box1, $delimiter, $i + 1 ); |
| 122 | if ( $i === false ) { |
| 123 | $i = strlen( $box2 ); |
| 124 | } else { |
| 125 | $i++; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return ( substr( $box1, 0, $i ) == substr( $boxs, 0, $i ) ); |
| 130 | } |
| 131 | |
| 132 | /* Defines special mailboxes */ |
| 133 | function isSpecialMailbox( $box ) { |
| 134 | global $trash_folder, $sent_folder, $draft_folder, |
| 135 | $move_to_trash, $move_to_sent, $save_as_draft; |
| 136 | |
| 137 | $ret = ( (strtolower($box) == 'inbox') || |
| 138 | ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) || |
| 139 | ( $move_to_sent && isBoxBelow( $box, $sent_folder )) || |
| 140 | ($save_as_draft && $box == $draft_folder ) ); |
| 141 | |
| 142 | if ( !$ret ) { |
| 143 | $ret = do_hook_function( 'special_mailbox', $box ); |
| 144 | } |
| 145 | |
| 146 | return $ret; |
| 147 | } |
| 148 | |
| 149 | /* Expunges a mailbox */ |
| 150 | function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true) { |
| 151 | $read = sqimap_run_command($imap_stream, 'EXPUNGE', $handle_errors, |
| 152 | $response, $message); |
| 153 | } |
| 154 | |
| 155 | /* Checks whether or not the specified mailbox exists */ |
| 156 | function sqimap_mailbox_exists ($imap_stream, $mailbox) { |
| 157 | if (! isset($mailbox)) { |
| 158 | return false; |
| 159 | } |
| 160 | $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"", |
| 161 | true, $response, $message); |
| 162 | return isset($mbx[0]); |
| 163 | } |
| 164 | |
| 165 | /* Selects a mailbox */ |
| 166 | function sqimap_mailbox_select ($imap_stream, $mailbox) { |
| 167 | global $auto_expunge; |
| 168 | |
| 169 | if ( $mailbox == 'None' ) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"", |
| 174 | true, $response, $message); |
| 175 | $result = array(); |
| 176 | for ($i=0; $i<count($read); $i++) { |
| 177 | if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) { |
| 178 | $result[strtoupper($regs[1])] = $regs[2]; |
| 179 | } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) { |
| 180 | $result[strtoupper($regs[2])] = $regs[1]; |
| 181 | } else { |
| 182 | if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) { |
| 183 | $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ; |
| 184 | $result['PERMANENTFLAGS'] = $regs[1]; |
| 185 | } |
| 186 | else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) { |
| 187 | $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ; |
| 188 | $result['FLAGS'] = $regs[1]; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | if (preg_match('/^\[(.+)\]/',$message, $regs)) { |
| 193 | $result['RIGHTS']=$regs[1]; |
| 194 | } |
| 195 | |
| 196 | if ($auto_expunge) { |
| 197 | $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b); |
| 198 | } |
| 199 | return $result; |
| 200 | } |
| 201 | |
| 202 | /* Creates a folder */ |
| 203 | function sqimap_mailbox_create ($imap_stream, $mailbox, $type) { |
| 204 | global $delimiter; |
| 205 | if (strtolower($type) == 'noselect') { |
| 206 | $mailbox .= $delimiter; |
| 207 | } |
| 208 | $mailbox = imap_utf7_encode_local($mailbox); |
| 209 | $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"", |
| 210 | true, $response, $message); |
| 211 | sqimap_subscribe ($imap_stream, $mailbox); |
| 212 | } |
| 213 | |
| 214 | /* Subscribes to an existing folder */ |
| 215 | function sqimap_subscribe ($imap_stream, $mailbox) { |
| 216 | $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"", |
| 217 | true, $response, $message); |
| 218 | } |
| 219 | |
| 220 | /* Unsubscribes to an existing folder */ |
| 221 | function sqimap_unsubscribe ($imap_stream, $mailbox) { |
| 222 | global $imap_server_type; |
| 223 | $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"", |
| 224 | true, $response, $message); |
| 225 | } |
| 226 | |
| 227 | /* Deletes the given folder */ |
| 228 | function sqimap_mailbox_delete ($imap_stream, $mailbox) { |
| 229 | global $data_dir, $username; |
| 230 | $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"", |
| 231 | true, $response, $message); |
| 232 | sqimap_unsubscribe ($imap_stream, $mailbox); |
| 233 | do_hook_function("rename_or_delete_folder", $args = array($mailbox, 'delete', '')); |
| 234 | removePref($data_dir, $username, "thread_$mailbox"); |
| 235 | } |
| 236 | |
| 237 | /* Determines if the user is subscribed to the folder or not */ |
| 238 | function sqimap_mailbox_is_subscribed($imap_stream, $folder) { |
| 239 | $boxesall = sqimap_mailbox_list ($imap_stream); |
| 240 | foreach ($boxesall as $ref) { |
| 241 | if ($ref['unformatted'] == $folder) { |
| 242 | return true; |
| 243 | } |
| 244 | } |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | /* Renames a mailbox */ |
| 249 | function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) { |
| 250 | if ( $old_name != $new_name ) { |
| 251 | global $delimiter, $imap_server_type, $data_dir, $username; |
| 252 | if ( substr( $old_name, -1 ) == $delimiter ) { |
| 253 | $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 ); |
| 254 | $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 ); |
| 255 | $postfix = $delimiter; |
| 256 | } else { |
| 257 | $postfix = ''; |
| 258 | } |
| 259 | $boxesall = sqimap_mailbox_list($imap_stream); |
| 260 | $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"'; |
| 261 | $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message); |
| 262 | sqimap_unsubscribe($imap_stream, $old_name.$postfix); |
| 263 | $oldpref = getPref($data_dir, $username, "thread_".$old_name.$postfix); |
| 264 | removePref($data_dir, $username, "thread_".$old_name.$postfix); |
| 265 | sqimap_subscribe($imap_stream, $new_name.$postfix); |
| 266 | setPref($data_dir, $username, "thread_".$new_name.$postfix, $oldpref); |
| 267 | do_hook_function("rename_or_delete_folder",$args = array($old_name, 'rename', $new_name)); |
| 268 | $l = strlen( $old_name ) + 1; |
| 269 | $p = 'unformatted'; |
| 270 | foreach ( $boxesall as $box ) { |
| 271 | if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) { |
| 272 | $new_sub = $new_name . $delimiter . substr($box[$p], $l); |
| 273 | if ($imap_server_type == 'cyrus') { |
| 274 | $cmd = 'RENAME "' . quoteIMAP($box[$p]) . '" "' . quoteIMAP($new_sub) . '"'; |
| 275 | $data = sqimap_run_command($imap_stream, $cmd, true, |
| 276 | $response, $message); |
| 277 | } |
| 278 | sqimap_unsubscribe($imap_stream, $box[$p]); |
| 279 | $oldpref = getPref($data_dir, $username, "thread_".$box[$p]); |
| 280 | removePref($data_dir, $username, "thread_".$box[$p]); |
| 281 | sqimap_subscribe($imap_stream, $new_sub); |
| 282 | setPref($data_dir, $username, "thread_".$new_sub, $oldpref); |
| 283 | do_hook_function("rename_or_delete_folder", |
| 284 | $args = array($box[$p], 'rename', $new_sub)); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * Formats a mailbox into 4 parts for the $boxesall array |
| 292 | * |
| 293 | * The four parts are: |
| 294 | * |
| 295 | * raw - Raw LIST/LSUB response from the IMAP server |
| 296 | * formatted - nicely formatted folder name |
| 297 | * unformatted - unformatted, but with delimiter at end removed |
| 298 | * unformatted-dm - folder name as it appears in raw response |
| 299 | * unformatted-disp - unformatted without $folder_prefix |
| 300 | */ |
| 301 | function sqimap_mailbox_parse ($line, $line_lsub) { |
| 302 | global $folder_prefix, $delimiter; |
| 303 | |
| 304 | /* Process each folder line */ |
| 305 | for ($g=0; $g < count($line); $g++) { |
| 306 | |
| 307 | /* Store the raw IMAP reply */ |
| 308 | if (isset($line[$g])) { |
| 309 | $boxesall[$g]["raw"] = $line[$g]; |
| 310 | } |
| 311 | else { |
| 312 | $boxesall[$g]["raw"] = ''; |
| 313 | } |
| 314 | |
| 315 | |
| 316 | /* Count number of delimiters ($delimiter) in folder name */ |
| 317 | $mailbox = trim($line_lsub[$g]); |
| 318 | $dm_count = substr_count($mailbox, $delimiter); |
| 319 | if (substr($mailbox, -1) == $delimiter) { |
| 320 | /* If name ends in delimiter, decrement count by one */ |
| 321 | $dm_count--; |
| 322 | } |
| 323 | |
| 324 | /* Format folder name, but only if it's a INBOX.* or has a parent. */ |
| 325 | $boxesallbyname[$mailbox] = $g; |
| 326 | $parentfolder = readMailboxParent($mailbox, $delimiter); |
| 327 | if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") || |
| 328 | (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) || |
| 329 | ( isset($boxesallbyname[$parentfolder]) && |
| 330 | (strlen($parentfolder) > 0) ) ) { |
| 331 | $indent = $dm_count - ( substr_count($folder_prefix, $delimiter)); |
| 332 | if ($indent > 0) { |
| 333 | $boxesall[$g]['formatted'] = str_repeat(' ', $indent); |
| 334 | } |
| 335 | else { |
| 336 | $boxesall[$g]['formatted'] = ''; |
| 337 | } |
| 338 | $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter)); |
| 339 | } |
| 340 | else { |
| 341 | $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox); |
| 342 | } |
| 343 | |
| 344 | $boxesall[$g]['unformatted-dm'] = $mailbox; |
| 345 | if (substr($mailbox, -1) == $delimiter) { |
| 346 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
| 347 | } |
| 348 | $boxesall[$g]['unformatted'] = $mailbox; |
| 349 | if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) { |
| 350 | $mailbox = substr($mailbox, strlen($folder_prefix)); |
| 351 | } |
| 352 | $boxesall[$g]['unformatted-disp'] = $mailbox; |
| 353 | $boxesall[$g]['id'] = $g; |
| 354 | |
| 355 | $boxesall[$g]['flags'] = array(); |
| 356 | if (isset($line[$g])) { |
| 357 | ereg("\(([^)]*)\)",$line[$g],$regs); |
| 358 | $flags = trim(strtolower(str_replace('\\', '',$regs[1]))); |
| 359 | if ($flags) { |
| 360 | $boxesall[$g]['flags'] = explode(' ', $flags); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | return $boxesall; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Sorting function used to sort mailbox names. |
| 370 | * + Original patch from dave_michmerhuizen@yahoo.com |
| 371 | * + Allows case insensitivity when sorting folders |
| 372 | * + Takes care of the delimiter being sorted to the end, causing |
| 373 | * subfolders to be listed in below folders that are prefixed |
| 374 | * with their parent folders name. |
| 375 | * |
| 376 | * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar |
| 377 | * Without special sort function: foobar between foo and foo.bar |
| 378 | * With special sort function: foobar AFTER foo and foo.bar :) |
| 379 | */ |
| 380 | function user_strcasecmp($a, $b) { |
| 381 | global $delimiter; |
| 382 | |
| 383 | /* Calculate the length of some strings. */ |
| 384 | $a_length = strlen($a); |
| 385 | $b_length = strlen($b); |
| 386 | $min_length = min($a_length, $b_length); |
| 387 | $delimiter_length = strlen($delimiter); |
| 388 | |
| 389 | /* Set the initial result value. */ |
| 390 | $result = 0; |
| 391 | |
| 392 | /* Check the strings... */ |
| 393 | for ($c = 0; $c < $min_length; ++$c) { |
| 394 | $a_del = substr($a, $c, $delimiter_length); |
| 395 | $b_del = substr($b, $c, $delimiter_length); |
| 396 | |
| 397 | if (($a_del == $delimiter) && ($b_del == $delimiter)) { |
| 398 | $result = 0; |
| 399 | } else if (($a_del == $delimiter) && ($b_del != $delimiter)) { |
| 400 | $result = -1; |
| 401 | } else if (($a_del != $delimiter) && ($b_del == $delimiter)) { |
| 402 | $result = 1; |
| 403 | } else { |
| 404 | $result = strcasecmp($a{$c}, $b{$c}); |
| 405 | } |
| 406 | |
| 407 | if ($result != 0) { |
| 408 | break; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /* If one string is a prefix of the other... */ |
| 413 | if ($result == 0) { |
| 414 | if ($a_length < $b_length) { |
| 415 | $result = -1; |
| 416 | } else if ($a_length > $b_length) { |
| 417 | $result = 1; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | return $result; |
| 422 | } |
| 423 | |
| 424 | |
| 425 | /* |
| 426 | * Returns sorted mailbox lists in several different ways. |
| 427 | * See comment on sqimap_mailbox_parse() for info about the returned array. |
| 428 | */ |
| 429 | function sqimap_mailbox_list($imap_stream) { |
| 430 | global $default_folder_prefix; |
| 431 | |
| 432 | if ( !isset( $boxesnew ) ) { |
| 433 | |
| 434 | global $data_dir, $username, $list_special_folders_first, |
| 435 | $folder_prefix, $trash_folder, $sent_folder, $draft_folder, |
| 436 | $move_to_trash, $move_to_sent, $save_as_draft, |
| 437 | $delimiter, $noselect_fix_enable; |
| 438 | |
| 439 | $inbox_in_list = false; |
| 440 | $inbox_subscribed = false; |
| 441 | |
| 442 | require_once('../src/load_prefs.php'); |
| 443 | require_once('../functions/array.php'); |
| 444 | |
| 445 | if ($noselect_fix_enable) { |
| 446 | $lsub_args = "LSUB \"$folder_prefix\" \"*%\""; |
| 447 | } |
| 448 | else { |
| 449 | $lsub_args = "LSUB \"$folder_prefix\" \"*\""; |
| 450 | } |
| 451 | /* LSUB array */ |
| 452 | $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args, |
| 453 | true, $response, $message); |
| 454 | |
| 455 | |
| 456 | /* |
| 457 | * Section about removing the last element was removed |
| 458 | * We don't return "* OK" anymore from sqimap_read_data |
| 459 | */ |
| 460 | |
| 461 | $sorted_lsub_ary = array(); |
| 462 | for ($i=0;$i < count($lsub_ary); $i++) { |
| 463 | /* |
| 464 | * Workaround for EIMS |
| 465 | * Doesn't work if the mailbox name is multiple lines |
| 466 | */ |
| 467 | if (isset($lsub_ary[$i + 1]) && |
| 468 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
| 469 | $lsub_ary[$i], $regs)) { |
| 470 | $i ++; |
| 471 | $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2]; |
| 472 | } |
| 473 | $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]); |
| 474 | $sorted_lsub_ary[] = $temp_mailbox_name; |
| 475 | if (strtoupper($temp_mailbox_name) == 'INBOX') { |
| 476 | $inbox_subscribed = true; |
| 477 | } |
| 478 | } |
| 479 | $new_ary = array(); |
| 480 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
| 481 | if (!in_array($sorted_lsub_ary[$i], $new_ary)) { |
| 482 | $new_ary[] = $sorted_lsub_ary[$i]; |
| 483 | } |
| 484 | } |
| 485 | $sorted_lsub_ary = $new_ary; |
| 486 | if (isset($sorted_lsub_ary)) { |
| 487 | usort($sorted_lsub_ary, 'user_strcasecmp'); |
| 488 | } |
| 489 | |
| 490 | /* LIST array */ |
| 491 | $sorted_list_ary = array(); |
| 492 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
| 493 | if (substr($sorted_lsub_ary[$i], -1) == $delimiter) { |
| 494 | $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1); |
| 495 | } |
| 496 | else { |
| 497 | $mbx = $sorted_lsub_ary[$i]; |
| 498 | } |
| 499 | |
| 500 | $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"", |
| 501 | true, $response, $message); |
| 502 | /* Another workaround for EIMS */ |
| 503 | if (isset($read[1]) && |
| 504 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
| 505 | $read[0], $regs)) { |
| 506 | $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) . '"' . $regs[2]; |
| 507 | } |
| 508 | |
| 509 | if (isset($sorted_list_ary[$i])) { |
| 510 | $sorted_list_ary[$i] = ''; |
| 511 | } |
| 512 | |
| 513 | if (isset($read[0])) { |
| 514 | $sorted_list_ary[$i] = $read[0]; |
| 515 | } |
| 516 | else { |
| 517 | $sorted_list_ary[$i] = ''; |
| 518 | } |
| 519 | |
| 520 | if (isset($sorted_list_ary[$i]) && |
| 521 | strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') { |
| 522 | $inbox_in_list = true; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Just in case they're not subscribed to their inbox, |
| 528 | * we'll get it for them anyway |
| 529 | */ |
| 530 | if (!$inbox_subscribed || !$inbox_in_list) { |
| 531 | $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"", |
| 532 | true, $response, $message); |
| 533 | /* Another workaround for EIMS */ |
| 534 | if (isset($inbox_ary[1]) && |
| 535 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
| 536 | $inbox_ary[0], $regs)) { |
| 537 | $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) . |
| 538 | '"' . $regs[2]; |
| 539 | } |
| 540 | |
| 541 | $sorted_list_ary[] = $inbox_ary[0]; |
| 542 | $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]); |
| 543 | } |
| 544 | |
| 545 | $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary); |
| 546 | |
| 547 | /* Now, lets sort for special folders */ |
| 548 | $boxesnew = $used = array(); |
| 549 | |
| 550 | /* Find INBOX */ |
| 551 | foreach ( $boxesall as $k => $box ) { |
| 552 | if ( strtolower($box['unformatted']) == 'inbox') { |
| 553 | $boxesnew[] = $box; |
| 554 | $used[$k] = true; |
| 555 | } else { |
| 556 | $used[$k] = false; |
| 557 | } |
| 558 | } |
| 559 | /* List special folders and their subfolders, if requested. */ |
| 560 | if ($list_special_folders_first) { |
| 561 | foreach ( $boxesall as $k => $box ) { |
| 562 | if ( !$used[$k] && isSpecialMailbox( $box['unformatted'] ) ) { |
| 563 | $boxesnew[] = $box; |
| 564 | $used[$k] = true; |
| 565 | } |
| 566 | $spec_sub = str_replace(' ', '', $box['formatted']); |
| 567 | $spec_sub = str_replace('*', '', $spec_sub); |
| 568 | $spec_sub = str_replace('?', '\?', $spec_sub); |
| 569 | |
| 570 | /* In case of problems with preg |
| 571 | here is a ereg version |
| 572 | if (!$used[$k] && ereg("^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$", $box['unformatted']) ) { */ |
| 573 | |
| 574 | if (!$used[$k] && preg_match("?^$default_folder_prefix(Sent|Drafts|Trash).{1}$spec_sub$?", $box['unformatted']) ) { |
| 575 | $boxesnew[] = $box; |
| 576 | $used[$k] = true; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | } |
| 581 | /* Rest of the folders */ |
| 582 | foreach ( $boxesall as $k => $box ) { |
| 583 | if ( !$used[$k] ) { |
| 584 | $boxesnew[] = $box; |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | return $boxesnew; |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Returns a list of all folders, subscribed or not |
| 593 | */ |
| 594 | function sqimap_mailbox_list_all($imap_stream) { |
| 595 | global $list_special_folders_first, $folder_prefix, $delimiter; |
| 596 | |
| 597 | require_once('../functions/array.php'); |
| 598 | |
| 599 | $ssid = sqimap_session_id(); |
| 600 | $lsid = strlen( $ssid ); |
| 601 | fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n"); |
| 602 | $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message); |
| 603 | $g = 0; |
| 604 | $phase = 'inbox'; |
| 605 | |
| 606 | for ($i = 0; $i < count($read_ary); $i++) { |
| 607 | /* Another workaround for EIMS */ |
| 608 | if (isset($read_ary[$i + 1]) && |
| 609 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
| 610 | $read_ary[$i], $regs)) { |
| 611 | $i ++; |
| 612 | $read_ary[$i] = $regs[1] . '"' . addslashes(trim($read_ary[$i])) . '"' . $regs[2]; |
| 613 | } |
| 614 | if (substr($read_ary[$i], 0, $lsid) != $ssid ) { |
| 615 | |
| 616 | /* Store the raw IMAP reply */ |
| 617 | $boxes[$g]['raw'] = $read_ary[$i]; |
| 618 | |
| 619 | /* Count number of delimiters ($delimiter) in folder name */ |
| 620 | $mailbox = find_mailbox_name($read_ary[$i]); |
| 621 | $dm_count = substr_count($mailbox, $delimiter); |
| 622 | if (substr($mailbox, -1) == $delimiter) { |
| 623 | /* If name ends in delimiter - decrement count by one */ |
| 624 | $dm_count--; |
| 625 | } |
| 626 | |
| 627 | /* Format folder name, but only if it's a INBOX.* or has a parent. */ |
| 628 | $boxesallbyname[$mailbox] = $g; |
| 629 | $parentfolder = readMailboxParent($mailbox, $delimiter); |
| 630 | if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) || |
| 631 | (ereg('^'.$folder_prefix, $mailbox)) || |
| 632 | ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) { |
| 633 | if ($dm_count) { |
| 634 | $boxes[$g]['formatted'] = str_repeat(' ', $dm_count); |
| 635 | } |
| 636 | else { |
| 637 | $boxes[$g]['formatted'] = ''; |
| 638 | } |
| 639 | $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter)); |
| 640 | } |
| 641 | else { |
| 642 | $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox); |
| 643 | } |
| 644 | |
| 645 | $boxes[$g]['unformatted-dm'] = $mailbox; |
| 646 | if (substr($mailbox, -1) == $delimiter) { |
| 647 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
| 648 | } |
| 649 | $boxes[$g]['unformatted'] = $mailbox; |
| 650 | $boxes[$g]['unformatted-disp'] = ereg_replace('^' . $folder_prefix, '', $mailbox); |
| 651 | $boxes[$g]['id'] = $g; |
| 652 | |
| 653 | /* Now lets get the flags for this mailbox */ |
| 654 | $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"", |
| 655 | true, $response, $message); |
| 656 | |
| 657 | /* Another workaround for EIMS */ |
| 658 | if (isset($read_mlbx[1]) && |
| 659 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", $read_mlbx[0], $regs)) { |
| 660 | $read_mlbx[0] = $regs[1] . '"' . addslashes(trim($read_mlbx[1])) . '"' . $regs[2]; |
| 661 | } |
| 662 | |
| 663 | $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+1); |
| 664 | $flags = substr($flags, 0, strpos($flags, ')')); |
| 665 | $flags = str_replace('\\', '', $flags); |
| 666 | $flags = trim(strtolower($flags)); |
| 667 | if ($flags) { |
| 668 | $boxes[$g]['flags'] = explode(' ', $flags); |
| 669 | } else { |
| 670 | $boxes[$g]['flags'] = array(); |
| 671 | } |
| 672 | } |
| 673 | $g++; |
| 674 | } |
| 675 | if(is_array($boxes)) { |
| 676 | $boxes = ary_sort ($boxes, 'unformatted', 1); |
| 677 | } |
| 678 | |
| 679 | return $boxes; |
| 680 | } |
| 681 | |
| 682 | function sqimap_mailbox_tree($imap_stream) { |
| 683 | global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type; |
| 684 | if ( !isset( $boxesnew ) ) { |
| 685 | |
| 686 | global $data_dir, $username, $list_special_folders_first, |
| 687 | $folder_prefix, $delimiter, $trash_folder, $move_to_trash; |
| 688 | |
| 689 | |
| 690 | $inbox_in_list = false; |
| 691 | $inbox_subscribed = false; |
| 692 | |
| 693 | require_once('../src/load_prefs.php'); |
| 694 | require_once('../functions/array.php'); |
| 695 | |
| 696 | /* LSUB array */ |
| 697 | $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"", |
| 698 | true, $response, $message); |
| 699 | |
| 700 | /* |
| 701 | * Section about removing the last element was removed |
| 702 | * We don't return "* OK" anymore from sqimap_read_data |
| 703 | */ |
| 704 | $sorted_lsub_ary = array(); |
| 705 | $cnt = count($lsub_ary); |
| 706 | for ($i=0;$i < $cnt; $i++) { |
| 707 | /* |
| 708 | * Workaround for EIMS |
| 709 | * Doesn't work if the mailbox name is multiple lines |
| 710 | */ |
| 711 | if (isset($lsub_ary[$i + 1]) && |
| 712 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
| 713 | $lsub_ary[$i], $regs)) { |
| 714 | $i ++; |
| 715 | $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) . '"' . $regs[2]; |
| 716 | } |
| 717 | |
| 718 | // if (preg_match("/^\*\s+LSUB\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$lsub_ary[$i],$regs)) { |
| 719 | // $flag = $regs[1]; |
| 720 | // $mbx = trim($regs[3]); |
| 721 | // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag); |
| 722 | // } |
| 723 | $mbx = find_mailbox_name($lsub_ary[$i]); |
| 724 | if (substr($mbx, -1) == $delimiter) { |
| 725 | $mbx = substr($mbx, 0, strlen($mbx) - 1); |
| 726 | } |
| 727 | $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => ''); |
| 728 | } |
| 729 | array_multisort($sorted_lsub_ary, SORT_ASC, SORT_REGULAR); |
| 730 | |
| 731 | foreach ($sorted_lsub_ary as $mbx) { |
| 732 | if ($mbx['mbx'] == 'INBOX') { |
| 733 | $inbox_in_list = true; |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Just in case they're not subscribed to their inbox, |
| 740 | * we'll get it for them anyway |
| 741 | */ |
| 742 | if (!$inbox_in_list) { |
| 743 | $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"", |
| 744 | true, $response, $message); |
| 745 | /* Another workaround for EIMS */ |
| 746 | if (isset($inbox_ary[1]) && |
| 747 | ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$", |
| 748 | $inbox_ary[0], $regs)) { |
| 749 | $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) . |
| 750 | '"' . $regs[2]; |
| 751 | } |
| 752 | $mbx = find_mailbox_name($inbox_ary[0]); |
| 753 | if (substr($mbx, -1) == $delimiter) { |
| 754 | $mbx = substr($mbx, 0, strlen($mbx) - 1); |
| 755 | } |
| 756 | if ( $mbx == 'INBOX') { |
| 757 | $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => ''); |
| 758 | sqimap_subscribe($imap_stream, 'INBOX'); |
| 759 | } |
| 760 | |
| 761 | // if (preg_match("/^\*\s+LIST\s+\((.*)\)\s+\"(.*)\"\s+\"?(.+(?=\")|.+).*$/",$inbox_ary[0],$regs)) { |
| 762 | // $flag = $regs[1]; |
| 763 | // $mbx = trim($regs[3]); |
| 764 | // if (substr($mbx, -1) == $delimiter) { |
| 765 | // $mbx = substr($mbx, 0, strlen($mbx) - 1); |
| 766 | // } |
| 767 | // $sorted_lsub_ary[] = array ('mbx' => $mbx, 'flag' => $flag); |
| 768 | // } |
| 769 | } |
| 770 | $cnt = count($sorted_lsub_ary); |
| 771 | for ($i=0 ; $i < $cnt; $i++) { |
| 772 | $mbx = $sorted_lsub_ary[$i]['mbx']; |
| 773 | if (($unseen_notify == 2 && $mbx == 'INBOX') |
| 774 | || $unseen_notify == 3 |
| 775 | || ($move_to_trash && ($mbx == $trash_folder))) { |
| 776 | $sorted_lsub_ary[$i]['unseen'] = sqimap_unseen_messages($imap_stream, $mbx); |
| 777 | if ($unseen_type == 2 || ($move_to_trash |
| 778 | && ($mbx == $trash_folder) )) { |
| 779 | $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx); |
| 780 | } |
| 781 | if ($mbx == $trash_folder) { |
| 782 | $sorted_lsub_ary[$i]['nummessages'] = sqimap_get_num_messages($imap_stream, $mbx); |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary); |
| 787 | return $boxesnew; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | |
| 792 | function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false) { |
| 793 | global $data_dir, $username, $list_special_folders_first, |
| 794 | $folder_prefix, $trash_folder, $sent_folder, $draft_folder, |
| 795 | $move_to_trash, $move_to_sent, $save_as_draft, |
| 796 | $delimiter; |
| 797 | |
| 798 | $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder); |
| 799 | |
| 800 | /* create virtual root node */ |
| 801 | $mailboxes= new mailboxes(); |
| 802 | $mailboxes->is_root = true; |
| 803 | $trail_del = false; |
| 804 | if (isset($folder_prefix) && $folder_prefix != '') { |
| 805 | $start = substr_count($folder_prefix,$delimiter); |
| 806 | if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) { |
| 807 | $trail_del = true; |
| 808 | $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1)); |
| 809 | } else { |
| 810 | $mailboxes->mailboxname_full = $folder_prefix; |
| 811 | $start++; |
| 812 | } |
| 813 | $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full; |
| 814 | } else $start = 0; |
| 815 | $cnt = count($mbx_ary); |
| 816 | for ($i=0; $i < $cnt; $i++) { |
| 817 | if ($mbx_ary[$i]['mbx'] !='' ) { |
| 818 | $mbx = new mailboxes(); |
| 819 | $mailbox = $mbx_ary[$i]['mbx']; |
| 820 | switch ($mailbox) { |
| 821 | case 'INBOX': |
| 822 | $mbx->is_inbox = true; |
| 823 | $mbx->is_special = true; |
| 824 | break; |
| 825 | case $trash_folder: |
| 826 | $mbx->is_trash = true; |
| 827 | $mbx->is_special = true; |
| 828 | break; |
| 829 | case $sent_folder: |
| 830 | $mbx->is_sent = true; |
| 831 | $mbx->is_special = true; |
| 832 | break; |
| 833 | case $draft_folder: |
| 834 | $mbx->is_draft = true; |
| 835 | $mbx->is_special = true; |
| 836 | break; |
| 837 | } |
| 838 | |
| 839 | if (isset($mbx_ary[$i]['unseen'])) { |
| 840 | $mbx->unseen = $mbx_ary[$i]['unseen']; |
| 841 | } |
| 842 | if (isset($mbx_ary[$i]['nummessages'])) { |
| 843 | $mbx->total = $mbx_ary[$i]['nummessages']; |
| 844 | } |
| 845 | |
| 846 | $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter); |
| 847 | if ($r_del_pos) { |
| 848 | $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1); |
| 849 | } else { /* mailbox is root folder */ |
| 850 | $mbx->mailboxname_sub = $mbx_ary[$i]['mbx']; |
| 851 | } |
| 852 | $mbx->mailboxname_full = $mbx_ary[$i]['mbx']; |
| 853 | $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | return $mailboxes; |
| 858 | } |
| 859 | |
| 860 | |
| 861 | ?> |