<?php 

	class Session{
		public static function ID($id){
			session_id(preg_replace("/[\.\,\#\?\*\@\!\$\&\+\(\)\[\]\{\}\´\`\^\~\'\"\=\;\ç\º\ª\á\à\ã\â\ä\ñ\é\è\ë\ê\í\ì\î\ï\ó\ò\ô\õ\ö\ú\ù\û\ü\Á\À\Ã\Â\Ä\Ñ\É\È\Ë\Ê\Í\Ì\Î\Ï\Ó\Ò\Ô\Õ\Ö\Ú\Ù\Û\Ü\Ç\ \¨\¬\£\¢\§\°\%]/", "", $id));
		}

		public static function name($name){
			session_name(filter_var($name, FILTER_SANITIZE_STRING));
		}

		public static function start($config=null){
			if (session_status() === PHP_SESSION_NONE) {
				if ($config != null and gettype($config) == "array") {
					session_start($config);
				}
				else{
					session_start();
				}
				return true;
			}
			else{
				return false;
			}
		}

		public static function close(){
			if (self::isActive()) {
				session_write_close();
				return true;
			}
			else{
				return false;
			}
		}

		public static function destroy(){
			if (self::isActive()) {
				session_destroy();
				return true;
			}
			else{
				return false;
			}
		}

		public static function reset(){
			if (self::isActive()) {
				session_reset();
				return true;
			}
			else{
				return false;
			}
		}

		public static function set($key, $value="SESSVALUENULL@35!", $filter=FILTER_DEFAULT){
			if (self::isActive()) {
				$args = func_get_args();
				if (gettype($args[0]) == "array" and count($args[0]) > 0) {
					if (isset($args[1])) {
						if (gettype($args[1]) == "array" and count($args[0]) != count($args[1])) {
							throw new Exception("ERROR: The amount of filter arguments in Session::set() is different from the amount arguments of values.", 500);
						}
					}
					else{
						$args[1] = FILTER_DEFAULT;
					}

					$i = 0;
					foreach ($args[0] as $k => $v) {
						if (gettype($args[1]) == "array") {
							$_SESSION[show_slug(SYS_NAME)."_".$k] = filter_var($v, $args[1][$i]);
							$i++;
						}
						else{
							$_SESSION[show_slug(SYS_NAME)."_".$k] = filter_var($v, $args[1]);
						}
					}
				}
				elseif (isset($key) and $value != "SESSVALUENULL@35!") {
					$_SESSION[show_slug(SYS_NAME)."_".$key] = filter_var($value, $filter);
				}
				else{
					throw new Exception("ERROR: Missing Required Arguments in Session::set().", 500);
				}
			}
			else{
				throw new Exception("ERROR: Session not started.", 500);
			}
		}

		public static function get($key, $filter=FILTER_DEFAULT){
			if (self::isActive()) {
				if (isset($_SESSION[show_slug(SYS_NAME)."_".$key])) {
					return filter_var($_SESSION[show_slug(SYS_NAME)."_".$key], $filter);
				}
				else{
					return null;
				}
			}
			else{
				throw new Exception("ERROR: Session not started.", 500);
			}
		}

		public static function getAll($filter=FILTER_DEFAULT){
			if (self::isActive()) {
				return filter_var_array($_SESSION, $filter);
			}
			else{
				throw new Exception("ERROR: Session not started.", 500);
			}
		}

		public static function remove($key){
			if (self::isActive()) {
				if (isset($_SESSION[show_slug(SYS_NAME)."_".$key])) {
					unset($_SESSION[show_slug(SYS_NAME)."_".$key]);
					return true;
				}
				else{
					return false;
				}
			}
			else{
				return false;
			}
		}

		public static function isRegistered($key){
			if (self::isActive()) {
				return isset($_SESSION[show_slug(SYS_NAME)."_".$key]);
			}
			else{
				return false;
			}
		}

		public static function isActive(){
			if (session_status() === PHP_SESSION_ACTIVE) {
				return true;
			}
			else{
				return false;
			}
		}
	}

?>