CAPTCHA.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4. * Common class for HTML_QuickForm elements to display a CAPTCHA
  5. *
  6. * The HTML_QuickForm_CAPTCHA package adds an element to the
  7. * HTML_QuickForm package to display a CAPTCHA question (image, riddle, etc...)
  8. *
  9. * This package requires the use of a PHP session ($_SESSION).
  10. *
  11. * PHP versions 4 and 5
  12. *
  13. * LICENSE:
  14. *
  15. * Copyright (c) 2006-2008, Philippe Jausions / 11abacus
  16. *
  17. * All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions are met:
  21. *
  22. * - Redistributions of source code must retain the above copyright notice,
  23. * this list of conditions and the following disclaimer.
  24. * - Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. * - Neither the name of 11abacus nor the names of its contributors may
  28. * be used to endorse or promote products derived from this software
  29. * without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  34. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  36. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  37. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  38. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  39. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  40. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  41. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. * @category HTML
  44. * @package HTML_QuickForm_CAPTCHA
  45. * @author Philippe Jausions <Philippe.Jausions@11abacus.com>
  46. * @copyright 2006-2008 by Philippe Jausions / 11abacus
  47. * @license http://www.opensource.org/licenses/bsd-license.php New BSD
  48. * @version CVS: $Id: CAPTCHA.php,v 1.1 2008/04/26 23:27:28 jausions Exp $
  49. * @link http://pear.php.net/package/HTML_QuickForm_CAPTCHA
  50. */
  51. /**
  52. * Common class for HTML_QuickForm elements to display a CAPTCHA
  53. *
  54. * The HTML_QuickForm_CAPTCHA package adds an element to the
  55. * HTML_QuickForm package to display a CAPTCHA question (image, riddle, etc...)
  56. *
  57. * This package requires the use of a PHP session ($_SESSION).
  58. *
  59. * Because the CAPTCHA element is serialized in the PHP session,
  60. * you need to include the class declaration BEFORE the session starts.
  61. * So BEWARE if you have php.ini session.auto_start enabled, you won't be
  62. * able to use this element, unless you're also using PHP 5's __autoload()
  63. * or php.ini's unserialize_callback_func setting
  64. *
  65. * @category HTML
  66. * @package HTML_QuickForm_CAPTCHA
  67. * @author Philippe Jausions <Philippe.Jausions@11abacus.com>
  68. * @copyright 2006-2008 by Philippe Jausions / 11abacus
  69. * @license http://www.opensource.org/licenses/bsd-license.php New BSD
  70. * @version Release: 0.3.0
  71. * @link http://pear.php.net/package/HTML_QuickForm_CAPTCHA
  72. * @abstract
  73. */
  74. class HTML_QuickForm_CAPTCHA extends HTML_QuickForm_input
  75. {
  76. /**
  77. * Default options
  78. *
  79. * @var array
  80. * @access protected
  81. */
  82. var $_options = array(
  83. 'sessionVar' => '_HTML_QuickForm_CAPTCHA',
  84. 'phrase' => null,
  85. );
  86. /**
  87. * CAPTCHA driver
  88. *
  89. * @var string
  90. * @access protected
  91. */
  92. var $_CAPTCHA_driver;
  93. /**
  94. * Class constructor
  95. *
  96. * @param string $elementName Name
  97. * @param mixed $elementLabel Label for the CAPTCHA
  98. * @param array $options Options for the Text_CAPTCHA package
  99. * <ul>
  100. * <li>'sessionVar' (string) name of session variable containing
  101. * the Text_CAPTCHA instance (defaults to
  102. * _HTML_QuickForm_CAPTCHA.)</li>
  103. * <li>Other options depend on the driver used</li>
  104. * </ul>
  105. * @param mixed $attributes HTML Attributes for the <a> tag surrounding
  106. * the image. Can be a string or array.
  107. *
  108. * @access public
  109. */
  110. public function __construct(
  111. $elementName = null,
  112. $elementLabel = null,
  113. $options = null,
  114. $attributes = null
  115. ) {
  116. parent::__construct($elementName, $elementLabel, $attributes);
  117. $this->setType('CAPTCHA_'.$this->_CAPTCHA_driver);
  118. if (is_array($options)) {
  119. $this->_options = array_merge($this->_options, $options);
  120. }
  121. }
  122. /**
  123. * Initializes the CAPTCHA instance (if needed)
  124. *
  125. * @return boolean TRUE or PEAR_Error on error
  126. * @access protected
  127. */
  128. function _initCAPTCHA()
  129. {
  130. $sessionVar = $this->_options['sessionVar'];
  131. if (empty($_SESSION[$sessionVar])) {
  132. $_SESSION[$sessionVar] = Text_CAPTCHA::factory($this->_CAPTCHA_driver);
  133. if (PEAR::isError($_SESSION[$sessionVar])) {
  134. return $_SESSION[$sessionVar];
  135. }
  136. $result = $_SESSION[$sessionVar]->init($this->_options);
  137. if (PEAR::isError($result)) {
  138. return $result;
  139. }
  140. }
  141. return true;
  142. }
  143. /**
  144. * Returns the answer/phrase of the CAPTCHA
  145. *
  146. * @param mixed &$values Ignored by this element
  147. *
  148. * @return string
  149. * @access private
  150. */
  151. function _findValue(&$values)
  152. {
  153. return $this->getValue();
  154. }
  155. /**
  156. * Returns the answer/phrase of the CAPTCHA
  157. *
  158. * @return string
  159. * @access public
  160. */
  161. function getValue()
  162. {
  163. $sessionVar = $this->_options['sessionVar'];
  164. return (!empty($_SESSION[$sessionVar]))
  165. ? $_SESSION[$sessionVar]->getPhrase()
  166. : null;
  167. }
  168. /**
  169. * Returns the answer/phrase of the CAPTCHA
  170. *
  171. * @param mixed &$submitValues Ignored by this element
  172. * @param boolean $assoc Whether to return an array
  173. *
  174. * @return string
  175. * @access public
  176. */
  177. function exportValue(&$submitValues, $assoc = false)
  178. {
  179. return ($assoc)
  180. ? array($this->getName() => $this->getValue())
  181. : $this->getValue();
  182. }
  183. /**
  184. * Sets the CAPTCHA question/phrase
  185. *
  186. * Pass NULL or no argument for a random question/phrase to be generated
  187. *
  188. * @param string $phrase Value of the CAPTCHA to set
  189. *
  190. * @return void
  191. * @access public
  192. */
  193. function setPhrase($phrase = null)
  194. {
  195. $this->_options['phrase'] = $phrase;
  196. if (!empty($_SESSION[$this->_options['sessionVar']])) {
  197. $_SESSION[$this->_options['sessionVar']]->setPhrase($phrase);
  198. }
  199. }
  200. /**
  201. * Destroys the CAPTCHA instance to prevent reuse
  202. *
  203. * @return void
  204. * @access public
  205. */
  206. function destroy()
  207. {
  208. unset($_SESSION[$this->_options['sessionVar']]);
  209. }
  210. /**
  211. * Returns the HTML for the CAPTCHA
  212. *
  213. * This can be overwritten by sub-classes for specific output behavior
  214. * (for instance the Image CAPTCHA displays an image)
  215. *
  216. * @return string
  217. * @access public
  218. */
  219. function toHtml()
  220. {
  221. $result = $this->_initCAPTCHA();
  222. if (PEAR::isError($result)) {
  223. return $result;
  224. }
  225. $captcha = $_SESSION[$this->_options['sessionVar']]->getCAPTCHA();
  226. $attr = $this->_attributes;
  227. unset($attr['type']);
  228. unset($attr['value']);
  229. unset($attr['name']);
  230. $html = $this->_getTabs()
  231. . '<span' . $this->_getAttrString($attr) . '>'
  232. . htmlspecialchars($captcha)
  233. . '</span>';
  234. return $html;
  235. }
  236. }