From 2f1e17ef1bae55087cbbe7d48611c24f1d317e24 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt Date: Sat, 5 Jan 2019 11:14:09 +0100 Subject: [PATCH] Add captcha form field See #2509 --- .../builder/field/CaptchaFormField.class.php | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 wcfsetup/install/files/lib/system/form/builder/field/CaptchaFormField.class.php 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'; + } +} -- 2.20.1