Added parameter to force captcha usage for registered users
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / form / AbstractCaptchaForm.class.php
1 <?php
2 namespace wcf\form;
3 use wcf\system\captcha\CaptchaHandler;
4 use wcf\system\exception\SystemException;
5 use wcf\system\WCF;
6
7 /**
8 * Abstract implementation of a form using captcha.
9 *
10 * @author Matthias Schmidt
11 * @copyright 2001-2016 WoltLab GmbH
12 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
13 * @package com.woltlab.wcf
14 * @subpackage form
15 * @category Community Framework
16 */
17 abstract class AbstractCaptchaForm extends AbstractForm {
18 /**
19 * captcha object type object
20 * @var \wcf\data\object\type\ObjectType
21 */
22 public $captchaObjectType = null;
23
24 /**
25 * name of the captcha object type; if empty, captcha is disabled
26 * @var string
27 */
28 public $captchaObjectTypeName = CAPTCHA_TYPE;
29
30 /**
31 * true if captcha is used
32 * @var boolean
33 */
34 public $useCaptcha = true;
35
36 /**
37 * true to force captcha usage
38 * @var boolean
39 */
40 public $forceCaptcha = false;
41
42 /**
43 * @inheritDoc
44 */
45 public function assignVariables() {
46 parent::assignVariables();
47
48 WCF::getTPL()->assign([
49 'captchaObjectType' => $this->captchaObjectType,
50 'useCaptcha' => $this->useCaptcha
51 ]);
52 }
53
54 /**
55 * @inheritDoc
56 */
57 public function readData() {
58 if ((!WCF::getUser()->userID || $this->forceCaptcha) && $this->useCaptcha && $this->captchaObjectTypeName) {
59 $this->captchaObjectType = CaptchaHandler::getInstance()->getObjectTypeByName($this->captchaObjectTypeName);
60 if ($this->captchaObjectType === null) {
61 throw new SystemException("Unknown captcha object type with name '".$this->captchaObjectTypeName."'");
62 }
63
64 if (!$this->captchaObjectType->getProcessor()->isAvailable()) {
65 $this->captchaObjectType = null;
66 }
67 }
68
69 parent::readData();
70 }
71
72 /**
73 * @inheritDoc
74 */
75 public function readFormParameters() {
76 parent::readFormParameters();
77
78 if ($this->captchaObjectType) {
79 $this->captchaObjectType->getProcessor()->readFormParameters();
80 }
81 }
82
83 /**
84 * @inheritDoc
85 */
86 public function save() {
87 parent::save();
88
89 if ($this->captchaObjectType) {
90 $this->captchaObjectType->getProcessor()->reset();
91 }
92 }
93
94 /**
95 * @inheritDoc
96 */
97 public function validate() {
98 parent::validate();
99
100 $this->validateCaptcha();
101 }
102
103 /**
104 * Validates the captcha.
105 */
106 protected function validateCaptcha() {
107 if ($this->captchaObjectType) {
108 $this->captchaObjectType->getProcessor()->validate();
109 }
110 }
111 }