engelsystem/includes/sys_page.php

138 lines
3.2 KiB
PHP
Raw Normal View History

2011-06-02 00:48:29 +02:00
<?php
2011-12-21 22:20:06 +01:00
/**
* Leitet den Browser an die übergebene URL weiter und hält das Script an.
*/
function redirect($to) {
2013-10-13 00:52:44 +02:00
header("Location: " . $to, true, 302);
2016-08-22 19:32:54 +02:00
raw_output("");
2011-12-21 22:20:06 +01:00
}
/**
* Echoes given output and dies.
2016-08-22 20:40:31 +02:00
*
* @param String $output
*/
function raw_output($output) {
echo $output;
die();
}
2016-09-27 17:24:18 +02:00
/**
* 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])) {
2016-09-27 17:24:18 +02:00
return new ValidationResult($null_allowed, null);
}
2016-09-27 17:24:18 +02:00
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))->getTimestamp());
}
if ($null_allowed) {
2016-09-27 17:24:18 +02:00
return new ValidationResult(true, null);
}
2016-09-27 17:24:18 +02:00
error($error_message);
return new ValidationResult(false, null);
}
2011-12-21 22:20:06 +01:00
/**
* Gibt den gefilterten REQUEST Wert ohne Zeilenumbrüche zurück
*/
function strip_request_item($name) {
2013-10-13 00:52:44 +02:00
return strip_item($_REQUEST[$name]);
}
2011-12-21 22:20:06 +01:00
/**
2013-10-13 00:52:44 +02:00
* Testet, ob der angegebene REQUEST Wert ein Integer ist, bzw.
* eine ID sein könnte.
2011-12-21 22:20:06 +01:00
*/
function test_request_int($name) {
2016-08-22 20:40:31 +02:00
if (isset($_REQUEST[$name])) {
2013-10-13 00:52:44 +02:00
return preg_match("/^[0-9]*$/", $_REQUEST[$name]);
2016-08-22 20:40:31 +02:00
}
2013-10-13 00:52:44 +02:00
return false;
2011-12-21 22:20:06 +01:00
}
/**
* Gibt den gefilterten REQUEST Wert mit Zeilenumbrüchen zurück
*/
2011-06-03 11:09:25 +02:00
function strip_request_item_nl($name) {
2013-10-13 00:52:44 +02:00
return preg_replace("/([^\p{L}\p{S}\p{P}\p{Z}\p{N}+\n]{1,})/ui", '', strip_tags($_REQUEST[$name]));
2011-12-21 22:20:06 +01:00
}
/**
* Entfernt unerwünschte Zeichen
*/
function strip_item($item) {
2013-10-13 00:52:44 +02:00
return preg_replace("/([^\p{L}\p{S}\p{P}\p{Z}\p{N}+]{1,})/ui", '', strip_tags($item));
2011-12-21 22:20:06 +01:00
}
2011-12-26 15:55:17 +01:00
/**
* Überprüft eine E-Mail-Adresse.
*/
function check_email($email) {
return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
2011-12-26 15:55:17 +01:00
}
2016-09-27 17:24:18 +02:00
class ValidationResult {
2016-09-29 12:08:12 +02:00
private $valid;
2016-09-27 17:24:18 +02:00
private $value;
/**
* Constructor.
*
2016-09-29 12:08:12 +02:00
* @param boolean $valid
2016-09-27 17:24:18 +02:00
* Is the value valid?
* @param * $value
* The validated value
*/
2016-09-29 12:08:12 +02:00
public function ValidationResult($valid, $value) {
$this->ok = $valid;
2016-09-27 17:24:18 +02:00
$this->value = $value;
}
/**
* Is the value valid?
*/
public function isOk() {
return $this->ok;
}
/**
* The parsed/validated value.
*/
public function getValue() {
return $this->value;
}
}
?>