CAPTCHA.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. function HTML_QuickForm_CAPTCHA($elementName = null, $elementLabel = null,
  111. $options = null, $attributes = null)
  112. {
  113. parent::__construct($elementName, $elementLabel, $attributes);
  114. $this->setType('CAPTCHA_'.$this->_CAPTCHA_driver);
  115. if (is_array($options)) {
  116. $this->_options = array_merge($this->_options, $options);
  117. }
  118. }
  119. /**
  120. * Initializes the CAPTCHA instance (if needed)
  121. *
  122. * @return boolean TRUE or PEAR_Error on error
  123. * @access protected
  124. */
  125. function _initCAPTCHA()
  126. {
  127. $sessionVar = $this->_options['sessionVar'];
  128. if (empty($_SESSION[$sessionVar])) {
  129. $_SESSION[$sessionVar] = Text_CAPTCHA::factory($this->_CAPTCHA_driver);
  130. if (PEAR::isError($_SESSION[$sessionVar])) {
  131. return $_SESSION[$sessionVar];
  132. }
  133. $result = $_SESSION[$sessionVar]->init($this->_options);
  134. if (PEAR::isError($result)) {
  135. return $result;
  136. }
  137. }
  138. return true;
  139. }
  140. /**
  141. * Returns the answer/phrase of the CAPTCHA
  142. *
  143. * @param mixed &$values Ignored by this element
  144. *
  145. * @return string
  146. * @access private
  147. */
  148. function _findValue(&$values)
  149. {
  150. return $this->getValue();
  151. }
  152. /**
  153. * Returns the answer/phrase of the CAPTCHA
  154. *
  155. * @return string
  156. * @access public
  157. */
  158. function getValue()
  159. {
  160. $sessionVar = $this->_options['sessionVar'];
  161. return (!empty($_SESSION[$sessionVar]))
  162. ? $_SESSION[$sessionVar]->getPhrase()
  163. : null;
  164. }
  165. /**
  166. * Returns the answer/phrase of the CAPTCHA
  167. *
  168. * @param mixed &$submitValues Ignored by this element
  169. * @param boolean $assoc Whether to return an array
  170. *
  171. * @return string
  172. * @access public
  173. */
  174. function exportValue(&$submitValues, $assoc = false)
  175. {
  176. return ($assoc)
  177. ? array($this->getName() => $this->getValue())
  178. : $this->getValue();
  179. }
  180. /**
  181. * Sets the CAPTCHA question/phrase
  182. *
  183. * Pass NULL or no argument for a random question/phrase to be generated
  184. *
  185. * @param string $phrase Value of the CAPTCHA to set
  186. *
  187. * @return void
  188. * @access public
  189. */
  190. function setPhrase($phrase = null)
  191. {
  192. $this->_options['phrase'] = $phrase;
  193. if (!empty($_SESSION[$this->_options['sessionVar']])) {
  194. $_SESSION[$this->_options['sessionVar']]->setPhrase($phrase);
  195. }
  196. }
  197. /**
  198. * Destroys the CAPTCHA instance to prevent reuse
  199. *
  200. * @return void
  201. * @access public
  202. */
  203. function destroy()
  204. {
  205. unset($_SESSION[$this->_options['sessionVar']]);
  206. }
  207. /**
  208. * Returns the HTML for the CAPTCHA
  209. *
  210. * This can be overwritten by sub-classes for specific output behavior
  211. * (for instance the Image CAPTCHA displays an image)
  212. *
  213. * @return string
  214. * @access public
  215. */
  216. function toHtml()
  217. {
  218. $result = $this->_initCAPTCHA();
  219. if (PEAR::isError($result)) {
  220. return $result;
  221. }
  222. $captcha = $_SESSION[$this->_options['sessionVar']]->getCAPTCHA();
  223. $attr = $this->_attributes;
  224. unset($attr['type']);
  225. unset($attr['value']);
  226. unset($attr['name']);
  227. $html = $this->_getTabs()
  228. . '<span' . $this->_getAttrString($attr) . '>'
  229. . htmlspecialchars($captcha)
  230. . '</span>';
  231. return $html;
  232. }
  233. }