6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This impliments all functions that manipulate mailboxes
14 function isBoxBelow( $box2, $box1 ) {
16 global $delimiter, $folder_prefix, $imap_server_type;
18 if ( $imap_server_type == 'uw' ) {
20 $i = strpos( $box1, $delimiter, strlen( $folder_prefix ) );
25 $boxs = $box2 . $delimiter;
26 // Skip next second delimiter
27 $i = strpos( $box1, $delimiter );
28 $i = strpos( $box1, $delimiter, $i +
1 );
36 return( substr( $box1, 0, $i ) == substr( $boxs, 0, $i ) );
41 Defines Special Mail Boxes
43 function isSpecialMailbox( $box ) {
45 global $trash_folder, $sent_folder, $draft_folder,
46 $move_to_trash, $move_to_sent, $save_as_draft,
47 $delimiter, $folder_prefix, $imap_server_type;
49 $ret = ( (strtolower($box) == 'inbox') ||
50 ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) ||
51 ( $move_to_sent && isBoxBelow( $box, $sent_folder )) ||
52 ($save_as_draft && $box == $draft_folder ) );
58 /*************************
59 ** Expunges a mailbox **
60 *************************/
61 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = TRUE)
63 $read = sqimap_run_command($imap_stream, 'EXPUNGE',
64 $handle_errors, $response, $message);
68 /******************************************************************************
69 ** Checks whether or not the specified mailbox exists
70 ******************************************************************************/
71 function sqimap_mailbox_exists ($imap_stream, $mailbox)
73 if (! isset($mailbox)) {
76 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
77 TRUE, $response, $message);
78 return isset($mbx[0]);
81 /******************************************************************************
83 ******************************************************************************/
84 function sqimap_mailbox_select ($imap_stream, $mailbox,
85 $hide=TRUE, $recent=false)
89 if ( $mailbox == 'None' ) {
93 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
94 TRUE, $response, $message);
96 for ($i=0; $i<count($read); $i++
) {
97 if (strpos(strtolower($read[$i]), 'recent')) {
98 $r = explode(' ', $read[$i]);
104 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE',
111 /******************************************************************************
113 ******************************************************************************/
114 function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
117 if (strtolower($type) == 'noselect') {
118 $mailbox = $mailbox.$delimiter;
120 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
121 TRUE, $response, $message);
123 sqimap_subscribe ($imap_stream, $mailbox);
128 /******************************************************************************
129 ** Subscribes to an existing folder
130 ******************************************************************************/
131 function sqimap_subscribe ($imap_stream, $mailbox)
133 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
134 TRUE, $response, $message);
139 /******************************************************************************
140 ** Unsubscribes to an existing folder
141 ******************************************************************************/
142 function sqimap_unsubscribe ($imap_stream, $mailbox)
144 global $imap_server_type;
146 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
147 TRUE, $response, $message);
152 /******************************************************************************
153 ** This function simply deletes the given folder
154 ******************************************************************************/
155 function sqimap_mailbox_delete ($imap_stream, $mailbox)
157 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
158 TRUE, $response, $message);
159 sqimap_unsubscribe ($imap_stream, $mailbox);
162 /***********************************************************************
163 ** Determines if the user is subscribed to the folder or not
164 **********************************************************************/
165 function sqimap_mailbox_is_subscribed($imap_stream, $folder)
167 $boxes = sqimap_mailbox_list ($imap_stream);
168 foreach ($boxes as $ref) {
169 if ($ref['unformatted'] == $folder) {
179 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
181 if ( $old_name <> $new_name ) {
185 if ( substr( $old_name, -1 ) == $delimiter ) {
186 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
187 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
188 $postfix = $delimiter;
189 $boxes = sqimap_mailbox_list($imap_stream);
195 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
196 $data = sqimap_run_command($imap_stream, $cmd,
197 TRUE, $response, $message);
198 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
199 sqimap_subscribe($imap_stream, $new_name.$postfix);
202 // Sub-unsub subfolders
203 $l = strlen( $old_name ) +
1;
205 foreach ( $boxes as $box ) {
206 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
207 sqimap_unsubscribe($imap_stream, $box[$p]);
208 sqimap_subscribe($imap_stream,
209 $new_name . $delimiter . substr( $box[$p], $l ) );
218 /******************************************************************************
219 ** Formats a mailbox into 4 parts for the $boxes array
221 ** The four parts are:
223 ** raw - Raw LIST/LSUB response from the IMAP server
224 ** formatted - nicely formatted folder name
225 ** unformatted - unformatted, but with delimiter at end removed
226 ** unformatted-dm - folder name as it appears in raw response
227 ** unformatted-disp - unformatted without $folder_prefix
229 ******************************************************************************/
230 function sqimap_mailbox_parse ($line, $line_lsub)
232 global $folder_prefix, $delimiter;
234 /* Process each folder line */
235 for ($g=0; $g < count($line); $g++
) {
237 /* Store the raw IMAP reply */
238 if (isset($line[$g])) {
239 $boxes[$g]["raw"] = $line[$g];
242 $boxes[$g]["raw"] = "";
246 /* Count number of delimiters ($delimiter) in folder name */
247 $mailbox = trim($line_lsub[$g]);
248 $dm_count = substr_count($mailbox, $delimiter);
249 if (substr($mailbox, -1) == $delimiter) {
250 /* If name ends in delimiter - decrement count by one */
254 /* Format folder name, but only if it's a INBOX.* or have */
256 $boxesbyname[$mailbox] = $g;
257 $parentfolder = readMailboxParent($mailbox, $delimiter);
258 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
259 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
260 ( isset($boxesbyname[$parentfolder]) &&
261 (strlen($parentfolder) > 0) ) ) {
262 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
264 $boxes[$g]['formatted'] = str_repeat(" ", $indent);
267 $boxes[$g]['formatted'] = '';
269 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
272 $boxes[$g]['formatted'] = $mailbox;
275 $boxes[$g]['unformatted-dm'] = $mailbox;
276 if (substr($mailbox, -1) == $delimiter) {
277 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
279 $boxes[$g]['unformatted'] = $mailbox;
280 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
281 $mailbox = substr($mailbox, strlen($folder_prefix));
283 $boxes[$g]['unformatted-disp'] = $mailbox;
284 $boxes[$g]['id'] = $g;
286 $boxes[$g]['flags'] = array();
287 if (isset($line[$g])) {
288 ereg("\(([^)]*)\)",$line[$g],$regs);
289 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
291 $boxes[$g]['flags'] = explode(' ', $flags);
300 * Sorting function used to sort mailbox names.
301 * + Original patch from dave_michmerhuizen@yahoo.com
302 * + Allows case insensitivity when sorting folders
303 * + Takes care of the delimiter being sorted to the end, causing
304 * subfolders to be listed in below folders that are prefixed
305 * with their parent folders name.
306 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
307 * Without special sort function: foobar between foo and foo.bar
308 * With special sort function: foobar AFTER foo and foo.bar :)
310 function user_strcasecmp($a, $b) {
313 /* Calculate the length of some strings. */
314 $a_length = strlen($a);
315 $b_length = strlen($b);
316 $min_length = min($a_length, $b_length);
317 $delimiter_length = strlen($delimiter);
319 /* Set the initial result value. */
322 /* Check the strings... */
323 for ($c = 0; $c < $min_length; ++
$c) {
324 $a_del = substr($a, $c, $delimiter_length);
325 $b_del = substr($b, $c, $delimiter_length);
327 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
329 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
331 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
334 $result = strcasecmp($a{$c}, $b{$c});
342 /* If one string is a prefix of the other... */
344 if ($a_length < $b_length) {
346 } else if ($a_length > $b_length) {
355 /******************************************************************************
356 ** Returns sorted mailbox lists in several different ways.
357 ** See comment on sqimap_mailbox_parse() for info about the returned array.
358 ******************************************************************************/
359 function sqimap_mailbox_list ($imap_stream) {
360 global $data_dir, $username, $list_special_folders_first,
361 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
362 $move_to_trash, $move_to_sent, $save_as_draft,
365 $inbox_in_list = false;
366 $inbox_subscribed = false;
368 require_once('../src/load_prefs.php');
369 require_once('../functions/array.php');
372 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
373 TRUE, $response, $message);
375 /* Section about removing the last element was removed */
376 /* We don't return "* OK" anymore from sqimap_read_data */
378 $sorted_lsub_ary = array();
379 for ($i=0;$i < count($lsub_ary); $i++
) {
380 /* Workaround for EIMS */
381 /* Doesn't work if the mailbox name is multiple lines */
382 if (isset($lsub_ary[$i +
1]) &&
383 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
384 $lsub_ary[$i], $regs)) {
386 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
389 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
390 $sorted_lsub_ary[] = $temp_mailbox_name;
391 if (strtoupper($temp_mailbox_name) == 'INBOX') {
392 $inbox_subscribed = TRUE;
396 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
397 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
398 $new_ary[] = $sorted_lsub_ary[$i];
401 $sorted_lsub_ary = $new_ary;
402 if (isset($sorted_lsub_ary)) {
403 usort($sorted_lsub_ary, 'user_strcasecmp');
407 $sorted_list_ary = array();
408 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
409 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
410 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
413 $mbx = $sorted_lsub_ary[$i];
416 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
417 TRUE, $response, $message);
418 /* Another workaround for EIMS */
419 if (isset($read[1]) &&
420 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
422 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
426 if (isset($sorted_list_ary[$i])) {
427 $sorted_list_ary[$i] = '';
430 if (isset($read[0])) {
431 $sorted_list_ary[$i] = $read[0];
434 $sorted_list_ary[$i] = '';
437 if (isset($sorted_list_ary[$i]) &&
438 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
439 $inbox_in_list = TRUE;
444 * Just in case they're not subscribed to their inbox,
445 * we'll get it for them anyway
447 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
448 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
449 TRUE, $response, $message);
450 /* Another workaround for EIMS */
451 if (isset($inbox_ary[1]) &&
452 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
453 $inbox_ary[0], $regs)) {
454 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
458 $sorted_list_ary[] = $inbox_ary[0];
459 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
462 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
464 /** Now, lets sort for special folders **/
465 $boxesnew = $used = array();
468 foreach ( $boxes as $k => $box ) {
469 if ( strtolower($box['unformatted']) == 'inbox') {
477 /* List special folders and their subfolders, if requested. */
478 if ($list_special_folders_first == TRUE) {
480 foreach ( $boxes as $k => $box ) {
482 isSpecialMailbox( $box['unformatted'] ) ) {
490 /* Rest of the folders */
491 foreach ( $boxes as $k => $box ) {
501 * Returns a list of all folders, subscribed or not
503 function sqimap_mailbox_list_all ($imap_stream)
505 global $list_special_folders_first, $folder_prefix;
508 if (!function_exists('ary_sort')) {
509 include_once('../functions/array.php');
512 $ssid = sqimap_session_id();
513 $lsid = strlen( $ssid );
514 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
515 $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
519 for ($i = 0; $i < count($read_ary); $i++
) {
520 /* Another workaround for EIMS */
521 if (isset($read_ary[$i +
1]) &&
522 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
523 $read_ary[$i], $regs)) {
525 $read_ary[$i] = $regs[1] . '"' .
526 addslashes(trim($read_ary[$i])) .
529 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
531 /* Store the raw IMAP reply */
532 $boxes[$g]["raw"] = $read_ary[$i];
534 /* Count number of delimiters ($delimiter) in folder name */
535 $mailbox = find_mailbox_name($read_ary[$i]);
536 $dm_count = substr_count($mailbox, $delimiter);
537 if (substr($mailbox, -1) == $delimiter) {
538 /* If name ends in delimiter - decrement count by one */
542 /* Format folder name, but only if it's a INBOX.* or have */
544 $boxesbyname[$mailbox] = $g;
545 $parentfolder = readMailboxParent($mailbox, $delimiter);
546 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
547 (ereg('^'.$folder_prefix, $mailbox)) ||
548 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
550 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
553 $boxes[$g]['formatted'] = '';
555 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
558 $boxes[$g]['formatted'] = $mailbox;
561 $boxes[$g]["unformatted-dm"] = $mailbox;
562 if (substr($mailbox, -1) == $delimiter) {
563 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
565 $boxes[$g]['unformatted'] = $mailbox;
566 $boxes[$g]['unformatted-disp'] =
567 ereg_replace('^' . $folder_prefix, '', $mailbox);
568 $boxes[$g]['id'] = $g;
570 /** Now lets get the flags for this mailbox **/
571 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
572 TRUE, $response, $message);
574 /* Another workaround for EIMS */
575 if (isset($read_mlbx[1]) &&
576 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
577 $read_mlbx[0], $regs)) {
578 $read_mlbx[0] = $regs[1] . '"' .
579 addslashes(trim($read_mlbx[1])) .
583 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
584 $flags = substr($flags, 0, strpos($flags, ')'));
585 $flags = str_replace('\\', '', $flags);
586 $flags = trim(strtolower($flags));
588 $boxes[$g]['flags'] = explode(' ', $flags);
590 $boxes[$g]['flags'] = array();
595 if(is_array($boxes)) {
596 $boxes = ary_sort ($boxes, 'unformatted', 1);