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;
24 if ( $move_to_sent ) {
25 $i = strpos( $sent_folder, $delimiter, strlen( $folder_prefix ) );
32 $ret = ( (strtolower($box) == 'inbox') ||
33 ($box == $trash_folder &&
35 ( substr( $sent_folder, 0, $i ) == substr( $box, 0, $i ) &&
37 ($box == $draft_folder &&
44 /*************************
45 ** Expunges a mailbox **
46 *************************/
47 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = TRUE)
49 $read = sqimap_run_command($imap_stream, 'EXPUNGE',
50 $handle_errors, $response, $message);
54 /******************************************************************************
55 ** Checks whether or not the specified mailbox exists
56 ******************************************************************************/
57 function sqimap_mailbox_exists ($imap_stream, $mailbox)
59 if (! isset($mailbox)) {
62 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
63 TRUE, $response, $message);
64 return isset($mbx[0]);
67 /******************************************************************************
69 ******************************************************************************/
70 function sqimap_mailbox_select ($imap_stream, $mailbox,
71 $hide=TRUE, $recent=false)
75 if ( $mailbox == 'None' ) {
79 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
80 TRUE, $response, $message);
82 for ($i=0; $i<count($read); $i++
) {
83 if (strpos(strtolower($read[$i]), 'recent')) {
84 $r = explode(' ', $read[$i]);
90 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE',
97 /******************************************************************************
99 ******************************************************************************/
100 function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
103 if (strtolower($type) == 'noselect') {
104 $mailbox = $mailbox.$delimiter;
106 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
107 TRUE, $response, $message);
109 sqimap_subscribe ($imap_stream, $mailbox);
114 /******************************************************************************
115 ** Subscribes to an existing folder
116 ******************************************************************************/
117 function sqimap_subscribe ($imap_stream, $mailbox)
119 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
120 TRUE, $response, $message);
125 /******************************************************************************
126 ** Unsubscribes to an existing folder
127 ******************************************************************************/
128 function sqimap_unsubscribe ($imap_stream, $mailbox)
130 global $imap_server_type;
132 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
133 TRUE, $response, $message);
138 /******************************************************************************
139 ** This function simply deletes the given folder
140 ******************************************************************************/
141 function sqimap_mailbox_delete ($imap_stream, $mailbox)
143 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
144 TRUE, $response, $message);
145 sqimap_unsubscribe ($imap_stream, $mailbox);
148 /***********************************************************************
149 ** Determines if the user is subscribed to the folder or not
150 **********************************************************************/
151 function sqimap_mailbox_is_subscribed($imap_stream, $folder)
153 $boxes = sqimap_mailbox_list ($imap_stream);
154 foreach ($boxes as $ref) {
155 if ($ref['unformatted'] == $folder) {
165 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
167 if ( $old_name <> $new_name ) {
171 if ( substr( $old_name, -1 ) == $delimiter ) {
172 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
173 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
174 $postfix = $delimiter;
175 $boxes = sqimap_mailbox_list($imap_stream);
181 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
182 $data = sqimap_run_command($imap_stream, $cmd,
183 TRUE, $response, $message);
184 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
185 sqimap_subscribe($imap_stream, $new_name.$postfix);
188 // Sub-unsub subfolders
189 $l = strlen( $old_name ) +
1;
191 foreach ( $boxes as $box ) {
192 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
193 sqimap_unsubscribe($imap_stream, $box[$p]);
194 sqimap_subscribe($imap_stream,
195 $new_name . $delimiter . substr( $box[$p], $l ) );
204 /******************************************************************************
205 ** Formats a mailbox into 4 parts for the $boxes array
207 ** The four parts are:
209 ** raw - Raw LIST/LSUB response from the IMAP server
210 ** formatted - nicely formatted folder name
211 ** unformatted - unformatted, but with delimiter at end removed
212 ** unformatted-dm - folder name as it appears in raw response
213 ** unformatted-disp - unformatted without $folder_prefix
215 ******************************************************************************/
216 function sqimap_mailbox_parse ($line, $line_lsub)
218 global $folder_prefix, $delimiter;
220 /* Process each folder line */
221 for ($g=0; $g < count($line); $g++
) {
223 /* Store the raw IMAP reply */
224 if (isset($line[$g])) {
225 $boxes[$g]["raw"] = $line[$g];
228 $boxes[$g]["raw"] = "";
232 /* Count number of delimiters ($delimiter) in folder name */
233 $mailbox = trim($line_lsub[$g]);
234 $dm_count = substr_count($mailbox, $delimiter);
235 if (substr($mailbox, -1) == $delimiter) {
236 /* If name ends in delimiter - decrement count by one */
240 /* Format folder name, but only if it's a INBOX.* or have */
242 $boxesbyname[$mailbox] = $g;
243 $parentfolder = readMailboxParent($mailbox, $delimiter);
244 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
245 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
246 ( isset($boxesbyname[$parentfolder]) &&
247 (strlen($parentfolder) > 0) ) ) {
248 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
250 $boxes[$g]['formatted'] = str_repeat(" ", $indent);
253 $boxes[$g]['formatted'] = '';
255 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
258 $boxes[$g]['formatted'] = $mailbox;
261 $boxes[$g]['unformatted-dm'] = $mailbox;
262 if (substr($mailbox, -1) == $delimiter) {
263 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
265 $boxes[$g]['unformatted'] = $mailbox;
266 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
267 $mailbox = substr($mailbox, strlen($folder_prefix));
269 $boxes[$g]['unformatted-disp'] = $mailbox;
270 $boxes[$g]['id'] = $g;
272 $boxes[$g]['flags'] = array();
273 if (isset($line[$g])) {
274 ereg("\(([^)]*)\)",$line[$g],$regs);
275 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
277 $boxes[$g]['flags'] = explode(' ', $flags);
286 * Sorting function used to sort mailbox names.
287 * + Original patch from dave_michmerhuizen@yahoo.com
288 * + Allows case insensitivity when sorting folders
289 * + Takes care of the delimiter being sorted to the end, causing
290 * subfolders to be listed in below folders that are prefixed
291 * with their parent folders name.
292 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
293 * Without special sort function: foobar between foo and foo.bar
294 * With special sort function: foobar AFTER foo and foo.bar :)
296 function user_strcasecmp($a, $b) {
299 /* Calculate the length of some strings. */
300 $a_length = strlen($a);
301 $b_length = strlen($b);
302 $min_length = min($a_length, $b_length);
303 $delimiter_length = strlen($delimiter);
305 /* Set the initial result value. */
308 /* Check the strings... */
309 for ($c = 0; $c < $min_length; ++
$c) {
310 $a_del = substr($a, $c, $delimiter_length);
311 $b_del = substr($b, $c, $delimiter_length);
313 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
315 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
317 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
320 $result = strcasecmp($a{$c}, $b{$c});
328 /* If one string is a prefix of the other... */
330 if ($a_length < $b_length) {
332 } else if ($a_length > $b_length) {
341 /******************************************************************************
342 ** Returns sorted mailbox lists in several different ways.
343 ** See comment on sqimap_mailbox_parse() for info about the returned array.
344 ******************************************************************************/
345 function sqimap_mailbox_list ($imap_stream) {
346 global $data_dir, $username, $list_special_folders_first,
347 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
348 $move_to_trash, $move_to_sent, $save_as_draft,
351 $inbox_in_list = false;
352 $inbox_subscribed = false;
354 require_once('../src/load_prefs.php');
355 require_once('../functions/array.php');
358 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
359 TRUE, $response, $message);
361 /* Section about removing the last element was removed */
362 /* We don't return "* OK" anymore from sqimap_read_data */
364 $sorted_lsub_ary = array();
365 for ($i=0;$i < count($lsub_ary); $i++
) {
366 /* Workaround for EIMS */
367 /* Doesn't work if the mailbox name is multiple lines */
368 if (isset($lsub_ary[$i +
1]) &&
369 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
370 $lsub_ary[$i], $regs)) {
372 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
375 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
376 $sorted_lsub_ary[] = $temp_mailbox_name;
377 if (strtoupper($temp_mailbox_name) == 'INBOX') {
378 $inbox_subscribed = TRUE;
382 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
383 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
384 $new_ary[] = $sorted_lsub_ary[$i];
387 $sorted_lsub_ary = $new_ary;
388 if (isset($sorted_lsub_ary)) {
389 usort($sorted_lsub_ary, 'user_strcasecmp');
393 $sorted_list_ary = array();
394 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
395 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
396 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
399 $mbx = $sorted_lsub_ary[$i];
402 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
403 TRUE, $response, $message);
404 /* Another workaround for EIMS */
405 if (isset($read[1]) &&
406 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
408 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
412 if (isset($sorted_list_ary[$i])) {
413 $sorted_list_ary[$i] = '';
416 if (isset($read[0])) {
417 $sorted_list_ary[$i] = $read[0];
420 $sorted_list_ary[$i] = '';
423 if (isset($sorted_list_ary[$i]) &&
424 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
425 $inbox_in_list = TRUE;
430 * Just in case they're not subscribed to their inbox,
431 * we'll get it for them anyway
433 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
434 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
435 TRUE, $response, $message);
436 /* Another workaround for EIMS */
437 if (isset($inbox_ary[1]) &&
438 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
439 $inbox_ary[0], $regs)) {
440 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
444 $sorted_list_ary[] = $inbox_ary[0];
445 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
448 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
450 /** Now, lets sort for special folders **/
451 $boxesnew = $used = array();
454 foreach ( $boxes as $k => $box ) {
455 if ( strtolower($box['unformatted']) == 'inbox') {
463 /* List special folders and their subfolders, if requested. */
464 if ($list_special_folders_first == TRUE) {
466 foreach ( $boxes as $k => $box ) {
468 isSpecialMailbox( $box['unformatted'] ) ) {
476 /* Rest of the folders */
477 foreach ( $boxes as $k => $box ) {
487 * Returns a list of all folders, subscribed or not
489 function sqimap_mailbox_list_all ($imap_stream)
491 global $list_special_folders_first, $folder_prefix;
494 if (!function_exists('ary_sort')) {
495 include_once('../functions/array.php');
498 $ssid = sqimap_session_id();
499 $lsid = strlen( $ssid );
500 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
501 $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
505 for ($i = 0; $i < count($read_ary); $i++
) {
506 /* Another workaround for EIMS */
507 if (isset($read_ary[$i +
1]) &&
508 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
509 $read_ary[$i], $regs)) {
511 $read_ary[$i] = $regs[1] . '"' .
512 addslashes(trim($read_ary[$i])) .
515 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
517 /* Store the raw IMAP reply */
518 $boxes[$g]["raw"] = $read_ary[$i];
520 /* Count number of delimiters ($delimiter) in folder name */
521 $mailbox = find_mailbox_name($read_ary[$i]);
522 $dm_count = substr_count($mailbox, $delimiter);
523 if (substr($mailbox, -1) == $delimiter) {
524 /* If name ends in delimiter - decrement count by one */
528 /* Format folder name, but only if it's a INBOX.* or have */
530 $boxesbyname[$mailbox] = $g;
531 $parentfolder = readMailboxParent($mailbox, $delimiter);
532 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
533 (ereg('^'.$folder_prefix, $mailbox)) ||
534 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
536 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
539 $boxes[$g]['formatted'] = '';
541 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
544 $boxes[$g]['formatted'] = $mailbox;
547 $boxes[$g]["unformatted-dm"] = $mailbox;
548 if (substr($mailbox, -1) == $delimiter) {
549 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
551 $boxes[$g]['unformatted'] = $mailbox;
552 $boxes[$g]['unformatted-disp'] =
553 ereg_replace('^' . $folder_prefix, '', $mailbox);
554 $boxes[$g]['id'] = $g;
556 /** Now lets get the flags for this mailbox **/
557 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
558 TRUE, $response, $message);
560 /* Another workaround for EIMS */
561 if (isset($read_mlbx[1]) &&
562 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
563 $read_mlbx[0], $regs)) {
564 $read_mlbx[0] = $regs[1] . '"' .
565 addslashes(trim($read_mlbx[1])) .
569 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
570 $flags = substr($flags, 0, strpos($flags, ')'));
571 $flags = str_replace('\\', '', $flags);
572 $flags = trim(strtolower($flags));
574 $boxes[$g]['flags'] = explode(' ', $flags);
576 $boxes[$g]['flags'] = array();
581 if(is_array($boxes)) {
582 $boxes = ary_sort ($boxes, 'unformatted', 1);