<commit_before><?php

/**
 * GUMP - A fast, extensible PHP input validation class
 *
 * @author		<NAME> (http://twitter.com/SeanNieuwoudt)
 * @copyright	Copyright (c) 2011 Wixel.net
 * @link		http://github.com/Wixel/GUMP
 * @version     0.5
 */

class GUMP
{	
	// ** ------------------------- Validation Helpers ---------------------------- ** //	
	
	/**
	 * Perform XSS clean to prevent cross site scripting
	 * 
	 * @static
	 * @access public
	 * @param  array $data
	 * @return array
	 */
	public static function xss_clean(array $data)
	{
		foreach($data as $k => $v)
		{
			$data[$k] = filter_var($v, FILTER_SANITIZE_STRING);		
		}
		
		return $data;
	}
	
	/**
	 * Sanitize the input data
	 * 
	 * @static
	 * @access public
	 * @param  array $data
	 * @return array
	 */
	public static function sanitize(array $input, $fields = NULL)
	{
		$magic_quotes = (bool)get_magic_quotes_gpc();
		
		if(is_null($fields))
		{
			$fields = array_keys($input);
		}

		foreach($fields as $field)
		{
			if(!isset($input[$field]))
			{
				continue;
			}
			else
			{
				$value = $input[$field]; 
				
				if(is_string($value))
				{
					if($magic_quotes === TRUE)
					{
						$value = stripslashes($value);
					}
					
					if(strpos($value, "\r") !== FALSE)
					{
						$value = trim($value);
					}
					
					if(function_exists('iconv'))
					{
						$value = iconv('ISO-8859-1', 'UTF-8', $value);
					}
					
					$value = filter_var($value, FILTER_SANITIZE_STRING);		
				}
				
				$input[$field] = $value;
			}
		}
		
		return $input;		
	}
	
	/**
	 * Perform data validation against the provided ruleset
	 * 
	 * @static
	 * @access public
	 * @param  mixed $input
	 * @param  array $ruleset
	 * @return mixed
	 */
	public static function validate(array $input, array $ruleset)
	{
		$errors = array();
		
		foreach($ruleset as $field => $rules)
		{
			$rules = explode('|', $rules);

			foreach($rules as $rule)
			{
				$method = NULL;
				$param  = NULL;
				
				// @TODO: Improve the param parser
				
				if(strstr($rule, ',') !== FALSE) // has params
				{
					$rule   = explode(',', $rule);
					$method = 'validate_'.$rule[0];
					$param  = $rule[1];
				}
				else
				{
					$method = 'validate_'.$rule;
				}

				if(method_exists('GUMP', $method))
				{
					$result = GUMP::$method($field, $input, $param);

					if(is_array($result)) // Validation Failed
					{
						$errors[] = $result;
					}
				}
				else
				{
					throw new Exception("Validator method '$method' does not exist.");
				}
			}
		}

		return (count($errors) > 0)? $errors : TRUE;
	}
	
	/**
	 * Filter the input data according to the specified filter set 
	 * 
	 * @static
	 * @access public
	 * @param  mixed $input
	 * @param  array $filterset
	 * @return mixed
	 */
	public static function filter(array $input, array $filterset)
	{
		foreach($filterset as $field => $filters)
		{
			$filters = explode('|', $filters);

			foreach($filters as $filter)
			{	
				$method = 'filter_'.$filter;

				if(method_exists('GUMP', $method))
				{
					$input[$field] = GUMP::$method($input[$field]);
				}
				else
				{<commit_msg>Allow support for php functions as a filter<commit_after><?php

/**
 * GUMP - A fast, extensible PHP input validation class
 *
 * @author		<NAME> (http://twitter.com/SeanNieuwoudt)
 * @copyright	Copyright (c) 2011 Wixel.net
 * @link		http://github.com/Wixel/GUMP
 * @version     0.5
 */

class GUMP
{	
	// ** ------------------------- Validation Helpers ---------------------------- ** //	
	
	/**
	 * Perform XSS clean to prevent cross site scripting
	 * 
	 * @static
	 * @access public
	 * @param  array $data
	 * @return array
	 */
	public static function xss_clean(array $data)
	{
		foreach($data as $k => $v)
		{
			$data[$k] = filter_var($v, FILTER_SANITIZE_STRING);		
		}
		
		return $data;
	}
	
	/**
	 * Sanitize the input data
	 * 
	 * @static
	 * @access public
	 * @param  array $data
	 * @return array
	 */
	public static function sanitize(array $input, $fields = NULL)
	{
		$magic_quotes = (bool)get_magic_quotes_gpc();
		
		if(is_null($fields))
		{
			$fields = array_keys($input);
		}

		foreach($fields as $field)
		{
			if(!isset($input[$field]))
			{
				continue;
			}
			else
			{
				$value = $input[$field]; 
				
				if(is_string($value))
				{
					if($magic_quotes === TRUE)
					{
						$value = stripslashes($value);
					}
					
					if(strpos($value, "\r") !== FALSE)
					{
						$value = trim($value);
					}
					
					if(function_exists('iconv'))
					{
						$value = iconv('ISO-8859-1', 'UTF-8', $value);
					}
					
					$value = filter_var($value, FILTER_SANITIZE_STRING);		
				}
				
				$input[$field] = $value;
			}
		}
		
		return $input;		
	}
	
	/**
	 * Perform data validation against the provided ruleset
	 * 
	 * @static
	 * @access public
	 * @param  mixed $input
	 * @param  array $ruleset
	 * @return mixed
	 */
	public static function validate(array $input, array $ruleset)
	{
		$errors = array();
		
		foreach($ruleset as $field => $rules)
		{
			$rules = explode('|', $rules);

			foreach($rules as $rule)
			{
				$method = NULL;
				$param  = NULL;
				
				// @TODO: Improve the param parser
				
				if(strstr($rule, ',') !== FALSE) // has params
				{
					$rule   = explode(',', $rule);
					$method = 'validate_'.$rule[0];
					$param  = $rule[1];
				}
				else
				{
					$method = 'validate_'.$rule;
				}

				if(method_exists('GUMP', $method))
				{
					$result = GUMP::$method($field, $input, $param);

					if(is_array($result)) // Validation Failed
					{
						$errors[] = $result;
					}
				}
				else
				{
					throw new Exception("Validator method '$method' does not exist.");
				}
			}
		}

		return (count($errors) > 0)? $errors : TRUE;
	}
	
	/**
	 * Filter the input data according to the specified filter set 
	 * 
	 * @static
	 * @access public
	 * @param  mixed $input
	 * @param  array $filterset
	 * @return mixed
	 */
	public static function filter(array $input, array $filterset)
	{
		foreach($filterset as $field => $filters)
		{
			$filters = explode('|', $filters);

			foreach($filters as $filter)
			{	
				$method = 'filter_'.$filter;
				
				if(method_exists('GUMP', $method))
				{
					$input[$field] = GUMP::$method($input[$field]);
				}
				else if(function_exists($method))
				{
					$input[$field] = $method($input[$field]);
				}
				else
				{
{'fraction_non_alphanumeric': 0.17670177645024368, 'fraction_numerical': 0.004716239584970917, 'mean_word_length': 2.2659137577002055, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '55167', 'n_tokens_mistral': 2850, 'n_tokens_neox': 2146, 'n_words': 489}