<?php
	// tomekk
        // http://tomekk.org/
        // GPL - http://www.gnu.org/copyleft/gpl.html
        // last fix: 13-01-2014

	class pgin
	{
		// separate arrays for GET and POST
		var $p = array(); // post array
		var $g = array(); // get array
		var $str_enc = ''; // force encoding for mb_strto*()

		// some defines at the start
		function pgin( $force_enc = '' )
		{
			define('HSC', 'html_special_chars');
		        define('NR', 'numbers');
		        define('LT', 'letters');
		        define('LTNR', 'letters_numbers');
		        define('CSR', 'custom_regexp');
		        define('ALLC', 'all_chars');

			// set enc
			if ($force_enc != '')
			{
				$this->str_enc = $force_enc;
			} else {
				$this->str_enc = mb_internal_encoding(); // default for mb_strto*() etc.
			}
		}

		// main parse function (input args)
		function parse( $src, $vars )
		{
			// temp array for vars
			$parsed_out = array();

			// which method?
			switch ($src)
			{
				case 'p':
					$method = $_POST;
					break;
				case 'g':
					$method = $_GET;
					break;
			}
	
			// only GET or POST
			if (isset($method))
			{
				foreach ($vars as $var => $type)
				{
					// what to do with the data - switches
					$data_format = '';

					// quick check if we need to do smth with the data
					// and remove our special format chars for further processing
					if (strpos($var, '^') !==  FALSE) // upper
					{
						$var = str_replace('^', '', $var);
						$data_format .= 'u';
					}
					
					if (strpos($var, ',') !==  FALSE) // lower
					{
						$var = str_replace(',', '', $var);
						$data_format .= 'l';
					}
					
					if (strpos($var, '-') !==  FALSE) // trim
					{
						$var = str_replace('-', '', $var);
						$data_format .= 't';
					}

					if (strpos($var, '*') !==  FALSE) // declare and set empty
					{
						$var = str_replace('*', '', $var);
						$data_format .= 's';
					}
	
					// only for custom_regexp - extracts key as type and value as pattern
					if (is_array($type))
					{
						$csr_key = key($type);
						$csr = $type[$csr_key];
						$type = $csr_key;
					}

					// input array arg is set, so go on
					if (isset($method[$var]))
					{
						// format data 
						if (strpos($data_format, 'u') !==  FALSE) $method[$var] = mb_strtoupper($method[$var], $this->str_enc);
						if (strpos($data_format, 'l') !==  FALSE) $method[$var] = mb_strtolower($method[$var], $this->str_enc);
						if (strpos($data_format, 't') !==  FALSE) $method[$var] = trim($method[$var]);

						// empty as default
						$parsed_out[$var] = '';

						// switch through types
						switch ($type)
						{
							case 'html_special_chars':
								$parsed_out[$var] = htmlspecialchars($method[$var], ENT_QUOTES);
								break;
							case 'numbers':
								if (preg_match("/^[0-9]+$/D", $method[$var])) $parsed_out[$var] = $method[$var];
								break;
							case 'letters':
								if (preg_match("/^[a-z]+$/Di", $method[$var])) $parsed_out[$var] = $method[$var];
								break;
							case 'letters_numbers':
								if (preg_match("/^[a-z0-9]+$/Di", $method[$var])) $parsed_out[$var] = $method[$var];
								break;
							case 'custom_regexp':
								if (preg_match($csr, $method[$var])) $parsed_out[$var] = $method[$var];
								break;
							case 'all_chars':
								$parsed_out[$var] = $method[$var];
								break;
						}
					} else {	
						// when input arg is declared but it is not set and the declaration has '*' -> declare it and set 'empty' (if we want ;))
						if (strpos($data_format, 's') !==  FALSE) $parsed_out[$var] = '';
					}
				}

				// assign proper array
				($src == 'p') ? $this->p = $parsed_out : $this->g = $parsed_out;
			}
		}
	}
?>
