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 $read = sqimap_run_command($imap_stream, 'EXPUNGE',
20 $handle_errors, $response, $message);
24 /******************************************************************************
25 ** Checks whether or not the specified mailbox exists
26 ******************************************************************************/
27 function sqimap_mailbox_exists ($imap_stream, $mailbox)
29 if (! isset($mailbox)) {
32 $mbx = sqimap_run_command($imap_stream, "LIST \"\" \"$mailbox\"",
33 true, $response, $message);
34 return isset($mbx[0]);
37 /******************************************************************************
39 ******************************************************************************/
40 function sqimap_mailbox_select ($imap_stream, $mailbox,
41 $hide=true, $recent=false)
45 if ( $mailbox == 'None' ) {
49 $read = sqimap_run_command($imap_stream, "SELECT \"$mailbox\"",
50 true, $response, $message);
52 for ($i=0; $i<count($read); $i++
) {
53 if (strpos(strtolower($read[$i]), 'recent')) {
54 $r = explode(' ', $read[$i]);
60 $tmp = sqimap_run_command($imap_stream, "EXPUNGE",
67 /******************************************************************************
69 ******************************************************************************/
70 function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
73 if (strtolower($type) == 'noselect') {
74 $mailbox = $mailbox.$delimiter;
76 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
77 true, $response, $message);
79 sqimap_subscribe ($imap_stream, $mailbox);
84 /******************************************************************************
85 ** Subscribes to an existing folder
86 ******************************************************************************/
87 function sqimap_subscribe ($imap_stream, $mailbox)
89 $read_ary = sqimap_run_command($imap_stream, " SUBSCRIBE \"$mailbox\"",
90 true, $response, $message);
95 /******************************************************************************
96 ** Unsubscribes to an existing folder
97 ******************************************************************************/
98 function sqimap_unsubscribe ($imap_stream, $mailbox)
100 global $imap_server_type;
102 $read_ary = sqimap_run_command($imap_stream, " UNSUBSCRIBE \"$mailbox\"",
103 true, $response, $message);
108 /******************************************************************************
109 ** This function simply deletes the given folder
110 ******************************************************************************/
111 function sqimap_mailbox_delete ($imap_stream, $mailbox)
113 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
114 true, $response, $message);
115 sqimap_unsubscribe ($imap_stream, $mailbox);
118 /***********************************************************************
119 ** Determines if the user is subscribed to the folder or not
120 **********************************************************************/
121 function sqimap_mailbox_is_subscribed($imap_stream, $folder)
123 $boxes = sqimap_mailbox_list ($imap_stream);
124 foreach ($boxes as $ref) {
125 if ($ref['unformatted'] == $folder) {
135 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
137 if ( $old_name <> $new_name ) {
141 if ( substr( $old_name, -1 ) == $delimiter ) {
142 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
143 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
144 $postfix = $delimiter;
145 $boxes = sqimap_mailbox_list($imap_stream);
151 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
152 $data = sqimap_run_command($imap_stream, $cmd,
153 TRUE, $response, $message);
154 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
155 sqimap_subscribe($imap_stream, $new_name.$postfix);
158 // Sub-unsub subfolders
159 $l = strlen( $old_name ) +
1;
161 foreach ( $boxes as $box ) {
162 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
163 sqimap_unsubscribe($imap_stream, $box[$p]);
164 sqimap_subscribe($imap_stream,
165 $new_name . $delimiter . substr( $box[$p], $l ) );
174 /******************************************************************************
175 ** Formats a mailbox into 4 parts for the $boxes array
177 ** The four parts are:
179 ** raw - Raw LIST/LSUB response from the IMAP server
180 ** formatted - nicely formatted folder name
181 ** unformatted - unformatted, but with delimiter at end removed
182 ** unformatted-dm - folder name as it appears in raw response
183 ** unformatted-disp - unformatted without $folder_prefix
185 ******************************************************************************/
186 function sqimap_mailbox_parse ($line, $line_lsub)
188 global $folder_prefix, $delimiter;
190 /* Process each folder line */
191 for ($g=0; $g < count($line); $g++
) {
193 /* Store the raw IMAP reply */
194 if (isset($line[$g])) {
195 $boxes[$g]["raw"] = $line[$g];
198 $boxes[$g]["raw"] = "";
202 /* Count number of delimiters ($delimiter) in folder name */
203 $mailbox = trim($line_lsub[$g]);
204 $dm_count = substr_count($mailbox, $delimiter);
205 if (substr($mailbox, -1) == $delimiter) {
206 /* If name ends in delimiter - decrement count by one */
210 /* Format folder name, but only if it's a INBOX.* or have */
212 $boxesbyname[$mailbox] = $g;
213 $parentfolder = readMailboxParent($mailbox, $delimiter);
214 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
215 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
216 ( isset($boxesbyname[$parentfolder]) &&
217 (strlen($parentfolder) > 0) ) ) {
218 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
220 $boxes[$g]["formatted"] = str_repeat(" ", $indent);
223 $boxes[$g]["formatted"] = '';
225 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
228 $boxes[$g]["formatted"] = $mailbox;
231 $boxes[$g]['unformatted-dm'] = $mailbox;
232 if (substr($mailbox, -1) == $delimiter) {
233 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
235 $boxes[$g]['unformatted'] = $mailbox;
236 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
237 $mailbox = substr($mailbox, strlen($folder_prefix));
239 $boxes[$g]['unformatted-disp'] = $mailbox;
240 $boxes[$g]['id'] = $g;
242 $boxes[$g]['flags'] = array();
243 if (isset($line[$g])) {
244 ereg("\(([^)]*)\)",$line[$g],$regs);
245 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
247 $boxes[$g]['flags'] = explode(' ', $flags);
256 * Sorting function used to sort mailbox names.
257 * + Original patch from dave_michmerhuizen@yahoo.com
258 * + Allows case insensitivity when sorting folders
259 * + Takes care of the delimiter being sorted to the end, causing
260 * subfolders to be listed in below folders that are prefixed
261 * with their parent folders name.
262 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
263 * Without special sort function: foobar between foo and foo.bar
264 * With special sort function: foobar AFTER foo and foo.bar :)
266 function user_strcasecmp($a, $b) {
269 /* Calculate the length of some strings. */
270 $a_length = strlen($a);
271 $b_length = strlen($b);
272 $min_length = min($a_length, $b_length);
273 $delimiter_length = strlen($delimiter);
275 /* Set the initial result value. */
278 /* Check the strings... */
279 for ($c = 0; $c < $min_length; ++
$c) {
280 $a_del = substr($a, $c, $delimiter_length);
281 $b_del = substr($b, $c, $delimiter_length);
283 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
285 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
287 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
290 $result = strcasecmp($a{$c}, $b{$c});
298 /* If one string is a prefix of the other... */
300 if ($a_length < $b_length) {
302 } else if ($a_length > $b_length) {
311 /******************************************************************************
312 ** Returns sorted mailbox lists in several different ways.
313 ** See comment on sqimap_mailbox_parse() for info about the returned array.
314 ******************************************************************************/
315 function sqimap_mailbox_list ($imap_stream) {
316 global $data_dir, $username, $list_special_folders_first;
317 global $folder_prefix, $trash_folder, $sent_folder, $draft_folder;
318 global $move_to_trash, $move_to_sent, $save_as_draft;
321 $inbox_in_list = false;
322 $inbox_subscribed = false;
324 require_once('../src/load_prefs.php');
325 require_once('../functions/array.php');
328 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
329 true, $response, $message);
331 /* Section about removing the last element was removed */
332 /* We don't return "* OK" anymore from sqimap_read_data */
334 $sorted_lsub_ary = array();
335 for ($i=0;$i < count($lsub_ary); $i++
) {
336 /* Workaround for EIMS */
337 /* Doesn't work if the mailbox name is multiple lines */
338 if (isset($lsub_ary[$i +
1]) &&
339 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
340 $lsub_ary[$i], $regs)) {
342 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
345 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
346 $sorted_lsub_ary[] = $temp_mailbox_name;
347 if (strtoupper($temp_mailbox_name) == 'INBOX') {
348 $inbox_subscribed = true;
352 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
353 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
354 $new_ary[] = $sorted_lsub_ary[$i];
357 $sorted_lsub_ary = $new_ary;
358 if (isset($sorted_lsub_ary)) {
359 usort($sorted_lsub_ary, 'user_strcasecmp');
363 $sorted_list_ary = array();
364 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
365 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
366 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
369 $mbx = $sorted_lsub_ary[$i];
372 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
373 true, $response, $message);
374 /* Another workaround for EIMS */
375 if (isset($read[1]) &&
376 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
378 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
382 if (isset($sorted_list_ary[$i])) {
383 $sorted_list_ary[$i] = "";
386 if (isset($read[0])) {
387 $sorted_list_ary[$i] = $read[0];
390 $sorted_list_ary[$i] = "";
393 if (isset($sorted_list_ary[$i]) &&
394 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == "INBOX") {
395 $inbox_in_list = true;
400 * Just in case they're not subscribed to their inbox,
401 * we'll get it for them anyway
403 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
404 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
405 true, $response, $message);
406 /* Another workaround for EIMS */
407 if (isset($inbox_ary[1]) &&
408 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
409 $inbox_ary[0], $regs)) {
410 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
414 $sorted_list_ary[] = $inbox_ary[0];
415 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
418 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
420 /** Now, lets sort for special folders **/
424 for ($i = 0; $i < count($boxes); $i++
) {
425 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
426 $boxesnew[] = $boxes[$i];
432 /* List special folders and their subfolders, if requested. */
433 if ($list_special_folders_first == true) {
434 /* First list the trash folder. */
435 for ($i = 0 ; $i < count($boxes) ; $i++
) {
436 if ($move_to_trash &&
437 eregi('^' . quotemeta($trash_folder) . '(' .
438 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
439 $boxesnew[] = $boxes[$i];
444 /* Then list the sent folder. */
445 for ($i = 0 ; $i < count($boxes) ; $i++
) {
447 eregi('^' . quotemeta($sent_folder) . '(' .
448 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
449 $boxesnew[] = $boxes[$i];
454 /* Lastly, list the list the draft folder. */
455 for ($i = 0 ; $i < count($boxes) ; $i++
) {
456 if ($save_as_draft &&
457 eregi('^' . quotemeta($draft_folder) . '(' .
458 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
459 $boxesnew[] = $boxes[$i];
464 /* Put INBOX.* folders ahead of the rest. */
465 for ($i = 0; $i < count($boxes); $i++
) {
466 if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
467 (!isset($used[$i]) ||
$used[$i] == false)) {
468 $boxesnew[] = $boxes[$i];
474 /* Rest of the folders */
475 for ($i = 0; $i < count($boxes); $i++
) {
476 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
477 (!isset($used[$i]) ||
$used[$i] == false)) {
478 $boxesnew[] = $boxes[$i];
486 /******************************************************************************
487 ** Returns a list of all folders, subscribed or not
488 ******************************************************************************/
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);
577 $boxes[$g]['flags'] = array();
582 if(is_array($boxes)) {
583 $boxes = ary_sort ($boxes, 'unformatted', 1);