c08504434a4800f34aace6a49c47d437b2a948c0
5 ** This implements functions that manipulate messages
8 if (!isset($mime_php)) include "../functions/mime.php";
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);
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;
24 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
25 sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
26 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
28 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
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);
40 /******************************************************************************
41 ** Returns some general header information -- FROM, DATE, and SUBJECT
42 ******************************************************************************/
44 var $from, $subject, $date, $to, $priority, $message_id;
47 function sqimap_get_small_header ($imap_stream, $id, $sent) {
49 fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority)]\r\n");
50 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
52 $subject = _("(no subject)");
53 $from = _("Unknown Sender");
58 for ($i = 0; $i < count($read); $i++
) {
59 if (eregi ("^to:", $read[$i])) {
60 //$to = sqimap_find_displayable_name(substr($read[$i], 3));
61 $to = substr($read[$i], 3);
62 } else if (eregi ("^from:", $read[$i])) {
63 //$from = sqimap_find_displayable_name(substr($read[$i], 5));
64 $from = substr($read[$i], 5);
65 } else if (eregi ("^x-priority:", $read[$i])) {
66 $priority = trim(substr($read[$i], 11));
67 } else if (eregi ("^message-id:", $read[$i])) {
68 $messageid = trim(substr($read[$i], 11));
69 } else if (eregi ("^cc:", $read[$i])) {
70 $cc = substr($read[$i], 3);
71 } else if (eregi ("^date:", $read[$i])) {
72 $date = substr($read[$i], 5);
73 } else if (eregi ("^subject:", $read[$i])) {
74 $subject = htmlspecialchars(eregi_replace ("^subject: ", "", $read[$i]));
75 if (trim($subject) == "")
76 $subject = _("(no subject)");
80 // If there isn't a date, it takes the internal date and uses
81 // that as the normal date.
82 if (trim($date) == "") {
83 fputs ($imap_stream, "a002 FETCH $id INTERNALDATE\r\n");
84 $internal_read = sqimap_read_data ($imap_stream, "a002", true, $r, $m);
86 // * 22 FETCH (INTERNALDATE " 8-Sep-2000 13:17:07 -0500")
87 $date = $internal_read[0];
88 $date = eregi_replace(".*internaldate \"", "", $date);
89 $date = eregi_replace("\".*", "", $date);
90 $date_ary = explode(" ", trim($date));
91 $date_ary[0] = str_replace("-", " ", $date_ary[0]);
92 $date = implode (" ", $date_ary);
95 fputs ($imap_stream, "a003 FETCH $id RFC822.SIZE\r\n");
96 $read = sqimap_read_data($imap_stream, "a003", true, $r, $m);
97 preg_match("/([0-9]+)\)\s*$/i", $read[0], $regs);
100 $header = new small_header
;
104 $header->from
= $from;
106 $header->date
= $date;
107 $header->subject
= $subject;
109 $header->priority
= $priority;
110 $header->message_id
= $messageid;
112 $header->size
= $size;
117 /******************************************************************************
118 ** Returns the flags for the specified messages
119 ******************************************************************************/
120 function sqimap_get_flags ($imap_stream, $i) {
121 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
122 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
123 if (strpos($read[0], "FLAGS")) {
124 $tmp = ereg_replace("\(", "", $read[0]);
125 $tmp = ereg_replace("\)", "", $tmp);
126 $tmp = str_replace("\\", "", $tmp);
127 $tmp = substr($tmp, strpos($tmp, "FLAGS")+
6, strlen($tmp));
129 $flags = explode(" ", $tmp);
136 /******************************************************************************
137 ** Returns a message array with all the information about a message. See
138 ** the documentation folder for more information about this array.
139 ******************************************************************************/
140 function sqimap_get_message ($imap_stream, $id, $mailbox) {
141 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
142 $msg = sqimap_get_message_body($imap_stream, &$header);
146 /******************************************************************************
147 ** Wrapper function that reformats the header information.
148 ******************************************************************************/
149 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
150 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
151 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
153 $header = sqimap_get_header($imap_stream, $read);
155 $header->mailbox
= $mailbox;
160 /******************************************************************************
161 ** Wrapper function that returns entity headers for use by decodeMime
162 ******************************************************************************/
164 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
165 $header = sqimap_get_header($imap_stream, $read);
166 $type0 = $header["TYPE0"];
167 $type1 = $header["TYPE1"];
168 $bound = $header["BOUNDARY"];
169 $encoding = $header["ENCODING"];
170 $charset = $header["CHARSET"];
171 $filename = $header["FILENAME"];
175 /******************************************************************************
176 ** Queries the IMAP server and gets all header information.
177 ******************************************************************************/
178 function sqimap_get_header ($imap_stream, $read) {
179 global $where, $what;
181 $hdr = new msg_header();
183 // Set up some defaults
184 $hdr->type0
= "text";
185 $hdr->type1
= "plain";
186 $hdr->charset
= "us-ascii";
188 preg_match("/\{([0-9]+)\}/", $read[$i], $regs);
189 preg_match("/[0-9]+/", $regs[0], $regs);
191 while ($i < count($read)) {
192 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
197 /** ENCODING TYPE **/
198 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
199 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
204 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
205 $cont = strtolower(trim(substr($read[$i], 13)));
206 if (strpos($cont, ";"))
207 $cont = substr($cont, 0, strpos($cont, ";"));
210 if (strpos($cont, "/")) {
211 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
212 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
220 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
221 str_replace("\n", "", $line);
222 str_replace("\n", "", $read[$i]);
223 $line = "$line $read[$i]";
227 /** Detect the boundary of a multipart message **/
228 if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
229 $hdr->boundary
= $regs[1];
232 /** Detect the charset **/
233 if (strpos(strtolower(trim($line)), "charset=")) {
234 $pos = strpos($line, "charset=") +
8;
235 $charset = trim($line);
236 if (strpos($line, " ", $pos) > 0) {
237 $charset = substr($charset, $pos, strpos($line, " ", $pos));
239 $charset = substr($charset, $pos);
241 $charset = str_replace("\"", "", $charset);
242 $hdr->charset
= $charset;
244 $hdr->charset
= "us-ascii";
249 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
250 /** Add better dontent-disposition support **/
254 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
255 str_replace("\n", "", $line);
256 str_replace("\n", "", $read[$i]);
257 $line = "$line $read[$i]";
261 /** Detects filename if any **/
262 if (strpos(strtolower(trim($line)), "filename=")) {
263 $pos = strpos($line, "filename=") +
9;
265 if (strpos($line, " ", $pos) > 0) {
266 $name = substr($name, $pos, strpos($line, " ", $pos));
268 $name = substr($name, $pos);
270 $name = str_replace("\"", "", $name);
271 $hdr->filename
= $name;
276 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
277 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
282 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
283 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
284 if ($hdr->replyto
== "")
285 $hdr->replyto
= $hdr->from
;
289 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
290 $d = substr($read[$i], 5);
292 $d = ereg_replace(" ", " ", $d);
293 $d = explode(" ", $d);
294 $hdr->date
= getTimeStamp($d);
298 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
299 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
300 if (strlen(Chop($hdr->subject
)) == 0)
301 $hdr->subject
= _("(no subject)");
303 if ($where == "SUBJECT") {
304 $hdr->subject
= eregi_replace($what, "<b>\\0</b>", $hdr->subject
);
309 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
311 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
313 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
315 $hdr->cc
[$pos] = trim($read[$i]);
320 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
322 $hdr->to
[$pos] = trim(substr($read[$i], 4));
324 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
326 $hdr->to
[$pos] = trim($read[$i]);
331 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
332 $hdr->message_id
= trim(substr($read[$i], 11));
337 /** ERROR CORRECTION **/
338 else if (substr($read[$i], 0, 1) == ")") {
339 if (strlen(trim($hdr->subject
)) == 0)
340 $hdr->subject
= _("(no subject)");
342 if (strlen(trim($hdr->from
)) == 0)
343 $hdr->from
= _("(unknown sender)");
345 if (strlen(trim($hdr->date
)) == 0)
357 /******************************************************************************
358 ** Returns the body of a message.
359 ******************************************************************************/
360 function sqimap_get_message_body ($imap_stream, &$header) {
362 return decodeMime($imap_stream, $body, &$header);
366 /******************************************************************************
367 ** Returns an array with the body structure
368 ******************************************************************************/