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;
48 $ret = ( (strtolower($box) == 'inbox') ||
49 ( $move_to_trash && isBoxBelow( $box, $trash_folder ) ) ||
50 ( $move_to_sent && isBoxBelow( $box, $sent_folder )) ||
51 ($save_as_draft && $box == $draft_folder ) );
57 /*************************
58 ** Expunges a mailbox **
59 *************************/
60 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = TRUE)
62 $read = sqimap_run_command($imap_stream, 'EXPUNGE',
63 $handle_errors, $response, $message);
67 /******************************************************************************
68 ** Checks whether or not the specified mailbox exists
69 ******************************************************************************/
70 function sqimap_mailbox_exists ($imap_stream, $mailbox)
72 if (! isset($mailbox)) {
75 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
76 TRUE, $response, $message);
77 return isset($mbx[0]);
80 /******************************************************************************
82 ******************************************************************************/
83 function sqimap_mailbox_select ($imap_stream, $mailbox,
84 $hide=TRUE, $recent=false)
88 if ( $mailbox == 'None' ) {
92 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
93 TRUE, $response, $message);
95 for ($i=0; $i<count($read); $i++
) {
96 if (strpos(strtolower($read[$i]), 'recent')) {
97 $r = explode(' ', $read[$i]);
103 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE',
110 /******************************************************************************
112 ******************************************************************************/
113 function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
116 if (strtolower($type) == 'noselect') {
117 $mailbox = $mailbox.$delimiter;
119 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
120 TRUE, $response, $message);
122 sqimap_subscribe ($imap_stream, $mailbox);
127 /******************************************************************************
128 ** Subscribes to an existing folder
129 ******************************************************************************/
130 function sqimap_subscribe ($imap_stream, $mailbox)
132 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
133 TRUE, $response, $message);
138 /******************************************************************************
139 ** Unsubscribes to an existing folder
140 ******************************************************************************/
141 function sqimap_unsubscribe ($imap_stream, $mailbox)
143 global $imap_server_type;
145 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
146 TRUE, $response, $message);
151 /******************************************************************************
152 ** This function simply deletes the given folder
153 ******************************************************************************/
154 function sqimap_mailbox_delete ($imap_stream, $mailbox)
156 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
157 TRUE, $response, $message);
158 sqimap_unsubscribe ($imap_stream, $mailbox);
161 /***********************************************************************
162 ** Determines if the user is subscribed to the folder or not
163 **********************************************************************/
164 function sqimap_mailbox_is_subscribed($imap_stream, $folder)
166 $boxes = sqimap_mailbox_list ($imap_stream);
167 foreach ($boxes as $ref) {
168 if ($ref['unformatted'] == $folder) {
178 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
180 if ( $old_name <> $new_name ) {
184 if ( substr( $old_name, -1 ) == $delimiter ) {
185 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
186 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
187 $postfix = $delimiter;
188 $boxes = sqimap_mailbox_list($imap_stream);
194 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
195 $data = sqimap_run_command($imap_stream, $cmd,
196 TRUE, $response, $message);
197 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
198 sqimap_subscribe($imap_stream, $new_name.$postfix);
201 // Sub-unsub subfolders
202 $l = strlen( $old_name ) +
1;
204 foreach ( $boxes as $box ) {
205 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
206 sqimap_unsubscribe($imap_stream, $box[$p]);
207 sqimap_subscribe($imap_stream,
208 $new_name . $delimiter . substr( $box[$p], $l ) );
217 /******************************************************************************
218 ** Formats a mailbox into 4 parts for the $boxes array
220 ** The four parts are:
222 ** raw - Raw LIST/LSUB response from the IMAP server
223 ** formatted - nicely formatted folder name
224 ** unformatted - unformatted, but with delimiter at end removed
225 ** unformatted-dm - folder name as it appears in raw response
226 ** unformatted-disp - unformatted without $folder_prefix
228 ******************************************************************************/
229 function sqimap_mailbox_parse ($line, $line_lsub)
231 global $folder_prefix, $delimiter;
233 /* Process each folder line */
234 for ($g=0; $g < count($line); $g++
) {
236 /* Store the raw IMAP reply */
237 if (isset($line[$g])) {
238 $boxes[$g]["raw"] = $line[$g];
241 $boxes[$g]["raw"] = "";
245 /* Count number of delimiters ($delimiter) in folder name */
246 $mailbox = trim($line_lsub[$g]);
247 $dm_count = substr_count($mailbox, $delimiter);
248 if (substr($mailbox, -1) == $delimiter) {
249 /* If name ends in delimiter - decrement count by one */
253 /* Format folder name, but only if it's a INBOX.* or have */
255 $boxesbyname[$mailbox] = $g;
256 $parentfolder = readMailboxParent($mailbox, $delimiter);
257 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
258 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
259 ( isset($boxesbyname[$parentfolder]) &&
260 (strlen($parentfolder) > 0) ) ) {
261 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
263 $boxes[$g]['formatted'] = str_repeat(" ", $indent);
266 $boxes[$g]['formatted'] = '';
268 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
271 $boxes[$g]['formatted'] = $mailbox;
274 $boxes[$g]['unformatted-dm'] = $mailbox;
275 if (substr($mailbox, -1) == $delimiter) {
276 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
278 $boxes[$g]['unformatted'] = $mailbox;
279 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
280 $mailbox = substr($mailbox, strlen($folder_prefix));
282 $boxes[$g]['unformatted-disp'] = $mailbox;
283 $boxes[$g]['id'] = $g;
285 $boxes[$g]['flags'] = array();
286 if (isset($line[$g])) {
287 ereg("\(([^)]*)\)",$line[$g],$regs);
288 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
290 $boxes[$g]['flags'] = explode(' ', $flags);
299 * Sorting function used to sort mailbox names.
300 * + Original patch from dave_michmerhuizen@yahoo.com
301 * + Allows case insensitivity when sorting folders
302 * + Takes care of the delimiter being sorted to the end, causing
303 * subfolders to be listed in below folders that are prefixed
304 * with their parent folders name.
305 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
306 * Without special sort function: foobar between foo and foo.bar
307 * With special sort function: foobar AFTER foo and foo.bar :)
309 function user_strcasecmp($a, $b) {
312 /* Calculate the length of some strings. */
313 $a_length = strlen($a);
314 $b_length = strlen($b);
315 $min_length = min($a_length, $b_length);
316 $delimiter_length = strlen($delimiter);
318 /* Set the initial result value. */
321 /* Check the strings... */
322 for ($c = 0; $c < $min_length; ++
$c) {
323 $a_del = substr($a, $c, $delimiter_length);
324 $b_del = substr($b, $c, $delimiter_length);
326 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
328 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
330 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
333 $result = strcasecmp($a{$c}, $b{$c});
341 /* If one string is a prefix of the other... */
343 if ($a_length < $b_length) {
345 } else if ($a_length > $b_length) {
354 /******************************************************************************
355 ** Returns sorted mailbox lists in several different ways.
356 ** See comment on sqimap_mailbox_parse() for info about the returned array.
357 ******************************************************************************/
358 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 = $inbox_subscribed = FALSE;
367 require_once('../src/load_prefs.php');
368 require_once('../functions/array.php');
371 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
372 TRUE, $response, $message);
374 /* Section about removing the last element was removed */
375 /* We don't return "* OK" anymore from sqimap_read_data */
377 $sorted_lsub_ary = array();
378 for ($i=0;$i < count($lsub_ary); $i++
) {
379 /* Workaround for EIMS */
380 /* Doesn't work if the mailbox name is multiple lines */
381 if (isset($lsub_ary[$i +
1]) &&
382 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
383 $lsub_ary[$i], $regs)) {
385 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
388 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
389 $sorted_lsub_ary[] = $temp_mailbox_name;
390 if (strtoupper($temp_mailbox_name) == 'INBOX') {
391 $inbox_subscribed = TRUE;
395 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
396 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
397 $new_ary[] = $sorted_lsub_ary[$i];
400 $sorted_lsub_ary = $new_ary;
401 if (isset($sorted_lsub_ary)) {
402 usort($sorted_lsub_ary, 'user_strcasecmp');
406 $sorted_list_ary = array();
407 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
408 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
409 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
412 $mbx = $sorted_lsub_ary[$i];
415 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
416 TRUE, $response, $message);
417 /* Another workaround for EIMS */
418 if (isset($read[1]) &&
419 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
421 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
425 if (isset($sorted_list_ary[$i])) {
426 $sorted_list_ary[$i] = '';
429 if (isset($read[0])) {
430 $sorted_list_ary[$i] = $read[0];
433 $sorted_list_ary[$i] = '';
436 if (isset($sorted_list_ary[$i]) &&
437 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
438 $inbox_in_list = TRUE;
443 * Just in case they're not subscribed to their inbox,
444 * we'll get it for them anyway
446 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
447 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
448 TRUE, $response, $message);
449 /* Another workaround for EIMS */
450 if (isset($inbox_ary[1]) &&
451 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
452 $inbox_ary[0], $regs)) {
453 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
457 $sorted_list_ary[] = $inbox_ary[0];
458 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
461 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
463 /** Now, lets sort for special folders **/
464 $boxesnew = $used = array();
467 foreach ( $boxes as $k => $box ) {
468 if ( strtolower($box['unformatted']) == 'inbox') {
476 /* List special folders and their subfolders, if requested. */
477 if ($list_special_folders_first == TRUE) {
479 foreach ( $boxes as $k => $box ) {
481 isSpecialMailbox( $box['unformatted'] ) ) {
489 /* Rest of the folders */
490 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 require_once('../functions/array.php');
510 $ssid = sqimap_session_id();
511 $lsid = strlen( $ssid );
512 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
513 $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
517 for ($i = 0; $i < count($read_ary); $i++
) {
518 /* Another workaround for EIMS */
519 if (isset($read_ary[$i +
1]) &&
520 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
521 $read_ary[$i], $regs)) {
523 $read_ary[$i] = $regs[1] . '"' .
524 addslashes(trim($read_ary[$i])) .
527 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
529 /* Store the raw IMAP reply */
530 $boxes[$g]['raw'] = $read_ary[$i];
532 /* Count number of delimiters ($delimiter) in folder name */
533 $mailbox = find_mailbox_name($read_ary[$i]);
534 $dm_count = substr_count($mailbox, $delimiter);
535 if (substr($mailbox, -1) == $delimiter) {
536 /* If name ends in delimiter - decrement count by one */
540 /* Format folder name, but only if it's a INBOX.* or have */
542 $boxesbyname[$mailbox] = $g;
543 $parentfolder = readMailboxParent($mailbox, $delimiter);
544 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
545 (ereg('^'.$folder_prefix, $mailbox)) ||
546 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
548 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
551 $boxes[$g]['formatted'] = '';
553 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
556 $boxes[$g]['formatted'] = $mailbox;
559 $boxes[$g]["unformatted-dm"] = $mailbox;
560 if (substr($mailbox, -1) == $delimiter) {
561 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
563 $boxes[$g]['unformatted'] = $mailbox;
564 $boxes[$g]['unformatted-disp'] =
565 ereg_replace('^' . $folder_prefix, '', $mailbox);
566 $boxes[$g]['id'] = $g;
568 /** Now lets get the flags for this mailbox **/
569 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
570 TRUE, $response, $message);
572 /* Another workaround for EIMS */
573 if (isset($read_mlbx[1]) &&
574 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
575 $read_mlbx[0], $regs)) {
576 $read_mlbx[0] = $regs[1] . '"' .
577 addslashes(trim($read_mlbx[1])) .
581 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
582 $flags = substr($flags, 0, strpos($flags, ')'));
583 $flags = str_replace('\\', '', $flags);
584 $flags = trim(strtolower($flags));
586 $boxes[$g]['flags'] = explode(' ', $flags);
588 $boxes[$g]['flags'] = array();
593 if(is_array($boxes)) {
594 $boxes = ary_sort ($boxes, 'unformatted', 1);