1: <?php
2: /**
3: * CAPTCHA for Image mode
4: *
5: * Based on DuGris' SecurityImage
6: *
7: * You may not change or alter any portion of this comment or credits
8: * of supporting developers from this source code or any supporting source code
9: * which is considered copyrighted (c) material of the original comment or credit authors.
10: * This program is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13: *
14: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
15: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
16: * @package class
17: * @subpackage CAPTCHA
18: * @since 2.3.0
19: * @author Taiwen Jiang <phppp@users.sourceforge.net>
20: */
21: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
22:
23: /**
24: * Class XoopsCaptchaImage
25: */
26: class XoopsCaptchaImage extends XoopsCaptchaMethod
27: {
28: /**
29: * XoopsCaptchaImage::isActive()
30: *
31: * @return bool
32: */
33: public function isActive()
34: {
35: if (!extension_loaded('gd')) {
36: trigger_error('GD library is not loaded', E_USER_WARNING);
37:
38: return false;
39: } else {
40: $required_functions = array(
41: 'imagecreatetruecolor',
42: 'imagecolorallocate',
43: 'imagefilledrectangle',
44: 'imagejpeg',
45: 'imagedestroy',
46: 'imageftbbox');
47: foreach ($required_functions as $func) {
48: if (!function_exists($func)) {
49: trigger_error('Function ' . $func . ' is not defined', E_USER_WARNING);
50:
51: return false;
52: }
53: }
54: }
55:
56: return true;
57: }
58:
59: /**
60: * XoopsCaptchaImage::render()
61: *
62: * @return string|void
63: */
64: public function render()
65: {
66: $js = "<script type='text/javascript'>
67: function xoops_captcha_refresh(imgId)
68: {
69: xoopsGetElementById(imgId).src = '" . XOOPS_URL . "/class/captcha/image/scripts/image.php?refresh='+Math.random();
70: }
71: </script>";
72: $image = $this->loadImage();
73: $image .= "<br><a href=\"javascript: xoops_captcha_refresh('" . $this->config['name'] . "')\">" . _CAPTCHA_REFRESH . '</a>';
74: $input = '<input type="text" name="' . $this->config['name'] . '" id="' . $this->config['name'] . '" size="' . $this->config['num_chars'] . '" maxlength="' . $this->config['num_chars'] . '" value="" />';
75: $rule = _CAPTCHA_RULE_IMAGE;
76: $rule .= '<br>' . (empty($this->config['casesensitive']) ? _CAPTCHA_RULE_CASEINSENSITIVE : _CAPTCHA_RULE_CASESENSITIVE);
77: if (!empty($this->config['maxattempts'])) {
78: $rule .= '<br>' . sprintf(_CAPTCHA_MAXATTEMPTS, $this->config['maxattempts']);
79: }
80:
81: return $js . $image . '<br><br>' . $input . '<br>' . $rule;
82: }
83:
84: /**
85: * XoopsCaptchaImage::loadImage()
86: *
87: * @return string
88: */
89: public function loadImage()
90: {
91: return '<img id="' . $this->config['name'] . '" src="' . XOOPS_URL . '/class/captcha/image/scripts/image.php" onclick=\'this.src="' . XOOPS_URL . '/class/captcha/image/scripts/image.php?refresh="+Math.random()' . '\' style="cursor: pointer; vertical-align: middle;" alt="" />';
92: }
93: }
94: