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