Example #1
0
 /**
  * Access headers collection
  *
  * Lazy-loads if not already attached.
  *
  * @return Headers
  */
 public function getHeaders()
 {
     if (null === $this->headers) {
         $this->setHeaders(new Headers());
         $date = Header\Date::fromString('Date: ' . date('r'));
         $this->headers->addHeader($date);
     }
     return $this->headers;
 }
Example #2
0
 /**
  * split a message in header and body part, if no header or an
  * invalid header is found $headers is empty
  *
  * The charset of the returned headers depend on your iconv settings.
  *
  * @param  string|Headers  $message raw message with header and optional content
  * @param  Headers         $headers output param, headers container
  * @param  string          $body    output param, content of message
  * @param  string          $EOL EOL string; defaults to {@link Zend\Mime\Mime::LINEEND}
  * @param  bool         $strict  enable strict mode for parsing message
  * @return null
  */
 public static function splitMessage($message, &$headers, &$body, $EOL = Mime::LINEEND, $strict = false)
 {
     if ($message instanceof Headers) {
         $message = $message->toString();
     }
     // check for valid header at first line
     $firstline = strtok($message, "\n");
     if (!preg_match('%^[^\\s]+[^:]*:%', $firstline)) {
         $headers = array();
         // TODO: we're ignoring \r for now - is this function fast enough and is it safe to assume noone needs \r?
         $body = str_replace(array("\r", "\n"), array('', $EOL), $message);
         return;
     }
     // see @ZF2-372, pops the first line off a message if it doesn't contain a header
     if (!$strict) {
         $parts = explode(':', $firstline, 2);
         if (count($parts) != 2) {
             $message = substr($message, strpos($message, $EOL) + 1);
         }
     }
     // find an empty line between headers and body
     // default is set new line
     if (strpos($message, $EOL . $EOL)) {
         list($headers, $body) = explode($EOL . $EOL, $message, 2);
         // next is the standard new line
     } elseif ($EOL != "\r\n" && strpos($message, "\r\n\r\n")) {
         list($headers, $body) = explode("\r\n\r\n", $message, 2);
         // next is the other "standard" new line
     } elseif ($EOL != "\n" && strpos($message, "\n\n")) {
         list($headers, $body) = explode("\n\n", $message, 2);
         // at last resort find anything that looks like a new line
     } else {
         ErrorHandler::start(E_NOTICE | E_WARNING);
         list($headers, $body) = preg_split("%([\r\n]+)\\1%U", $message, 2);
         ErrorHandler::stop();
     }
     $headers = Headers::fromString($headers, $EOL);
 }
Example #3
0
 /**
  * Public constructor
  *
  * This handler supports the following params:
  * - file     filename or open file handler with message content (required)
  * - startPos start position of message or part in file (default: current position)
  * - endPos   end position of message or part in file (default: end of file)
  *
  * @param   array $params  full message with or without headers
  * @throws Exception\RuntimeException
  * @throws Exception\InvalidArgumentException
  */
 public function __construct(array $params)
 {
     if (empty($params['file'])) {
         throw new Exception\InvalidArgumentException('no file given in params');
     }
     if (!is_resource($params['file'])) {
         $this->fh = fopen($params['file'], 'r');
     } else {
         $this->fh = $params['file'];
     }
     if (!$this->fh) {
         throw new Exception\RuntimeException('could not open file');
     }
     if (isset($params['startPos'])) {
         fseek($this->fh, $params['startPos']);
     }
     $header = '';
     $endPos = isset($params['endPos']) ? $params['endPos'] : null;
     while (($endPos === null || ftell($this->fh) < $endPos) && trim($line = fgets($this->fh))) {
         $header .= $line;
     }
     $this->headers = Headers::fromString($header);
     $this->contentPos[0] = ftell($this->fh);
     if ($endPos !== null) {
         $this->contentPos[1] = $endPos;
     } else {
         fseek($this->fh, 0, SEEK_END);
         $this->contentPos[1] = ftell($this->fh);
     }
     if (!$this->isMultipart()) {
         return;
     }
     $boundary = $this->getHeaderField('content-type', 'boundary');
     if (!$boundary) {
         throw new Exception\RuntimeException('no boundary found in content type to split message');
     }
     $part = array();
     $pos = $this->contentPos[0];
     fseek($this->fh, $pos);
     while (!feof($this->fh) && ($endPos === null || $pos < $endPos)) {
         $line = fgets($this->fh);
         if ($line === false) {
             if (feof($this->fh)) {
                 break;
             }
             throw new Exception\RuntimeException('error reading file');
         }
         $lastPos = $pos;
         $pos = ftell($this->fh);
         $line = trim($line);
         if ($line == '--' . $boundary) {
             if ($part) {
                 // not first part
                 $part[1] = $lastPos;
                 $this->partPos[] = $part;
             }
             $part = array($pos);
         } elseif ($line == '--' . $boundary . '--') {
             $part[1] = $lastPos;
             $this->partPos[] = $part;
             break;
         }
     }
     $this->countParts = count($this->partPos);
 }
Example #4
0
 /**
  * Access headers collection
  *
  * Lazy-loads if not already attached.
  *
  * @return Headers
  */
 public function getHeaders()
 {
     if (null === $this->headers) {
         if ($this->mail) {
             $part = $this->mail->getRawHeader($this->messageNum);
             $this->headers = Headers::fromString($part);
         } else {
             $this->headers = new Headers();
         }
     }
     return $this->headers;
 }