1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xoops\Form;
13:
14: /**
15: * Captcha - captcha form element
16: *
17: * For form creation:
18: * Add form element where proper:
19: * <code>
20: * $xoopsform->addElement(new \Xoops\Form\Captcha($caption, $name, $skipmember, $configs));
21: * </code>
22: *
23: * For verification:
24: * <code>
25: * $xoopsCaptcha = \XoopsCaptcha::getInstance();
26: * if (! $xoopsCaptcha->verify() ) {
27: * echo $xoopsCaptcha->getMessage();
28: * ...
29: * }
30: * </code>
31: *
32: * @category Xoops\Form\Captcha
33: * @package Xoops\Form
34: * @author Taiwen Jiang <phppp@users.sourceforge.net>
35: * @copyright 2008-2015 XOOPS Project (http://xoops.org)
36: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
37: * @link http://xoops.org
38: */
39: class Captcha extends Element
40: {
41: /**
42: * @var \XoopsCaptcha
43: */
44: public $captchaHandler;
45:
46: /**
47: * __construct
48: *
49: * @param string|array $caption Caption (default defined in captcha/language/) or array of all attributes
50: * @param string $name Name for the input box
51: * @param boolean $skipmember Skip CAPTCHA check for members
52: * @param array $configs key/value pairs
53: */
54: public function __construct($caption = '', $name = 'xoopscaptcha', $skipmember = true, $configs = array())
55: {
56: $this->captchaHandler = \XoopsCaptcha::getInstance();
57:
58: if (is_array($caption)) {
59: parent::__construct($caption);
60: } else {
61: parent::__construct([]);
62: $this->setIfNotEmpty('caption', $caption);
63: $this->setIfNotEmpty('name', $name);
64: $this->setIfNotSet(':skipmember', $skipmember);
65: $this->setIfNotEmpty(':configs', $configs);
66: }
67:
68: $this->setIfNotSet('caption', $this->captchaHandler->getCaption());
69: $this->setIfNotSet('name', 'xoopscaptcha');
70:
71: $configs = $this->get(':configs', []);
72: $configs['name'] = $this->get('name');
73: $configs['skipmember'] = $this->get(':skipmember', true);
74: $configs = $this->captchaHandler->loadConfig();
75:
76: $this->captchaHandler->setConfigs($configs);
77: if (! $this->captchaHandler->isActive()) {
78: $this->setHidden();
79: }
80: }
81:
82: /**
83: * setConfig
84: *
85: * @param string $name name
86: * @param mixed $val value
87: *
88: * @return boolean
89: */
90: public function setConfig($name, $val)
91: {
92: return $this->captchaHandler->setConfig($name, $val);
93: }
94:
95: /**
96: * render
97: *
98: * @return string
99: */
100: public function render()
101: {
102: return $this->captchaHandler->render();
103: }
104:
105: /**
106: * renderValidationJS
107: *
108: * @return string
109: */
110: public function renderValidationJS()
111: {
112: return $this->captchaHandler->renderValidationJS();
113: }
114: }
115: