Example #1
0
 /**
  * Parse it as a MIME message
  *
  * @param int $id message id
  * @param string $folder
  * @param string [$nopersonalities]
  * @param string [$cache]
  *
  * @access public
  */
 function reademail($id, $folder, $nopersonalities = null, $cache = null, $path = null)
 {
     global $atmail, $domains, $pref;
     // Load our temporary filename
     $this->tmpdir = "{$pref['user_dir']}/tmp/" . $this->Username . '@' . $this->Pop3host . "/";
     // Create our temporary directory, if its missing, init in Global.php
     if (!is_dir($this->tmpdir)) {
         mkdir($this->tmpdir, 0777);
     }
     if (!$this->File && $cache) {
         $this->File = GetMail::check_cache($atmail->tmpdir . "/{$this->SessionID}-{$cache}.data");
         // Set to blank if the cache no longer exists
         //if(!file_exists($this->File))
         //	$this->File = '';
     }
     // Read our email from the server
     if (!is_string($this->File)) {
         if (!$nopersonalities && isset($atmail)) {
             $this->FromField = $atmail->loadpersonalities();
         }
         $status = $this->mail->login();
         // We have an error while logging in. Tell the user
         if ($status) {
             $this->status = $status;
             return;
         }
         if (is_null($path)) {
             $this->path = $this->mail->get($id, $folder, '', $cache);
         } else {
             $this->path = $path;
         }
         $this->mail->quit();
         if ($this->Type == 'imap') {
             $this->MessageState = $this->mail->MessageState;
         }
         // User has specified the msg from the spellchecker
     } else {
         if (file_exists($atmail->tmpdir . "/{$this->File}")) {
             $this->path = $atmail->tmpdir . "/{$this->File}";
             $this->MessageState = 'o';
         } else {
             if (file_exists($this->File)) {
                 $this->path = $this->File;
                 $this->MessageState = 'o';
             }
         }
     }
     // Just to be sure nobody is reading below a directory
     $this->path = str_replace('../', '', $this->path);
     $this->parser = new MailParser();
     if (!is_dir($this->tmpdir)) {
         mkdir($this->tmpdir, 0777);
     }
     // Users have their own mime-tmp directory
     if (!$this->parser->output_dir($this->tmpdir)) {
         catcherror("Could not parse message to temp directory '{$this->tmpdir}' -\n\t        Check the directory exists with permissions to write");
     }
     if (!$this->parser->parse_open($this->path)) {
         $this->txt = array_pop($this->parser->errors);
         return;
     }
     if ($this->rawemail) {
         $this->rawbody = $this->parser->stringify();
         $this->rawbody = str_replace("\r/", "\r\n", $this->rawbody);
         return;
     }
     if ($this->head) {
         $this->headers = $this->parser->stringify_header();
         $this->headers = str_replace(array('<', '>'), array('&lt;', '&gt;'), $this->headers);
     }
     $this->Charset = $this->parser->get_charset();
     // Get any mail headers
     $this->from = $this->parser->get_header_field('From');
     $this->replyto = $this->parser->get_header_field('Reply-To');
     if (preg_match('/(.*?)<.*?>/', $this->from, $match)) {
         $this->username = $match[1];
     }
     // Default to our previous subject in msg, if it does not exist. e.g , reading
     // a msg with multiple attachments
     $this->subject = $this->parser->get_header_field('Subject');
     if ($this->subject == '') {
         $this->subject = 'No Subject';
     }
     $this->cc = $this->parser->get_header_field('CC');
     $this->bcc = $this->parser->get_header_field('BCC');
     $this->to = $this->parser->get_header_field('To');
     $this->VideoMail = $this->quote_header($this->parser->get_header_field('X-VideoMail'));
     if ($this->VideoMail) {
         $this->VideoMail .= "/mini";
     }
     // Quote any ISO headers
     foreach (array('from', 'replyto', 'subject', 'to', 'cc', 'bcc') as $field) {
         // Store the encoding of the email-message
         if (preg_match('/\\s*=\\?([^\\?]+)\\?([QqBb])/', $this->{$field}, $match)) {
             $this->Encoding = $match[1];
             if (strtoupper($match[2]) == 'Q') {
                 $this->{$field} = preg_replace('/\\s*=\\?([^\\?]+)\\?[Qq]\\?([^\\?]+)?\\?=/e', "\$this->mail->decode_language('\\1', GetMail::decode_mime_head('\\1', stripslashes('\\2')))", $this->{$field});
             } else {
                 $this->{$field} = preg_replace('/\\s*=\\?([^\\?]+)\\?[Bb]\\?([^\\?]+)?\\?=/e', "\$this->mail->decode_language('\\1', base64_decode('\\2'))", $this->{$field});
             }
         } else {
             $this->{$field} = $this->mail->decode_language($this->Charset, $this->{$field});
         }
     }
     $this->date = $this->parser->get_header_field('Date');
     $date = $this->date ? $this->date : 'today';
     $date = $this->mail->calc_timezone($date);
     $time = strtotime($date);
     if ($this->Language == "japanese") {
         setlocale(LC_TIME, 'ja_JP.UTF-8', 'en_US');
         $this->date = strftime("{$this->mail->DateFormat} %a {$this->mail->TimeFormat}", $time);
     } else {
         setlocale(LC_TIME, strtolower($this->Language), 'en_US');
         $this->date = strftime("%a " . $this->mail->DateFormat . " " . $this->mail->TimeFormat, $time);
         $this->date = iconv('iso-8859-1', "UTF-8", $this->date);
     }
     // Take away the timezone and seconds
     $this->date = preg_replace('/:\\d\\d \\+?-?\\d{4}.*/', '', $this->date);
     $this->ctype = $this->parser->get_header_field('Content-Type');
     list($this->mimetype) = explode(';', $this->ctype);
     $this->mimetype = strtolower(trim($this->mimetype));
     if ($this->mimetype == 'text/html') {
         $this->type = 'HTML Msg';
     } elseif ($this->mimetype == 'multipart/alternative') {
         $this->type = 'Embeded HTML/Text';
     } elseif (strpos($this->mimetype, 'multipart') !== false) {
         $this->type = 'Attachments';
     } else {
         $this->type = 'Text';
     }
     // If we are using the maildir format, the message-id number if the unique id
     if ($this->mail->Type == 'file' && $domains[$this->Pop3host]) {
         $this->UIDL = $id;
         $this->UIDL = preg_replace('/cur\\/|new\\//', '', $this->UIDL);
     } else {
         if (!($this->UIDL = $this->parser->get_header_field('x-uidl'))) {
             $this->UIDL = $this->parser->get_header_field('message-id');
         }
         // Make the UIDL header from the Subject/Date if the Message-ID or XUIDL does not exist
         if (!$this->UIDL) {
             $this->UIDL = md5($this->subject . $this->parser->get_header_field('date'));
         }
     }
     // Take away illegal characters from the UIDL
     $this->UIDL = str_replace("'", '"', $this->UIDL);
     $this->UIDL = str_replace('"', '', $this->UIDL);
     $this->UIDL = preg_replace('/:.*/', '', $this->UIDL);
     $this->UIDL = str_replace(array("\n", "\r", ' ', ':', '+', '<', '>', '*', '|', '\\', '/', '&gt;', '&lt;'), '', $this->UIDL);
     if ($this->Type == 'pop3' || $this->Type == 'imap') {
         $this->EmailCache = $this->UIDL;
     }
     // Take away any newlines from the UIDL
     $this->UIDL = trim($this->UIDL);
     // Set the email priority as Normal, otherwise find the value in the header(s)
     $this->priority = 'Normal';
     if (substr($this->parser->get_header_field('x-priority'), 0, 1) == 1 || $this->parser->get_header_field('X-MSMail-Priority') == 'High' || $this->parser->get_header_field('Importance') == 'High') {
         $this->priority = 'High';
     }
     if (substr($this->parser->get_header_field('x-priority'), 0, 1) == 5 || $this->parser->get_header_field('X-MSMail-Priority') == 'Low' || $this->parser->get_header_field('Importance') == 'Low') {
         $this->priority = 'Low';
     }
     if (preg_match("/{$this->emailexp}/", $this->from, $match)) {
         $this->emailfrom = $match[1];
     }
     $this->emailfrom = str_replace(array('&gt;', '&lt;'), '', $this->emailfrom);
     // Cleaup the email, take away " signs, which close the HTML input tag
     //$this->to = preg_replace('/"(.*?),(.*?)"/', '$1 $2', $this->to);
     //$this->cc = preg_replace('/"(.*?),(.*?)"/', '$1 $2', $this->cc);
     //$this->to = str_replace('"', "'", $this->to);
     //$this->cc = str_replace('"', "'", $this->cc);
     //$this->bcc = str_replace('"', "'", $this->bcc);
     //$this->ctype = $this->parser->get_header_field('Content-Type');
     // See if we are permitted to display images in messages
     if (isset($atmail)) {
         $this->DisplayImages = $atmail->load_displayimages();
         if ($atmail->DisplayImages == '2') {
             $atmail->DisplayImages = $atmail->load_abook_emails($this->emailfrom);
         }
     }
     $this->dump_entity();
     if (isset($this->multiparttxt) && !empty($this->multiparttxt)) {
         $this->multiparttxt = $atmail->escape_jscript($this->multiparttxt);
     }
     if (isset($this->html) && !empty($this->html)) {
         $this->html = $atmail->escape_jscript($this->html);
         if (isset($this->multiparttxt)) {
             $this->html .= $this->multiparttxt;
         }
     }
     if (isset($this->txt) && !empty($this->txt)) {
         $this->txt = $atmail->escape_jscript($this->txt);
         if (isset($this->multiparttxt)) {
             $this->txt .= $this->multiparttxt;
         }
     }
     $this->scan_inline();
     // Fix an error where certain messages cannot be displayed ( e.g Apple mailers as multipart msgs )
     if (!$this->html && !$this->txt) {
         $this->txt = $this->multiparttxt;
     }
 }