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