dee22165663967b710938bf69051c4fdfc86923d
5 ** This implements functions that manipulate messages
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);
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;
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");
26 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
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);
38 /******************************************************************************
39 ** Returns some general header information -- FROM, DATE, and SUBJECT
40 ******************************************************************************/
42 var $from, $subject, $date;
45 function sqimap_get_small_header ($imap_stream, $id, $sent) {
46 fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Subject)]\r\n");
47 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
49 $subject = _("(no subject)");
50 $from = _("Unknown Sender");
52 for ($i = 0; $i < count($read); $i++
) {
54 if (eregi ("^to:", $read[$i])) {
55 $from = sqimap_find_displayable_name(substr($read[$i], 3));
58 if (eregi ("^from:", $read[$i])) {
59 $from = sqimap_find_displayable_name(substr($read[$i], 5));
63 if (eregi ("^date:", $read[$i])) {
64 $date = substr($read[$i], 5);
65 } else if (eregi ("^subject:", $read[$i])) {
66 $subject = htmlspecialchars(eregi_replace ("^subject: ", "", $read[$i]));
67 if (strlen($subject) == 0)
68 $subject = _("(no subject)");
72 $header = new small_header
;
73 $header->from
= $from;
74 $header->date
= $date;
75 $header->subject
= $subject;
80 /******************************************************************************
81 ** Returns the flags for the specified messages
82 ******************************************************************************/
83 function sqimap_get_flags ($imap_stream, $i) {
84 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
85 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
86 if (strpos($read[0], "FLAGS")) {
87 $tmp = ereg_replace("\(", "", $read[0]);
88 $tmp = ereg_replace("\)", "", $tmp);
89 $tmp = str_replace("\\", "", $tmp);
90 $tmp = substr($tmp, strpos($tmp, "FLAGS")+
6, strlen($tmp));
92 $flags = explode(" ", $tmp);
99 /******************************************************************************
100 ** Returns a message array with all the information about a message. See
101 ** the documentation folder for more information about this array.
102 ******************************************************************************/
103 function sqimap_get_message ($imap_stream, $id, $mailbox) {
104 $message["INFO"]["ID"] = $id;
105 $message["INFO"]["MAILBOX"] = $mailbox;
106 $message["HEADER"] = sqimap_get_message_header($imap_stream, $id);
107 $message["ENTITIES"] = sqimap_get_message_body($imap_stream, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"], $message["HEADER"]["ENCODING"], $message["HEADER"]["CHARSET"]);
111 /******************************************************************************
112 ** Wrapper function that reformats the header information.
113 ******************************************************************************/
114 function sqimap_get_message_header ($imap_stream, $id) {
115 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
116 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
118 return sqimap_get_header($imap_stream, $read);
121 /******************************************************************************
122 ** Wrapper function that returns entity headers for use by decodeMime
123 ******************************************************************************/
124 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
125 $header = sqimap_get_header($imap_stream, $read);
126 $type0 = $header["TYPE0"];
127 $type1 = $header["TYPE1"];
128 $bound = $header["BOUNDARY"];
129 $encoding = $header["ENCODING"];
130 $charset = $header["CHARSET"];
131 $filename = $header["FILENAME"];
134 /******************************************************************************
135 ** Queries the IMAP server and gets all header information.
136 ******************************************************************************/
137 function sqimap_get_header ($imap_stream, $read) {
139 // Set up some defaults
140 $header["TYPE0"] = "text";
141 $header["TYPE1"] = "plain";
142 $header["CHARSET"] = "us-ascii";
144 while ($i < count($read)) {
145 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
146 $header["MIME"] = true;
150 /** ENCODING TYPE **/
151 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
152 $header["ENCODING"] = strtolower(trim(substr($read[$i], 26)));
157 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
158 $cont = strtolower(trim(substr($read[$i], 13)));
159 if (strpos($cont, ";"))
160 $cont = substr($cont, 0, strpos($cont, ";"));
163 if (strpos($cont, "/")) {
164 $header["TYPE0"] = substr($cont, 0, strpos($cont, "/"));
165 $header["TYPE1"] = substr($cont, strpos($cont, "/")+
1);
167 $header["TYPE0"] = $cont;
173 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
174 str_replace("\n", "", $line);
175 str_replace("\n", "", $read[$i]);
176 $line = "$line $read[$i]";
180 /** Detect the boundary of a multipart message **/
181 if (eregi("boundary=\"([^\"]+)\"", $line, $regs)) {
182 $header["BOUNDARY"] = $regs[1];
185 /** Detect the charset **/
186 if (strpos(strtolower(trim($line)), "charset=")) {
187 $pos = strpos($line, "charset=") +
8;
188 $charset = trim($line);
189 if (strpos($line, " ", $pos) > 0) {
190 $charset = substr($charset, $pos, strpos($line, " ", $pos));
192 $charset = substr($charset, $pos);
194 $charset = str_replace("\"", "", $charset);
195 $header["CHARSET"] = $charset;
197 $header["CHARSET"] = "us-ascii";
202 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
203 /** Add better dontent-disposition support **/
207 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
208 str_replace("\n", "", $line);
209 str_replace("\n", "", $read[$i]);
210 $line = "$line $read[$i]";
214 /** Detects filename if any **/
215 if (strpos(strtolower(trim($line)), "filename=")) {
216 $pos = strpos($line, "filename=") +
9;
218 if (strpos($line, " ", $pos) > 0) {
219 $name = substr($name, $pos, strpos($line, " ", $pos));
221 $name = substr($name, $pos);
223 $name = str_replace("\"", "", $name);
224 $header["FILENAME"] = $name;
229 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
230 $header["REPLYTO"] = trim(substr($read[$i], 9, strlen($read[$i])));
235 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
236 $header["FROM"] = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
237 if ($header["REPLYTO"] == "")
238 $header["REPLYTO"] = $header["FROM"];
242 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
243 $d = substr($read[$i], 5);
245 $d = ereg_replace(" ", " ", $d);
246 $d = explode(" ", $d);
247 $header["DATE"] = getTimeStamp($d);
251 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
252 $header["SUBJECT"] = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
253 if (strlen(Chop($header["SUBJECT"])) == 0)
254 $header["SUBJECT"] = _("(no subject)");
258 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
260 $header["CC"][$pos] = trim(substr($read[$i], 4));
262 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
264 $header["CC"][$pos] = trim($read[$i]);
269 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
271 $header["TO"][$pos] = trim(substr($read[$i], 4));
273 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
275 $header["TO"][$pos] = trim($read[$i]);
280 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
281 $header["MESSAGE-ID"] = trim(substr($read[$i], 11));
286 /** ERROR CORRECTION **/
287 else if (substr($read[$i], 0, 1) == ")") {
288 if ($header["SUBJECT"] == "")
289 $header["SUBJECT"] = _("(no subject)");
291 if ($header["FROM"] == "")
292 $header["FROM"] = _("(unknown sender)");
294 if ($header["DATE"] == "")
295 $header["DATE"] = time();
306 /******************************************************************************
307 ** Returns the body of a message.
308 ******************************************************************************/
309 function sqimap_get_message_body ($imap_stream, $bound, $id, $type0, $type1, $encoding, $charset) {
310 fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\r\n");
311 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
315 while ($i < count($read)-1) {
317 $bodytmp[$j] = $read[$i];
324 return decodeMime($body, $bound, $type0, $type1, $encoding, $charset);