Add specific exception for invalid hashes
[gitphp.git] / include / controller / Controller_Message.class.php
blob:a/include/controller/Controller_Message.class.php -> blob:b/include/controller/Controller_Message.class.php
--- a/include/controller/Controller_Message.class.php
+++ b/include/controller/Controller_Message.class.php
@@ -12,12 +12,16 @@
 	/**
 	 * Constructor
 	 */
-	public function __construct()
-	{
-		$this->config = GitPHP_Config::GetInstance();
-
-		if (GitPHP_Resource::Instantiated() && (GitPHP_Resource::GetLocale() != 'en_US'));
-			$this->resource = GitPHP_Resource::GetInstance();
+	public function Initialize()
+	{
+		try {
+			$this->InitializeConfig();
+		} catch (Exception $e) {
+		}
+
+		$this->InitializeResource();
+
+		$this->InitializeUserList();
 
 		$this->InitializeGitExe(false);
 
@@ -31,19 +35,21 @@
 		} catch (Exception $e) {
 		}
 
-		if (isset($_GET['p']) && $this->projectList) {
-			$project = $this->projectList->GetProject(str_replace(chr(0), '', $_GET['p']));
+		if (!empty($this->params['project']) && $this->projectList) {
+			$project = $this->projectList->GetProject($this->params['project']);
 			if ($project) {
-				$this->project = $project->GetProject();
+				if ($this->userList && ($this->userList->GetCount() > 0)) {
+					if ($project->UserCanAccess((!empty($_SESSION['gitphpuser']) ? $_SESSION['gitphpuser'] : null))) {
+						$this->project = $project->GetProject();
+					}
+				} else {
+					$this->project = $project->GetProject();
+				}
 			}
 		}
 
-		if (isset($_GET['s']))
-			$this->params['search'] = $_GET['s'];
-		if (isset($_GET['st']))
-			$this->params['searchtype'] = $_GET['st'];
-
-		$this->ReadQuery();
+		if (empty($this->params['hash']))
+			$this->params['hash'] = 'HEAD';
 	}
 
 	/**
@@ -81,21 +87,12 @@
 	}
 
 	/**
-	 * Read query into parameters
-	 */
-	protected function ReadQuery()
-	{
-		if (isset($_GET['h']))
-			$this->params['hash'] = $_GET['h'];
-		else
-			$this->params['hash'] = 'HEAD';
-	}
-
-	/**
 	 * Loads headers for this template
 	 */
 	protected function LoadHeaders()
 	{
+		parent::LoadHeaders();
+
 		if (($this->params['exception'] instanceof GitPHP_MessageException) && ($this->params['exception']->StatusCode)) {
 			$partialHeader = $this->StatusCodeHeader($this->params['exception']->StatusCode);
 			if (!empty($partialHeader)) {
@@ -184,6 +181,101 @@
 			return sprintf('Invalid hash %1$s', $exception->Hash);
 		}
 
+		if ($exception instanceof GitPHP_InvalidGitExecutableException) {
+			if ($this->resource)
+				return sprintf($this->resource->translate('Could not run the git executable "%1$s".  You may need to set the "%2$s" config value.'), $exception->Executable, 'gitbin');
+			return sprintf('Could not run the git executable "%1$s".  You may need to set the "%2$s" config value.', $exception->Executable, 'gitbin');
+		}
+
+		if ($exception instanceof GitPHP_MissingProjectrootException) {
+			if ($this->resource)
+				return $this->resource->translate('A projectroot must be set in the config');
+			return 'A projectroot must be set in the config';
+		}
+
+		if ($exception instanceof GitPHP_MissingMemcacheException) {
+			if ($this->resource)
+				return $this->resource->translate('The Memcached or Memcache PHP extension is required for Memcache support');
+			return 'The Memcached or Memcache PHP extension is required for Memcache support';
+		}
+
+		if (($exception instanceof GitPHP_InvalidDirectoryException) || ($exception instanceof GitPHP_InvalidDirectoryConfigurationException)) {
+			if ($this->resource)
+				return sprintf($this->resource->translate('%1$s is not a directory'), $exception->Directory);
+			return sprintf('%1$s is not a directory', $exception->Directory);
+		}
+
+		if ($exception instanceof GitPHP_InvalidFileException) {
+			if ($this->resource)
+				return sprintf($this->resource->translate('%1$s is not a file'), $exception->File);
+			return sprintf('%1$s is not a file', $exception->File);
+		}
+
+		if ($exception instanceof GitPHP_InvalidGitRepositoryException) {
+			if ($this->resource)
+				return sprintf($this->resource->translate('%1$s is not a git repository'), $exception->Repository);
+			return sprintf('%1$s is not a git repository', $exception->Repository);
+		}
+
+		if ($exception instanceof GitPHP_ProjectListFileReadException) {
+			if ($this->resource)
+				return sprintf($this->resource->translate('Failed to open project list file %1$s'), $exception->File);
+			return sprintf('Failed to open project list file %1$s', $exception->File);
+		}
+
+		if ($exception instanceof GitPHP_DirectoryTraversalException) {
+			if ($this->resource)
+				return sprintf($this->resource->translate('%1$s is attempting directory traversal'), $exception->Path);
+			return sprintf('%1$s is attempting directory traversal', $exception->Path);
+		}
+
+		if ($exception instanceof GitPHP_ProjectrootBoundException) {
+			if ($this->resource)
+				return sprintf($this->resource->translate('%1$s is outside of the projectroot'), $exception->Path);
+			return sprintf('%1$s is outside of the projectroot', $exception->Path);
+		}
+
+		if ($exception instanceof GitPHP_InvalidConfigFileException) {
+			if ($this->resource)
+				return sprintf($this->resource->translate('Could not load config file %1$s'), $exception->File);
+			return sprintf('Could not load config file %1$s', $exception->File);
+		}
+
+		if ($exception instanceof GitPHP_AmbiguousHashException) {
+			if ($this->resource)
+				return sprintf($this->resource->translate('Ambiguous abbreviated hash %1$s'), $exception->Hash);
+			
+			return sprintf('Ambiguous abbreviated hash %1$s', $exception->Hash);
+		}
+
+		if ($exception instanceof GitPHP_DirectoryNotFoundException) {
+			if ($this->resource)
+				return sprintf($this->resource->translate('Directory %1$s not found'), $exception->Directory);
+
+			return sprintf('Directory %1$s not found', $exception->Directory);
+		}
+
+		if ($exception instanceof GitPHP_FileNotFoundException) {
+			if ($this->resource)
+				return sprintf($this->resource->translate('File %1$s not found'), $exception->File);
+
+			return sprintf('File %1$s not found', $exception->File);
+		}
+
+		if ($exception instanceof GitPHP_UnauthorizedProjectException) {
+			if ($this->resource)
+				return sprintf($this->resource->translate('You are not authorized to access project %1$s'), $exception->Project);
+
+			return sprintf('You are not authorized to access project %1$s', $exception->Project);
+		}
+
+		if ($exception instanceof GitPHP_DisabledFunctionException) {
+			if ($this->resource)
+				return sprintf($this->resource->translate('Required function %1$s has been disabled'), $exception->Function);
+
+			return sprintf('Required function %1$s has been disabled', $exception->Function);
+		}
+
 		return $exception->getMessage();
 	}
 
@@ -196,6 +288,10 @@
 	private function StatusCodeHeader($code)
 	{
 		switch ($code) {
+			case 403:
+				return '403 Forbidden';
+			case 404:
+				return '404 Not Found';
 			case 500:
 				return '500 Internal Server Error';
 		}

comments