Image.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. * Required packages
  23. */
  24. require_once 'HTML/QuickForm/CAPTCHA.php';
  25. require_once 'Text/CAPTCHA/Driver/Image.php';
  26. /**
  27. * Element for HTML_QuickForm to display a CAPTCHA image
  28. *
  29. * The HTML_QuickForm_CAPTCHA package adds an element to the
  30. * HTML_QuickForm package to display a CAPTCHA image.
  31. *
  32. * Options for the element
  33. * <ul>
  34. * <li>'width' (integer) width of the image,</li>
  35. * <li>'height' (integer) height of the image,</li>
  36. * <li>'imageOptions' (array) options passed to the Image_Text
  37. * constructor,</li>
  38. * <li>'callback' (string) URL of callback script that will generate
  39. * and output the image itself,</li>
  40. * <li>'alt' (string) the alt text for the image,</li>
  41. * <li>'sessionVar' (string) name of session variable containing
  42. * the Text_CAPTCHA instance (defaults to
  43. * _HTML_QuickForm_CAPTCHA.)</li>
  44. * </ul>
  45. *
  46. * This package requires the use of a PHP session.
  47. *
  48. * @category HTML
  49. * @package HTML_QuickForm_CAPTCHA
  50. * @author Philippe Jausions <Philippe.Jausions@11abacus.com>
  51. * @copyright 2006-2008 by Philippe Jausions / 11abacus
  52. * @license http://www.opensource.org/licenses/bsd-license.php New BSD
  53. * @version Release: 0.3.0
  54. * @link http://pear.php.net/package/HTML_QuickForm_CAPTCHA
  55. * @see Text_CAPTCHA_Driver_Image
  56. */
  57. class HTML_QuickForm_CAPTCHA_Image extends HTML_QuickForm_CAPTCHA
  58. {
  59. function HTML_QuickForm_CAPTCHA_Image($elementName = null, $elementLabel = null, $options = null, $attributes = null) {
  60. return parent::HTML_QuickForm_CAPTCHA($elementName, $elementLabel, $options, $attributes);
  61. }
  62. /**
  63. * Default options
  64. *
  65. * @var array
  66. * @access protected
  67. */
  68. var $_options = array(
  69. 'sessionVar' => '_HTML_QuickForm_CAPTCHA',
  70. 'width' => '200',
  71. 'height' => '80',
  72. 'alt' => 'Click to view another image',
  73. 'callback' => '',
  74. 'imageOptions' => null,
  75. 'phrase' => null,
  76. );
  77. /**
  78. * CAPTCHA driver
  79. *
  80. * @var string
  81. * @access protected
  82. */
  83. var $_CAPTCHA_driver = 'Image';
  84. /**
  85. * Returns the HTML for the CAPTCHA image
  86. *
  87. * @return string
  88. * @access public
  89. */
  90. function toHtml()
  91. {
  92. if ($this->_flagFrozen) {
  93. return '';
  94. }
  95. $result = parent::_initCAPTCHA();
  96. if (PEAR::isError($result)) {
  97. return $result;
  98. }
  99. $html = '';
  100. $tabs = $this->_getTabs();
  101. $inputName = $this->getName();
  102. $imgName = 'QF_CAPTCHA_'.$inputName;
  103. if ($this->getComment() != '') {
  104. $html .= $tabs.'<!-- '.$this->getComment().' // -->';
  105. }
  106. $attr = $this->_attributes;
  107. unset($attr['type']);
  108. unset($attr['value']);
  109. unset($attr['name']);
  110. $html = $tabs.'<a href="'.$this->_options['callback']
  111. .'" target="_blank" '
  112. .$this->_getAttrString($attr)
  113. .' onclick="var cancelClick = false; '
  114. .$this->getOnclickJs($imgName)
  115. .' return !cancelClick;"><img src="'
  116. .$this->_options['callback'].'" name="'.$imgName
  117. .'" id="'.$imgName.'" width="'.$this->_options['width']
  118. .'" height="'.$this->_options['height'].'" title="'
  119. .htmlspecialchars($this->_options['alt']).'" /></a>';
  120. return $html;
  121. }
  122. /**
  123. * Creates the javascript for the onclick event which will
  124. * reload a new CAPTCHA image
  125. *
  126. * @param string $imageName The image name/id
  127. *
  128. * @return string
  129. * @access public
  130. */
  131. function getOnclickJs($imageName)
  132. {
  133. $onclickJs = ''
  134. .'if (document.images) {'
  135. .' var img = new Image();'
  136. .' var d = new Date();'
  137. .' img.src = this.href + ((this.href.indexOf(\'?\') == -1) '
  138. .'? \'?\' : \'&\') + d.getTime();'
  139. .' document.images[\''.addslashes($imageName).'\'].src = img.src;'
  140. .' cancelClick = true;'
  141. .'}';
  142. return $onclickJs;
  143. }
  144. }