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