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