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