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