Example #1
0
 public function getEncoding()
 {
     if (!$this->encoding) {
         $this->encoding = Mime::isPrintable($this->getFieldValue(HeaderInterface::FORMAT_RAW)) ? 'ASCII' : 'UTF-8';
     }
     return $this->encoding;
 }
Example #2
0
 public function getEncoding()
 {
     if (!$this->encoding) {
         $this->encoding = Mime::isPrintable($this->subject) ? 'ASCII' : 'UTF-8';
     }
     return $this->encoding;
 }
 public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
 {
     $prepared = $this->type;
     if (empty($this->parameters)) {
         return $prepared;
     }
     $values = [$prepared];
     foreach ($this->parameters as $attribute => $value) {
         if (HeaderInterface::FORMAT_ENCODED === $format && !Mime::isPrintable($value)) {
             $this->encoding = 'UTF-8';
             $value = HeaderWrap::wrap($value, $this);
             $this->encoding = 'ASCII';
         }
         $values[] = sprintf('%s="%s"', $attribute, $value);
     }
     return implode(';' . Headers::FOLDING, $values);
 }
Example #4
0
 /**
  * Encode header fields
  *
  * Encodes header content according to RFC1522 if it contains non-printable
  * characters.
  *
  * @param  string $value
  * @return string
  */
 protected function _encodeHeader($value)
 {
     if (Mime\Mime::isPrintable($value) === false) {
         if ($this->getHeaderEncoding() === Mime\Mime::ENCODING_QUOTEDPRINTABLE) {
             $value = Mime\Mime::encodeQuotedPrintableHeader($value, $this->getCharset(), Mime\Mime::LINELENGTH, Mime\Mime::LINEEND);
         } else {
             $value = Mime\Mime::encodeBase64Header($value, $this->getCharset(), Mime\Mime::LINELENGTH, Mime\Mime::LINEEND);
         }
     }
     return $value;
 }
Example #5
0
    /**
     * Assertion that checks if a given mailing header string is RFC conform.
     *
     * @param  string $header
     * @return void
     */
    protected function assertMailHeaderConformsToRfc($header)
    {
        $this->numAssertions++;
        $parts = explode(Mime\Mime::LINEEND, $header);
        if(count($parts) > 0) {
            for($i = 0; $i < count($parts); $i++) {
                if(preg_match('/(=?[a-z0-9-_]+\?[q|b]{1}\?)/i', $parts[$i], $matches)) {
                    $dce = sprintf("=?%s", $matches[0]);
                    // Check that Delimiter, Charset, Encoding are at the front of the string
                    if(substr(trim($parts[$i]), 0, strlen($dce)) != $dce) {
                        $this->fail(sprintf(
                            "Header-Part '%s' in line '%d' has missing or malformated delimiter, charset, encoding information.",
                            $parts[$i],
                            $i+1
                        ));
                    }
                    // check that the encoded word is not too long.);
                    // this is only some kind of suggestion by the standard, in PHP its hard to hold it, so we do not enforce it here.
                    /*if(strlen($parts[$i]) > 75) {
                        $this->fail(sprintf(
                            "Each encoded-word is only allowed to be 75 chars long, but line %d is %s chars long: %s",
                            $i+1,
                            strlen($parts[$i]),
                            $parts[$i]
                        ));
                    }*/
                    // Check that the end-delmiter ?= is correctly placed
                    if(substr(trim($parts[$i]), -2, 2) != "?=") {
                        $this->fail(sprintf(
                            "Lines with an encoded-word have to end in ?=, but line %d does not: %s",
                            $i+1,
                            substr(trim($parts[$i]), -2, 2)
                        ));
                    }

                    // Check that only one encoded-word can be found per line.
                    if(substr_count($parts[$i], "=?") != 1) {
                        $this->fail(sprintf(
                            "Only one encoded-word is allowed per line in the header. It seems line %d contains more: %s",
                            $i+1,
                            $parts[$i]
                        ));
                    }

                    // Check that the encoded-text only contains US-ASCII chars, and no space
                    $encodedText = substr(trim($parts[$i]), strlen($dce), -2);
                    if(preg_match('/([\s]+)/', $encodedText)) {
                        $this->fail(sprintf(
                            "No whitespace characters allowed in encoded-text of line %d: %s",
                            $i+1,
                            $parts[$i]
                        ));
                    }
                    for($i = 0; $i < strlen($encodedText); $i++) {
                        if(ord($encodedText[$i]) > 127) {
                            $this->fail(sprintf(
                                "No non US-ASCII characters allowed, but line %d has them: %s",
                                 $i+1,
                                 $parts[$i]
                            ));
                        }
                    }
                } else if(Mime\Mime::isPrintable($parts[$i]) == false) {
                    $this->fail(sprintf(
                        "Encoded-word in line %d contains non printable characters.",
                        $i+1
                    ));
                }
            }
        }
    }
Example #6
0
 public function testIsPrintable_isPrintable()
 {
     $this->assertTrue(Mime\Mime::isPrintable('Test without special chars'));
 }