5 ** This implements functions that manipulate messages
8 if (!$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))) {
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;
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 X-Priority)]\r\n");
49 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
51 $subject = _("(no subject)");
52 $from = _("Unknown Sender");
55 for ($i = 0; $i < count($read); $i++
) {
56 if (eregi ("^to:", $read[$i])) {
57 $to = sqimap_find_displayable_name(substr($read[$i], 3));
59 if (eregi ("^from:", $read[$i])) {
60 $from = sqimap_find_displayable_name(substr($read[$i], 5));
62 if (eregi ("^x-priority:", $read[$i])) {
63 $priority = trim(substr($read[$i], 11));
65 if (eregi ("^date:", $read[$i])) {
66 $date = substr($read[$i], 5);
67 } else if (eregi ("^subject:", $read[$i])) {
68 $subject = htmlspecialchars(eregi_replace ("^subject: ", "", $read[$i]));
69 if (trim($subject) == "")
70 $subject = _("(no subject)");
74 $header = new small_header
;
78 $header->from
= $from;
79 $header->date
= $date;
80 $header->subject
= $subject;
82 $header->priority
= $priority;
86 /******************************************************************************
87 ** Returns the flags for the specified messages
88 ******************************************************************************/
89 function sqimap_get_flags ($imap_stream, $i) {
90 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
91 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
92 if (strpos($read[0], "FLAGS")) {
93 $tmp = ereg_replace("\(", "", $read[0]);
94 $tmp = ereg_replace("\)", "", $tmp);
95 $tmp = str_replace("\\", "", $tmp);
96 $tmp = substr($tmp, strpos($tmp, "FLAGS")+
6, strlen($tmp));
98 $flags = explode(" ", $tmp);
105 /******************************************************************************
106 ** Returns a message array with all the information about a message. See
107 ** the documentation folder for more information about this array.
108 ******************************************************************************/
109 function sqimap_get_message ($imap_stream, $id, $mailbox) {
111 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
112 $msg = sqimap_get_message_body($imap_stream, &$header);
116 /******************************************************************************
117 ** Wrapper function that reformats the header information.
118 ******************************************************************************/
119 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
120 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
121 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
123 $header = sqimap_get_header($imap_stream, $read);
125 $header->mailbox
= $mailbox;
130 /******************************************************************************
131 ** Wrapper function that returns entity headers for use by decodeMime
132 ******************************************************************************/
134 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
135 $header = sqimap_get_header($imap_stream, $read);
136 $type0 = $header["TYPE0"];
137 $type1 = $header["TYPE1"];
138 $bound = $header["BOUNDARY"];
139 $encoding = $header["ENCODING"];
140 $charset = $header["CHARSET"];
141 $filename = $header["FILENAME"];
145 /******************************************************************************
146 ** Queries the IMAP server and gets all header information.
147 ******************************************************************************/
148 function sqimap_get_header ($imap_stream, $read) {
149 $hdr = new msg_header();
151 // Set up some defaults
152 $hdr->type0
= "text";
153 $hdr->type1
= "plain";
154 $hdr->charset
= "us-ascii";
156 while ($i < count($read)) {
157 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
162 /** ENCODING TYPE **/
163 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
164 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
169 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
170 $cont = strtolower(trim(substr($read[$i], 13)));
171 if (strpos($cont, ";"))
172 $cont = substr($cont, 0, strpos($cont, ";"));
175 if (strpos($cont, "/")) {
176 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
177 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
185 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
186 str_replace("\n", "", $line);
187 str_replace("\n", "", $read[$i]);
188 $line = "$line $read[$i]";
192 /** Detect the boundary of a multipart message **/
193 if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
194 $hdr->boundary
= $regs[1];
197 /** Detect the charset **/
198 if (strpos(strtolower(trim($line)), "charset=")) {
199 $pos = strpos($line, "charset=") +
8;
200 $charset = trim($line);
201 if (strpos($line, " ", $pos) > 0) {
202 $charset = substr($charset, $pos, strpos($line, " ", $pos));
204 $charset = substr($charset, $pos);
206 $charset = str_replace("\"", "", $charset);
207 $hdr->charset
= $charset;
209 $hdr->charset
= "us-ascii";
214 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
215 /** Add better dontent-disposition support **/
219 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
220 str_replace("\n", "", $line);
221 str_replace("\n", "", $read[$i]);
222 $line = "$line $read[$i]";
226 /** Detects filename if any **/
227 if (strpos(strtolower(trim($line)), "filename=")) {
228 $pos = strpos($line, "filename=") +
9;
230 if (strpos($line, " ", $pos) > 0) {
231 $name = substr($name, $pos, strpos($line, " ", $pos));
233 $name = substr($name, $pos);
235 $name = str_replace("\"", "", $name);
236 $hdr->filename
= $name;
241 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
242 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
247 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
248 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
249 if ($hdr->replyto
== "")
250 $hdr->replyto
= $hdr->from
;
254 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
255 $d = substr($read[$i], 5);
257 $d = ereg_replace(" ", " ", $d);
258 $d = explode(" ", $d);
259 $hdr->date
= getTimeStamp($d);
263 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
264 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
265 if (strlen(Chop($hdr->subject
)) == 0)
266 $hdr->subject
= _("(no subject)");
270 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
272 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
274 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
276 $hdr->cc
[$pos] = trim($read[$i]);
281 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
283 $hdr->to
[$pos] = trim(substr($read[$i], 4));
285 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
287 $hdr->to
[$pos] = trim($read[$i]);
292 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
293 $hdr->message_id
= trim(substr($read[$i], 11));
298 /** ERROR CORRECTION **/
299 else if (substr($read[$i], 0, 1) == ")") {
300 if (strlen(trim($hdr->subject
)) == 0)
301 $hdr->subject
= _("(no subject)");
303 if (strlen(trim($hdr->from
)) == 0)
304 $hdr->from
= _("(unknown sender)");
306 if (strlen(trim($hdr->date
)) == 0)
318 /******************************************************************************
319 ** Returns the body of a message.
320 ******************************************************************************/
321 function sqimap_get_message_body ($imap_stream, &$header) {
323 //fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\r\n");
324 //$read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
328 while ($i < count($read)-1) {
330 $body[$j] = $read[$i];
335 return decodeMime($body, &$header);
339 /******************************************************************************
340 ** Returns an array with the body structure
341 ******************************************************************************/