summaryrefslogtreecommitdiff
path: root/includes/sys_page.php
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2016-09-27 17:24:18 +0200
committermsquare <msquare@notrademark.de>2016-09-27 17:49:39 +0200
commit45bbf95972777e9499996d56a873c2a304815b3f (patch)
treee4acec80f969e1d220f7ec1a1e975c8ce3d4b541 /includes/sys_page.php
parentf82a3fb1d81c4800bbe343231479ee29f935b190 (diff)
fix settings validation
Diffstat (limited to 'includes/sys_page.php')
-rw-r--r--includes/sys_page.php74
1 files changed, 74 insertions, 0 deletions
diff --git a/includes/sys_page.php b/includes/sys_page.php
index 6863c517..67da83c7 100644
--- a/includes/sys_page.php
+++ b/includes/sys_page.php
@@ -19,6 +19,46 @@ function raw_output($output) {
}
/**
+ * Checks if given request item (name) can be parsed to a date.
+ * If not parsable, given error message is put into msg() and null is returned.
+ *
+ * @param string $input
+ * String to be parsed into a date.
+ * @param string $error_message
+ * the error message displayed if $input is not parsable
+ * @param boolean $null_allowed
+ * is a null value allowed?
+ * @return ValidationResult containing the parsed date
+ */
+function check_request_date($name, $error_message = null, $null_allowed = false) {
+ if (! isset($_REQUEST[$name]))
+ return new ValidationResult($null_allowed, null);
+ return check_date($_REQUEST[$name], $error_message, $null_allowed);
+}
+
+/**
+ * Checks if given string can be parsed to a date.
+ * If not parsable, given error message is put into msg() and null is returned.
+ *
+ * @param string $input
+ * String to be parsed into a date.
+ * @param string $error_message
+ * the error message displayed if $input is not parsable
+ * @param boolean $null_allowed
+ * is a null value allowed?
+ * @return ValidationResult containing the parsed date
+ */
+function check_date($input, $error_message = null, $null_allowed = false) {
+ if (DateTime::createFromFormat("Y-m-d", trim($input)))
+ return new ValidationResult(true, DateTime::createFromFormat("Y-m-d", trim($input)));
+ if ($null_allowed)
+ return new ValidationResult(true, null);
+
+ error($error_message);
+ return new ValidationResult(false, null);
+}
+
+/**
* Gibt den gefilterten REQUEST Wert ohne Zeilenumbrüche zurück
*/
function strip_request_item($name) {
@@ -57,4 +97,38 @@ function check_email($email) {
return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
}
+class ValidationResult {
+
+ private $ok;
+
+ private $value;
+
+ /**
+ * Constructor.
+ *
+ * @param boolean $ok
+ * Is the value valid?
+ * @param * $value
+ * The validated value
+ */
+ public function ValidationResult($ok, $value) {
+ $this->ok = $ok;
+ $this->value = $value;
+ }
+
+ /**
+ * Is the value valid?
+ */
+ public function isOk() {
+ return $this->ok;
+ }
+
+ /**
+ * The parsed/validated value.
+ */
+ public function getValue() {
+ return $this->value;
+ }
+}
+
?>