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