<?php 
class Url { 
	var $data = array();
   
  	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'];
	  		} 	  
    	}
  	}
		          
  	function href($controller, $action = NULL, $query = array()) {
		return $this->create(HTTP_SERVER, $controller, $action, $query);
  	}

  	function ssl($controller, $action = NULL, $query = array()) {
		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);
  	}
	
	function create($server, $controller, $action = NULL, $query = array()) {
    	$link = 'controller=' . $controller;

    	if ($action) {
      		$link .= '&action=' . $action;
    	}
    
		foreach ($query as $key => $value) {
	  		if ($value) {
	    		$link .= '&' . $key . '=' . urlencode($value);
	  		}
		}
	
		if (isset($this->data[$link])) {
	  		$link = $this->data[$link];
		} else {
	  		$link = 'index.php?' . $link;
		}
		
		return $server . $link;
	}
}
?>
