changed <? to <?php in everything
[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 /******************************************************************************
40 ** Returns some general header information -- FROM, DATE, and SUBJECT
41 ******************************************************************************/
42 function sqimap_get_small_header ($imap_stream, $id, &$from, &$subject, &$date) {
43 fputs ($imap_stream, "a001 FETCH $id:$id RFC822.HEADER.LINES (From Subject Date)\r\n");
44 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
45
46 $subject = _("(no subject)");
47 $from = _("Unknown Sender");
48 for ($i = 0; $i < count($read); $i++) {
49 if (strtolower(substr($read[$i], 0, 5)) == "from:") {
50 $from = sqimap_find_displayable_name(substr($read[$i], 5));
51 } else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
52 $date = substr($read[$i], 5);
53 } else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
54 $subject = htmlspecialchars(substr($read[$i], 8));
55 if (strlen(trim($subject)) == 0)
56 $subject = _("(no subject)");
57 }
58 }
59 }
60
61 /******************************************************************************
62 ** Returns the flags for the specified messages
63 ******************************************************************************/
64 function sqimap_get_flags ($imap_stream, $i) {
65 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
66 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
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";
76 }
77 return $flags;
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);
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"]);
89 return $message;
90 }
91
92 /******************************************************************************
93 ** Wrapper function that reformats the header information.
94 ******************************************************************************/
95 function sqimap_get_message_header ($imap_stream, $id) {
96 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
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;
120 // Set up some defaults
121 $header["TYPE0"] = "text";
122 $header["TYPE1"] = "plain";
123 $header["CHARSET"] = "us-ascii";
124
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 **/
138 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
139 $cont = strtolower(trim(substr($read[$i], 13)));
140 if (strpos($cont, ";"))
141 $cont = substr($cont, 0, strpos($cont, ";"));
142
143
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
151
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 (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
163 $header["BOUNDARY"] = $regs[1];
164 }
165
166 /** Detect the charset **/
167 if (strpos(strtolower(trim($line)), "charset=")) {
168 $pos = strpos($line, "charset=") + 8;
169 $charset = trim($line);
170 if (strpos($line, " ", $pos) > 0) {
171 $charset = substr($charset, $pos, strpos($line, " ", $pos));
172 } else {
173 $charset = substr($charset, $pos);
174 }
175 $charset = str_replace("\"", "", $charset);
176 $header["CHARSET"] = $charset;
177 } else {
178 $header["CHARSET"] = "us-ascii";
179 }
180
181 }
182
183 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
184 /** Add better dontent-disposition support **/
185
186 $line = $read[$i];
187 $i++;
188 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
189 str_replace("\n", "", $line);
190 str_replace("\n", "", $read[$i]);
191 $line = "$line $read[$i]";
192 $i++;
193 }
194
195 /** Detects filename if any **/
196 if (strpos(strtolower(trim($line)), "filename=")) {
197 $pos = strpos($line, "filename=") + 9;
198 $name = trim($line);
199 if (strpos($line, " ", $pos) > 0) {
200 $name = substr($name, $pos, strpos($line, " ", $pos));
201 } else {
202 $name = substr($name, $pos);
203 }
204 $name = str_replace("\"", "", $name);
205 $header["FILENAME"] = $name;
206 }
207 }
208
209 /** REPLY-TO **/
210 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
211 $header["REPLYTO"] = trim(substr($read[$i], 9, strlen($read[$i])));
212 $i++;
213 }
214
215 /** FROM **/
216 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
217 $header["FROM"] = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
218 if ($header["REPLYTO"] == "")
219 $header["REPLYTO"] = $header["FROM"];
220 $i++;
221 }
222 /** DATE **/
223 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
224 $d = substr($read[$i], 5);
225 $d = trim($d);
226 $d = ereg_replace(" ", " ", $d);
227 $d = explode(" ", $d);
228 $header["DATE"] = getTimeStamp($d);
229 $i++;
230 }
231 /** SUBJECT **/
232 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
233 $header["SUBJECT"] = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
234 if (strlen(Chop($header["SUBJECT"])) == 0)
235 $header["SUBJECT"] = _("(no subject)");
236 $i++;
237 }
238 /** CC **/
239 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
240 $pos = 0;
241 $header["CC"][$pos] = trim(substr($read[$i], 4));
242 $i++;
243 while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")) {
244 $pos++;
245 $header["CC"][$pos] = trim($read[$i]);
246 $i++;
247 }
248 }
249 /** TO **/
250 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
251 $pos = 0;
252 $header["TO"][$pos] = trim(substr($read[$i], 4));
253 $i++;
254 while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")){
255 $pos++;
256 $header["TO"][$pos] = trim($read[$i]);
257 $i++;
258 }
259 }
260 /** MESSAGE ID **/
261 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
262 $header["MESSAGE-ID"] = trim(substr($read[$i], 11));
263 $i++;
264 }
265
266
267 /** ERROR CORRECTION **/
268 else if (substr($read[$i], 0, 1) == ")") {
269 if ($header["SUBJECT"] == "")
270 $header["SUBJECT"] = _("(no subject)");
271
272 if ($header["FROM"] == "")
273 $header["FROM"] = _("(unknown sender)");
274
275 if ($header["DATE"] == "")
276 $header["DATE"] = time();
277 $i++;
278 }
279 else {
280 $i++;
281 }
282 }
283 return $header;
284 }
285
286
287 /******************************************************************************
288 ** Returns the body of a message.
289 ******************************************************************************/
290 function sqimap_get_message_body ($imap_stream, $bound, $id, $type0, $type1, $encoding, $charset) {
291 fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\r\n");
292 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
293
294 $i = 0;
295 $j = 0;
296 while ($i < count($read)-1) {
297 if ( ($i != 0) ) {
298 $bodytmp[$j] = $read[$i];
299 $j++;
300 }
301 $i++;
302 }
303 $body = $bodytmp;
304
305 return decodeMime($body, $bound, $type0, $type1, $encoding, $charset);
306 }
307 ?>