CAPTCHA.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. * Required packages
  53. */
  54. require_once 'HTML/QuickForm/input.php';
  55. require_once 'Text/CAPTCHA.php';
  56. /**
  57. * Common class for HTML_QuickForm elements to display a CAPTCHA
  58. *
  59. * The HTML_QuickForm_CAPTCHA package adds an element to the
  60. * HTML_QuickForm package to display a CAPTCHA question (image, riddle, etc...)
  61. *
  62. * This package requires the use of a PHP session ($_SESSION).
  63. *
  64. * Because the CAPTCHA element is serialized in the PHP session,
  65. * you need to include the class declaration BEFORE the session starts.
  66. * So BEWARE if you have php.ini session.auto_start enabled, you won't be
  67. * able to use this element, unless you're also using PHP 5's __autoload()
  68. * or php.ini's unserialize_callback_func setting
  69. *
  70. * @category HTML
  71. * @package HTML_QuickForm_CAPTCHA
  72. * @author Philippe Jausions <Philippe.Jausions@11abacus.com>
  73. * @copyright 2006-2008 by Philippe Jausions / 11abacus
  74. * @license http://www.opensource.org/licenses/bsd-license.php New BSD
  75. * @version Release: 0.3.0
  76. * @link http://pear.php.net/package/HTML_QuickForm_CAPTCHA
  77. * @abstract
  78. */
  79. class HTML_QuickForm_CAPTCHA extends HTML_QuickForm_input
  80. {
  81. /**
  82. * Default options
  83. *
  84. * @var array
  85. * @access protected
  86. */
  87. var $_options = array(
  88. 'sessionVar' => '_HTML_QuickForm_CAPTCHA',
  89. 'phrase' => null,
  90. );
  91. /**
  92. * CAPTCHA driver
  93. *
  94. * @var string
  95. * @access protected
  96. */
  97. var $_CAPTCHA_driver;
  98. /**
  99. * Class constructor
  100. *
  101. * @param string $elementName Name
  102. * @param mixed $elementLabel Label for the CAPTCHA
  103. * @param array $options Options for the Text_CAPTCHA package
  104. * <ul>
  105. * <li>'sessionVar' (string) name of session variable containing
  106. * the Text_CAPTCHA instance (defaults to
  107. * _HTML_QuickForm_CAPTCHA.)</li>
  108. * <li>Other options depend on the driver used</li>
  109. * </ul>
  110. * @param mixed $attributes HTML Attributes for the <a> tag surrounding
  111. * the image. Can be a string or array.
  112. *
  113. * @access public
  114. */
  115. function HTML_QuickForm_CAPTCHA($elementName = null, $elementLabel = null,
  116. $options = null, $attributes = null)
  117. {
  118. HTML_QuickForm_input::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
  119. $this->setType('CAPTCHA_'.$this->_CAPTCHA_driver);
  120. if (is_array($options)) {
  121. $this->_options = array_merge($this->_options, $options);
  122. }
  123. }
  124. /**
  125. * Initializes the CAPTCHA instance (if needed)
  126. *
  127. * @return boolean TRUE or PEAR_Error on error
  128. * @access protected
  129. */
  130. function _initCAPTCHA()
  131. {
  132. $sessionVar = $this->_options['sessionVar'];
  133. if (empty($_SESSION[$sessionVar])) {
  134. $_SESSION[$sessionVar] = Text_CAPTCHA::factory($this->_CAPTCHA_driver);
  135. if (PEAR::isError($_SESSION[$sessionVar])) {
  136. return $_SESSION[$sessionVar];
  137. }
  138. $result = $_SESSION[$sessionVar]->init($this->_options);
  139. if (PEAR::isError($result)) {
  140. return $result;
  141. }
  142. }
  143. return true;
  144. }
  145. /**
  146. * Returns the answer/phrase of the CAPTCHA
  147. *
  148. * @param mixed &$values Ignored by this element
  149. *
  150. * @return string
  151. * @access private
  152. */
  153. function _findValue(&$values)
  154. {
  155. return $this->getValue();
  156. }
  157. /**
  158. * Returns the answer/phrase of the CAPTCHA
  159. *
  160. * @return string
  161. * @access public
  162. */
  163. function getValue()
  164. {
  165. $sessionVar = $this->_options['sessionVar'];
  166. return (!empty($_SESSION[$sessionVar]))
  167. ? $_SESSION[$sessionVar]->getPhrase()
  168. : null;
  169. }
  170. /**
  171. * Returns the answer/phrase of the CAPTCHA
  172. *
  173. * @param mixed &$submitValues Ignored by this element
  174. * @param boolean $assoc Whether to return an array
  175. *
  176. * @return string
  177. * @access public
  178. */
  179. function exportValue(&$submitValues, $assoc = false)
  180. {
  181. return ($assoc)
  182. ? array($this->getName() => $this->getValue())
  183. : $this->getValue();
  184. }
  185. /**
  186. * Sets the CAPTCHA question/phrase
  187. *
  188. * Pass NULL or no argument for a random question/phrase to be generated
  189. *
  190. * @param string $phrase Value of the CAPTCHA to set
  191. *
  192. * @return void
  193. * @access public
  194. */
  195. function setPhrase($phrase = null)
  196. {
  197. $this->_options['phrase'] = $phrase;
  198. if (!empty($_SESSION[$this->_options['sessionVar']])) {
  199. $_SESSION[$this->_options['sessionVar']]->setPhrase($phrase);
  200. }
  201. }
  202. /**
  203. * Destroys the CAPTCHA instance to prevent reuse
  204. *
  205. * @return void
  206. * @access public
  207. */
  208. function destroy()
  209. {
  210. unset($_SESSION[$this->_options['sessionVar']]);
  211. }
  212. /**
  213. * Returns the HTML for the CAPTCHA
  214. *
  215. * This can be overwritten by sub-classes for specific output behavior
  216. * (for instance the Image CAPTCHA displays an image)
  217. *
  218. * @return string
  219. * @access public
  220. */
  221. function toHtml()
  222. {
  223. $result = $this->_initCAPTCHA();
  224. if (PEAR::isError($result)) {
  225. return $result;
  226. }
  227. $captcha = $_SESSION[$this->_options['sessionVar']]->getCAPTCHA();
  228. $attr = $this->_attributes;
  229. unset($attr['type']);
  230. unset($attr['value']);
  231. unset($attr['name']);
  232. $html = $this->_getTabs()
  233. . '<span' . $this->_getAttrString($attr) . '>'
  234. . htmlspecialchars($captcha)
  235. . '</span>';
  236. return $html;
  237. }
  238. }
  239. /**
  240. * Register the rule with QuickForm
  241. */
  242. require_once 'HTML/QuickForm/Rule/CAPTCHA.php';
  243. ?>