- }
- }
- arsort($read_list);
-
- $patterns = array (
- "/^To:(.*)\$/AUi",
- "/^From:(.*)\$/AUi",
- "/^X-Priority:(.*)\$/AUi",
- "/^Cc:(.*)\$/AUi",
- "/^Date:(.*)\$/AUi",
- "/^Subject:(.*)\$/AUi",
- "/^Content-Type:(.*)\$/AUi"
- );
- $regpattern = '';
-
- for ($msgi = 0; $msgi < $maxmsg; $msgi++) {
- $subject = _("(no subject)");
- $from = _("Unknown Sender");
- $priority = 0;
- $messageid = "<>";
- $cc = "";
- $to = "";
- $date = "";
- $type[0] = "";
- $type[1] = "";
- $inrepto = "";
- $flag_seen = false;
- $flag_answered = false;
- $flag_deleted = false;
- $flag_flagged = false;
- $read = $read_list[$msgi];
- $prevline = false;
-
- foreach ($read as $read_part) {
- //unfold multi-line headers
- while ($prevline && strspn($read_part, "\t ") > 0) {
- $read_part = substr($prevline, 0, -2) . ' ' . ltrim($read_part);
- }
- $prev_line = $read_part;
- if ($read_part{0} == '*') {
- if ($internaldate) {
- if (preg_match ("/^.+INTERNALDATE\s+\"(.+)\".+/iUA",$read_part, $reg)) {
- $tmpdate = trim($reg[1]);
- $tmpdate = str_replace(' ',' ',$tmpdate);
- $tmpdate = explode(' ',$tmpdate);
- $date = str_replace('-',' ',$tmpdate[0]) . " " .
- $tmpdate[1] . " " .
- $tmpdate[2];
+ $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;
+ /*
+ * use allcaps for imap items and lowcaps for headers as key for the $aMsg array
+ */
+ 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;
+ $aFlags = array();
+ foreach ($flags as $flag) {
+ $flag = strtolower($flag);
+ $aFlags[$flag] = true;
+ }
+ $aMsg['FLAGS'] = $aFlags;
+ break;
+ case 'RFC822.SIZE':
+ $i_pos = strpos($read,' ',$i);
+ if (!$i_pos) {
+ $i_pos = strpos($read,')',$i);
+ }
+ if ($i_pos) {
+ $aMsg['SIZE'] = substr($read,$i,$i_pos-$i);
+ $i = $i_pos+1;
+ } else {
+ break 3;
+ }
+ break;
+ case 'ENVELOPE':
+ // sqimap_parse_address($read,$i,$aMsg);
+ break; // to be implemented, moving imap code out of the Message class
+ case 'BODYSTRUCTURE':
+ break; // to be implemented, moving imap code out of the Message class
+ case 'INTERNALDATE':
+ $aMsg['INTERNALDATE'] = trim(str_replace(' ', ' ',parseString($read,$i)));
+ break;
+ case 'BODY.PEEK[HEADER.FIELDS':
+ case 'BODY[HEADER.FIELDS':
+ $i = strpos($read,'{',$i); // header is always returned as literal because it contain \n characters
+ $header = parseString($read,$i);
+ if ($header === false) break 2;
+ /* First we replace all \r\n by \n, and unfold the header */
+ $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $header));
+ /* Now we can make a new header array with
+ each element representing a headerline */
+ $aHdr = explode("\n" , $hdr);
+ $aReceived = array();
+ foreach ($aHdr 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 'date':
+ $aMsg['date'] = trim(str_replace(' ', ' ', $value));
+ break;
+ case 'x-priority': $aMsg['x-priority'] = ($value) ? (int) $value{0} : 3; break;
+ case 'priority':
+ case 'importance':
+ if (!isset($aMsg['x-priority'])) {
+ $aPrio = split('/\w/',trim($value));
+ $sPrio = strtolower(array_shift($aPrio));
+ if (is_numeric($sPrio)) {
+ $iPrio = (int) $sPrio;
+ } elseif ( $sPrio == 'non-urgent' || $sPrio == 'low' ) {
+ $iPrio = 3;
+ } elseif ( $sPrio == 'urgent' || $sPrio == 'high' ) {
+ $iPrio = 1;
+ } else {
+ // default is normal priority
+ $iPrio = 3;
+ }
+ $aMsg['x-priority'] = $iPrio;
+ }
+ break;
+ case 'content-type':
+ $type = $value;
+ if ($pos = strpos($type, ";")) {
+ $type = substr($type, 0, $pos);
+ }
+ $type = explode("/", $type);
+ if(!is_array($type) || count($type) < 2) {
+ $aMsg['content-type'] = array('text','plain');
+ } else {
+ $aMsg['content-type'] = array(strtolower($type[0]),strtolower($type[1]));
+ }
+ break;
+ case 'received':
+ $aMsg['received'][] = $value;
+ break;
+ default:
+ $aMsg[$field] = $value;
+ break;
+ }
+ }