diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:21:16 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:21:16 +0000 |
commit | 2e582fe0b8b6a8e67982ddb84935db1bd3b401fe (patch) | |
tree | dd511b321f308264952cffb005a4288ea4e478e6 /library/Graphite/Web/Form/Validator | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-graphite-2e582fe0b8b6a8e67982ddb84935db1bd3b401fe.tar.xz icingaweb2-module-graphite-2e582fe0b8b6a8e67982ddb84935db1bd3b401fe.zip |
Adding upstream version 1.2.2.upstream/1.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Graphite/Web/Form/Validator')
3 files changed, 93 insertions, 0 deletions
diff --git a/library/Graphite/Web/Form/Validator/CustomErrorMessagesValidator.php b/library/Graphite/Web/Form/Validator/CustomErrorMessagesValidator.php new file mode 100644 index 0000000..893a5b7 --- /dev/null +++ b/library/Graphite/Web/Form/Validator/CustomErrorMessagesValidator.php @@ -0,0 +1,42 @@ +<?php + +namespace Icinga\Module\Graphite\Web\Form\Validator; + +use Zend_Validate_Abstract; + +/** + * Provides an easy way to implement validators with custom error messages + * + * TODO(ak): move to framework(?) + */ +abstract class CustomErrorMessagesValidator extends Zend_Validate_Abstract +{ + /** + * Constructor + */ + public function __construct() + { + $this->_messageTemplates = ['CUSTOM_ERROR' => '']; + } + + public function isValid($value) + { + $errorMessage = $this->validate($value); + if ($errorMessage === null) { + return true; + } + + $this->setMessage($errorMessage, 'CUSTOM_ERROR'); + $this->_error('CUSTOM_ERROR'); + return false; + } + + /** + * Validate the given value and return an error message if it's invalid + * + * @param string $value + * + * @return string|null + */ + abstract protected function validate($value); +} diff --git a/library/Graphite/Web/Form/Validator/HttpUserValidator.php b/library/Graphite/Web/Form/Validator/HttpUserValidator.php new file mode 100644 index 0000000..d5f4a86 --- /dev/null +++ b/library/Graphite/Web/Form/Validator/HttpUserValidator.php @@ -0,0 +1,30 @@ +<?php + +namespace Icinga\Module\Graphite\Web\Form\Validator; + +use Zend_Validate_Abstract; + +/** + * Validates http basic authn user names + * + * TODO(ak): move to Icinga Web 2 + */ +class HttpUserValidator extends Zend_Validate_Abstract +{ + /** + * Constructor + */ + public function __construct() + { + $this->_messageTemplates = ['HAS_COLON' => mt('graphite', 'The username must not contain colons.')]; + } + + public function isValid($value) + { + $hasColon = false !== strpos($value, ':'); + if ($hasColon) { + $this->_error('HAS_COLON'); + } + return ! $hasColon; + } +} diff --git a/library/Graphite/Web/Form/Validator/MacroTemplateValidator.php b/library/Graphite/Web/Form/Validator/MacroTemplateValidator.php new file mode 100644 index 0000000..8ff4e3c --- /dev/null +++ b/library/Graphite/Web/Form/Validator/MacroTemplateValidator.php @@ -0,0 +1,21 @@ +<?php + +namespace Icinga\Module\Graphite\Web\Form\Validator; + +use Icinga\Module\Graphite\Util\MacroTemplate; +use InvalidArgumentException; + +/** + * Validates Icinga-style macro templates + */ +class MacroTemplateValidator extends CustomErrorMessagesValidator +{ + protected function validate($value) + { + try { + new MacroTemplate($value); + } catch (InvalidArgumentException $e) { + return $e->getMessage(); + } + } +} |