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
15 Defines Special Mail Boxes
18 function isSpecialMailbox( $box ) {
20 global $trash_folder, $sent_folder, $draft_folder,
21 $move_to_trash, $move_to_sent, $save_as_draft,
22 $delimiter, $folder_prefix, $imap_server_type;
24 if ( $move_to_sent ) {
25 if ( $imap_server_type == 'uw' ) {
26 $i = strpos( $sent_folder, $delimiter, strlen( $folder_prefix ) );
35 $ret = ( (strtolower($box) == 'inbox') ||
36 ($box == $trash_folder &&
38 ( substr( $sent_folder, 0, $i ) == substr( $box, 0, $i ) &&
40 ($box == $draft_folder &&
47 /*************************
48 ** Expunges a mailbox **
49 *************************/
50 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = TRUE)
52 $read = sqimap_run_command($imap_stream, 'EXPUNGE',
53 $handle_errors, $response, $message);
57 /******************************************************************************
58 ** Checks whether or not the specified mailbox exists
59 ******************************************************************************/
60 function sqimap_mailbox_exists ($imap_stream, $mailbox)
62 if (! isset($mailbox)) {
65 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
66 TRUE, $response, $message);
67 return isset($mbx[0]);
70 /******************************************************************************
72 ******************************************************************************/
73 function sqimap_mailbox_select ($imap_stream, $mailbox,
74 $hide=TRUE, $recent=false)
78 if ( $mailbox == 'None' ) {
82 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
83 TRUE, $response, $message);
85 for ($i=0; $i<count($read); $i++
) {
86 if (strpos(strtolower($read[$i]), 'recent')) {
87 $r = explode(' ', $read[$i]);
93 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE',
100 /******************************************************************************
102 ******************************************************************************/
103 function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
106 if (strtolower($type) == 'noselect') {
107 $mailbox = $mailbox.$delimiter;
109 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
110 TRUE, $response, $message);
112 sqimap_subscribe ($imap_stream, $mailbox);
117 /******************************************************************************
118 ** Subscribes to an existing folder
119 ******************************************************************************/
120 function sqimap_subscribe ($imap_stream, $mailbox)
122 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
123 TRUE, $response, $message);
128 /******************************************************************************
129 ** Unsubscribes to an existing folder
130 ******************************************************************************/
131 function sqimap_unsubscribe ($imap_stream, $mailbox)
133 global $imap_server_type;
135 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
136 TRUE, $response, $message);
141 /******************************************************************************
142 ** This function simply deletes the given folder
143 ******************************************************************************/
144 function sqimap_mailbox_delete ($imap_stream, $mailbox)
146 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
147 TRUE, $response, $message);
148 sqimap_unsubscribe ($imap_stream, $mailbox);
151 /***********************************************************************
152 ** Determines if the user is subscribed to the folder or not
153 **********************************************************************/
154 function sqimap_mailbox_is_subscribed($imap_stream, $folder)
156 $boxes = sqimap_mailbox_list ($imap_stream);
157 foreach ($boxes as $ref) {
158 if ($ref['unformatted'] == $folder) {
168 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
170 if ( $old_name <> $new_name ) {
174 if ( substr( $old_name, -1 ) == $delimiter ) {
175 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
176 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
177 $postfix = $delimiter;
178 $boxes = sqimap_mailbox_list($imap_stream);
184 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
185 $data = sqimap_run_command($imap_stream, $cmd,
186 TRUE, $response, $message);
187 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
188 sqimap_subscribe($imap_stream, $new_name.$postfix);
191 // Sub-unsub subfolders
192 $l = strlen( $old_name ) +
1;
194 foreach ( $boxes as $box ) {
195 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
196 sqimap_unsubscribe($imap_stream, $box[$p]);
197 sqimap_subscribe($imap_stream,
198 $new_name . $delimiter . substr( $box[$p], $l ) );
207 /******************************************************************************
208 ** Formats a mailbox into 4 parts for the $boxes array
210 ** The four parts are:
212 ** raw - Raw LIST/LSUB response from the IMAP server
213 ** formatted - nicely formatted folder name
214 ** unformatted - unformatted, but with delimiter at end removed
215 ** unformatted-dm - folder name as it appears in raw response
216 ** unformatted-disp - unformatted without $folder_prefix
218 ******************************************************************************/
219 function sqimap_mailbox_parse ($line, $line_lsub)
221 global $folder_prefix, $delimiter;
223 /* Process each folder line */
224 for ($g=0; $g < count($line); $g++
) {
226 /* Store the raw IMAP reply */
227 if (isset($line[$g])) {
228 $boxes[$g]["raw"] = $line[$g];
231 $boxes[$g]["raw"] = "";
235 /* Count number of delimiters ($delimiter) in folder name */
236 $mailbox = trim($line_lsub[$g]);
237 $dm_count = substr_count($mailbox, $delimiter);
238 if (substr($mailbox, -1) == $delimiter) {
239 /* If name ends in delimiter - decrement count by one */
243 /* Format folder name, but only if it's a INBOX.* or have */
245 $boxesbyname[$mailbox] = $g;
246 $parentfolder = readMailboxParent($mailbox, $delimiter);
247 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
248 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
249 ( isset($boxesbyname[$parentfolder]) &&
250 (strlen($parentfolder) > 0) ) ) {
251 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
253 $boxes[$g]['formatted'] = str_repeat(" ", $indent);
256 $boxes[$g]['formatted'] = '';
258 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
261 $boxes[$g]['formatted'] = $mailbox;
264 $boxes[$g]['unformatted-dm'] = $mailbox;
265 if (substr($mailbox, -1) == $delimiter) {
266 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
268 $boxes[$g]['unformatted'] = $mailbox;
269 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
270 $mailbox = substr($mailbox, strlen($folder_prefix));
272 $boxes[$g]['unformatted-disp'] = $mailbox;
273 $boxes[$g]['id'] = $g;
275 $boxes[$g]['flags'] = array();
276 if (isset($line[$g])) {
277 ereg("\(([^)]*)\)",$line[$g],$regs);
278 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
280 $boxes[$g]['flags'] = explode(' ', $flags);
289 * Sorting function used to sort mailbox names.
290 * + Original patch from dave_michmerhuizen@yahoo.com
291 * + Allows case insensitivity when sorting folders
292 * + Takes care of the delimiter being sorted to the end, causing
293 * subfolders to be listed in below folders that are prefixed
294 * with their parent folders name.
295 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
296 * Without special sort function: foobar between foo and foo.bar
297 * With special sort function: foobar AFTER foo and foo.bar :)
299 function user_strcasecmp($a, $b) {
302 /* Calculate the length of some strings. */
303 $a_length = strlen($a);
304 $b_length = strlen($b);
305 $min_length = min($a_length, $b_length);
306 $delimiter_length = strlen($delimiter);
308 /* Set the initial result value. */
311 /* Check the strings... */
312 for ($c = 0; $c < $min_length; ++
$c) {
313 $a_del = substr($a, $c, $delimiter_length);
314 $b_del = substr($b, $c, $delimiter_length);
316 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
318 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
320 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
323 $result = strcasecmp($a{$c}, $b{$c});
331 /* If one string is a prefix of the other... */
333 if ($a_length < $b_length) {
335 } else if ($a_length > $b_length) {
344 /******************************************************************************
345 ** Returns sorted mailbox lists in several different ways.
346 ** See comment on sqimap_mailbox_parse() for info about the returned array.
347 ******************************************************************************/
348 function sqimap_mailbox_list ($imap_stream) {
349 global $data_dir, $username, $list_special_folders_first,
350 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
351 $move_to_trash, $move_to_sent, $save_as_draft,
354 $inbox_in_list = false;
355 $inbox_subscribed = false;
357 require_once('../src/load_prefs.php');
358 require_once('../functions/array.php');
361 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
362 TRUE, $response, $message);
364 /* Section about removing the last element was removed */
365 /* We don't return "* OK" anymore from sqimap_read_data */
367 $sorted_lsub_ary = array();
368 for ($i=0;$i < count($lsub_ary); $i++
) {
369 /* Workaround for EIMS */
370 /* Doesn't work if the mailbox name is multiple lines */
371 if (isset($lsub_ary[$i +
1]) &&
372 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
373 $lsub_ary[$i], $regs)) {
375 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
378 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
379 $sorted_lsub_ary[] = $temp_mailbox_name;
380 if (strtoupper($temp_mailbox_name) == 'INBOX') {
381 $inbox_subscribed = TRUE;
385 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
386 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
387 $new_ary[] = $sorted_lsub_ary[$i];
390 $sorted_lsub_ary = $new_ary;
391 if (isset($sorted_lsub_ary)) {
392 usort($sorted_lsub_ary, 'user_strcasecmp');
396 $sorted_list_ary = array();
397 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
398 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
399 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
402 $mbx = $sorted_lsub_ary[$i];
405 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
406 TRUE, $response, $message);
407 /* Another workaround for EIMS */
408 if (isset($read[1]) &&
409 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
411 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
415 if (isset($sorted_list_ary[$i])) {
416 $sorted_list_ary[$i] = '';
419 if (isset($read[0])) {
420 $sorted_list_ary[$i] = $read[0];
423 $sorted_list_ary[$i] = '';
426 if (isset($sorted_list_ary[$i]) &&
427 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
428 $inbox_in_list = TRUE;
433 * Just in case they're not subscribed to their inbox,
434 * we'll get it for them anyway
436 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
437 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
438 TRUE, $response, $message);
439 /* Another workaround for EIMS */
440 if (isset($inbox_ary[1]) &&
441 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
442 $inbox_ary[0], $regs)) {
443 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
447 $sorted_list_ary[] = $inbox_ary[0];
448 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
451 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
453 /** Now, lets sort for special folders **/
454 $boxesnew = $used = array();
457 foreach ( $boxes as $k => $box ) {
458 if ( strtolower($box['unformatted']) == 'inbox') {
466 /* List special folders and their subfolders, if requested. */
467 if ($list_special_folders_first == TRUE) {
469 foreach ( $boxes as $k => $box ) {
471 isSpecialMailbox( $box['unformatted'] ) ) {
479 /* Rest of the folders */
480 foreach ( $boxes as $k => $box ) {
490 * Returns a list of all folders, subscribed or not
492 function sqimap_mailbox_list_all ($imap_stream)
494 global $list_special_folders_first, $folder_prefix;
497 if (!function_exists('ary_sort')) {
498 include_once('../functions/array.php');
501 $ssid = sqimap_session_id();
502 $lsid = strlen( $ssid );
503 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
504 $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
508 for ($i = 0; $i < count($read_ary); $i++
) {
509 /* Another workaround for EIMS */
510 if (isset($read_ary[$i +
1]) &&
511 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
512 $read_ary[$i], $regs)) {
514 $read_ary[$i] = $regs[1] . '"' .
515 addslashes(trim($read_ary[$i])) .
518 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
520 /* Store the raw IMAP reply */
521 $boxes[$g]["raw"] = $read_ary[$i];
523 /* Count number of delimiters ($delimiter) in folder name */
524 $mailbox = find_mailbox_name($read_ary[$i]);
525 $dm_count = substr_count($mailbox, $delimiter);
526 if (substr($mailbox, -1) == $delimiter) {
527 /* If name ends in delimiter - decrement count by one */
531 /* Format folder name, but only if it's a INBOX.* or have */
533 $boxesbyname[$mailbox] = $g;
534 $parentfolder = readMailboxParent($mailbox, $delimiter);
535 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
536 (ereg('^'.$folder_prefix, $mailbox)) ||
537 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
539 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
542 $boxes[$g]['formatted'] = '';
544 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
547 $boxes[$g]['formatted'] = $mailbox;
550 $boxes[$g]["unformatted-dm"] = $mailbox;
551 if (substr($mailbox, -1) == $delimiter) {
552 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
554 $boxes[$g]['unformatted'] = $mailbox;
555 $boxes[$g]['unformatted-disp'] =
556 ereg_replace('^' . $folder_prefix, '', $mailbox);
557 $boxes[$g]['id'] = $g;
559 /** Now lets get the flags for this mailbox **/
560 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
561 TRUE, $response, $message);
563 /* Another workaround for EIMS */
564 if (isset($read_mlbx[1]) &&
565 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
566 $read_mlbx[0], $regs)) {
567 $read_mlbx[0] = $regs[1] . '"' .
568 addslashes(trim($read_mlbx[1])) .
572 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
573 $flags = substr($flags, 0, strpos($flags, ')'));
574 $flags = str_replace('\\', '', $flags);
575 $flags = trim(strtolower($flags));
577 $boxes[$g]['flags'] = explode(' ', $flags);
579 $boxes[$g]['flags'] = array();
584 if(is_array($boxes)) {
585 $boxes = ary_sort ($boxes, 'unformatted', 1);