* lib - class.validator > fix validator class for required and non required in class.validator when many parameters are sent.. the validator if you used required only validate the last one.. fix so if this is not required and the first one is required, let the validation pass or do not pass any * lib - class.validator > support for GET and POST autodetection validator allows to send and assigation of the array vars validations this allows to you to use also PUT for api calls.. autodetection of the type, if will be GET or POST array if no speciall call is given.. * lib - class.model > mkid autoincrement string/key for ODBC/sql ansi DBMS Create PICCORO's crazy ID posta is not crazy.. allows to use autoincremnet no matter if database support it or not this permit that you can create a simpel nont dependendant database. with this you identify several things, full date, where when inserted it sorts itself since it will never give a smaller number * editor config, set to 0 spaces ini files * ignore shit of mocosoft vscode
196 lines
5.6 KiB
PHP
196 lines
5.6 KiB
PHP
<?php
|
|
/* Copyright (c) by Hugo Leisink <hugo@leisink.net>
|
|
* This file is part of the Banshee PHP framework
|
|
* https://www.banshee-php.org/
|
|
*
|
|
* Licensed under The MIT License
|
|
*/
|
|
|
|
class validator {
|
|
private $msg = null;
|
|
private $messages = array(
|
|
"boolean" => "El campo [label] debería contener un booleano",
|
|
"charset" => "El campo [label] contiene caracteres no válidos.",
|
|
"email" => "El campo [label] contiene una dirección de correo electrónico no válida.",
|
|
"enum" => "El campo [label] es invalido.",
|
|
"integer" => "The field [label] should contain an integer.",
|
|
"float" => "El campo [label] debe contener un número decimal.",
|
|
"intmin" => "El valor de [label] debe ser al menos [min].",
|
|
"intmax" => "El valor de [label] debe ser como máximo [max].",
|
|
"pattern" => "El campo [label] no coincide con el patrón requerido.",
|
|
"required" => "El campo [label] no puede estar vacío.",
|
|
"timestamp" => "El campo [label] contiene una marca de tiempo no válida.",
|
|
"minlen" => "La longitud de [label] debe ser al menos [minlen].",
|
|
"maxlen" => "La longitud de [label] no debe exceder de [maxlen].."
|
|
);
|
|
private $arrayvars = null;
|
|
|
|
/* Constructor
|
|
*
|
|
* INPUT: object view
|
|
* OUTPUT: -
|
|
* ERROR: -
|
|
*/
|
|
public function __construct($arrayvars = null) {
|
|
if(is_array($arrayvars) and count($arrayvars) > 0)
|
|
$this->arrayvars = $arrayvars;
|
|
elseif(is_array($_POST) and count($_POST) > 0)
|
|
$this->arrayvars = $_POST;
|
|
else
|
|
$this->arrayvars = $_GET;
|
|
}
|
|
|
|
/* Add validation feedback to output
|
|
*
|
|
* INPUT: int message index, array message replacements
|
|
* OUTPUT: -
|
|
* ERROR: -
|
|
*/
|
|
private function add_message($msg_idx, $replacements) {
|
|
if (isset($replacements["message"])) {
|
|
$message = $replacements["message"];
|
|
} else {
|
|
$message = $this->messages[$msg_idx];
|
|
foreach ($replacements as $from => $to) {
|
|
$message = str_replace("[".$from."]", $to, $message);
|
|
}
|
|
}
|
|
|
|
$this->msg = $message;
|
|
}
|
|
|
|
private function _required($name,$rule) {
|
|
$result = false;
|
|
if (array_key_exists($name,$this->arrayvars)) {
|
|
if(trim($this->arrayvars[$name]) != '')
|
|
$result = true;
|
|
else
|
|
$this->add_message("required", $rule);
|
|
}
|
|
else
|
|
$this->add_message("required", $rule);
|
|
//*/ trigger_error($rule["required"].' rsulta para '.$name.' en '.$result, E_USER_WARNING);
|
|
return $result;
|
|
}
|
|
|
|
/* Start validation process
|
|
*
|
|
* INPUT: array pattern to validate POST data
|
|
* OUTPUT: boolean validation oke
|
|
* ERROR: -
|
|
*/
|
|
public function execute($pattern,$arrayvars = null) {
|
|
|
|
if(is_array($arrayvars) and count($arrayvars) > 0)
|
|
$this->arrayvars = $arrayvars;
|
|
elseif(is_array($_POST) and count($_POST) > 0)
|
|
$this->arrayvars = $_POST;
|
|
else
|
|
$this->arrayvars = $_GET;
|
|
$result = true;
|
|
|
|
foreach ($pattern as $name => $rule) {
|
|
if (isset($rule["label"]) == false) {
|
|
$rule["label"] = $name;
|
|
}
|
|
|
|
if($rule["required"] == true) {
|
|
$result = $this->_required($name,$rule);
|
|
}
|
|
|
|
switch ($rule["type"]) {
|
|
case "boolean":
|
|
if (($this->arrayvars[$name] != null) && ($this->arrayvars[$name] != "On")) {
|
|
$this->add_message("boolean", $rule);
|
|
$result = false;
|
|
}
|
|
break;
|
|
case "email":
|
|
if ($this->arrayvars[$name] != "") {
|
|
if (valid_email($this->arrayvars[$name]) == false) {
|
|
$this->add_message("email", $rule);
|
|
$result = false;
|
|
}
|
|
}
|
|
break;
|
|
case "enum":
|
|
if ($this->arrayvars[$name] != "") {
|
|
if (in_array($this->arrayvars[$name], $rule["values"]) == false) {
|
|
$this->add_message("enum", $rule);
|
|
$result = false;
|
|
}
|
|
}
|
|
break;
|
|
case "integer":
|
|
if (valid_input($this->arrayvars[$name], VALIDATE_NUMBERS) == false) {
|
|
$this->add_message("integer", $rule);
|
|
$result = false;
|
|
} else {
|
|
if (isset($rule["min"])) {
|
|
if ($this->arrayvars[$name] < $rule["min"]) {
|
|
$this->add_message("intmin", $rule);
|
|
$result = false;
|
|
}
|
|
}
|
|
|
|
if (isset($rule["max"])) {
|
|
if ($this->arrayvars[$name] > $rule["max"]) {
|
|
$this->add_message("intmax", $rule);
|
|
$result = false;
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case "float":
|
|
if (is_numeric($this->arrayvars[$name]) == false) {
|
|
$this->add_message("float", $rule);
|
|
$result = false;
|
|
}
|
|
break;
|
|
case "string":
|
|
if (isset($rule["minlen"])) {
|
|
if (strlen($this->arrayvars[$name]) < $rule["minlen"]) {
|
|
$this->add_message("minlen", $rule);
|
|
$result = false;
|
|
}
|
|
}
|
|
|
|
if (isset($rule["maxlen"])) {
|
|
if (strlen($this->arrayvars[$name]) > $rule["maxlen"]) {
|
|
$this->add_message("maxlen", $rule);
|
|
$result = false;
|
|
}
|
|
}
|
|
|
|
if (isset($rule["charset"])) {
|
|
if (valid_input($this->arrayvars[$name], $rule["charset"]) == false) {
|
|
$this->add_message("charset", $rule);
|
|
$result = false;
|
|
}
|
|
}
|
|
|
|
if (isset($rule["pattern"])) {
|
|
if (preg_match("/".$rule["pattern"]."/", $this->arrayvars[$name]) == false) {
|
|
$this->add_message("pattern", $rule);
|
|
$result = false;
|
|
}
|
|
}
|
|
break;
|
|
case "timestamp":
|
|
if ($this->arrayvars[$name] != "") {
|
|
if (valid_timestamp($this->arrayvars[$name]) == false) {
|
|
$this->add_message("timestamp", $rule);
|
|
$result = false;
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
$this->view->add_message("Tipo de valor no válido para ".$rule["label"].".");
|
|
}
|
|
}
|
|
|
|
return [$result, $this->msg];
|
|
}
|
|
}
|
|
?>
|