Merge pull request #5989 from WoltLab/wsc-rpc-api-const
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / form / AbstractCaptchaForm.class.php
CommitLineData
96714cab 1<?php
a9229942 2
96714cab 3namespace wcf\form;
a9229942 4
96714cab
MS
5use wcf\system\captcha\CaptchaHandler;
6use wcf\system\exception\SystemException;
96714cab 7use wcf\system\WCF;
96714cab
MS
8
9/**
10 * Abstract implementation of a form using captcha.
a9229942
TD
11 *
12 * @author Matthias Schmidt
13 * @copyright 2001-2019 WoltLab GmbH
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
96714cab 15 */
a9229942
TD
16abstract class AbstractCaptchaForm extends AbstractForm
17{
18 /**
19 * captcha object type object
20 * @var \wcf\data\object\type\ObjectType
21 */
22 public $captchaObjectType;
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 bool
33 */
34 public $useCaptcha = true;
35
36 /**
37 * true to force captcha usage
38 * @var bool
39 */
40 public $forceCaptcha = false;
41
42 /**
43 * @inheritDoc
44 */
45 public function assignVariables()
46 {
47 parent::assignVariables();
48
49 WCF::getTPL()->assign([
50 'captchaObjectType' => $this->captchaObjectType,
51 'useCaptcha' => $this->useCaptcha,
52 ]);
53 }
54
55 /**
56 * @inheritDoc
57 */
58 public function readData()
59 {
60 if ((!WCF::getUser()->userID || $this->forceCaptcha) && $this->useCaptcha && $this->captchaObjectTypeName) {
61 $this->captchaObjectType = CaptchaHandler::getInstance()->getObjectTypeByName($this->captchaObjectTypeName);
62 if ($this->captchaObjectType === null) {
63 throw new SystemException("Unknown captcha object type with name '" . $this->captchaObjectTypeName . "'");
64 }
65
66 if (!$this->captchaObjectType->getProcessor()->isAvailable()) {
67 $this->captchaObjectType = null;
68 }
69 }
70
71 parent::readData();
72 }
73
74 /**
75 * @inheritDoc
76 */
77 public function readFormParameters()
78 {
79 parent::readFormParameters();
80
81 if ($this->captchaObjectType) {
82 $this->captchaObjectType->getProcessor()->readFormParameters();
83 }
84 }
85
86 /**
87 * @inheritDoc
88 */
89 public function save()
90 {
91 parent::save();
92
93 if ($this->captchaObjectType) {
94 $this->captchaObjectType->getProcessor()->reset();
95 }
96 }
97
98 /**
99 * @inheritDoc
100 */
101 public function validate()
102 {
103 parent::validate();
104
105 $this->validateCaptcha();
106 }
107
108 /**
109 * Validates the captcha.
110 */
111 protected function validateCaptcha()
112 {
113 if ($this->captchaObjectType) {
114 $this->captchaObjectType->getProcessor()->validate();
115 }
116 }
96714cab 117}