Пример #1
0
 /**
  * Decode header if necessary
  *
  * @param  string $header
  * @return string
  */
 protected function decode($header)
 {
     if (preg_match('/^=\\?([^\\?]+)\\?([QB])\\?([^\\?]+)\\?=$/', $header, $matches)) {
         if ('Q' === $matches[2]) {
             return iconv($matches[1], \xp::ENCODING, QuotedPrintable::decode($matches[3]));
         } else {
             if ('B' === $matches[2]) {
                 return \text\encode\Base64::decode($matches[3]);
             } else {
                 throw new \lang\FormatException('Cannot decode header "' . $header . '"');
             }
         }
     }
     return $header;
 }
Пример #2
0
 /**
  * Create an InternetAddress object from a string
  *
  * Recognizes:
  * <pre>
  *   Timm Friebe <*****@*****.**>
  *   friebe@example.com (Timm Friebe)
  *   "Timm Friebe" <*****@*****.**>
  *   friebe@example.com
  *   <*****@*****.**>
  *   =?iso-8859-1?Q?Timm_Friebe?= <*****@*****.**>
  * </pre>
  *
  * @param   string str
  * @return  peer.mail.InternetAddress address object
  * @throws  lang.FormatException in case the string could not be parsed into an address
  */
 public static function fromString($str)
 {
     static $matches = ['/^=\\?([^\\?])+\\?([QB])\\?([^\\?]+)\\?= <([^ @]+@[0-9a-z.-]+)>$/i' => 3, '/^<?([^ @]+@[0-9a-z.-]+)>?$/i' => 0, '/^"([^"]+)" <([^ @]+@[0-9a-z.-]+)>$/i' => 2, '/^([^<]+) <([^ @]+@[0-9a-z.-]+)>$/i' => 2, '/^([^ @]+@[0-9a-z.-]+) \\(([^\\)]+)\\)$/i' => 1];
     $str = trim(chop($str));
     foreach ($matches as $match => $def) {
         if (!preg_match($match, $str, $_)) {
             continue;
         }
         switch ($def) {
             case 0:
                 $mail = $_[1];
                 $personal = '';
                 break;
             case 1:
                 $mail = $_[1];
                 $personal = $_[2];
                 break;
             case 2:
                 $mail = $_[2];
                 $personal = $_[1];
                 break;
             case 3:
                 $mail = $_[4];
                 switch (strtoupper($_[2])) {
                     case 'Q':
                         $personal = QuotedPrintable::decode($_[3]);
                         break;
                     case 'B':
                         $personal = Base64::decode($_[3]);
                         break;
                 }
                 break;
         }
         break;
     }
     // Was it unparsable?
     if (!isset($mail)) {
         throw new \lang\FormatException('String "' . $str . '" could not be parsed');
     }
     return new InternetAddress($mail, $personal);
 }