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