File indexing completed on 2024-12-22 05:36:50

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Mail
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 
0023 /**
0024  * @category   Zend
0025  * @package    Zend_Mail
0026  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0027  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0028  */
0029 final class Zend_Mail_Header_HeaderValue
0030 {
0031     /**
0032      * No public constructor.
0033      */
0034     private function __construct()
0035     {
0036     }
0037 
0038     /**
0039      * Filter the header value according to RFC 2822
0040      *
0041      * @see    http://www.rfc-base.org/txt/rfc-2822.txt (section 2.2)
0042      * @param  string $value
0043      * @return string
0044      */
0045     public static function filter($value)
0046     {
0047         $result = '';
0048         $tot    = strlen($value);
0049 
0050         // Filter for CR and LF characters, leaving CRLF + WSP sequences for
0051         // Long Header Fields (section 2.2.3 of RFC 2822)
0052         for ($i = 0; $i < $tot; $i += 1) {
0053             $ord = ord($value[$i]);
0054             if (($ord < 32 || $ord > 126)
0055                 && $ord !== 13
0056             ) {
0057                 continue;
0058             }
0059 
0060             if ($ord === 13) {
0061                 if ($i + 2 >= $tot) {
0062                     continue;
0063                 }
0064 
0065                 $lf = ord($value[$i + 1]);
0066                 $sp = ord($value[$i + 2]);
0067 
0068                 if ($lf !== 10 || $sp !== 32) {
0069                     continue;
0070                 }
0071 
0072                 $result .= "\r\n ";
0073                 $i += 2;
0074                 continue;
0075             }
0076 
0077             $result .= $value[$i];
0078         }
0079 
0080         return $result;
0081     }
0082 
0083     /**
0084      * Determine if the header value contains any invalid characters.
0085      *
0086      * @see    http://www.rfc-base.org/txt/rfc-2822.txt (section 2.2)
0087      * @param string $value
0088      * @return bool
0089      */
0090     public static function isValid($value)
0091     {
0092         $tot = strlen($value);
0093         for ($i = 0; $i < $tot; $i += 1) {
0094             $ord = ord($value[$i]);
0095             if (($ord < 32 || $ord > 126)
0096                 && $ord !== 13
0097             ) {
0098                 return false;
0099             }
0100 
0101             if ($ord === 13) {
0102                 if ($i + 2 >= $tot) {
0103                     return false;
0104                 }
0105 
0106                 $lf = ord($value[$i + 1]);
0107                 $sp = ord($value[$i + 2]);
0108 
0109                 if ($lf !== 10 || $sp !== 32) {
0110                     return false;
0111                 }
0112 
0113                 $i += 2;
0114             }
0115         }
0116 
0117         return true;
0118     }
0119 
0120     /**
0121      * Assert that the header value is valid.
0122      *
0123      * Raises an exception if invalid.
0124      *
0125      * @param string $value
0126      * @throws Exception\RuntimeException
0127      * @return void
0128      */
0129     public static function assertValid($value)
0130     {
0131         if (! self::isValid($value)) {
0132             // require_once 'Zend/Mail/Exception.php';
0133             throw new Zend_Mail_Exception('Invalid header value detected');
0134         }
0135     }
0136 }