Use my own variable instead of hardcoded SCRIPT_NAME constant
[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
@@ -1,7 +1,5 @@
 <?php
 /**
- * GitPHP ControllerBase
- *
  * Base class that all controllers extend
  *
  * @author Christopher Han <xiphux@gmail.com
@@ -9,104 +7,158 @@
  * @package GitPHP
  * @subpackage Controller
  */
-
-/**
- * ControllerBase class
- *
- * @package GitPHP
- * @subpackage Controller
- * @abstract
- */
 abstract class GitPHP_ControllerBase
 {
 
 	/**
-	 * config
-	 *
 	 * Config handler instance
 	 *
-	 * @access protected
+	 * @var GitPHP_Config
 	 */
 	protected $config;
 
 	/**
-	 * tpl
-	 *
 	 * Smarty instance
 	 *
-	 * @access protected
+	 * @var Smarty
 	 */
 	protected $tpl;
 
 	/**
-	 * projectList
-	 *
 	 * Project list
 	 *
-	 * @access protected
+	 * @var GitPHP_ProjectListBase
 	 */
 	protected $projectList;
 
 	/**
-	 * project
-	 *
 	 * Current project
 	 *
-	 * @access protected
+	 * @var GitPHP_Project
 	 */
 	protected $project;
 
 	/**
-	 * multiProject
-	 *
 	 * Flag if this is a multi project controller
 	 *
-	 * @access protected
+	 * @var boolean
 	 */
 	protected $multiProject;
 
 	/**
-	 * params
-	 *
 	 * Parameters
 	 *
-	 * @access protected
+	 * @var array
 	 */
 	protected $params = array();
 
 	/**
-	 * headers
-	 *
-	 * Headers
-	 *
-	 * @access protected
+	 * HTTP Headers
+	 *
+	 * @var string[]
 	 */
 	protected $headers = array();
 
 	/**
-	 * preserveWhitespace
-	 *
-	 * Flag to preserve whitespace in output
-	 * (for non-html output)
-	 *
-	 * @access protected
+	 * Flag to preserve whitespace in output (for non-html output)
+	 *
+	 * @var boolean
 	 */
 	protected $preserveWhitespace = false;
 
 	/**
-	 * __construct
-	 *
+	 * Logger instance
+	 *
+	 * @var GitPHP_DebugLog
+	 */
+	protected $log;
+
+	/**
+	 * Git executable instance
+	 *
+	 * @var GitPHP_GitExe
+	 */
+	protected $exe;
+
+	/**
 	 * Constructor
-	 *
-	 * @access public
-	 * @return mixed controller object
-	 * @throws Exception on invalid project
 	 */
 	public function __construct()
 	{
 		$this->config = GitPHP_Config::GetInstance();
-		$this->projectList = GitPHP_ProjectList::GetInstance();
-
+
+		$log = GitPHP_DebugLog::GetInstance();
+		if ($log && $log->GetEnabled())
+			$this->log = $log;
+
+		$this->InitializeGitExe();
+
+		$this->InitializeProjectList();
+
+		$this->InitializeSmarty();
+
+		if ($this->multiProject) {
+			$this->projectList->LoadProjects();
+		}
+
+		if (isset($_GET['p'])) {
+			$project = $this->projectList->GetProject(str_replace(chr(0), '', $_GET['p']));
+			if (!$project) {
+				throw new GitPHP_MessageException(sprintf(__('Invalid project %1$s'), $_GET['p']), true);
+			}
+			$this->project = $project->GetProject();
+		}
+
+		if (!($this->project || $this->multiProject)) {
+			throw new GitPHP_MessageException(__('Project is required'), true);
+		}
+
+		if (isset($_GET['s']))
+			$this->params['search'] = $_GET['s'];
+		if (isset($_GET['st']))
+			$this->params['searchtype'] = $_GET['st'];
+
+		$this->ReadQuery();
+	}
+
+	/**
+	 * Initialize executable
+	 *
+	 * @param boolean $validate whether the exe should be validated
+	 */
+	protected function InitializeGitExe($validate = true)
+	{
+		$this->exe = new GitPHP_GitExe($this->config->GetValue('gitbin'));
+		if ($this->log)
+			$this->exe->AddObserver($this->log);
+		if ($validate && !$this->exe->Valid()) {
+			throw new GitPHP_MessageException(sprintf(__('Could not run the git executable "%1$s".  You may need to set the "%2$s" config value.'), $this->exe->GetBinary(), 'gitbin'), true, 500);
+		}
+	}
+
+	/**
+	 * Initialize project list
+	 */
+	protected function InitializeProjectList()
+	{
+		if (file_exists(GITPHP_CONFIGDIR . 'projects.conf.php')) {
+			$this->projectList = GitPHP_ProjectList::Instantiate(GITPHP_CONFIGDIR . 'projects.conf.php', false);
+		} else {
+			$this->projectList = GitPHP_ProjectList::Instantiate(GITPHP_CONFIGDIR . 'gitphp.conf.php', true);
+		}
+
+		$this->projectList->SetExe($this->exe);
+
+		if ($this->log)
+			$this->projectList->AddObserver($this->log);
+
+	}
+
+	/**
+	 * Initialize smarty
+	 */
+	protected function InitializeSmarty()
+	{
 		require_once(GITPHP_SMARTYDIR . 'Smarty.class.php');
 		$this->tpl = new Smarty;
 		$this->tpl->error_reporting = E_ALL & ~E_NOTICE;
@@ -127,37 +179,37 @@
 
 		}
 
-		if (isset($_GET['p'])) {
-			$project = $this->projectList->GetProject(str_replace(chr(0), '', $_GET['p']));
-			if (!$project) {
-				throw new GitPHP_MessageException(sprintf(__('Invalid project %1$s'), $_GET['p']), true);
-			}
-			$this->project = $project->GetProject();
-		}
-
-		if (!($this->project || $this->multiProject)) {
-			throw new GitPHP_MessageException(__('Project is required'), true);
-		}
-
-		if ($this->multiProject) {
-			$this->projectList->LoadProjects();
-		}
-
-		if (isset($_GET['s']))
-			$this->params['search'] = $_GET['s'];
-		if (isset($_GET['st']))
-			$this->params['searchtype'] = $_GET['st'];
-
-		$this->ReadQuery();
-	}
-
-	/**
-	 * GetProject
-	 *
+	}
+
+	/**
+	 * Get log instance
+	 *
+	 * @return GitPHP_DebugLog
+	 */
+	public function GetLog()
+	{
+		return $this->log;
+	}
+
+	/**
+	 * Disable logging
+	 */
+	protected function DisableLogging()
+	{
+		if (!$this->log)
+			return;
+
+		$this->projectList->RemoveObserver($this->log);
+
+		$this->log->SetEnabled(false);
+
+		$this->log = null;
+	}
+
+	/**
 	 * Gets the project for this controller
 	 *
-	 * @access public
-	 * @return mixed project
+	 * @return GitPHP_Project|null project
 	 */
 	public function GetProject()
 	{
@@ -167,34 +219,23 @@
 	}
 
 	/**
-	 * GetTemplate
-	 *
 	 * Gets the template for this controller
 	 *
-	 * @access protected
-	 * @abstract
 	 * @return string template filename
 	 */
 	protected abstract function GetTemplate();
 
 	/**
-	 * GetCacheKey
-	 *
 	 * Gets the cache key for this controller
 	 *
-	 * @access protected
-	 * @abstract
 	 * @return string cache key
 	 */
 	protected abstract function GetCacheKey();
 
 	/**
-	 * GetCacheKeyPrefix
-	 *
 	 * Get the prefix for all cache keys
 	 *
-	 * @access private
-	 * @param string $projectKeys include project-specific key pieces
+	 * @param boolean $projectKeys include project-specific key pieces
 	 * @return string cache key prefix
 	 */
 	private function GetCacheKeyPrefix($projectKeys = true)
@@ -212,11 +253,8 @@
 	}
 
 	/** 
-	 * GetFullCacheKey
-	 *
 	 * Get the full cache key
 	 *
-	 * @access protected
 	 * @return string full cache key
 	 */
 	protected function GetFullCacheKey()
