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 /*************************
15 ** Expunges a mailbox **
16 *************************/
17 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true)
19 fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
20 $read = sqimap_read_data($imap_stream, sqimap_session_id(),
21 $handle_errors, $response, $message);
25 /******************************************************************************
26 ** Checks whether or not the specified mailbox exists
27 ******************************************************************************/
28 function sqimap_mailbox_exists ($imap_stream, $mailbox)
30 if (! isset($mailbox)) {
33 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
34 $mbx = sqimap_read_data($imap_stream, sqimap_session_id(),
35 true, $response, $message);
36 return isset($mbx[0]);
39 /******************************************************************************
41 ******************************************************************************/
42 function sqimap_mailbox_select ($imap_stream, $mailbox,
43 $hide=true, $recent=false)
47 if ( $mailbox == 'None' ) {
51 fputs ($imap_stream, sqimap_session_id() . " SELECT \"$mailbox\"\r\n");
52 $read = sqimap_read_data($imap_stream, sqimap_session_id(),
53 true, $response, $message);
55 for ($i=0; $i<count($read); $i++
) {
56 if (strpos(strtolower($read[$i]), 'recent')) {
57 $r = explode(' ', $read[$i]);
63 fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
64 $tmp = sqimap_read_data($imap_stream, sqimap_session_id(),
71 /******************************************************************************
73 ******************************************************************************/
74 function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
77 if (strtolower($type) == 'noselect') {
78 $mailbox = $mailbox.$delimiter;
80 fputs ($imap_stream, sqimap_session_id() . " CREATE \"$mailbox\"\r\n");
81 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
82 true, $response, $message);
84 sqimap_subscribe ($imap_stream, $mailbox);
89 /******************************************************************************
90 ** Subscribes to an existing folder
91 ******************************************************************************/
92 function sqimap_subscribe ($imap_stream, $mailbox)
94 fputs ($imap_stream, sqimap_session_id() . " SUBSCRIBE \"$mailbox\"\r\n");
95 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
96 true, $response, $message);
101 /******************************************************************************
102 ** Unsubscribes to an existing folder
103 ******************************************************************************/
104 function sqimap_unsubscribe ($imap_stream, $mailbox)
106 global $imap_server_type;
108 fputs ($imap_stream, sqimap_session_id() . " UNSUBSCRIBE \"$mailbox\"\r\n");
109 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
110 true, $response, $message);
115 /******************************************************************************
116 ** This function simply deletes the given folder
117 ******************************************************************************/
118 function sqimap_mailbox_delete ($imap_stream, $mailbox)
120 fputs ($imap_stream, sqimap_session_id() . " DELETE \"$mailbox\"\r\n");
121 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
122 true, $response, $message);
123 sqimap_unsubscribe ($imap_stream, $mailbox);
126 /***********************************************************************
127 ** Determines if the user is subscribed to the folder or not
128 **********************************************************************/
129 function sqimap_mailbox_is_subscribed($imap_stream, $folder)
131 $boxes = sqimap_mailbox_list ($imap_stream);
132 foreach ($boxes as $ref) {
133 if ($ref['unformatted'] == $folder) {
143 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
145 if ( $old_name <> $new_name ) {
149 if ( substr( $old_name, -1 ) == $delimiter ) {
150 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
151 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
152 $postfix = $delimiter;
153 $boxes = sqimap_mailbox_list($imap_stream);
159 $cmd = sqimap_session_id() . " RENAME \"" . quoteIMAP($old_name) . "\" \"" .
160 quoteIMAP($new_name) . "\"\r\n";
161 fputs($imap_stream, $cmd);
162 $data = sqimap_read_data($imap_stream, sqimap_session_id(),
163 TRUE, $response, $message);
164 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
165 sqimap_subscribe($imap_stream, $new_name.$postfix);
168 // Sub-unsub subfolders
169 $l = strlen( $old_name ) +
1;
171 foreach ( $boxes as $box ) {
172 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
173 sqimap_unsubscribe($imap_stream, $box[$p]);
174 sqimap_subscribe($imap_stream,
175 $new_name . $delimiter . substr( $box[$p], $l ) );
184 /******************************************************************************
185 ** Formats a mailbox into 4 parts for the $boxes array
187 ** The four parts are:
189 ** raw - Raw LIST/LSUB response from the IMAP server
190 ** formatted - nicely formatted folder name
191 ** unformatted - unformatted, but with delimiter at end removed
192 ** unformatted-dm - folder name as it appears in raw response
193 ** unformatted-disp - unformatted without $folder_prefix
195 ******************************************************************************/
196 function sqimap_mailbox_parse ($line, $line_lsub)
198 global $folder_prefix, $delimiter;
200 /* Process each folder line */
201 for ($g=0; $g < count($line); $g++
) {
203 /* Store the raw IMAP reply */
204 if (isset($line[$g])) {
205 $boxes[$g]["raw"] = $line[$g];
208 $boxes[$g]["raw"] = "";
212 /* Count number of delimiters ($delimiter) in folder name */
213 $mailbox = trim($line_lsub[$g]);
214 $dm_count = substr_count($mailbox, $delimiter);
215 if (substr($mailbox, -1) == $delimiter) {
216 /* If name ends in delimiter - decrement count by one */
220 /* Format folder name, but only if it's a INBOX.* or have */
222 $boxesbyname[$mailbox] = $g;
223 $parentfolder = readMailboxParent($mailbox, $delimiter);
224 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
225 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
226 ( isset($boxesbyname[$parentfolder]) &&
227 (strlen($parentfolder) > 0) ) ) {
228 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
230 $boxes[$g]["formatted"] = str_repeat(" ", $indent);
233 $boxes[$g]["formatted"] = '';
235 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
238 $boxes[$g]["formatted"] = $mailbox;
241 $boxes[$g]['unformatted-dm'] = $mailbox;
242 if (substr($mailbox, -1) == $delimiter) {
243 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
245 $boxes[$g]['unformatted'] = $mailbox;
246 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
247 $mailbox = substr($mailbox, strlen($folder_prefix));
249 $boxes[$g]['unformatted-disp'] = $mailbox;
250 $boxes[$g]['id'] = $g;
252 $boxes[$g]['flags'] = array();
253 if (isset($line[$g])) {
254 ereg("\(([^)]*)\)",$line[$g],$regs);
255 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
257 $boxes[$g]['flags'] = explode(' ', $flags);
266 * Sorting function used to sort mailbox names.
267 * + Original patch from dave_michmerhuizen@yahoo.com
268 * + Allows case insensitivity when sorting folders
269 * + Takes care of the delimiter being sorted to the end, causing
270 * subfolders to be listed in below folders that are prefixed
271 * with their parent folders name.
272 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
273 * Without special sort function: foobar between foo and foo.bar
274 * With special sort function: foobar AFTER foo and foo.bar :)
276 function user_strcasecmp($a, $b) {
279 /* Calculate the length of some strings. */
280 $a_length = strlen($a);
281 $b_length = strlen($b);
282 $min_length = min($a_length, $b_length);
283 $delimiter_length = strlen($delimiter);
285 /* Set the initial result value. */
288 /* Check the strings... */
289 for ($c = 0; $c < $min_length; ++
$c) {
290 $a_del = substr($a, $c, $delimiter_length);
291 $b_del = substr($b, $c, $delimiter_length);
293 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
295 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
297 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
300 $result = strcasecmp($a{$c}, $b{$c});
308 /* If one string is a prefix of the other... */
310 if ($a_length < $b_length) {
312 } else if ($a_length > $b_length) {
321 /******************************************************************************
322 ** Returns sorted mailbox lists in several different ways.
323 ** See comment on sqimap_mailbox_parse() for info about the returned array.
324 ******************************************************************************/
325 function sqimap_mailbox_list ($imap_stream) {
326 global $data_dir, $username, $list_special_folders_first;
327 global $folder_prefix, $trash_folder, $sent_folder, $draft_folder;
328 global $move_to_trash, $move_to_sent, $save_as_draft;
331 $inbox_in_list = false;
332 $inbox_subscribed = false;
334 require_once('../src/load_prefs.php');
335 require_once('../functions/array.php');
338 fputs ($imap_stream, sqimap_session_id() .
339 " LSUB \"$folder_prefix\" \"*\"\r\n");
340 $lsub_ary = sqimap_read_data ($imap_stream, sqimap_session_id(),
341 true, $response, $message);
343 /* Section about removing the last element was removed */
344 /* We don't return "* OK" anymore from sqimap_read_data */
346 $sorted_lsub_ary = array();
347 for ($i=0;$i < count($lsub_ary); $i++
) {
348 /* Workaround for EIMS */
349 /* Doesn't work if the mailbox name is multiple lines */
350 if (isset($lsub_ary[$i +
1]) &&
351 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
352 $lsub_ary[$i], $regs)) {
354 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
357 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
358 $sorted_lsub_ary[] = $temp_mailbox_name;
359 if (strtoupper($temp_mailbox_name) == 'INBOX') {
360 $inbox_subscribed = true;
364 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
365 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
366 $new_ary[] = $sorted_lsub_ary[$i];
369 $sorted_lsub_ary = $new_ary;
370 if (isset($sorted_lsub_ary)) {
371 usort($sorted_lsub_ary, 'user_strcasecmp');
375 $sorted_list_ary = array();
376 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
377 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
378 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
381 $mbx = $sorted_lsub_ary[$i];
384 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mbx\"\r\n");
385 $read = sqimap_read_data ($imap_stream, sqimap_session_id(),
386 true, $response, $message);
387 /* Another workaround for EIMS */
388 if (isset($read[1]) &&
389 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
391 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
395 if (isset($sorted_list_ary[$i])) {
396 $sorted_list_ary[$i] = "";
399 if (isset($read[0])) {
400 $sorted_list_ary[$i] = $read[0];
403 $sorted_list_ary[$i] = "";
406 if (isset($sorted_list_ary[$i]) &&
407 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == "INBOX") {
408 $inbox_in_list = true;
413 * Just in case they're not subscribed to their inbox,
414 * we'll get it for them anyway
416 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
417 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"INBOX\"\r\n");
418 $inbox_ary = sqimap_read_data ($imap_stream, sqimap_session_id(),
419 true, $response, $message);
420 /* Another workaround for EIMS */
421 if (isset($inbox_ary[1]) &&
422 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
423 $inbox_ary[0], $regs)) {
424 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
428 $sorted_list_ary[] = $inbox_ary[0];
429 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
432 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
434 /** Now, lets sort for special folders **/
438 for ($i = 0; $i < count($boxes); $i++
) {
439 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
440 $boxesnew[] = $boxes[$i];
446 /* List special folders and their subfolders, if requested. */
447 if ($list_special_folders_first == true) {
448 /* First list the trash folder. */
449 for ($i = 0 ; $i < count($boxes) ; $i++
) {
450 if ($move_to_trash &&
451 eregi('^' . quotemeta($trash_folder) . '(' .
452 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
453 $boxesnew[] = $boxes[$i];
458 /* Then list the sent folder. */
459 for ($i = 0 ; $i < count($boxes) ; $i++
) {
461 eregi('^' . quotemeta($sent_folder) . '(' .
462 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
463 $boxesnew[] = $boxes[$i];
468 /* Lastly, list the list the draft folder. */
469 for ($i = 0 ; $i < count($boxes) ; $i++
) {
470 if ($save_as_draft &&
471 eregi('^' . quotemeta($draft_folder) . '(' .
472 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
473 $boxesnew[] = $boxes[$i];
478 /* Put INBOX.* folders ahead of the rest. */
479 for ($i = 0; $i < count($boxes); $i++
) {
480 if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
481 (!isset($used[$i]) ||
$used[$i] == false)) {
482 $boxesnew[] = $boxes[$i];
488 /* Rest of the folders */
489 for ($i = 0; $i < count($boxes); $i++
) {
490 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
491 (!isset($used[$i]) ||
$used[$i] == false)) {
492 $boxesnew[] = $boxes[$i];
500 /******************************************************************************
501 ** Returns a list of all folders, subscribed or not
502 ******************************************************************************/
503 function sqimap_mailbox_list_all ($imap_stream)
505 global $list_special_folders_first, $folder_prefix;
508 if (!function_exists ("ary_sort")) {
509 include_once('../functions/array.php');
512 $ssid = sqimap_session_id();
513 $lsid = strlen( $ssid );
514 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
515 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
519 for ($i = 0; $i < count($read_ary); $i++
) {
520 /* Another workaround for EIMS */
521 if (isset($read_ary[$i +
1]) &&
522 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
523 $read_ary[$i], $regs)) {
525 $read_ary[$i] = $regs[1] . '"' .
526 addslashes(trim($read_ary[$i])) .
529 if (substr ($read_ary[$i], 0, $lsid) != $ssid ) {
531 /* Store the raw IMAP reply */
532 $boxes[$g]["raw"] = $read_ary[$i];
534 /* Count number of delimiters ($delimiter) in folder name */
535 $mailbox = find_mailbox_name($read_ary[$i]);
536 $dm_count = substr_count($mailbox, $delimiter);
537 if (substr($mailbox, -1) == $delimiter) {
538 /* If name ends in delimiter - decrement count by one */
542 /* Format folder name, but only if it's a INBOX.* or have */
544 $boxesbyname[$mailbox] = $g;
545 $parentfolder = readMailboxParent($mailbox, $delimiter);
546 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
547 (ereg('^'.$folder_prefix, $mailbox)) ||
548 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
550 $boxes[$g]["formatted"] = str_repeat(" ", $dm_count);
553 $boxes[$g]["formatted"] = '';
555 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
558 $boxes[$g]["formatted"] = $mailbox;
561 $boxes[$g]["unformatted-dm"] = $mailbox;
562 if (substr($mailbox, -1) == $delimiter) {
563 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
565 $boxes[$g]["unformatted"] = $mailbox;
566 $boxes[$g]["unformatted-disp"] =
567 ereg_replace('^' . $folder_prefix, '', $mailbox);
568 $boxes[$g]["id"] = $g;
570 /** Now lets get the flags for this mailbox **/
571 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
572 $read_mlbx = sqimap_read_data ($imap_stream, sqimap_session_id(),
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);
592 $boxes[$g]['flags'] = array();
597 if(is_array($boxes)) {
598 $boxes = ary_sort ($boxes, 'unformatted', 1);