Added parameter to force captcha usage for registered users
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / form / AbstractCaptchaForm.class.php
CommitLineData
96714cab
MS
1<?php
2namespace wcf\form;
3use wcf\system\captcha\CaptchaHandler;
4use wcf\system\exception\SystemException;
96714cab 5use wcf\system\WCF;
96714cab
MS
6
7/**
8 * Abstract implementation of a form using captcha.
9 *
10 * @author Matthias Schmidt
7d739af0 11 * @copyright 2001-2016 WoltLab GmbH
96714cab
MS
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 */
17abstract 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 */
fbb526f2 28 public $captchaObjectTypeName = CAPTCHA_TYPE;
96714cab
MS
29
30 /**
17b9b150 31 * true if captcha is used
96714cab
MS
32 * @var boolean
33 */
34 public $useCaptcha = true;
35
17b9b150
MW
36 /**
37 * true to force captcha usage
38 * @var boolean
39 */
40 public $forceCaptcha = false;
41
96714cab 42 /**
0fcfe5f6 43 * @inheritDoc
96714cab
MS
44 */
45 public function assignVariables() {
46 parent::assignVariables();
47
058cbd6a 48 WCF::getTPL()->assign([
fbb526f2
MS
49 'captchaObjectType' => $this->captchaObjectType,
50 'useCaptcha' => $this->useCaptcha
058cbd6a 51 ]);
96714cab
MS
52 }
53
54 /**
0fcfe5f6 55 * @inheritDoc
96714cab
MS
56 */
57 public function readData() {
17b9b150 58 if ((!WCF::getUser()->userID || $this->forceCaptcha) && $this->useCaptcha && $this->captchaObjectTypeName) {
96714cab
MS
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 /**
0fcfe5f6 73 * @inheritDoc
96714cab
MS
74 */
75 public function readFormParameters() {
76 parent::readFormParameters();
77
78 if ($this->captchaObjectType) {
79 $this->captchaObjectType->getProcessor()->readFormParameters();
80 }
96714cab
MS
81 }
82
83 /**
0fcfe5f6 84 * @inheritDoc
96714cab
MS
85 */
86 public function save() {
87 parent::save();
88
89 if ($this->captchaObjectType) {
90 $this->captchaObjectType->getProcessor()->reset();
91 }
96714cab
MS
92 }
93
94 /**
0fcfe5f6 95 * @inheritDoc
96714cab
MS
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 }
96714cab
MS
110 }
111}