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