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 if (defined ('imap_mailbox_php'))
16 define ('imap_mailbox_php', true);
18 /******************************************************************************
20 ******************************************************************************/
21 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) {
22 fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
23 $read = sqimap_read_data($imap_stream, sqimap_session_id(), $handle_errors, $response, $message);
27 /******************************************************************************
28 ** Checks whether or not the specified mailbox exists
29 ******************************************************************************/
30 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
31 if (! isset($mailbox))
33 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
34 $mbx = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
35 return isset($mbx[0]);
38 /******************************************************************************
40 ******************************************************************************/
41 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
44 if( $mailbox == 'None' )
47 fputs ($imap_stream, sqimap_session_id() . " SELECT \"$mailbox\"\r\n");
48 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
50 for ($i=0; $i<count($read); $i++
) {
51 if (strpos(strtolower($read[$i]), 'recent')) {
52 $r = explode(' ', $read[$i]);
58 fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
59 $tmp = sqimap_read_data($imap_stream, sqimap_session_id(), false, $a, $b);
65 /******************************************************************************
67 ******************************************************************************/
68 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
70 if (strtolower($type) == 'noselect') {
71 $mailbox = $mailbox.$delimiter;
73 fputs ($imap_stream, sqimap_session_id() . " CREATE \"$mailbox\"\r\n");
74 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
76 sqimap_subscribe ($imap_stream, $mailbox);
81 /******************************************************************************
82 ** Subscribes to an existing folder
83 ******************************************************************************/
84 function sqimap_subscribe ($imap_stream, $mailbox) {
85 fputs ($imap_stream, sqimap_session_id() . " SUBSCRIBE \"$mailbox\"\r\n");
86 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
92 /******************************************************************************
93 ** Unsubscribes to an existing folder
94 ******************************************************************************/
95 function sqimap_unsubscribe ($imap_stream, $mailbox) {
96 global $imap_server_type;
98 fputs ($imap_stream, sqimap_session_id() . " UNSUBSCRIBE \"$mailbox\"\r\n");
99 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
105 /******************************************************************************
106 ** This function simply deletes the given folder
107 ******************************************************************************/
108 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
109 fputs ($imap_stream, sqimap_session_id() . " DELETE \"$mailbox\"\r\n");
110 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
111 sqimap_unsubscribe ($imap_stream, $mailbox);
114 /***********************************************************************
115 ** Determines if the user is subscribed to the folder or not
116 **********************************************************************/
117 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
118 $boxes = sqimap_mailbox_list ($imap_stream);
119 foreach ($boxes as $ref) {
120 if ($ref['unformatted'] == $folder)
128 /******************************************************************************
129 ** Formats a mailbox into 4 parts for the $boxes array
131 ** The four parts are:
133 ** raw - Raw LIST/LSUB response from the IMAP server
134 ** formatted - nicely formatted folder name
135 ** unformatted - unformatted, but with delimiter at end removed
136 ** unformatted-dm - folder name as it appears in raw response
137 ** unformatted-disp - unformatted without $folder_prefix
139 ******************************************************************************/
140 function sqimap_mailbox_parse ($line, $line_lsub) {
141 global $folder_prefix, $delimiter;
143 // Process each folder line
144 for ($g=0; $g < count($line); $g++
) {
146 // Store the raw IMAP reply
147 if (isset($line[$g]))
148 $boxes[$g]["raw"] = $line[$g];
150 $boxes[$g]["raw"] = "";
153 // Count number of delimiters ($delimiter) in folder name
154 $mailbox = trim($line_lsub[$g]);
155 $dm_count = countCharInString($mailbox, $delimiter);
156 if (substr($mailbox, -1) == $delimiter)
157 $dm_count--; // If name ends in delimiter - decrement count by one
159 // Format folder name, but only if it's a INBOX.* or have
161 $boxesbyname[$mailbox] = $g;
162 $parentfolder = readMailboxParent($mailbox, $delimiter);
163 if((strtolower(substr($mailbox, 0, 5)) == "inbox") ||
164 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
165 (isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
166 $indent = $dm_count - (countCharInString($folder_prefix, $delimiter));
168 $boxes[$g]["formatted"] = str_repeat(" ", $indent);
170 $boxes[$g]["formatted"] = '';
171 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
173 $boxes[$g]["formatted"] = $mailbox;
176 $boxes[$g]['unformatted-dm'] = $mailbox;
177 if (substr($mailbox, -1) == $delimiter)
178 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
179 $boxes[$g]['unformatted'] = $mailbox;
180 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix)
181 $mailbox = substr($mailbox, strlen($folder_prefix));
182 $boxes[$g]['unformatted-disp'] = $mailbox;
183 $boxes[$g]['id'] = $g;
185 $boxes[$g]['flags'] = array();
186 if (isset($line[$g])) {
187 ereg("\(([^)]*)\)",$line[$g],$regs);
188 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
190 $boxes[$g]['flags'] = explode(' ', $flags);
197 /* Apparently you must call a user function with usort instead
198 * of calling a built-in directly. Stupid.
199 * Patch from dave_michmerhuizen@yahoo.com
200 * Allows case insensitivity when sorting folders
202 function user_strcasecmp($a, $b)
204 return strcasecmp($a, $b);
208 /******************************************************************************
209 ** Returns sorted mailbox lists in several different ways.
210 ** See comment on sqimap_mailbox_parse() for info about the returned array.
211 ******************************************************************************/
212 function sqimap_mailbox_list ($imap_stream) {
213 global $data_dir, $username, $list_special_folders_first;
214 global $folder_prefix, $trash_folder, $sent_folder, $draft_folder;
215 global $move_to_trash, $move_to_sent, $save_as_draft;
218 $inbox_in_list = false;
219 $inbox_subscribed = false;
221 require_once('../src/load_prefs.php');
222 require_once('../functions/array.php');
225 fputs ($imap_stream, sqimap_session_id() . " LSUB \"$folder_prefix\" \"*\"\r\n");
226 $lsub_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
228 // Section about removing the last element was removed
229 // We don't return "* OK" anymore from sqimap_read_data
231 $sorted_lsub_ary = array();
232 for ($i=0;$i < count($lsub_ary); $i++
) {
233 // Workaround for EIMS
234 // Doesn't work if the mailbox name is multiple lines
235 if (isset($lsub_ary[$i +
1]) &&
236 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
237 $lsub_ary[$i], $regs)) {
239 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
242 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
243 $sorted_lsub_ary[] = $temp_mailbox_name;
244 if (strtoupper($temp_mailbox_name) == 'INBOX')
245 $inbox_subscribed = true;
248 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
249 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
250 $new_ary[] = $sorted_lsub_ary[$i];
253 $sorted_lsub_ary = $new_ary;
254 if (isset($sorted_lsub_ary)) {
255 usort($sorted_lsub_ary, 'user_strcasecmp');
256 //sort($sorted_lsub_ary);
260 $sorted_list_ary = array();
261 for ($i=0; $i < count($sorted_lsub_ary); $i++
) {
262 if (substr($sorted_lsub_ary[$i], -1) == $delimiter)
263 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
265 $mbx = $sorted_lsub_ary[$i];
267 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mbx\"\r\n");
268 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
269 // Another workaround for EIMS
270 if (isset($read[1]) &&
271 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
273 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
276 if (isset($sorted_list_ary[$i]))
277 $sorted_list_ary[$i] = "";
279 $sorted_list_ary[$i] = $read[0];
281 $sorted_list_ary[$i] = "";
282 if (isset($sorted_list_ary[$i]) &&
283 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == "INBOX")
284 $inbox_in_list = true;
288 * Just in case they're not subscribed to their inbox,
289 * we'll get it for them anyway
291 if ($inbox_subscribed == false ||
$inbox_in_list == false) {
292 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"INBOX\"\r\n");
293 $inbox_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
294 // Another workaround for EIMS
295 if (isset($inbox_ary[1]) &&
296 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
297 $inbox_ary[0], $regs)) {
298 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
302 $sorted_list_ary[] = $inbox_ary[0];
303 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
306 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
308 /** Now, lets sort for special folders **/
312 for ($i = 0; $i < count($boxes); $i++
) {
313 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
314 $boxesnew[] = $boxes[$i];
320 /* List special folders and their subfolders, if requested. */
321 if ($list_special_folders_first == true) {
322 /* First list the trash folder. */
323 for ($i = 0 ; $i < count($boxes) ; $i++
) {
324 if ($move_to_trash &&
325 eregi('^' . quotemeta($trash_folder) . '(' .
326 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
327 $boxesnew[] = $boxes[$i];
332 /* Then list the sent folder. */
333 for ($i = 0 ; $i < count($boxes) ; $i++
) {
335 eregi('^' . quotemeta($sent_folder) . '(' .
336 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
337 $boxesnew[] = $boxes[$i];
342 /* Lastly, list the list the draft folder. */
343 for ($i = 0 ; $i < count($boxes) ; $i++
) {
344 if ($save_as_draft &&
345 eregi('^' . quotemeta($draft_folder) . '(' .
346 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
347 $boxesnew[] = $boxes[$i];
352 /* Put INBOX.* folders ahead of the rest. */
353 for ($i = 0; $i < count($boxes); $i++
) {
354 if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
355 (!isset($used[$i]) ||
$used[$i] == false)) {
356 $boxesnew[] = $boxes[$i];
362 // Rest of the folders
363 for ($i = 0; $i < count($boxes); $i++
) {
364 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
365 (!isset($used[$i]) ||
$used[$i] == false)) {
366 $boxesnew[] = $boxes[$i];
374 /******************************************************************************
375 ** Returns a list of all folders, subscribed or not
376 ******************************************************************************/
377 function sqimap_mailbox_list_all ($imap_stream) {
378 global $list_special_folders_first, $folder_prefix;
381 if (!function_exists ("ary_sort"))
382 include_once('../functions/array.php');
384 $ssid = sqimap_session_id();
385 $lsid = strlen( $ssid );
386 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
387 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
391 for ($i = 0; $i < count($read_ary); $i++
) {
392 // Another workaround for EIMS
393 if (isset($read_ary[$i +
1]) &&
394 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
395 $read_ary[$i], $regs)) {
397 $read_ary[$i] = $regs[1] . '"' .
398 addslashes(trim($read_ary[$i])) .
401 if (substr ($read_ary[$i], 0, $lsid) != $ssid ) {
403 // Store the raw IMAP reply
404 $boxes[$g]["raw"] = $read_ary[$i];
406 // Count number of delimiters ($delimiter) in folder name
407 $mailbox = find_mailbox_name($read_ary[$i]);
408 $dm_count = countCharInString($mailbox, $delimiter);
409 if (substr($mailbox, -1) == $delimiter)
410 $dm_count--; // If name ends in delimiter - decrement count by one
412 // Format folder name, but only if it's a INBOX.* or have
414 $boxesbyname[$mailbox] = $g;
415 $parentfolder = readMailboxParent($mailbox, $delimiter);
416 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
417 (ereg('^'.$folder_prefix, $mailbox)) ||
418 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
420 $boxes[$g]["formatted"] = str_repeat(" ", $dm_count);
422 $boxes[$g]["formatted"] = '';
423 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
425 $boxes[$g]["formatted"] = $mailbox;
428 $boxes[$g]["unformatted-dm"] = $mailbox;
429 if (substr($mailbox, -1) == $delimiter)
430 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
431 $boxes[$g]["unformatted"] = $mailbox;
432 $boxes[$g]["unformatted-disp"] = ereg_replace('^' . $folder_prefix, '', $mailbox);
433 $boxes[$g]["id"] = $g;
435 /** Now lets get the flags for this mailbox **/
436 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
437 $read_mlbx = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
439 // Another workaround for EIMS
440 if (isset($read_mlbx[1]) &&
441 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
442 $read_mlbx[0], $regs)) {
443 $read_mlbx[0] = $regs[1] . '"' .
444 addslashes(trim($read_mlbx[1])) .
448 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+
1);
449 $flags = substr($flags, 0, strpos($flags, ")"));
450 $flags = str_replace('\\', '', $flags);
451 $flags = trim(strtolower($flags));
453 $boxes[$g]['flags'] = explode(" ", $flags);
457 $boxes[$g]['flags'] = array();
462 if(is_array($boxes)) {
463 $boxes = ary_sort ($boxes, "unformatted", 1);