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