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