b3ff5a469b340f17d728656d947b9a62826bf168
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 ($box == $draft_folder &&
59 /*************************
60 ** Expunges a mailbox **
61 *************************/
62 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = TRUE)
64 $read = sqimap_run_command($imap_stream, 'EXPUNGE',
65 $handle_errors, $response, $message);
69 /******************************************************************************
70 ** Checks whether or not the specified mailbox exists
71 ******************************************************************************/
72 function sqimap_mailbox_exists ($imap_stream, $mailbox)
74 if (! isset($mailbox)) {
77 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
78 TRUE, $response, $message);
79 return isset($mbx[0]);
82 /******************************************************************************
84 ******************************************************************************/
85 function sqimap_mailbox_select ($imap_stream, $mailbox,
86 $hide=TRUE, $recent=false)
90 if ( $mailbox == 'None' ) {
94 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
95 TRUE, $response, $message);
97 for ($i=0; $i<count($read); $i++
) {
98 if (strpos(strtolower($read[$i]), 'recent')) {
99 $r = explode(' ', $read[$i]);
105 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE',
112 /******************************************************************************
114 ******************************************************************************/
115 function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
118 if (strtolower($type) == 'noselect') {
119 $mailbox = $mailbox.$delimiter;
121 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
122 TRUE, $response, $message);
124 sqimap_subscribe ($imap_stream, $mailbox);
129 /******************************************************************************
130 ** Subscribes to an existing folder
131 ******************************************************************************/
132 function sqimap_subscribe ($imap_stream, $mailbox)
134 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
135 TRUE, $response, $message);
140 /******************************************************************************
141 ** Unsubscribes to an existing folder
142 ******************************************************************************/
143 function sqimap_unsubscribe ($imap_stream, $mailbox)
145 global $imap_server_type;
147 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
148 TRUE, $response, $message);
153 /******************************************************************************
154 ** This function simply deletes the given folder
155 ******************************************************************************/
156 function sqimap_mailbox_delete ($imap_stream, $mailbox)
158 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
159 TRUE, $response, $message);
160 sqimap_unsubscribe ($imap_stream, $mailbox);
163 /***********************************************************************
164 ** Determines if the user is subscribed to the folder or not
165 **********************************************************************/
166 function sqimap_mailbox_is_subscribed($imap_stream, $folder)
168 $boxes = sqimap_mailbox_list ($imap_stream);
169 foreach ($boxes as $ref) {
170 if ($ref['unformatted'] == $folder) {
180 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
182 if ( $old_name <> $new_name ) {
186 if ( substr( $old_name, -1 ) == $delimiter ) {
187 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
188 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
189 $postfix = $delimiter;
190 $boxes = sqimap_mailbox_list($imap_stream);
196 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
197 $data = sqimap_run_command($imap_stream, $cmd,
198 TRUE, $response, $message);
199 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
200 sqimap_subscribe($imap_stream, $new_name.$postfix);
203 // Sub-unsub subfolders
204 $l = strlen( $old_name ) +
1;
206 foreach ( $boxes as $box ) {
207 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
208 sqimap_unsubscribe($imap_stream, $box[$p]);
209 sqimap_subscribe($imap_stream,
210 $new_name . $delimiter . substr( $box[$p], $l ) );
219 /******************************************************************************
220 ** Formats a mailbox into 4 parts for the $boxes array
222 ** The four parts are:
224 ** raw - Raw LIST/LSUB response from the IMAP server
225 ** formatted - nicely formatted folder name
226 ** unformatted - unformatted, but with delimiter at end removed
227 ** unformatted-dm - folder name as it appears in raw response
228 ** unformatted-disp - unformatted without $folder_prefix
230 ******************************************************************************/
231 function sqimap_mailbox_parse ($line, $line_lsub)
233 global $folder_prefix, $delimiter;
235 /* Process each folder line */
236 for ($g=0; $g < count($line); $g++
) {
238 /* Store the raw IMAP reply */
239 if (isset($line[$g])) {
240 $boxes[$g]["raw"] = $line[$g];
243 $boxes[$g]["raw"] = "";
247 /* Count number of delimiters ($delimiter) in folder name */
248 $mailbox = trim($line_lsub[$g]);
249 $dm_count = substr_count($mailbox, $delimiter);
250 if (substr($mailbox, -1) == $delimiter) {
251 /* If name ends in delimiter - decrement count by one */
255 /* Format folder name, but only if it's a INBOX.* or have */
257 $boxesbyname[$mailbox] = $g;
258 $parentfolder = readMailboxParent($mailbox, $delimiter);
259 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
260 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
261 ( isset($boxesbyname[$parentfolder]) &&
262 (strlen($parentfolder) > 0) ) ) {
263 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
265 $boxes[$g]['formatted'] = str_repeat(" ", $indent);
268 $boxes[$g]['formatted'] = '';
270 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
273 $boxes[$g]['formatted'] = $mailbox;
276 $boxes[$g]['unformatted-dm'] = $mailbox;
277 if (substr($mailbox, -1) == $delimiter) {
278 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
280 $boxes[$g]['unformatted'] = $mailbox;
281 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
282 $mailbox = substr($mailbox, strlen($folder_prefix));
284 $boxes[$g]['unformatted-disp'] = $mailbox;
285 $boxes[$g]['id'] = $g;
287 $boxes[$g]['flags'] = array();
288 if (isset($line[$g])) {
289 ereg("\(([^)]*)\)",$line[$g],$regs);
290 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
292 $boxes[$g]['flags'] = explode(' ', $flags);
301 * Sorting function used to sort mailbox names.
302 * + Original patch from dave_michmerhuizen@yahoo.com
303 * + Allows case insensitivity when sorting folders
304 * + Takes care of the delimiter being sorted to the end, causing
305 * subfolders to be listed in below folders that are prefixed
306 * with their parent folders name.
307 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
308 * Without special sort function: foobar between foo and foo.bar
309 * With special sort function: foobar AFTER foo and foo.bar :)
311 function user_strcasecmp($a, $b) {
314 /* Calculate the length of some strings. */
315 $a_length = strlen($a);
316 $b_length = strlen($b);
317 $min_length = min($a_length, $b_length);
318 $delimiter_length = strlen($delimiter);
320 /* Set the initial result value. */
323 /* Check the strings... */
324 for ($c = 0; $c < $min_length; ++
$c) {
325 $a_del = substr($a, $c, $delimiter_length);
326 $b_del = substr($b, $c, $delimiter_length);
328 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
330 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
332 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
335 $result = strcasecmp($a{$c}, $b{$c});
343 /* If one string is a prefix of the other... */
345 if ($a_length < $b_length) {
347 } else if ($a_length > $b_length) {
356 /******************************************************************************
357 ** Returns sorted mailbox lists in several different ways.
358 ** See comment on sqimap_mailbox_parse() for info about the returned array.
359 ******************************************************************************/
360 function sqimap_mailbox_list ($imap_stream) {
361 global $data_dir, $username, $list_special_folders_first,
362 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
363 $move_to_trash, $move_to_sent, $save_as_draft,
366 $inbox_in_list = false;
367 $inbox_subscribed = false;
369 require_once('../src/load_prefs.php');
370 require_once('../functions/array.php');
373 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
374 TRUE, $response, $message);
376 /* Section about removing the last element was removed */
377 /* We don't return "* OK" anymore from sqimap_read_data */
379 $sorted_lsub_ary = array();
380 for ($i=0;$i < count($lsub_ary); $i++
) {
381 /* Workaround for EIMS */
382 /* Doesn't work if the mailbox name is multiple lines */
383 if (isset($lsub_ary[$i +
1]) &&
384 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
385 $lsub_ary[$i], $regs)) {
387 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
390 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
391 $sorted_lsub_ary[] = $temp_mailbox_name;
392 if (strtoupper($temp_mailbox_name) == 'INBOX') {
393 $inbox_subscribed = TRUE;
397 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
398 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
399 $new_ary[] = $sorted_lsub_ary[$i];
402 $sorted_lsub_ary = $new_ary;
403 if (isset($sorted_lsub_ary)) {
404 usort($sorted_lsub_ary, 'user_strcasecmp');
408 $sorted_list_ary = array();
409 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
410 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
411 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
414 $mbx = $sorted_lsub_ary[$i];
417 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
418 TRUE, $response, $message);
419 /* Another workaround for EIMS */
420 if (isset($read[1]) &&
421 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
423 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
427 if (isset($sorted_list_ary[$i])) {
428 $sorted_list_ary[$i] = '';
431 if (isset($read[0])) {
432 $sorted_list_ary[$i] = $read[0];
435 $sorted_list_ary[$i] = '';
438 if (isset($sorted_list_ary[$i]) &&
439 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
440 $inbox_in_list = TRUE;
445 * Just in case they're not subscribed to their inbox,
446 * we'll get it for them anyway
448 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
449 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
450 TRUE, $response, $message);
451 /* Another workaround for EIMS */
452 if (isset($inbox_ary[1]) &&
453 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
454 $inbox_ary[0], $regs)) {
455 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
459 $sorted_list_ary[] = $inbox_ary[0];
460 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
463 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
465 /** Now, lets sort for special folders **/
466 $boxesnew = $used = array();
469 foreach ( $boxes as $k => $box ) {
470 if ( strtolower($box['unformatted']) == 'inbox') {
478 /* List special folders and their subfolders, if requested. */
479 if ($list_special_folders_first == TRUE) {
481 foreach ( $boxes as $k => $box ) {
483 isSpecialMailbox( $box['unformatted'] ) ) {
491 /* Rest of the folders */
492 foreach ( $boxes as $k => $box ) {
502 * Returns a list of all folders, subscribed or not
504 function sqimap_mailbox_list_all ($imap_stream)
506 global $list_special_folders_first, $folder_prefix;
509 if (!function_exists('ary_sort')) {
510 include_once('../functions/array.php');
513 $ssid = sqimap_session_id();
514 $lsid = strlen( $ssid );
515 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
516 $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
520 for ($i = 0; $i < count($read_ary); $i++
) {
521 /* Another workaround for EIMS */
522 if (isset($read_ary[$i +
1]) &&
523 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
524 $read_ary[$i], $regs)) {
526 $read_ary[$i] = $regs[1] . '"' .
527 addslashes(trim($read_ary[$i])) .
530 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
532 /* Store the raw IMAP reply */
533 $boxes[$g]["raw"] = $read_ary[$i];
535 /* Count number of delimiters ($delimiter) in folder name */
536 $mailbox = find_mailbox_name($read_ary[$i]);
537 $dm_count = substr_count($mailbox, $delimiter);
538 if (substr($mailbox, -1) == $delimiter) {
539 /* If name ends in delimiter - decrement count by one */
543 /* Format folder name, but only if it's a INBOX.* or have */
545 $boxesbyname[$mailbox] = $g;
546 $parentfolder = readMailboxParent($mailbox, $delimiter);
547 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
548 (ereg('^'.$folder_prefix, $mailbox)) ||
549 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
551 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
554 $boxes[$g]['formatted'] = '';
556 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
559 $boxes[$g]['formatted'] = $mailbox;
562 $boxes[$g]["unformatted-dm"] = $mailbox;
563 if (substr($mailbox, -1) == $delimiter) {
564 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
566 $boxes[$g]['unformatted'] = $mailbox;
567 $boxes[$g]['unformatted-disp'] =
568 ereg_replace('^' . $folder_prefix, '', $mailbox);
569 $boxes[$g]['id'] = $g;
571 /** Now lets get the flags for this mailbox **/
572 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
573 TRUE, $response, $message);
575 /* Another workaround for EIMS */
576 if (isset($read_mlbx[1]) &&
577 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
578 $read_mlbx[0], $regs)) {
579 $read_mlbx[0] = $regs[1] . '"' .
580 addslashes(trim($read_mlbx[1])) .
584 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
585 $flags = substr($flags, 0, strpos($flags, ')'));
586 $flags = str_replace('\\', '', $flags);
587 $flags = trim(strtolower($flags));
589 $boxes[$g]['flags'] = explode(' ', $flags);
591 $boxes[$g]['flags'] = array();
596 if(is_array($boxes)) {
597 $boxes = ary_sort ($boxes, 'unformatted', 1);