Access static resources with absolute paths
[gitphp.git] / include / controller / ControllerBase.class.php
blob:a/include/controller/ControllerBase.class.php -> blob:b/include/controller/ControllerBase.class.php
--- a/include/controller/ControllerBase.class.php
+++ b/include/controller/ControllerBase.class.php
@@ -18,6 +18,13 @@
 	protected $config;
 
 	/**
+	 * User list instance
+	 *
+	 * @var GitPHP_UserList
+	 */
+	protected $userList;
+
+	/**
 	 * Resource handler instance
 	 *
 	 * @var GitPHP_Resource
@@ -88,6 +95,13 @@
 	protected $exe;
 
 	/**
+	 * Url router instance
+	 *
+	 * @var GitPHP_Router
+	 */
+	protected $router;
+
+	/**
 	 * Initialize controller
 	 */
 	public function Initialize()
@@ -95,6 +109,8 @@
 		$this->InitializeConfig();
 
 		$this->InitializeResource();
+
+		$this->InitializeUserList();
 
 		$this->EnableLogging();
 
@@ -137,11 +153,13 @@
 	{
 		$locale = null;
 
+		$baseurl = GitPHP_Util::BaseUrl();
+
 		if (!empty($this->params['lang'])) {
 			/*
 			 * User picked something
 			 */
-			setcookie(GitPHP_Resource::LocaleCookie, $this->params['lang'], time()+GitPHP_Resource::LocaleCookieLifetime);
+			setcookie(GitPHP_Resource::LocaleCookie, $this->params['lang'], time()+GitPHP_Resource::LocaleCookieLifetime, $baseurl);
 			$locale = $this->params['lang'];
 		} else if (!empty($_COOKIE[GitPHP_Resource::LocaleCookie])) {
 			/**
@@ -156,7 +174,7 @@
 				$httpAcceptLang = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
 				$locale = GitPHP_Resource::FindPreferredLocale($_SERVER['HTTP_ACCEPT_LANGUAGE']);
 				if (!empty($locale)) {
-					setcookie(GitPHP_Resource::LocaleCookie, $locale, time()+GitPHP_Resource::LocaleCookieLifetime);
+					setcookie(GitPHP_Resource::LocaleCookie, $locale, time()+GitPHP_Resource::LocaleCookieLifetime, $baseurl);
 				}
 			}
 		}
@@ -173,6 +191,19 @@
 				$this->resource = new GitPHP_Resource($locale);
 			} catch (Exception $e) {
 			}
+		}
+	}
+
+	/**
+	 * Initialize user list
+	 */
+	public function InitializeUserList()
+	{
+		$this->userList = new GitPHP_UserList();
+		$this->userList->LoadUsers(GITPHP_CONFIGDIR . 'users.conf.php');
+		if ($this->userList->GetCount() > 0) {
+			if (!isset($_SESSION))
+				session_start();
 		}
 	}
 
@@ -272,6 +303,16 @@
 	}
 
 	/**
+	 * Set router instance
+	 *
+	 * @param GitPHP_Router $router router
+	 */
+	public function SetRouter($router)
+	{
+		$this->router = $router;
+	}
+
+	/**
 	 * Get config instance
 	 *
 	 * @return GitPHP_Config
@@ -430,6 +471,7 @@
 	 */
 	protected function LoadHeaders()
 	{
+		$this->headers[] = 'Content-Type: text/html; charset=UTF-8';
 	}
 
 	/**
@@ -482,43 +524,31 @@
 			$this->tpl->assign('searchtype', $this->params['searchtype']);
 		if ($this->resource) {
 			$this->tpl->assign('currentlocale', $this->resource->GetLocale());
+			$this->tpl->assign('currentprimarylocale', $this->resource->GetPrimaryLocale());
 			$this->tpl->assign('resource', $this->resource);
 		} else {
 			$this->tpl->assign('currentlocale', 'en_US');
+			$this->tpl->assign('currentprimarylocale', 'en');
 		}
 		$this->tpl->assign('supportedlocales', GitPHP_Resource::SupportedLocales(true));
 		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);
-
-		$baseurl = $scripturl;
-		if (substr_compare($baseurl, '.php', -4) === 0) {
-			$baseurl = dirname($baseurl);
-		}
-		$baseurl = rtrim($baseurl, "\\");
-		$this->tpl->assign('baseurl', $baseurl);
+		$this->tpl->assign('baseurl', GitPHP_Util::BaseUrl());
+
+		$requesturl = $_SERVER['REQUEST_URI'];
+		$querypos = strpos($requesturl, '?');
+		if ($querypos !== false)
+			$requesturl = substr($requesturl, 0, $querypos);
+		$this->tpl->assign('requesturl', $requesturl);
 		
-		if ($this->config->GetValue('abbreviateurl')) {
-			$this->tpl->assign('abbreviateurl', true);
+		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);
 		}
 
 		$getvars = explode('&', $_SERVER['QUERY_STRING']);
@@ -528,7 +558,7 @@
 			if ($eqpos > 0) {
 				$var = substr($varstr, 0, $eqpos);
 				$val = substr($varstr, $eqpos + 1);
-				if (!(empty($var) || empty($val))) {
+				if (!(empty($var) || empty($val) || ($var == 'q'))) {
 					$getvarsmapped[$var] = urldecode($val);
 				}
 			}
@@ -536,6 +566,16 @@
 		$this->tpl->assign('requestvars', $getvarsmapped);
 
 		$this->tpl->assign('snapshotformats', GitPHP_Archive::SupportedFormats());
+
+		if ($this->userList && ($this->userList->GetCount() > 0)) {
+			$this->tpl->assign('loginenabled', true);
+			if (!empty($_SESSION['gitphpuser'])) {
+				$user = $this->userList->GetUser($_SESSION['gitphpuser']);
+				if ($user) {
+					$this->tpl->assign('loggedinuser', $user->GetUsername());
+				}
+			}
+		}
 	}
 
 	/**
@@ -546,7 +586,16 @@
 		$this->LoadHeaders();
 
 		if (count($this->headers) > 0) {
+			$hascontenttype = false;
 			foreach ($this->headers as $hdr) {
+				if (empty($hdr))
+					continue;
+
+				if (strncmp($hdr, 'Content-Type:', 13) === 0) {
+					if ($hascontenttype)
+						throw new Exception('Duplicate Content-Type header');
+					$hascontenttype = true;
+				}
 				header($hdr);
 			}
 		}
@@ -582,7 +631,7 @@
 
 		$this->tpl->clearAllAssign();
 
-		if ($this->log)
+		if ($this->log && $this->projectList)
 			$this->log->Log('MemoryCache count: ' . $this->projectList->GetMemoryCache()->GetCount());
 	}
 

comments