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