Various fixes
[squirrelmail.git] / functions / imap_mailbox.php
1 <?php
2
3 /**
4 ** imap_mailbox.php
5 ** Copyright (c) 1999-2001 The Squirrelmail Development Team
6 ** Licensed under the GNU GPL. For full terms see the file COPYING.
7 **
8 ** This impliments all functions that manipulate mailboxes
9 **
10 ** $Id$
11 **/
12
13 if (defined ('imap_mailbox_php'))
14 return;
15 define ('imap_mailbox_php', true);
16
17 /******************************************************************************
18 ** Expunges a mailbox
19 ******************************************************************************/
20 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) {
21 fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
22 $read = sqimap_read_data($imap_stream, sqimap_session_id(), $handle_errors, $response, $message);
23 }
24
25
26 /******************************************************************************
27 ** Checks whether or not the specified mailbox exists
28 ******************************************************************************/
29 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
30 if (! isset($mailbox))
31 return false;
32 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
33 $mbx = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
34 return isset($mbx[0]);
35 }
36
37 /******************************************************************************
38 ** Selects a mailbox
39 ******************************************************************************/
40 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
41 global $auto_expunge;
42
43 if( $mailbox == _("None") )
44 return;
45
46 fputs ($imap_stream, sqimap_session_id() . " SELECT \"$mailbox\"\r\n");
47 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
48 if ($recent) {
49 for ($i=0; $i<count($read); $i++) {
50 if (strpos(strtolower($read[$i]), 'recent')) {
51 $r = explode(' ', $read[$i]);
52 }
53 }
54 return $r[1];
55 }
56 if ($auto_expunge) {
57 fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
58 $tmp = sqimap_read_data($imap_stream, sqimap_session_id(), false, $a, $b);
59 }
60 }
61
62
63
64 /******************************************************************************
65 ** Creates a folder
66 ******************************************************************************/
67 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
68 global $delimiter;
69 if (strtolower($type) == "noselect") {
70 $mailbox = $mailbox.$delimiter;
71 }
72 fputs ($imap_stream, sqimap_session_id() . " CREATE \"$mailbox\"\r\n");
73 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
74
75 sqimap_subscribe ($imap_stream, $mailbox);
76 }
77
78
79
80 /******************************************************************************
81 ** Subscribes to an existing folder
82 ******************************************************************************/
83 function sqimap_subscribe ($imap_stream, $mailbox) {
84 fputs ($imap_stream, sqimap_session_id() . " SUBSCRIBE \"$mailbox\"\r\n");
85 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
86 }
87
88
89
90
91 /******************************************************************************
92 ** Unsubscribes to an existing folder
93 ******************************************************************************/
94 function sqimap_unsubscribe ($imap_stream, $mailbox) {
95 global $imap_server_type;
96
97 fputs ($imap_stream, sqimap_session_id() . " UNSUBSCRIBE \"$mailbox\"\r\n");
98 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
99 }
100
101
102
103
104 /******************************************************************************
105 ** This function simply deletes the given folder
106 ******************************************************************************/
107 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
108 fputs ($imap_stream, sqimap_session_id() . " DELETE \"$mailbox\"\r\n");
109 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
110 sqimap_unsubscribe ($imap_stream, $mailbox);
111 }
112
113 /***********************************************************************
114 ** Determines if the user is subscribed to the folder or not
115 **********************************************************************/
116 function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
117 $boxes = sqimap_mailbox_list ($imap_stream);
118 foreach ($boxes as $ref) {
119 if ($ref['unformatted'] == $folder)
120 return true;
121 }
122 return false;
123 }
124
125
126
127 /******************************************************************************
128 ** Formats a mailbox into 4 parts for the $boxes array
129 **
130 ** The four parts are:
131 **
132 ** raw - Raw LIST/LSUB response from the IMAP server
133 ** formatted - nicely formatted folder name
134 ** unformatted - unformatted, but with delimiter at end removed
135 ** unformatted-dm - folder name as it appears in raw response
136 ** unformatted-disp - unformatted without $folder_prefix
137 **
138 ******************************************************************************/
139 function sqimap_mailbox_parse ($line, $line_lsub) {
140 global $folder_prefix, $delimiter;
141
142 // Process each folder line
143 for ($g=0; $g < count($line); $g++) {
144
145 // Store the raw IMAP reply
146 if (isset($line[$g]))
147 $boxes[$g]["raw"] = $line[$g];
148 else
149 $boxes[$g]["raw"] = "";
150
151
152 // Count number of delimiters ($delimiter) in folder name
153 $mailbox = trim($line_lsub[$g]);
154 $dm_count = countCharInString($mailbox, $delimiter);
155 if (substr($mailbox, -1) == $delimiter)
156 $dm_count--; // If name ends in delimiter - decrement count by one
157
158 // Format folder name, but only if it's a INBOX.* or have
159 // a parent.
160 $boxesbyname[$mailbox] = $g;
161 $parentfolder = readMailboxParent($mailbox, $delimiter);
162 if((strtolower(substr($mailbox, 0, 5)) == "inbox") ||
163 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
164 (isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
165 $indent = $dm_count - (countCharInString($folder_prefix, $delimiter));
166 if ($indent > 0)
167 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
168 else
169 $boxes[$g]["formatted"] = '';
170 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
171 } else {
172 $boxes[$g]["formatted"] = $mailbox;
173 }
174
175 $boxes[$g]['unformatted-dm'] = $mailbox;
176 if (substr($mailbox, -1) == $delimiter)
177 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
178 $boxes[$g]['unformatted'] = $mailbox;
179 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix)
180 $mailbox = substr($mailbox, strlen($folder_prefix));
181 $boxes[$g]['unformatted-disp'] = $mailbox;
182 $boxes[$g]['id'] = $g;
183
184 $boxes[$g]['flags'] = array();
185 if (isset($line[$g])) {
186 ereg("\(([^)]*)\)",$line[$g],$regs);
187 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
188 if ($flags)
189 $boxes[$g]['flags'] = explode(' ', $flags);
190 }
191 }
192
193 return $boxes;
194 }
195
196 /* Apparently you must call a user function with usort instead
197 * of calling a built-in directly. Stupid.
198 * Patch from dave_michmerhuizen@yahoo.com
199 * Allows case insensitivity when sorting folders
200 */
201 function user_strcasecmp($a, $b)
202 {
203 return strcasecmp($a, $b);
204 }
205
206
207 /******************************************************************************
208 ** Returns sorted mailbox lists in several different ways.
209 ** See comment on sqimap_mailbox_parse() for info about the returned array.
210 ******************************************************************************/
211 function sqimap_mailbox_list ($imap_stream) {
212 global $data_dir, $username, $list_special_folders_first;
213 global $folder_prefix, $trash_folder, $sent_folder, $draft_folder;
214 global $move_to_trash, $move_to_sent, $save_as_draft;
215 global $delimiter;
216
217 $inbox_in_list = false;
218 $inbox_subscribed = false;
219
220 require_once('../src/load_prefs.php');
221 require_once('../functions/array.php');
222
223 /** LSUB array **/
224 fputs ($imap_stream, sqimap_session_id() . " LSUB \"$folder_prefix\" \"*\"\r\n");
225 $lsub_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
226
227 // Section about removing the last element was removed
228 // We don't return "* OK" anymore from sqimap_read_data
229
230 $sorted_lsub_ary = array();
231 for ($i=0;$i < count($lsub_ary); $i++) {
232 // Workaround for EIMS
233 // Doesn't work if the mailbox name is multiple lines
234 if (isset($lsub_ary[$i + 1]) &&
235 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
236 $lsub_ary[$i], $regs)) {
237 $i ++;
238 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
239 '"' . $regs[2];
240 }
241 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
242 $sorted_lsub_ary[] = $temp_mailbox_name;
243 if (strtoupper($temp_mailbox_name) == 'INBOX')
244 $inbox_subscribed = true;
245 }
246 $new_ary = array();
247 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
248 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
249 $new_ary[] = $sorted_lsub_ary[$i];
250 }
251 }
252 $sorted_lsub_ary = $new_ary;
253 if (isset($sorted_lsub_ary)) {
254 usort($sorted_lsub_ary, 'user_strcasecmp');
255 //sort($sorted_lsub_ary);
256 }
257
258 /** LIST array **/
259 $sorted_list_ary = array();
260 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
261 if (substr($sorted_lsub_ary[$i], -1) == $delimiter)
262 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
263 else
264 $mbx = $sorted_lsub_ary[$i];
265
266 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mbx\"\r\n");
267 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
268 // Another workaround for EIMS
269 if (isset($read[1]) &&
270 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
271 $read[0], $regs)) {
272 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
273 '"' . $regs[2];
274 }
275 if (isset($sorted_list_ary[$i]))
276 $sorted_list_ary[$i] = "";
277 if (isset($read[0]))
278 $sorted_list_ary[$i] = $read[0];
279 else
280 $sorted_list_ary[$i] = "";
281 if (isset($sorted_list_ary[$i]) &&
282 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == "INBOX")
283 $inbox_in_list = true;
284 }
285
286 /**
287 * Just in case they're not subscribed to their inbox,
288 * we'll get it for them anyway
289 */
290 if ($inbox_subscribed == false || $inbox_in_list == false) {
291 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"INBOX\"\r\n");
292 $inbox_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
293 // Another workaround for EIMS
294 if (isset($inbox_ary[1]) &&
295 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
296 $inbox_ary[0], $regs)) {
297 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
298 '"' . $regs[2];
299 }
300
301 $sorted_list_ary[] = $inbox_ary[0];
302 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
303 }
304
305 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
306
307 /** Now, lets sort for special folders **/
308 $boxesnew = Array();
309
310 /* Find INBOX */
311 for ($i = 0; $i < count($boxes); $i++) {
312 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
313 $boxesnew[] = $boxes[$i];
314 $used[$i] = true;
315 $i = count($boxes);
316 }
317 }
318
319 /* List special folders and their subfolders, if requested. */
320 if ($list_special_folders_first == true) {
321 /* First list the trash folder. */
322 for ($i = 0 ; $i < count($boxes) ; $i++) {
323 if ($move_to_trash &&
324 eregi('^' . quotemeta($trash_folder) . '(' .
325 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
326 $boxesnew[] = $boxes[$i];
327 $used[$i] = true;
328 }
329 }
330
331 /* Then list the sent folder. */
332 for ($i = 0 ; $i < count($boxes) ; $i++) {
333 if ($move_to_sent &&
334 eregi('^' . quotemeta($sent_folder) . '(' .
335 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
336 $boxesnew[] = $boxes[$i];
337 $used[$i] = true;
338 }
339 }
340
341 /* Lastly, list the list the draft folder. */
342 for ($i = 0 ; $i < count($boxes) ; $i++) {
343 if ($save_as_draft &&
344 eregi('^' . quotemeta($draft_folder) . '(' .
345 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
346 $boxesnew[] = $boxes[$i];
347 $used[$i] = true;
348 }
349 }
350
351 /* Put INBOX.* folders ahead of the rest. */
352 for ($i = 0; $i < count($boxes); $i++) {
353 if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
354 (!isset($used[$i]) || $used[$i] == false)) {
355 $boxesnew[] = $boxes[$i];
356 $used[$i] = true;
357 }
358 }
359 }
360
361 // Rest of the folders
362 for ($i = 0; $i < count($boxes); $i++) {
363 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
364 (!isset($used[$i]) || $used[$i] == false)) {
365 $boxesnew[] = $boxes[$i];
366 $used[$i] = true;
367 }
368 }
369
370 return $boxesnew;
371 }
372
373 /******************************************************************************
374 ** Returns a list of all folders, subscribed or not
375 ******************************************************************************/
376 function sqimap_mailbox_list_all ($imap_stream) {
377 global $list_special_folders_first, $folder_prefix;
378 global $delimiter;
379
380 if (!function_exists ("ary_sort"))
381 include_once('../functions/array.php');
382
383 $ssid = sqimap_session_id();
384 $lsid = strlen( $ssid );
385 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
386 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
387 $g = 0;
388 $phase = "inbox";
389
390 for ($i = 0; $i < count($read_ary); $i++) {
391 // Another workaround for EIMS
392 if (isset($read_ary[$i + 1]) &&
393 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
394 $read_ary[$i], $regs)) {
395 $i ++;
396 $read_ary[$i] = $regs[1] . '"' .
397 addslashes(trim($read_ary[$i])) .
398 '"' . $regs[2];
399 }
400 if (substr ($read_ary[$i], 0, $lsid) != $ssid ) {
401
402 // Store the raw IMAP reply
403 $boxes[$g]["raw"] = $read_ary[$i];
404
405 // Count number of delimiters ($delimiter) in folder name
406 $mailbox = find_mailbox_name($read_ary[$i]);
407 $dm_count = countCharInString($mailbox, $delimiter);
408 if (substr($mailbox, -1) == $delimiter)
409 $dm_count--; // If name ends in delimiter - decrement count by one
410
411 // Format folder name, but only if it's a INBOX.* or have
412 // a parent.
413 $boxesbyname[$mailbox] = $g;
414 $parentfolder = readMailboxParent($mailbox, $delimiter);
415 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
416 (ereg('^'.$folder_prefix, $mailbox)) ||
417 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
418 if ($dm_count)
419 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
420 else
421 $boxes[$g]["formatted"] = '';
422 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
423 } else {
424 $boxes[$g]["formatted"] = $mailbox;
425 }
426
427 $boxes[$g]["unformatted-dm"] = $mailbox;
428 if (substr($mailbox, -1) == $delimiter)
429 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
430 $boxes[$g]["unformatted"] = $mailbox;
431 $boxes[$g]["unformatted-disp"] = ereg_replace('^' . $folder_prefix, '', $mailbox);
432 $boxes[$g]["id"] = $g;
433
434 /** Now lets get the flags for this mailbox **/
435 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
436 $read_mlbx = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
437
438 // Another workaround for EIMS
439 if (isset($read_mlbx[1]) &&
440 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
441 $read_mlbx[0], $regs)) {
442 $read_mlbx[0] = $regs[1] . '"' .
443 addslashes(trim($read_mlbx[1])) .
444 '"' . $regs[2];
445 }
446
447 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
448 $flags = substr($flags, 0, strpos($flags, ")"));
449 $flags = str_replace('\\', '', $flags);
450 $flags = trim(strtolower($flags));
451 if ($flags) {
452 $boxes[$g]['flags'] = explode(" ", $flags);
453 }
454 else
455 {
456 $boxes[$g]['flags'] = array();
457 }
458 }
459 $g++;
460 }
461 if(is_array($boxes)) {
462 $boxes = ary_sort ($boxes, "unformatted", 1);
463 }
464
465 return $boxes;
466 }
467
468 ?>