<?php
	
	global $_SLUG;
	$_SLUG = Pagination::getData();

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

			if (preg_match("/^[\/]/", $path) == 0) {
				$path = "/".$path;
			}

			if (preg_match("/[\/]$/", $path) == 0) {
				$path .= "/";
			}

			if ($path == "/") {
				return "";
			}
			else{
				return $path;
			}
		}

		public static function getPath(){
			$path = self::getOriginalPath();

			if (empty($path)) {
				return "";
			}
			else{
				$aux = preg_replace("/^[\/]/", "", $path);
				$aux = preg_replace("/[\/]$/", "", $aux);

				$result = explode("/", $aux);

				return "/".$result[0]."/";
			}
		}

		public static function getData(){
			$path = self::getOriginalPath();

			if ($path != "/") {
				$aux = preg_replace("/^[\/]/", "", $path);
				$aux = preg_replace("/[\/]$/", "", $aux);

				$result = explode("/", $aux);

				$data = [];
				for ($i=1; $i < count($result); $i++) {
					$data[] = $result[$i];
				}

				return $data;
			}
		}

		public static function issetPage($path = null){
			if (empty($path)) {
				$path = self::getPath();
			}
			if (URLs[$path] != null and file_exists(URLs[$path])){
				return true;
			}
			else{
				return false;
			}
		}

		public static function getPage($path = null){
			if (empty($path)) {
				$path = self::getPath();
			}
			if (self::issetPage($path)) {
				return URLs[$path];
			}
			else{
				return null;
			}
		}

		public static function link($path = null){
			if (empty($path)) {
				$path = self::getPath();
			}
			return URL_BASE.$path;
		}

		public static function getErrorPage($page){
			if (ERROR_PAGES[$page] != null and file_exists(ERROR_PAGES[$page])) {
				return ERROR_PAGES[$page];
			}
			else{
				throw new Exception("ERROR: This error page does not exist.", 500);
			}
		}
	}

?>
