Store urls on router rather than in template
Store urls on router rather than in template

--- a/include/Util.class.php
+++ b/include/Util.class.php
@@ -147,13 +147,21 @@
 	/**
 	 * Get the base install url (without index)
 	 *
+	 * @param boolean $full true to return full url (include protocol and hostname)
 	 * @return string base url
 	 */
-	public static function BaseUrl()
+	public static function BaseUrl($full = false)
 	{
 		$baseurl = $_SERVER['SCRIPT_NAME'];
-		if (substr_compare($baseurl, '.php', -4) === 0)
+		if (substr_compare($baseurl, 'index.php', -9) === 0)
 			$baseurl = dirname($baseurl);
+		if ($full) {
+			$baseurl = $_SERVER['HTTP_HOST'] . $baseurl;
+			if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on'))
+				$baseurl = 'https://' . $baseurl;
+			else
+				$baserul = 'http://' . $baseurl;
+		}
 		return rtrim($baseurl, "/");
 	}
 

--- a/include/controller/ControllerBase.class.php
+++ b/include/controller/ControllerBase.class.php
@@ -509,26 +509,6 @@
 		if ($this->config->GetValue('graphs'))
 			$this->tpl->assign('enablegraphs', true);
 
-		$scripturl = $_SERVER['SCRIPT_NAME'];
-		$fullscripturl = '';
-		if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on'))
-			$fullscripturl = 'https://';
-		else
-			$fullscripturl = 'http://';
-		$fullscripturl .= $_SERVER['HTTP_HOST'] . $scripturl;
-
-		if ($this->config->HasKey('self')) {
-			$selfurl = $this->config->GetValue('self');
-			if (!empty($selfurl)) {
-				if (substr($selfurl, -4) != '.php') {
-					$selfurl = GitPHP_Util::AddSlash($selfurl);
-				}
-				$fullscripturl = $selfurl;
-			}
-		}
-		$this->tpl->assign('scripturl', $scripturl);
-		$this->tpl->assign('fullscripturl', $fullscripturl);
-
 		$this->tpl->assign('baseurl', GitPHP_Util::BaseUrl());
 
 		$requesturl = $_SERVER['REQUEST_URI'];
@@ -540,6 +520,9 @@
 		if ($this->router) {
 			$this->router->SetCleanUrl($this->config->GetValue('cleanurl') ? true : false);
 			$this->router->SetAbbreviate($this->config->GetValue('abbreviateurl') ? true : false);
+			if ($this->config->HasKey('self')) {
+				$this->router->SetBaseUrl($this->config->GetValue('self'));
+			}
 			$this->tpl->assign('router', $this->router);
 		}
 

--- a/include/router/Router.class.php
+++ b/include/router/Router.class.php
@@ -39,6 +39,20 @@
 	protected $abbreviate = false;
 
 	/**
+	 * Base url
+	 *
+	 * @var string
+	 */
+	protected $baseurl;
+
+	/**
+	 * Full url
+	 *
+	 * @var string
+	 */
+	protected $fullurl;
+
+	/**
 	 * Constructor
 	 *
 	 * @param boolean $cleanurl true to generate clean urls
@@ -48,6 +62,10 @@
 	{
 		$this->cleanurl = $cleanurl;
 		$this->abbreviate = $abbreviate;
+
+		$this->baseurl = GitPHP_Util::BaseUrl();
+		$this->fullurl = GitPHP_Util::BaseUrl(true);
+
 		$this->InitializeRoutes();
 		$this->InitializeQueryParameters();
 	}
@@ -90,6 +108,31 @@
 	public function SetAbbreviate($abbreviate)
 	{
 		$this->abbreviate = $abbreviate;
+	}
+
+	/**
+	 * Get base url
+	 *
+	 * @param boolean $full true to return full base url (include protocol and hostname)
+	 * @return string base url
+	 */
+	public function GetBaseUrl($full = false)
+	{
+		if ($full)
+			return $this->fullurl;
+
+		return $this->baseurl;
+	}
+
+	/**
+	 * Set base url
+	 *
+	 * @param string $baseurl base url
+	 */
+	public function SetBaseUrl($baseurl)
+	{
+		$this->baseurl = $baseurl;
+		$this->fullurl = $baseurl;
 	}
 
 	/**
@@ -508,16 +551,25 @@
 	/**
 	 * Generate a url
 	 *
-	 * @param string $baseurl base request url
 	 * @param array $params request parameters
-	 */
-	public function GetUrl($baseurl, $params = array())
-	{
+	 * @param boolean $full true to get full url (include protocol and hostname)
+	 */
+	public function GetUrl($params = array(), $full = false)
+	{
+		if ($full)
+			$baseurl = $this->fullurl;
+		else
+			$baseurl = $this->baseurl;
+
 		if ($this->cleanurl) {
-			if (substr_compare($baseurl, '.php', -4) === 0) {
+			if (substr_compare($baseurl, 'index.php', -9) === 0) {
 				$baseurl = dirname($baseurl);
 			}
 			$baseurl = GitPHP_Util::AddSlash($baseurl);
+		} else {
+			if (substr_compare($baseurl, 'index.php', -9) !== 0) {
+				$baseurl = GitPHP_Util::AddSlash($baseurl);
+			}
 		}
 
 		if (count($params) < 1)

--- a/include/smartyplugins/function.geturl.php
+++ b/include/smartyplugins/function.geturl.php
@@ -13,25 +13,13 @@
  */
 function smarty_function_geturl($params, Smarty_Internal_Template $template)
 {
-	$url = null;
-	$escape = false;
-	if (empty($params['url'])) {
-		if (!empty($params['fullurl']) && ($params['fullurl'] == true))
-			$url = $template->getTemplateVars('fullscripturl');
-		else
-			$url = $template->getTemplateVars('scripturl');
-
-		if (empty($url)) {
-			trigger_error("geturl: missing url");
-			return;
-		}
-	} else {
-		$url = $params['url'];
-		unset($params['url']);
+	$full = false;
+	if (!empty($params['fullurl']) && ($params['fullurl'] == true)) {
+		$full = true;
 	}
-
 	unset($params['fullurl']);
 
+	$escape = false;
 	if (!empty($params['escape']) && ($params['escape'] == true))
 		$escape = true;
 	unset($params['escape']);
@@ -41,10 +29,10 @@
 		trigger_error("geturl: missing router");
 		return;
 	}
-	$fullurl = $router->GetUrl($url, $params);
+	$finalurl = $router->GetUrl($params, $full);
 	if ($escape)
-		$fullurl = htmlspecialchars($fullurl);
+		$finalurl = htmlspecialchars($finalurl);
 
-	return $fullurl;
+	return $finalurl;
 }
 

comments