Code cleanup brigage...
[squirrelmail.git] / functions / imap_mailbox.php
1 <?php
2
3 /**
4 * imap_mailbox.php
5 *
6 * Copyright (c) 1999-2001 The SquirrelMail Development 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 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
16 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
17 /*** + Base level indent should begin at left margin, as ***/
18 /*** the function definition starts below. ***/
19 /*** + All identation should consist of four space blocks ***/
20 /*** + Tab characters are evil. ***/
21 /*** + all comments should use "slash-star ... star-slash" ***/
22 /*** style -- no pound characters, no slash-slash style ***/
23 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
24 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
25 /*** + Please use ' instead of ", when possible. Note " ***/
26 /*** should always be used in _( ) function calls. ***/
27 /*** Thank you for your help making the SM code more readable. ***/
28 /*****************************************************************/
29
30 /*************************
31 ** Expunges a mailbox **
32 *************************/
33 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) {
34 fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
35 $read = sqimap_read_data($imap_stream, sqimap_session_id(), $handle_errors, $response, $message);
36 }
37
38
39 /******************************************************************************
40 ** Checks whether or not the specified mailbox exists
41 ******************************************************************************/
42 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
43 if (! isset($mailbox))
44 return false;
45 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
46 $mbx = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
47 return isset($mbx[0]);
48 }
49
50 /******************************************************************************
51 ** Selects a mailbox
52 ******************************************************************************/
53 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
54 global $auto_expunge;
55
56 if( $mailbox == 'None' )
57 return;
58
59 fputs ($imap_stream, sqimap_session_id() . " SELECT \"$mailbox\"\r\n");
60 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
61 if ($recent) {
62 for ($i=0; $i<count($read); $i++) {
63 if (strpos(strtolower($read[$i]), 'recent')) {
64 $r = explode(' ', $read[$i]);
65 }
66 }
67 return $r[1];
68 }
69 if ($auto_expunge) {
70 fputs ($imap_stream, sqimap_session_id() . " EXPUNGE\r\n");
71 $tmp = sqimap_read_data($imap_stream, sqimap_session_id(), false, $a, $b);
72 }
73 }
74
75
76
77 /******************************************************************************
78 ** Creates a folder
79 ******************************************************************************/
80 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
81 global $delimiter;
82 if (strtolower($type) == 'noselect') {
83 $mailbox = $mailbox.$delimiter;
84 }
85 fputs ($imap_stream, sqimap_session_id() . " CREATE \"$mailbox\"\r\n");
86 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
87
88 sqimap_subscribe ($imap_stream, $mailbox);
89 }
90
91
92
93 /******************************************************************************
94 ** Subscribes to an existing folder
95 ******************************************************************************/
96 function sqimap_subscribe ($imap_stream, $mailbox) {
97 fputs ($imap_stream, sqimap_session_id() . " SUBSCRIBE \"$mailbox\"\r\n");
98 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
99 }
100
101
102
103
104 /******************************************************************************
105 ** Unsubscribes to an existing folder
106 ******************************************************************************/
107 function sqimap_unsubscribe ($imap_stream, $mailbox) {
108 global $imap_server_type;
109
110 fputs ($imap_stream, sqimap_session_id() . " UNSUBSCRIBE \"$mailbox\"\r\n");
111 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
112 }
113
114
115
116
117 /******************************************************************************
118 ** This function simply deletes the given folder
119 ******************************************************************************/
120 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
121 fputs ($imap_stream, sqimap_session_id() . " DELETE \"$mailbox\"\r\n");
122 $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), 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 $boxes = sqimap_mailbox_list ($imap_stream);
131 foreach ($boxes as $ref) {
132 if ($ref['unformatted'] == $folder)
133 return true;
134 }
135 return false;
136 }
137
138
139
140 /******************************************************************************
141 ** Formats a mailbox into 4 parts for the $boxes array
142 **
143 ** The four parts are:
144 **
145 ** raw - Raw LIST/LSUB response from the IMAP server
146 ** formatted - nicely formatted folder name
147 ** unformatted - unformatted, but with delimiter at end removed
148 ** unformatted-dm - folder name as it appears in raw response
149 ** unformatted-disp - unformatted without $folder_prefix
150 **
151 ******************************************************************************/
152 function sqimap_mailbox_parse ($line, $line_lsub) {
153 global $folder_prefix, $delimiter;
154
155 // Process each folder line
156 for ($g=0; $g < count($line); $g++) {
157
158 // Store the raw IMAP reply
159 if (isset($line[$g]))
160 $boxes[$g]["raw"] = $line[$g];
161 else
162 $boxes[$g]["raw"] = "";
163
164
165 // Count number of delimiters ($delimiter) in folder name
166 $mailbox = trim($line_lsub[$g]);
167 $dm_count = countCharInString($mailbox, $delimiter);
168 if (substr($mailbox, -1) == $delimiter)
169 $dm_count--; // If name ends in delimiter - decrement count by one
170
171 // Format folder name, but only if it's a INBOX.* or have
172 // a parent.
173 $boxesbyname[$mailbox] = $g;
174 $parentfolder = readMailboxParent($mailbox, $delimiter);
175 if((strtolower(substr($mailbox, 0, 5)) == "inbox") ||
176 (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
177 (isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
178 $indent = $dm_count - (countCharInString($folder_prefix, $delimiter));
179 if ($indent > 0)
180 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $indent);
181 else
182 $boxes[$g]["formatted"] = '';
183 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
184 } else {
185 $boxes[$g]["formatted"] = $mailbox;
186 }
187
188 $boxes[$g]['unformatted-dm'] = $mailbox;
189 if (substr($mailbox, -1) == $delimiter)
190 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
191 $boxes[$g]['unformatted'] = $mailbox;
192 if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix)
193 $mailbox = substr($mailbox, strlen($folder_prefix));
194 $boxes[$g]['unformatted-disp'] = $mailbox;
195 $boxes[$g]['id'] = $g;
196
197 $boxes[$g]['flags'] = array();
198 if (isset($line[$g])) {
199 ereg("\(([^)]*)\)",$line[$g],$regs);
200 $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
201 if ($flags)
202 $boxes[$g]['flags'] = explode(' ', $flags);
203 }
204 }
205
206 return $boxes;
207 }
208
209 /* Apparently you must call a user function with usort instead
210 * of calling a built-in directly. Stupid.
211 * Patch from dave_michmerhuizen@yahoo.com
212 * Allows case insensitivity when sorting folders
213 */
214 function user_strcasecmp($a, $b)
215 {
216 return strcasecmp($a, $b);
217 }
218
219
220 /******************************************************************************
221 ** Returns sorted mailbox lists in several different ways.
222 ** See comment on sqimap_mailbox_parse() for info about the returned array.
223 ******************************************************************************/
224 function sqimap_mailbox_list ($imap_stream) {
225 global $data_dir, $username, $list_special_folders_first;
226 global $folder_prefix, $trash_folder, $sent_folder, $draft_folder;
227 global $move_to_trash, $move_to_sent, $save_as_draft;
228 global $delimiter;
229
230 $inbox_in_list = false;
231 $inbox_subscribed = false;
232
233 require_once('../src/load_prefs.php');
234 require_once('../functions/array.php');
235
236 /** LSUB array **/
237 fputs ($imap_stream, sqimap_session_id() . " LSUB \"$folder_prefix\" \"*\"\r\n");
238 $lsub_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
239
240 // Section about removing the last element was removed
241 // We don't return "* OK" anymore from sqimap_read_data
242
243 $sorted_lsub_ary = array();
244 for ($i=0;$i < count($lsub_ary); $i++) {
245 // Workaround for EIMS
246 // Doesn't work if the mailbox name is multiple lines
247 if (isset($lsub_ary[$i + 1]) &&
248 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
249 $lsub_ary[$i], $regs)) {
250 $i ++;
251 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
252 '"' . $regs[2];
253 }
254 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
255 $sorted_lsub_ary[] = $temp_mailbox_name;
256 if (strtoupper($temp_mailbox_name) == 'INBOX')
257 $inbox_subscribed = true;
258 }
259 $new_ary = array();
260 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
261 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
262 $new_ary[] = $sorted_lsub_ary[$i];
263 }
264 }
265 $sorted_lsub_ary = $new_ary;
266 if (isset($sorted_lsub_ary)) {
267 usort($sorted_lsub_ary, 'user_strcasecmp');
268 //sort($sorted_lsub_ary);
269 }
270
271 /** LIST array **/
272 $sorted_list_ary = array();
273 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
274 if (substr($sorted_lsub_ary[$i], -1) == $delimiter)
275 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
276 else
277 $mbx = $sorted_lsub_ary[$i];
278
279 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mbx\"\r\n");
280 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
281 // Another workaround for EIMS
282 if (isset($read[1]) &&
283 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
284 $read[0], $regs)) {
285 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
286 '"' . $regs[2];
287 }
288 if (isset($sorted_list_ary[$i]))
289 $sorted_list_ary[$i] = "";
290 if (isset($read[0]))
291 $sorted_list_ary[$i] = $read[0];
292 else
293 $sorted_list_ary[$i] = "";
294 if (isset($sorted_list_ary[$i]) &&
295 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == "INBOX")
296 $inbox_in_list = true;
297 }
298
299 /**
300 * Just in case they're not subscribed to their inbox,
301 * we'll get it for them anyway
302 */
303 if ($inbox_subscribed == false || $inbox_in_list == false) {
304 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"INBOX\"\r\n");
305 $inbox_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
306 // Another workaround for EIMS
307 if (isset($inbox_ary[1]) &&
308 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
309 $inbox_ary[0], $regs)) {
310 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
311 '"' . $regs[2];
312 }
313
314 $sorted_list_ary[] = $inbox_ary[0];
315 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
316 }
317
318 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
319
320 /** Now, lets sort for special folders **/
321 $boxesnew = Array();
322
323 /* Find INBOX */
324 for ($i = 0; $i < count($boxes); $i++) {
325 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
326 $boxesnew[] = $boxes[$i];
327 $used[$i] = true;
328 $i = count($boxes);
329 }
330 }
331
332 /* List special folders and their subfolders, if requested. */
333 if ($list_special_folders_first == true) {
334 /* First list the trash folder. */
335 for ($i = 0 ; $i < count($boxes) ; $i++) {
336 if ($move_to_trash &&
337 eregi('^' . quotemeta($trash_folder) . '(' .
338 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
339 $boxesnew[] = $boxes[$i];
340 $used[$i] = true;
341 }
342 }
343
344 /* Then list the sent folder. */
345 for ($i = 0 ; $i < count($boxes) ; $i++) {
346 if ($move_to_sent &&
347 eregi('^' . quotemeta($sent_folder) . '(' .
348 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
349 $boxesnew[] = $boxes[$i];
350 $used[$i] = true;
351 }
352 }
353
354 /* Lastly, list the list the draft folder. */
355 for ($i = 0 ; $i < count($boxes) ; $i++) {
356 if ($save_as_draft &&
357 eregi('^' . quotemeta($draft_folder) . '(' .
358 quotemeta($delimiter) . '.*)?$', $boxes[$i]['unformatted'])) {
359 $boxesnew[] = $boxes[$i];
360 $used[$i] = true;
361 }
362 }
363
364 /* Put INBOX.* folders ahead of the rest. */
365 for ($i = 0; $i < count($boxes); $i++) {
366 if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
367 (!isset($used[$i]) || $used[$i] == false)) {
368 $boxesnew[] = $boxes[$i];
369 $used[$i] = true;
370 }
371 }
372 }
373
374 // Rest of the folders
375 for ($i = 0; $i < count($boxes); $i++) {
376 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
377 (!isset($used[$i]) || $used[$i] == false)) {
378 $boxesnew[] = $boxes[$i];
379 $used[$i] = true;
380 }
381 }
382
383 return $boxesnew;
384 }
385
386 /******************************************************************************
387 ** Returns a list of all folders, subscribed or not
388 ******************************************************************************/
389 function sqimap_mailbox_list_all ($imap_stream) {
390 global $list_special_folders_first, $folder_prefix;
391 global $delimiter;
392
393 if (!function_exists ("ary_sort"))
394 include_once('../functions/array.php');
395
396 $ssid = sqimap_session_id();
397 $lsid = strlen( $ssid );
398 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
399 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
400 $g = 0;
401 $phase = "inbox";
402
403 for ($i = 0; $i < count($read_ary); $i++) {
404 // Another workaround for EIMS
405 if (isset($read_ary[$i + 1]) &&
406 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
407 $read_ary[$i], $regs)) {
408 $i ++;
409 $read_ary[$i] = $regs[1] . '"' .
410 addslashes(trim($read_ary[$i])) .
411 '"' . $regs[2];
412 }
413 if (substr ($read_ary[$i], 0, $lsid) != $ssid ) {
414
415 // Store the raw IMAP reply
416 $boxes[$g]["raw"] = $read_ary[$i];
417
418 // Count number of delimiters ($delimiter) in folder name
419 $mailbox = find_mailbox_name($read_ary[$i]);
420 $dm_count = countCharInString($mailbox, $delimiter);
421 if (substr($mailbox, -1) == $delimiter)
422 $dm_count--; // If name ends in delimiter - decrement count by one
423
424 // Format folder name, but only if it's a INBOX.* or have
425 // a parent.
426 $boxesbyname[$mailbox] = $g;
427 $parentfolder = readMailboxParent($mailbox, $delimiter);
428 if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
429 (ereg('^'.$folder_prefix, $mailbox)) ||
430 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
431 if ($dm_count)
432 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
433 else
434 $boxes[$g]["formatted"] = '';
435 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $delimiter);
436 } else {
437 $boxes[$g]["formatted"] = $mailbox;
438 }
439
440 $boxes[$g]["unformatted-dm"] = $mailbox;
441 if (substr($mailbox, -1) == $delimiter)
442 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
443 $boxes[$g]["unformatted"] = $mailbox;
444 $boxes[$g]["unformatted-disp"] = ereg_replace('^' . $folder_prefix, '', $mailbox);
445 $boxes[$g]["id"] = $g;
446
447 /** Now lets get the flags for this mailbox **/
448 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
449 $read_mlbx = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
450
451 // Another workaround for EIMS
452 if (isset($read_mlbx[1]) &&
453 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
454 $read_mlbx[0], $regs)) {
455 $read_mlbx[0] = $regs[1] . '"' .
456 addslashes(trim($read_mlbx[1])) .
457 '"' . $regs[2];
458 }
459
460 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
461 $flags = substr($flags, 0, strpos($flags, ")"));
462 $flags = str_replace('\\', '', $flags);
463 $flags = trim(strtolower($flags));
464 if ($flags) {
465 $boxes[$g]['flags'] = explode(" ", $flags);
466 }
467 else
468 {
469 $boxes[$g]['flags'] = array();
470 }
471 }
472 $g++;
473 }
474 if(is_array($boxes)) {
475 $boxes = ary_sort ($boxes, "unformatted", 1);
476 }
477
478 return $boxes;
479 }
480
481 ?>