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