PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/jvit/library/Zend/Filter/HtmlEntities.php

https://github.com/juanjack/JUANJACK
PHP | 216 lines | 89 code | 20 blank | 107 comment | 12 complexity | b0f237026c2d646425186228d098388f MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Filter
  17. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: HtmlEntities.php 24011 2011-05-04 18:56:38Z matthew $
  20. */
  21. /**
  22. * @see Zend_Filter_Interface
  23. */
  24. require_once 'Zend/Filter/Interface.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Filter
  28. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Filter_HtmlEntities implements Zend_Filter_Interface
  32. {
  33. /**
  34. * Corresponds to the second htmlentities() argument
  35. *
  36. * @var integer
  37. */
  38. protected $_quoteStyle;
  39. /**
  40. * Corresponds to the third htmlentities() argument
  41. *
  42. * @var string
  43. */
  44. protected $_encoding;
  45. /**
  46. * Corresponds to the forth htmlentities() argument
  47. *
  48. * @var unknown_type
  49. */
  50. protected $_doubleQuote;
  51. /**
  52. * Sets filter options
  53. *
  54. * @param integer|array $quoteStyle
  55. * @param string $charSet
  56. * @return void
  57. */
  58. public function __construct($options = array())
  59. {
  60. if ($options instanceof Zend_Config) {
  61. $options = $options->toArray();
  62. } else if (!is_array($options)) {
  63. $options = func_get_args();
  64. $temp['quotestyle'] = array_shift($options);
  65. if (!empty($options)) {
  66. $temp['charset'] = array_shift($options);
  67. }
  68. $options = $temp;
  69. }
  70. if (!isset($options['quotestyle'])) {
  71. $options['quotestyle'] = ENT_COMPAT;
  72. }
  73. if (!isset($options['encoding'])) {
  74. $options['encoding'] = 'UTF-8';
  75. }
  76. if (isset($options['charset'])) {
  77. $options['encoding'] = $options['charset'];
  78. }
  79. if (!isset($options['doublequote'])) {
  80. $options['doublequote'] = true;
  81. }
  82. $this->setQuoteStyle($options['quotestyle']);
  83. $this->setEncoding($options['encoding']);
  84. $this->setDoubleQuote($options['doublequote']);
  85. }
  86. /**
  87. * Returns the quoteStyle option
  88. *
  89. * @return integer
  90. */
  91. public function getQuoteStyle()
  92. {
  93. return $this->_quoteStyle;
  94. }
  95. /**
  96. * Sets the quoteStyle option
  97. *
  98. * @param integer $quoteStyle
  99. * @return Zend_Filter_HtmlEntities Provides a fluent interface
  100. */
  101. public function setQuoteStyle($quoteStyle)
  102. {
  103. $this->_quoteStyle = $quoteStyle;
  104. return $this;
  105. }
  106. /**
  107. * Get encoding
  108. *
  109. * @return string
  110. */
  111. public function getEncoding()
  112. {
  113. return $this->_encoding;
  114. }
  115. /**
  116. * Set encoding
  117. *
  118. * @param string $value
  119. * @return Zend_Filter_HtmlEntities
  120. */
  121. public function setEncoding($value)
  122. {
  123. $this->_encoding = (string) $value;
  124. return $this;
  125. }
  126. /**
  127. * Returns the charSet option
  128. *
  129. * Proxies to {@link getEncoding()}
  130. *
  131. * @return string
  132. */
  133. public function getCharSet()
  134. {
  135. return $this->getEncoding();
  136. }
  137. /**
  138. * Sets the charSet option
  139. *
  140. * Proxies to {@link setEncoding()}
  141. *
  142. * @param string $charSet
  143. * @return Zend_Filter_HtmlEntities Provides a fluent interface
  144. */
  145. public function setCharSet($charSet)
  146. {
  147. return $this->setEncoding($charSet);
  148. }
  149. /**
  150. * Returns the doubleQuote option
  151. *
  152. * @return boolean
  153. */
  154. public function getDoubleQuote()
  155. {
  156. return $this->_doubleQuote;
  157. }
  158. /**
  159. * Sets the doubleQuote option
  160. *
  161. * @param boolean $doubleQuote
  162. * @return Zend_Filter_HtmlEntities Provides a fluent interface
  163. */
  164. public function setDoubleQuote($doubleQuote)
  165. {
  166. $this->_doubleQuote = (boolean) $doubleQuote;
  167. return $this;
  168. }
  169. /**
  170. * Defined by Zend_Filter_Interface
  171. *
  172. * Returns the string $value, converting characters to their corresponding HTML entity
  173. * equivalents where they exist
  174. *
  175. * @param string $value
  176. * @return string
  177. */
  178. public function filter($value)
  179. {
  180. $filtered = htmlentities((string) $value, $this->getQuoteStyle(), $this->getEncoding(), $this->getDoubleQuote());
  181. if (strlen((string) $value) && !strlen($filtered)) {
  182. if (!function_exists('iconv')) {
  183. require_once 'Zend/Filter/Exception.php';
  184. throw new Zend_Filter_Exception('Encoding mismatch has resulted in htmlentities errors');
  185. }
  186. $enc = $this->getEncoding();
  187. $value = iconv('', $enc . '//IGNORE', (string) $value);
  188. $filtered = htmlentities($value, $this->getQuoteStyle(), $enc, $this->getDoubleQuote());
  189. if (!strlen($filtered)) {
  190. require_once 'Zend/Filter/Exception.php';
  191. throw new Zend_Filter_Exception('Encoding mismatch has resulted in htmlentities errors');
  192. }
  193. }
  194. return $filtered;
  195. }
  196. }