diff options
Diffstat (limited to 'lib/classes/forms')
| -rw-r--r-- | lib/classes/forms/Captcha.php | 29 | ||||
| -rw-r--r-- | lib/classes/forms/CaptchaInput.php | 38 | ||||
| -rw-r--r-- | lib/classes/forms/Form.php | 12 | ||||
| -rw-r--r-- | lib/classes/forms/Input.php | 11 | ||||
| -rw-r--r-- | lib/classes/forms/Part.php | 2 |
5 files changed, 83 insertions, 9 deletions
diff --git a/lib/classes/forms/Captcha.php b/lib/classes/forms/Captcha.php new file mode 100644 index 0000000..c01b702 --- /dev/null +++ b/lib/classes/forms/Captcha.php @@ -0,0 +1,29 @@ +<?php + +namespace Studip\Forms; + +use CaptchaChallenge; + +/** + * The Text class represents a part of a form that displays a captcha. + */ +class Captcha extends Fieldset +{ + private CaptchaInput $captcha_input; + + public function __construct() + { + parent::__construct(_('Bitte bestätigen Sie, dass Sie kein Roboter sind')); + + $captchaInput = new CaptchaInput('altcha', $this->legend, null); + $captchaInput->setStoringFunction(function (string $payload) { + $json = CaptchaChallenge::decodePayload($payload); + + CaptchaChallenge::create([ + 'salt' => $json['salt'], + 'number' => $json['number'], + ]); + }); + $this->addInput($captchaInput); + } +} diff --git a/lib/classes/forms/CaptchaInput.php b/lib/classes/forms/CaptchaInput.php new file mode 100644 index 0000000..6476f87 --- /dev/null +++ b/lib/classes/forms/CaptchaInput.php @@ -0,0 +1,38 @@ +<?php + +namespace Studip\Forms; + +use CaptchaChallenge; +use URLHelper; + +/** + * The Text class represents a part of a form that displays a captcha. + */ +final class CaptchaInput extends Input +{ + public function hasValidation(): bool + { + return true; + } + + public function getValidationCallback(): callable + { + return fn($value) => \CaptchaChallenge::validatePayload($value); + } + + public function render(): string + { + return sprintf( + '<captcha-input challenge-url="%s" v-model="%s" auto="onload"></captcha-input>', + URLHelper::getLink('dispatch.php/captcha/challenge', [], true), + htmlReady($this->name) + ); + } + + public function renderWithCondition(): string + { + return $this->render(); + } + + +} diff --git a/lib/classes/forms/Form.php b/lib/classes/forms/Form.php index fa0422e..9c22c4f 100644 --- a/lib/classes/forms/Form.php +++ b/lib/classes/forms/Form.php @@ -297,6 +297,8 @@ class Form extends Part \PageLayout::postSuccess($this->success_message); } page_close(); + //This indicates that the form has been stored successfully. + echo "STUDIPFORM_STORE_SUCCESS"; die(); } } @@ -309,7 +311,7 @@ class Form extends Part //verify the user input: $output = []; foreach ($this->getAllInputs() as $input) { - if ($input->validate) { + if ($input->hasValidation()) { $callback = $input->getValidationCallback(); $value = $this->getStorableValueFromRequest($input); $valid = $callback($value, $input); @@ -317,7 +319,7 @@ class Form extends Part $output[$input->getName()] = [ 'name' => $input->getName(), 'label' => $input->getTitle(), - 'error' => $callback($value, $input) + 'error' => $valid, ]; } } @@ -396,7 +398,7 @@ class Form extends Part $stored = 0; foreach ($this->getAllInputs() as $input) { - if ($input->validate) { + if ($input->hasValidation()) { $callback = $input->getValidationCallback(); $value = $this->getStorableValueFromRequest($input); $valid = $callback($value, $input); @@ -450,7 +452,7 @@ class Form extends Part /** * Returns all the Part objects like Fieldsets as an array. - * @return array + * @return Part[] */ public function getParts() : array { @@ -492,7 +494,7 @@ class Form extends Part /** * Renders the whole form as a string. * @return string - * @throws \Flexi_TemplateNotFoundException + * @throws \Flexi\TemplateNotFoundException */ public function render() { diff --git a/lib/classes/forms/Input.php b/lib/classes/forms/Input.php index 9d2ad32..ef506e9 100644 --- a/lib/classes/forms/Input.php +++ b/lib/classes/forms/Input.php @@ -150,12 +150,17 @@ abstract class Input } /** - * Returns the value of this input. - * @return null + * Returns the value of this input. If $this->value is a callable this->getValue() returns the computed result. + * @return mixed */ public function getValue() { - return $this->value; + if (is_callable($this->value)) { + $callable = $this->value; + return $callable(); + } else { + return $this->value; + } } /** diff --git a/lib/classes/forms/Part.php b/lib/classes/forms/Part.php index fdca8f5..779cab7 100644 --- a/lib/classes/forms/Part.php +++ b/lib/classes/forms/Part.php @@ -139,7 +139,7 @@ abstract class Part /** * Recursively returns all Input elements attached to this Part object or any child Parts. - * @return array + * @return Input[] */ public function getAllInputs() { |
