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