2022-06-06 21:30:34 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class Validator{
|
|
|
|
|
|
|
|
|
|
private $_field_data = array();
|
|
|
|
|
private $_error_messages = array();
|
|
|
|
|
private $_error_prefix = '<p>';
|
|
|
|
|
private $_error_suffix = '</p>';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function __construct(){
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Установка полей валидации
|
2022-06-06 21:30:34 +00:00
|
|
|
|
*/
|
|
|
|
|
function set_rules($field, $label = '', $rules = ''){
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//Нет POST данных
|
2022-06-06 21:30:34 +00:00
|
|
|
|
if (sizeof($_POST) == 0){
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//Если правила валидации переданы в виде массива
|
2022-06-06 21:30:34 +00:00
|
|
|
|
if(is_array($field)){
|
|
|
|
|
|
|
|
|
|
foreach ($field as $row){
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//Если не установлено поле валидации или правила валидации,
|
|
|
|
|
//то пропускаем это поле
|
2022-06-06 21:30:34 +00:00
|
|
|
|
if ( ! isset($row['field']) OR ! isset($row['rules'])){
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//Если название поля не передано используем имя поля
|
2022-06-06 21:30:34 +00:00
|
|
|
|
$label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->set_rules($row['field'], $label, $row['rules']);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//Правила валидации должны быть переданы в виде массива,
|
|
|
|
|
//а поле валидации строкой
|
2022-06-06 21:30:34 +00:00
|
|
|
|
if ( ! is_string($field) OR ! is_array($rules) OR $field == ''){
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//Если название поля не передано используем имя поля
|
2022-06-06 21:30:34 +00:00
|
|
|
|
$label = ($label == '') ? $field : $label;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->_field_data[$field] = array(
|
|
|
|
|
'field' => $field,
|
|
|
|
|
'label' => $label,
|
|
|
|
|
'rules' => $rules,
|
|
|
|
|
'postdata' => NULL,
|
|
|
|
|
'error' => ''
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Валидация данных
|
2022-06-06 21:30:34 +00:00
|
|
|
|
*/
|
|
|
|
|
function run(){
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//Нет POST данных
|
2022-06-06 21:30:34 +00:00
|
|
|
|
if (sizeof($_POST) == 0){
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//Если нет заданных полей для валидации
|
2022-06-06 21:30:34 +00:00
|
|
|
|
if(sizeof($this->_field_data) == 0){
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($this->_field_data as $field => $row){
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//Получаем POST данные для установленных полей валидации
|
2022-06-06 21:30:34 +00:00
|
|
|
|
//if (isset($_POST[$field])){
|
|
|
|
|
|
|
|
|
|
$this->_field_data[$field]['postdata'] = (isset($_POST[$field]))? $_POST[$field]: NULL;
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//Проверка правил валидации
|
2022-06-06 21:30:34 +00:00
|
|
|
|
$this->checkrule($row,$this->_field_data[$field]['postdata']);
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$total_errors = sizeof($this->_error_messages);
|
|
|
|
|
|
|
|
|
|
if($total_errors == 0){
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Проверка правил валидации
|
2022-06-06 21:30:34 +00:00
|
|
|
|
*/
|
|
|
|
|
function checkrule($field,$postdata){
|
|
|
|
|
|
|
|
|
|
if(is_array($postdata)){
|
|
|
|
|
|
|
|
|
|
foreach($postdata as $key => $val){
|
|
|
|
|
|
|
|
|
|
$this->checkrule($field,$val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach($field['rules'] as $rule => $message){
|
|
|
|
|
|
|
|
|
|
$param = FALSE;
|
|
|
|
|
|
|
|
|
|
if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
|
|
|
|
|
{
|
2023-01-10 16:29:32 +00:00
|
|
|
|
$rule = $match[1]; //Правило валидации
|
|
|
|
|
$param = $match[2]; //Параметры
|
2022-06-06 21:30:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//если это правило не входит с состав библиотеки
|
2022-06-06 21:30:34 +00:00
|
|
|
|
if(!method_exists($this, $rule)){
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//то будем считать, что это стандартная функция PHP
|
|
|
|
|
//которая принимает только один входной параметр
|
2022-06-06 21:30:34 +00:00
|
|
|
|
if(function_exists($rule)){
|
|
|
|
|
|
|
|
|
|
$result = $rule($postdata);
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//Если функция возвращает булевое значение (TRUR|FALSE),
|
|
|
|
|
//то мы не изменяем переданные POST данные, иначе записываем
|
|
|
|
|
//отформатированные данные
|
2022-06-06 21:30:34 +00:00
|
|
|
|
$postdata = (is_bool($result)) ? $postdata : $result;
|
|
|
|
|
$this->set_field_postdata($field['field'],$postdata);
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
|
|
|
|
|
$result = $this->$rule($postdata,$param);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$postdata = (is_bool($result)) ? $postdata : $result;
|
|
|
|
|
$this->set_field_postdata($field['field'],$postdata);
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//если данные не прошли валидацию
|
2023-04-08 15:14:19 +00:00
|
|
|
|
if($result == FALSE && $message != ''){
|
2022-06-06 21:30:34 +00:00
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//Формируем сообщение об ошибке
|
2022-06-06 21:30:34 +00:00
|
|
|
|
$error = sprintf($message, $field['label']);
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
//Сохраняем сообщение об ошибке
|
2022-06-06 21:30:34 +00:00
|
|
|
|
$this->_field_data[$field['field']]['error'] = $error;
|
|
|
|
|
|
|
|
|
|
if ( ! isset($this->_error_messages[$field['field']])){
|
|
|
|
|
|
|
|
|
|
$this->_error_messages[$field['field']] = $error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Установка POST данных
|
2022-06-06 21:30:34 +00:00
|
|
|
|
*/
|
|
|
|
|
private function set_field_postdata($field,$postdata){
|
|
|
|
|
|
|
|
|
|
if(isset($this->_field_data[$field]['postdata'])){
|
|
|
|
|
|
|
|
|
|
$this->_field_data[$field]['postdata'] = $postdata;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Возвращает POST данные для нужного элемента
|
2022-06-06 21:30:34 +00:00
|
|
|
|
*/
|
|
|
|
|
function postdata($field){
|
|
|
|
|
|
|
|
|
|
if(isset($this->_field_data[$field]['postdata'])){
|
|
|
|
|
|
|
|
|
|
return $this->_field_data[$field]['postdata'];
|
|
|
|
|
}
|
|
|
|
|
else return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Очищает все POST данные
|
2022-06-06 21:30:34 +00:00
|
|
|
|
*/
|
|
|
|
|
function reset_postdata(){
|
|
|
|
|
|
|
|
|
|
$this->_field_data = array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Возвращает все сообщения об ошибках в виде строки
|
2022-06-06 21:30:34 +00:00
|
|
|
|
*/
|
|
|
|
|
function get_string_errors($prefix = '',$suffix = ''){
|
|
|
|
|
|
|
|
|
|
|
2023-04-08 15:14:19 +00:00
|
|
|
|
if (count($this->_error_messages) == 0){
|
2022-06-06 21:30:34 +00:00
|
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($prefix == '')
|
|
|
|
|
{
|
|
|
|
|
$prefix = $this->_error_prefix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($suffix == '')
|
|
|
|
|
{
|
|
|
|
|
$suffix = $this->_error_suffix;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
// Формируем строку с ошибками
|
2022-06-06 21:30:34 +00:00
|
|
|
|
$str = '';
|
|
|
|
|
foreach ($this->_error_messages as $val)
|
|
|
|
|
{
|
|
|
|
|
if ($val != '')
|
|
|
|
|
{
|
|
|
|
|
$str .= $prefix.$val.$suffix."\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Возвращает все сообщения об ошибках в виде строки
|
2022-06-06 21:30:34 +00:00
|
|
|
|
*/
|
|
|
|
|
function get_array_errors(){
|
|
|
|
|
|
|
|
|
|
return $this->_error_messages;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Возвращает сообщение об ошибка для указанного поля
|
2022-06-06 21:30:34 +00:00
|
|
|
|
* @param string
|
|
|
|
|
*/
|
|
|
|
|
function form_error($field){
|
|
|
|
|
|
|
|
|
|
if(isset($this->_error_messages[$field])){
|
|
|
|
|
|
|
|
|
|
return $this->_error_prefix.$this->_error_messages[$field].$this->_error_suffix;
|
|
|
|
|
}
|
|
|
|
|
else return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function set_error_delimiters($prefix = '<p>', $suffix = '</p>')
|
|
|
|
|
{
|
|
|
|
|
$this->_error_prefix = $prefix;
|
|
|
|
|
$this->_error_suffix = $suffix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Значение не может быть пустым
|
2022-06-06 21:30:34 +00:00
|
|
|
|
*
|
|
|
|
|
* @access public
|
|
|
|
|
* @param string
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
function required($str)
|
|
|
|
|
{
|
|
|
|
|
if ( ! is_array($str))
|
|
|
|
|
{
|
|
|
|
|
return (trim($str) == '') ? FALSE : TRUE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return ( ! empty($str));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Проверка поля на целое цисло
|
2022-06-06 21:30:34 +00:00
|
|
|
|
* @param string
|
|
|
|
|
*/
|
|
|
|
|
function integer($str){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return filter_var($str, FILTER_VALIDATE_INT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Проверка поля на цисло c плавающей точкой
|
2022-06-06 21:30:34 +00:00
|
|
|
|
* @param string
|
|
|
|
|
*/
|
|
|
|
|
function float($str){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return filter_var($str, FILTER_VALIDATE_FLOAT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Валидация URL
|
2022-06-06 21:30:34 +00:00
|
|
|
|
* @param string
|
|
|
|
|
*/
|
|
|
|
|
function valid_url($str){
|
|
|
|
|
|
|
|
|
|
return filter_var($str, FILTER_VALIDATE_URL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Валидация email-адреса
|
2022-06-06 21:30:34 +00:00
|
|
|
|
* @param string
|
|
|
|
|
*/
|
|
|
|
|
function valid_email($str){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return filter_var($str, FILTER_VALIDATE_EMAIL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Валидация IP-адреса
|
2022-06-06 21:30:34 +00:00
|
|
|
|
* @param string
|
|
|
|
|
*/
|
|
|
|
|
function valid_ip($str){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return filter_var($str, FILTER_VALIDATE_IP);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Match one field to another
|
|
|
|
|
*
|
|
|
|
|
* @access public
|
|
|
|
|
* @param string
|
|
|
|
|
* @param field
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
function matches($str, $field)
|
|
|
|
|
{
|
|
|
|
|
if ( ! isset($_POST[$field]))
|
|
|
|
|
{
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$field = $_POST[$field];
|
|
|
|
|
|
|
|
|
|
return ($str !== $field) ? FALSE : TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Только буквы латинского алфавита
|
2022-06-06 21:30:34 +00:00
|
|
|
|
*
|
|
|
|
|
* @access public
|
|
|
|
|
* @param string
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
function alpha($str)
|
|
|
|
|
{
|
|
|
|
|
return ( ! preg_match("/^([a-z])+$/i", $str)) ? FALSE : TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Проверка капчи
|
2022-06-06 21:30:34 +00:00
|
|
|
|
*
|
|
|
|
|
* @access public
|
|
|
|
|
* @param string
|
|
|
|
|
* @param string
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
function valid_captcha($str,$name){
|
|
|
|
|
|
|
|
|
|
return (!empty($_SESSION[$name]) && $_SESSION[$name] == $str)? TRUE: FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Проверка даты
|
2022-06-06 21:30:34 +00:00
|
|
|
|
*
|
|
|
|
|
* @access public
|
|
|
|
|
* @param string
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
function valid_date($str){
|
|
|
|
|
|
|
|
|
|
$stamp = strtotime( $str );
|
|
|
|
|
|
|
|
|
|
if (!is_numeric($stamp)){
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$month = date( 'm', $stamp );
|
|
|
|
|
$day = date( 'd', $stamp );
|
|
|
|
|
$year = date( 'Y', $stamp );
|
|
|
|
|
|
|
|
|
|
return checkdate($month, $day, $year);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function unique($str,$fields){
|
|
|
|
|
|
|
|
|
|
list($table,$field) = explode('.',$fields);
|
|
|
|
|
|
|
|
|
|
$result = mysql_query("SELECT COUNT(*) AS count FROM `".$table."` WHERE ".mysql_real_escape_string($field)."='".mysql_real_escape_string($str)."'");
|
|
|
|
|
|
|
|
|
|
$myrow = mysql_fetch_assoc($result);
|
|
|
|
|
|
|
|
|
|
return $myrow['count'] == 0;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|