c221dbd91653792d04c543455a4601f5e03865bf
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 ( $imap_server_type == 'uw' ) {
26 $i = strpos( $sent_folder, $delimiter, strlen( $folder_prefix ) );
31 $boxs = $box . $delimiter;
32 // Skip next second delimiter
33 $i = strpos( $sent_folder, $delimiter );
34 $i = strpos( $sent_folder, $delimiter, $i +
1 );
42 $ret = ( (strtolower($box) == 'inbox') ||
43 ( substr( $trash_folder, 0, $i ) == substr( $boxs, 0, $i ) &&
45 ( substr( $sent_folder, 0, $i ) == substr( $boxs, 0, $i ) &&
47 ($box == $draft_folder &&
54 /*************************
55 ** Expunges a mailbox **
56 *************************/
57 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = TRUE)
59 $read = sqimap_run_command($imap_stream, 'EXPUNGE',
60 $handle_errors, $response, $message);
64 /******************************************************************************
65 ** Checks whether or not the specified mailbox exists
66 ******************************************************************************/
67 function sqimap_mailbox_exists ($imap_stream, $mailbox)
69 if (! isset($mailbox)) {
72 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
73 TRUE, $response, $message);
74 return isset($mbx[0]);
77 /******************************************************************************
79 ******************************************************************************/
80 function sqimap_mailbox_select ($imap_stream, $mailbox,
81 $hide=TRUE, $recent=false)
85 if ( $mailbox == 'None' ) {
89 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
90 TRUE, $response, $message);
92 for ($i=0; $i<count($read); $i++
) {
93 if (strpos(strtolower($read[$i]), 'recent')) {
94 $r = explode(' ', $read[$i]);
100 $tmp = sqimap_run_command($imap_stream, 'EXPUNGE',
107 /******************************************************************************
109 ******************************************************************************/
110 function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
113 if (strtolower($type) == 'noselect') {
114 $mailbox = $mailbox.$delimiter;
116 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
117 TRUE, $response, $message);
119 sqimap_subscribe ($imap_stream, $mailbox);
124 /******************************************************************************
125 ** Subscribes to an existing folder
126 ******************************************************************************/
127 function sqimap_subscribe ($imap_stream, $mailbox)
129 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
130 TRUE, $response, $message);
135 /******************************************************************************
136 ** Unsubscribes to an existing folder
137 ******************************************************************************/
138 function sqimap_unsubscribe ($imap_stream, $mailbox)
140 global $imap_server_type;
142 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
143 TRUE, $response, $message);
148 /******************************************************************************
149 ** This function simply deletes the given folder
150 ******************************************************************************/
151 function sqimap_mailbox_delete ($imap_stream, $mailbox)
153 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
154 TRUE, $response, $message);
155 sqimap_unsubscribe ($imap_stream, $mailbox);
158 /***********************************************************************
159 ** Determines if the user is subscribed to the folder or not
160 **********************************************************************/
161 function sqimap_mailbox_is_subscribed($imap_stream, $folder)
163 $boxes = sqimap_mailbox_list ($imap_stream);
164 foreach ($boxes as $ref) {
165 if ($ref['unformatted'] == $folder) {
175 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
177 if ( $old_name <> $new_name ) {
181 if ( substr( $old_name, -1 ) == $delimiter ) {
182 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
183 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
184 $postfix = $delimiter;
185 $boxes = sqimap_mailbox_list($imap_stream);
191 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
192 $data = sqimap_run_command($imap_stream, $cmd,
193 TRUE, $response, $message);
194 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
195 sqimap_subscribe($imap_stream, $new_name.$postfix);
198 // Sub-unsub subfolders
199 $l = strlen( $old_name ) +
1;
201 foreach ( $boxes as $box ) {
202 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
203 sqimap_unsubscribe($imap_stream, $box[$p]);
204 sqimap_subscribe($imap_stream,
205 $new_name . $delimiter . substr( $box[$p], $l ) );
214 /******************************************************************************
215 ** Formats a mailbox into 4 parts for the $boxes array
217 ** The four parts are:
219 ** raw - Raw LIST/LSUB response from the IMAP server
220 ** formatted - nicely formatted folder name
221 ** unformatted - unformatted, but with delimiter at end removed
222 ** unformatted-dm - folder name as it appears in raw response
223 ** unformatted-disp - unformatted without $folder_prefix
225 ******************************************************************************/
226 function sqimap_mailbox_parse ($line, $line_lsub)
228 global $folder_prefix, $delimiter;
230 /* Process each folder line */
231 for ($g=0; $g < count($line); $g++
) {
233 /* Store the raw IMAP reply */
234 if (isset($line[$g])) {
235 $boxes[$g]["raw"] = $line[$g];
238 $boxes[$g]["raw"] = "";
242 /* Count number of delimiters ($delimiter) in folder name */
243 $mailbox = trim($line_lsub[$g]);
244 $dm_count = substr_count($mailbox, $delimiter);
245 if (substr($mailbox, -1) == $delimiter) {
246 /* If name ends in delimiter - decrement count by one */
250 /* Format folder name, but only if it's a INBOX.* or have */
252 $boxesbyname[$mailbox] = $g;
253 $parentfolder = readMailboxParent($mailbox, $delimiter);
254 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
255 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
256 ( isset($boxesbyname[$parentfolder]) &&
257 (strlen($parentfolder) > 0) ) ) {
258 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
260 $boxes[$g]['formatted'] = str_repeat(" ", $indent);
263 $boxes[$g]['formatted'] = '';
265 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
268 $boxes[$g]['formatted'] = $mailbox;
271 $boxes[$g]['unformatted-dm'] = $mailbox;
272 if (substr($mailbox, -1) == $delimiter) {
273 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
275 $boxes[$g]['unformatted'] = $mailbox;
276 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
277 $mailbox = substr($mailbox, strlen($folder_prefix));
279 $boxes[$g]['unformatted-disp'] = $mailbox;
280 $boxes[$g]['id'] = $g;
282 $boxes[$g]['flags'] = array();
283 if (isset($line[$g])) {
284 ereg("\(([^)]*)\)",$line[$g],$regs);
285 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
287 $boxes[$g]['flags'] = explode(' ', $flags);
296 * Sorting function used to sort mailbox names.
297 * + Original patch from dave_michmerhuizen@yahoo.com
298 * + Allows case insensitivity when sorting folders
299 * + Takes care of the delimiter being sorted to the end, causing
300 * subfolders to be listed in below folders that are prefixed
301 * with their parent folders name.
302 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
303 * Without special sort function: foobar between foo and foo.bar
304 * With special sort function: foobar AFTER foo and foo.bar :)
306 function user_strcasecmp($a, $b) {
309 /* Calculate the length of some strings. */
310 $a_length = strlen($a);
311 $b_length = strlen($b);
312 $min_length = min($a_length, $b_length);
313 $delimiter_length = strlen($delimiter);
315 /* Set the initial result value. */
318 /* Check the strings... */
319 for ($c = 0; $c < $min_length; ++
$c) {
320 $a_del = substr($a, $c, $delimiter_length);
321 $b_del = substr($b, $c, $delimiter_length);
323 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
325 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
327 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
330 $result = strcasecmp($a{$c}, $b{$c});
338 /* If one string is a prefix of the other... */
340 if ($a_length < $b_length) {
342 } else if ($a_length > $b_length) {
351 /******************************************************************************
352 ** Returns sorted mailbox lists in several different ways.
353 ** See comment on sqimap_mailbox_parse() for info about the returned array.
354 ******************************************************************************/
355 function sqimap_mailbox_list ($imap_stream) {
356 global $data_dir, $username, $list_special_folders_first,
357 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
358 $move_to_trash, $move_to_sent, $save_as_draft,
361 $inbox_in_list = false;
362 $inbox_subscribed = false;
364 require_once('../src/load_prefs.php');
365 require_once('../functions/array.php');
368 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
369 TRUE, $response, $message);
371 /* Section about removing the last element was removed */
372 /* We don't return "* OK" anymore from sqimap_read_data */
374 $sorted_lsub_ary = array();
375 for ($i=0;$i < count($lsub_ary); $i++
) {
376 /* Workaround for EIMS */
377 /* Doesn't work if the mailbox name is multiple lines */
378 if (isset($lsub_ary[$i +
1]) &&
379 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
380 $lsub_ary[$i], $regs)) {
382 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
385 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
386 $sorted_lsub_ary[] = $temp_mailbox_name;
387 if (strtoupper($temp_mailbox_name) == 'INBOX') {
388 $inbox_subscribed = TRUE;
392 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
393 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
394 $new_ary[] = $sorted_lsub_ary[$i];
397 $sorted_lsub_ary = $new_ary;
398 if (isset($sorted_lsub_ary)) {
399 usort($sorted_lsub_ary, 'user_strcasecmp');
403 $sorted_list_ary = array();
404 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
405 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
406 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
409 $mbx = $sorted_lsub_ary[$i];
412 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
413 TRUE, $response, $message);
414 /* Another workaround for EIMS */
415 if (isset($read[1]) &&
416 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
418 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
422 if (isset($sorted_list_ary[$i])) {
423 $sorted_list_ary[$i] = '';
426 if (isset($read[0])) {
427 $sorted_list_ary[$i] = $read[0];
430 $sorted_list_ary[$i] = '';
433 if (isset($sorted_list_ary[$i]) &&
434 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
435 $inbox_in_list = TRUE;
440 * Just in case they're not subscribed to their inbox,
441 * we'll get it for them anyway
443 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
444 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
445 TRUE, $response, $message);
446 /* Another workaround for EIMS */
447 if (isset($inbox_ary[1]) &&
448 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
449 $inbox_ary[0], $regs)) {
450 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
454 $sorted_list_ary[] = $inbox_ary[0];
455 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
458 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
460 /** Now, lets sort for special folders **/
461 $boxesnew = $used = array();
464 foreach ( $boxes as $k => $box ) {
465 if ( strtolower($box['unformatted']) == 'inbox') {
473 /* List special folders and their subfolders, if requested. */
474 if ($list_special_folders_first == TRUE) {
476 foreach ( $boxes as $k => $box ) {
478 isSpecialMailbox( $box['unformatted'] ) ) {
486 /* Rest of the folders */
487 foreach ( $boxes as $k => $box ) {
497 * Returns a list of all folders, subscribed or not
499 function sqimap_mailbox_list_all ($imap_stream)
501 global $list_special_folders_first, $folder_prefix;
504 if (!function_exists('ary_sort')) {
505 include_once('../functions/array.php');
508 $ssid = sqimap_session_id();
509 $lsid = strlen( $ssid );
510 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
511 $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
515 for ($i = 0; $i < count($read_ary); $i++
) {
516 /* Another workaround for EIMS */
517 if (isset($read_ary[$i +
1]) &&
518 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
519 $read_ary[$i], $regs)) {
521 $read_ary[$i] = $regs[1] . '"' .
522 addslashes(trim($read_ary[$i])) .
525 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
527 /* Store the raw IMAP reply */
528 $boxes[$g]["raw"] = $read_ary[$i];
530 /* Count number of delimiters ($delimiter) in folder name */
531 $mailbox = find_mailbox_name($read_ary[$i]);
532 $dm_count = substr_count($mailbox, $delimiter);
533 if (substr($mailbox, -1) == $delimiter) {
534 /* If name ends in delimiter - decrement count by one */
538 /* Format folder name, but only if it's a INBOX.* or have */
540 $boxesbyname[$mailbox] = $g;
541 $parentfolder = readMailboxParent($mailbox, $delimiter);
542 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
543 (ereg('^'.$folder_prefix, $mailbox)) ||
544 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
546 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
549 $boxes[$g]['formatted'] = '';
551 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
554 $boxes[$g]['formatted'] = $mailbox;
557 $boxes[$g]["unformatted-dm"] = $mailbox;
558 if (substr($mailbox, -1) == $delimiter) {
559 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
561 $boxes[$g]['unformatted'] = $mailbox;
562 $boxes[$g]['unformatted-disp'] =
563 ereg_replace('^' . $folder_prefix, '', $mailbox);
564 $boxes[$g]['id'] = $g;
566 /** Now lets get the flags for this mailbox **/
567 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
568 TRUE, $response, $message);
570 /* Another workaround for EIMS */
571 if (isset($read_mlbx[1]) &&
572 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
573 $read_mlbx[0], $regs)) {
574 $read_mlbx[0] = $regs[1] . '"' .
575 addslashes(trim($read_mlbx[1])) .
579 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
580 $flags = substr($flags, 0, strpos($flags, ')'));
581 $flags = str_replace('\\', '', $flags);
582 $flags = trim(strtolower($flags));
584 $boxes[$g]['flags'] = explode(' ', $flags);
586 $boxes[$g]['flags'] = array();
591 if(is_array($boxes)) {
592 $boxes = ary_sort ($boxes, 'unformatted', 1);