<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of uri
 *
 * @author oCoder
 */
class Uri {

	
    
    /**
     * get the root from 
     */
    public static function root(){
    	
        $base = "http://";
        if (HelperAdmin::isSSL()) {
            $base = "https://";
        }
        
        $fullURL = $base.$_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
        
        $info = pathinfo($fullURL);
        $url = $info["dirname"]."/";
        
        return $url;
    }
    
    /**
     * get folder of the admin
     */
    public static function getAdminFolder(){
    	$path = dirname(__FILE__)."/../";
    	
    	$path = realpath($path);
    	$info = pathinfo($path);
    	
    	$folderName = HelperAdmin::getVal($info, "basename");
    	
    	if(empty($folderName))
    		$folderName = HelperAdmin::getVal($info, "filename");
    	
    	return($folderName);
    }
    
    
    /**
     * get admin url
     */
    public static function getUrlAdmin(){
    	
    	$urlRoot = self::root();
    	
    	$isAdminFolder = defined("IS_ADMIN_FOLDER");
    	
    	if($isAdminFolder == false){
    		$folder = self::getAdminFolder();
    		$urlRoot = $urlRoot.$folder."/";
    	}
    	
   	
    	return($urlRoot);
    }
    
    
    /**
     * get site url
     */
    public static function getUrlSite(){
    	
    	$urlAdmin = self::getUrlAdmin();
		
    	$urlSite = $urlAdmin."../";
    	$urlSite = self::cleanPath($urlSite);
    	
    	return($urlSite);
    }
    

    /**
     * check page is admin or not
     */
    public static function isAdminPage() {
        $result = parse_url($_SERVER['REQUEST_URI']);
        if (strpos($result['path'], '/admin') != -1 && strpos($result['path'], '/admin') !== false) {
            return true;
        }
        return false;
    }
	
    
    /**
     * clean path string
     */
    public static function cleanPath($path) {
        $path = explode('/', preg_replace('#(/+)#', '/', $path));
		
        for ($i = 0, $n = count($path); $i < $n; $i++) {
            if ($path[$i] == '.' || $path[$i] == '..') {
                if (($path[$i] == '.') || ($path[$i] == '..' && $i == 1 && $path[0] == '')) {
                    unset($path[$i]);
                    $path = array_values($path);
                    $i--;
                    $n--;
                } elseif ($path[$i] == '..' && ($i > 1 || ($i == 1 && $path[0] != ''))) {
                    unset($path[$i]);
                    unset($path[$i - 1]);
                    $path = array_values($path);
                    $i -= 2;
                    $n -= 2;
                }
            }
        }
        
        $path = implode('/', $path);
        
        $path = str_replace("http:","http:/",$path);
        $path = str_replace("https:","https:/",$path);
        $path = str_replace("HTTP:","HTTP:/",$path);
        $path = str_replace("HTTPS:","HTTPS:/",$path);
        $path = str_replace("///","//",$path);
        
        return $path;
    }
    
    
    /**
     * get relative path
     */
    public static function getPathRelative($path){
    	
    	$info = pathinfo($path);
    	$folder = $info["basename"];
    	
    	$relative = $folder."/";
    	
    	return($relative);
    }

}
