Библиотека для cis, online, cms1
This commit is contained in:
commit
3c2e614d87
269 changed files with 39854 additions and 0 deletions
41
core/validator/rule/abstract.php
Normal file
41
core/validator/rule/abstract.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
abstract class Rule_Abstract
|
||||
{
|
||||
public $field;
|
||||
protected $errorMsg;
|
||||
protected $ctx;
|
||||
|
||||
public function __construct($field, $errorMsg)
|
||||
{
|
||||
$this->field = $field;
|
||||
$this->errorMsg = $errorMsg;
|
||||
}
|
||||
|
||||
public function setName($field)
|
||||
{
|
||||
$this->field = $field;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setErrorMsg($errorMsg)
|
||||
{
|
||||
$this->errorMsg = $errorMsg;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return $this->errorMsg;
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function setContext($ctx)
|
||||
{
|
||||
$this->ctx = $ctx;
|
||||
}
|
||||
}
|
||||
2
core/validator/rule/all.php
Normal file
2
core/validator/rule/all.php
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
<?php
|
||||
foreach (glob(dirname(__FILE__) . "/*.php") as $file) { require_once $file; }
|
||||
20
core/validator/rule/alpha.php
Normal file
20
core/validator/rule/alpha.php
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
require_once 'abstract.php';
|
||||
|
||||
/**
|
||||
* Ïðîâåðêà íà ÷èñëî
|
||||
*/
|
||||
class Rule_Alpha extends Rule_Abstract
|
||||
{
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Ïîëå äîëæíî ñîäåðæàòü òîëüêî áóêâû";
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
return ctype_alpha($container->get($this->field));
|
||||
}
|
||||
}
|
||||
|
||||
59
core/validator/rule/code.php
Normal file
59
core/validator/rule/code.php
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
require_once 'abstract.php';
|
||||
|
||||
/**
|
||||
* Ïðîâåðêà ôîðìàòà ýëåêòðîííîé ïî÷òû
|
||||
*/
|
||||
class Rule_Code extends Rule_Abstract
|
||||
{
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Íåïðàâèëüíî óêàçàí ïåðñîíàëüíûé êîä";
|
||||
}
|
||||
|
||||
function checkCode($code) {
|
||||
foreach($code as $c) {
|
||||
if (empty($c)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
if ($status == 'update') return true;
|
||||
$name = $this->field;
|
||||
|
||||
if (is_array($_POST[$name . '_code_genre'])) {
|
||||
for($n = 0; $n < count($_POST[$name . '_code_genre']); $n++) {
|
||||
$code = array(
|
||||
$_POST[$name . '_code_genre'][$n],
|
||||
$_POST[$name . '_code_f'][$n],
|
||||
$_POST[$name . '_code_i'][$n],
|
||||
$_POST[$name . '_code_o'][$n],
|
||||
$_POST[$name . '_code_year'][$n],
|
||||
$_POST[$name . '_code_month'][$n],
|
||||
$_POST[$name . '_code_day'][$n]
|
||||
);
|
||||
if (!$this->checkCode($code)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
$code = array(
|
||||
$_POST[$name . '_code_genre'],
|
||||
$_POST[$name . '_code_f'],
|
||||
$_POST[$name . '_code_i'],
|
||||
$_POST[$name . '_code_o'],
|
||||
$_POST[$name . '_code_year'],
|
||||
$_POST[$name . '_code_month'],
|
||||
$_POST[$name . '_code_day']
|
||||
);
|
||||
|
||||
return $this->checkCode($code);
|
||||
}
|
||||
}
|
||||
}
|
||||
34
core/validator/rule/count.php
Normal file
34
core/validator/rule/count.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
require_once 'abstract.php';
|
||||
|
||||
/**
|
||||
* Ïðîâåðêà ôîðìàòà äàòû
|
||||
*/
|
||||
class Rule_Count extends Rule_Abstract
|
||||
{
|
||||
public $size = 1;
|
||||
public $max = false;
|
||||
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Êîëè÷åñòâî çàïèñåé äîëæíî áûòü íå ìåííå {$this->size} è íå áîëåå {$this->max}";
|
||||
}
|
||||
|
||||
function not_empty($s) {
|
||||
return $s != "";
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
|
||||
if (!$this->max) {
|
||||
$this->max = $this->size;
|
||||
}
|
||||
$count = count(array_filter(array_map('trim',
|
||||
explode(";", $container->get($this->field))), array($this, 'not_empty')));
|
||||
|
||||
return $count >= $this->size && $count <= $this->max;
|
||||
}
|
||||
}
|
||||
|
||||
24
core/validator/rule/date.php
Normal file
24
core/validator/rule/date.php
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
require_once 'abstract.php';
|
||||
|
||||
/**
|
||||
* Ïðîâåðêà ôîðìàòà äàòû
|
||||
*/
|
||||
class Rule_Date extends Rule_Abstract
|
||||
{
|
||||
private $split = "\\/";
|
||||
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Íåâåðíûé ôîðìàò äàòû";
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
$pattern = "/^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})$/";
|
||||
return (preg_match($pattern, $container->get($this->field), $matches)
|
||||
&& checkdate($matches[2], $matches[1], $matches[3]));
|
||||
}
|
||||
}
|
||||
|
||||
30
core/validator/rule/email.php
Normal file
30
core/validator/rule/email.php
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
require_once 'abstract.php';
|
||||
|
||||
/**
|
||||
* Ïðîâåðêà ôîðìàòà ýëåêòðîííîé ïî÷òû
|
||||
*/
|
||||
class Rule_Email extends Rule_Abstract
|
||||
{
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Íåâåðíûé ôîðìàò ýëåêòðîííîé ïî÷òû";
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
$user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\|\{\}~\']+';
|
||||
$doIsValid = '(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]\.?)+';
|
||||
$ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}';
|
||||
$ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}';
|
||||
|
||||
$emails = explode(",", $container->get($this->field));
|
||||
foreach ($emails as $email) {
|
||||
// if (! preg_match("/^$user@($doIsValid|(\[($ipv4|$ipv6)\]))$/", $email)) return false;
|
||||
if (! filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
28
core/validator/rule/emaillist.php
Normal file
28
core/validator/rule/emaillist.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
require_once 'abstract.php';
|
||||
|
||||
/**
|
||||
* Ïðîâåðêà ôîðìàòà ýëåêòðîííîé ïî÷òû
|
||||
*/
|
||||
class Rule_EmailList extends Rule_Abstract
|
||||
{
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Íåâåðíûé ôîðìàò ýëåêòðîííîé ïî÷òû";
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null) {
|
||||
$user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\|\{\}~\']+';
|
||||
$doIsValid = '(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]\.?)+';
|
||||
$ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}';
|
||||
$ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}';
|
||||
|
||||
$emails = $container->get($this->field);
|
||||
foreach ($emails as $email) {
|
||||
// if (! preg_match("/^$user@($doIsValid|(\[($ipv4|$ipv6)\]))$/", $email)) return false;
|
||||
if (! filter_var($email, FILTER_VALIDATE_EMAIL)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
28
core/validator/rule/match.php
Normal file
28
core/validator/rule/match.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
require_once 'abstract.php';
|
||||
|
||||
/**
|
||||
* Ïðîâåðêà íà ðàâåíòñòâî äâóõ ïîëåé
|
||||
*/
|
||||
class Rule_Match extends Rule_Abstract
|
||||
{
|
||||
public $same;
|
||||
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Ïîëÿ íå ñîâïàäàþò";
|
||||
}
|
||||
|
||||
/* public function __construct($field, $refField, $errorMsg)
|
||||
{
|
||||
$this->field = $field;
|
||||
$this->refField = $refField;
|
||||
$this->errorMsg = $errorMsg;
|
||||
}
|
||||
*/
|
||||
|
||||
public function isValid(Collection $container, $status = null) {
|
||||
return (strcmp($container->get($this->field), $container->get($this->same)) == 0);
|
||||
}
|
||||
}
|
||||
25
core/validator/rule/notnull.php
Normal file
25
core/validator/rule/notnull.php
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
require_once 'abstract.php';
|
||||
|
||||
class Rule_Notnull extends Rule_Abstract
|
||||
{
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Ïîëå íå äîëæíî áûòü ïóñòûì";
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
$data = $container->get($this->field);
|
||||
if (is_array($data)) {
|
||||
foreach($data as $c) {
|
||||
if (trim($c) != '') return true;
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
$value = trim($data);
|
||||
return $value != '';
|
||||
}
|
||||
}
|
||||
}
|
||||
19
core/validator/rule/numeric.php
Normal file
19
core/validator/rule/numeric.php
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
require_once 'abstract.php';
|
||||
|
||||
/**
|
||||
* Ïðîâåðêà íà ÷èñëî
|
||||
*/
|
||||
class Rule_Numeric extends Rule_Abstract
|
||||
{
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Çíà÷åíèå ïîëÿ äîëæíî áûòü ÷èñëîì";
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
return (is_numeric($container->get($this->field)));
|
||||
}
|
||||
}
|
||||
35
core/validator/rule/time.php
Normal file
35
core/validator/rule/time.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
require_once 'abstract.php';
|
||||
|
||||
/**
|
||||
* Ïðîâåðêà ôîðìàòà âðåìåíè
|
||||
*/
|
||||
class Rule_Time extends Rule_Abstract
|
||||
{
|
||||
private $split = ":";
|
||||
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return "Íåâåðíûé ôîðìàò âðåìåíè";
|
||||
}
|
||||
|
||||
static function checktime($hour, $minute)
|
||||
{
|
||||
if ($hour > -1 && $hour < 24 && $minute > -1 && $minute < 60) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
$tmp = explode($this->split, $container->get($this->field), 2);
|
||||
if ($tmp) {
|
||||
if (self::checktime ($tmp[0], $tmp[1])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
20
core/validator/rule/unique.php
Normal file
20
core/validator/rule/unique.php
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
require_once 'abstract.php';
|
||||
|
||||
/**
|
||||
* Ïðîâåðêà ôîðìàòà âðåìåíè
|
||||
*/
|
||||
class Rule_Unique extends Rule_Abstract
|
||||
{
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return $this->ctx->getMessage();
|
||||
}
|
||||
|
||||
public function isValid(Collection $container, $status = null)
|
||||
{
|
||||
return $this->ctx->isUnique($container->get($this->field), $status);
|
||||
}
|
||||
}
|
||||
|
||||
118
core/validator/validator.php
Normal file
118
core/validator/validator.php
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
<?php
|
||||
/**
|
||||
* Ïðîâåðêà êîëëåêöèè
|
||||
*/
|
||||
class Validator
|
||||
{
|
||||
protected $chain = array(); // Ìàññèâ ïðàâèë
|
||||
protected $errorMsg = array(); // Ìàññèâ îøèáîê
|
||||
|
||||
/**
|
||||
* Äîáàâëåíèå ñïèñêà ïðàâèë â ñïåöèàëüíîì ôîðìàòå
|
||||
* array(array('name' => fieldname, 'validate' => ruletext), ...)
|
||||
* fieldname - Èìÿ ïåðåìåíîé äëÿ ïðîâåðêè
|
||||
* ruletext - Îïèñàíèå ïðàâèëà ñì. ôîðìàò ïðàâèëà íèæå
|
||||
*/
|
||||
public function addRuleList(array $input)
|
||||
{
|
||||
require_once "core/validator/rule/all.php";
|
||||
|
||||
$type = array(
|
||||
'date' => 'Rule_Date',
|
||||
'email' => 'Rule_Email',
|
||||
'emaillist'=> 'Rule_EmailList',
|
||||
'match' => 'Rule_Match',
|
||||
'time' => 'Rule_Time',
|
||||
'alpha' => 'Rule_Alpha',
|
||||
'require' => 'Rule_Notnull',
|
||||
'numeric' => 'Rule_Numeric',
|
||||
'unique' => 'Rule_Unique',
|
||||
'count' => 'Rule_Count',
|
||||
'code' => 'Rule_Code'
|
||||
);
|
||||
|
||||
// Ðàçáîð ïðàâèëà ïðîâåðêè
|
||||
// Ôîðìàò ïðàâèëà 'rule1|rule2,param1=value1|rule3,param1=value1,param2=value2'
|
||||
foreach ($input as $value) {
|
||||
// Ñïèñîê ïðàâèë
|
||||
if (! isset($value['validate'])) continue;
|
||||
$rules = explode("|", $value['validate']);
|
||||
foreach ($rules as $rule) {
|
||||
// Ñïèñîê ïàðàìåòðîâ ïðàâèëà
|
||||
$rule_param = explode(",", $rule);
|
||||
$name = array_shift($rule_param);
|
||||
|
||||
if (isset($type[$name])) {
|
||||
$constructor = $type[$name]; // "Rule_" . ucfirst($name)
|
||||
$rule = new $constructor($value['name'], false); // Íóæíû øàáëîííûå ñîîáùåíèÿ äëÿ ïðàâèë
|
||||
if (isset($value['context'])) {
|
||||
$rule->setContext($value['context']);
|
||||
}
|
||||
foreach ($rule_param as $param) {
|
||||
// Èìÿ è çíà÷åíèå ïàðàìåòðà
|
||||
list($name, $value) = explode("=", $param);
|
||||
$rule->$name = $value;
|
||||
}
|
||||
$this->addRule($rule);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function addRule(&$rule)
|
||||
{
|
||||
if (is_array($rule)) {
|
||||
$this->chain = array_merge($this->chain, $rule);
|
||||
} else {
|
||||
$this->chain[] = $rule;
|
||||
}
|
||||
}
|
||||
|
||||
public function Skip($rule, $container) // -> Rule_Abstract
|
||||
{
|
||||
if ($rule instanceof Rule_Notnull) {
|
||||
return false;
|
||||
} else {
|
||||
$data = $container->get($rule->field);
|
||||
if (!is_array($data)) {
|
||||
$value = trim($data);
|
||||
return $value == '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function validate(Collection $container, $rule = null, $status = null)
|
||||
{
|
||||
if ($rule) {
|
||||
$this->chain = $rule;
|
||||
}
|
||||
$this->errorMsg = array();
|
||||
foreach ($this->chain as $key => $rule) {
|
||||
if (!$this->Skip($rule, $container) && !$rule->isValid($container, $status)) {
|
||||
$name = $rule->field;
|
||||
$this->errorMsg[$name] = $rule->getErrorMsg();
|
||||
}
|
||||
}
|
||||
return $this->isValid();
|
||||
}
|
||||
|
||||
public function addError($name, $message)
|
||||
{
|
||||
$this->errorMsg[$name] = $message;
|
||||
}
|
||||
|
||||
public function isError()
|
||||
{
|
||||
return !empty($this->errorMsg);
|
||||
}
|
||||
|
||||
public function isValid()
|
||||
{
|
||||
return empty($this->errorMsg);
|
||||
}
|
||||
|
||||
public function getErrorMsg()
|
||||
{
|
||||
return $this->errorMsg;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue