may have fixed the blank subject problem
[squirrelmail.git] / functions / imap_messages.php
1 <?php
2 /**
3 ** imap_messages.php
4 **
5 ** This implements functions that manipulate messages
6 **/
7
8 if (!$mime_php) include "../functions/mime.php";
9
10 /******************************************************************************
11 ** Copies specified messages to specified folder
12 ******************************************************************************/
13 function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
14 fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n");
15 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
16 }
17
18 /******************************************************************************
19 ** Deletes specified messages and moves them to trash if possible
20 ******************************************************************************/
21 function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
22 global $move_to_trash, $trash_folder, $auto_expunge;
23
24 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder))) {
25 sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
26 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
27 } else {
28 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
29 }
30 }
31
32 /******************************************************************************
33 ** Sets the specified messages with specified flag
34 ******************************************************************************/
35 function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
36 fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\r\n");
37 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
38 }
39
40 /******************************************************************************
41 ** Returns some general header information -- FROM, DATE, and SUBJECT
42 ******************************************************************************/
43 class small_header {
44 var $from, $subject, $date, $to;
45 }
46
47 function sqimap_get_small_header ($imap_stream, $id, $sent) {
48 fputs ($imap_stream, "a001 FETCH $id 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 $g = 0;
54 for ($i = 0; $i < count($read); $i++) {
55 if (eregi ("^to:", $read[$i])) {
56 $to = sqimap_find_displayable_name(substr($read[$i], 3));
57 }
58 if (eregi ("^from:", $read[$i])) {
59 $from = sqimap_find_displayable_name(substr($read[$i], 5));
60 }
61 if (eregi ("^date:", $read[$i])) {
62 $date = substr($read[$i], 5);
63 } else if (eregi ("^subject:", $read[$i])) {
64 $subject = htmlspecialchars(eregi_replace ("^subject: ", "", $read[$i]));
65 if (trim($subject) == "")
66 $subject = _("(no subject)");
67 }
68 }
69
70 $header = new small_header;
71 if ($sent == true)
72 $header->from = $to;
73 else
74 $header->from = $from;
75 $header->date = $date;
76 $header->subject = $subject;
77 $header->to = $to;
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
107 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
108 $msg = sqimap_get_message_body($imap_stream, &$header);
109 return $msg;
110 }
111
112 /******************************************************************************
113 ** Wrapper function that reformats the header information.
114 ******************************************************************************/
115 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
116 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
117 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
118
119 $header = sqimap_get_header($imap_stream, $read);
120 $header->id = $id;
121 $header->mailbox = $mailbox;
122
123 return $header;
124 }
125
126 /******************************************************************************
127 ** Wrapper function that returns entity headers for use by decodeMime
128 ******************************************************************************/
129 /*
130 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
131 $header = sqimap_get_header($imap_stream, $read);
132 $type0 = $header["TYPE0"];
133 $type1 = $header["TYPE1"];
134 $bound = $header["BOUNDARY"];
135 $encoding = $header["ENCODING"];
136 $charset = $header["CHARSET"];
137 $filename = $header["FILENAME"];
138 }
139 */
140
141 /******************************************************************************
142 ** Queries the IMAP server and gets all header information.
143 ******************************************************************************/
144 function sqimap_get_header ($imap_stream, $read) {
145 $hdr = new msg_header();
146 $i = 0;
147 // Set up some defaults
148 $hdr->type0 = "text";
149 $hdr->type1 = "plain";
150 $hdr->charset = "us-ascii";
151
152 while ($i < count($read)) {
153 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
154 $hdr->mime = true;
155 $i++;
156 }
157
158 /** ENCODING TYPE **/
159 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
160 $hdr->encoding = strtolower(trim(substr($read[$i], 26)));
161 $i++;
162 }
163
164 /** CONTENT-TYPE **/
165 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
166 $cont = strtolower(trim(substr($read[$i], 13)));
167 if (strpos($cont, ";"))
168 $cont = substr($cont, 0, strpos($cont, ";"));
169
170
171 if (strpos($cont, "/")) {
172 $hdr->type0 = substr($cont, 0, strpos($cont, "/"));
173 $hdr->type1 = substr($cont, strpos($cont, "/")+1);
174 } else {
175 $hdr->type0 = $cont;
176 }
177
178
179 $line = $read[$i];
180 $i++;
181 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
182 str_replace("\n", "", $line);
183 str_replace("\n", "", $read[$i]);
184 $line = "$line $read[$i]";
185 $i++;
186 }
187
188 /** Detect the boundary of a multipart message **/
189 if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
190 $hdr->boundary = $regs[1];
191 }
192
193 /** Detect the charset **/
194 if (strpos(strtolower(trim($line)), "charset=")) {
195 $pos = strpos($line, "charset=") + 8;
196 $charset = trim($line);
197 if (strpos($line, " ", $pos) > 0) {
198 $charset = substr($charset, $pos, strpos($line, " ", $pos));
199 } else {
200 $charset = substr($charset, $pos);
201 }
202 $charset = str_replace("\"", "", $charset);
203 $hdr->charset = $charset;
204 } else {
205 $hdr->charset = "us-ascii";
206 }
207
208 }
209
210 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
211 /** Add better dontent-disposition support **/
212
213 $line = $read[$i];
214 $i++;
215 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
216 str_replace("\n", "", $line);
217 str_replace("\n", "", $read[$i]);
218 $line = "$line $read[$i]";
219 $i++;
220 }
221
222 /** Detects filename if any **/
223 if (strpos(strtolower(trim($line)), "filename=")) {
224 $pos = strpos($line, "filename=") + 9;
225 $name = trim($line);
226 if (strpos($line, " ", $pos) > 0) {
227 $name = substr($name, $pos, strpos($line, " ", $pos));
228 } else {
229 $name = substr($name, $pos);
230 }
231 $name = str_replace("\"", "", $name);
232 $hdr->filename = $name;
233 }
234 }
235
236 /** REPLY-TO **/
237 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
238 $hdr->replyto = trim(substr($read[$i], 9, strlen($read[$i])));
239 $i++;
240 }
241
242 /** FROM **/
243 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
244 $hdr->from = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
245 if ($hdr->replyto == "")
246 $hdr->replyto = $hdr->from;
247 $i++;
248 }
249 /** DATE **/
250 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
251 $d = substr($read[$i], 5);
252 $d = trim($d);
253 $d = ereg_replace(" ", " ", $d);
254 $d = explode(" ", $d);
255 $hdr->date = getTimeStamp($d);
256 $i++;
257 }
258 /** SUBJECT **/
259 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
260 $hdr->subject = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
261 if (strlen(Chop($hdr->subject)) == 0)
262 $hdr->subject = _("(no subject)");
263 $i++;
264 }
265 /** CC **/
266 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
267 $pos = 0;
268 $hdr->cc[$pos] = trim(substr($read[$i], 4));
269 $i++;
270 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
271 $pos++;
272 $hdr->cc[$pos] = trim($read[$i]);
273 $i++;
274 }
275 }
276 /** TO **/
277 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
278 $pos = 0;
279 $hdr->to[$pos] = trim(substr($read[$i], 4));
280 $i++;
281 while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
282 $pos++;
283 $hdr->to[$pos] = trim($read[$i]);
284 $i++;
285 }
286 }
287 /** MESSAGE ID **/
288 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
289 $hdr->message_id = trim(substr($read[$i], 11));
290 $i++;
291 }
292
293
294 /** ERROR CORRECTION **/
295 else if (substr($read[$i], 0, 1) == ")") {
296 if (strlen(trim($hdr->subject)) == 0)
297 $hdr->subject = _("(no subject)");
298
299 if (strlen(trim($hdr->from)) == 0)
300 $hdr->from = _("(unknown sender)");
301
302 if (strlen(trim($hdr->date)) == 0)
303 $hdr->date = time();
304 $i++;
305 }
306 else {
307 $i++;
308 }
309 }
310 return $hdr;
311 }
312
313
314 /******************************************************************************
315 ** Returns the body of a message.
316 ******************************************************************************/
317 function sqimap_get_message_body ($imap_stream, &$header) {
318 $id = $header->id;
319 //fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\r\n");
320 //$read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
321
322 $i = 0;
323 $j = 0;
324 while ($i < count($read)-1) {
325 if ( ($i != 0) ) {
326 $body[$j] = $read[$i];
327 $j++;
328 }
329 $i++;
330 }
331 return decodeMime($body, &$header);
332 }
333
334
335 /******************************************************************************
336 ** Returns an array with the body structure
337 ******************************************************************************/
338 ?>