fixed filename problem with attachments
[squirrelmail.git] / functions / imap_messages.php
1 <?
2 /**
3 ** imap_messages.php
4 **
5 ** This implements functions that manipulate messages
6 **/
7
8 /******************************************************************************
9 ** Copies specified messages to specified folder
10 ******************************************************************************/
11 function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
12 fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n");
13 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
14 }
15
16 /******************************************************************************
17 ** Deletes specified messages and moves them to trash if possible
18 ******************************************************************************/
19 function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
20 global $move_to_trash, $trash_folder, $auto_expunge;
21
22 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder))) {
23 sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
24 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
25 } else {
26 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
27 }
28 }
29
30 /******************************************************************************
31 ** Sets the specified messages with specified flag
32 ******************************************************************************/
33 function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
34 fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\r\n");
35 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
36 }
37
38 /******************************************************************************
39 ** Returns some general header information -- FROM, DATE, and SUBJECT
40 ******************************************************************************/
41 function sqimap_get_small_header ($imap_stream, $id, &$from, &$subject, &$date) {
42 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER.FIELDS (From Subject Date)]\r\n");
43 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
44
45 for ($i = 0; $i < count($read); $i++) {
46 if (strtolower(substr($read[$i], 0, 5)) == "from:") {
47 $from = sqimap_find_displayable_name(substr($read[$i], 5));
48 } else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
49 $date = substr($read[$i], 5);
50 } else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
51 $subject = htmlspecialchars(substr($read[$i], 8));
52 if (strlen(trim($subject)) == 0)
53 $subject = _("(no subject)");
54 }
55 }
56 }
57
58 /******************************************************************************
59 ** Returns the flags for the specified messages
60 ******************************************************************************/
61 function sqimap_get_flags ($imap_stream, $start, $end) {
62 fputs ($imap_stream, "a001 FETCH $start:$end FLAGS\r\n");
63 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
64 $i = 0;
65 while ($i < count($read)) {
66 if (strpos($read[$i], "FLAGS")) {
67 $tmp = ereg_replace("\(", "", $read[$i]);
68 $tmp = ereg_replace("\)", "", $tmp);
69 $tmp = str_replace("\\", "", $tmp);
70 $tmp = substr($tmp, strpos($tmp, "FLAGS")+6, strlen($tmp));
71 $tmp = trim($tmp);
72 $flags[$i] = explode(" ", $tmp);
73 } else {
74 $flags[$i][0] = "None";
75 }
76 $i++;
77 }
78 return $flags;
79 }
80
81 /******************************************************************************
82 ** Returns a message array with all the information about a message. See
83 ** the documentation folder for more information about this array.
84 ******************************************************************************/
85 function sqimap_get_message ($imap_stream, $id, $mailbox) {
86 $message["INFO"]["ID"] = $id;
87 $message["INFO"]["MAILBOX"] = $mailbox;
88 $message["HEADER"] = sqimap_get_message_header($imap_stream, $id);
89 $message["ENTITIES"] = sqimap_get_message_body($imap_stream, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"], $message["HEADER"]["ENCODING"], $message["HEADER"]["CHARSET"]);
90 return $message;
91 }
92
93 /******************************************************************************
94 ** Wrapper function that reformats the header information.
95 ******************************************************************************/
96 function sqimap_get_message_header ($imap_stream, $id) {
97 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
98 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
99
100 return sqimap_get_header($imap_stream, $read);
101 }
102
103 /******************************************************************************
104 ** Wrapper function that returns entity headers for use by decodeMime
105 ******************************************************************************/
106 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
107 $header = sqimap_get_header($imap_stream, $read);
108 $type0 = $header["TYPE0"];
109 $type1 = $header["TYPE1"];
110 $bound = $header["BOUNDARY"];
111 $encoding = $header["ENCODING"];
112 $charset = $header["CHARSET"];
113 $filename = $header["FILENAME"];
114 }
115
116 /******************************************************************************
117 ** Queries the IMAP server and gets all header information.
118 ******************************************************************************/
119 function sqimap_get_header ($imap_stream, $read) {
120 $i = 0;
121 // Set up some defaults
122 $header["TYPE0"] = "text";
123 $header["TYPE1"] = "plain";
124 $header["CHARSET"] = "us-ascii";
125
126 while ($i < count($read)) {
127 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
128 $header["MIME"] = true;
129 $i++;
130 }
131
132 /** ENCODING TYPE **/
133 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
134 $header["ENCODING"] = strtolower(trim(substr($read[$i], 26)));
135 $i++;
136 }
137
138 /** CONTENT-TYPE **/
139 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
140 $cont = strtolower(trim(substr($read[$i], 13)));
141 if (strpos($cont, ";"))
142 $cont = substr($cont, 0, strpos($cont, ";"));
143
144
145 if (strpos($cont, "/")) {
146 $header["TYPE0"] = substr($cont, 0, strpos($cont, "/"));
147 $header["TYPE1"] = substr($cont, strpos($cont, "/")+1);
148 } else {
149 $header["TYPE0"] = $cont;
150 }
151
152
153 $line = $read[$i];
154 $i++;
155 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
156 str_replace("\n", "", $line);
157 str_replace("\n", "", $read[$i]);
158 $line = "$line $read[$i]";
159 $i++;
160 }
161
162 /** Detect the boundary of a multipart message **/
163 if (strpos(strtolower(trim($line)), "boundary=")) {
164 $pos = strpos(strtolower($line), "boundary=") + 9;
165 $bound = trim($line);
166 if (strpos($line, " ", $pos) > 0) {
167 $bound = substr($bound, $pos, strpos($line, " ", $pos));
168 } else {
169 $bound = substr($bound, $pos);
170 }
171 $bound = str_replace("\"", "", $bound);
172 $header["BOUNDARY"] = $bound;
173 }
174
175 /** Detect the charset **/
176 if (strpos(strtolower(trim($line)), "charset=")) {
177 $pos = strpos($line, "charset=") + 8;
178 $charset = trim($line);
179 if (strpos($line, " ", $pos) > 0) {
180 $charset = substr($charset, $pos, strpos($line, " ", $pos));
181 } else {
182 $charset = substr($charset, $pos);
183 }
184 $charset = str_replace("\"", "", $charset);
185 $header["CHARSET"] = $charset;
186 } else {
187 $header["CHARSET"] = "us-ascii";
188 }
189
190 }
191
192 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
193 /** Add better dontent-disposition support **/
194
195 $line = $read[$i];
196 $i++;
197 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
198 str_replace("\n", "", $line);
199 str_replace("\n", "", $read[$i]);
200 $line = "$line $read[$i]";
201 $i++;
202 }
203
204 /** Detects filename if any **/
205 if (strpos(strtolower(trim($line)), "filename=")) {
206 $pos = strpos($line, "filename=") + 9;
207 $name = trim($line);
208 if (strpos($line, " ", $pos) > 0) {
209 $name = substr($name, $pos, strpos($line, " ", $pos));
210 } else {
211 $name = substr($name, $pos);
212 }
213 $name = str_replace("\"", "", $name);
214 $header["FILENAME"] = $name;
215 }
216 }
217
218 /** REPLY-TO **/
219 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
220 $header["REPLYTO"] = trim(substr($read[$i], 9, strlen($read[$i])));
221 $i++;
222 }
223
224 /** FROM **/
225 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
226 $header["FROM"] = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
227 if ($header["REPLYTO"] == "")
228 $header["REPLYTO"] = $header["FROM"];
229 $i++;
230 }
231 /** DATE **/
232 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
233 $d = substr($read[$i], 5);
234 $d = trim($d);
235 $d = ereg_replace(" ", " ", $d);
236 $d = explode(" ", $d);
237 $header["DATE"] = getTimeStamp($d);
238 $i++;
239 }
240 /** SUBJECT **/
241 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
242 $header["SUBJECT"] = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
243 if (strlen(Chop($header["SUBJECT"])) == 0)
244 $header["SUBJECT"] = _("(no subject)");
245 $i++;
246 }
247 /** CC **/
248 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
249 $pos = 0;
250 $header["CC"][$pos] = trim(substr($read[$i], 4));
251 $i++;
252 while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")) {
253 $pos++;
254 $header["CC"][$pos] = trim($read[$i]);
255 $i++;
256 }
257 }
258 /** TO **/
259 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
260 $pos = 0;
261 $header["TO"][$pos] = trim(substr($read[$i], 4));
262 $i++;
263 while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")){
264 $pos++;
265 $header["TO"][$pos] = trim($read[$i]);
266 $i++;
267 }
268 }
269 /** MESSAGE ID **/
270 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
271 $header["MESSAGE-ID"] = trim(substr($read[$i], 11));
272 $i++;
273 }
274
275
276 /** ERROR CORRECTION **/
277 else if (substr($read[$i], 0, 1) == ")") {
278 if ($header["SUBJECT"] == "")
279 $header["SUBJECT"] = _("(no subject)");
280
281 if ($header["FROM"] == "")
282 $header["FROM"] = _("(unknown sender)");
283
284 if ($header["DATE"] == "")
285 $header["DATE"] = time();
286 $i++;
287 }
288 else {
289 $i++;
290 }
291 }
292 return $header;
293 }
294
295
296 /******************************************************************************
297 ** Returns the body of a message.
298 ******************************************************************************/
299 function sqimap_get_message_body ($imap_stream, $bound, $id, $type0, $type1, $encoding, $charset) {
300 fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\r\n");
301 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
302
303 $i = 0;
304 $j = 0;
305 while ($i < count($read)-1) {
306 if ( ($i != 0) ) {
307 $bodytmp[$j] = $read[$i];
308 $j++;
309 }
310 $i++;
311 }
312 $body = $bodytmp;
313
314 return decodeMime($body, $bound, $type0, $type1, $encoding, $charset);
315 }
316 ?>