Use sqimap_session_id instead of axxxx for imap connections.
[squirrelmail.git] / functions / imap_mailbox.php
1 <?php
2 /**
3 ** imap_mailbox.php
4 **
5 ** This impliments all functions that manipulate mailboxes
6 **
7 ** $Id$
8 **/
9
10 if (defined ('imap_mailbox_php'))
11 return;
12 define ('imap_mailbox_php', true);
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 if (strtolower($type) == "noselect") {
66 $dm = sqimap_get_delimiter($imap_stream);
67 $mailbox = $mailbox.$dm;
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, $dm) {
137 global $folder_prefix;
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 ($dm) in folder name
150 $mailbox = trim($line_lsub[$g]);
151 $dm_count = countCharInString($mailbox, $dm);
152 if (substr($mailbox, -1) == $dm)
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, $dm);
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, $dm));
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, $dm);
168 } else {
169 $boxes[$g]["formatted"] = $mailbox;
170 }
171
172 $boxes[$g]['unformatted-dm'] = $mailbox;
173 if (substr($mailbox, -1) == $dm)
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 $trash_folder, $sent_folder;
211 global $move_to_trash, $move_to_sent, $folder_prefix;
212
213 $inbox_in_list = false;
214 $inbox_subscribed = false;
215
216 require_once('../src/load_prefs.php');
217 require_once('../functions/array.php');
218
219 $dm = sqimap_get_delimiter ($imap_stream);
220
221 /** LSUB array **/
222 fputs ($imap_stream, sqimap_session_id() . " LSUB \"$folder_prefix\" \"*\"\r\n");
223 $lsub_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
224
225 // Section about removing the last element was removed
226 // We don't return "* OK" anymore from sqimap_read_data
227
228 $sorted_lsub_ary = array();
229 for ($i=0;$i < count($lsub_ary); $i++) {
230 // Workaround for EIMS
231 // Doesn't work if the mailbox name is multiple lines
232 if (isset($lsub_ary[$i + 1]) &&
233 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
234 $lsub_ary[$i], $regs)) {
235 $i ++;
236 $lsub_ary[$i] = $regs[1] . '"' . addslashes(trim($lsub_ary[$i])) .
237 '"' . $regs[2];
238 }
239 $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
240 $sorted_lsub_ary[] = $temp_mailbox_name;
241 if (strtoupper($temp_mailbox_name) == 'INBOX')
242 $inbox_subscribed = true;
243 }
244 $new_ary = array();
245 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
246 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
247 $new_ary[] = $sorted_lsub_ary[$i];
248 }
249 }
250 $sorted_lsub_ary = $new_ary;
251 if (isset($sorted_lsub_ary)) {
252 usort($sorted_lsub_ary, 'user_strcasecmp');
253 //sort($sorted_lsub_ary);
254 }
255
256 /** LIST array **/
257 $sorted_list_ary = array();
258 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
259 if (substr($sorted_lsub_ary[$i], -1) == $dm)
260 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
261 else
262 $mbx = $sorted_lsub_ary[$i];
263
264 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mbx\"\r\n");
265 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
266 // Another workaround for EIMS
267 if (isset($read[1]) &&
268 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
269 $read[0], $regs)) {
270 $read[0] = $regs[1] . '"' . addslashes(trim($read[1])) .
271 '"' . $regs[2];
272 }
273 if (isset($sorted_list_ary[$i]))
274 $sorted_list_ary[$i] = "";
275 if (isset($read[0]))
276 $sorted_list_ary[$i] = $read[0];
277 else
278 $sorted_list_ary[$i] = "";
279 if (isset($sorted_list_ary[$i]) &&
280 strtoupper(find_mailbox_name($sorted_list_ary[$i])) == "INBOX")
281 $inbox_in_list = true;
282 }
283
284 /** Just in case they're not subscribed to their inbox, we'll get it
285 for them anyway **/
286 if ($inbox_subscribed == false || $inbox_in_list == false) {
287 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"INBOX\"\r\n");
288 $inbox_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
289 // Another workaround for EIMS
290 if (isset($inbox_ary[1]) &&
291 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
292 $inbox_ary[0], $regs)) {
293 $inbox_ary[0] = $regs[1] . '"' . addslashes(trim($inbox_ary[1])) .
294 '"' . $regs[2];
295 }
296
297 $sorted_list_ary[] = $inbox_ary[0];
298 $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
299 }
300
301 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
302
303
304 /** Now, lets sort for special folders **/
305
306 $boxesnew = Array();
307
308 // Find INBOX
309 for ($i = 0; $i < count($boxes); $i++) {
310 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
311 $boxesnew[] = $boxes[$i];
312 $used[$i] = true;
313 $i = count($boxes);
314 }
315 }
316
317 if ($list_special_folders_first == true) {
318
319 // Then list special folders and their subfolders
320 for ($i = 0 ; $i < count($boxes) ; $i++) {
321 if ($move_to_trash &&
322 eregi('^' . quotemeta($trash_folder) . '(' .
323 quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
324 $boxesnew[] = $boxes[$i];
325 $used[$i] = true;
326 }
327 elseif ($move_to_sent &&
328 eregi('^' . quotemeta($sent_folder) . '(' .
329 quotemeta($dm) . '.*)?$', $boxes[$i]["unformatted"])) {
330 $boxesnew[] = $boxes[$i];
331 $used[$i] = true;
332 }
333 }
334
335 // Put INBOX.* folders ahead of the rest
336 for ($i = 0; $i < count($boxes); $i++) {
337 if (eregi('^inbox\\.', $boxes[$i]["unformatted"]) &&
338 (!isset($used[$i]) || $used[$i] == false)) {
339 $boxesnew[] = $boxes[$i];
340 $used[$i] = true;
341 }
342 }
343
344 }
345
346 // Rest of the folders
347 for ($i = 0; $i < count($boxes); $i++) {
348 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
349 (!isset($used[$i]) || $used[$i] == false)) {
350 $boxesnew[] = $boxes[$i];
351 $used[$i] = true;
352 }
353 }
354
355 return $boxesnew;
356 }
357
358 /******************************************************************************
359 ** Returns a list of all folders, subscribed or not
360 ******************************************************************************/
361 function sqimap_mailbox_list_all ($imap_stream) {
362 global $list_special_folders_first, $folder_prefix;
363
364 if (!function_exists ("ary_sort"))
365 include_once('../functions/array.php');
366
367 $dm = sqimap_get_delimiter ($imap_stream);
368
369 $ssid = sqimap_session_id();
370 $lsid = strlen( $ssid );
371 fputs ($imap_stream, $ssid . " LIST \"$folder_prefix\" *\r\n");
372 $read_ary = sqimap_read_data ($imap_stream, $ssid, true, $response, $message);
373 $g = 0;
374 $phase = "inbox";
375
376 for ($i = 0; $i < count($read_ary); $i++) {
377 // Another workaround for EIMS
378 if (isset($read_ary[$i + 1]) &&
379 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
380 $read_ary[$i], $regs)) {
381 $i ++;
382 $read_ary[$i] = $regs[1] . '"' .
383 addslashes(trim($read_ary[$i])) .
384 '"' . $regs[2];
385 }
386 if (substr ($read_ary[$i], 0, $lsid) != $ssid ) {
387
388 // Store the raw IMAP reply
389 $boxes[$g]["raw"] = $read_ary[$i];
390
391 // Count number of delimiters ($dm) in folder name
392 $mailbox = find_mailbox_name($read_ary[$i]);
393 $dm_count = countCharInString($mailbox, $dm);
394 if (substr($mailbox, -1) == $dm)
395 $dm_count--; // If name ends in delimiter - decrement count by one
396
397 // Format folder name, but only if it's a INBOX.* or have
398 // a parent.
399 $boxesbyname[$mailbox] = $g;
400 $parentfolder = readMailboxParent($mailbox, $dm);
401 if((eregi('^inbox'.quotemeta($dm), $mailbox)) ||
402 (ereg('^'.$folder_prefix, $mailbox)) ||
403 ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
404 if ($dm_count)
405 $boxes[$g]["formatted"] = str_repeat("&nbsp;&nbsp;", $dm_count);
406 else
407 $boxes[$g]["formatted"] = '';
408 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
409 } else {
410 $boxes[$g]["formatted"] = $mailbox;
411 }
412
413 $boxes[$g]["unformatted-dm"] = $mailbox;
414 if (substr($mailbox, -1) == $dm)
415 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
416 $boxes[$g]["unformatted"] = $mailbox;
417 $boxes[$g]["unformatted-disp"] = ereg_replace('^' . $folder_prefix, '', $mailbox);
418 $boxes[$g]["id"] = $g;
419
420 /** Now lets get the flags for this mailbox **/
421 fputs ($imap_stream, sqimap_session_id() . " LIST \"\" \"$mailbox\"\r\n");
422 $read_mlbx = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $response, $message);
423
424 // Another workaround for EIMS
425 if (isset($read_mlbx[1]) &&
426 ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
427 $read_mlbx[0], $regs)) {
428 $read_mlbx[0] = $regs[1] . '"' .
429 addslashes(trim($read_mlbx[1])) .
430 '"' . $regs[2];
431 }
432
433 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
434 $flags = substr($flags, 0, strpos($flags, ")"));
435 $flags = str_replace('\\', '', $flags);
436 $flags = trim(strtolower($flags));
437 if ($flags) {
438 $boxes[$g]['flags'] = explode(" ", $flags);
439 }
440 else
441 {
442 $boxes[$g]['flags'] = array();
443 }
444 }
445 $g++;
446 }
447 if(is_array($boxes)) {
448 $boxes = ary_sort ($boxes, "unformatted", 1);
449 }
450
451 return $boxes;
452 }
453
454 ?>