<commit_before><?php
/**
 * Validator class
 */
class Validator
{
	var $_cache_dir = '';
	var $_last_error;
	var $_xml_ruleset = null;
	var $_rules;
	var $_filters;
	var $_has_mb_func;
	var $_version = '1.0';
	var $_xml_path = '';

	/**
	 * @constructor
	 */
	function Validator($xml_path=''){
		$this->_rules   = array();
		$this->_filters = array();
		$this->_xml_ruleset = null;

		if($xml_path) $this->load($xml_path);
		
		// 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)$/'
		));

		$this->_has_mb_func = is_callable('mb_strlen');
		$this->setCacheDir('./files/cache');
	}

	/**
	 * Load a xml file
	 * @param[in] string $xml_path A file name to be loaded
	 */
	function load($xml_path) {
		$this->_xml_ruleset = null;

		$xml_path = realpath($xml_path);
		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;

		// custom rules
		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) {
				if(!isset($rule->attrs) || !isset($rule->attrs->name)) continue;

				$rule = (array)$rule->attrs;
				$name = $rule['name'];
				unset($rule['name']);

				$rules[$name] = $rule;
			}
			if(count($rules)) $this->addRule($rules);
		}

		// filters
		$fields = $xml->ruleset->fields->field;
		if(!is_array($fields)) $fields = array($fields);

		$filters = array();
		foreach($fields as $field) {
			$name   = '';
			$filter = array();

			if(!isset($field->attrs) || !isset($field->attrs->name)) continue;
			$filter = (array)$field->attrs;

			$name = $filter['name'];
			unset($filter['name']);

			// conditional statement
			if(isset($field->if)) {
				$if = $field->if;
				if(!is_array($if)) $if = array($if);
				foreach($if as $idx=>$cond) {
					$if[$idx] = (array)$cond->attrs;
				}
				$filter['if'] = $if;
			}

			$filters[$name] = $filter;
		}

		$this->_xml_ruleset = $xml->ruleset;
		$this->_filters  = $filters;
		$this->_xml_path = $xml_path;

		return true;
	}

	/**
	 * Set root cache directory
	 * @param[in] string $cache_dir Root cache directory
	 */
	function setCacheDir($cache_dir){
		if(is_dir($cache_dir)) {
			$this->_cache_dir = preg_replace('@/$@', '', $cache_dir);
		}
	}

	/**
	 * Validate the fields. If the fields aren't passed, validation will be execute on the Context variables.
	 * @param[in] (optional) array $fields Target fields. The keys of the array represents field's name, its values represents field's value.
	 * @return bool True if it is valid, FALSE otherwise.
	 */
	function validate($fields_=array()){
		if(is_array($fields_)) {
			$fields = $fields_;
		} else {
			$args   = array_keys($this->_filters);
			$fields = (array)Context::getRequestVars();
		}

		if(!is_array($fields) || !count($fields)) return true;
<commit_msg>Fixed a buf for validation<commit_after><?php
/**
 * Validator class
 */
class Validator
{
	var $_cache_dir = '';
	var $_last_error;
	var $_xml_ruleset = null;
	var $_rules;
	var $_filters;
	var $_has_mb_func;
	var $_version = '1.0';
	var $_xml_path = '';

	/**
	 * @constructor
	 */
	function Validator($xml_path=''){
		$this->_rules   = array();
		$this->_filters = array();
		$this->_xml_ruleset = null;

		if($xml_path) $this->load($xml_path);
		
		// 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)$/'
		));

		$this->_has_mb_func = is_callable('mb_strlen');
		$this->setCacheDir('./files/cache');
	}

	/**
	 * Load a xml file
	 * @param[in] string $xml_path A file name to be loaded
	 */
	function load($xml_path) {
		$this->_xml_ruleset = null;

		$xml_path = realpath($xml_path);
		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;

		// custom rules
		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) {
				if(!isset($rule->attrs) || !isset($rule->attrs->name)) continue;

				$rule = (array)$rule->attrs;
				$name = $rule['name'];
				unset($rule['name']);

				$rules[$name] = $rule;
			}
			if(count($rules)) $this->addRule($rules);
		}

		// filters
		$fields = $xml->ruleset->fields->field;
		if(!is_array($fields)) $fields = array($fields);

		$filters = array();
		foreach($fields as $field) {
			$name   = '';
			$filter = array();

			if(!isset($field->attrs) || !isset($field->attrs->name)) continue;
			$filter = (array)$field->attrs;

			$name = $filter['name'];
			unset($filter['name']);

			// conditional statement
			if(isset($field->if)) {
				$if = $field->if;
				if(!is_array($if)) $if = array($if);
				foreach($if as $idx=>$cond) {
					$if[$idx] = (array)$cond->attrs;
				}
				$filter['if'] = $if;
			}

			$filters[$name] = $filter;
		}

		$this->_xml_ruleset = $xml->ruleset;
		$this->_filters  = $filters;
		$this->_xml_path = $xml_path;

		return true;
	}

	/**
	 * Set root cache directory
	 * @param[in] string $cache_dir Root cache directory
	 */
	function setCacheDir($cache_dir){
		if(is_dir($cache_dir)) {
			$this->_cache_dir = preg_replace('@/$@', '', $cache_dir);
		}
	}

	/**
	 * Validate the fields. If the fields aren't passed, validation will be execute on the Context variables.
	 * @param[in] (optional) array $fields Target fields. The keys of the array represents field's name, its values represents field's value.
	 * @return bool True if it is valid, FALSE otherwise.
	 */
	function validate($fields_=null){
		if(is_array($fields_)) {
			$fields = $fields_;
		} else {
			$args   = array_keys($this->_filters);
			$fields = (array)Context::getRequestVars();
		}

		if(!is_array($fields) || !count($fields)) return true;

{'fraction_non_alphanumeric': 0.22673384706579727, 'fraction_numerical': 0.0038529934795494963, 'mean_word_length': 3.472498343273691, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 52}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5815061', 'n_tokens_mistral': 2788, 'n_tokens_neox': 2447, 'n_words': 466}