Add captcha form field
authorMatthias Schmidt <gravatronics@live.com>
Sat, 5 Jan 2019 10:14:09 +0000 (11:14 +0100)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 5 Jan 2019 10:14:09 +0000 (11:14 +0100)
See #2509

wcfsetup/install/files/lib/system/form/builder/field/CaptchaFormField.class.php [new file with mode: 0644]

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 (file)
index 0000000..c65b480
--- /dev/null
@@ -0,0 +1,114 @@
+<?php
+namespace wcf\system\form\builder\field;
+use wcf\system\captcha\ICaptchaHandler;
+use wcf\system\exception\SystemException;
+
+/**
+ * Implementation of a form field for a captcha.
+ * 
+ * @author     Matthias Schmidt
+ * @copyright  2001-2018 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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';
+       }
+}