Throw an error when an invalid path is specified in the url
Throw an error when an invalid path is specified in the url

--- a/include/controller/Controller_Blame.class.php
+++ b/include/controller/Controller_Blame.class.php
@@ -74,6 +74,8 @@
 
 		if ((!isset($this->params['hash'])) && (isset($this->params['file']))) {
 			$this->params['hash'] = $commit->GetTree()->PathToHash($this->params['file']);
+			if (empty($this->params['hash']))
+				throw new GitPHP_FileNotFoundException($this->params['file']);
 		}
 		
 		$blob = $this->GetProject()->GetObjectManager()->GetBlob($this->params['hash']);

--- a/include/controller/Controller_Blob.class.php
+++ b/include/controller/Controller_Blob.class.php
@@ -81,6 +81,8 @@
 					if ((!isset($this->params['hash'])) && (isset($this->params['file']))) {
 						$commit = $this->GetProject()->GetCommit($this->params['hashbase']);
 						$this->params['hash'] = $commit->GetTree()->PathToHash($this->params['file']);
+						if (empty($this->params['hash']))
+							throw new GitPHP_FileNotFoundException($this->params['file']);
 					}
 
 					$blob = $this->GetProject()->GetObjectManager()->GetBlob($this->params['hash']);
@@ -122,6 +124,8 @@
 
 		if ((!isset($this->params['hash'])) && (isset($this->params['file']))) {
 			$this->params['hash'] = $tree->PathToHash($this->params['file']);
+			if (empty($this->params['hash']))
+				throw new GitPHP_FileNotFoundException($this->params['file']);
 		}
 
 		$blob = $this->GetProject()->GetObjectManager()->GetBlob($this->params['hash']);

--- a/include/controller/Controller_History.class.php
+++ b/include/controller/Controller_History.class.php
@@ -69,6 +69,8 @@
 		$this->tpl->assign('tree', $co->GetTree());
 
 		$blobhash = $tree->PathToHash($this->params['file']);
+		if (empty($blobhash))
+			throw new GitPHP_FileNotFoundException($this->params['file']);
 		$blob = $this->GetProject()->GetObjectManager()->GetBlob($blobhash);
 		$blob->SetCommit($co);
 		$blob->SetPath($this->params['file']);

--- a/include/controller/Controller_Message.class.php
+++ b/include/controller/Controller_Message.class.php
@@ -240,6 +240,20 @@
 			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);
+		}
+
 		return $exception->getMessage();
 	}
 

--- a/include/controller/Controller_Tree.class.php
+++ b/include/controller/Controller_Tree.class.php
@@ -80,6 +80,8 @@
 		if (!isset($this->params['hash'])) {
 			if (isset($this->params['file'])) {
 				$this->params['hash'] = $commit->GetTree()->PathToHash($this->params['file']);
+				if (empty($this->params['hash']))
+					throw new GitPHP_DirectoryNotFoundException($this->params['file']);
 			} else {
 				$this->params['hash'] = $commit->GetTreeHash();
 			}

--- /dev/null
+++ b/include/exception/DirectoryNotFoundException.class.php
@@ -1,1 +1,34 @@
+<?php
+/**
+ * Custom exception when a specified directory is not found
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2012 Christopher Han
+ * @package GitPHP
+ * @subpackage Exception
+ */
+class GitPHP_DirectoryNotFoundException extends GitPHP_MessageException
+{
+	/**
+	 * Directory
+	 *
+	 * @var string
+	 */
+	public $Directory;
 
+	/**
+	 * Constructor
+	 *
+	 * @param string $dir directory
+	 * @param string $message message
+	 * @param int $code exception code
+	 */
+	public function __construct($dir, $message = '', $code = 0)
+	{
+		$this->Directory = $dir;
+		if (empty($message))
+			$message = sprintf('Directory %1$s not found', $dir);
+		parent::__construct($message, true, 404, $code);
+	}
+}
+

--- /dev/null
+++ b/include/exception/FileNotFoundException.class.php
@@ -1,1 +1,34 @@
+<?php
+/**
+ * Custom exception when a specified file is not found
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2012 Christopher Han
+ * @package GitPHP
+ * @subpackage Exception
+ */
+class GitPHP_FileNotFoundException extends GitPHP_MessageException
+{
+	/**
+	 * File
+	 *
+	 * @var string
+	 */
+	public $File;
 
+	/**
+	 * Constructor
+	 *
+	 * @param string $file file
+	 * @param string $message message
+	 * @param int $code exception code
+	 */
+	public function __construct($file, $message = '', $code = 0)
+	{
+		$this->File = $file;
+		if (empty($message))
+			$message = sprintf('File %1$s not found', $file);
+		parent::__construct($message, true, 404, $code);
+	}
+}
+

comments