Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- #-----------------------------------------------------#
- # ********* ROTORCMS ********* #
- # Made by : VANTUZ #
- # E-mail : visavi.net@mail.ru #
- # Site : http://pizdec.ru #
- # WAP-Site : http://visavi.net #
- # ICQ : 36-44-66 #
- # Вы не имеете право вносить изменения в код скрипта #
- # для его дальнейшего распространения #
- #-----------------------------------------------------#
- if (!defined('BASEDIR')) {
- header('Location:../index.php');
- exit;
- }
- class Validation{
- /*
- * @errors array
- */
- public $errors = array();
- /*
- * @the validation rules array
- */
- private $validation_rules = array();
- /**
- *
- * @the constructor, duh!
- *
- */
- public function __construct()
- {
- }
- /**
- *
- * @run the validation rules
- *
- * @access public
- *
- */
- public function run()
- {
- /*** set the vars ***/
- foreach( new ArrayIterator($this->validation_rules) as $var=>$opt)
- {
- switch($opt['type'])
- {
- case 'string':
- $this->validateString($opt['var'], $opt['label'], $opt['min'], $opt['max'], $opt['required']);
- break;
- case 'numeric':
- $this->validateNumeric($opt['var'], $opt['label'], $opt['min'], $opt['max'], $opt['required']);
- break;
- case 'equal':
- $this->validateEqual($opt['var'], $opt['label']);
- break;
- case 'email':
- $this->validateEmail($opt['var'], $opt['label'], $opt['required']);
- break;
- case 'url':
- $this->validateUrl($opt['var'], $opt['label'], $opt['required']);
- break;
- case 'float':
- $this->validateFloat($opt['var'], $opt['label'], $opt['required']);
- break;
- case 'bool':
- $this->validateBool($opt['var'], $opt['label'], $opt['required']);
- break;
- }
- }
- $total_errors = count($this->errors);
- if (empty($total_errors)){
- return true;
- }
- }
- /**
- *
- * @add a rule to the validation rules array
- *
- * @access public
- *
- * @param string $var The variable name
- *
- * @param string $label The label of variable
- *
- * @param string $type The type of variable
- *
- * @param bool $required If the field is required
- *
- * @param int $min The minimum length or range
- *
- * @param int $max the maximum length or range
- *
- */
- public function addRule($var, $label, $type, $required=false, $min=0, $max=0)
- {
- $this->validation_rules[] = array('var'=>$var, 'label'=>$label, 'type'=>$type, 'required'=>$required, 'min'=>$min, 'max'=>$max);
- var_dump($this->validation_rules);
- /*** allow chaining ***/
- return $this;
- }
- /**
- *
- * @validate a string
- *
- * @access private
- *
- * @param string $var The variable name
- *
- * @param string $label The label of variable
- *
- * @param int $min the minimum string length
- *
- * @param int $max The maximum string length
- *
- * @param bool $required
- *
- */
- private function validateString($var, $label, $min=0, $max=0, $required=false)
- {
- if($required==false && utf_strlen($var) == 0)
- {
- return true;
- }
- if(isset($var))
- {
- if(utf_strlen($var) < $min)
- {
- $this->errors[] = 'Ошибка! Значение поля "'.$label.'" слишком короткое! (Не менее '.$min.' симв.)';
- }
- elseif(utf_strlen($var) > $max)
- {
- $this->errors[] = 'Ошибка! Значение поля "'.$label.'" слишком длинное! (Не более '.$max.' симв.)';
- }
- elseif(!is_string($var))
- {
- $this->errors[] = 'Ошибка! Значение поля "'.$label.'" является недопустимой строкой!';
- }
- }
- }
- /**
- *
- * @validate an number
- *
- * @access private
- *
- * @param string $var the variable name
- *
- * @param string $label The label of variable
- *
- * @param int $min The minimum number range
- *
- * @param int $max The maximum number range
- *
- * @param bool $required
- *
- */
- private function validateNumeric($var, $label, $min=0, $max=0, $required=false)
- {
- if($required==false && utf_strlen($var) == 0)
- {
- return true;
- }
- if(filter_var($var, FILTER_VALIDATE_INT, array("options" => array("min_range"=>$min, "max_range"=>$max)))===FALSE)
- {
- $this->errors[] = 'Ошибка! Значение поля "'.$label.'" должно быть в числовом диапазоне от '.$min.' до '.$max.'!';
- }
- }
- public function validateEqual($var, $label)
- {
- if (!is_array($var) || $var[0] != $var[1]) {
- $this->errors[] = 'Ошибка! Не совпадают данные поля "'.$label.'"!';
- }
- }
- /**
- *
- * @validate a floating point number
- *
- * @access private
- *
- * @param $var The variable name
- *
- * @param string $label The label of variable
- *
- * @param bool $required
- */
- private function validateFloat($var, $label, $required=false)
- {
- if($required==false && utf_strlen($var) == 0)
- {
- return true;
- }
- if(filter_var($var, FILTER_VALIDATE_FLOAT) === false)
- {
- $this->errors[] = 'Ошибка! Значение поля "'.$label.'" является недопустимым числом!';
- }
- }
- /**
- *
- * @validate a url
- *
- * @access private
- *
- * @param string $var The variable name
- *
- * @param string $label The label of variable
- *
- * @param bool $required
- *
- */
- private function validateUrl($var, $label, $required=false)
- {
- if($required==false && utf_strlen($var) == 0)
- {
- return true;
- }
- if (!preg_match('#^http://([а-яa-z0-9_\-\.])+(\.([а-яa-z0-9\/])+)+$#u', $var))
- //if(filter_var($var, FILTER_VALIDATE_URL) === FALSE)
- {
- $this->errors[] = 'Ошибка! Значение поля "'.$label.'" не является допустимым URL!';
- }
- }
- /**
- *
- * @validate an email address
- *
- * @access private
- *
- * @param string $var The variable name
- *
- * @param string $label The label of variable
- *
- * @param bool $required
- *
- */
- private function validateEmail($var, $label, $required=false)
- {
- if($required==false && utf_strlen($var) == 0)
- {
- return true;
- }
- if(!preg_match('#^([a-z0-9_\-\.])+\@([a-z0-9_\-\.])+(\.([a-z0-9])+)+$#', $var))
- //if(filter_var($var, FILTER_VALIDATE_EMAIL) === FALSE)
- {
- $this->errors[] = 'Ошибка! Значение поля "'.$label.'" не является допустимым адресом электронной почты!';
- }
- }
- /**
- * @validate a boolean
- *
- * @access private
- *
- * @param string $var the variable name
- *
- * @param string $label The label of variable
- *
- * @param bool $required
- *
- */
- private function validateBool($var, $label, $required=false)
- {
- if($required==false && utf_strlen($var) == 0)
- {
- return true;
- }
- filter_var($var, FILTER_VALIDATE_BOOLEAN);
- {
- $this->errors[] = 'Ошибка! Значение поля "'.$label.'" недопустимое (true или false)!';
- }
- }
- } /*** end of class ***/
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement