7fc6889d4ae33551bd2d2af079a0a6a8dede6c5c
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, $extrainfo=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',
110 if (isset( $extrainfo ) && $extrainfo) {
112 for ($i=0; $i<count($read); $i++
) {
113 if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
114 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
115 $result['PERMANENTFLAGS'] = $regs[1];
117 else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
118 $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
119 $result["FLAGS"] = $regs[1];
121 else if (preg_match("/(.*)EXISTS/i",$read[$i], $regs)) {
122 $result['EXISTS']=trim($regs[1]);
124 else if (preg_match("/(.*)RECENT/i",$read[$i], $regs)) {
125 $result['RECENT']=trim($regs[1]);
127 else if (preg_match("/\[UNSEEN(.*)\]/i",$read[$i], $regs)) {
128 $result['UNSEEN']=trim($regs[1]);
139 /******************************************************************************
141 ******************************************************************************/
142 function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
145 if (strtolower($type) == 'noselect') {
146 $mailbox = $mailbox.$delimiter;
148 $read_ary = sqimap_run_command($imap_stream, "CREATE \"$mailbox\"",
149 TRUE, $response, $message);
151 sqimap_subscribe ($imap_stream, $mailbox);
156 /******************************************************************************
157 ** Subscribes to an existing folder
158 ******************************************************************************/
159 function sqimap_subscribe ($imap_stream, $mailbox)
161 $read_ary = sqimap_run_command($imap_stream, "SUBSCRIBE \"$mailbox\"",
162 TRUE, $response, $message);
167 /******************************************************************************
168 ** Unsubscribes to an existing folder
169 ******************************************************************************/
170 function sqimap_unsubscribe ($imap_stream, $mailbox)
172 global $imap_server_type;
174 $read_ary = sqimap_run_command($imap_stream, "UNSUBSCRIBE \"$mailbox\"",
175 TRUE, $response, $message);
180 /******************************************************************************
181 ** This function simply deletes the given folder
182 ******************************************************************************/
183 function sqimap_mailbox_delete ($imap_stream, $mailbox)
185 $read_ary = sqimap_run_command($imap_stream, "DELETE \"$mailbox\"",
186 TRUE, $response, $message);
187 sqimap_unsubscribe ($imap_stream, $mailbox);
190 /***********************************************************************
191 ** Determines if the user is subscribed to the folder or not
192 **********************************************************************/
193 function sqimap_mailbox_is_subscribed($imap_stream, $folder)
195 $boxes = sqimap_mailbox_list ($imap_stream);
196 foreach ($boxes as $ref) {
197 if ($ref['unformatted'] == $folder) {
207 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
209 if ( $old_name <> $new_name ) {
213 if ( substr( $old_name, -1 ) == $delimiter ) {
214 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
215 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
216 $postfix = $delimiter;
217 $boxes = sqimap_mailbox_list($imap_stream);
223 $cmd = 'RENAME "' . quoteIMAP($old_name) . '" "' . quoteIMAP($new_name) . '"';
224 $data = sqimap_run_command($imap_stream, $cmd,
225 TRUE, $response, $message);
226 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
227 sqimap_subscribe($imap_stream, $new_name.$postfix);
230 // Sub-unsub subfolders
231 $l = strlen( $old_name ) +
1;
233 foreach ( $boxes as $box ) {
234 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
235 sqimap_unsubscribe($imap_stream, $box[$p]);
236 sqimap_subscribe($imap_stream,
237 $new_name . $delimiter . substr( $box[$p], $l ) );
246 /******************************************************************************
247 ** Formats a mailbox into 4 parts for the $boxes array
249 ** The four parts are:
251 ** raw - Raw LIST/LSUB response from the IMAP server
252 ** formatted - nicely formatted folder name
253 ** unformatted - unformatted, but with delimiter at end removed
254 ** unformatted-dm - folder name as it appears in raw response
255 ** unformatted-disp - unformatted without $folder_prefix
257 ******************************************************************************/
258 function sqimap_mailbox_parse ($line, $line_lsub)
260 global $folder_prefix, $delimiter;
262 /* Process each folder line */
263 for ($g=0; $g < count($line); $g++
) {
265 /* Store the raw IMAP reply */
266 if (isset($line[$g])) {
267 $boxes[$g]["raw"] = $line[$g];
270 $boxes[$g]["raw"] = "";
274 /* Count number of delimiters ($delimiter) in folder name */
275 $mailbox = trim($line_lsub[$g]);
276 $dm_count = substr_count($mailbox, $delimiter);
277 if (substr($mailbox, -1) == $delimiter) {
278 /* If name ends in delimiter - decrement count by one */
282 /* Format folder name, but only if it's a INBOX.* or have */
284 $boxesbyname[$mailbox] = $g;
285 $parentfolder = readMailboxParent($mailbox, $delimiter);
286 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
287 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
288 ( isset($boxesbyname[$parentfolder]) &&
289 (strlen($parentfolder) > 0) ) ) {
290 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
292 $boxes[$g]['formatted'] = str_repeat(" ", $indent);
295 $boxes[$g]['formatted'] = '';
297 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
300 $boxes[$g]['formatted'] = $mailbox;
303 $boxes[$g]['unformatted-dm'] = $mailbox;
304 if (substr($mailbox, -1) == $delimiter) {
305 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
307 $boxes[$g]['unformatted'] = $mailbox;
308 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
309 $mailbox = substr($mailbox, strlen($folder_prefix));
311 $boxes[$g]['unformatted-disp'] = $mailbox;
312 $boxes[$g]['id'] = $g;
314 $boxes[$g]['flags'] = array();
315 if (isset($line[$g])) {
316 ereg("\(([^)]*)\)",$line[$g],$regs);
317 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
319 $boxes[$g]['flags'] = explode(' ', $flags);
328 * Sorting function used to sort mailbox names.
329 * + Original patch from dave_michmerhuizen@yahoo.com
330 * + Allows case insensitivity when sorting folders
331 * + Takes care of the delimiter being sorted to the end, causing
332 * subfolders to be listed in below folders that are prefixed
333 * with their parent folders name.
334 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
335 * Without special sort function: foobar between foo and foo.bar
336 * With special sort function: foobar AFTER foo and foo.bar :)
338 function user_strcasecmp($a, $b) {
341 /* Calculate the length of some strings. */
342 $a_length = strlen($a);
343 $b_length = strlen($b);
344 $min_length = min($a_length, $b_length);
345 $delimiter_length = strlen($delimiter);
347 /* Set the initial result value. */
350 /* Check the strings... */
351 for ($c = 0; $c < $min_length; ++
$c) {
352 $a_del = substr($a, $c, $delimiter_length);
353 $b_del = substr($b, $c, $delimiter_length);
355 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
357 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
359 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
362 $result = strcasecmp($a{$c}, $b{$c});
370 /* If one string is a prefix of the other... */
372 if ($a_length < $b_length) {
374 } else if ($a_length > $b_length) {
383 /******************************************************************************
384 ** Returns sorted mailbox lists in several different ways.
385 ** See comment on sqimap_mailbox_parse() for info about the returned array.
386 ******************************************************************************/
387 function sqimap_mailbox_list ($imap_stream) {
389 global $data_dir, $username, $list_special_folders_first,
390 $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
391 $move_to_trash, $move_to_sent, $save_as_draft,
394 $inbox_in_list = $inbox_subscribed = FALSE;
396 require_once('../src/load_prefs.php');
397 require_once('../functions/array.php');
400 $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
401 TRUE, $response, $message);
403 /* Section about removing the last element was removed */
404 /* We don't return "* OK" anymore from sqimap_read_data */
406 $sorted_lsub_ary = array();
407 for ($i=0;$i < count($lsub_ary); $i++
) {
408 /* Workaround for EIMS */
409 /* Doesn't work if the mailbox name is multiple lines */
410 if (isset($lsub_ary[$i +
1]) &&
411 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
412 $lsub_ary[$i], $regs)) {
414 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
417 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
418 $sorted_lsub_ary[] = $temp_mailbox_name;
419 if (strtoupper($temp_mailbox_name) == 'INBOX') {
420 $inbox_subscribed = TRUE;
424 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
425 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
426 $new_ary[] = $sorted_lsub_ary[$i];
429 $sorted_lsub_ary = $new_ary;
430 if (isset($sorted_lsub_ary)) {
431 usort($sorted_lsub_ary, 'user_strcasecmp');
435 $sorted_list_ary = array();
436 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
437 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
438 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
441 $mbx = $sorted_lsub_ary[$i];
444 $read = sqimap_run_command ($imap_stream, "LIST \"\" \"$mbx\"",
445 TRUE, $response, $message);
446 /* Another workaround for EIMS */
447 if (isset($read[1]) &&
448 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
450 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
454 if (isset($sorted_list_ary[$i])) {
455 $sorted_list_ary[$i] = '';
458 if (isset($read[0])) {
459 $sorted_list_ary[$i] = $read[0];
462 $sorted_list_ary[$i] = '';
465 if (isset($sorted_list_ary[$i]) &&
466 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == 'INBOX') {
467 $inbox_in_list = TRUE;
472 * Just in case they're not subscribed to their inbox,
473 * we'll get it for them anyway
475 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
476 $inbox_ary = sqimap_run_command ($imap_stream, "LIST \"\" \"INBOX\"",
477 TRUE, $response, $message);
478 /* Another workaround for EIMS */
479 if (isset($inbox_ary[1]) &&
480 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
481 $inbox_ary[0], $regs)) {
482 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
486 $sorted_list_ary[] = $inbox_ary[0];
487 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
490 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
492 /** Now, lets sort for special folders **/
493 $boxesnew = $used = array();
496 foreach ( $boxes as $k => $box ) {
497 if ( strtolower($box['unformatted']) == 'inbox') {
505 /* List special folders and their subfolders, if requested. */
506 if ($list_special_folders_first == TRUE) {
508 foreach ( $boxes as $k => $box ) {
510 isSpecialMailbox( $box['unformatted'] ) ) {
518 /* Rest of the folders */
519 foreach ( $boxes as $k => $box ) {
530 * Returns a list of all folders, subscribed or not
532 function sqimap_mailbox_list_all ($imap_stream)
534 global $list_special_folders_first, $folder_prefix;
537 require_once('../functions/array.php');
539 $ssid = sqimap_session_id();
540 $lsid = strlen( $ssid );
541 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
542 $read_ary = sqimap_read_data ($imap_stream, $ssid, TRUE, $response, $message);
546 for ($i = 0; $i < count($read_ary); $i++
) {
547 /* Another workaround for EIMS */
548 if (isset($read_ary[$i +
1]) &&
549 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
550 $read_ary[$i], $regs)) {
552 $read_ary[$i] = $regs[1] . '"' .
553 addslashes(trim($read_ary[$i])) .
556 if (substr($read_ary[$i], 0, $lsid) != $ssid ) {
558 /* Store the raw IMAP reply */
559 $boxes[$g]['raw'] = $read_ary[$i];
561 /* Count number of delimiters ($delimiter) in folder name */
562 $mailbox = find_mailbox_name($read_ary[$i]);
563 $dm_count = substr_count($mailbox, $delimiter);
564 if (substr($mailbox, -1) == $delimiter) {
565 /* If name ends in delimiter - decrement count by one */
569 /* Format folder name, but only if it's a INBOX.* or have */
571 $boxesbyname[$mailbox] = $g;
572 $parentfolder = readMailboxParent($mailbox, $delimiter);
573 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
574 (ereg('^'.$folder_prefix, $mailbox)) ||
575 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
577 $boxes[$g]['formatted'] = str_repeat(' ', $dm_count);
580 $boxes[$g]['formatted'] = '';
582 $boxes[$g]['formatted'] .= readShortMailboxName($mailbox, $delimiter);
585 $boxes[$g]['formatted'] = $mailbox;
588 $boxes[$g]["unformatted-dm"] = $mailbox;
589 if (substr($mailbox, -1) == $delimiter) {
590 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
592 $boxes[$g]['unformatted'] = $mailbox;
593 $boxes[$g]['unformatted-disp'] =
594 ereg_replace('^' . $folder_prefix, '', $mailbox);
595 $boxes[$g]['id'] = $g;
597 /** Now lets get the flags for this mailbox **/
598 $read_mlbx = sqimap_run_command ($imap_stream, "LIST \"\" \"$mailbox\"",
599 TRUE, $response, $message);
601 /* Another workaround for EIMS */
602 if (isset($read_mlbx[1]) &&
603 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
604 $read_mlbx[0], $regs)) {
605 $read_mlbx[0] = $regs[1] . '"' .
606 addslashes(trim($read_mlbx[1])) .
610 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], '(')+
1);
611 $flags = substr($flags, 0, strpos($flags, ')'));
612 $flags = str_replace('\\', '', $flags);
613 $flags = trim(strtolower($flags));
615 $boxes[$g]['flags'] = explode(' ', $flags);
617 $boxes[$g]['flags'] = array();
622 if(is_array($boxes)) {
623 $boxes = ary_sort ($boxes, 'unformatted', 1);