验证上传文件大小
@access protected
@param mixed     $file  上传文件
@param mixed     $rule  验证规则
@return bool
554139
protected function fileSize($file, $rule)
    {
        if (!($file instanceof File)) {
            return false;
        }
        if (is_array($file)) {
            foreach ($file as $item) {
                if (!$item->checkSize($rule)) {
                    return false;
                }
            }
            return true;
        } else {
            return $file->checkSize($rule);
        }
    }
1208649
public function useEncoding($rule)
    {
        if ($rule->encoding === null) {
            return false;
        }

        if (! function_exists('mb_strtolower')) {
            throw new FilterException('mbstring is required to use ToLower with an encoding.');
        }

        $encodings = array_map('strtolower', mb_list_encodings());

        if (!in_array(strtolower($rule->encoding), $encodings)) {
            throw new FilterException(
                "mbstring does not support the '".$rule->encoding."' encoding"
            );
        }

        return true;
    }
1068263
protected function maxValidate($value, $rule)
    {
        if (is_array($value)) {
            $length = count($value);
        }else {
            $length = mb_strlen((string) $value);
        }
        return $length <= $rule;
    }
821473
public function is($value, $rule, $data = [])
    {
        $rule = preg_replace_callback('/_([a-zA-Z])/', function ($match) {
            return strtoupper($match[1]);
        }, $rule);

        switch (lcfirst($rule)) {
            case 'require':
                // 必须
                $result = !empty($value) || '0' == $value;
                break;
            case 'accepted':
                // 接受
                $result = in_array($value, ['1', 'on', 'yes']);
                break;
            case 'date':
                // 是否是一个有效日期
                $result = false !== strtotime($value);
                break;
            case 'activeUrl':
                // 是否为有效的网址
                $result = checkdnsrr($value);
                break;
            case 'boolean':
            case 'bool':
                // 是否为布尔值
                $result = in_array($value, [true, false, 0, 1, '0', '1'], true);
                break;
            case 'number':
                $result = is_numeric($value);
                break;
            case 'array':
                // 是否为数组
                $result = is_array($value);
                break;
            case 'file':
                $result = $value instanceof SplFileObject;
                break;
            case 'image':
                $result = $value instanceof SplFileObject && in_array($this->getImageType($value->getRealPath()), [1, 2, 3, 6]);
                break;
            default:
                if (isset(self::$type[$rule])) {
                    // 注册的验证规则
                    $result = call_user_func_array(self::$type[$rule], [$value]);
                } elseif (isset($this->filter[$rule])) {
                    // Filter_var验证规则
                    $result = $this->filter($value, $this->filter[$rule]);
                } else {
                    // 正则验证
                    $result = $this->regex($value, $rule);
                }
        }

        return $result;
    }
1209352
public function uploadimg($file)
    {
        if (!$file) {
            $this->setError('参数缺失');

            return false;
        }

        if (!file_exists($file)) {
            $this->setError('文件路径不正确');

            return false;
        }

        $data          = [];
        $data['media'] = '@' . realpath($file);

        $node = 'uploadimg';

        $res = $this->_post($node, $data, false);

        return $res;
    }
291152
public function image( $file, $rule )
	{
		if( !($file instanceof File) ){
			return false;
		}
		if( $rule ){
			$rule = explode( ',', $rule );
			list( $width, $height, $type ) = getimagesize( $file->getRealPath() );
			if( isset( $rule[2] ) ){
				$imageType = strtolower( $rule[2] );
				if( 'jpeg' == $imageType ){
					$imageType = 'jpg';
				}
				if( image_type_to_extension( $type, false ) != $imageType ){
					return false;
				}
			}
			list( $w, $h ) = $rule;
			return $w == $width && $h == $height;
		} else{
			return in_array( $this->getImageType( $file->getRealPath() ), [1, 2, 3, 6] );
		}
	}
654786
public function evaluate(Rule $rule)
    {
        if (is_array($rule->getLeft()) && is_array($rule->getRight())) {
            $intersect = array_intersect($rule->getLeft(), $rule->getRight());
            if (array_diff($rule->getRight(), $intersect))
                return false;
            return true;
        } elseif (is_array($rule->getLeft()) && !is_array($rule->getRight())) {
            return in_array($rule->getRight(), $rule->getLeft());
        } elseif (!is_array($rule->getLeft()) && is_array($rule->getRight())) {
            return in_array($rule->getLeft(), $rule->getRight());
        }

        return false;
    }
264720
public function parseFile($file)
    {
        if (!file_exists($file)) {
            return false;
        }
        $fh = fopen($file, 'r');
        if (!$fh) {
            return false;
        }
        $ret = $this->parseHandle($fh);
        fclose($fh);
        return $ret;
    }
646922
private function fixTransliteratorRule(string $rule): string
	{
		static $latinAsciiFix;
		static $deAsciiFix;

		if ($latinAsciiFix === null) {
			$latinAsciiFix = \in_array('Latin-ASCII', \Transliterator::listIDs(), true)
				? false
				: file_get_contents(__DIR__.'/Resources/Latin-ASCII.txt')
			;
		}

		if ($deAsciiFix === null) {
			$deAsciiFix = \in_array('de-ASCII', \Transliterator::listIDs(), true)
				? false
				: file_get_contents(__DIR__.'/Resources/de-ASCII.txt')
			;
			if ($latinAsciiFix) {
				$deAsciiFix = str_replace('::Latin-ASCII;', $latinAsciiFix, $deAsciiFix);
			}
		}

		// Add the de-ASCII transform if a CLDR version lower than 32.0 is used.
		if ($deAsciiFix && $rule === 'de-ASCII') {
			return $deAsciiFix;
		}

		// Add the Latin-ASCII transform if a CLDR version lower than 1.9 is used.
		if ($latinAsciiFix && $rule === 'Latin-ASCII') {
			return $latinAsciiFix;
		}

		return $rule;
	}
