File indexing completed on 2024-05-12 06:02:33

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_Filter
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  * @see Zend_Filter_Interface
0024  */
0025 // require_once 'Zend/Filter/Interface.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Filter
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Filter_HtmlEntities implements Zend_Filter_Interface
0034 {
0035     /**
0036      * Corresponds to the second htmlentities() argument
0037      *
0038      * @var integer
0039      */
0040     protected $_quoteStyle;
0041 
0042     /**
0043      * Corresponds to the third htmlentities() argument
0044      *
0045      * @var string
0046      */
0047     protected $_encoding;
0048 
0049     /**
0050      * Corresponds to the forth htmlentities() argument
0051      *
0052      * @var unknown_type
0053      */
0054     protected $_doubleQuote;
0055 
0056     /**
0057      * Sets filter options
0058      *
0059      * @param  integer|array $quoteStyle
0060      * @param  string  $charSet
0061      * @return void
0062      */
0063     public function __construct($options = array())
0064     {
0065         if ($options instanceof Zend_Config) {
0066             $options = $options->toArray();
0067         } else if (!is_array($options)) {
0068             $options = func_get_args();
0069             $temp['quotestyle'] = array_shift($options);
0070             if (!empty($options)) {
0071                 $temp['charset'] = array_shift($options);
0072             }
0073 
0074             $options = $temp;
0075         }
0076 
0077         if (!isset($options['quotestyle'])) {
0078             $options['quotestyle'] = ENT_COMPAT;
0079         }
0080 
0081         if (!isset($options['encoding'])) {
0082             $options['encoding'] = 'UTF-8';
0083         }
0084         if (isset($options['charset'])) {
0085             $options['encoding'] = $options['charset'];
0086         }
0087 
0088         if (!isset($options['doublequote'])) {
0089             $options['doublequote'] = true;
0090         }
0091 
0092         $this->setQuoteStyle($options['quotestyle']);
0093         $this->setEncoding($options['encoding']);
0094         $this->setDoubleQuote($options['doublequote']);
0095     }
0096 
0097     /**
0098      * Returns the quoteStyle option
0099      *
0100      * @return integer
0101      */
0102     public function getQuoteStyle()
0103     {
0104         return $this->_quoteStyle;
0105     }
0106 
0107     /**
0108      * Sets the quoteStyle option
0109      *
0110      * @param  integer $quoteStyle
0111      * @return Zend_Filter_HtmlEntities Provides a fluent interface
0112      */
0113     public function setQuoteStyle($quoteStyle)
0114     {
0115         $this->_quoteStyle = $quoteStyle;
0116         return $this;
0117     }
0118 
0119 
0120     /**
0121      * Get encoding
0122      *
0123      * @return string
0124      */
0125     public function getEncoding()
0126     {
0127          return $this->_encoding;
0128     }
0129 
0130     /**
0131      * Set encoding
0132      *
0133      * @param  string $value
0134      * @return Zend_Filter_HtmlEntities
0135      */
0136     public function setEncoding($value)
0137     {
0138         $this->_encoding = (string) $value;
0139         return $this;
0140     }
0141 
0142     /**
0143      * Returns the charSet option
0144      *
0145      * Proxies to {@link getEncoding()}
0146      *
0147      * @return string
0148      */
0149     public function getCharSet()
0150     {
0151         return $this->getEncoding();
0152     }
0153 
0154     /**
0155      * Sets the charSet option
0156      *
0157      * Proxies to {@link setEncoding()}
0158      *
0159      * @param  string $charSet
0160      * @return Zend_Filter_HtmlEntities Provides a fluent interface
0161      */
0162     public function setCharSet($charSet)
0163     {
0164         return $this->setEncoding($charSet);
0165     }
0166 
0167     /**
0168      * Returns the doubleQuote option
0169      *
0170      * @return boolean
0171      */
0172     public function getDoubleQuote()
0173     {
0174         return $this->_doubleQuote;
0175     }
0176 
0177     /**
0178      * Sets the doubleQuote option
0179      *
0180      * @param boolean $doubleQuote
0181      * @return Zend_Filter_HtmlEntities Provides a fluent interface
0182      */
0183     public function setDoubleQuote($doubleQuote)
0184     {
0185         $this->_doubleQuote = (boolean) $doubleQuote;
0186         return $this;
0187     }
0188 
0189     /**
0190      * Defined by Zend_Filter_Interface
0191      *
0192      * Returns the string $value, converting characters to their corresponding HTML entity
0193      * equivalents where they exist
0194      *
0195      * @param  string $value
0196      * @return string
0197      */
0198     public function filter($value)
0199     {
0200         $filtered = htmlentities((string) $value, $this->getQuoteStyle(), $this->getEncoding(), $this->getDoubleQuote());
0201         if (strlen((string) $value) && !strlen($filtered)) {
0202             if (!function_exists('iconv')) {
0203                 // require_once 'Zend/Filter/Exception.php';
0204                 throw new Zend_Filter_Exception('Encoding mismatch has resulted in htmlentities errors');
0205             }
0206             $enc      = $this->getEncoding();
0207             $value    = iconv('', $enc . '//IGNORE', (string) $value);
0208             $filtered = htmlentities($value, $this->getQuoteStyle(), $enc, $this->getDoubleQuote());
0209             if (!strlen($filtered)) {
0210                 // require_once 'Zend/Filter/Exception.php';
0211                 throw new Zend_Filter_Exception('Encoding mismatch has resulted in htmlentities errors');
0212             }
0213         }
0214         return $filtered;
0215     }
0216 }