In order to make sent_subfolders work I had to change a lot of code.
[squirrelmail.git] / functions / imap_mailbox.php
1 <?php
2
3 /**
4 * imap_mailbox.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This impliments all functions that manipulate mailboxes
10 *
11 * $Id$
12 */
13
14 /*************************
15 ** Expunges a mailbox **
16 *************************/
17 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true)
18 {
19 fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
20 $read = sqimap_read_data($imap_stream, sqimap_session_id(),
21 $handle_errors, $response, $message);
22 }
23
24
25 /******************************************************************************
26 ** Checks whether or not the specified mailbox exists
27 ******************************************************************************/
28 function sqimap_mailbox_exists ($imap_stream, $mailbox)
29 {
30 if (! isset($mailbox)) {
31 return false;
32 }
33 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
34 $mbx = sqimap_read_data($imap_stream, sqimap_session_id(),
35 true, $response, $message);
36 return isset($mbx[0]);
37 }
38
39 /******************************************************************************
40 ** Selects a mailbox
41 ******************************************************************************/
42 function sqimap_mailbox_select ($imap_stream, $mailbox,
43 $hide=true, $recent=false)
44 {
45 global $auto_expunge;
46
47 if ( $mailbox == 'None' ) {
48 return;
49 }
50
51 fputs ($imap_stream, sqimap_session_id() . " SELECT \"$mailbox\"\r\n");
52 $read = sqimap_read_data($imap_stream, sqimap_session_id(),
53 true, $response, $message);
54 if ($recent) {
55 for ($i=0; $i<count($read); $i++) {
56 if (strpos(strtolower($read[$i]), 'recent')) {
57 $r = explode(' ', $read[$i]);
58 }
59 }
60 return $r[1];
61 }
62 if ($auto_expunge) {
63 fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
64 $tmp = sqimap_read_data($imap_stream, sqimap_session_id(),
65 false, $a, $b);
66 }
67 }
68
69
70
71 /******************************************************************************
72 ** Creates a folder
73 ******************************************************************************/
74 function sqimap_mailbox_create ($imap_stream, $mailbox, $type)
75 {
76 global $delimiter;
77 if (strtolower($type) == 'noselect') {
78 $mailbox = $mailbox.$delimiter;
79 }
80 fputs ($imap_stream, sqimap_session_id() . " CREATE \"$mailbox\"\r\n");
81 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
82 true, $response, $message);
83
84 sqimap_subscribe ($imap_stream, $mailbox);
85 }
86
87
88
89 /******************************************************************************
90 ** Subscribes to an existing folder
91 ******************************************************************************/
92 function sqimap_subscribe ($imap_stream, $mailbox)
93 {
94 fputs ($imap_stream, sqimap_session_id() . " SUBSCRIBE \"$mailbox\"\r\n");
95 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
96 true, $response, $message);
97 }
98
99
100
101 /******************************************************************************
102 ** Unsubscribes to an existing folder
103 ******************************************************************************/
104 function sqimap_unsubscribe ($imap_stream, $mailbox)
105 {
106 global $imap_server_type;
107
108 fputs ($imap_stream, sqimap_session_id() . " UNSUBSCRIBE \"$mailbox\"\r\n");
109 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
110 true, $response, $message);
111 }
112
113
114
115 /******************************************************************************
116 ** This function simply deletes the given folder
117 ******************************************************************************/
118 function sqimap_mailbox_delete ($imap_stream, $mailbox)
119 {
120 fputs ($imap_stream, sqimap_session_id() . " DELETE \"$mailbox\"\r\n");
121 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(),
122 true, $response, $message);
123 sqimap_unsubscribe ($imap_stream, $mailbox);
124 }
125
126 /***********************************************************************
127 ** Determines if the user is subscribed to the folder or not
128 **********************************************************************/
129 function sqimap_mailbox_is_subscribed($imap_stream, $folder)
130 {
131 $boxes = sqimap_mailbox_list ($imap_stream);
132 foreach ($boxes as $ref) {
133 if ($ref['unformatted'] == $folder) {
134 return true;
135 }
136 }
137 return false;
138 }
139
140 /*
141 Renames a mailbox
142 */
143 function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
144
145 if ( $old_name <> $new_name ) {
146
147 global $delimiter;
148
149 if ( substr( $old_name, -1 ) == $delimiter ) {
150 $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
151 $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
152 $postfix = $delimiter;
153 $boxes = sqimap_mailbox_list($imap_stream);
154 } else {
155 $postfix = '';
156 $boxes = FALSE;
157 }
158
159 $cmd = sqimap_session_id() . " RENAME \"" . quoteIMAP($old_name) . "\" \"" .
160 quoteIMAP($new_name) . "\"\r\n";
161 fputs($imap_stream, $cmd);
162 $data = sqimap_read_data($imap_stream, sqimap_session_id(),
163 TRUE, $response, $message);
164 sqimap_unsubscribe($imap_stream, $old_name.$postfix);
165 sqimap_subscribe($imap_stream, $new_name.$postfix);
166
167 if ( $boxes ) {
168 // Sub-unsub subfolders
169 $l = strlen( $old_name ) + 1;
170 $p = 'unformatted';
171 foreach ( $boxes as $box ) {
172 if ( substr( $box[$p], 0, $l ) == $old_name . $delimiter ) {
173 sqimap_unsubscribe($imap_stream, $box[$p]);
174 sqimap_subscribe($imap_stream,
175 $new_name . $delimiter . substr( $box[$p], $l ) );
176 }
177 }
178 }
179
180 }
181
182 }
183
184 /******************************************************************************
185 ** Formats a mailbox into 4 parts for the $boxes array
186 **
187 ** The four parts are:
188 **
189 ** raw - Raw LIST/LSUB response from the IMAP server
190 ** formatted - nicely formatted folder name
191 ** unformatted - unformatted, but with delimiter at end removed
192 ** unformatted-dm - folder name as it appears in raw response
193 ** unformatted-disp - unformatted without $folder_prefix
194 **
195 ******************************************************************************/
196 function sqimap_mailbox_parse ($line, $line_lsub)
197 {
198 global $folder_prefix, $delimiter;
199
200 /* Process each folder line */
201 for ($g=0; $g < count($line); $g++) {
202
203 /* Store the raw IMAP reply */
204 if (isset($line[$g])) {
205 $boxes[$g]["raw"] = $line[$g];
206 }
207 else {
208 $boxes[$g]["raw"] = "";
209 }
210
211
212 /* Count number of delimiters ($delimiter) in folder name */
213 $mailbox = trim($line_lsub[$g]);
214 $dm_count = substr_count($mailbox, $delimiter);
215 if (substr($mailbox, -1) == $delimiter) {
216 /* If name ends in delimiter - decrement count by one */
217 $dm_count--;
218 }
219
220 /* Format folder name, but only if it's a INBOX.* or have */
221 /* a parent. */
222 $boxesbyname[$mailbox] = $g;
223 $parentfolder = readMailboxParent($mailbox, $delimiter);
224 if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
225 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
226 ( isset($boxesbyname[$parentfolder]) &&
227 (strlen($parentfolder) > 0) ) ) {
228 $indent = $dm_count - ( substr_count($folder_prefix, $delimiter));
229 if ($indent > 0) {
230 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
231 }
232 else {
233 $boxes[$g]["formatted"] = '';
234 }
235 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
236 }
237 else {
238 $boxes[$g]["formatted"] = $mailbox;
239 }
240
241 $boxes[$g]['unformatted-dm'] = $mailbox;
242 if (substr($mailbox, -1) == $delimiter) {
243 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
244 }
245 $boxes[$g]['unformatted'] = $mailbox;
246 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
247 $mailbox = substr($mailbox, strlen($folder_prefix));
248 }
249 $boxes[$g]['unformatted-disp'] = $mailbox;
250 $boxes[$g]['id'] = $g;
251
252 $boxes[$g]['flags'] = array();
253 if (isset($line[$g])) {
254 ereg("\(([^)]*)\)",$line[$g],$regs);
255 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
256 if ($flags) {
257 $boxes[$g]['flags'] = explode(' ', $flags);
258 }
259 }
260 }
261
262 return $boxes;
263 }
264
265 /**
266 * Sorting function used to sort mailbox names.
267 * + Original patch from dave_michmerhuizen@yahoo.com
268 * + Allows case insensitivity when sorting folders
269 * + Takes care of the delimiter being sorted to the end, causing
270 * subfolders to be listed in below folders that are prefixed
271 * with their parent folders name.
272 * For example: INBOX.foo, INBOX.foobar, and INBOX.foo.bar
273 * Without special sort function: foobar between foo and foo.bar
274 * With special sort function: foobar AFTER foo and foo.bar :)
275 */
276 function user_strcasecmp($a, $b) {
277 global $delimiter;
278
279 /* Calculate the length of some strings. */
280 $a_length = strlen($a);
281 $b_length = strlen($b);
282 $min_length = min($a_length, $b_length);
283 $delimiter_length = strlen($delimiter);
284
285 /* Set the initial result value. */
286 $result = 0;
287
288 /* Check the strings... */
289 for ($c = 0; $c < $min_length; ++$c) {
290 $a_del = substr($a, $c, $delimiter_length);
291 $b_del = substr($b, $c, $delimiter_length);
292
293 if (($a_del == $delimiter) && ($b_del == $delimiter)) {
294 $result = 0;
295 } else if (($a_del == $delimiter) && ($b_del != $delimiter)) {
296 $result = -1;
297 } else if (($a_del != $delimiter) && ($b_del == $delimiter)) {
298 $result = 1;
299 } else {
300 $result = strcasecmp($a{$c}, $b{$c});
301 }
302
303 if ($result != 0) {
304 break;
305 }
306 }
307
308 /* If one string is a prefix of the other... */
309 if ($result == 0) {
310 if ($a_length < $b_length) {
311 $result = -1;
312 } else if ($a_length > $b_length) {
313 $result = 1;
314 }
315 }
316
317 return ($result);
318 }
319
320
321 /******************************************************************************
322 ** Returns sorted mailbox lists in several different ways.
323 ** See comment on sqimap_mailbox_parse() for info about the returned array.
324 ******************************************************************************/
325 function sqimap_mailbox_list ($imap_stream) {
326 global $data_dir, $username, $list_special_folders_first;
327 global $folder_prefix, $trash_folder, $sent_folder, $draft_folder;
328 global $move_to_trash, $move_to_sent, $save_as_draft;
329 global $delimiter;
330
331 $inbox_in_list = false;
332 $inbox_subscribed = false;
333
334 require_once('../src/load_prefs.php');
335 require_once('../functions/array.php');
336
337 /** LSUB array **/
338 fputs ($imap_stream, sqimap_session_id() .
339 " LSUB \"$folder_prefix\" \"*\"\r\n");
340 $lsub_ary = sqimap_read_data ($imap_stream, sqimap_session_id(),
341 true, $response, $message);
342
343 /* Section about removing the last element was removed */
344 /* We don't return "* OK" anymore from sqimap_read_data */
345
346 $sorted_lsub_ary = array();
347 for ($i=0;$i < count($lsub_ary); $i++) {
348 /* Workaround for EIMS */
349 /* Doesn't work if the mailbox name is multiple lines */
350 if (isset($lsub_ary[$i + 1]) &&
351 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
352 $lsub_ary[$i], $regs)) {
353 $i ++;
354 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
355 '"' . $regs[2];
356 }
357 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
358 $sorted_lsub_ary[] = $temp_mailbox_name;
359 if (strtoupper($temp_mailbox_name) == 'INBOX') {
360 $inbox_subscribed = true;
361 }
362 }
363 $new_ary = array();
364 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
365 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
366 $new_ary[] = $sorted_lsub_ary[$i];
367 }
368 }
369 $sorted_lsub_ary = $new_ary;
370 if (isset($sorted_lsub_ary)) {
371 usort($sorted_lsub_ary, 'user_strcasecmp');
372 }
373
374 /** LIST array **/
375 $sorted_list_ary = array();
376 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
377 if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
378 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
379 }
380 else {
381 $mbx = $sorted_lsub_ary[$i];
382 }
383
384 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mbx\"\r\n");
385 $read = sqimap_read_data ($imap_stream, sqimap_session_id(),
386 true, $response, $message);
387 /* Another workaround for EIMS */
388 if (isset($read[1]) &&
389 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
390 $read[0], $regs)) {
391 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
392 '"' . $regs[2];
393 }
394
395 if (isset($sorted_list_ary[$i])) {
396 $sorted_list_ary[$i] = "";
397 }
398
399 if (isset($read[0])) {
400 $sorted_list_ary[$i] = $read[0];
401 }
402 else {
403 $sorted_list_ary[$i] = "";
404 }
405
406 if (isset($sorted_list_ary[$i]) &&
407 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == "INBOX") {
408 $inbox_in_list = true;
409 }
410 }
411
412 /**
413 * Just in case they're not subscribed to their inbox,
414 * we'll get it for them anyway
415 */
416 if ($inbox_subscribed == false || $inbox_in_list == false) {
417 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"INBOX\"\r\n");
418 $inbox_ary = sqimap_read_data ($imap_stream, sqimap_session_id(),
419 true, $response, $message);
420 /* Another workaround for EIMS */
421 if (isset($inbox_ary[1]) &&
422 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
423 $inbox_ary[0], $regs)) {
424 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
425 '"' . $regs[2];
426 }
427
428 $sorted_list_ary[] = $inbox_ary[0];
429 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
430 }
431
432 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
433
434 /** Now, lets sort for special folders **/
435 $boxesnew = Array();
436
437 /* Find INBOX */
438 for ($i = 0; $i < count($boxes); $i++) {
439 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
440 $boxesnew[] = $boxes[$i];
441 $used[$i] = true;
442 $i = count($boxes);
443 }
444 }
445
446 /* List special folders and their subfolders, if requested. */
447 if ($list_special_folders_first == true) {
448 /* First list the trash folder. */
449 for ($i = 0 ; $i < count($boxes) ; $i++) {
450 if ($move_to_trash &&
451 eregi('^' . quotemeta($trash_folder) . '(' .
452 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
453 $boxesnew[] = $boxes[$i];
454 $used[$i] = true;
455 }
456 }
457
458 /* Then list the sent folder. */
459 for ($i = 0 ; $i < count($boxes) ; $i++) {
460 if ($move_to_sent &&
461 eregi('^' . quotemeta($sent_folder) . '(' .
462 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
463 $boxesnew[] = $boxes[$i];
464 $used[$i] = true;
465 }
466 }
467
468 /* Lastly, list the list the draft folder. */
469 for ($i = 0 ; $i < count($boxes) ; $i++) {
470 if ($save_as_draft &&
471 eregi('^' . quotemeta($draft_folder) . '(' .
472 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
473 $boxesnew[] = $boxes[$i];
474 $used[$i] = true;
475 }
476 }
477
478 /* Put INBOX.* folders ahead of the rest. */
479 for ($i = 0; $i < count($boxes); $i++) {
480 if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
481 (!isset($used[$i]) || $used[$i] == false)) {
482 $boxesnew[] = $boxes[$i];
483 $used[$i] = true;
484 }
485 }
486 }
487
488 /* Rest of the folders */
489 for ($i = 0; $i < count($boxes); $i++) {
490 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
491 (!isset($used[$i]) || $used[$i] == false)) {
492 $boxesnew[] = $boxes[$i];
493 $used[$i] = true;
494 }
495 }
496
497 return $boxesnew;
498 }
499
500 /******************************************************************************
501 ** Returns a list of all folders, subscribed or not
502 ******************************************************************************/
503 function sqimap_mailbox_list_all ($imap_stream)
504 {
505 global $list_special_folders_first, $folder_prefix;
506 global $delimiter;
507
508 if (!function_exists ("ary_sort")) {
509 include_once('../functions/array.php');
510 }
511
512 $ssid = sqimap_session_id();
513 $lsid = strlen( $ssid );
514 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
515 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
516 $g = 0;
517 $phase = "inbox";
518
519 for ($i = 0; $i < count($read_ary); $i++) {
520 /* Another workaround for EIMS */
521 if (isset($read_ary[$i + 1]) &&
522 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
523 $read_ary[$i], $regs)) {
524 $i ++;
525 $read_ary[$i] = $regs[1] . '"' .
526 addslashes(trim($read_ary[$i])) .
527 '"' . $regs[2];
528 }
529 if (substr ($read_ary[$i], 0, $lsid) != $ssid ) {
530
531 /* Store the raw IMAP reply */
532 $boxes[$g]["raw"] = $read_ary[$i];
533
534 /* Count number of delimiters ($delimiter) in folder name */
535 $mailbox = find_mailbox_name($read_ary[$i]);
536 $dm_count = substr_count($mailbox, $delimiter);
537 if (substr($mailbox, -1) == $delimiter) {
538 /* If name ends in delimiter - decrement count by one */
539 $dm_count--;
540 }
541
542 /* Format folder name, but only if it's a INBOX.* or have */
543 /* a parent. */
544 $boxesbyname[$mailbox] = $g;
545 $parentfolder = readMailboxParent($mailbox, $delimiter);
546 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
547 (ereg('^'.$folder_prefix, $mailbox)) ||
548 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
549 if ($dm_count) {
550 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
551 }
552 else {
553 $boxes[$g]["formatted"] = '';
554 }
555 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
556 }
557 else {
558 $boxes[$g]["formatted"] = $mailbox;
559 }
560
561 $boxes[$g]["unformatted-dm"] = $mailbox;
562 if (substr($mailbox, -1) == $delimiter) {
563 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
564 }
565 $boxes[$g]["unformatted"] = $mailbox;
566 $boxes[$g]["unformatted-disp"] =
567 ereg_replace('^' . $folder_prefix, '', $mailbox);
568 $boxes[$g]["id"] = $g;
569
570 /** Now lets get the flags for this mailbox **/
571 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
572 $read_mlbx = sqimap_read_data ($imap_stream, sqimap_session_id(),
573 true, $response, $message);
574
575 /* Another workaround for EIMS */
576 if (isset($read_mlbx[1]) &&
577 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
578 $read_mlbx[0], $regs)) {
579 $read_mlbx[0] = $regs[1] . '"' .
580 addslashes(trim($read_mlbx[1])) .
581 '"' . $regs[2];
582 }
583
584 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
585 $flags = substr($flags, 0, strpos($flags, ")"));
586 $flags = str_replace('\\', '', $flags);
587 $flags = trim(strtolower($flags));
588 if ($flags) {
589 $boxes[$g]['flags'] = explode(' ', $flags);
590 }
591 else {
592 $boxes[$g]['flags'] = array();
593 }
594 }
595 $g++;
596 }
597 if(is_array($boxes)) {
598 $boxes = ary_sort ($boxes, 'unformatted', 1);
599 }
600
601 return $boxes;
602 }
603
604 ?>