277873
public static function parseDotRule($rule)
    {
        $result = preg_split('#(?<!\\\)\.#', $rule);
        array_walk($result, function(&$value, $key){
            if(false !== strpos($value,'\.'))
            {
                $value = str_replace('\.', '.', $value);
            }
        });
        return $result;
    }
1006933
public function save($file, $replace=true) {
        $filename = $this->rootPath . $file['savepath'] . $file['savename'];

        /* 不覆盖同名文件 */
        // if (!$replace && is_file($filename)) {
        //     $this->error = '存在同名文件' . $file['savename'];
        //     return false;
        // }

        /* 移动文件 */
        if (!ftp_put($this->link, $filename, $file['tmp_name'], FTP_BINARY)) {
            $this->error = '文件上传保存错误！';
            return false;
        }
        return true;
    }
1244962
public static function format($varName, $rule, $params) {
        $value = isset($rule['default']) ? $rule['default'] : NULL;
        $type = !empty($rule['type']) ? strtolower($rule['type']) : 'string';

        $key = isset($rule['name']) ? $rule['name'] : $varName;
        $value = isset($params[$key]) ? $params[$key] : $value;

        if ($value === NULL && $type != 'file') { //排除文件类型
            return $value;
        }

        return static::formatAllType($type, $value, $rule);
    }
171733
private function validateRule($rule): bool
    {
        if ($rule instanceof \Closure || $rule instanceof RuleInterface) {
            return true;
        }

        if (is_array($rule)) {
            return is_callable($rule, true);
        }

        if (is_string($rule) && class_exists($rule)) {
            try {
                $reflection = new \ReflectionClass($rule);
            } catch (\ReflectionException $e) {
                return false;
            }

            return $reflection->isSubclassOf(RuleInterface::class);
        }

        return false;
    }
1210612
private function checkCleanParamRule($rule, $path)
    {
        $cleanParam = $this->explodeCleanParamRule($rule);
        // check if path prefix matches the path of the url we're checking
        if (!$this->checkBasicRule($cleanParam['path'], $path)) {
            return false;
        }
        foreach ($cleanParam['param'] as $param) {
            if (!strpos($path, "?$param=")
                && !strpos($path, "&$param=")
            ) {
                return false;
            }
        }
        $this->log[] = 'Rule match: ' . self::DIRECTIVE_CLEAN_PARAM . ' directive';
        return true;
    }
1180786
public function check($rule = []) {
		$rule = $rule ?: $this->validate;

		/* 检查文件大小 */
		if (isset($rule['size']) && !$this->checkSize($rule['size'])) {
			$this->error = '上传文件大小不符！';
			return false;
		}

		/* 检查文件Mime类型 */
		if (isset($rule['type']) && !$this->checkMime($rule['type'])) {
			$this->error = '上传文件MIME类型不允许！';
			return false;
		}

		/* 检查文件后缀 */
		if (isset($rule['ext']) && !$this->checkExt($rule['ext'])) {
			$this->error = '上传文件后缀不允许';
			return false;
		}

		/* 检查图像文件 */
		if (!$this->checkImg()) {
			$this->error = '非法图像文件！';
			return false;
		}

		return true;
	}
508385
public function upload($file,$path){
        if( $this->validExt($file->getClientOriginalName())){
            if($file->getClientSize() > ($this->getMaxUploadFileSize() * 1024 * 1024) ){

                $result = array("query"=>"BE_UPLOAD_FILE_SIZE_NOT_SERVER","params"=>array($file->getClientSize()));
                $this->setInfo(array("msg"=>$result));

                if( $this->config['debug'] ) $this->_log(__METHOD__." - file size no permitido server: ".$file->getClientSize());
                return ;
            }elseif($file->getClientSize() > ($this->config['upload']['size_max'] * 1024 * 1024) ){

                $result = array("query"=>"BE_UPLOAD_FILE_SIZE_NOT_PERMITIDO","params"=>array($file->getClientSize()));
                $this->setInfo(array("msg"=>$result));

                if( $this->config['debug'] ) $this->_log(__METHOD__." - file size no permitido: ".$file->getClientSize());
                return ;
            }else{
                if($file->isValid()){
                    $dir = $this->getFullPath().$path;
                    $namefile = $file->getClientOriginalName();
                    $namefile = $this->clearNameFile($namefile);
                    $nametemp = $namefile;
                    if( $this->config["upload"]["overwrite"] ==false){
                        $ext = $this->getExtension($namefile);
                        $i=0;
                        while(true){
                            $pathnametemp = $dir.$nametemp;
                            if(file_exists($pathnametemp)){
                                $i++;
                                $nametemp = $this->removeExtension( $namefile ) . '_' . $i . '.' . $ext ;
                            }else{
                                break;
                            }
                        }
                    }
                    $file->move($dir,$nametemp);
                    $file = new  \SplFileInfo($dir.$nametemp);
                    return $file;
                }
            }
        }else{
            if( $this->config['debug'] ) $this->_log(__METHOD__." - file extension no permitido: ".$file->getExtension());
        }
    }
1133732
public function equals(Rule $rule)
    {
        if ($this->ruleHash !== $rule->ruleHash) {
            return false;
        }

        if (count($this->literals) != count($rule->literals)) {
            return false;
        }

        for ($i = 0, $n = count($this->literals); $i < $n; $i++) {
            if ($this->literals[$i] !== $rule->literals[$i]) {
                return false;
            }
        }

        return true;
    }