PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Engine/Filter/HtmlSpecialChars.php

https://github.com/shopaholiccompany/shopaholic
PHP | 162 lines | 66 code | 16 blank | 80 comment | 7 complexity | 40a0c8e5a09b9ab71ad8b9a0788436b3 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Engine
  6. * @package Engine_Filter
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: HtmlSpecialChars.php 7244 2010-09-01 01:49:53Z john $
  10. */
  11. /**
  12. * @category Engine
  13. * @package Engine_Filter
  14. * @copyright Copyright 2006-2010 Webligo Developments
  15. * @license http://www.socialengine.net/license/
  16. */
  17. class Engine_Filter_HtmlSpecialChars implements Zend_Filter_Interface
  18. {
  19. /**
  20. * Corresponds to the second htmlentities() argument
  21. *
  22. * @var integer
  23. */
  24. protected $_quoteStyle;
  25. /**
  26. * Corresponds to the third htmlentities() argument
  27. *
  28. * @var string
  29. */
  30. protected $_charSet;
  31. /**
  32. * Corresponds to the forth htmlentities() argument
  33. *
  34. * @var unknown_type
  35. */
  36. protected $_doubleQuote;
  37. /**
  38. * Sets filter options
  39. *
  40. * @param integer|array $quoteStyle
  41. * @param string $charSet
  42. * @return void
  43. */
  44. public function __construct($options = array())
  45. {
  46. if (!is_array($options)) {
  47. trigger_error('Support for multiple arguments is deprecated in favor of a single options array', E_USER_NOTICE);
  48. $options = func_get_args();
  49. $temp['quotestyle'] = array_shift($options);
  50. if (!empty($options)) {
  51. $temp['charset'] = array_shift($options);
  52. }
  53. $options = $temp;
  54. }
  55. if (!isset($options['quotestyle'])) {
  56. $options['quotestyle'] = ENT_COMPAT;
  57. }
  58. if (!isset($options['charset'])) {
  59. $options['charset'] = 'UTF-8'; //'ISO-8859-1';
  60. }
  61. if (!isset($options['doublequote'])) {
  62. $options['doublequote'] = true;
  63. }
  64. $this->setQuoteStyle($options['quotestyle']);
  65. $this->setCharSet($options['charset']);
  66. $this->setDoubleQuote($options['doublequote']);
  67. }
  68. /**
  69. * Returns the quoteStyle option
  70. *
  71. * @return integer
  72. */
  73. public function getQuoteStyle()
  74. {
  75. return $this->_quoteStyle;
  76. }
  77. /**
  78. * Sets the quoteStyle option
  79. *
  80. * @param integer $quoteStyle
  81. * @return Zend_Filter_HtmlEntities Provides a fluent interface
  82. */
  83. public function setQuoteStyle($quoteStyle)
  84. {
  85. $this->_quoteStyle = $quoteStyle;
  86. return $this;
  87. }
  88. /**
  89. * Returns the charSet option
  90. *
  91. * @return string
  92. */
  93. public function getCharSet()
  94. {
  95. return $this->_charSet;
  96. }
  97. /**
  98. * Sets the charSet option
  99. *
  100. * @param string $charSet
  101. * @return Zend_Filter_HtmlEntities Provides a fluent interface
  102. */
  103. public function setCharSet($charSet)
  104. {
  105. $this->_charSet = $charSet;
  106. return $this;
  107. }
  108. /**
  109. * Returns the doubleQuote option
  110. *
  111. * @return boolean
  112. */
  113. public function getDoubleQuote()
  114. {
  115. return $this->_doubleQuote;
  116. }
  117. /**
  118. * Sets the doubleQuote option
  119. *
  120. * @param boolean $doubleQuote
  121. * @return Zend_Filter_HtmlEntities Provides a fluent interface
  122. */
  123. public function setDoubleQuote($doubleQuote)
  124. {
  125. $this->_doubleQuote = (boolean) $doubleQuote;
  126. return $this;
  127. }
  128. /**
  129. * Defined by Zend_Filter_Interface
  130. *
  131. * Returns the string $value, converting characters to their corresponding HTML entity
  132. * equivalents where they exist
  133. *
  134. * @param string $value
  135. * @return string
  136. */
  137. public function filter($value)
  138. {
  139. // Webligo PHP 5.1 compat
  140. if( version_compare(PHP_VERSION, '5.2.3', '<') ) {
  141. return htmlspecialchars((string) $value, $this->_quoteStyle, $this->_charSet);
  142. } else {
  143. return htmlspecialchars((string) $value, $this->_quoteStyle, $this->_charSet, $this->_doubleQuote);
  144. }
  145. }
  146. }