Image.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. function HTML_QuickForm_CAPTCHA_Image($elementName = null, $elementLabel = null, $options = null, $attributes = null) {
  55. return parent::HTML_QuickForm_CAPTCHA($elementName, $elementLabel, $options, $attributes);
  56. }
  57. /**
  58. * Default options
  59. *
  60. * @var array
  61. * @access protected
  62. */
  63. var $_options = array(
  64. 'sessionVar' => '_HTML_QuickForm_CAPTCHA',
  65. 'width' => '200',
  66. 'height' => '80',
  67. 'alt' => 'Click to view another image',
  68. 'callback' => '',
  69. 'imageOptions' => null,
  70. 'phrase' => null,
  71. );
  72. /**
  73. * CAPTCHA driver
  74. *
  75. * @var string
  76. * @access protected
  77. */
  78. var $_CAPTCHA_driver = 'Image';
  79. /**
  80. * Code based in HTML_QuickForm_text::getTemplate()
  81. * In order to render correctly the captcha in different layouts
  82. * @param string $layout
  83. *
  84. * @return string
  85. */
  86. public static function getTemplate($layout)
  87. {
  88. $size = 8;
  89. switch ($layout) {
  90. case FormValidator::LAYOUT_INLINE:
  91. return '
  92. <div class="form-group {error_class}">
  93. <label {label-for} >
  94. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  95. {label}
  96. </label>
  97. {element}
  98. </div>';
  99. break;
  100. case FormValidator::LAYOUT_HORIZONTAL:
  101. return '
  102. <div class="form-group {error_class}">
  103. <label {label-for} class="col-sm-2 control-label" >
  104. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  105. {label}
  106. </label>
  107. <div class="col-sm-'.$size.'">
  108. {icon}
  109. {element}
  110. <!-- BEGIN label_2 -->
  111. <p class="help-block">{label_2}</p>
  112. <!-- END label_2 -->
  113. <!-- BEGIN error -->
  114. <span class="help-inline">{error}</span>
  115. <!-- END error -->
  116. </div>
  117. <div class="col-sm-2">
  118. <!-- BEGIN label_3 -->
  119. {label_3}
  120. <!-- END label_3 -->
  121. </div>
  122. </div>';
  123. break;
  124. case FormValidator::LAYOUT_BOX_NO_LABEL:
  125. return '
  126. <div class="input-group">
  127. {icon}
  128. {element}
  129. </div>';
  130. break;
  131. }
  132. }
  133. /**
  134. * Returns the HTML for the CAPTCHA image
  135. *
  136. * @return string
  137. * @access public
  138. */
  139. public function toHtml()
  140. {
  141. if ($this->_flagFrozen) {
  142. return '';
  143. }
  144. $result = parent::_initCAPTCHA();
  145. if (PEAR::isError($result)) {
  146. return $result;
  147. }
  148. $html = '';
  149. $tabs = $this->_getTabs();
  150. $inputName = $this->getName();
  151. $imgName = 'QF_CAPTCHA_'.$inputName;
  152. if ($this->getComment() != '') {
  153. $html .= $tabs.'<!-- '.$this->getComment().' // -->';
  154. }
  155. $attr = $this->_attributes;
  156. unset($attr['type']);
  157. unset($attr['value']);
  158. unset($attr['name']);
  159. $html = $tabs.'<a href="'.$this->_options['callback']
  160. .'" target="_blank" '
  161. .$this->_getAttrString($attr)
  162. .' onclick="var cancelClick = false; '
  163. .$this->getOnclickJs($imgName)
  164. .' return !cancelClick;"><img src="'
  165. .$this->_options['callback'].'" name="'.$imgName
  166. .'" id="'.$imgName.'" width="'.$this->_options['width']
  167. .'" height="'.$this->_options['height'].'" title="'
  168. .htmlspecialchars($this->_options['alt']).'" /></a>';
  169. return $html;
  170. }
  171. /**
  172. * Creates the javascript for the onclick event which will
  173. * reload a new CAPTCHA image
  174. *
  175. * @param string $imageName The image name/id
  176. *
  177. * @return string
  178. * @access public
  179. */
  180. function getOnclickJs($imageName)
  181. {
  182. $onclickJs = ''
  183. .'if (document.images) {'
  184. .' var img = new Image();'
  185. .' var d = new Date();'
  186. .' img.src = this.href + ((this.href.indexOf(\'?\') == -1) '
  187. .'? \'?\' : \'&\') + d.getTime();'
  188. .' document.images[\''.addslashes($imageName).'\'].src = img.src;'
  189. .' cancelClick = true;'
  190. .'}';
  191. return $onclickJs;
  192. }
  193. }