59177427 |
1 | <?php |
052e0c26 |
2 | /** |
3 | ** imap_messages.php |
4 | ** |
245a6892 |
5 | ** This implements functions that manipulate messages |
6 | ** |
7 | ** $Id$ |
052e0c26 |
8 | **/ |
9 | |
7aaa81fc |
10 | if (!isset($mime_php)) include "../functions/mime.php"; |
8beafbbc |
11 | |
052e0c26 |
12 | /****************************************************************************** |
13 | ** Copies specified messages to specified folder |
14 | ******************************************************************************/ |
15 | function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) { |
16 | fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n"); |
17 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
18 | } |
19 | |
20 | /****************************************************************************** |
21 | ** Deletes specified messages and moves them to trash if possible |
22 | ******************************************************************************/ |
23 | function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) { |
24 | global $move_to_trash, $trash_folder, $auto_expunge; |
25 | |
6eac5393 |
26 | if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) { |
052e0c26 |
27 | sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder); |
28 | sqimap_messages_flag ($imap_stream, $start, $end, "Deleted"); |
29 | } else { |
30 | sqimap_messages_flag ($imap_stream, $start, $end, "Deleted"); |
31 | } |
32 | } |
33 | |
34 | /****************************************************************************** |
35 | ** Sets the specified messages with specified flag |
36 | ******************************************************************************/ |
37 | function sqimap_messages_flag ($imap_stream, $start, $end, $flag) { |
38 | fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\r\n"); |
39 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
052e0c26 |
40 | } |
41 | |
f9b3e5d9 |
42 | |
43 | /****************************************************************************** |
44 | ** Remove specified flag from specified messages |
45 | ******************************************************************************/ |
46 | function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag) { |
47 | fputs ($imap_stream, "a001 STORE $start:$end -FLAGS (\\$flag)\r\n"); |
48 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
49 | } |
50 | |
51 | |
052e0c26 |
52 | /****************************************************************************** |
53 | ** Returns some general header information -- FROM, DATE, and SUBJECT |
54 | ******************************************************************************/ |
60994e13 |
55 | class small_header { |
245a6892 |
56 | var $from = '', $subject = '', $date = '', $to = '', |
57 | $priority = 0, $message_id = 0, $cc = ''; |
60994e13 |
58 | } |
15a9cd31 |
59 | |
4bbba0d8 |
60 | function sqimap_get_small_header ($imap_stream, $id, $sent) { |
508c06bf |
61 | |
9db5f5fc |
62 | fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type)]\r\n"); |
052e0c26 |
63 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
64 | |
65 | $subject = _("(no subject)"); |
66 | $from = _("Unknown Sender"); |
266fe275 |
67 | $priority = "0"; |
60994e13 |
68 | $messageid = "<>"; |
245a6892 |
69 | $cc = ""; |
70 | $to = ""; |
71 | $date = ""; |
72 | $type[0] = ""; |
73 | $type[1] = ""; |
60994e13 |
74 | |
75 | $g = 0; |
052e0c26 |
76 | for ($i = 0; $i < count($read); $i++) { |
441f2d33 |
77 | if (eregi ("^to:(.*)$", $read[$i], $regs)) { |
c36ed9cf |
78 | //$to = sqimap_find_displayable_name(substr($read[$i], 3)); |
441f2d33 |
79 | $to = $regs[1]; |
80 | } else if (eregi ("^from:(.*)$", $read[$i], $regs)) { |
c36ed9cf |
81 | //$from = sqimap_find_displayable_name(substr($read[$i], 5)); |
441f2d33 |
82 | $from = $regs[1]; |
83 | } else if (eregi ("^x-priority:(.*)$", $read[$i], $regs)) { |
84 | $priority = trim($regs[1]); |
85 | } else if (eregi ("^message-id:(.*)$", $read[$i], $regs)) { |
86 | $messageid = trim($regs[1]); |
87 | } else if (eregi ("^cc:(.*)$", $read[$i], $regs)) { |
88 | $cc = $regs[1]; |
89 | } else if (eregi ("^date:(.*)$", $read[$i], $regs)) { |
90 | $date = $regs[1]; |
91 | } else if (eregi ("^subject:(.*)$", $read[$i], $regs)) { |
92 | $subject = htmlspecialchars(trim($regs[1])); |
93 | if ($subject == "") |
4bbba0d8 |
94 | $subject = _("(no subject)"); |
441f2d33 |
95 | } else if (eregi ("^content-type:(.*)$", $read[$i], $regs)) { |
96 | $type = strtolower(trim($regs[1])); |
9db5f5fc |
97 | if ($pos = strpos($type, ";")) |
98 | $type = substr($type, 0, $pos); |
99 | $type = explode("/", $type); |
4bbba0d8 |
100 | } |
9db5f5fc |
101 | |
60994e13 |
102 | } |
4bbba0d8 |
103 | |
8fca0a65 |
104 | // If there isn't a date, it takes the internal date and uses |
105 | // that as the normal date. |
106 | if (trim($date) == "") { |
107 | fputs ($imap_stream, "a002 FETCH $id INTERNALDATE\r\n"); |
108 | $internal_read = sqimap_read_data ($imap_stream, "a002", true, $r, $m); |
109 | |
110 | // * 22 FETCH (INTERNALDATE " 8-Sep-2000 13:17:07 -0500") |
111 | $date = $internal_read[0]; |
112 | $date = eregi_replace(".*internaldate \"", "", $date); |
113 | $date = eregi_replace("\".*", "", $date); |
114 | $date_ary = explode(" ", trim($date)); |
115 | $date_ary[0] = str_replace("-", " ", $date_ary[0]); |
116 | $date = implode (" ", $date_ary); |
117 | } |
118 | |
fde32e3f |
119 | fputs ($imap_stream, "a003 FETCH $id RFC822.SIZE\r\n"); |
120 | $read = sqimap_read_data($imap_stream, "a003", true, $r, $m); |
121 | preg_match("/([0-9]+)\)\s*$/i", $read[0], $regs); |
6e7468f6 |
122 | $size = $regs[1]; |
fde32e3f |
123 | |
60994e13 |
124 | $header = new small_header; |
3e69e88b |
125 | if ($sent == true) |
441f2d33 |
126 | $header->from = (trim($to) != '')? $to : _('(only Cc/Bcc)'); |
3e69e88b |
127 | else |
1195c340 |
128 | $header->from = $from; |
60994e13 |
129 | |
130 | $header->date = $date; |
131 | $header->subject = $subject; |
3e69e88b |
132 | $header->to = $to; |
266fe275 |
133 | $header->priority = $priority; |
60994e13 |
134 | $header->message_id = $messageid; |
1195c340 |
135 | $header->cc = $cc; |
fde32e3f |
136 | $header->size = $size; |
9db5f5fc |
137 | $header->type0 = $type[0]; |
138 | $header->type1 = $type[1]; |
245a6892 |
139 | |
60994e13 |
140 | return $header; |
052e0c26 |
141 | } |
142 | |
143 | /****************************************************************************** |
144 | ** Returns the flags for the specified messages |
145 | ******************************************************************************/ |
146 | function sqimap_get_flags ($imap_stream, $i) { |
147 | fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n"); |
148 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
441f2d33 |
149 | if (ereg("FLAGS(.*)", $read[0], $regs)) |
150 | return explode(" ", trim(ereg_replace('[\(\)\\]', '', $regs[1]))); |
151 | return Array('None'); |
052e0c26 |
152 | } |
153 | |
154 | /****************************************************************************** |
155 | ** Returns a message array with all the information about a message. See |
156 | ** the documentation folder for more information about this array. |
157 | ******************************************************************************/ |
158 | function sqimap_get_message ($imap_stream, $id, $mailbox) { |
8beafbbc |
159 | $header = sqimap_get_message_header($imap_stream, $id, $mailbox); |
245a6892 |
160 | return sqimap_get_message_body($imap_stream, &$header); |
052e0c26 |
161 | } |
162 | |
163 | /****************************************************************************** |
164 | ** Wrapper function that reformats the header information. |
165 | ******************************************************************************/ |
8beafbbc |
166 | function sqimap_get_message_header ($imap_stream, $id, $mailbox) { |
052e0c26 |
167 | fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n"); |
168 | $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message); |
169 | |
8beafbbc |
170 | $header = sqimap_get_header($imap_stream, $read); |
171 | $header->id = $id; |
172 | $header->mailbox = $mailbox; |
173 | |
174 | return $header; |
052e0c26 |
175 | } |
176 | |
177 | /****************************************************************************** |
178 | ** Wrapper function that returns entity headers for use by decodeMime |
179 | ******************************************************************************/ |
8beafbbc |
180 | /* |
052e0c26 |
181 | function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) { |
182 | $header = sqimap_get_header($imap_stream, $read); |
183 | $type0 = $header["TYPE0"]; |
184 | $type1 = $header["TYPE1"]; |
185 | $bound = $header["BOUNDARY"]; |
186 | $encoding = $header["ENCODING"]; |
187 | $charset = $header["CHARSET"]; |
188 | $filename = $header["FILENAME"]; |
189 | } |
8beafbbc |
190 | */ |
052e0c26 |
191 | |
192 | /****************************************************************************** |
193 | ** Queries the IMAP server and gets all header information. |
194 | ******************************************************************************/ |
195 | function sqimap_get_header ($imap_stream, $read) { |
508c06bf |
196 | global $where, $what; |
197 | |
8beafbbc |
198 | $hdr = new msg_header(); |
052e0c26 |
199 | $i = 0; |
200 | // Set up some defaults |
8beafbbc |
201 | $hdr->type0 = "text"; |
202 | $hdr->type1 = "plain"; |
203 | $hdr->charset = "us-ascii"; |
052e0c26 |
204 | |
fde32e3f |
205 | preg_match("/\{([0-9]+)\}/", $read[$i], $regs); |
206 | preg_match("/[0-9]+/", $regs[0], $regs); |
207 | |
052e0c26 |
208 | while ($i < count($read)) { |
209 | if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") { |
8beafbbc |
210 | $hdr->mime = true; |
052e0c26 |
211 | $i++; |
212 | } |
213 | |
214 | /** ENCODING TYPE **/ |
215 | else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") { |
8beafbbc |
216 | $hdr->encoding = strtolower(trim(substr($read[$i], 26))); |
052e0c26 |
217 | $i++; |
218 | } |
219 | |
220 | /** CONTENT-TYPE **/ |
221 | else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") { |
222 | $cont = strtolower(trim(substr($read[$i], 13))); |
223 | if (strpos($cont, ";")) |
224 | $cont = substr($cont, 0, strpos($cont, ";")); |
225 | |
226 | |
227 | if (strpos($cont, "/")) { |
8beafbbc |
228 | $hdr->type0 = substr($cont, 0, strpos($cont, "/")); |
229 | $hdr->type1 = substr($cont, strpos($cont, "/")+1); |
052e0c26 |
230 | } else { |
8beafbbc |
231 | $hdr->type0 = $cont; |
052e0c26 |
232 | } |
233 | |
234 | |
235 | $line = $read[$i]; |
236 | $i++; |
237 | while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) { |
238 | str_replace("\n", "", $line); |
239 | str_replace("\n", "", $read[$i]); |
240 | $line = "$line $read[$i]"; |
241 | $i++; |
242 | } |
243 | |
244 | /** Detect the boundary of a multipart message **/ |
245 | if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) { |
8beafbbc |
246 | $hdr->boundary = $regs[1]; |
052e0c26 |
247 | } |
248 | |
249 | /** Detect the charset **/ |
250 | if (strpos(strtolower(trim($line)), "charset=")) { |
251 | $pos = strpos($line, "charset=") + 8; |
252 | $charset = trim($line); |
3309f5b9 |
253 | if (strpos($line, ";", $pos) > 0) { |
254 | $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos); |
052e0c26 |
255 | } else { |
256 | $charset = substr($charset, $pos); |
257 | } |
258 | $charset = str_replace("\"", "", $charset); |
8beafbbc |
259 | $hdr->charset = $charset; |
052e0c26 |
260 | } else { |
8beafbbc |
261 | $hdr->charset = "us-ascii"; |
052e0c26 |
262 | } |
263 | |
264 | } |
265 | |
266 | else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") { |
267 | /** Add better dontent-disposition support **/ |
268 | |
269 | $line = $read[$i]; |
270 | $i++; |
271 | while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) { |
272 | str_replace("\n", "", $line); |
273 | str_replace("\n", "", $read[$i]); |
274 | $line = "$line $read[$i]"; |
275 | $i++; |
276 | } |
277 | |
278 | /** Detects filename if any **/ |
279 | if (strpos(strtolower(trim($line)), "filename=")) { |
280 | $pos = strpos($line, "filename=") + 9; |
281 | $name = trim($line); |
282 | if (strpos($line, " ", $pos) > 0) { |
283 | $name = substr($name, $pos, strpos($line, " ", $pos)); |
284 | } else { |
285 | $name = substr($name, $pos); |
286 | } |
287 | $name = str_replace("\"", "", $name); |
8beafbbc |
288 | $hdr->filename = $name; |
052e0c26 |
289 | } |
290 | } |
291 | |
292 | /** REPLY-TO **/ |
293 | else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") { |
8beafbbc |
294 | $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i]))); |
052e0c26 |
295 | $i++; |
296 | } |
297 | |
298 | /** FROM **/ |
299 | else if (strtolower(substr($read[$i], 0, 5)) == "from:") { |
8beafbbc |
300 | $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6)); |
245a6892 |
301 | if (! isset($hdr->replyto) || $hdr->replyto == "") |
8beafbbc |
302 | $hdr->replyto = $hdr->from; |
052e0c26 |
303 | $i++; |
304 | } |
305 | /** DATE **/ |
306 | else if (strtolower(substr($read[$i], 0, 5)) == "date:") { |
307 | $d = substr($read[$i], 5); |
308 | $d = trim($d); |
309 | $d = ereg_replace(" ", " ", $d); |
310 | $d = explode(" ", $d); |
8beafbbc |
311 | $hdr->date = getTimeStamp($d); |
052e0c26 |
312 | $i++; |
313 | } |
314 | /** SUBJECT **/ |
315 | else if (strtolower(substr($read[$i], 0, 8)) == "subject:") { |
8beafbbc |
316 | $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9)); |
317 | if (strlen(Chop($hdr->subject)) == 0) |
318 | $hdr->subject = _("(no subject)"); |
508c06bf |
319 | |
320 | if ($where == "SUBJECT") { |
321 | $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject); |
322 | } |
052e0c26 |
323 | $i++; |
324 | } |
325 | /** CC **/ |
326 | else if (strtolower(substr($read[$i], 0, 3)) == "cc:") { |
327 | $pos = 0; |
8beafbbc |
328 | $hdr->cc[$pos] = trim(substr($read[$i], 4)); |
052e0c26 |
329 | $i++; |
11728a4d |
330 | while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){ |
052e0c26 |
331 | $pos++; |
8beafbbc |
332 | $hdr->cc[$pos] = trim($read[$i]); |
052e0c26 |
333 | $i++; |
334 | } |
335 | } |
336 | /** TO **/ |
337 | else if (strtolower(substr($read[$i], 0, 3)) == "to:") { |
338 | $pos = 0; |
8beafbbc |
339 | $hdr->to[$pos] = trim(substr($read[$i], 4)); |
052e0c26 |
340 | $i++; |
11728a4d |
341 | while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){ |
052e0c26 |
342 | $pos++; |
8beafbbc |
343 | $hdr->to[$pos] = trim($read[$i]); |
052e0c26 |
344 | $i++; |
345 | } |
346 | } |
347 | /** MESSAGE ID **/ |
348 | else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") { |
8beafbbc |
349 | $hdr->message_id = trim(substr($read[$i], 11)); |
052e0c26 |
350 | $i++; |
351 | } |
352 | |
353 | |
354 | /** ERROR CORRECTION **/ |
355 | else if (substr($read[$i], 0, 1) == ")") { |
8beafbbc |
356 | if (strlen(trim($hdr->subject)) == 0) |
357 | $hdr->subject = _("(no subject)"); |
052e0c26 |
358 | |
8beafbbc |
359 | if (strlen(trim($hdr->from)) == 0) |
360 | $hdr->from = _("(unknown sender)"); |
052e0c26 |
361 | |
8beafbbc |
362 | if (strlen(trim($hdr->date)) == 0) |
363 | $hdr->date = time(); |
052e0c26 |
364 | $i++; |
365 | } |
366 | else { |
367 | $i++; |
368 | } |
369 | } |
8beafbbc |
370 | return $hdr; |
052e0c26 |
371 | } |
372 | |
373 | |
374 | /****************************************************************************** |
375 | ** Returns the body of a message. |
376 | ******************************************************************************/ |
8d8ab69a |
377 | function sqimap_get_message_body ($imap_stream, &$header) { |
8beafbbc |
378 | $id = $header->id; |
245a6892 |
379 | return decodeMime($imap_stream, &$header); |
052e0c26 |
380 | } |
8beafbbc |
381 | |
382 | |
383 | /****************************************************************************** |
384 | ** Returns an array with the body structure |
385 | ******************************************************************************/ |
052e0c26 |
386 | ?> |