Add specific exception for project list file read failure
[gitphp.git] / include / controller / Controller_Message.class.php
blob:a/include/controller/Controller_Message.class.php -> blob:b/include/controller/Controller_Message.class.php
<?php <?php
/** /**
* Controller for displaying a message page * Controller for displaying a message page
* *
* @author Christopher Han <xiphux@gmail.com> * @author Christopher Han <xiphux@gmail.com>
* @copyright Copyright (c) 2010 Christopher Han * @copyright Copyright (c) 2010 Christopher Han
* @package GitPHP * @package GitPHP
* @subpackage Controller * @subpackage Controller
*/ */
class GitPHP_Controller_Message extends GitPHP_ControllerBase class GitPHP_Controller_Message extends GitPHP_ControllerBase
{ {
/** /**
* Constructor * Constructor
*/ */
public function __construct() public function Initialize()
{ {
$this->config = GitPHP_Config::GetInstance(); try {
  $this->InitializeConfig();
if (GitPHP_Resource::Instantiated() && (GitPHP_Resource::GetLocale() != 'en_US')); } catch (Exception $e) {
$this->resource = GitPHP_Resource::GetInstance(); }
   
  $this->InitializeResource();
   
  $this->InitializeUserList();
   
$this->InitializeGitExe(false); $this->InitializeGitExe(false);
   
try { try {
$this->InitializeProjectList(); $this->InitializeProjectList();
} catch (Exception $e) { } catch (Exception $e) {
} }
   
try { try {
$this->InitializeSmarty(); $this->InitializeSmarty();
} catch (Exception $e) { } catch (Exception $e) {
} }
   
if (isset($_GET['p']) && $this->projectList) { if (!empty($this->params['project']) && $this->projectList) {
$project = $this->projectList->GetProject(str_replace(chr(0), '', $_GET['p'])); $project = $this->projectList->GetProject($this->params['project']);
if ($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'])) if (empty($this->params['hash']))
$this->params['search'] = $_GET['s']; $this->params['hash'] = 'HEAD';
if (isset($_GET['st']))  
$this->params['searchtype'] = $_GET['st'];  
   
$this->ReadQuery();  
} }
   
/** /**
* Gets the template for this controller * Gets the template for this controller
* *
* @return string template filename * @return string template filename
*/ */
protected function GetTemplate() protected function GetTemplate()
{ {
if ($this->project) if ($this->project)
return 'projectmessage.tpl'; return 'projectmessage.tpl';
return 'message.tpl'; return 'message.tpl';
} }
   
/** /**
* Gets the cache key for this controller * Gets the cache key for this controller
* *
* @return string cache key * @return string cache key
*/ */
protected function GetCacheKey() protected function GetCacheKey()
{ {
return sha1(serialize($this->params['exception'])); return sha1(serialize($this->params['exception']));
} }
   
/** /**
* Gets the name of this controller's action * Gets the name of this controller's action
* *
* @param boolean $local true if caller wants the localized action name * @param boolean $local true if caller wants the localized action name
* @return string action name * @return string action name
*/ */
public function GetName($local = false) public function GetName($local = false)
{ {
// This isn't a real controller // This isn't a real controller
return ''; return '';
} }
   
/** /**
* 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 * Loads headers for this template
*/ */
protected function LoadHeaders() protected function LoadHeaders()
{ {
  parent::LoadHeaders();
   
if (($this->params['exception'] instanceof GitPHP_MessageException) && ($this->params['exception']->StatusCode)) { if (($this->params['exception'] instanceof GitPHP_MessageException) && ($this->params['exception']->StatusCode)) {
$partialHeader = $this->StatusCodeHeader($this->params['exception']->StatusCode); $partialHeader = $this->StatusCodeHeader($this->params['exception']->StatusCode);
if (!empty($partialHeader)) { if (!empty($partialHeader)) {
if (substr(php_sapi_name(), 0, 8) == 'cgi-fcgi') { if (substr(php_sapi_name(), 0, 8) == 'cgi-fcgi') {
/* /*
* FastCGI requires a different header * FastCGI requires a different header
*/ */
$this->headers[] = 'Status: ' . $partialHeader; $this->headers[] = 'Status: ' . $partialHeader;
} else { } else {
$this->headers[] = 'HTTP/1.1 ' . $partialHeader; $this->headers[] = 'HTTP/1.1 ' . $partialHeader;
} }
} }
} }
} }
   
/** /**
* Loads data for this template * Loads data for this template
*/ */
protected function LoadData() protected function LoadData()
{ {
$this->tpl->assign('message', $this->ExceptionToMessage($this->params['exception'])); $this->tpl->assign('message', $this->ExceptionToMessage($this->params['exception']));
if (($this->params['exception'] instanceof GitPHP_MessageException) && ($this->params['exception']->Error)) { if (($this->params['exception'] instanceof GitPHP_MessageException) && ($this->params['exception']->Error)) {
$this->tpl->assign('error', true); $this->tpl->assign('error', true);
} }
if ($this->project) { if ($this->project) {
try { try {
$co = $this->GetProject()->GetCommit($this->params['hash']); $co = $this->GetProject()->GetCommit($this->params['hash']);
if ($co) { if ($co) {
$this->tpl->assign('commit', $co); $this->tpl->assign('commit', $co);
} }
} catch (Exception $e) { } catch (Exception $e) {
} }
} }
} }
   
/** /**
* Gets the user-displayed message for an exception * Gets the user-displayed message for an exception
* *
* @param Exception $exception exception * @param Exception $exception exception
* @return string message * @return string message
*/ */
private function ExceptionToMessage($exception) private function ExceptionToMessage($exception)
{ {
if (!$exception) if (!$exception)
return; return;
   
if ($exception instanceof GitPHP_InvalidProjectParameterException) { if ($exception instanceof GitPHP_InvalidProjectParameterException) {
if ($this->resource) if ($this->resource)
return sprintf($this->resource->translate('Invalid project %1$s'), $exception->Project); return sprintf($this->resource->translate('Invalid project %1$s'), $exception->Project);
return sprintf('Invalid project %1$s', $exception->Project); return sprintf('Invalid project %1$s', $exception->Project);
} }
if ($exception instanceof GitPHP_MissingProjectParameterException) { if ($exception instanceof GitPHP_MissingProjectParameterException) {
if ($this->resource) if ($this->resource)
return $this->resource->translate('Project is required'); return $this->resource->translate('Project is required');
return 'Project is required'; return 'Project is required';
} }
   
if ($exception instanceof GitPHP_SearchDisabledException) { if ($exception instanceof GitPHP_SearchDisabledException) {
if ($exception->FileSearch) { if ($exception->FileSearch) {
if ($this->resource) if ($this->resource)
return $this->resource->translate('File search has been disabled'); return $this->resource->translate('File search has been disabled');
return 'File search has been disabled'; return 'File search has been disabled';
} else { } else {
if ($this->resource) if ($this->resource)
return $this->resource->translate('Search has been disabled'); return $this->resource->translate('Search has been disabled');
return 'Search has been disabled'; return 'Search has been disabled';
} }
} }
   
if ($exception instanceof GitPHP_InvalidSearchTypeException) { if ($exception instanceof GitPHP_InvalidSearchTypeException) {
if ($this->resource) if ($this->resource)
return $this->resource->translate('Invalid search type'); return $this->resource->translate('Invalid search type');
return 'Invalid search type'; return 'Invalid search type';
} }
   
if ($exception instanceof GitPHP_SearchLengthException) { if ($exception instanceof GitPHP_SearchLengthException) {
if ($this->resource) if ($this->resource)
return sprintf($this->resource->ngettext('You must enter search text of at least %1$d character', 'You must enter search text of at least %1$d characters', $exception->MinimumLength), $exception->MinimumLength); return sprintf($this->resource->ngettext('You must enter search text of at least %1$d character', 'You must enter search text of at least %1$d characters', $exception->MinimumLength), $exception->MinimumLength);
return sprintf($exception->MinimumLength == 1 ? 'You must enter search text of at least %1$d character' : 'You must enter search text of at least %1$d characters', $exception->MinimumLength); return sprintf($exception->MinimumLength == 1 ? 'You must enter search text of at least %1$d character' : 'You must enter search text of at least %1$d characters', $exception->MinimumLength);
} }
   
if ($exception instanceof GitPHP_InvalidHashException) { if ($exception instanceof GitPHP_InvalidHashException) {
if ($this->resource) if ($this->resource)
return sprintf($this->resource->translate('Invalid hash %1$s'), $exception->Hash); return sprintf($this->resource->translate('Invalid hash %1$s'), $exception->Hash);
return sprintf('Invalid hash %1$s', $exception->Hash); return sprintf('Invalid hash %1$s', $exception->Hash);
} }
   
if ($exception instanceof GitPHP_InvalidGitExecutableException) { if ($exception instanceof GitPHP_InvalidGitExecutableException) {
if ($this->resource) 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($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'); 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 ($exception instanceof GitPHP_MissingProjectrootException) {
if ($this->resource) if ($this->resource)
return $this->resource->translate('A projectroot must be set in the config'); return $this->resource->translate('A projectroot must be set in the config');
return 'A projectroot must be set in the config'; return 'A projectroot must be set in the config';
} }
   
if ($exception instanceof GitPHP_MissingMemcacheException) { if ($exception instanceof GitPHP_MissingMemcacheException) {
if ($this->resource) if ($this->resource)
return $this->resource->translate('The Memcached or Memcache PHP extension is required for Memcache support'); 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'; return 'The Memcached or Memcache PHP extension is required for Memcache support';
} }
   
if (($exception instanceof GitPHP_InvalidDirectoryException) || ($exception instanceof GitPHP_InvalidDirectoryConfigurationException)) { if (($exception instanceof GitPHP_InvalidDirectoryException) || ($exception instanceof GitPHP_InvalidDirectoryConfigurationException)) {
if ($this->resource) if ($this->resource)
return sprintf($this->resource->translate('%1$s is not a directory'), $exception->Directory); return sprintf($this->resource->translate('%1$s is not a directory'), $exception->Directory);
return sprintf('%1$s is not a directory', $exception->Directory); return sprintf('%1$s is not a directory', $exception->Directory);
} }
   
if ($exception instanceof GitPHP_InvalidFileException) { if ($exception instanceof GitPHP_InvalidFileException) {
if ($this->resource) if ($this->resource)
return sprintf($this->resource->translate('%1$s is not a file'), $exception->File); return sprintf($this->resource->translate('%1$s is not a file'), $exception->File);
return sprintf('%1$s is not a file', $exception->File); return sprintf('%1$s is not a file', $exception->File);
} }
   
if ($exception instanceof GitPHP_InvalidGitRepositoryException) { if ($exception instanceof GitPHP_InvalidGitRepositoryException) {
if ($this->resource) if ($this->resource)
return sprintf($this->resource->translate('%1$s is not a git repository'), $exception->Repository); 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); return sprintf('%1$s is not a git repository', $exception->Repository);
} }
   
if ($exception instanceof GitPHP_ProjectListFileReadException) { if ($exception instanceof GitPHP_ProjectListFileReadException) {
if ($this->resource) if ($this->resource)
return sprintf($this->resource->translate('Failed to open project list file %1$s'), $exception->File); 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); 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(); return $exception->getMessage();
} }
   
/** /**
* Gets the header for an HTTP status code * Gets the header for an HTTP status code
* *
* @param integer $code status code * @param integer $code status code
* @return string header * @return string header
*/ */
private function StatusCodeHeader($code) private function StatusCodeHeader($code)
{ {
switch ($code) { switch ($code) {
  case 403:
  return '403 Forbidden';
  case 404:
  return '404 Not Found';
case 500: case 500:
return '500 Internal Server Error'; return '500 Internal Server Error';
} }
} }
   
} }
   
comments