View file sys/plugins/classes/ac_method.class.php

File size: 3.98Kb
<?php	
	class ac_method
	{
		private $method = array(), $value, $option, $type, $result = null;
		private static $option_all = array();
		private function has_option($option)
		{
			return (in_array($option, self::$option_all)?false:true);
		}
		private function set_option($option)
		{
			array_push(self::$option_all, $option);
			$this -> option = $option; 
		}
		private function __construct($option, $method, $value, $type)
		{
			$this -> set_option($option);
			$this -> set_method($method);
			$this -> set_type($type); 
			$this -> value = $value;
			$this -> result();
		}
		public static function set($option, $method, $value = null, $type = null)
		{
			if($m = new self($option, $method, $value, $type))
			{
				if(!is_null($m -> result))
				{
					echo eval($m -> result); 
					if(isset($return))
					{
						if(in_array('$_SESSION', $m -> method))
						 $_SESSION[$m -> option] = $return;
						if(in_array('$_COOKIE', $m -> method))
						 setcookie($m -> option, $return, time() + 60 * 60 * 24 * 365, '/');
						return $return;
					}
					else return false;
				}
			}
		}
		private function has_method($method)
		{
			return (preg_match("#[^GPSC]#si", $method)?false:true);
		}
		private function set_method($method)
		{
			if(!$this -> has_method($method))
			 trigger_error('Ошибка '.(__CLASS__).', '.(__FUNCTION__).'! Допустимые значения G - _GET, P - _POST, S - _SESSION, C - _COOKIE. Проверьте правильность ввода'); 
			$this -> get_method($method);
		}
		private function get_method($method)
		{
			$method = strtoupper($method);
			for($i = 0;$i < strlen($method);$i++)
			{
				switch($method[$i]):
					case 'P': 
					 $this -> method[] = "\$_POST";
					break;
					case 'S':
					 $this -> method[] = "\$_SESSION";
					break;
					case 'C':
					 $this -> method[] = "\$_COOKIE";
					break; 
					default: 
					case 'G':
					 $this -> method[] = "\$_GET";
					break;
				endswitch;
			}
		}
		private function create($i, $method)
		{
			$return = ($i > 0?'else':'')."if(isset({$this -> method[$i]}['{$this -> option}'])";
			if(!is_null($this -> type))
			 $return.= $this -> get_type($i, $method); 
			$return.= ")\r\n";
			$return.= " \$return = {$this -> method[$i]}['{$this -> option}'];\r\n";
			return $return;
		}
		private function has_type($type)
		{
			return (preg_match("#[^FBIS]#si", $type)?false:true);
		}
		private function get_type($i, $method)
		{
			switch($this -> type):
				case 'I':
				 $return = " && !preg_match(\"#[^0-9]#s\", {$this -> method[$i]}['{$this -> option}'])";
				break;
				case 'F':
				 $return = " && preg_match(\"#[0-9]\.[0-9]#s\", {$this -> method[$i]}['{$this -> option}'])";
				break;
				case 'B': 
				 $return = " && ac_file::is_blob({$this -> method[$i]}['{$this -> option}'])";
				break;
				case 'S':
				default: 
				 $return = " && is_string({$this -> method[$i]}['{$this -> option}'])";
				break;
			endswitch;
			return $return;
		}
		private function set_type($type)
		{
			if(!is_null($type))
			{
				if(!$this -> has_type($type))
				 trigger_error('Ошибка '.(__CLASS__).', '.(__FUNCTION__).'! Допустимые значения I - INTEGER, F - FLOAT, S - STRING, B - BLOB. Проверьте правильность ввода!');
				$this -> type = strtoupper($type);
			}
		}
		private function result()
		{
			foreach($this -> method as $i => $post)
			 $this -> result.= $this -> create($i, $post);
			$this -> is_empty();
		}
		private function is_empty()
		{
			if(!is_null($this -> value))
			{
				$ex_value = explode('|', $this -> value);
				$this -> result.= "if(empty(\$return)";
				if(count($ex_value) >= 2)
				 $this -> result.= " || !preg_match(\"#".join('|', $ex_value)."#umi\", \$return)";
				$this -> result.= ")\r\n";
				$this -> result.= " \$return = \"{$ex_value[0]}\";\r\n";
			}
		}
		public function __destruct()
		{
			unset($this);
		}
	}
?>