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