| 1 | <?php |
| 2 | /** |
| 3 | ** imap_mailbox.php |
| 4 | ** |
| 5 | ** This impliments all functions that manipulate mailboxes |
| 6 | **/ |
| 7 | |
| 8 | /****************************************************************************** |
| 9 | ** Expunges a mailbox |
| 10 | ******************************************************************************/ |
| 11 | function sqimap_mailbox_expunge ($imap_stream, $mailbox) { |
| 12 | sqimap_mailbox_select ($imap_stream, $mailbox); |
| 13 | fputs ($imap_stream, "a001 EXPUNGE\r\n"); |
| 14 | $read = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
| 15 | } |
| 16 | |
| 17 | |
| 18 | /****************************************************************************** |
| 19 | ** Checks whether or not the specified mailbox exists |
| 20 | ******************************************************************************/ |
| 21 | function sqimap_mailbox_exists ($imap_stream, $mailbox) { |
| 22 | $boxes = sqimap_mailbox_list ($imap_stream); |
| 23 | $found = false; |
| 24 | for ($i = 0; $i < count ($boxes); $i++) { |
| 25 | if ($boxes[$i]["unformatted"] == $mailbox) |
| 26 | $found = true; |
| 27 | } |
| 28 | return $found; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | |
| 33 | /****************************************************************************** |
| 34 | ** Selects a mailbox |
| 35 | ******************************************************************************/ |
| 36 | function sqimap_mailbox_select ($imap_stream, $mailbox, $hide) { |
| 37 | fputs ($imap_stream, "a001 SELECT \"$mailbox\"\r\n"); |
| 38 | $read = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | |
| 43 | /****************************************************************************** |
| 44 | ** Creates a folder |
| 45 | ******************************************************************************/ |
| 46 | function sqimap_mailbox_create ($imap_stream, $mailbox, $type) { |
| 47 | if (strtolower($type) == "noselect") { |
| 48 | $dm = sqimap_get_delimiter($imap_stream); |
| 49 | $mailbox = $mailbox.$dm; |
| 50 | } |
| 51 | fputs ($imap_stream, "a001 CREATE \"$mailbox\"\r\n"); |
| 52 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
| 53 | |
| 54 | sqimap_subscribe ($imap_stream, $mailbox); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | |
| 59 | /****************************************************************************** |
| 60 | ** Subscribes to an existing folder |
| 61 | ******************************************************************************/ |
| 62 | function sqimap_subscribe ($imap_stream, $mailbox) { |
| 63 | fputs ($imap_stream, "a001 SUBSCRIBE \"$mailbox\"\r\n"); |
| 64 | $read_ary = sqimap_read_data($imap_stream, "a001", true, $response, $message); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | /****************************************************************************** |
| 71 | ** Unsubscribes to an existing folder |
| 72 | ******************************************************************************/ |
| 73 | function sqimap_unsubscribe ($imap_stream, $mailbox) { |
| 74 | global $imap_server_type; |
| 75 | |
| 76 | /** This is a hack for UW server |
| 77 | ** Sometimes a folder will have a / at the end. If that's the case, |
| 78 | ** the unsubscribe doesn't work for a box named "mailbox/". We have |
| 79 | ** to strip off the / at the end. There may be a better way of doing |
| 80 | ** this, but this is the best I've found so far. (lme - April 26, 2000) |
| 81 | **/ |
| 82 | if ($imap_server_type == "uw") { |
| 83 | if (substr($mailbox, -1) == "/") { |
| 84 | $mailbox = substr($mailbox, 0, strlen($mailbox)-1); |
| 85 | } |
| 86 | } |
| 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 | function sqimap_mailbox_parse ($line, $dm) { |
| 109 | global $folder_prefix; |
| 110 | for ($g=0; $g < count($line); $g++) { |
| 111 | $boxes[$g]["raw"] = $line[$g]; |
| 112 | |
| 113 | $mailbox = find_mailbox_name($line[$g]); |
| 114 | $dm_count = countCharInString($mailbox, $dm); |
| 115 | if (substr($mailbox, -1) == $dm) |
| 116 | $dm_count--; |
| 117 | |
| 118 | for ($j = 0; $j < $dm_count - (countCharInString($folder_prefix, $dm)); $j++) |
| 119 | $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " "; |
| 120 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
| 121 | |
| 122 | $boxes[$g]["unformatted-dm"] = $mailbox; |
| 123 | if (substr($mailbox, -1) == $dm) |
| 124 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
| 125 | $boxes[$g]["unformatted"] = $mailbox; |
| 126 | $boxes[$g]["id"] = $g; |
| 127 | |
| 128 | $flags = substr($line[$g], strpos($line[$g], "(")+1); |
| 129 | $flags = substr($flags, 0, strpos($flags, ")")); |
| 130 | $flags = str_replace("\\", "", $flags); |
| 131 | $flags = trim(strtolower($flags)); |
| 132 | if ($flags) { |
| 133 | $boxes[$g]["flags"] = explode(" ", $flags); |
| 134 | } |
| 135 | /**** I'm not sure why this was even in here to begin with.. (lme) |
| 136 | for ($i=0; $i < count($boxes[$g]["flags"]); $i++) { |
| 137 | if ($boxes[$g]["flags"][$i] == "noselect") { |
| 138 | $boxes[$g]["unformatted-dm"] = $boxes[$g]["unformatted-dm"].$dm; |
| 139 | // echo $boxes[$g]["unformatted-dm"]." - debug<br>"; |
| 140 | } |
| 141 | } |
| 142 | ****/ |
| 143 | } |
| 144 | return $boxes; |
| 145 | } |
| 146 | |
| 147 | /****************************************************************************** |
| 148 | ** Returns sorted mailbox lists in several different ways. |
| 149 | ** The array returned looks like this: |
| 150 | ******************************************************************************/ |
| 151 | function sqimap_mailbox_list ($imap_stream) { |
| 152 | global $load_prefs_php, $prefs_php, $config_php, $data_dir, $username, $list_special_folders_first; |
| 153 | global $trash_folder, $sent_folder; |
| 154 | global $move_to_trash, $move_to_sent; |
| 155 | |
| 156 | $inbox_in_list = false; |
| 157 | $inbox_subscribed = false; |
| 158 | |
| 159 | if (!isset($load_prefs_php)) include "../src/load_prefs.php"; |
| 160 | else global $folder_prefix; |
| 161 | if (!function_exists ("ary_sort")) include "../functions/array.php"; |
| 162 | |
| 163 | $dm = sqimap_get_delimiter ($imap_stream); |
| 164 | |
| 165 | /** LSUB array **/ |
| 166 | $inbox_subscribed = false; |
| 167 | fputs ($imap_stream, "a001 LSUB \"\" \"*\"\r\n"); |
| 168 | $lsub_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
| 169 | for ($i=0;$i < count($lsub_ary); $i++) { |
| 170 | $sorted_lsub_ary[$i] = find_mailbox_name($lsub_ary[$i]); |
| 171 | if (substr($sorted_lsub_ary[$i], -1) == $dm) |
| 172 | $sorted_lsub_ary[$i] = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1); |
| 173 | if ($sorted_lsub_ary[$i] == "INBOX") |
| 174 | $inbox_subscribed = true; |
| 175 | } |
| 176 | if (isset($sorted_lsub_ary)) { |
| 177 | sort($sorted_lsub_ary); |
| 178 | } |
| 179 | |
| 180 | /** LIST array **/ |
| 181 | for ($i=0; $i < count($sorted_lsub_ary); $i++) { |
| 182 | if (substr($sorted_lsub_ary[$i], -1) == $dm) |
| 183 | $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1); |
| 184 | else |
| 185 | $mbx = $sorted_lsub_ary[$i]; |
| 186 | |
| 187 | fputs ($imap_stream, "a001 LIST \"\" \"$mbx\"\r\n"); |
| 188 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
| 189 | $sorted_list_ary[$i] = $read[0]; |
| 190 | if (find_mailbox_name($sorted_list_ary[$i]) == "INBOX") |
| 191 | $inbox_in_list = true; |
| 192 | } |
| 193 | |
| 194 | /** Just in case they're not subscribed to their inbox, we'll get it for them anyway **/ |
| 195 | if ($inbox_subscribed == false || $inbox_in_list == false) { |
| 196 | fputs ($imap_stream, "a001 LIST \"\" \"INBOX\"\r\n"); |
| 197 | $inbox_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
| 198 | $sorted_list_ary[count($sorted_list_ary)] = $inbox_ary[0]; |
| 199 | } |
| 200 | |
| 201 | $boxes = sqimap_mailbox_parse ($sorted_list_ary, $dm); |
| 202 | |
| 203 | /** Now, lets sort for special folders **/ |
| 204 | for ($i = 0; $i < count($boxes); $i++) { |
| 205 | if (strtolower($boxes[$i]["unformatted"]) == "inbox") { |
| 206 | $boxesnew[0] = $boxes[$i]; |
| 207 | $boxes[$i]["used"] = true; |
| 208 | $i = count($boxes); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | if ($list_special_folders_first == true) { |
| 213 | for ($i = count($boxes)-1; $i >= 0 ; $i--) { |
| 214 | if (($boxes[$i]["unformatted"] == $trash_folder) && ($move_to_trash)) { |
| 215 | $pos = count($boxesnew); |
| 216 | $boxesnew[$pos] = $boxes[$i]; |
| 217 | $boxes[$i]["used"] = true; |
| 218 | $trash_found = true; |
| 219 | } |
| 220 | else if (($boxes[$i]["unformatted"] == $sent_folder) && ($move_to_sent)) { |
| 221 | $pos = count($boxesnew); |
| 222 | $boxesnew[$pos] = $boxes[$i]; |
| 223 | $boxes[$i]["used"] = true; |
| 224 | $sent_found = true; |
| 225 | } |
| 226 | |
| 227 | if (($sent_found && $trash_found) || ($sent_found && !$move_to_trash) || ($trash_found && !$move_to_sent) || (!$move_to_sent && !$move_to_trash)) |
| 228 | $i = -1; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | for ($i = 0; $i < count($boxes); $i++) { |
| 233 | if ((strtolower($boxes[$i]["unformatted"]) != "inbox") && |
| 234 | ($boxes[$i]["used"] == false)) { |
| 235 | $pos = count($boxesnew); |
| 236 | $boxesnew[$pos] = $boxes[$i]; |
| 237 | $boxes[$i]["used"] = true; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return $boxesnew; |
| 242 | } |
| 243 | |
| 244 | /****************************************************************************** |
| 245 | ** Returns a list of all folders, subscribed or not |
| 246 | ******************************************************************************/ |
| 247 | function sqimap_mailbox_list_all ($imap_stream) { |
| 248 | global $list_special_folders_first, $folder_prefix; |
| 249 | |
| 250 | if (!function_exists ("ary_sort")) |
| 251 | include ("../functions/array.php"); |
| 252 | |
| 253 | $dm = sqimap_get_delimiter ($imap_stream); |
| 254 | |
| 255 | fputs ($imap_stream, "a001 LIST \"$folder_prefix\" *\r\n"); |
| 256 | $read_ary = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
| 257 | $g = 0; |
| 258 | $phase = "inbox"; |
| 259 | for ($i = 0; $i < count($read_ary); $i++) { |
| 260 | if (substr ($read_ary[$i], 0, 4) != "a001") { |
| 261 | $boxes[$g]["raw"] = $read_ary[$i]; |
| 262 | |
| 263 | $mailbox = find_mailbox_name($read_ary[$i]); |
| 264 | $dm_count = countCharInString($mailbox, $dm); |
| 265 | if (substr($mailbox, -1) == $dm) |
| 266 | $dm_count--; |
| 267 | |
| 268 | for ($j = 0; $j < $dm_count; $j++) |
| 269 | $boxes[$g]["formatted"] = $boxes[$g]["formatted"] . " "; |
| 270 | $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm); |
| 271 | |
| 272 | $boxes[$g]["unformatted-dm"] = $mailbox; |
| 273 | if (substr($mailbox, -1) == $dm) |
| 274 | $mailbox = substr($mailbox, 0, strlen($mailbox) - 1); |
| 275 | $boxes[$g]["unformatted"] = $mailbox; |
| 276 | $boxes[$g]["id"] = $g; |
| 277 | |
| 278 | /** Now lets get the flags for this mailbox **/ |
| 279 | fputs ($imap_stream, "a002 LIST \"\" \"$mailbox\"\r\n"); |
| 280 | $read_mlbx = sqimap_read_data ($imap_stream, "a002", true, $response, $message); |
| 281 | |
| 282 | $flags = substr($read_mlbx[0], strpos($read_mlbx[0], "(")+1); |
| 283 | $flags = substr($flags, 0, strpos($flags, ")")); |
| 284 | $flags = str_replace("\\", "", $flags); |
| 285 | $flags = trim(strtolower($flags)); |
| 286 | if ($flags) { |
| 287 | $boxes[$g]["flags"] = explode(" ", $flags); |
| 288 | } |
| 289 | } |
| 290 | $g++; |
| 291 | } |
| 292 | $boxes = ary_sort ($boxes, "unformatted", 1); |
| 293 | return $boxes; |
| 294 | } |
| 295 | |
| 296 | ?> |