@@ -236,33 +274,21 @@
 	}
 
 	/**
-	 * GetName
-	 *
 	 * Gets the name of this controller's action
 	 *
-	 * @abstract
-	 * @access public
 	 * @param boolean $local true if caller wants the localized action name
 	 * @return string action name
 	 */
 	public abstract function GetName($local = false);
 
 	/**
-	 * ReadQuery
-	 *
 	 * Read query into parameters
-	 *
-	 * @abstract
-	 * @access protected
 	 */
 	protected abstract function ReadQuery();
 
 	/**
-	 * SetParam
-	 *
 	 * Set a parameter
 	 *
-	 * @access protected
 	 * @param string $key key to set
 	 * @param mixed $value value to set
 	 */
@@ -278,32 +304,19 @@
 	}
 
 	/**
-	 * LoadHeaders
-	 *
 	 * Loads headers for this template
-	 *
-	 * @access protected
 	 */
 	protected function LoadHeaders()
 	{
 	}
 
 	/**
-	 * LoadData
-	 *
 	 * Loads data for this template
-	 *
-	 * @access protected
-	 * @abstract
 	 */
 	protected abstract function LoadData();
 
 	/**
-	 * LoadCommonData
-	 *
 	 * Loads common data used by all templates
-	 *
-	 * @access private
 	 */
 	private function LoadCommonData()
 	{
@@ -335,9 +348,27 @@
 		if (isset($this->params['searchtype']))
 			$this->tpl->assign('searchtype', $this->params['searchtype']);
 		$this->tpl->assign('currentlocale', GitPHP_Resource::GetLocale());
-		$this->tpl->assign('supportedlocales', GitPHP_Resource::SupportedLocales());
-
-		$this->tpl->assign('scripturl', $_SERVER['SCRIPT_NAME']);
+		$this->tpl->assign('supportedlocales', GitPHP_Resource::SupportedLocales($this->config->GetValue('debug', false)));
+
+		$scripturl = $_SERVER['SCRIPT_NAME'];
+		$fullscripturl = '';
+		if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on'))
+			$fullscripturl = 'https://';
+		else
+			$fullscripturl = 'http://';
+		$fullscripturl .= $_SERVER['HTTP_HOST'] . $scripturl;
+
+		if (GitPHP_Config::GetInstance()->HasKey('self')) {
+			$selfurl = GitPHP_Config::GetInstance()->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);
 
 		$getvars = explode('&', $_SERVER['QUERY_STRING']);
 		$getvarsmapped = array();
@@ -357,11 +388,7 @@
 	}
 
 	/**
-	 * RenderHeaders
-	 *
 	 * Renders any special headers
-	 *
-	 * @access public
 	 */
 	public function RenderHeaders()
 	{
@@ -375,11 +402,7 @@
 	}
 
 	/**
-	 * Render
-	 *
 	 * Renders the output
-	 *
-	 * @access public
 	 */
 	public function Render()
 	{
@@ -388,33 +411,33 @@
 
 		if (!$this->tpl->isCached($this->GetTemplate(), $this->GetFullCacheKey())) {
 			$this->tpl->clearAllAssign();
-			if (GitPHP_DebugLog::GetInstance()->GetBenchmark())
-				GitPHP_DebugLog::GetInstance()->Log("Data load begin");
+			if ($this->log && $this->log->GetBenchmark())
+				$this->log->Log("Data load begin");
 			$this->LoadCommonData();
 			$this->LoadData();
-			if (GitPHP_DebugLog::GetInstance()->GetBenchmark())
-				GitPHP_DebugLog::GetInstance()->Log("Data load end");
+			if ($this->log && $this->log->GetBenchmark())
+				$this->log->Log("Data load end");
 		}
 
 		if (!$this->preserveWhitespace) {
 			//$this->tpl->loadFilter('output', 'trimwhitespace');
 		}
 
-		if (GitPHP_DebugLog::GetInstance()->GetBenchmark())
-			GitPHP_DebugLog::GetInstance()->Log("Smarty render begin");
+		if ($this->log && $this->log->GetBenchmark())
+			$this->log->Log("Smarty render begin");
 		$this->tpl->display($this->GetTemplate(), $this->GetFullCacheKey());
-		if (GitPHP_DebugLog::GetInstance()->GetBenchmark())
-			GitPHP_DebugLog::GetInstance()->Log("Smarty render end");
+		if ($this->log && $this->log->GetBenchmark())
+			$this->log->Log("Smarty render end");
 
 		$this->tpl->clearAllAssign();
-	}
-
-	/**
-	 * CacheExpire
-	 *
+
+		if ($this->log)
+			$this->log->Log('MemoryCache count: ' . $this->projectList->GetMemoryCache()->GetCount());
+	}
+
+	/**
 	 * Expires the cache
 	 *
-	 * @access public
 	 * @param boolean $expireAll expire the whole cache
 	 */
 	public function CacheExpire($expireAll = false)

comments