5 ** This implements functions that manipulate messages
10 if (defined ('imap_messages_php'))
12 define ('imap_messages_php', true);
14 /******************************************************************************
15 ** Copies specified messages to specified folder
16 ******************************************************************************/
17 function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) {
18 fputs ($imap_stream, "a001 COPY $start:$end \"$mailbox\"\r\n");
19 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
22 /******************************************************************************
23 ** Deletes specified messages and moves them to trash if possible
24 ******************************************************************************/
25 function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) {
26 global $move_to_trash, $trash_folder, $auto_expunge;
28 if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) {
29 sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder);
30 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
32 sqimap_messages_flag ($imap_stream, $start, $end, "Deleted");
36 /******************************************************************************
37 ** Sets the specified messages with specified flag
38 ******************************************************************************/
39 function sqimap_messages_flag ($imap_stream, $start, $end, $flag) {
40 fputs ($imap_stream, "a001 STORE $start:$end +FLAGS (\\$flag)\r\n");
41 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
45 /******************************************************************************
46 ** Remove specified flag from specified messages
47 ******************************************************************************/
48 function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag) {
49 fputs ($imap_stream, "a001 STORE $start:$end -FLAGS (\\$flag)\r\n");
50 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
54 /******************************************************************************
55 ** Returns some general header information -- FROM, DATE, and SUBJECT
56 ******************************************************************************/
58 var $from = '', $subject = '', $date = '', $to = '',
59 $priority = 0, $message_id = 0, $cc = '';
62 function sqimap_get_small_header ($imap_stream, $id, $sent) {
64 fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject Message-Id X-Priority Content-Type)]\r\n");
65 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
67 $subject = _("(no subject)");
68 $from = _("Unknown Sender");
78 for ($i = 0; $i < count($read); $i++
) {
79 if (eregi ("^to:(.*)$", $read[$i], $regs)) {
80 //$to = sqimap_find_displayable_name(substr($read[$i], 3));
82 } else if (eregi ("^from:(.*)$", $read[$i], $regs)) {
83 //$from = sqimap_find_displayable_name(substr($read[$i], 5));
85 } else if (eregi ("^x-priority:(.*)$", $read[$i], $regs)) {
86 $priority = trim($regs[1]);
87 } else if (eregi ("^message-id:(.*)$", $read[$i], $regs)) {
88 $messageid = trim($regs[1]);
89 } else if (eregi ("^cc:(.*)$", $read[$i], $regs)) {
91 } else if (eregi ("^date:(.*)$", $read[$i], $regs)) {
93 } else if (eregi ("^subject:(.*)$", $read[$i], $regs)) {
94 $subject = htmlspecialchars(trim($regs[1]));
96 $subject = _("(no subject)");
97 } else if (eregi ("^content-type:(.*)$", $read[$i], $regs)) {
98 $type = strtolower(trim($regs[1]));
99 if ($pos = strpos($type, ";"))
100 $type = substr($type, 0, $pos);
101 $type = explode("/", $type);
102 if (! isset($type[1]))
108 // If there isn't a date, it takes the internal date and uses
109 // that as the normal date.
110 if (trim($date) == "") {
111 fputs ($imap_stream, "a002 FETCH $id INTERNALDATE\r\n");
112 $internal_read = sqimap_read_data ($imap_stream, "a002", true, $r, $m);
114 // * 22 FETCH (INTERNALDATE " 8-Sep-2000 13:17:07 -0500")
115 $date = $internal_read[0];
116 $date = eregi_replace(".*internaldate \"", "", $date);
117 $date = eregi_replace("\".*", "", $date);
118 $date_ary = explode(" ", trim($date));
119 $date_ary[0] = str_replace("-", " ", $date_ary[0]);
120 $date = implode (" ", $date_ary);
123 fputs ($imap_stream, "a003 FETCH $id RFC822.SIZE\r\n");
124 $read = sqimap_read_data($imap_stream, "a003", true, $r, $m);
125 eregi("([0-9]+)[^0-9]*$", $read[0], $regs);
128 $header = new small_header
;
130 $header->from
= (trim($to) != '')?
$to : _("(only Cc/Bcc)");
132 $header->from
= $from;
134 $header->date
= $date;
135 $header->subject
= $subject;
137 $header->priority
= $priority;
138 $header->message_id
= $messageid;
140 $header->size
= $size;
141 $header->type0
= $type[0];
142 $header->type1
= $type[1];
147 /******************************************************************************
148 ** Returns the flags for the specified messages
149 ******************************************************************************/
150 function sqimap_get_flags ($imap_stream, $i) {
151 fputs ($imap_stream, "a001 FETCH $i:$i FLAGS\r\n");
152 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
153 if (ereg("FLAGS(.*)", $read[0], $regs))
154 return explode(" ", trim(ereg_replace('[\\(\\)\\\\]', '', $regs[1])));
155 return Array('None');
158 /******************************************************************************
159 ** Returns a message array with all the information about a message. See
160 ** the documentation folder for more information about this array.
161 ******************************************************************************/
162 function sqimap_get_message ($imap_stream, $id, $mailbox) {
163 $header = sqimap_get_message_header($imap_stream, $id, $mailbox);
164 return sqimap_get_message_body($imap_stream, $header);
167 /******************************************************************************
168 ** Wrapper function that reformats the header information.
169 ******************************************************************************/
170 function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
171 fputs ($imap_stream, "a001 FETCH $id:$id BODY[HEADER]\r\n");
172 $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
174 $header = sqimap_get_header($imap_stream, $read);
176 $header->mailbox
= $mailbox;
181 /******************************************************************************
182 ** Wrapper function that returns entity headers for use by decodeMime
183 ******************************************************************************/
185 function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) {
186 $header = sqimap_get_header($imap_stream, $read);
187 $type0 = $header["TYPE0"];
188 $type1 = $header["TYPE1"];
189 $bound = $header["BOUNDARY"];
190 $encoding = $header["ENCODING"];
191 $charset = $header["CHARSET"];
192 $filename = $header["FILENAME"];
196 /******************************************************************************
197 ** Queries the IMAP server and gets all header information.
198 ******************************************************************************/
199 function sqimap_get_header ($imap_stream, $read) {
200 global $where, $what;
202 $hdr = new msg_header();
204 // Set up some defaults
205 $hdr->type0
= "text";
206 $hdr->type1
= "plain";
207 $hdr->charset
= "us-ascii";
209 while ($i < count($read)) {
210 if (substr($read[$i], 0, 17) == "MIME-Version: 1.0") {
215 /** ENCODING TYPE **/
216 else if (substr(strtolower($read[$i]), 0, 26) == "content-transfer-encoding:") {
217 $hdr->encoding
= strtolower(trim(substr($read[$i], 26)));
222 else if (strtolower(substr($read[$i], 0, 13)) == "content-type:") {
223 $cont = strtolower(trim(substr($read[$i], 13)));
224 if (strpos($cont, ";"))
225 $cont = substr($cont, 0, strpos($cont, ";"));
228 if (strpos($cont, "/")) {
229 $hdr->type0
= substr($cont, 0, strpos($cont, "/"));
230 $hdr->type1
= substr($cont, strpos($cont, "/")+
1);
238 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
239 str_replace("\n", "", $line);
240 str_replace("\n", "", $read[$i]);
241 $line = "$line $read[$i]";
245 /** Detect the boundary of a multipart message **/
246 if (eregi('boundary="([^"]+)"', $line, $regs)) {
247 $hdr->boundary
= $regs[1];
250 /** Detect the charset **/
251 if (strpos(strtolower(trim($line)), "charset=")) {
252 $pos = strpos($line, "charset=") +
8;
253 $charset = trim($line);
254 if (strpos($line, ";", $pos) > 0) {
255 $charset = substr($charset, $pos, strpos($line, ";", $pos)-$pos);
257 $charset = substr($charset, $pos);
259 $charset = str_replace("\"", "", $charset);
260 $hdr->charset
= $charset;
262 $hdr->charset
= "us-ascii";
267 else if (strtolower(substr($read[$i], 0, 20)) == "content-disposition:") {
268 /** Add better dontent-disposition support **/
272 while ( (substr(substr($read[$i], 0, strpos($read[$i], " ")), -1) != ":") && (trim($read[$i]) != "") && (trim($read[$i]) != ")")) {
273 str_replace("\n", "", $line);
274 str_replace("\n", "", $read[$i]);
275 $line = "$line $read[$i]";
279 /** Detects filename if any **/
280 if (strpos(strtolower(trim($line)), "filename=")) {
281 $pos = strpos($line, "filename=") +
9;
283 if (strpos($line, " ", $pos) > 0) {
284 $name = substr($name, $pos, strpos($line, " ", $pos));
286 $name = substr($name, $pos);
288 $name = str_replace("\"", "", $name);
289 $hdr->filename
= $name;
294 else if (strtolower(substr($read[$i], 0, 9)) == "reply-to:") {
295 $hdr->replyto
= trim(substr($read[$i], 9, strlen($read[$i])));
300 else if (strtolower(substr($read[$i], 0, 5)) == "from:") {
301 $hdr->from
= trim(substr($read[$i], 5, strlen($read[$i]) - 6));
302 if (! isset($hdr->replyto
) ||
$hdr->replyto
== "")
303 $hdr->replyto
= $hdr->from
;
307 else if (strtolower(substr($read[$i], 0, 5)) == "date:") {
308 $d = substr($read[$i], 5);
310 $d = strtr($d, array(' ' => ' '));
311 $d = explode(' ', $d);
312 $hdr->date
= getTimeStamp($d);
316 else if (strtolower(substr($read[$i], 0, 8)) == "subject:") {
317 $hdr->subject
= trim(substr($read[$i], 8, strlen($read[$i]) - 9));
318 if (strlen(Chop($hdr->subject
)) == 0)
319 $hdr->subject
= _("(no subject)");
321 if ($where == "SUBJECT") {
322 $hdr->subject
= eregi_replace($what, "<b>\\0</b>", $hdr->subject
);
327 else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
329 $hdr->cc
[$pos] = trim(substr($read[$i], 4));
331 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
333 $hdr->cc
[$pos] = trim($read[$i]);
338 else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
340 $hdr->to
[$pos] = trim(substr($read[$i], 4));
342 while (((substr($read[$i], 0, 1) == " ") ||
(substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
344 $hdr->to
[$pos] = trim($read[$i]);
349 else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
350 $hdr->message_id
= trim(substr($read[$i], 11));
355 /** ERROR CORRECTION **/
356 else if (substr($read[$i], 0, 1) == ")") {
357 if (strlen(trim($hdr->subject
)) == 0)
358 $hdr->subject
= _("(no subject)");
360 if (strlen(trim($hdr->from
)) == 0)
361 $hdr->from
= _("(unknown sender)");
363 if (strlen(trim($hdr->date
)) == 0)
375 /******************************************************************************
376 ** Returns the body of a message.
377 ******************************************************************************/
378 function sqimap_get_message_body ($imap_stream, &$header) {
380 return decodeMime($imap_stream, $header);
384 /******************************************************************************
385 ** Returns an array with the body structure
386 ******************************************************************************/