mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-09 03:32:00 +09:00
issue 2119. supporting php 5.4. validator class.
git-svn-id: http://xe-core.googlecode.com/svn/branches/maserati@12695 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
parent
0973ed647a
commit
19cda127e4
1 changed files with 267 additions and 131 deletions
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Validator class
|
||||
* @author NHN (developers@xpressengine.com)
|
||||
|
|
@ -7,57 +8,55 @@
|
|||
*/
|
||||
class Validator
|
||||
{
|
||||
|
||||
/**
|
||||
* cache directory
|
||||
* @var string
|
||||
*/
|
||||
var $_cache_dir = '';
|
||||
|
||||
/**
|
||||
* last error
|
||||
* @var array
|
||||
*/
|
||||
var $_last_error;
|
||||
|
||||
/**
|
||||
* xml ruleset object
|
||||
* @var Xml_Node_ object
|
||||
*/
|
||||
var $_xml_ruleset = null;
|
||||
var $_xml_ruleset = NULL;
|
||||
|
||||
/**
|
||||
* rule list
|
||||
* @var array
|
||||
*/
|
||||
var $_rules;
|
||||
|
||||
/**
|
||||
* filter list
|
||||
* @var array
|
||||
*/
|
||||
var $_filters;
|
||||
|
||||
/**
|
||||
* Can usable status for multibyte string function
|
||||
* @var boolean
|
||||
*/
|
||||
var $_has_mb_func;
|
||||
|
||||
/**
|
||||
* validator version
|
||||
* @var string
|
||||
*/
|
||||
var $_version = '1.0';
|
||||
|
||||
/**
|
||||
* ruleset xml file path
|
||||
* @var string
|
||||
*/
|
||||
var $_xml_path = '';
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @param string $xml_path
|
||||
* @return void
|
||||
*/
|
||||
function Validator($xml_path='')
|
||||
{
|
||||
$this->__construct($xml_path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @param string $xml_path
|
||||
|
|
@ -67,9 +66,10 @@ class Validator
|
|||
{
|
||||
$this->_rules = array();
|
||||
$this->_filters = array();
|
||||
$this->_xml_ruleset = null;
|
||||
$this->_xml_ruleset = NULL;
|
||||
|
||||
if($xml_path) $this->load($xml_path);
|
||||
if($xml_path)
|
||||
$this->load($xml_path);
|
||||
|
||||
// predefined rules
|
||||
$this->addRule(array(
|
||||
|
|
@ -91,8 +91,8 @@ class Validator
|
|||
*/
|
||||
function __destruct()
|
||||
{
|
||||
$this->_rules = null;
|
||||
$this->_filters = null;
|
||||
$this->_rules = NULL;
|
||||
$this->_filters = NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -102,25 +102,37 @@ class Validator
|
|||
*/
|
||||
function load($xml_path)
|
||||
{
|
||||
$this->_xml_ruleset = null;
|
||||
$this->_xml_ruleset = NULL;
|
||||
|
||||
$xml_path = realpath($xml_path);
|
||||
if(!is_readable($xml_path)) return false;
|
||||
if(!is_readable($xml_path))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$parser = new XmlParser();
|
||||
$xml = $parser->loadXmlFile($xml_path);
|
||||
if(!isset($xml->ruleset) || !isset($xml->ruleset->fields) || !isset($xml->ruleset->fields->field)) return false;
|
||||
if(!isset($xml->ruleset) || !isset($xml->ruleset->fields) || !isset($xml->ruleset->fields->field))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// custom rules
|
||||
if(isset($xml->ruleset->customrules) && isset($xml->ruleset->customrules->rule))
|
||||
{
|
||||
$customrules = $xml->ruleset->customrules->rule;
|
||||
if(!is_array($customrules)) $customrules = array($customrules);
|
||||
if(!is_array($customrules))
|
||||
{
|
||||
$customrules = array($customrules);
|
||||
}
|
||||
|
||||
$rules = array();
|
||||
foreach($customrules as $rule)
|
||||
{
|
||||
if(!isset($rule->attrs) || !isset($rule->attrs->name)) continue;
|
||||
if(!isset($rule->attrs) || !isset($rule->attrs->name))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$rule = (array) $rule->attrs;
|
||||
$name = $rule['name'];
|
||||
|
|
@ -128,12 +140,18 @@ class Validator
|
|||
|
||||
$rules[$name] = $rule;
|
||||
}
|
||||
if(count($rules)) $this->addRule($rules);
|
||||
if(count($rules))
|
||||
{
|
||||
$this->addRule($rules);
|
||||
}
|
||||
}
|
||||
|
||||
// filters
|
||||
$fields = $xml->ruleset->fields->field;
|
||||
if(!is_array($fields)) $fields = array($fields);
|
||||
if(!is_array($fields))
|
||||
{
|
||||
$fields = array($fields);
|
||||
}
|
||||
|
||||
$filters = array();
|
||||
foreach($fields as $field)
|
||||
|
|
@ -141,7 +159,10 @@ class Validator
|
|||
$name = '';
|
||||
$filter = array();
|
||||
|
||||
if(!isset($field->attrs) || !isset($field->attrs->name)) continue;
|
||||
if(!isset($field->attrs) || !isset($field->attrs->name))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
$filter = (array) $field->attrs;
|
||||
|
||||
$name = $filter['name'];
|
||||
|
|
@ -151,7 +172,10 @@ class Validator
|
|||
if(isset($field->if))
|
||||
{
|
||||
$if = $field->if;
|
||||
if(!is_array($if)) $if = array($if);
|
||||
if(!is_array($if))
|
||||
{
|
||||
$if = array($if);
|
||||
}
|
||||
foreach($if as $idx => $cond)
|
||||
{
|
||||
$if[$idx] = (array) $cond->attrs;
|
||||
|
|
@ -166,7 +190,7 @@ class Validator
|
|||
$this->_filters = $filters;
|
||||
$this->_xml_path = $xml_path;
|
||||
|
||||
return true;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -199,7 +223,10 @@ class Validator
|
|||
$fields = (array) Context::getRequestVars();
|
||||
}
|
||||
|
||||
if(!is_array($fields)) return true;
|
||||
if(!is_array($fields))
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
$filter_default = array(
|
||||
'required' => 'false',
|
||||
|
|
@ -234,7 +261,10 @@ class Validator
|
|||
$filters[$key] = $filter;
|
||||
}
|
||||
|
||||
if(!count($names)) continue;
|
||||
if(!count($names))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach($names as $name)
|
||||
{
|
||||
|
|
@ -251,12 +281,12 @@ class Validator
|
|||
if(preg_match("/(^[a-z_]*)[\[](?:\'|\")?([a-z_]*)(?:\'|\")?[\]]$/i", $key, $matches))
|
||||
{
|
||||
$exists = array_key_exists($matches[1], $fields);
|
||||
$value = $exists ? $fields[$matches[1]][$matches[2]] : null;
|
||||
$value = $exists ? $fields[$matches[1]][$matches[2]] : NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
$exists = array_key_exists($key, $fields);
|
||||
$value = $exists ? $fields[$fname] : null;
|
||||
$value = $exists ? $fields[$fname] : NULL;
|
||||
}
|
||||
|
||||
if(is_array($value))
|
||||
|
|
@ -274,31 +304,52 @@ class Validator
|
|||
// conditional statement
|
||||
foreach($filter['if'] as $cond)
|
||||
{
|
||||
if(!isset($cond['test']) || !isset($cond['attr'])) continue;
|
||||
if(!isset($cond['test']) || !isset($cond['attr']))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$func_body = preg_replace('/\\$(\w+)/', '$c[\'$1\']', $cond['test']);
|
||||
$func = create_function('$c', "return !!({$func_body});");
|
||||
|
||||
if($func($fields)) $filter[$cond['attr']] = $cond['value'];
|
||||
if($func($fields))
|
||||
{
|
||||
$filter[$cond['attr']] = $cond['value'];
|
||||
}
|
||||
}
|
||||
|
||||
// attr : default
|
||||
if(!$value && strlen($default = trim($filter['default'])))
|
||||
{
|
||||
$value = $default;
|
||||
if(is_null($fields_)) Context::set($fname, $value);
|
||||
else $fields_[$fname] = $value;
|
||||
if(is_null($fields_))
|
||||
{
|
||||
Context::set($fname, $value);
|
||||
}
|
||||
else
|
||||
{
|
||||
$fields_[$fname] = $value;
|
||||
}
|
||||
}
|
||||
$value_len = strlen($value);
|
||||
|
||||
// attr : modifier
|
||||
if(is_string($modifiers=$filter['modifiers'])) $modifiers = explode(',', trim($modifiers));
|
||||
if(is_string($modifiers = $filter['modifiers']))
|
||||
{
|
||||
$modifiers = explode(',', trim($modifiers));
|
||||
}
|
||||
|
||||
// attr : required
|
||||
if($filter['required'] === 'true' && !$value_len) return $this->error($key, 'isnull');
|
||||
if($filter['required'] === 'true' && !$value_len)
|
||||
{
|
||||
return $this->error($key, 'isnull');
|
||||
}
|
||||
|
||||
// if the field wasn't passed, ignore this value
|
||||
if(!$exists && !$value_len) continue;
|
||||
if(!$exists && !$value_len)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// attr : length
|
||||
if($length = $filter['length'])
|
||||
|
|
@ -314,13 +365,19 @@ class Validator
|
|||
$strlength = $this->_has_mb_func ? mb_strlen($value, 'utf-8') : $this->mbStrLen($value);
|
||||
}
|
||||
|
||||
if(($min && $min > ($is_min_b?$strbytes:$strlength)) || ($max && $max < ($is_max_b?$strbytes:$strlength))) return $this->error($key, 'outofrange');
|
||||
if(($min && $min > ($is_min_b ? $strbytes : $strlength)) || ($max && $max < ($is_max_b ? $strbytes : $strlength)))
|
||||
{
|
||||
return $this->error($key, 'outofrange');
|
||||
}
|
||||
}
|
||||
|
||||
// equalto
|
||||
if($equalto = $filter['equalto'])
|
||||
{
|
||||
if(!array_key_exists($equalto, $fields) || trim($fields[$equalto]) !== $value) return $this->error($key, 'equalto');
|
||||
if(!array_key_exists($equalto, $fields) || trim($fields[$equalto]) !== $value)
|
||||
{
|
||||
return $this->error($key, 'equalto');
|
||||
}
|
||||
}
|
||||
|
||||
// rules
|
||||
|
|
@ -331,13 +388,19 @@ class Validator
|
|||
{
|
||||
$result = $this->applyRule($rule, $value);
|
||||
// apply the 'not' modifier
|
||||
if(in_array('not', $modifiers)) $result = !$result;
|
||||
if(!$result) return $this->error($key, 'invalid_'.$rule);
|
||||
if(in_array('not', $modifiers))
|
||||
{
|
||||
$result = !$result;
|
||||
}
|
||||
if(!$result)
|
||||
{
|
||||
return $this->error($key, 'invalid_' . $rule);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -347,7 +410,10 @@ class Validator
|
|||
*/
|
||||
function arrayTrim($array)
|
||||
{
|
||||
if(!is_array($array)) return trim($array);
|
||||
if(!is_array($array))
|
||||
{
|
||||
return trim($array);
|
||||
}
|
||||
|
||||
foreach($array as $key => $value)
|
||||
{
|
||||
|
|
@ -370,7 +436,7 @@ class Validator
|
|||
|
||||
$this->_last_error = array('field' => $field, 'msg' => $msg);
|
||||
|
||||
return false;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -390,13 +456,25 @@ class Validator
|
|||
*/
|
||||
function addRule($name, $rule = '')
|
||||
{
|
||||
if(is_array($name)) $args = $name;
|
||||
else $args = array($name=>$rule);
|
||||
if(is_array($name))
|
||||
{
|
||||
$args = $name;
|
||||
}
|
||||
else
|
||||
{
|
||||
$args = array($name => $rule);
|
||||
}
|
||||
|
||||
foreach($args as $name => $rule)
|
||||
{
|
||||
if(!$rule) continue;
|
||||
if(is_string($rule)) $rule = array('type'=>'regex', 'test'=>$rule);
|
||||
if(!$rule)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if(is_string($rule))
|
||||
{
|
||||
$rule = array('type' => 'regex', 'test' => $rule);
|
||||
}
|
||||
|
||||
if($rule['type'] == 'enum')
|
||||
{
|
||||
|
|
@ -426,19 +504,31 @@ class Validator
|
|||
*/
|
||||
function addFilter($name, $filter = '')
|
||||
{
|
||||
if(is_array($name)) $args = $name;
|
||||
else $args = array($name=>$filter);
|
||||
if(is_array($name))
|
||||
{
|
||||
$args = $name;
|
||||
}
|
||||
else
|
||||
{
|
||||
$args = array($name => $filter);
|
||||
}
|
||||
|
||||
foreach($args as $name => $filter)
|
||||
{
|
||||
if(!$filter) continue;
|
||||
if(!$filter)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(isset($filter['if']))
|
||||
{
|
||||
if(is_array($filter['if']) && count($filter['if']))
|
||||
{
|
||||
$key = key($filter['if']);
|
||||
if(!is_int($key)) $filter['if'] = array($filter['if']);
|
||||
if(!is_int($key))
|
||||
{
|
||||
$filter['if'] = array($filter['if']);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -489,7 +579,7 @@ class Validator
|
|||
return $rule['func_test']($value);
|
||||
}
|
||||
|
||||
return true;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -513,21 +603,36 @@ class Validator
|
|||
*/
|
||||
function getJsPath()
|
||||
{
|
||||
if(!$this->_cache_dir) return false;
|
||||
if(!$this->_cache_dir)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
$dir = $this->_cache_dir . '/ruleset';
|
||||
if(!is_dir($dir) && !mkdir($dir)) return false;
|
||||
if(!$this->_xml_path) return false;
|
||||
if(!is_dir($dir) && !mkdir($dir))
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
if(!$this->_xml_path)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// current language
|
||||
$lang_type = class_exists('Context') ? Context::getLangType() : 'en';
|
||||
|
||||
// check the file
|
||||
$filepath = $dir . '/' . md5($this->_version . ' ' . $this->_xml_path) . ".{$lang_type}.js";
|
||||
if(is_readable($filepath) && filemtime($filepath) > filemtime($this->_xml_path)) return $filepath;
|
||||
if(is_readable($filepath) && filemtime($filepath) > filemtime($this->_xml_path))
|
||||
{
|
||||
return $filepath;
|
||||
}
|
||||
|
||||
$content = $this->_compile2js();
|
||||
if($content === false) return false;
|
||||
if($content === FALSE)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
if(is_callable('file_put_contents'))
|
||||
{
|
||||
|
|
@ -557,7 +662,10 @@ class Validator
|
|||
$ruleset = basename($this->_xml_path, '.xml');
|
||||
$content = array();
|
||||
|
||||
if(preg_match('@(^|/)files/ruleset/\w+\.xml$@i', $this->_xml_path)) $ruleset = '@'.$ruleset;
|
||||
if(preg_match('@(^|/)files/ruleset/\w+\.xml$@i', $this->_xml_path))
|
||||
{
|
||||
$ruleset = '@' . $ruleset;
|
||||
}
|
||||
|
||||
list($ruleset) = explode('.', $ruleset);
|
||||
|
||||
|
|
@ -568,7 +676,10 @@ class Validator
|
|||
$addrules = array();
|
||||
foreach($this->_rules as $name => $rule)
|
||||
{
|
||||
if(strpos('email,userid,url,alpha,alpha_number,number,', $name.',') !== false) continue;
|
||||
if(strpos('email,userid,url,alpha,alpha_number,number,', $name . ',') !== false)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
switch($rule['type'])
|
||||
{
|
||||
case 'regex':
|
||||
|
|
@ -599,20 +710,41 @@ class Validator
|
|||
$messages[] = "v.cast('ADD_MESSAGE',['{$name}','{$field_lang}']);";
|
||||
}
|
||||
|
||||
if($filter['required'] == 'true') $field[] = 'required:true';
|
||||
if($filter['rule']) $field[] = "rule:'{$filter['rule']}'";
|
||||
if($filter['default']) $field[] = "default:'{$filter['default']}'";
|
||||
if($filter['modifier']) $field[] = "modifier:'{$filter['modifier']}'";
|
||||
if($filter['required'] == 'true')
|
||||
{
|
||||
$field[] = 'required:true';
|
||||
}
|
||||
if($filter['rule'])
|
||||
{
|
||||
$field[] = "rule:'{$filter['rule']}'";
|
||||
}
|
||||
if($filter['default'])
|
||||
{
|
||||
$field[] = "default:'{$filter['default']}'";
|
||||
}
|
||||
if($filter['modifier'])
|
||||
{
|
||||
$field[] = "modifier:'{$filter['modifier']}'";
|
||||
}
|
||||
if($filter['length'])
|
||||
{
|
||||
list($min, $max) = explode(':', $filter['length']);
|
||||
if($min) $field[] = "minlength:'{$min}'";
|
||||
if($max) $field[] = "maxlength:'{$max}'";
|
||||
if($min)
|
||||
{
|
||||
$field[] = "minlength:'{$min}'";
|
||||
}
|
||||
if($max)
|
||||
{
|
||||
$field[] = "maxlength:'{$max}'";
|
||||
}
|
||||
}
|
||||
if($filter['if'])
|
||||
{
|
||||
$ifs = array();
|
||||
if(!isset($filter['if'][0])) $filter['if'] = array($filter['if']);
|
||||
if(!isset($filter['if'][0]))
|
||||
{
|
||||
$filter['if'] = array($filter['if']);
|
||||
}
|
||||
foreach($filter['if'] as $if)
|
||||
{
|
||||
$ifs[] = "{test:'" . addslashes($if['test']) . "', attr:'{$if['attr']}', value:'" . addslashes($if['value']) . "'}";
|
||||
|
|
@ -626,7 +758,10 @@ class Validator
|
|||
}
|
||||
}
|
||||
|
||||
if(!$content) return '/* Error : empty ruleset */';
|
||||
if(!$content)
|
||||
{
|
||||
return '/* Error : empty ruleset */';
|
||||
}
|
||||
|
||||
// error messages
|
||||
foreach($lang->filter as $key => $text)
|
||||
|
|
@ -643,6 +778,7 @@ class Validator
|
|||
|
||||
return "(function($,v){\nv=xe.getApp('validator')[0];if(!v)return;\n{$addrules}\nv.cast('ADD_FILTER',['{$ruleset}', {{$content}}]);\n{$messages}\n})(jQuery);";
|
||||
}
|
||||
|
||||
}
|
||||
/* End of file Validator.class.php */
|
||||
/* Location: ./classes/validator/Validator.class.php */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue