<?php
/**
 * class url
 *
 * Description for class url
 *
 * @author:
*/
namespace core;

class url {
	public static function root($reqiureSSL = false) {
		$url = $_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"])."/";
		
		$url = rawurlencode($url);
		$url = str_replace("%3A",":",$url);
		$url = str_replace("%2F","/",$url);
		$url = str_replace("%5C","/",$url);
		
		return ((isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"]=="on") || $reqiureSSL ? "https://" : "http://").str_replace("//", "/", $url);
	}

	public static function host() {
		$url = $_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"])."/";
		
		$url = rawurlencode($url);
		$url = str_replace("%3A",":",$url);
		$url = str_replace("%2F","/",$url);
		$url = str_replace("%5C","/",$url);

		return str_replace("//", "", $url);
	}

	public static function action($controller, $action = "", $id = "", $area = false, $reqiureSSL = false) {
		if($area === false) {
			$areaCheck = url::getArea();
			if($areaCheck !== false) {
				$area = $areaCheck;

				switch($area) {
					case "presenter":
						$area = "prezenter";
						break;
				}
			}
		}
		
		if(empty($action)) {
			$action = "index";
		}

		if($controller == "index" && $action == "index" && empty($id)) {
			return url::root($reqiureSSL).$area;
		}

		if($action == "index" && empty($id)) {
			return url::root($reqiureSSL).(!empty($area) ? "{$area}/" : "")."{$controller}";
		}

		$url = url::root($reqiureSSL).(!empty($area) ? "{$area}/" : "")."{$controller}/{$action}";
		if(!empty($id)) {
			$url .= "/{$id}";
		}

		return $url;
	}

	public static function content($path) {
		return url::root().$path;
	}

	public static function redirect($url) {
		header("Location: ".$url);
		exit;
	}

	public static function redirectAction($controller, $action = "", $id = "", $area = false) {
		header("Location: ".url::action($controller, $action, $id, $area));
		exit;
	}

	public static function redirectArea($area) {
		header("Location: ".url::action("index", "index", "", $area));
		exit;
	}

	public static function getArea() {
		global $__routing;

		$area = false;

		if(isset($__routing[0][2]) && $__routing[0][2] != "") {
			$area = $__routing[0][2];
		}
		
		return $area;
	}
}
?>