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