replaced "foo" with 'foo' while doing other
[squirrelmail.git] / functions / imap_messages.php
1 <?php
2 /**
3 ** imap_messages.php
4 **
5 ** This implements functions that manipulate messages
6 **
7 ** $Id$
8 **/
9
10 if (!isset($mime_php)) include "../functions/mime.php";
11
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
26 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
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);
40 }
41
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
52 /******************************************************************************
53 ** Returns some general header information -- FROM, DATE, and SUBJECT
54 ******************************************************************************/
55 class small_header {
56 var $from = '', $subject = '', $date = '', $to = '',
57 $priority = 0, $message_id = 0, $cc = '';
58 }
59
60 function sqimap_get_small_header ($imap_stream, $id, $sent) {
61
62 fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type)]\r\n");
63 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
64
65 $subject = _("(no subject)");
66 $from = _("Unknown Sender");
67 $priority = "0";
68 $messageid = "<>";
69 $cc = "";
70 $to = "";
71 $date = "";
72 $type[0] = "";
73 $type[1] = "";
74
75 $g = 0;
76 for ($i = 0; $i < count($read); $i++) {
77 if (eregi ("^to:(.*)$", $read[$i], $regs)) {
78 //$to = sqimap_find_displayable_name(substr($read[$i], 3));
79 $to = $regs[1];
80 } else if (eregi ("^from:(.*)$", $read[$i], $regs)) {
81 //$from = sqimap_find_displayable_name(substr($read[$i], 5));
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 == "")
94 $subject = _("(no subject)");
95 } else if (eregi ("^content-type:(.*)$", $read[$i], $regs)) {
96 $type = strtolower(trim($regs[1]));
97 if ($pos = strpos($type, ";"))
98 $type = substr($type, 0, $pos);
99 $type = explode("/", $type);
100 }
101
102 }
103
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
119 fputs ($imap_stream, "a003 FETCH $id RFC822.SIZE\r\n");
120 $read = sqimap_read_data($imap_stream, "a003", true, $r, $m);
121 eregi("([0-9]+)[^0-9]*$", $read[0], $regs);
122 $size = $regs[1];
123
124 $header = new small_header;
125 if ($sent == true)
126 $header->from = (trim($to) != '')? $to : _("(only Cc/Bcc)");
127 else
128 $header->from = $from;
129
130 $header->date = $date;
131 $header->subject = $subject;
132 $header->to = $to;
133 $header->priority = $priority;
134 $header->message_id = $messageid;
135 $header->cc = $cc;
136 $header->size = $size;
137 $header->type0 = $type[0];
138 $header->type1 = $type[1];
139
140 return $header;
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);
149 if (ereg("FLAGS(.*)", $read[0], $regs))
150 return explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
151 return Array('None');
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) {
159 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
160 return sqimap_get_message_body($imap_stream, $header);
161 }
162
163 /******************************************************************************
164 ** Wrapper function that reformats the header information.
165 ******************************************************************************/
166 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
167 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
168 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
169
170 $header = sqimap_get_header($imap_stream, $read);
171 $header->id = $id;
172 $header->mailbox = $mailbox;
173
174 return $header;
175 }
176
177 /******************************************************************************
178 ** Wrapper function that returns entity headers for use by decodeMime
179 ******************************************************************************/
180 /*
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 }
190 */
191
192 /******************************************************************************
193 ** Queries the IMAP server and gets all header information.
194 ******************************************************************************/
195 function sqimap_get_header ($imap_stream, $read) {
196 global $where, $what;
197
198 $hdr = new msg_header();
199 $i = 0;
200 // Set up some defaults
201 $hdr->type0 = "text";
202 $hdr->type1 = "plain";
203 $hdr->charset = "us-ascii";
204
205 while ($i < count($read)) {
206 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
207 $hdr->mime = true;
208 $i++;
209 }
210
211 /** ENCODING TYPE **/
212 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
213 $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
214 $i++;
215 }
216
217 /** CONTENT-TYPE **/
218 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
219 $cont = strtolower(trim(substr($read[$i], 13)));
220 if (strpos($cont, ";"))
221 $cont = substr($cont, 0, strpos($cont, ";"));
222
223
224 if (strpos($cont, "/")) {
225 $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
226 $hdr->type1 = substr($cont, strpos($cont, "/")+1);
227 } else {
228 $hdr->type0 = $cont;
229 }
230
231
232 $line = $read[$i];
233 $i++;
234 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
235 str_replace("\n", "", $line);
236 str_replace("\n", "", $read[$i]);
237 $line = "$line $read[$i]";
238 $i++;
239 }
240
241 /** Detect the boundary of a multipart message **/
242 if (eregi('boundary="([^"]+)"', $line, $regs)) {
243 $hdr->boundary = $regs[1];
244 }
245
246 /** Detect the charset **/
247 if (strpos(strtolower(trim($line)), "charset=")) {
248 $pos = strpos($line, "charset=") + 8;
249 $charset = trim($line);
250 if (strpos($line, ";", $pos) > 0) {
251 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
252 } else {
253 $charset = substr($charset, $pos);
254 }
255 $charset = str_replace("\"", "", $charset);
256 $hdr->charset = $charset;
257 } else {
258 $hdr->charset = "us-ascii";
259 }
260
261 }
262
263 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
264 /** Add better dontent-disposition support **/
265
266 $line = $read[$i];
267 $i++;
268 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
269 str_replace("\n", "", $line);
270 str_replace("\n", "", $read[$i]);
271 $line = "$line $read[$i]";
272 $i++;
273 }
274
275 /** Detects filename if any **/
276 if (strpos(strtolower(trim($line)), "filename=")) {
277 $pos = strpos($line, "filename=") + 9;
278 $name = trim($line);
279 if (strpos($line, " ", $pos) > 0) {
280 $name = substr($name, $pos, strpos($line, " ", $pos));
281 } else {
282 $name = substr($name, $pos);
283 }
284 $name = str_replace("\"", "", $name);
285 $hdr->filename = $name;
286 }
287 }
288
289 /** REPLY-TO **/
290 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
291 $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
292 $i++;
293 }
294
295 /** FROM **/
296 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
297 $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
298 if (! isset($hdr->replyto) || $hdr->replyto == "")
299 $hdr->replyto = $hdr->from;
300 $i++;
301 }
302 /** DATE **/
303 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
304 $d = substr($read[$i], 5);
305 $d = trim($d);
306 $d = strtr($d, array(' ' => ' '));
307 $d = explode(' ', $d);
308 $hdr->date = getTimeStamp($d);
309 $i++;
310 }
311 /** SUBJECT **/
312 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
313 $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
314 if (strlen(Chop($hdr->subject)) == 0)
315 $hdr->subject = _("(no subject)");
316
317 if ($where == "SUBJECT") {
318 $hdr->subject = eregi_replace($what, "<b>\\0</b>", $hdr->subject);
319 }
320 $i++;
321 }
322 /** CC **/
323 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
324 $pos = 0;
325 $hdr->cc[$pos] = trim(substr($read[$i], 4));
326 $i++;
327 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
328 $pos++;
329 $hdr->cc[$pos] = trim($read[$i]);
330 $i++;
331 }
332 }
333 /** TO **/
334 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
335 $pos = 0;
336 $hdr->to[$pos] = trim(substr($read[$i], 4));
337 $i++;
338 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
339 $pos++;
340 $hdr->to[$pos] = trim($read[$i]);
341 $i++;
342 }
343 }
344 /** MESSAGE ID **/
345 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
346 $hdr->message_id = trim(substr($read[$i], 11));
347 $i++;
348 }
349
350
351 /** ERROR CORRECTION **/
352 else if (substr($read[$i], 0, 1) == ")") {
353 if (strlen(trim($hdr->subject)) == 0)
354 $hdr->subject = _("(no subject)");
355
356 if (strlen(trim($hdr->from)) == 0)
357 $hdr->from = _("(unknown sender)");
358
359 if (strlen(trim($hdr->date)) == 0)
360 $hdr->date = time();
361 $i++;
362 }
363 else {
364 $i++;
365 }
366 }
367 return $hdr;
368 }
369
370
371 /******************************************************************************
372 ** Returns the body of a message.
373 ******************************************************************************/
374 function sqimap_get_message_body ($imap_stream, &$header) {
375 $id = $header->id;
376 return decodeMime($imap_stream, $header);
377 }
378
379
380 /******************************************************************************
381 ** Returns an array with the body structure
382 ******************************************************************************/
383 ?>