mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-05 09:41:40 +09:00
issue 2662 coding convention
git-svn-id: http://xe-core.googlecode.com/svn/branches/maserati@12220 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
parent
f9ca200edf
commit
15fa10dc39
11 changed files with 697 additions and 522 deletions
|
|
@ -53,7 +53,8 @@ class Validator
|
|||
* @param string $xml_path
|
||||
* @return void
|
||||
*/
|
||||
function Validator($xml_path='') {
|
||||
function Validator($xml_path='')
|
||||
{
|
||||
$this->__construct($xml_path);
|
||||
}
|
||||
|
||||
|
|
@ -62,7 +63,8 @@ class Validator
|
|||
* @param string $xml_path
|
||||
* @return void
|
||||
*/
|
||||
function __construct($xml_path='') {
|
||||
function __construct($xml_path='')
|
||||
{
|
||||
$this->_rules = array();
|
||||
$this->_filters = array();
|
||||
$this->_xml_ruleset = null;
|
||||
|
|
@ -71,13 +73,13 @@ class Validator
|
|||
|
||||
// predefined rules
|
||||
$this->addRule(array(
|
||||
'email' => '/^[\w-]+((?:\.|\+|\~)[\w-]+)*@[\w-]+(\.[\w-]+)+$/',
|
||||
'userid' => '/^[a-z]+[\w-]*[a-z0-9_]+$/i',
|
||||
'url' => '/^(https?|ftp|mms):\/\/[0-9a-z-]+(\.[_0-9a-z-]+)+(:\d+)?/',
|
||||
'alpha' => '/^[a-z]*$/i',
|
||||
'alpha_number' => '/^[a-z][a-z0-9_]*$/i',
|
||||
'number' => '/^(?:[1-9]\\d*|0)$/'
|
||||
));
|
||||
'email' => '/^[\w-]+((?:\.|\+|\~)[\w-]+)*@[\w-]+(\.[\w-]+)+$/',
|
||||
'userid' => '/^[a-z]+[\w-]*[a-z0-9_]+$/i',
|
||||
'url' => '/^(https?|ftp|mms):\/\/[0-9a-z-]+(\.[_0-9a-z-]+)+(:\d+)?/',
|
||||
'alpha' => '/^[a-z]*$/i',
|
||||
'alpha_number' => '/^[a-z][a-z0-9_]*$/i',
|
||||
'number' => '/^(?:[1-9]\\d*|0)$/'
|
||||
));
|
||||
|
||||
$this->_has_mb_func = is_callable('mb_strlen');
|
||||
$this->setCacheDir('./files/cache');
|
||||
|
|
@ -87,7 +89,8 @@ class Validator
|
|||
* @destructor
|
||||
* @return void
|
||||
*/
|
||||
function __destruct() {
|
||||
function __destruct()
|
||||
{
|
||||
$this->_rules = null;
|
||||
$this->_filters = null;
|
||||
}
|
||||
|
|
@ -97,7 +100,8 @@ class Validator
|
|||
* @param string $xml_path A file name to be loaded
|
||||
* @return boolean
|
||||
*/
|
||||
function load($xml_path) {
|
||||
function load($xml_path)
|
||||
{
|
||||
$this->_xml_ruleset = null;
|
||||
|
||||
$xml_path = realpath($xml_path);
|
||||
|
|
@ -108,12 +112,14 @@ class Validator
|
|||
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)) {
|
||||
if(isset($xml->ruleset->customrules) && isset($xml->ruleset->customrules->rule))
|
||||
{
|
||||
$customrules = $xml->ruleset->customrules->rule;
|
||||
if(!is_array($customrules)) $customrules = array($customrules);
|
||||
|
||||
$rules = array();
|
||||
foreach($customrules as $rule) {
|
||||
foreach($customrules as $rule)
|
||||
{
|
||||
if(!isset($rule->attrs) || !isset($rule->attrs->name)) continue;
|
||||
|
||||
$rule = (array)$rule->attrs;
|
||||
|
|
@ -130,7 +136,8 @@ class Validator
|
|||
if(!is_array($fields)) $fields = array($fields);
|
||||
|
||||
$filters = array();
|
||||
foreach($fields as $field) {
|
||||
foreach($fields as $field)
|
||||
{
|
||||
$name = '';
|
||||
$filter = array();
|
||||
|
||||
|
|
@ -141,10 +148,12 @@ class Validator
|
|||
unset($filter['name']);
|
||||
|
||||
// conditional statement
|
||||
if(isset($field->if)) {
|
||||
if(isset($field->if))
|
||||
{
|
||||
$if = $field->if;
|
||||
if(!is_array($if)) $if = array($if);
|
||||
foreach($if as $idx=>$cond) {
|
||||
foreach($if as $idx=>$cond)
|
||||
{
|
||||
$if[$idx] = (array)$cond->attrs;
|
||||
}
|
||||
$filter['if'] = $if;
|
||||
|
|
@ -165,8 +174,10 @@ class Validator
|
|||
* @param string $cache_dir Root cache directory
|
||||
* @return void
|
||||
*/
|
||||
function setCacheDir($cache_dir){
|
||||
if(is_dir($cache_dir)) {
|
||||
function setCacheDir($cache_dir)
|
||||
{
|
||||
if(is_dir($cache_dir))
|
||||
{
|
||||
$this->_cache_dir = preg_replace('@/$@', '', $cache_dir);
|
||||
}
|
||||
}
|
||||
|
|
@ -176,10 +187,14 @@ class Validator
|
|||
* @param array $fields Target fields. The keys of the array represents field's name, its values represents field's value.
|
||||
* @return boolean TRUE if it is valid, FALSE otherwise.
|
||||
*/
|
||||
function validate($fields_=null) {
|
||||
if(is_array($fields_)) {
|
||||
function validate($fields_=null)
|
||||
{
|
||||
if(is_array($fields_))
|
||||
{
|
||||
$fields = $fields_;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
$args = array_keys($this->_filters);
|
||||
$fields = (array)Context::getRequestVars();
|
||||
}
|
||||
|
|
@ -187,14 +202,14 @@ class Validator
|
|||
if(!is_array($fields)) return true;
|
||||
|
||||
$filter_default = array(
|
||||
'required' => 'false',
|
||||
'default' => '',
|
||||
'modifiers' => array(),
|
||||
'length' => 0,
|
||||
'equalto' => 0,
|
||||
'rule' => 0,
|
||||
'if' => array()
|
||||
);
|
||||
'required' => 'false',
|
||||
'default' => '',
|
||||
'modifiers' => array(),
|
||||
'length' => 0,
|
||||
'equalto' => 0,
|
||||
'rule' => 0,
|
||||
'if' => array()
|
||||
);
|
||||
|
||||
$fields = array_map(array($this, 'arrayTrim'), $fields);
|
||||
$field_names = array_keys($fields);
|
||||
|
|
@ -202,11 +217,15 @@ class Validator
|
|||
$filters = array();
|
||||
|
||||
// get field names matching patterns
|
||||
foreach($this->_filters as $key=>$filter) {
|
||||
foreach($this->_filters as $key=>$filter)
|
||||
{
|
||||
$names = array();
|
||||
if($key{0} == '^') {
|
||||
if($key{0} == '^')
|
||||
{
|
||||
$names = preg_grep('/^'.preg_quote(substr($key,1)).'/', $field_names);
|
||||
}elseif(substr($key,-2) == '[]'){
|
||||
}
|
||||
elseif(substr($key,-2) == '[]')
|
||||
{
|
||||
$filters[substr($key,0,-2)] = $filter;
|
||||
unset($filters[$key]);
|
||||
}
|
||||
|
|
@ -217,14 +236,15 @@ class Validator
|
|||
|
||||
if(!count($names)) continue;
|
||||
|
||||
foreach($names as $name) {
|
||||
foreach($names as $name)
|
||||
{
|
||||
$filters[$name] = $filter;
|
||||
}
|
||||
|
||||
unset($filters[$key]);
|
||||
}
|
||||
|
||||
foreach($filters as $key=>$filter) {
|
||||
foreach($filters as $key=>$filter)
|
||||
{
|
||||
$fname = preg_replace('/\[\]$/', '', $key);
|
||||
$filter = array_merge($filter_default, $filter);
|
||||
|
||||
|
|
@ -239,16 +259,21 @@ class Validator
|
|||
$value = $exists ? $fields[$fname] : null;
|
||||
}
|
||||
|
||||
if(is_array($value)) {
|
||||
if(!isset($value[tmp_name])){
|
||||
$value = implode('', $value);
|
||||
}else{
|
||||
$value = $value['name'];
|
||||
}
|
||||
}
|
||||
if(is_array($value))
|
||||
{
|
||||
if(!isset($value[tmp_name]))
|
||||
{
|
||||
$value = implode('', $value);
|
||||
}
|
||||
else
|
||||
{
|
||||
$value = $value['name'];
|
||||
}
|
||||
}
|
||||
|
||||
// conditional statement
|
||||
foreach($filter['if'] as $cond) {
|
||||
foreach($filter['if'] as $cond)
|
||||
{
|
||||
if(!isset($cond['test']) || !isset($cond['attr'])) continue;
|
||||
|
||||
$func_body = preg_replace('/\\$(\w+)/', '$c[\'$1\']', $cond['test']);
|
||||
|
|
@ -258,7 +283,8 @@ class Validator
|
|||
}
|
||||
|
||||
// attr : default
|
||||
if(!$value && strlen($default=trim($filter['default']))) {
|
||||
if(!$value && strlen($default=trim($filter['default'])))
|
||||
{
|
||||
$value = $default;
|
||||
if(is_null($fields_)) Context::set($fname, $value);
|
||||
else $fields_[$fname] = $value;
|
||||
|
|
@ -275,14 +301,16 @@ class Validator
|
|||
if(!$exists && !$value_len) continue;
|
||||
|
||||
// attr : length
|
||||
if($length=$filter['length']){
|
||||
if($length=$filter['length'])
|
||||
{
|
||||
list($min, $max) = explode(':', trim($length));
|
||||
$is_min_b = (substr($min, -1) === 'b');
|
||||
$is_max_b = (substr($max, -1) === 'b');
|
||||
list($min, $max) = array((int)$min, (int)$max);
|
||||
|
||||
$strbytes = strlen($value);
|
||||
if(!$is_min_b || !$is_max_b) {
|
||||
if(!$is_min_b || !$is_max_b)
|
||||
{
|
||||
$strlength = $this->_has_mb_func?mb_strlen($value,'utf-8'):$this->mbStrLen($value);
|
||||
}
|
||||
|
||||
|
|
@ -290,14 +318,17 @@ class Validator
|
|||
}
|
||||
|
||||
// equalto
|
||||
if($equalto=$filter['equalto']){
|
||||
if($equalto=$filter['equalto'])
|
||||
{
|
||||
if(!array_key_exists($equalto, $fields) || trim($fields[$equalto]) !== $value) return $this->error($key, 'equalto');
|
||||
}
|
||||
|
||||
// rules
|
||||
if($rules=$filter['rule']){
|
||||
if($rules=$filter['rule'])
|
||||
{
|
||||
$rules = explode(',', $rules);
|
||||
foreach($rules as $rule) {
|
||||
foreach($rules as $rule)
|
||||
{
|
||||
$result = $this->applyRule($rule, $value);
|
||||
// apply the 'not' modifier
|
||||
if(in_array('not', $modifiers)) $result = !$result;
|
||||
|
|
@ -316,7 +347,7 @@ class Validator
|
|||
*/
|
||||
function arrayTrim($array)
|
||||
{
|
||||
if (!is_array($array)) return trim($array);
|
||||
if(!is_array($array)) return trim($array);
|
||||
|
||||
foreach($array as $key => $value)
|
||||
{
|
||||
|
|
@ -331,7 +362,8 @@ class Validator
|
|||
* @param $msg error message
|
||||
* @return boolean always false
|
||||
*/
|
||||
function error($field, $msg){
|
||||
function error($field, $msg)
|
||||
{
|
||||
$lang_filter = Context::getLang('filter');
|
||||
$msg = isset($lang_filter->{$msg})?$lang_filter->{$msg}:$lang_filter->invalid;
|
||||
$msg = sprintf($msg, Context::getLang($field));
|
||||
|
|
@ -345,7 +377,8 @@ class Validator
|
|||
* Returns the last error infomation including a field name and an error message.
|
||||
* @return array The last error infomation
|
||||
*/
|
||||
function getLastError(){
|
||||
function getLastError()
|
||||
{
|
||||
return $this->_last_error;
|
||||
}
|
||||
|
||||
|
|
@ -355,15 +388,18 @@ class Validator
|
|||
* @param mixed $rule
|
||||
* @return void
|
||||
*/
|
||||
function addRule($name, $rule=''){
|
||||
function addRule($name, $rule='')
|
||||
{
|
||||
if(is_array($name)) $args = $name;
|
||||
else $args = array($name=>$rule);
|
||||
|
||||
foreach($args as $name=>$rule){
|
||||
foreach($args as $name=>$rule)
|
||||
{
|
||||
if(!$rule) continue;
|
||||
if(is_string($rule)) $rule = array('type'=>'regex', 'test'=>$rule);
|
||||
|
||||
if($rule['type'] == 'enum') {
|
||||
if($rule['type'] == 'enum')
|
||||
{
|
||||
$delim = isset($rule['delim'])?$rule['delim']:',';
|
||||
$rule['test'] = explode($delim, $rule['test']);
|
||||
}
|
||||
|
|
@ -377,7 +413,8 @@ class Validator
|
|||
* @param string $name rule name
|
||||
* @return void
|
||||
*/
|
||||
function removeRule($name){
|
||||
function removeRule($name)
|
||||
{
|
||||
unset($this->_rules[$name]);
|
||||
}
|
||||
|
||||
|
|
@ -387,18 +424,24 @@ class Validator
|
|||
* @param string $filter filter
|
||||
* @return void
|
||||
*/
|
||||
function addFilter($name, $filter='') {
|
||||
function addFilter($name, $filter='')
|
||||
{
|
||||
if(is_array($name)) $args = $name;
|
||||
else $args = array($name=>$filter);
|
||||
|
||||
foreach($args as $name=>$filter) {
|
||||
foreach($args as $name=>$filter)
|
||||
{
|
||||
if(!$filter) continue;
|
||||
|
||||
if(isset($filter['if'])) {
|
||||
if(is_array($filter['if']) && count($filter['if'])) {
|
||||
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']);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
unset($filter['if']);
|
||||
}
|
||||
}
|
||||
|
|
@ -412,7 +455,8 @@ class Validator
|
|||
* @param string $name rule name
|
||||
* @return void
|
||||
*/
|
||||
function removeFilter($name) {
|
||||
function removeFilter($name)
|
||||
{
|
||||
unset($this->_filters[$name]);
|
||||
}
|
||||
|
||||
|
|
@ -422,7 +466,8 @@ class Validator
|
|||
* @param string $value a value to be validated
|
||||
* @return boolean TRUE if the field is valid, FALSE otherwise.
|
||||
*/
|
||||
function applyRule($name, $value){
|
||||
function applyRule($name, $value)
|
||||
{
|
||||
$rule = $this->_rules[$name];
|
||||
|
||||
if (is_array($value) && isset($value['tmp_name']))
|
||||
|
|
@ -430,13 +475,15 @@ class Validator
|
|||
$value = $value['name'];
|
||||
}
|
||||
|
||||
switch($rule['type']) {
|
||||
switch($rule['type'])
|
||||
{
|
||||
case 'regex':
|
||||
return (preg_match($rule['test'], $value) > 0);
|
||||
case 'enum':
|
||||
return in_array($value, $rule['test']);
|
||||
case 'expr':
|
||||
if(!$rule['func_test']) {
|
||||
if(!$rule['func_test'])
|
||||
{
|
||||
$rule['func_test'] = create_function('$a', 'return ('.preg_replace('/\$\$/', '$a', html_entity_decode($rule['test'])).');');
|
||||
}
|
||||
return $rule['func_test']($value);
|
||||
|
|
@ -450,9 +497,11 @@ class Validator
|
|||
* @param string $str
|
||||
* @return int
|
||||
*/
|
||||
function mbStrLen($str){
|
||||
function mbStrLen($str)
|
||||
{
|
||||
$arr = count_chars($str);
|
||||
for($i=0x80; $i < 0xc0; $i++) {
|
||||
for($i=0x80; $i < 0xc0; $i++)
|
||||
{
|
||||
unset($arr[$i]);
|
||||
}
|
||||
return array_sum($arr);
|
||||
|
|
@ -462,7 +511,8 @@ class Validator
|
|||
* Returns compiled javascript file path. The path begins from XE root directory.
|
||||
* @return string Compiled JavaScript file path
|
||||
*/
|
||||
function getJsPath(){
|
||||
function getJsPath()
|
||||
{
|
||||
if(!$this->_cache_dir) return false;
|
||||
|
||||
$dir = $this->_cache_dir.'/ruleset';
|
||||
|
|
@ -479,11 +529,15 @@ class Validator
|
|||
$content = $this->_compile2js();
|
||||
if($content === false) return false;
|
||||
|
||||
if(is_callable('file_put_contents')) {
|
||||
if(is_callable('file_put_contents'))
|
||||
{
|
||||
@file_put_contents($filepath, $content);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
$fp = @fopen($filepath, 'w');
|
||||
if(is_resource($fp)) {
|
||||
if(is_resource($fp))
|
||||
{
|
||||
fwrite($fp, $content);
|
||||
fclose($fp);
|
||||
}
|
||||
|
|
@ -496,7 +550,8 @@ class Validator
|
|||
* Compile a ruleset to a javascript file
|
||||
* @return string
|
||||
*/
|
||||
function _compile2js() {
|
||||
function _compile2js()
|
||||
{
|
||||
global $lang;
|
||||
|
||||
$ruleset = basename($this->_xml_path,'.xml');
|
||||
|
|
@ -511,9 +566,11 @@ class Validator
|
|||
|
||||
// custom rulesets
|
||||
$addrules = array();
|
||||
foreach($this->_rules as $name=>$rule) {
|
||||
foreach($this->_rules as $name=>$rule)
|
||||
{
|
||||
if(strpos('email,userid,url,alpha,alpha_number,number,', $name.',') !== false) continue;
|
||||
switch($rule['type']) {
|
||||
switch($rule['type'])
|
||||
{
|
||||
case 'regex':
|
||||
$content[] = "v.cast('ADD_RULE', ['{$name}', {$rule['test']}]);";
|
||||
break;
|
||||
|
|
@ -531,11 +588,13 @@ class Validator
|
|||
// filters
|
||||
$content = array();
|
||||
$messages = array();
|
||||
foreach($this->_filters as $name=>$filter) {
|
||||
foreach($this->_filters as $name=>$filter)
|
||||
{
|
||||
$field = array();
|
||||
|
||||
// form filed name
|
||||
if(isset($lang->{$name})) {
|
||||
if(isset($lang->{$name}))
|
||||
{
|
||||
$field_lang = addslashes($lang->{$name});
|
||||
$messages[] = "v.cast('ADD_MESSAGE',['{$name}','{$field_lang}']);";
|
||||
}
|
||||
|
|
@ -544,20 +603,24 @@ class Validator
|
|||
if($filter['rule']) $field[] = "rule:'{$filter['rule']}'";
|
||||
if($filter['default']) $field[] = "default:'{$filter['default']}'";
|
||||
if($filter['modifier']) $field[] = "modifier:'{$filter['modifier']}'";
|
||||
if($filter['length']) {
|
||||
if($filter['length'])
|
||||
{
|
||||
list($min, $max) = explode(':', $filter['length']);
|
||||
if($min) $field[] = "minlength:'{$min}'";
|
||||
if($max) $field[] = "maxlength:'{$max}'";
|
||||
}
|
||||
if($filter['if']) {
|
||||
if($filter['if'])
|
||||
{
|
||||
$ifs = array();
|
||||
if(!isset($filter['if'][0])) $filter['if'] = array($filter['if']);
|
||||
foreach($filter['if'] as $if) {
|
||||
foreach($filter['if'] as $if)
|
||||
{
|
||||
$ifs[] = "{test:'".addslashes($if['test'])."', attr:'{$if['attr']}', value:'".addslashes($if['value'])."'}";
|
||||
}
|
||||
$field[] = "'if':[".implode(',', $ifs)."]";
|
||||
}
|
||||
if(count($field)) {
|
||||
if(count($field))
|
||||
{
|
||||
$field = '{'.implode(',', $field).'}';
|
||||
$content[] = "'{$name}':{$field}";
|
||||
}
|
||||
|
|
@ -566,8 +629,10 @@ class Validator
|
|||
if(!$content) return '/* Error : empty ruleset */';
|
||||
|
||||
// error messages
|
||||
foreach($lang->filter as $key=>$text) {
|
||||
if($text) {
|
||||
foreach($lang->filter as $key=>$text)
|
||||
{
|
||||
if($text)
|
||||
{
|
||||
$text = preg_replace('@\r?\n@', '\\n', addslashes($text));
|
||||
$messages[] = "v.cast('ADD_MESSAGE',['{$key}','{$text}']);";
|
||||
}
|
||||
|
|
@ -579,6 +644,5 @@ 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