コード例 #1
0
 /**
  * Parse string into its InternetAddresses.
  *
  * <code>
  *   $p= new InternetAddressParser();
  *   try {
  *     $addr= $p->parse('"Kiesel, Alex" <*****@*****.**>, Christian Lang <*****@*****.**>');
  *   } catch(FormatException $e)) {
  *     $e->printStackTrace();
  *   }
  *   
  *   foreach (array_keys($addr) as $idx) { Console::writeLine($addr[$idx]->toString()); }
  *
  * </code>
  *
  * @return  InternetAddress[]
  * @throws  lang.FormatException in case the string is malformed
  */
 public function parse($str)
 {
     $result = array();
     $st = new StringTokenizer($str, ',');
     $st->hasMoreTokens() && ($tok = $st->nextToken());
     while ($tok) {
         // No " in this string, so this contains one address
         if (FALSE === ($pos = strpos($tok, '"'))) {
             $result[] = InternetAddress::fromString($tok);
             $tok = $st->nextToken();
             continue;
         }
         // When having at least one double-quote, we have to make sure, the address delimiter ','
         // is not inside the quotes. If so, search the next delimiter and perform this check again.
         // Additionally, inside a quote, the quote delimiter may be quoted with \ itself. Catch
         // that case as well.
         $inquot = 0;
         for ($i = 0; $i < strlen($tok); $i++) {
             if ($tok[$i] == '"' && (!$inquot || ($i == 0 || $tok[$i - 1] != '\\'))) {
                 $inquot = 1 - $inquot;
             }
         }
         if ($inquot) {
             if (!$st->hasMoreTokens()) {
                 throw new FormatException('Cannot parse string: no ending delimiter found.');
             }
             $tok = $tok . ',' . $st->nextToken();
             continue;
         }
         $result[] = InternetAddress::fromString($tok);
         // Handle next token
         $tok = $st->nextToken();
     }
     return $result;
 }
コード例 #2
0
 public function testFromString()
 {
     $strings = array('Alex Kiesel <*****@*****.**>', 'kiesel@example.com (Alex Kiesel)', '"Alex Kiesel" <*****@*****.**>', '*****@*****.**', '=?iso-8859-1?Q?Alex_Kiesel?= <*****@*****.**>');
     foreach ($strings as $string) {
         $address = InternetAddress::fromString($string);
         $this->assertEquals('kiesel', $address->localpart);
         $this->assertEquals('example.com', $address->domain);
     }
 }
コード例 #3
0
 /**
  * Cast a given value
  *
  * @see     xp://scriptlet.xml.workflow.casters.ParamCaster
  * @param   array value
  * @return  array value
  */
 public function castValue($value)
 {
     $return = array();
     foreach ($value as $k => $v) {
         try {
             $addr = InternetAddress::fromString($v);
         } catch (FormatException $e) {
             return $e->getMessage();
         }
         $return[$k] = $addr;
     }
     return $return;
 }
コード例 #4
0
 /**
  * Set headers from string
  *
  * @param   string str
  */
 public function setHeaderString($str)
 {
     $this->subject = $this->contenttype = '';
     $t = strtok($str, "\n\r");
     while ($t) {
         if ("\t" != $t[0] && ' ' != $t[0]) {
             @(list($k, $t) = explode(': ', $t, 2));
         }
         switch (ucfirst($k)) {
             case HEADER_FROM:
                 try {
                     if ('' != trim($t)) {
                         $this->setFrom(InternetAddress::fromString($t));
                     }
                 } catch (FormatException $e) {
                     $this->setFrom(new InternetAddress(array(NULL, NULL), $t));
                     // Fall through
                 }
                 break;
             case HEADER_TO:
             case HEADER_CC:
             case HEADER_BCC:
                 try {
                     $k = strtolower($k);
                     if ('' != trim($t)) {
                         $this->addRecipient($k, InternetAddress::fromString($t));
                     }
                 } catch (FormatException $e) {
                     $this->addRecipient($k, new InternetAddress(array(NULL, NULL), $t));
                     // Fall through
                 }
                 break;
             case HEADER_MIMEVER:
                 $this->mimever = $t;
                 break;
             case HEADER_SUBJECT:
                 $this->subject .= $t;
                 break;
             case HEADER_CONTENTTYPE:
                 $this->contenttype .= $t;
                 break;
             case HEADER_ENCODING:
                 $this->encoding = $t;
                 break;
             case HEADER_DATE:
                 $this->setDate($t);
                 break;
             case HEADER_PRIORITY:
                 $this->priority = (int) $t;
                 break;
             case HEADER_MESSAGEID:
                 $this->message_id = $t;
                 break;
             default:
                 $this->_setHeader($k, $t, "\n");
                 break;
         }
         $t = strtok("\n\r");
     }
 }
コード例 #5
0
 /**
  * Set headers from string
  *
  * @param   string str
  */
 public function setHeaderString($str)
 {
     $this->subject = $this->contenttype = '';
     $t = strtok($str, "\n\r");
     while ($t) {
         if ("\t" === $t[0] || ' ' === $t[0]) {
             $value = substr($t, 1);
         } else {
             $value = NULL;
             sscanf($t, "%[^:]: %[^\r]", $k, $value);
         }
         switch (strtolower($k)) {
             case 'from':
                 if ('' === $value) {
                     break;
                 }
                 try {
                     $this->setFrom(InternetAddress::fromString($value));
                 } catch (FormatException $e) {
                     $this->setFrom(new InternetAddress(array(NULL, NULL), $value));
                 }
                 break;
             case 'to':
             case 'cc':
             case 'bcc':
                 if ('' === $value) {
                     break;
                 }
                 $k = strtolower($k);
                 $offset = 0;
                 do {
                     if ('"' === $value[$offset]) {
                         $quote = strpos($value, '"', $offset + 1);
                         $span = strcspn($value, ',', $offset + $quote) + $quote;
                     } else {
                         $span = strcspn($value, ',', $offset);
                     }
                     $recipient = substr($value, $offset, $span);
                     try {
                         $this->addRecipient($k, InternetAddress::fromString($recipient));
                     } catch (FormatException $e) {
                         $this->addRecipient($k, new InternetAddress(array(NULL, NULL), $value));
                     }
                     $offset += $span + strspn($value, ', ', $offset + $span);
                 } while ($offset < strlen($value));
                 break;
             case 'mime-version':
                 $this->mimever = $value;
                 break;
             case 'subject':
                 $this->subject .= ($this->subject ? ' ' : '') . $this->decode($value);
                 break;
             case 'content-type':
                 $this->contenttype .= $value;
                 break;
             case 'content-transfer-encoding':
                 $this->encoding = $value;
                 break;
             case 'date':
                 $this->setDate($value);
                 break;
             case 'x-priority':
                 $this->priority = (int) $value;
                 break;
             case 'message-id':
                 $this->message_id = $value;
                 break;
             default:
                 $this->_setHeader($k, $this->decode($value), ' ');
                 break;
         }
         $t = strtok("\n\r");
     }
 }