Image.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4. * Element for HTML_QuickForm to display a CAPTCHA image
  5. *
  6. * The HTML_QuickForm_CAPTCHA package adds an element to the
  7. * HTML_QuickForm package to display a CAPTCHA image.
  8. *
  9. * This package requires the use of a PHP session.
  10. *
  11. * PHP versions 4 and 5
  12. *
  13. * @category HTML
  14. * @package HTML_QuickForm_CAPTCHA
  15. * @author Philippe Jausions <Philippe.Jausions@11abacus.com>
  16. * @copyright 2006-2008 by Philippe Jausions / 11abacus
  17. * @license http://www.opensource.org/licenses/bsd-license.php New BSD
  18. * @version CVS: $Id: Image.php,v 1.1 2008/04/26 23:27:30 jausions Exp $
  19. * @link http://pear.php.net/package/HTML_QuickForm_CAPTCHA
  20. */
  21. /**
  22. * Element for HTML_QuickForm to display a CAPTCHA image
  23. *
  24. * The HTML_QuickForm_CAPTCHA package adds an element to the
  25. * HTML_QuickForm package to display a CAPTCHA image.
  26. *
  27. * Options for the element
  28. * <ul>
  29. * <li>'width' (integer) width of the image,</li>
  30. * <li>'height' (integer) height of the image,</li>
  31. * <li>'imageOptions' (array) options passed to the Image_Text
  32. * constructor,</li>
  33. * <li>'callback' (string) URL of callback script that will generate
  34. * and output the image itself,</li>
  35. * <li>'alt' (string) the alt text for the image,</li>
  36. * <li>'sessionVar' (string) name of session variable containing
  37. * the Text_CAPTCHA instance (defaults to
  38. * _HTML_QuickForm_CAPTCHA.)</li>
  39. * </ul>
  40. *
  41. * This package requires the use of a PHP session.
  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 Release: 0.3.0
  49. * @link http://pear.php.net/package/HTML_QuickForm_CAPTCHA
  50. * @see Text_CAPTCHA_Driver_Image
  51. */
  52. class HTML_QuickForm_CAPTCHA_Image extends HTML_QuickForm_CAPTCHA
  53. {
  54. public function __construct(
  55. $elementName = null,
  56. $elementLabel = null,
  57. $options = null,
  58. $attributes = null
  59. ) {
  60. return parent::__construct(
  61. $elementName,
  62. $elementLabel,
  63. $options,
  64. $attributes
  65. );
  66. }
  67. /**
  68. * Default options
  69. *
  70. * @var array
  71. * @access protected
  72. */
  73. var $_options = array(
  74. 'sessionVar' => '_HTML_QuickForm_CAPTCHA',
  75. 'width' => '200',
  76. 'height' => '80',
  77. 'alt' => 'Click to view another image',
  78. 'callback' => '',
  79. 'imageOptions' => null,
  80. 'phrase' => null,
  81. );
  82. /**
  83. * CAPTCHA driver
  84. *
  85. * @var string
  86. * @access protected
  87. */
  88. var $_CAPTCHA_driver = 'Image';
  89. /**
  90. * Code based in HTML_QuickForm_text::getTemplate()
  91. * In order to render correctly the captcha in different layouts
  92. * @param string $layout
  93. *
  94. * @return string
  95. */
  96. public static function getTemplate($layout)
  97. {
  98. $size = 8;
  99. switch ($layout) {
  100. case FormValidator::LAYOUT_INLINE:
  101. return '
  102. <div class="form-group {error_class}">
  103. <label {label-for} >
  104. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  105. {label}
  106. </label>
  107. {element}
  108. </div>';
  109. break;
  110. case FormValidator::LAYOUT_HORIZONTAL:
  111. return '
  112. <div class="form-group {error_class}">
  113. <label {label-for} class="col-sm-2 control-label" >
  114. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  115. {label}
  116. </label>
  117. <div class="col-sm-'.$size.'">
  118. {icon}
  119. {element}
  120. <!-- BEGIN label_2 -->
  121. <p class="help-block">{label_2}</p>
  122. <!-- END label_2 -->
  123. <!-- BEGIN error -->
  124. <span class="help-inline help-block">{error}</span>
  125. <!-- END error -->
  126. </div>
  127. <div class="col-sm-2">
  128. <!-- BEGIN label_3 -->
  129. {label_3}
  130. <!-- END label_3 -->
  131. </div>
  132. </div>';
  133. break;
  134. case FormValidator::LAYOUT_BOX_NO_LABEL:
  135. return '
  136. <div class="input-group">
  137. {icon}
  138. {element}
  139. </div>';
  140. break;
  141. }
  142. }
  143. /**
  144. * Returns the HTML for the CAPTCHA image
  145. *
  146. * @return string
  147. * @access public
  148. */
  149. public function toHtml()
  150. {
  151. if ($this->_flagFrozen) {
  152. return '';
  153. }
  154. $result = parent::_initCAPTCHA();
  155. if (PEAR::isError($result)) {
  156. return $result;
  157. }
  158. $html = '';
  159. $tabs = $this->_getTabs();
  160. $inputName = $this->getName();
  161. $imgName = 'QF_CAPTCHA_'.$inputName;
  162. if ($this->getComment() != '') {
  163. $html .= $tabs.'<!-- '.$this->getComment().' // -->';
  164. }
  165. $attr = $this->_attributes;
  166. unset($attr['type']);
  167. unset($attr['value']);
  168. unset($attr['name']);
  169. $html = $tabs.'<a href="'.$this->_options['callback']
  170. .'" target="_blank" '
  171. .$this->_getAttrString($attr)
  172. .' onclick="var cancelClick = false; '
  173. .$this->getOnclickJs($imgName)
  174. .' return !cancelClick;"><img src="'
  175. .$this->_options['callback'].'" name="'.$imgName
  176. .'" id="'.$imgName.'" width="'.$this->_options['width']
  177. .'" height="'.$this->_options['height'].'" title="'
  178. .htmlspecialchars($this->_options['alt']).'" /></a>';
  179. return $html;
  180. }
  181. /**
  182. * Creates the javascript for the onclick event which will
  183. * reload a new CAPTCHA image
  184. *
  185. * @param string $imageName The image name/id
  186. *
  187. * @return string
  188. * @access public
  189. */
  190. function getOnclickJs($imageName)
  191. {
  192. $onclickJs = ''
  193. .'if (document.images) {'
  194. .' var img = new Image();'
  195. .' var d = new Date();'
  196. .' img.src = this.href + ((this.href.indexOf(\'?\') == -1) '
  197. .'? \'?\' : \'&\') + d.getTime();'
  198. .' document.images[\''.addslashes($imageName).'\'].src = img.src;'
  199. .' cancelClick = true;'
  200. .'}';
  201. return $onclickJs;
  202. }
  203. }