(
+ */
+
+ /* extract the message id */
+ $i_space = strpos($read,' ',2);
+ $id = substr($read,2,$i_space-2);
+ $fetch = substr($read,$i_space+1,5);
+ if (!is_numeric($id) && $fetch !== 'FETCH') {
+ set_up_language($squirrelmail_language);
+ echo '
' .
+ _("ERROR : Could not complete request.") .
+ '
' .
+ _("Unknown response from IMAP server: ") . ' 1.' .
+ htmlspecialchars($read) . "
\n";
+ break;
}
- /** CC **/
- else if (strtolower(substr($read[$i], 0, 3)) == "cc:") {
- $pos = 0;
- $hdr->cc[$pos] = trim(substr($read[$i], 4));
- $i++;
- while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
- $pos++;
- $hdr->cc[$pos] = trim($read[$i]);
- $i++;
+ $i = strpos($read,'(',$i_space+5);
+ $read = substr($read,$i+1);
+ $i_len = strlen($read);
+ $i = 0;
+ while ($i < $i_len && $i !== false) {
+ /* get argument */
+ $read = trim(substr($read,$i));
+ $i_len = strlen($read);
+ $i = strpos($read,' ');
+ $arg = substr($read,0,$i);
+ ++$i;
+ switch ($arg)
+ {
+ case 'UID':
+ $i_pos = strpos($read,' ',$i);
+ if (!$i_pos) {
+ $i_pos = strpos($read,')',$i);
+ }
+ if ($i_pos) {
+ $unique_id = substr($read,$i,$i_pos-$i);
+ $i = $i_pos+1;
+ } else {
+ break 3;
+ }
+ break;
+ case 'FLAGS':
+ $flags = parseArray($read,$i);
+ if (!$flags) break 3;
+ foreach ($flags as $flag) {
+ $flag = strtolower($flag);
+ switch ($flag)
+ {
+ case '\\seen': $flag_seen = true; break;
+ case '\\answered': $flag_answered = true; break;
+ case '\\deleted': $flag_deleted = true; break;
+ case '\\flagged': $flag_flagged = true; break;
+ default: break;
+ }
+ }
+ break;
+ case 'RFC822.SIZE':
+ $i_pos = strpos($read,' ',$i);
+ if (!$i_pos) {
+ $i_pos = strpos($read,')',$i);
+ }
+ if ($i_pos) {
+ $size = substr($read,$i,$i_pos-$i);
+ $i = $i_pos+1;
+ } else {
+ break 3;
+ }
+
+ break;
+ case 'INTERNALDATE':
+ $date = parseString($read,$i);
+ //if ($tmpdate === false) break 3;
+ //$tmpdate = str_replace(' ',' ',$tmpdate);
+ //$tmpdate = explode(' ',$tmpdate);
+ //$date = str_replace('-',' ',$tmpdate[0]) . " " .
+ // $tmpdate[1] . ' ' . $tmpdate[2];
+ break;
+ case 'BODY.PEEK[HEADER.FIELDS':
+ case 'BODY[HEADER.FIELDS':
+ $i = strpos($read,'{',$i);
+ $header = parseString($read,$i);
+ if ($header === false) break 3;
+ /* First we unfold the header */
+ $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $header));
+ /* Now we can make a new header array with */
+ /* each element representing a headerline */
+ $hdr = explode("\r\n" , $hdr);
+ foreach ($hdr as $line) {
+ $pos = strpos($line, ':');
+ if ($pos > 0) {
+ $field = strtolower(substr($line, 0, $pos));
+ if (!strstr($field,' ')) { /* valid field */
+ $value = trim(substr($line, $pos+1));
+ switch($field)
+ {
+ case 'to': $to = $value; break;
+ case 'cc': $cc = $value; break;
+ case 'from': $from = $value; break;
+ case 'date': $date = $value; break;
+ case 'x-priority': $priority = $value; break;
+ case 'subject':
+ $subject = $value;
+ if ($subject == "") {
+ $subject = _("(no subject)");
+ }
+ break;
+ case 'content-type':
+ $type = $value;
+ if ($pos = strpos($type, ";")) {
+ $type = substr($type, 0, $pos);
+ }
+ $type = explode("/", $type);
+ if(!is_array($type)) {
+ $type[0] = 'text';
+ }
+ if (!isset($type[1])) {
+ $type[1] = '';
+ }
+ break;
+ default: break;
+ }
+ }
+ }
+ }
+ break;
+ default:
+ ++$i;
+ break;
}
}
- /** BCC **/
- else if (strtolower(substr($read[$i], 0, 4)) == "bcc:") {
- $pos = 0;
- $hdr->bcc[$pos] = trim(substr($read[$i], 5));
- $i++;
- while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
- $pos++;
- $hdr->bcc[$pos] = trim($read[$i]);
- $i++;
- }
+ if (isset($date)) {
+ $date = str_replace(' ', ' ', $date);
+ $tmpdate = explode(' ', trim($date));
+ } else {
+ $tmpdate = $date = array('', '', '', '', '', '');
}
- /** TO **/
- else if (strtolower(substr($read[$i], 0, 3)) == "to:") {
- $pos = 0;
- $hdr->to[$pos] = trim(substr($read[$i], 4));
- $i++;
- while (((substr($read[$i], 0, 1) == " ") || (substr($read[$i], 0, 1) == "\t")) && (trim($read[$i]) != "")){
- $pos++;
- $hdr->to[$pos] = trim($read[$i]);
- $i++;
- }
+ if ($uid_support) {
+ $msgi ="$unique_id";
+ $messages[$msgi]['ID'] = $unique_id;
+ } else {
+ $msgi = "$id";
+ $messages[$msgi]['ID'] = $id;
}
- /** MESSAGE ID **/
- else if (strtolower(substr($read[$i], 0, 11)) == "message-id:") {
- $hdr->message_id = trim(substr($read[$i], 11));
- $i++;
+ $messages[$msgi]['TIME_STAMP'] = getTimeStamp($tmpdate);
+ $messages[$msgi]['DATE_STRING'] = getDateString($messages[$msgi]['TIME_STAMP']);
+ $messages[$msgi]['FROM'] = $from; //parseAddress($from);
+ $messages[$msgi]['SUBJECT'] = $subject;
+// if (handleAsSent($mailbox)) {
+ $messages[$msgi]['TO'] = $to; //parseAddress($to);
+// }
+ $messages[$msgi]['PRIORITY'] = $priority;
+ $messages[$msgi]['CC'] = $cc; //parseAddress($cc);
+ $messages[$msgi]['SIZE'] = $size;
+ $messages[$msgi]['TYPE0'] = $type[0];
+ $messages[$msgi]['FLAG_DELETED'] = $flag_deleted;
+ $messages[$msgi]['FLAG_ANSWERED'] = $flag_answered;
+ $messages[$msgi]['FLAG_SEEN'] = $flag_seen;
+ $messages[$msgi]['FLAG_FLAGGED'] = $flag_flagged;
+
+ /* non server sort stuff */
+ if (!$allow_server_sort) {
+ $from = parseAddress($from);
+ if ($from[0][1]) {
+ $from = decodeHeader($from[0][1]);
+ } else {
+ $from = $from[0][0];
+ }
+ $messages[$msgi]['FROM-SORT'] = $from;
+ $subject_sort = strtolower(decodeHeader($subject));
+ if (preg_match("/^(vedr|sv|re|aw):\s*(.*)$/si", $subject_sort, $matches)){
+ $messages[$msgi]['SUBJECT-SORT'] = $matches[2];
+ } else {
+ $messages[$msgi]['SUBJECT-SORT'] = $subject_sort;
+ }
}
+ ++$msgi;
+ }
+ array_reverse($messages);
+ $new_messages = array();
+ foreach ($messages as $i =>$message) {
+ $new_messages[] = $message;
+ }
+ return $new_messages;
+}
+// obsolete?
+function sqimap_get_headerfield($imap_stream, $field) {
+ global $uid_support;
+ $sid = sqimap_session_id(false);
- /** ERROR CORRECTION **/
- else if (substr($read[$i], 0, 1) == ")") {
- if (strlen(trim($hdr->subject)) == 0) {
- $hdr->subject = _("(no subject)");
- }
- if (strlen(trim($hdr->from)) == 0) {
- $hdr->from = _("(unknown sender)");
+ $results = array();
+ $read_list = array();
+
+ $query = "FETCH 1:* (UID BODY.PEEK[HEADER.FIELDS ($field)])";
+ $readin_list = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, $uid_support);
+ $i = 0;
+
+ foreach ($readin_list as $r) {
+ $r = implode('',$r);
+ /* first we unfold the header */
+ $r = str_replace(array("\r\n\t","\r\n\s"),array('',''),$r);
+ /*
+ * now we can make a new header array with each element representing
+ * a headerline
+ */
+ $r = explode("\r\n" , $r);
+ if (!$uid_support) {
+ if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH/iAU",$r[0], $regs)) {
+ set_up_language($squirrelmail_language);
+ echo '
' .
+ _("ERROR : Could not complete request.") .
+ '
' .
+ _("Unknown response from IMAP server: ") . ' 1.' .
+ $r[0] . "
\n";
+ } else {
+ $id = $regs[1];
}
- if (strlen(trim($hdr->date)) == 0) {
- $hdr->date = time();
+ } else {
+ if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH.*UID\s+([0-9]+)\s+/iAU",$r[0], $regs)) {
+ set_up_language($squirrelmail_language);
+ echo '
' .
+ _("ERROR : Could not complete request.") .
+ '
' .
+ _("Unknown response from IMAP server: ") . ' 1.' .
+ $r[0] . "
\n";
+ } else {
+ $id = $regs[2];
}
- $i++;
}
- /** X-PRIORITY **/
- else if (strtolower(substr($read[$i], 0, 11)) == "x-priority:") {
- $hdr->priority = trim(substr($read[$i], 11));
- $i++;
- }
- else {
- $i++;
+ $field = $r[1];
+ $field = substr($field,strlen($field)+2);
+ $result[] = array($id,$field);
+ }
+ return $result;
+}
+
+
+
+
+
+/*
+ * Returns a message array with all the information about a message.
+ * See the documentation folder for more information about this array.
+ */
+function sqimap_get_message ($imap_stream, $id, $mailbox) {
+ global $uid_support;
+
+ $flags = array();
+ $read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, $uid_support);
+ if ($read) {
+ if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) {
+ if (trim($regs[1])) {
+ $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY');
+ }
}
- }
- return $hdr;
+ } else {
+ /* the message was not found, maybe the mailbox was modified? */
+ global $sort, $startMessage, $color;
+
+ $errmessage = _("The server couldn't find the message you requested.") .
+ ''._("Most probably your message list was out of date and the message has been moved away or deleted (perhaps by another program accessing the same mailbox).");
+ /* this will include a link back to the message list */
+ error_message($errmessage, $mailbox, $sort, $startMessage, $color);
+ exit;
+ }
+ $bodystructure = implode('',$read);
+ $msg = mime_structure($bodystructure,$flags);
+ $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support);
+ $rfc822_header = new Rfc822Header();
+ $rfc822_header->parseHeader($read);
+ $msg->rfc822_header = $rfc822_header;
+ return $msg;
}
+/* Wrapper function that reformats the header information. */
+// obsolete?
+function sqimap_get_message_header ($imap_stream, $id, $mailbox) {
+ global $uid_support;
+ $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support);
+ $header = sqimap_get_header($imap_stream, $read);
+ $header->id = $id;
+ $header->mailbox = $mailbox;
+ return $header;
+}
-/****************************************************************************
- ** Returns the body of a message.
- ****************************************************************************/
-function sqimap_get_message_body ($imap_stream, &$header) {
- $id = $header->id;
- return decodeMime($imap_stream, $header);
+/* Wrapper function that reformats the entity header information. */
+// obsolete?
+function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) {
+ global $uid_support;
+ $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent.HEADER]", true, $response, $message, $uid_support);
+ $header = sqimap_get_header($imap_stream, $read);
+ $header->id = $id;
+ $header->mailbox = $mailbox;
+ return $header;
}
+/* function to get the mime headers */
+// obsolete?
+function sqimap_get_mime_ent_header ($imap_stream, $id, $mailbox, $ent) {
+ global $uid_support;
+ $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.MIME]", true, $response, $message, $uid_support);
+ $header = sqimap_get_header($imap_stream, $read);
+ $header->id = $id;
+ $header->mailbox = $mailbox;
+ return $header;
+}
-/****************************************************************************
- ** Returns an array with the body structure
- ****************************************************************************/
?>