- added patch from srakhada that uses quotemeta() function around password to allow
[squirrelmail.git] / functions / imap_mailbox.php
CommitLineData
59177427 1<?php
8e9e8afa 2 /**
3 ** imap_mailbox.php
4 **
5 ** This impliments all functions that manipulate mailboxes
6 **/
7
8 /******************************************************************************
9 ** Expunges a mailbox
10 ******************************************************************************/
a7f3c40d 11 function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) {
8e9e8afa 12 sqimap_mailbox_select ($imap_stream, $mailbox);
13 fputs ($imap_stream, "a001 EXPUNGE\r\n");
8cf653ad 14 $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message);
a2790a61 15 sqimap_mailbox_close ($imap_stream);
8e9e8afa 16 }
17
18
19 /******************************************************************************
20 ** Checks whether or not the specified mailbox exists
21 ******************************************************************************/
22 function sqimap_mailbox_exists ($imap_stream, $mailbox) {
26511b3e 23 fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n");
24 $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message);
44f642f5 25 if ($mailbox) {
f9b3e5d9 26 return !!(ereg ("$mailbox", $mbx[0])); // To force into true/false
8e9e8afa 27 }
8e9e8afa 28 }
29
a2790a61 30 /******************************************************************************
31 ** Closes an open mailbox
32 ******************************************************************************/
33 function sqimap_mailbox_close ($imap_stream) {
34 fputs ($imap_stream, "a001 CLOSE\r\n");
c95df380 35 $tmp = sqimap_read_data($imap_stream, "a001", false, $response, $message);
a2790a61 36 }
8e9e8afa 37
8e9e8afa 38 /******************************************************************************
39 ** Selects a mailbox
40 ******************************************************************************/
04632dbc 41 function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) {
3751dc7f 42 global $auto_expunge;
43
8e9e8afa 44 fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n");
c5eb2c03 45 $read = sqimap_read_data($imap_stream, "a001", true, $response, $message);
04632dbc 46 if ($recent) {
47 for ($i=0; $i<count($read); $i++) {
48 if (strpos(strtolower($read[$i]), "recent")) {
49 $r = explode(" ", $read[$i]);
50 }
51 }
52 return $r[1];
53 }
3751dc7f 54 if ($auto_expunge) {
55 fputs ($imap_stream, "a001 EXPUNGE\r\n");
8cf653ad 56 $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b);
3751dc7f 57 }
8e9e8afa 58 }
59
60
61
62 /******************************************************************************
63 ** Creates a folder
64 ******************************************************************************/
65 function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
66 if (strtolower($type) == "noselect") {
67 $dm = sqimap_get_delimiter($imap_stream);
68 $mailbox = $mailbox.$dm;
69 }
70 fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n");
71 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
72
73 sqimap_subscribe ($imap_stream, $mailbox);
74 }
75
76
77
78 /******************************************************************************
79 ** Subscribes to an existing folder
80 ******************************************************************************/
81 function sqimap_subscribe ($imap_stream, $mailbox) {
82 fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n");
83 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
84 }
85
86
87
88
89 /******************************************************************************
90 ** Unsubscribes to an existing folder
91 ******************************************************************************/
92 function sqimap_unsubscribe ($imap_stream, $mailbox) {
cbdc5621 93 global $imap_server_type;
94
8e9e8afa 95 fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n");
96 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
97 }
98
99
100
101
102 /******************************************************************************
103 ** This function simply deletes the given folder
104 ******************************************************************************/
105 function sqimap_mailbox_delete ($imap_stream, $mailbox) {
106 fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n");
107 $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message);
108 sqimap_unsubscribe ($imap_stream, $mailbox);
109 }
110
111
112 /******************************************************************************
113 ** Formats a mailbox into 4 parts for the $boxes array
114 ******************************************************************************/
16530f8b 115 function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
aed206bf 116 global $folder_prefix;
8e9e8afa 117 for ($g=0; $g < count($line); $g++) {
118 $boxes[$g]["raw"] = $line[$g];
119
6477eb2d 120 $mailbox = trim($line_lsub[$g]);
8e9e8afa 121 $dm_count = countCharInString($mailbox, $dm);
122 if (substr($mailbox, -1) == $dm)
123 $dm_count--;
124
aed206bf 125 for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++)
390372b4 126 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . "&nbsp;&nbsp;";
8e9e8afa 127 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
128
129 $boxes[$g]["unformatted-dm"] = $mailbox;
130 if (substr($mailbox, -1) == $dm)
131 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
132 $boxes[$g]["unformatted"] = $mailbox;
133 $boxes[$g]["id"] = $g;
134
f9b3e5d9 135 ereg("\(([^)]*)\)",$line[$g],$regs);
136 $flags = trim(strtolower(str_replace("\\", "",$regs[1])));
8e9e8afa 137 if ($flags) {
138 $boxes[$g]["flags"] = explode(" ", $flags);
139 }
8e9e8afa 140 }
141 return $boxes;
142 }
10359c65 143
144 /* patch from dave_michmerhuizen@yahoo.com
145 * allows case insensativity when sorting folders
146 */
147 function _icmp ($a, $b) {
148 return strcasecmp($a, $b);
149 }
8e9e8afa 150
151 /******************************************************************************
152 ** Returns sorted mailbox lists in several different ways.
153 ** The array returned looks like this:
154 ******************************************************************************/
155 function sqimap_mailbox_list ($imap_stream) {
aed206bf 156 global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first;
6477eb2d 157 global $trash_folder, $sent_folder;
f9b3e5d9 158 global $move_to_trash, $move_to_sent;
8e9e8afa 159
1590a668 160 $inbox_in_list = false;
161 $inbox_subscribed = false;
162
8e9e8afa 163 if (!isset($load_prefs_php)) include "../src/load_prefs.php";
164 else global $folder_prefix;
165 if (!function_exists ("ary_sort")) include "../functions/array.php";
166
167 $dm = sqimap_get_delimiter ($imap_stream);
168
8e9e8afa 169 /** LSUB array **/
170 $inbox_subscribed = false;
6477eb2d 171 fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n");
8e9e8afa 172 $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
6477eb2d 173
8e9e8afa 174 for ($i=0;$i < count($lsub_ary); $i++) {
175 $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]);
8e9e8afa 176 if ($sorted_lsub_ary[$i] == "INBOX")
177 $inbox_subscribed = true;
178 }
ee62ce13 179 $new_ary = array();
180 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
181 if (!in_array($sorted_lsub_ary[$i], $new_ary)) {
182 $new_ary[] = $sorted_lsub_ary[$i];
183 }
184 }
185 $sorted_lsub_ary = $new_ary;
8e9e8afa 186 if (isset($sorted_lsub_ary)) {
10359c65 187 usort($sorted_lsub_ary, "_icmp");
188 //sort($sorted_lsub_ary);
8e9e8afa 189 }
190
3cb866d7 191 /** LIST array **/
f9b3e5d9 192 for ($i=0; $i < count($sorted_lsub_ary); $i++) {
193 if (substr($sorted_lsub_ary[$i], -1) == $dm)
194 $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
195 else
196 $mbx = $sorted_lsub_ary[$i];
197
198 fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n");
199 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
200 $sorted_list_ary[$i] = $read[0];
3cb866d7 201 if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX")
202 $inbox_in_list = true;
f9b3e5d9 203 }
3cb866d7 204
8e9e8afa 205 /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/
206 if ($inbox_subscribed == false || $inbox_in_list == false) {
207 fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n");
208 $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
591000ec 209
210 $pos = count($sorted_list_ary);
211 $sorted_list_ary[$pos] = $inbox_ary[0];
212
213 $pos = count($sorted_lsub_ary);
214 $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]);
8e9e8afa 215 }
216
f9b3e5d9 217 $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm);
218
219 /** Now, lets sort for special folders **/
8e9e8afa 220 for ($i = 0; $i < count($boxes); $i++) {
1e0628fb 221 if (strtolower($boxes[$i]["unformatted"]) == "inbox") {
8e9e8afa 222 $boxesnew[0] = $boxes[$i];
223 $boxes[$i]["used"] = true;
c5eb2c03 224 $i = count($boxes);
8e9e8afa 225 }
226 }
aed206bf 227
8e9e8afa 228 if ($list_special_folders_first == true) {
c5eb2c03 229 for ($i = count($boxes)-1; $i >= 0 ; $i--) {
1e0628fb 230 if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) {
231 $pos = count($boxesnew);
232 $boxesnew[$pos] = $boxes[$i];
233 $boxes[$i]["used"] = true;
c5eb2c03 234 $trash_found = true;
1e0628fb 235 }
236 else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) {
237 $pos = count($boxesnew);
238 $boxesnew[$pos] = $boxes[$i];
239 $boxes[$i]["used"] = true;
c5eb2c03 240 $sent_found = true;
8e9e8afa 241 }
c5eb2c03 242
243 if (($sent_found && $trash_found) || ($sent_found && !$move_to_trash) || ($trash_found && !$move_to_sent) || (!$move_to_sent && !$move_to_trash))
244 $i = -1;
8e9e8afa 245 }
246 }
c5eb2c03 247
8e9e8afa 248 for ($i = 0; $i < count($boxes); $i++) {
1e0628fb 249 if ((strtolower($boxes[$i]["unformatted"]) != "inbox") &&
8e9e8afa 250 ($boxes[$i]["used"] == false)) {
251 $pos = count($boxesnew);
252 $boxesnew[$pos] = $boxes[$i];
253 $boxes[$i]["used"] = true;
254 }
255 }
256
257 return $boxesnew;
258 }
8e9e8afa 259
260 /******************************************************************************
261 ** Returns a list of all folders, subscribed or not
262 ******************************************************************************/
263 function sqimap_mailbox_list_all ($imap_stream) {
1e0628fb 264 global $list_special_folders_first, $folder_prefix;
8e9e8afa 265
266 if (!function_exists ("ary_sort"))
267 include ("../functions/array.php");
268
269 $dm = sqimap_get_delimiter ($imap_stream);
270
271 fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n");
272 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
273 $g = 0;
274 $phase = "inbox";
275 for ($i = 0; $i < count($read_ary); $i++) {
276 if (substr ($read_ary[$i], 0, 4) != "a001") {
277 $boxes[$g]["raw"] = $read_ary[$i];
278
279 $mailbox = find_mailbox_name($read_ary[$i]);
280 $dm_count = countCharInString($mailbox, $dm);
281 if (substr($mailbox, -1) == $dm)
282 $dm_count--;
283
284 for ($j = 0; $j < $dm_count; $j++)
285 $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " ";
286 $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
287
288 $boxes[$g]["unformatted-dm"] = $mailbox;
289 if (substr($mailbox, -1) == $dm)
290 $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
291 $boxes[$g]["unformatted"] = $mailbox;
292 $boxes[$g]["id"] = $g;
293
294 /** Now lets get the flags for this mailbox **/
295 fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n");
296 $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message);
297
298 $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1);
299 $flags = substr($flags, 0, strpos($flags, ")"));
300 $flags = str_replace("\\", "", $flags);
301 $flags = trim(strtolower($flags));
302 if ($flags) {
303 $boxes[$g]["flags"] = explode(" ", $flags);
304 }
305 }
306 $g++;
307 }
591000ec 308 if ($boxes) {
309 $boxes = ary_sort ($boxes, "unformatted", 1);
310 }
8e9e8afa 311 return $boxes;
312 }
313
052e0c26 314?>