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