From: Matthias Schmidt Date: Sat, 5 Jan 2019 10:14:09 +0000 (+0100) Subject: Add captcha form field X-Git-Tag: 5.2.0_Alpha_1~374 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=2f1e17ef1bae55087cbbe7d48611c24f1d317e24;p=GitHub%2FWoltLab%2FWCF.git Add captcha form field See #2509 --- diff --git a/wcfsetup/install/files/lib/system/form/builder/field/CaptchaFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/CaptchaFormField.class.php new file mode 100644 index 0000000000..c65b480557 --- /dev/null +++ b/wcfsetup/install/files/lib/system/form/builder/field/CaptchaFormField.class.php @@ -0,0 +1,114 @@ + + * @package WoltLabSuite\Core\System\Form\Builder\Field + * @since 3.2 + */ +class CaptchaFormField extends AbstractFormField implements IObjectTypeFormField { + use TDefaultIdFormField; + use TObjectTypeFormField { + objectType as defaultObjectType; + } + + /** + * @inheritDoc + */ + public function cleanup() { + try { + /** @var ICaptchaHandler $captcha */ + $captcha = $this->getObjectType()->getProcessor(); + + $captcha->reset(); + } + catch (\BadMethodCallException $e) { + // ignore + } + + return $this; + } + + /** + * @inheritDoc + */ + public function getHtml() { + /** @var ICaptchaHandler $captcha */ + $captcha = $this->getObjectType()->getProcessor(); + + return $captcha->getFormElement(); + } + + /** + * @inheritDoc + */ + public function getObjectTypeDefinition() { + return 'com.woltlab.wcf.captcha'; + } + + /** + * @inheritDoc + */ + public function hasSaveValue() { + return false; + } + + /** + * @inheritDoc + */ + public function objectType($objectType) { + // ignore empty object type which is the case if no captcha has been set + if ($objectType === '') { + return $this; + } + + return $this->defaultObjectType($objectType); + } + + /** + * @inheritDoc + */ + public function readValue() { + /** @var ICaptchaHandler $captcha */ + $captcha = $this->getObjectType()->getProcessor(); + + // the captcha API relies on `$_POST` thus make sure that request data is in `$_POST`, + // at least temporarily + $requestData = $this->getDocument()->getRequestData(); + $post = null; + if ($requestData !== $_POST) { + $post = $_POST; + $_POST = $requestData; + } + + $captcha->readFormParameters(); + + // restore `$_POST` + if ($post !== null) { + $_POST = $post; + } + } + + /** + * @inheritDoc + */ + public function validate() { + /** @var ICaptchaHandler $captcha */ + $captcha = $this->getObjectType()->getProcessor(); + + $captcha->validate(); + } + + /** + * @inheritDoc + */ + protected static function getDefaultId() { + return 'captcha'; + } +}