<?php
class url
{
    public $data = array();

    public function __construct(&$locator)
    {
        $this->config   =& $locator->get('config');
        $this->database =& $locator->get('database');

        if ($this->config->get('config_url_alias')) {
            $results = $this->database->cache('url', "select * from url_alias");

            foreach ($results as $result) {
                $this->data[$result['query']] = $result['alias'];
            }
        }
    }

    public function href($controller, $action = NULL, $query = array(), $extension = 0)
    {
        if (empty($controller)) {
            return HTTP_SERVER;
        }

        return $this->create(HTTP_SERVER, $controller, $action, $query, $extension);
    }

    public function ssl($controller, $action = NULL, $query = array(), $extension = 0)
    {
        if (($this->config->get('config_ssl')) && (defined('HTTPS_SERVER')) && (HTTPS_SERVER)) {
            $server = HTTPS_SERVER;
        } else {
            $server = HTTP_SERVER;
        }

        return $this->create($server, $controller, $action, $query, $extension);
    }

    public function create($server, $controller, $action = NULL, $query = array(), $extension = 0)
    {
        if ($extension) {
            $link = 'extension=' . $controller;
        } else {
            $link = 'controller=' . $controller;
        }

        if ($action) {
              $link .= '&action=' . $action;
        }

        foreach ($query as $key => $value) {
            if ($value) {
                $link .= '&' . $key . '=' . urlencode($value);
            }
        }
       if (intval($_GET['fp'] == 1)) {
            $link .= '&fp=1';
       }
    if (isset($this->data[$link])) {
            $link = $this->data[$link];
        } else {
            $link = empty($link) ? 'index.php' : 'index.php?' . $link;
        }
//		$link = empty($link) ? 'index.php' : 'index.php?' . $link;
        return $server . $link;
    }

    public function getCurUrl()
    {
//		echo "<script>alert('call get_current_url".$_SERVER['HTTP_REFERER']."');</script>";

        $ret = (   isset($_SERVER['HTTPS'])   &&   $_SERVER['HTTPS']   !=   'off')   ?   'https://'   :   'http://';

        $query_string = empty($_SERVER['QUERY_STRING'])?"":"?".$_SERVER["QUERY_STRING"];

        $ret = $ret.$_SERVER["HTTP_HOST"].$_SERVER["PHP_SELF"].$query_string;

        return $ret;
    }
}
