1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-07-03 06:55:03 +03:00

Валидатор каптчи и сравнения

This commit is contained in:
Mzhelskiy Maxim 2012-03-21 22:40:04 +04:00
parent 6d1fd3d6e8
commit 4a4c7dc235
5 changed files with 224 additions and 0 deletions

View file

@ -47,6 +47,18 @@ abstract class ModuleValidate_EntityValidator extends Entity {
* @var null|array
*/
public $on=null;
/**
* Объект текущей сущности, которая проходит валидацию
*
* @var null|Entity
*/
protected $oEntityCurrent=null;
/**
* Объект текущей сущности, которая проходит валидацию
*
* @var null|string
*/
protected $sFieldCurrent=null;
/**
* Основной метод валидации
@ -121,6 +133,7 @@ abstract class ModuleValidate_EntityValidator extends Entity {
} else {
$aFields=$this->fields;
}
$this->oEntityCurrent=$oEntity;
/**
* Запускаем валидацию для каждого поля
*/
@ -140,6 +153,7 @@ abstract class ModuleValidate_EntityValidator extends Entity {
* @return bool
*/
public function validateEntityField($oEntity,$sField) {
$this->sFieldCurrent=$sField;
/**
* Получаем значение поля у сущности через геттер
*/
@ -155,5 +169,17 @@ abstract class ModuleValidate_EntityValidator extends Entity {
return true;
}
}
/**
* Возвращает значение поля текущей сущности
*
* @param $sField
* @return mixed|null
*/
protected function getValueOfCurrentEntity($sField) {
if ($this->oEntityCurrent) {
return call_user_func_array(array($this->oEntityCurrent,'get'.func_camelize($sField)),array());
}
return null;
}
}
?>

View file

@ -0,0 +1,50 @@
<?php
/*-------------------------------------------------------
*
* LiveStreet Engine Social Networking
* Copyright © 2008 Mzhelskiy Maxim
*
*--------------------------------------------------------
*
* Official site: www.livestreet.ru
* Contact e-mail: rus.engine@gmail.com
*
* GNU General Public License, version 2:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/
/**
* Валидатор каптчи (число с картинки)
*/
class ModuleValidate_EntityValidatorCaptcha extends ModuleValidate_EntityValidator {
/**
* Допускать или нет пустое значение
*
* @var bool
*/
public $allowEmpty=false;
/**
* Запуск валидации
*
* @param $sValue Данные для валидации
*
* @return bool|string
*/
public function validate($sValue) {
if($this->allowEmpty && $this->isEmpty($sValue)) {
return true;
}
if (!isset($_SESSION['captcha_keystring']) or $_SESSION['captcha_keystring']!=strtolower($sValue)) {
return $this->getMessage($this->Lang_Get('validate_captcha_not_valid',null,false),'msg');
}
return true;
}
}
?>

View file

@ -0,0 +1,132 @@
<?php
/*-------------------------------------------------------
*
* LiveStreet Engine Social Networking
* Copyright © 2008 Mzhelskiy Maxim
*
*--------------------------------------------------------
*
* Official site: www.livestreet.ru
* Contact e-mail: rus.engine@gmail.com
*
* GNU General Public License, version 2:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/
/**
* CCompareValidator class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* Валидатор сравнения значений
*/
class ModuleValidate_EntityValidatorCompare extends ModuleValidate_EntityValidator {
/**
* Имя поля для сравнения
*
* @var string
*/
public $compareField;
/**
* Значение для сравнения
*
* @var string
*/
public $compareValue;
/**
* Название поля сущности для сравнения, используется в сообщениях об ошибках
*
* @var string
*/
public $compareLabel;
/**
* Строгое сравнение
*
* @var bool
*/
public $strict=false;
/**
* Допускать или нет пустое значение
*
* @var bool
*/
public $allowEmpty=false;
/**
* Оператор для сравнения
* Доступны: '=' или '==', '!=', '>', '>=', '<', '<='
*
* @var string
*/
public $operator='=';
/**
* Запуск валидации
*
* @param $sValue Данные для валидации
*
* @return bool|string
*/
public function validate($sValue) {
if($this->allowEmpty && $this->isEmpty($sValue)) {
return true;
}
/**
* Определяем значение для сравнения
*/
if($this->compareValue!==null or !$this->oEntityCurrent) {
$sCompareLabel=$sCompareValue=$this->compareValue;
} else {
$sCompareField=$this->compareField===null ? $this->sFieldCurrent.'_repeat' : $this->compareField;
$sCompareValue=$this->getValueOfCurrentEntity($sCompareField);
$sCompareLabel=is_null($this->compareLabel) ? $sCompareField : $this->compareLabel;
}
switch($this->operator) {
case '=':
case '==':
if(($this->strict && $sValue!==$sCompareValue) || (!$this->strict && $sValue!=$sCompareValue)) {
return $this->getMessage($this->Lang_Get('validate_compare_must_repeated',null,false),'msg',array('compare_field'=>$sCompareLabel));
}
break;
case '!=':
if(($this->strict && $sValue===$sCompareValue) || (!$this->strict && $sValue==$sCompareValue)) {
return $this->getMessage($this->Lang_Get('validate_compare_must_not_equal',null,false),'msg',array('compare_field'=>$sCompareLabel,'compare_value'=>htmlspecialchars($sCompareValue)));
}
break;
case '>':
if($sValue<=$sCompareValue) {
return $this->getMessage($this->Lang_Get('validate_compare_must_greater',null,false),'msg',array('compare_field'=>$sCompareLabel,'compare_value'=>htmlspecialchars($sCompareValue)));
}
break;
case '>=':
if($sValue<$sCompareValue) {
return $this->getMessage($this->Lang_Get('validate_compare_must_greater_equal',null,false),'msg',array('compare_field'=>$sCompareLabel,'compare_value'=>htmlspecialchars($sCompareValue)));
}
break;
case '<':
if($sValue>=$sCompareValue) {
return $this->getMessage($this->Lang_Get('validate_compare_must_less',null,false),'msg',array('compare_field'=>$sCompareLabel,'compare_value'=>htmlspecialchars($sCompareValue)));
}
break;
case '<=':
if($sValue>$sCompareValue) {
return $this->getMessage($this->Lang_Get('validate_compare_must_less_equal',null,false),'msg',array('compare_field'=>$sCompareLabel,'compare_value'=>htmlspecialchars($sCompareValue)));
}
break;
default:
return $this->getMessage($this->Lang_Get('validate_compare_invalid_operator',null,false),'msg',array('operator'=>$this->operator));
}
return true;
}
}
?>

View file

@ -916,6 +916,14 @@ return array(
'validate_required_must_be' => 'Field %%field%% must be %%value%%',
'validate_required_cannot_blank' => 'Field %%field%% cannot be blank',
'validate_url_not_valid' => 'Field %%field%% is not a valid URL',
'validate_captcha_not_valid' => 'Field %%field%% is not a valid code',
'validate_compare_must_repeated' => 'Field %%field%% must be repeated %%compare_field%%',
'validate_compare_must_not_equal' => 'Field %%field%% must not be equal to %%compare_value%%',
'validate_compare_must_greater' => 'Field %%field%% must be greater than %%compare_value%%',
'validate_compare_must_greater_equal' => 'Field %%field%% must be greater than or equal to %%compare_value%%',
'validate_compare_must_less' => 'Field %%field%% must be less than %%compare_value%%',
'validate_compare_must_less_equal' => 'Field %%field%% must be less than or equal to %%compare_value%%',
'validate_compare_invalid_operator' => 'Field %%field%% invalid operator %%operator%%',
/**
* Подписка
*/

View file

@ -916,6 +916,14 @@ return array(
'validate_required_must_be' => 'Поле %%field%% должно иметь значение %%value%%',
'validate_required_cannot_blank' => 'Поле %%field%% не может быть пустым',
'validate_url_not_valid' => 'Поле %%field%% не соотвествует формату URL адреса',
'validate_captcha_not_valid' => 'Поле %%field%% содержит неверный код',
'validate_compare_must_repeated' => 'Поле %%field%% должно повторять %%compare_field%%',
'validate_compare_must_not_equal' => 'Поле %%field%% не должно повторять %%compare_value%%',
'validate_compare_must_greater' => 'Поле %%field%% должно быть больше чем %%compare_value%%',
'validate_compare_must_greater_equal' => 'Поле %%field%% должно быть больше или равно %%compare_value%%',
'validate_compare_must_less' => 'Поле %%field%% должно быть меньше чем %%compare_value%%',
'validate_compare_must_less_equal' => 'Поле %%field%% должно быть меньше или равно %%compare_value%%',
'validate_compare_invalid_operator' => 'У поля %%field%% неверный оператор сравнения %%operator%%',
/**
* Подписка
*/