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