6 * Copyright (c) 1999-2001 The SquirrelMail Development Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This impliments all functions that manipulate mailboxes
14 /*****************************************************************/
15 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
16 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
17 /*** + Base level indent should begin at left margin, as ***/
18 /*** the function definition starts below. ***/
19 /*** + All identation should consist of four space blocks ***/
20 /*** + Tab characters are evil. ***/
21 /*** + all comments should use "slash-star ... star-slash" ***/
22 /*** style -- no pound characters, no slash-slash style ***/
23 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
24 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
25 /*** + Please use ' instead of ", when possible. Note " ***/
26 /*** should always be used in _( ) function calls. ***/
27 /*** Thank you for your help making the SM code more readable. ***/
28 /*****************************************************************/
30 /*************************
31 ** Expunges a mailbox **
32 *************************/
33 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) {
34 fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
35 $read = sqimap_read_data($imap_stream, sqimap_session_id(), $handle_errors, $response, $message);
39 /******************************************************************************
40 ** Checks whether or not the specified mailbox exists
41 ******************************************************************************/
42 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
43 if (! isset($mailbox))
45 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
46 $mbx = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
47 return isset($mbx[0]);
50 /******************************************************************************
52 ******************************************************************************/
53 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
56 if( $mailbox == 'None' )
59 fputs ($imap_stream, sqimap_session_id() . " SELECT \"$mailbox\"\r\n");
60 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
62 for ($i=0; $i<count($read); $i++
) {
63 if (strpos(strtolower($read[$i]), 'recent')) {
64 $r = explode(' ', $read[$i]);
70 fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
71 $tmp = sqimap_read_data($imap_stream, sqimap_session_id(), false, $a, $b);
77 /******************************************************************************
79 ******************************************************************************/
80 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
82 if (strtolower($type) == 'noselect') {
83 $mailbox = $mailbox.$delimiter;
85 fputs ($imap_stream, sqimap_session_id() . " CREATE \"$mailbox\"\r\n");
86 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
88 sqimap_subscribe ($imap_stream, $mailbox);
93 /******************************************************************************
94 ** Subscribes to an existing folder
95 ******************************************************************************/
96 function sqimap_subscribe ($imap_stream, $mailbox) {
97 fputs ($imap_stream, sqimap_session_id() . " SUBSCRIBE \"$mailbox\"\r\n");
98 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
104 /******************************************************************************
105 ** Unsubscribes to an existing folder
106 ******************************************************************************/
107 function sqimap_unsubscribe ($imap_stream, $mailbox) {
108 global $imap_server_type;
110 fputs ($imap_stream, sqimap_session_id() . " UNSUBSCRIBE \"$mailbox\"\r\n");
111 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
117 /******************************************************************************
118 ** This function simply deletes the given folder
119 ******************************************************************************/
120 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
121 fputs ($imap_stream, sqimap_session_id() . " DELETE \"$mailbox\"\r\n");
122 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), 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) {
130 $boxes = sqimap_mailbox_list ($imap_stream);
131 foreach ($boxes as $ref) {
132 if ($ref['unformatted'] == $folder)
140 /******************************************************************************
141 ** Formats a mailbox into 4 parts for the $boxes array
143 ** The four parts are:
145 ** raw - Raw LIST/LSUB response from the IMAP server
146 ** formatted - nicely formatted folder name
147 ** unformatted - unformatted, but with delimiter at end removed
148 ** unformatted-dm - folder name as it appears in raw response
149 ** unformatted-disp - unformatted without $folder_prefix
151 ******************************************************************************/
152 function sqimap_mailbox_parse ($line, $line_lsub) {
153 global $folder_prefix, $delimiter;
155 // Process each folder line
156 for ($g=0; $g < count($line); $g++
) {
158 // Store the raw IMAP reply
159 if (isset($line[$g]))
160 $boxes[$g]["raw"] = $line[$g];
162 $boxes[$g]["raw"] = "";
165 // Count number of delimiters ($delimiter) in folder name
166 $mailbox = trim($line_lsub[$g]);
167 $dm_count = countCharInString($mailbox, $delimiter);
168 if (substr($mailbox, -1) == $delimiter)
169 $dm_count--; // If name ends in delimiter - decrement count by one
171 // Format folder name, but only if it's a INBOX.* or have
173 $boxesbyname[$mailbox] = $g;
174 $parentfolder = readMailboxParent($mailbox, $delimiter);
175 if((strtolower(substr($mailbox, 0, 5)) == "inbox") ||
176 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
177 (isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
178 $indent = $dm_count - (countCharInString($folder_prefix, $delimiter));
180 $boxes[$g]["formatted"] = str_repeat(" ", $indent);
182 $boxes[$g]["formatted"] = '';
183 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
185 $boxes[$g]["formatted"] = $mailbox;
188 $boxes[$g]['unformatted-dm'] = $mailbox;
189 if (substr($mailbox, -1) == $delimiter)
190 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
191 $boxes[$g]['unformatted'] = $mailbox;
192 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix)
193 $mailbox = substr($mailbox, strlen($folder_prefix));
194 $boxes[$g]['unformatted-disp'] = $mailbox;
195 $boxes[$g]['id'] = $g;
197 $boxes[$g]['flags'] = array();
198 if (isset($line[$g])) {
199 ereg("\(([^)]*)\)",$line[$g],$regs);
200 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
202 $boxes[$g]['flags'] = explode(' ', $flags);
209 /* Apparently you must call a user function with usort instead
210 * of calling a built-in directly. Stupid.
211 * Patch from dave_michmerhuizen@yahoo.com
212 * Allows case insensitivity when sorting folders
214 function user_strcasecmp($a, $b)
216 return strcasecmp($a, $b);
220 /******************************************************************************
221 ** Returns sorted mailbox lists in several different ways.
222 ** See comment on sqimap_mailbox_parse() for info about the returned array.
223 ******************************************************************************/
224 function sqimap_mailbox_list ($imap_stream) {
225 global $data_dir, $username, $list_special_folders_first;
226 global $folder_prefix, $trash_folder, $sent_folder, $draft_folder;
227 global $move_to_trash, $move_to_sent, $save_as_draft;
230 $inbox_in_list = false;
231 $inbox_subscribed = false;
233 require_once('../src/load_prefs.php');
234 require_once('../functions/array.php');
237 fputs ($imap_stream, sqimap_session_id() . " LSUB \"$folder_prefix\" \"*\"\r\n");
238 $lsub_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
240 // Section about removing the last element was removed
241 // We don't return "* OK" anymore from sqimap_read_data
243 $sorted_lsub_ary = array();
244 for ($i=0;$i < count($lsub_ary); $i++
) {
245 // Workaround for EIMS
246 // Doesn't work if the mailbox name is multiple lines
247 if (isset($lsub_ary[$i +
1]) &&
248 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
249 $lsub_ary[$i], $regs)) {
251 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
254 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
255 $sorted_lsub_ary[] = $temp_mailbox_name;
256 if (strtoupper($temp_mailbox_name) == 'INBOX')
257 $inbox_subscribed = true;
260 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
261 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
262 $new_ary[] = $sorted_lsub_ary[$i];
265 $sorted_lsub_ary = $new_ary;
266 if (isset($sorted_lsub_ary)) {
267 usort($sorted_lsub_ary, 'user_strcasecmp');
268 //sort($sorted_lsub_ary);
272 $sorted_list_ary = array();
273 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
274 if (substr($sorted_lsub_ary[$i], -1) == $delimiter)
275 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
277 $mbx = $sorted_lsub_ary[$i];
279 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mbx\"\r\n");
280 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
281 // Another workaround for EIMS
282 if (isset($read[1]) &&
283 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
285 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
288 if (isset($sorted_list_ary[$i]))
289 $sorted_list_ary[$i] = "";
291 $sorted_list_ary[$i] = $read[0];
293 $sorted_list_ary[$i] = "";
294 if (isset($sorted_list_ary[$i]) &&
295 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == "INBOX")
296 $inbox_in_list = true;
300 * Just in case they're not subscribed to their inbox,
301 * we'll get it for them anyway
303 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
304 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"INBOX\"\r\n");
305 $inbox_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
306 // Another workaround for EIMS
307 if (isset($inbox_ary[1]) &&
308 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
309 $inbox_ary[0], $regs)) {
310 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
314 $sorted_list_ary[] = $inbox_ary[0];
315 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
318 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
320 /** Now, lets sort for special folders **/
324 for ($i = 0; $i < count($boxes); $i++
) {
325 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
326 $boxesnew[] = $boxes[$i];
332 /* List special folders and their subfolders, if requested. */
333 if ($list_special_folders_first == true) {
334 /* First list the trash folder. */
335 for ($i = 0 ; $i < count($boxes) ; $i++
) {
336 if ($move_to_trash &&
337 eregi('^' . quotemeta($trash_folder) . '(' .
338 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
339 $boxesnew[] = $boxes[$i];
344 /* Then list the sent folder. */
345 for ($i = 0 ; $i < count($boxes) ; $i++
) {
347 eregi('^' . quotemeta($sent_folder) . '(' .
348 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
349 $boxesnew[] = $boxes[$i];
354 /* Lastly, list the list the draft folder. */
355 for ($i = 0 ; $i < count($boxes) ; $i++
) {
356 if ($save_as_draft &&
357 eregi('^' . quotemeta($draft_folder) . '(' .
358 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
359 $boxesnew[] = $boxes[$i];
364 /* Put INBOX.* folders ahead of the rest. */
365 for ($i = 0; $i < count($boxes); $i++
) {
366 if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
367 (!isset($used[$i]) ||
$used[$i] == false)) {
368 $boxesnew[] = $boxes[$i];
374 // Rest of the folders
375 for ($i = 0; $i < count($boxes); $i++
) {
376 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
377 (!isset($used[$i]) ||
$used[$i] == false)) {
378 $boxesnew[] = $boxes[$i];
386 /******************************************************************************
387 ** Returns a list of all folders, subscribed or not
388 ******************************************************************************/
389 function sqimap_mailbox_list_all ($imap_stream) {
390 global $list_special_folders_first, $folder_prefix;
393 if (!function_exists ("ary_sort"))
394 include_once('../functions/array.php');
396 $ssid = sqimap_session_id();
397 $lsid = strlen( $ssid );
398 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
399 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
403 for ($i = 0; $i < count($read_ary); $i++
) {
404 // Another workaround for EIMS
405 if (isset($read_ary[$i +
1]) &&
406 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
407 $read_ary[$i], $regs)) {
409 $read_ary[$i] = $regs[1] . '"' .
410 addslashes(trim($read_ary[$i])) .
413 if (substr ($read_ary[$i], 0, $lsid) != $ssid ) {
415 // Store the raw IMAP reply
416 $boxes[$g]["raw"] = $read_ary[$i];
418 // Count number of delimiters ($delimiter) in folder name
419 $mailbox = find_mailbox_name($read_ary[$i]);
420 $dm_count = countCharInString($mailbox, $delimiter);
421 if (substr($mailbox, -1) == $delimiter)
422 $dm_count--; // If name ends in delimiter - decrement count by one
424 // Format folder name, but only if it's a INBOX.* or have
426 $boxesbyname[$mailbox] = $g;
427 $parentfolder = readMailboxParent($mailbox, $delimiter);
428 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
429 (ereg('^'.$folder_prefix, $mailbox)) ||
430 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
432 $boxes[$g]["formatted"] = str_repeat(" ", $dm_count);
434 $boxes[$g]["formatted"] = '';
435 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
437 $boxes[$g]["formatted"] = $mailbox;
440 $boxes[$g]["unformatted-dm"] = $mailbox;
441 if (substr($mailbox, -1) == $delimiter)
442 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
443 $boxes[$g]["unformatted"] = $mailbox;
444 $boxes[$g]["unformatted-disp"] = ereg_replace('^' . $folder_prefix, '', $mailbox);
445 $boxes[$g]["id"] = $g;
447 /** Now lets get the flags for this mailbox **/
448 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
449 $read_mlbx = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
451 // Another workaround for EIMS
452 if (isset($read_mlbx[1]) &&
453 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
454 $read_mlbx[0], $regs)) {
455 $read_mlbx[0] = $regs[1] . '"' .
456 addslashes(trim($read_mlbx[1])) .
460 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+
1);
461 $flags = substr($flags, 0, strpos($flags, ")"));
462 $flags = str_replace('\\', '', $flags);
463 $flags = trim(strtolower($flags));
465 $boxes[$g]['flags'] = explode(" ", $flags);
469 $boxes[$g]['flags'] = array();
474 if(is_array($boxes)) {
475 $boxes = ary_sort ($boxes, "unformatted", 1);