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);
39 /******************************************************************************
40 ** Returns some general header information -- FROM, DATE, and SUBJECT
41 ******************************************************************************/
42 function sqimap_get_small_header ($imap_stream, $id, &$from, &$subject, &$date) {
43 fputs ($imap_stream, "a001 FETCH $id:$id RFC822.HEADER.LINES (From Subject Date)\r\n");
44 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
46 $subject = _("(no subject)");
47 $from = _("Unknown Sender");
48 for ($i = 0; $i < count($read); $i++
) {
49 if (strtolower(substr($read[$i], 0, 5)) == "from:") {
50 $from = sqimap_find_displayable_name(substr($read[$i], 5));
51 } else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
52 $date = substr($read[$i], 5);
53 } else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
54 $subject = htmlspecialchars(substr($read[$i], 8));
55 if (strlen(trim($subject)) == 0)
56 $subject = _("(no subject)");
61 /******************************************************************************
62 ** Returns the flags for the specified messages
63 ******************************************************************************/
64 function sqimap_get_flags ($imap_stream, $i) {
65 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
66 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
67 if (strpos($read[0], "FLAGS")) {
68 $tmp = ereg_replace("\(", "", $read[0]);
69 $tmp = ereg_replace("\)", "", $tmp);
70 $tmp = str_replace("\\", "", $tmp);
71 $tmp = substr($tmp, strpos($tmp, "FLAGS")+
6, strlen($tmp));
73 $flags = explode(" ", $tmp);
80 /******************************************************************************
81 ** Returns a message array with all the information about a message. See
82 ** the documentation folder for more information about this array.
83 ******************************************************************************/
84 function sqimap_get_message ($imap_stream, $id, $mailbox) {
85 $message["INFO"]["ID"] = $id;
86 $message["INFO"]["MAILBOX"] = $mailbox;
87 $message["HEADER"] = sqimap_get_message_header($imap_stream, $id);
88 $message["ENTITIES"] = sqimap_get_message_body($imap_stream, $message["HEADER"]["BOUNDARY"], $id, $message["HEADER"]["TYPE0"], $message["HEADER"]["TYPE1"], $message["HEADER"]["ENCODING"], $message["HEADER"]["CHARSET"]);
92 /******************************************************************************
93 ** Wrapper function that reformats the header information.
94 ******************************************************************************/
95 function sqimap_get_message_header ($imap_stream, $id) {
96 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
97 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
99 return sqimap_get_header($imap_stream, $read);
102 /******************************************************************************
103 ** Wrapper function that returns entity headers for use by decodeMime
104 ******************************************************************************/
105 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
106 $header = sqimap_get_header($imap_stream, $read);
107 $type0 = $header["TYPE0"];
108 $type1 = $header["TYPE1"];
109 $bound = $header["BOUNDARY"];
110 $encoding = $header["ENCODING"];
111 $charset = $header["CHARSET"];
112 $filename = $header["FILENAME"];
115 /******************************************************************************
116 ** Queries the IMAP server and gets all header information.
117 ******************************************************************************/
118 function sqimap_get_header ($imap_stream, $read) {
120 // Set up some defaults
121 $header["TYPE0"] = "text";
122 $header["TYPE1"] = "plain";
123 $header["CHARSET"] = "us-ascii";
125 while ($i < count($read)) {
126 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
127 $header["MIME"] = true;
131 /** ENCODING TYPE **/
132 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
133 $header["ENCODING"] = strtolower(trim(substr($read[$i], 26)));
138 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
139 $cont = strtolower(trim(substr($read[$i], 13)));
140 if (strpos($cont, ";"))
141 $cont = substr($cont, 0, strpos($cont, ";"));
144 if (strpos($cont, "/")) {
145 $header["TYPE0"] = substr($cont, 0, strpos($cont, "/"));
146 $header["TYPE1"] = substr($cont, strpos($cont, "/")+
1);
148 $header["TYPE0"] = $cont;
154 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
155 str_replace("\n", "", $line);
156 str_replace("\n", "", $read[$i]);
157 $line = "$line $read[$i]";
161 /** Detect the boundary of a multipart message **/
162 if (strpos(strtolower(trim($line)), "boundary=")) {
163 $pos = strpos(strtolower($line), "boundary=") +
9;
164 $bound = trim($line);
165 if (strpos($line, " ", $pos) > 0) {
166 $bound = substr($bound, $pos, strpos($line, " ", $pos));
168 $bound = substr($bound, $pos);
170 $bound = str_replace("\"", "", $bound);
171 $header["BOUNDARY"] = $bound;
174 /** Detect the charset **/
175 if (strpos(strtolower(trim($line)), "charset=")) {
176 $pos = strpos($line, "charset=") +
8;
177 $charset = trim($line);
178 if (strpos($line, " ", $pos) > 0) {
179 $charset = substr($charset, $pos, strpos($line, " ", $pos));
181 $charset = substr($charset, $pos);
183 $charset = str_replace("\"", "", $charset);
184 $header["CHARSET"] = $charset;
186 $header["CHARSET"] = "us-ascii";
191 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
192 /** Add better dontent-disposition support **/
196 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
197 str_replace("\n", "", $line);
198 str_replace("\n", "", $read[$i]);
199 $line = "$line $read[$i]";
203 /** Detects filename if any **/
204 if (strpos(strtolower(trim($line)), "filename=")) {
205 $pos = strpos($line, "filename=") +
9;
207 if (strpos($line, " ", $pos) > 0) {
208 $name = substr($name, $pos, strpos($line, " ", $pos));
210 $name = substr($name, $pos);
212 $name = str_replace("\"", "", $name);
213 $header["FILENAME"] = $name;
218 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
219 $header["REPLYTO"] = trim(substr($read[$i], 9, strlen($read[$i])));
224 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
225 $header["FROM"] = trim(substr($read[$i], 5, strlen($read[$i]) - 6));
226 if ($header["REPLYTO"] == "")
227 $header["REPLYTO"] = $header["FROM"];
231 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
232 $d = substr($read[$i], 5);
234 $d = ereg_replace(" ", " ", $d);
235 $d = explode(" ", $d);
236 $header["DATE"] = getTimeStamp($d);
240 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
241 $header["SUBJECT"] = trim(substr($read[$i], 8, strlen($read[$i]) - 9));
242 if (strlen(Chop($header["SUBJECT"])) == 0)
243 $header["SUBJECT"] = _("(no subject)");
247 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
249 $header["CC"][$pos] = trim(substr($read[$i], 4));
251 while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")) {
253 $header["CC"][$pos] = trim($read[$i]);
258 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
260 $header["TO"][$pos] = trim(substr($read[$i], 4));
262 while ((substr($read[$i], 0, 1) == " ") && (trim($read[$i]) != "")){
264 $header["TO"][$pos] = trim($read[$i]);
269 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
270 $header["MESSAGE-ID"] = trim(substr($read[$i], 11));
275 /** ERROR CORRECTION **/
276 else if (substr($read[$i], 0, 1) == ")") {
277 if ($header["SUBJECT"] == "")
278 $header["SUBJECT"] = _("(no subject)");
280 if ($header["FROM"] == "")
281 $header["FROM"] = _("(unknown sender)");
283 if ($header["DATE"] == "")
284 $header["DATE"] = time();
295 /******************************************************************************
296 ** Returns the body of a message.
297 ******************************************************************************/
298 function sqimap_get_message_body ($imap_stream, $bound, $id, $type0, $type1, $encoding, $charset) {
299 fputs ($imap_stream, "a001 FETCH $id:$id BODY[TEXT]\r\n");
300 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
304 while ($i < count($read)-1) {
306 $bodytmp[$j] = $read[$i];
313 return decodeMime($body, $bound, $type0, $type1, $encoding, $charset);