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