| 1 | <?php |
| 2 | /** |
| 3 | ** imap_messages.php |
| 4 | ** |
| 5 | ** This implements functions that manipulate messages |
| 6 | **/ |
| 7 | |
| 8 | /****************************************************************************** |
| 9 | ** Copies specified messages to specified folder |
| 10 | ******************************************************************************/ |
| 11 | function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) { |
| 12 | fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n"); |
| 13 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
| 14 | } |
| 15 | |
| 16 | /****************************************************************************** |
| 17 | ** Deletes specified messages and moves them to trash if possible |
| 18 | ******************************************************************************/ |
| 19 | function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) { |
| 20 | global $move_to_trash, $trash_folder, $auto_expunge; |
| 21 | |
| 22 | if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder))) { |
| 23 | sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder); |
| 24 | sqimap_messages_flag ($imap_stream, $start, $end, "Deleted"); |
| 25 | } else { |
| 26 | sqimap_messages_flag ($imap_stream, $start, $end, "Deleted"); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /****************************************************************************** |
| 31 | ** Sets the specified messages with specified flag |
| 32 | ******************************************************************************/ |
| 33 | function sqimap_messages_flag ($imap_stream, $start, $end, $flag) { |
| 34 | fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\r\n"); |
| 35 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
| 36 | } |
| 37 | |
| 38 | /****************************************************************************** |
| 39 | ** Returns some general header information -- FROM, DATE, and SUBJECT |
| 40 | ******************************************************************************/ |
| 41 | class small_header { |
| 42 | var $from, $subject, $date; |
| 43 | } |
| 44 | |
| 45 | function sqimap_get_small_header ($imap_stream, $id, $sent) { |
| 46 | fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Subject)]\r\n"); |
| 47 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
| 48 | |
| 49 | $subject = _("(no subject)"); |
| 50 | $from = _("Unknown Sender"); |
| 51 | $g = 0; |
| 52 | for ($i = 0; $i < count($read); $i++) { |
| 53 | if ($sent == true) { |
| 54 | if (eregi ("^to:", $read[$i])) { |
| 55 | $from = sqimap_find_displayable_name(substr($read[$i], 3)); |
| 56 | } |
| 57 | } else { |
| 58 | if (eregi ("^from:", $read[$i])) { |
| 59 | $from = sqimap_find_displayable_name(substr($read[$i], 5)); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (eregi ("^date:", $read[$i])) { |
| 64 | $date = substr($read[$i], 5); |
| 65 | } else if (eregi ("^subject:", $read[$i])) { |
| 66 | $subject = htmlspecialchars(eregi_replace ("^subject: ", "", $read[$i])); |
| 67 | if (strlen(Chop($subject)) == 0) |
| 68 | $subject = _("(no subject)"); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | $header = new small_header; |
| 73 | $header->from = $from; |
| 74 | $header->date = $date; |
| 75 | $header->subject = $subject; |
| 76 | |
| 77 | return $header; |
| 78 | } |
| 79 | |
| 80 | /****************************************************************************** |
| 81 | ** Returns the flags for the specified messages |
| 82 | ******************************************************************************/ |
| 83 | function sqimap_get_flags ($imap_stream, $i) { |
| 84 | fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n"); |
| 85 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
| 86 | if (strpos($read[0], "FLAGS")) { |
| 87 | $tmp = ereg_replace("\(", "", $read[0]); |
| 88 | $tmp = ereg_replace("\)", "", $tmp); |
| 89 | $tmp = str_replace("\\", "", $tmp); |
| 90 | $tmp = substr($tmp, strpos($tmp, "FLAGS")+6, strlen($tmp)); |
| 91 | $tmp = trim($tmp); |
| 92 | $flags = explode(" ", $tmp); |
| 93 | } else { |
| 94 | $flags[0] = "None"; |
| 95 | } |
| 96 | return $flags; |
| 97 | } |
| 98 | |
| 99 | /****************************************************************************** |
| 100 | ** Returns a message array with all the information about a message. See |
| 101 | ** the documentation folder for more information about this array. |
| 102 | ******************************************************************************/ |
| 103 | function sqimap_get_message ($imap_stream, $id, $mailbox) { |
| 104 | $message["INFO"]["ID"] = $id; |
| 105 | $message["INFO"]["MAILBOX"] = $mailbox; |
| 106 | $message["HEADER"] = sqimap_get_message_header($imap_stream, $id); |
| 107 | $message["ENTITIES"] = sqimap_get_message_body($imap_stream, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"], $message["HEADER"]["ENCODING"], $message["HEADER"]["CHARSET"]); |
| 108 | return $message; |
| 109 | } |
| 110 | |
| 111 | /****************************************************************************** |
| 112 | ** Wrapper function that reformats the header information. |
| 113 | ******************************************************************************/ |
| 114 | function sqimap_get_message_header ($imap_stream, $id) { |
| 115 | fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n"); |
| 116 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
| 117 | |
| 118 | return sqimap_get_header($imap_stream, $read); |
| 119 | } |
| 120 | |
| 121 | /****************************************************************************** |
| 122 | ** Wrapper function that returns entity headers for use by decodeMime |
| 123 | ******************************************************************************/ |
| 124 | function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) { |
| 125 | $header = sqimap_get_header($imap_stream, $read); |
| 126 | $type0 = $header["TYPE0"]; |
| 127 | $type1 = $header["TYPE1"]; |
| 128 | $bound = $header["BOUNDARY"]; |
| 129 | $encoding = $header["ENCODING"]; |
| 130 | $charset = $header["CHARSET"]; |
| 131 | $filename = $header["FILENAME"]; |
| 132 | } |
| 133 | |
| 134 | /****************************************************************************** |
| 135 | ** Queries the IMAP server and gets all header information. |
| 136 | ******************************************************************************/ |
| 137 | function sqimap_get_header ($imap_stream, $read) { |
| 138 | $i = 0; |
| 139 | // Set up some defaults |
| 140 | $header["TYPE0"] = "text"; |
| 141 | $header["TYPE1"] = "plain"; |
| 142 | $header["CHARSET"] = "us-ascii"; |
| 143 | |
| 144 | while ($i < count($read)) { |
| 145 | if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") { |
| 146 | $header["MIME"] = true; |
| 147 | $i++; |
| 148 | } |
| 149 | |
| 150 | /** ENCODING TYPE **/ |
| 151 | else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") { |
| 152 | $header["ENCODING"] = strtolower(trim(substr($read[$i], 26))); |
| 153 | $i++; |
| 154 | } |
| 155 | |
| 156 | /** CONTENT-TYPE **/ |
| 157 | else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") { |
| 158 | $cont = strtolower(trim(substr($read[$i], 13))); |
| 159 | if (strpos($cont, ";")) |
| 160 | $cont = substr($cont, 0, strpos($cont, ";")); |
| 161 | |
| 162 | |
| 163 | if (strpos($cont, "/")) { |
| 164 | $header["TYPE0"] = substr($cont, 0, strpos($cont, "/")); |
| 165 | $header["TYPE1"] = substr($cont, strpos($cont, "/")+1); |
| 166 | } else { |
| 167 | $header["TYPE0"] = $cont; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | $line = $read[$i]; |
| 172 | $i++; |
| 173 | while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) { |
| 174 | str_replace("\n", "", $line); |
| 175 | str_replace("\n", "", $read[$i]); |
| 176 | $line = "$line $read[$i]"; |
| 177 | $i++; |
| 178 | } |
| 179 | |
| 180 | /** Detect the boundary of a multipart message **/ |
| 181 | if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) { |
| 182 | $header["BOUNDARY"] = $regs[1]; |
| 183 | } |
| 184 | |
| 185 | /** Detect the charset **/ |
| 186 | if (strpos(strtolower(trim($line)), "charset=")) { |
| 187 | $pos = strpos($line, "charset=") + 8; |
| 188 | $charset = trim($line); |
| 189 | if (strpos($line, " ", $pos) > 0) { |
| 190 | $charset = substr($charset, $pos, strpos($line, " ", $pos)); |
| 191 | } else { |
| 192 | $charset = substr($charset, $pos); |
| 193 | } |
| 194 | $charset = str_replace("\"", "", $charset); |
| 195 | $header["CHARSET"] = $charset; |
| 196 | } else { |
| 197 | $header["CHARSET"] = "us-ascii"; |
| 198 | } |
| 199 | |
| 200 | } |
| 201 | |
| 202 | else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") { |
| 203 | /** Add better dontent-disposition support **/ |
| 204 | |
| 205 | $line = $read[$i]; |
| 206 | $i++; |
| 207 | while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) { |
| 208 | str_replace("\n", "", $line); |
| 209 | str_replace("\n", "", $read[$i]); |
| 210 | $line = "$line $read[$i]"; |
| 211 | $i++; |
| 212 | } |
| 213 | |
| 214 | /** Detects filename if any **/ |
| 215 | if (strpos(strtolower(trim($line)), "filename=")) { |
| 216 | $pos = strpos($line, "filename=") + 9; |
| 217 | $name = trim($line); |
| 218 | if (strpos($line, " ", $pos) > 0) { |
| 219 | $name = substr($name, $pos, strpos($line, " ", $pos)); |
| 220 | } else { |
| 221 | $name = substr($name, $pos); |
| 222 | } |
| 223 | $name = str_replace("\"", "", $name); |
| 224 | $header["FILENAME"] = $name; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** REPLY-TO **/ |
| 229 | else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") { |
| 230 | $header["REPLYTO"] = trim(substr($read[$i], 9, strlen($read[$i]))); |
| 231 | $i++; |
| 232 | } |
| 233 | |
| 234 | /** FROM **/ |
| 235 | else if (strtolower(substr($read[$i], 0, 5)) == "from:") { |
| 236 | $header["FROM"] = trim(substr($read[$i], 5, strlen($read[$i]) - 6)); |
| 237 | if ($header["REPLYTO"] == "") |
| 238 | $header["REPLYTO"] = $header["FROM"]; |
| 239 | $i++; |
| 240 | } |
| 241 | /** DATE **/ |
| 242 | else if (strtolower(substr($read[$i], 0, 5)) == "date:") { |
| 243 | $d = substr($read[$i], 5); |
| 244 | $d = trim($d); |
| 245 | $d = ereg_replace(" ", " ", $d); |
| 246 | $d = explode(" ", $d); |
| 247 | $header["DATE"] = getTimeStamp($d); |
| 248 | $i++; |
| 249 | } |
| 250 | /** SUBJECT **/ |
| 251 | else if (strtolower(substr($read[$i], 0, 8)) == "subject:") { |
| 252 | $header["SUBJECT"] = trim(substr($read[$i], 8, strlen($read[$i]) - 9)); |
| 253 | if (strlen(Chop($header["SUBJECT"])) == 0) |
| 254 | $header["SUBJECT"] = _("(no subject)"); |
| 255 | $i++; |
| 256 | } |
| 257 | /** CC **/ |
| 258 | else if (strtolower(substr($read[$i], 0, 3)) == "cc:") { |
| 259 | $pos = 0; |
| 260 | $header["CC"][$pos] = trim(substr($read[$i], 4)); |
| 261 | $i++; |
| 262 | while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){ |
| 263 | $pos++; |
| 264 | $header["CC"][$pos] = trim($read[$i]); |
| 265 | $i++; |
| 266 | } |
| 267 | } |
| 268 | /** TO **/ |
| 269 | else if (strtolower(substr($read[$i], 0, 3)) == "to:") { |
| 270 | $pos = 0; |
| 271 | $header["TO"][$pos] = trim(substr($read[$i], 4)); |
| 272 | $i++; |
| 273 | while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){ |
| 274 | $pos++; |
| 275 | $header["TO"][$pos] = trim($read[$i]); |
| 276 | $i++; |
| 277 | } |
| 278 | } |
| 279 | /** MESSAGE ID **/ |
| 280 | else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") { |
| 281 | $header["MESSAGE-ID"] = trim(substr($read[$i], 11)); |
| 282 | $i++; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | /** ERROR CORRECTION **/ |
| 287 | else if (substr($read[$i], 0, 1) == ")") { |
| 288 | if (strlen(trim($header["SUBJECT"])) == 0) |
| 289 | $header["SUBJECT"] = _("(no subject)"); |
| 290 | |
| 291 | if (strlen(trim($header["FROM"])) == 0) |
| 292 | $header["FROM"] = _("(unknown sender)"); |
| 293 | |
| 294 | if (strlen(trim($header["DATE"])) == 0) |
| 295 | $header["DATE"] = time(); |
| 296 | $i++; |
| 297 | } |
| 298 | else { |
| 299 | $i++; |
| 300 | } |
| 301 | } |
| 302 | return $header; |
| 303 | } |
| 304 | |
| 305 | |
| 306 | /****************************************************************************** |
| 307 | ** Returns the body of a message. |
| 308 | ******************************************************************************/ |
| 309 | function sqimap_get_message_body ($imap_stream, $bound, $id, $type0, $type1, $encoding, $charset) { |
| 310 | fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\r\n"); |
| 311 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
| 312 | |
| 313 | $i = 0; |
| 314 | $j = 0; |
| 315 | while ($i < count($read)-1) { |
| 316 | if ( ($i != 0) ) { |
| 317 | $bodytmp[$j] = $read[$i]; |
| 318 | $j++; |
| 319 | } |
| 320 | $i++; |
| 321 | } |
| 322 | $body = $bodytmp; |
| 323 | |
| 324 | return decodeMime($body, $bound, $type0, $type1, $encoding, $charset); |
| 325 | } |
| 326 | ?> |