| 1 | <?php |
| 2 | /** |
| 3 | ** imap_mailbox.php |
| 4 | ** |
| 5 | ** This impliments all functions that manipulate mailboxes |
| 6 | ** |
| 7 | ** $Id$ |
| 8 | **/ |
| 9 | |
| 10 | /****************************************************************************** |
| 11 | ** Expunges a mailbox |
| 12 | ******************************************************************************/ |
| 13 | function sqimap_mailbox_expunge ($imap_stream, $mailbox,$handle_errors = true) { |
| 14 | sqimap_mailbox_select ($imap_stream, $mailbox); |
| 15 | fputs ($imap_stream, "a001 EXPUNGE\r\n"); |
| 16 | $read = sqimap_read_data($imap_stream, "a001", $handle_errors, $response, $message); |
| 17 | } |
| 18 | |
| 19 | |
| 20 | /****************************************************************************** |
| 21 | ** Checks whether or not the specified mailbox exists |
| 22 | ******************************************************************************/ |
| 23 | function sqimap_mailbox_exists ($imap_stream, $mailbox) { |
| 24 | fputs ($imap_stream, "a001 LIST \"\" \"$mailbox\"\r\n"); |
| 25 | $mbx = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
| 26 | if ($mailbox) { |
| 27 | return !!(ereg ("$mailbox", $mbx[0])); // To force into true/false |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /****************************************************************************** |
| 32 | ** Selects a mailbox |
| 33 | ******************************************************************************/ |
| 34 | function sqimap_mailbox_select ($imap_stream, $mailbox, $hide=true, $recent=false) { |
| 35 | global $auto_expunge; |
| 36 | |
| 37 | fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n"); |
| 38 | $read = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
| 39 | if ($recent) { |
| 40 | for ($i=0; $i<count($read); $i++) { |
| 41 | if (strpos(strtolower($read[$i]), "recent")) { |
| 42 | $r = explode(" ", $read[$i]); |
| 43 | } |
| 44 | } |
| 45 | return $r[1]; |
| 46 | } |
| 47 | if ($auto_expunge) { |
| 48 | fputs ($imap_stream, "a001 EXPUNGE\r\n"); |
| 49 | $tmp = sqimap_read_data($imap_stream, "a001", false, $a, $b); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
| 54 | |
| 55 | /****************************************************************************** |
| 56 | ** Creates a folder |
| 57 | ******************************************************************************/ |
| 58 | function sqimap_mailbox_create ($imap_stream, $mailbox, $type) { |
| 59 | if (strtolower($type) == "noselect") { |
| 60 | $dm = sqimap_get_delimiter($imap_stream); |
| 61 | $mailbox = $mailbox.$dm; |
| 62 | } |
| 63 | fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n"); |
| 64 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
| 65 | |
| 66 | sqimap_subscribe ($imap_stream, $mailbox); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | /****************************************************************************** |
| 72 | ** Subscribes to an existing folder |
| 73 | ******************************************************************************/ |
| 74 | function sqimap_subscribe ($imap_stream, $mailbox) { |
| 75 | fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n"); |
| 76 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | /****************************************************************************** |
| 83 | ** Unsubscribes to an existing folder |
| 84 | ******************************************************************************/ |
| 85 | function sqimap_unsubscribe ($imap_stream, $mailbox) { |
| 86 | global $imap_server_type; |
| 87 | |
| 88 | fputs ($imap_stream, "a001 UNSUBSCRIBE \"$mailbox\"\r\n"); |
| 89 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | /****************************************************************************** |
| 96 | ** This function simply deletes the given folder |
| 97 | ******************************************************************************/ |
| 98 | function sqimap_mailbox_delete ($imap_stream, $mailbox) { |
| 99 | fputs ($imap_stream, "a001 DELETE \"$mailbox\"\r\n"); |
| 100 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
| 101 | sqimap_unsubscribe ($imap_stream, $mailbox); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /****************************************************************************** |
| 106 | ** Formats a mailbox into 4 parts for the $boxes array |
| 107 | ** |
| 108 | ** The four parts are: |
| 109 | ** |
| 110 | ** raw - Raw LIST/LSUB response from the IMAP server |
| 111 | ** formatted - nicely formatted folder name |
| 112 | ** unformatted - unformatted, but with delimiter at end removed |
| 113 | ** unformatted-dm - folder name as it appears in raw response |
| 114 | ** unformatted-disp - unformatted without $folder_prefix |
| 115 | ** |
| 116 | ******************************************************************************/ |
| 117 | function sqimap_mailbox_parse ($line, $line_lsub, $dm) { |
| 118 | global $folder_prefix; |
| 119 | |
| 120 | // Process each folder line |
| 121 | for ($g=0; $g < count($line); $g++) { |
| 122 | |
| 123 | // Store the raw IMAP reply |
| 124 | $boxes[$g]["raw"] = $line[$g]; |
| 125 | |
| 126 | // Count number of delimiters ($dm) in folder name |
| 127 | $mailbox = trim($line_lsub[$g]); |
| 128 | $dm_count = countCharInString($mailbox, $dm); |
| 129 | if (substr($mailbox, -1) == $dm) |
| 130 | $dm_count--; // If name ends in delimiter - decrement count by one |
| 131 | |
| 132 | // Format folder name, but only if it's a INBOX.* or have |
| 133 | // a parent. |
| 134 | $boxesbyname[$mailbox] = $g; |
| 135 | $parentfolder = readMailboxParent($mailbox, $dm); |
| 136 | if((eregi("^inbox".quotemeta($dm), $mailbox)) || |
| 137 | (ereg("^".$folder_prefix, $mailbox)) || |
| 138 | ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) { |
| 139 | $indent = $dm_count - (countCharInString($folder_prefix, $dm)); |
| 140 | if ($indent) |
| 141 | $boxes[$g]["formatted"] = str_repeat(" ", $indent); |
| 142 | else |
| 143 | $boxes[$g]["formatted"] = ''; |
| 144 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
| 145 | } else { |
| 146 | $boxes[$g]["formatted"] = $mailbox; |
| 147 | } |
| 148 | |
| 149 | $boxes[$g]["unformatted-dm"] = $mailbox; |
| 150 | if (substr($mailbox, -1) == $dm) |
| 151 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
| 152 | $boxes[$g]["unformatted"] = $mailbox; |
| 153 | $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox); |
| 154 | $boxes[$g]["id"] = $g; |
| 155 | |
| 156 | ereg("\(([^)]*)\)",$line[$g],$regs); |
| 157 | $flags = trim(strtolower(str_replace("\\", "",$regs[1]))); |
| 158 | if ($flags) { |
| 159 | $boxes[$g]["flags"] = explode(" ", $flags); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | return $boxes; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /****************************************************************************** |
| 168 | ** Returns sorted mailbox lists in several different ways. |
| 169 | ** See comment on sqimap_mailbox_parse() for info about the returned array. |
| 170 | ******************************************************************************/ |
| 171 | function sqimap_mailbox_list ($imap_stream) { |
| 172 | global $load_prefs_php, $prefs_php, $config_php; |
| 173 | global $data_dir, $username, $list_special_folders_first; |
| 174 | global $trash_folder, $sent_folder; |
| 175 | global $move_to_trash, $move_to_sent; |
| 176 | |
| 177 | $inbox_in_list = false; |
| 178 | $inbox_subscribed = false; |
| 179 | |
| 180 | if (!isset($load_prefs_php)) include "../src/load_prefs.php"; |
| 181 | else global $folder_prefix; |
| 182 | if (!function_exists ("ary_sort")) include "../functions/array.php"; |
| 183 | |
| 184 | $dm = sqimap_get_delimiter ($imap_stream); |
| 185 | |
| 186 | /** LSUB array **/ |
| 187 | $inbox_subscribed = false; |
| 188 | fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n"); |
| 189 | $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
| 190 | |
| 191 | /** OS: we don't want to parse last element of array, 'cause it is OK command, so we unset it **/ |
| 192 | unset($lsub_ary[count($lsub_ary)-1]); |
| 193 | |
| 194 | for ($i=0;$i < count($lsub_ary); $i++) { |
| 195 | $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]); |
| 196 | if ($sorted_lsub_ary[$i] == "INBOX") |
| 197 | $inbox_subscribed = true; |
| 198 | } |
| 199 | $new_ary = array(); |
| 200 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
| 201 | if (!in_array($sorted_lsub_ary[$i], $new_ary)) { |
| 202 | $new_ary[] = $sorted_lsub_ary[$i]; |
| 203 | } |
| 204 | } |
| 205 | $sorted_lsub_ary = $new_ary; |
| 206 | if (isset($sorted_lsub_ary)) { |
| 207 | usort($sorted_lsub_ary, "strcasecmp"); |
| 208 | //sort($sorted_lsub_ary); |
| 209 | } |
| 210 | |
| 211 | /** LIST array **/ |
| 212 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
| 213 | if (substr($sorted_lsub_ary[$i], -1) == $dm) |
| 214 | $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1); |
| 215 | else |
| 216 | $mbx = $sorted_lsub_ary[$i]; |
| 217 | |
| 218 | fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n"); |
| 219 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
| 220 | $sorted_list_ary[$i] = $read[0]; |
| 221 | if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX") |
| 222 | $inbox_in_list = true; |
| 223 | } |
| 224 | |
| 225 | /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/ |
| 226 | if ($inbox_subscribed == false || $inbox_in_list == false) { |
| 227 | fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n"); |
| 228 | $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
| 229 | |
| 230 | $pos = count($sorted_list_ary); |
| 231 | $sorted_list_ary[$pos] = $inbox_ary[0]; |
| 232 | |
| 233 | $pos = count($sorted_lsub_ary); |
| 234 | $sorted_lsub_ary[$pos] = find_mailbox_name($inbox_ary[0]); |
| 235 | } |
| 236 | |
| 237 | $boxes = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary, $dm); |
| 238 | |
| 239 | |
| 240 | /** Now, lets sort for special folders **/ |
| 241 | |
| 242 | $boxesnew = Array(); |
| 243 | |
| 244 | // Find INBOX |
| 245 | for ($i = 0; $i < count($boxes); $i++) { |
| 246 | if (strtolower($boxes[$i]["unformatted"]) == "inbox") { |
| 247 | $boxesnew[] = $boxes[$i]; |
| 248 | $boxes[$i]["used"] = true; |
| 249 | $i = count($boxes); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if ($list_special_folders_first == true) { |
| 254 | |
| 255 | // Then list special folders and their subfolders |
| 256 | for ($i = 0 ; $i < count($boxes) ; $i++) { |
| 257 | if ($move_to_trash && |
| 258 | eregi("^" . quotemeta($trash_folder) . "(" . |
| 259 | quotemeta($dm) . ".*)?$", $boxes[$i]["unformatted"])) { |
| 260 | $boxesnew[] = $boxes[$i]; |
| 261 | $boxes[$i]["used"] = true; |
| 262 | } |
| 263 | elseif ($move_to_sent && |
| 264 | eregi("^" . quotemeta($sent_folder) . "(" . |
| 265 | quotemeta($dm) . ".*)?$", $boxes[$i]["unformatted"])) { |
| 266 | $boxesnew[] = $boxes[$i]; |
| 267 | $boxes[$i]["used"] = true; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // Put INBOX.* folders ahead of the rest |
| 272 | for ($i = 0; $i < count($boxes); $i++) { |
| 273 | if (eregi("^inbox\.", $boxes[$i]["unformatted"]) && |
| 274 | (!isset($boxes[$i]["used"]) || $boxes[$i]["used"] == false)) { |
| 275 | $boxesnew[] = $boxes[$i]; |
| 276 | $boxes[$i]["used"] = true; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | } |
| 281 | |
| 282 | // Rest of the folders |
| 283 | for ($i = 0; $i < count($boxes); $i++) { |
| 284 | if ((strtolower($boxes[$i]["unformatted"]) != "inbox") && |
| 285 | ($boxes[$i]["used"] == false)) { |
| 286 | $boxesnew[] = $boxes[$i]; |
| 287 | $boxes[$i]["used"] = true; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | return $boxesnew; |
| 292 | } |
| 293 | |
| 294 | /****************************************************************************** |
| 295 | ** Returns a list of all folders, subscribed or not |
| 296 | ******************************************************************************/ |
| 297 | function sqimap_mailbox_list_all ($imap_stream) { |
| 298 | global $list_special_folders_first, $folder_prefix; |
| 299 | |
| 300 | if (!function_exists ("ary_sort")) |
| 301 | include ("../functions/array.php"); |
| 302 | |
| 303 | $dm = sqimap_get_delimiter ($imap_stream); |
| 304 | |
| 305 | fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n"); |
| 306 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
| 307 | $g = 0; |
| 308 | $phase = "inbox"; |
| 309 | |
| 310 | for ($i = 0; $i < count($read_ary); $i++) { |
| 311 | if (substr ($read_ary[$i], 0, 4) != "a001") { |
| 312 | |
| 313 | // Store the raw IMAP reply |
| 314 | $boxes[$g]["raw"] = $read_ary[$i]; |
| 315 | |
| 316 | // Count number of delimiters ($dm) in folder name |
| 317 | $mailbox = find_mailbox_name($read_ary[$i]); |
| 318 | $dm_count = countCharInString($mailbox, $dm); |
| 319 | if (substr($mailbox, -1) == $dm) |
| 320 | $dm_count--; // If name ends in delimiter - decrement count by one |
| 321 | |
| 322 | // Format folder name, but only if it's a INBOX.* or have |
| 323 | // a parent. |
| 324 | $boxesbyname[$mailbox] = $g; |
| 325 | $parentfolder = readMailboxParent($mailbox, $dm); |
| 326 | if((eregi("^inbox".quotemeta($dm), $mailbox)) || |
| 327 | (ereg("^".$folder_prefix, $mailbox)) || |
| 328 | ( isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) { |
| 329 | if ($dm_count) |
| 330 | $boxes[$g]["formatted"] = str_repeat(" ", $dm_count); |
| 331 | else |
| 332 | $boxes[$g]["formatted"] = ''; |
| 333 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
| 334 | } else { |
| 335 | $boxes[$g]["formatted"] = $mailbox; |
| 336 | } |
| 337 | |
| 338 | $boxes[$g]["unformatted-dm"] = $mailbox; |
| 339 | if (substr($mailbox, -1) == $dm) |
| 340 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
| 341 | $boxes[$g]["unformatted"] = $mailbox; |
| 342 | $boxes[$g]["unformatted-disp"] = ereg_replace("^" . $folder_prefix, "", $mailbox); |
| 343 | $boxes[$g]["id"] = $g; |
| 344 | |
| 345 | /** Now lets get the flags for this mailbox **/ |
| 346 | fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n"); |
| 347 | $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message); |
| 348 | |
| 349 | $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1); |
| 350 | $flags = substr($flags, 0, strpos($flags, ")")); |
| 351 | $flags = str_replace("\\", "", $flags); |
| 352 | $flags = trim(strtolower($flags)); |
| 353 | if ($flags) { |
| 354 | $boxes[$g]["flags"] = explode(" ", $flags); |
| 355 | } |
| 356 | } |
| 357 | $g++; |
| 358 | } |
| 359 | if(is_array($boxes)) { |
| 360 | $boxes = ary_sort ($boxes, "unformatted", 1); |
| 361 | } |
| 362 | |
| 363 | return $boxes; |
| 364 | } |
| 365 | |
| 366 | ?> |