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