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