e04d5bcf37e153719c2fec79f9b4f06f3adf139b
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 ) );
54 $ret = do_hook_function( 'special_mailbox', $box );
61 /*************************
62 ** Expunges a mailbox **
63 *************************/
64 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = TRUE)
66 $read = sqimap_run_command($imap_stream, 'EXPUNGE',
67 $handle_errors, $response, $message);
71 /******************************************************************************
72 ** Checks whether or not the specified mailbox exists
73 ******************************************************************************/
74 function sqimap_mailbox_exists ($imap_stream, $mailbox)
76 if (! isset($mailbox)) {
79 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
80 TRUE, $response, $message);
81 return isset($mbx[0]);
84 /******************************************************************************
86 ******************************************************************************/
87 function sqimap_mailbox_select ($imap_stream, $mailbox,
88 $hide=TRUE, $recent=false)
92 if ( $mailbox == 'None' ) {
96 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
97 TRUE, $response, $message);
99 for ($i=0; $i<count($read); $i++
) {
100 if (strpos(strtolower($read[$i]), 'recent')) {
101 $r = explode(' ', $read[$i]);
107 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE',
116 /******************************************************************************
118 ******************************************************************************/
119 function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
122 if (strtolower($type) == 'noselect') {
123 $mailbox = $mailbox.$delimiter;
125 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
126 TRUE, $response, $message);
128 sqimap_subscribe ($imap_stream, $mailbox);
133 /******************************************************************************
134 ** Subscribes to an existing folder
135 ******************************************************************************/
136 function sqimap_subscribe ($imap_stream, $mailbox)
138 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
139 TRUE, $response, $message);
144 /******************************************************************************
145 ** Unsubscribes to an existing folder
146 ******************************************************************************/
147 function sqimap_unsubscribe ($imap_stream, $mailbox)
149 global $imap_server_type;
151 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
152 TRUE, $response, $message);
157 /******************************************************************************
158 ** This function simply deletes the given folder
159 ******************************************************************************/
160 function sqimap_mailbox_delete ($imap_stream, $mailbox)
162 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
163 TRUE, $response, $message);
164 sqimap_unsubscribe ($imap_stream, $mailbox);
167 /***********************************************************************
168 ** Determines if the user is subscribed to the folder or not
169 **********************************************************************/
170 function sqimap_mailbox_is_subscribed($imap_stream, $folder)
172 $boxes = sqimap_mailbox_list ($imap_stream);
173 foreach ($boxes as $ref) {
174 if ($ref['unformatted'] == $folder) {
184 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
186 if ( $old_name <> $new_name ) {
190 if ( substr( $old_name, -1 ) == $delimiter ) {
191 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
192 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
193 $postfix = $delimiter;
194 $boxes = sqimap_mailbox_list($imap_stream);
200 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
201 $data = sqimap_run_command($imap_stream, $cmd,
202 TRUE, $response, $message);
203 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
204 sqimap_subscribe($imap_stream, $new_name.$postfix);
207 // Sub-unsub subfolders
208 $l = strlen( $old_name ) +
1;
210 foreach ( $boxes as $box ) {
211 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
212 sqimap_unsubscribe($imap_stream, $box[$p]);
213 sqimap_subscribe($imap_stream,
214 $new_name . $delimiter . substr( $box[$p], $l ) );
223 /******************************************************************************
224 ** Formats a mailbox into 4 parts for the $boxes array
226 ** The four parts are:
228 ** raw - Raw LIST/LSUB response from the IMAP server
229 ** formatted - nicely formatted folder name
230 ** unformatted - unformatted, but with delimiter at end removed
231 ** unformatted-dm - folder name as it appears in raw response
232 ** unformatted-disp - unformatted without $folder_prefix
234 ******************************************************************************/
235 function sqimap_mailbox_parse ($line, $line_lsub)
237 global $folder_prefix, $delimiter;
239 /* Process each folder line */
240 for ($g=0; $g < count($line); $g++
) {
242 /* Store the raw IMAP reply */
243 if (isset($line[$g])) {
244 $boxes[$g]["raw"] = $line[$g];
247 $boxes[$g]["raw"] = "";
251 /* Count number of delimiters ($delimiter) in folder name */
252 $mailbox = trim($line_lsub[$g]);
253 $dm_count = substr_count($mailbox, $delimiter);
254 if (substr($mailbox, -1) == $delimiter) {
255 /* If name ends in delimiter - decrement count by one */
259 /* Format folder name, but only if it's a INBOX.* or have */
261 $boxesbyname[$mailbox] = $g;
262 $parentfolder = readMailboxParent($mailbox, $delimiter);
263 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
264 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
265 ( isset($boxesbyname[$parentfolder]) &&
266 (strlen($parentfolder) > 0) ) ) {
267 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
269 $boxes[$g]['formatted'] = str_repeat(" ", $indent);
272 $boxes[$g]['formatted'] = '';
274 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
277 $boxes[$g]['formatted'] = $mailbox;
280 $boxes[$g]['unformatted-dm'] = $mailbox;
281 if (substr($mailbox, -1) == $delimiter) {
282 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
284 $boxes[$g]['unformatted'] = $mailbox;
285 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
286 $mailbox = substr($mailbox, strlen($folder_prefix));
288 $boxes[$g]['unformatted-disp'] = $mailbox;
289 $boxes[$g]['id'] = $g;
291 $boxes[$g]['flags'] = array();
292 if (isset($line[$g])) {
293 ereg("\(([^)]*)\)",$line[$g],$regs);
294 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
296 $boxes[$g]['flags'] = explode(' ', $flags);
305 * Sorting function used to sort mailbox names.
306 * + Original patch from dave_michmerhuizen@yahoo.com
307 * + Allows case insensitivity when sorting folders
308 * + Takes care of the delimiter being sorted to the end, causing
309 * subfolders to be listed in below folders that are prefixed
310 * with their parent folders name.
311 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
312 * Without special sort function: foobar between foo and foo.bar
313 * With special sort function: foobar AFTER foo and foo.bar :)
315 function user_strcasecmp($a, $b) {
318 /* Calculate the length of some strings. */
319 $a_length = strlen($a);
320 $b_length = strlen($b);
321 $min_length = min($a_length, $b_length);
322 $delimiter_length = strlen($delimiter);
324 /* Set the initial result value. */
327 /* Check the strings... */
328 for ($c = 0; $c < $min_length; ++
$c) {
329 $a_del = substr($a, $c, $delimiter_length);
330 $b_del = substr($b, $c, $delimiter_length);
332 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
334 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
336 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
339 $result = strcasecmp($a{$c}, $b{$c});
347 /* If one string is a prefix of the other... */
349 if ($a_length < $b_length) {
351 } else if ($a_length > $b_length) {
360 /******************************************************************************
361 ** Returns sorted mailbox lists in several different ways.
362 ** See comment on sqimap_mailbox_parse() for info about the returned array.
363 ******************************************************************************/
364 function sqimap_mailbox_list ($imap_stream) {
366 global $data_dir, $username, $list_special_folders_first,
367 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
368 $move_to_trash, $move_to_sent, $save_as_draft,
371 $inbox_in_list = $inbox_subscribed = FALSE;
373 require_once('../src/load_prefs.php');
374 require_once('../functions/array.php');
377 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
378 TRUE, $response, $message);
380 /* Section about removing the last element was removed */
381 /* We don't return "* OK" anymore from sqimap_read_data */
383 $sorted_lsub_ary = array();
384 for ($i=0;$i < count($lsub_ary); $i++
) {
385 /* Workaround for EIMS */
386 /* Doesn't work if the mailbox name is multiple lines */
387 if (isset($lsub_ary[$i +
1]) &&
388 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
389 $lsub_ary[$i], $regs)) {
391 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
394 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
395 $sorted_lsub_ary[] = $temp_mailbox_name;
396 if (strtoupper($temp_mailbox_name) == 'INBOX') {
397 $inbox_subscribed = TRUE;
401 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
402 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
403 $new_ary[] = $sorted_lsub_ary[$i];
406 $sorted_lsub_ary = $new_ary;
407 if (isset($sorted_lsub_ary)) {
408 usort($sorted_lsub_ary, 'user_strcasecmp');
412 $sorted_list_ary = array();
413 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
414 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
415 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
418 $mbx = $sorted_lsub_ary[$i];
421 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
422 TRUE, $response, $message);
423 /* Another workaround for EIMS */
424 if (isset($read[1]) &&
425 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
427 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
431 if (isset($sorted_list_ary[$i])) {
432 $sorted_list_ary[$i] = '';
435 if (isset($read[0])) {
436 $sorted_list_ary[$i] = $read[0];
439 $sorted_list_ary[$i] = '';
442 if (isset($sorted_list_ary[$i]) &&
443 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
444 $inbox_in_list = TRUE;
449 * Just in case they're not subscribed to their inbox,
450 * we'll get it for them anyway
452 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
453 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
454 TRUE, $response, $message);
455 /* Another workaround for EIMS */
456 if (isset($inbox_ary[1]) &&
457 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
458 $inbox_ary[0], $regs)) {
459 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
463 $sorted_list_ary[] = $inbox_ary[0];
464 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
467 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
469 /** Now, lets sort for special folders **/
470 $boxesnew = $used = array();
473 foreach ( $boxes as $k => $box ) {
474 if ( strtolower($box['unformatted']) == 'inbox') {
482 /* List special folders and their subfolders, if requested. */
483 if ($list_special_folders_first == TRUE) {
485 foreach ( $boxes as $k => $box ) {
487 isSpecialMailbox( $box['unformatted'] ) ) {
495 /* Rest of the folders */
496 foreach ( $boxes as $k => $box ) {
507 * Returns a list of all folders, subscribed or not
509 function sqimap_mailbox_list_all ($imap_stream)
511 global $list_special_folders_first, $folder_prefix;
514 require_once('../functions/array.php');
516 $ssid = sqimap_session_id();
517 $lsid = strlen( $ssid );
518 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
519 $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
523 for ($i = 0; $i < count($read_ary); $i++
) {
524 /* Another workaround for EIMS */
525 if (isset($read_ary[$i +
1]) &&
526 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
527 $read_ary[$i], $regs)) {
529 $read_ary[$i] = $regs[1] . '"' .
530 addslashes(trim($read_ary[$i])) .
533 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
535 /* Store the raw IMAP reply */
536 $boxes[$g]['raw'] = $read_ary[$i];
538 /* Count number of delimiters ($delimiter) in folder name */
539 $mailbox = find_mailbox_name($read_ary[$i]);
540 $dm_count = substr_count($mailbox, $delimiter);
541 if (substr($mailbox, -1) == $delimiter) {
542 /* If name ends in delimiter - decrement count by one */
546 /* Format folder name, but only if it's a INBOX.* or have */
548 $boxesbyname[$mailbox] = $g;
549 $parentfolder = readMailboxParent($mailbox, $delimiter);
550 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
551 (ereg('^'.$folder_prefix, $mailbox)) ||
552 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
554 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
557 $boxes[$g]['formatted'] = '';
559 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
562 $boxes[$g]['formatted'] = $mailbox;
565 $boxes[$g]["unformatted-dm"] = $mailbox;
566 if (substr($mailbox, -1) == $delimiter) {
567 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
569 $boxes[$g]['unformatted'] = $mailbox;
570 $boxes[$g]['unformatted-disp'] =
571 ereg_replace('^' . $folder_prefix, '', $mailbox);
572 $boxes[$g]['id'] = $g;
574 /** Now lets get the flags for this mailbox **/
575 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
576 TRUE, $response, $message);
578 /* Another workaround for EIMS */
579 if (isset($read_mlbx[1]) &&
580 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
581 $read_mlbx[0], $regs)) {
582 $read_mlbx[0] = $regs[1] . '"' .
583 addslashes(trim($read_mlbx[1])) .
587 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
588 $flags = substr($flags, 0, strpos($flags, ')'));
589 $flags = str_replace('\\', '', $flags);
590 $flags = trim(strtolower($flags));
592 $boxes[$g]['flags'] = explode(' ', $flags);
594 $boxes[$g]['flags'] = array();
599 if(is_array($boxes)) {
600 $boxes = ary_sort ($boxes, 'unformatted', 1);