123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- /**
- * Controller for displaying a message page
- *
- * @author Christopher Han <xiphux@gmail.com>
- * @copyright Copyright (c) 2010 Christopher Han
- * @package GitPHP
- * @subpackage Controller
- */
- class GitPHP_Controller_Message extends GitPHP_ControllerBase
- {
- /**
- * Constructor
- */
- public function Initialize()
- {
- try {
- $this->InitializeConfig();
- } catch (Exception $e) {
- }
- $this->InitializeResource();
- $this->InitializeUserList();
- $this->InitializeGitExe(false);
- try {
- $this->InitializeProjectList();
- } catch (Exception $e) {
- }
- try {
- $this->InitializeSmarty();
- } catch (Exception $e) {
- }
- if (!empty($this->params['project']) && $this->projectList) {
- $project = $this->projectList->GetProject($this->params['project']);
- if ($project) {
- 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 (empty($this->params['hash']))
- $this->params['hash'] = 'HEAD';
- }
- /**
- * Gets the template for this controller
- *
- * @return string template filename
- */
- protected function GetTemplate()
- {
- if ($this->project)
- return 'projectmessage.tpl';
- return 'message.tpl';
- }
- /**
- * Gets the cache key for this controller
- *
- * @return string cache key
- */
- protected function GetCacheKey()
- {
- return sha1(serialize($this->params['exception']));
- }
- /**
- * Gets the name of this controller's action
- *
- * @param boolean $local true if caller wants the localized action name
- * @return string action name
- */
- public function GetName($local = false)
- {
- // This isn't a real controller
- return '';
- }
- /**
- * 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)) {
- if (substr(php_sapi_name(), 0, 8) == 'cgi-fcgi') {
- /*
- * FastCGI requires a different header
- */
- $this->headers[] = 'Status: ' . $partialHeader;
- } else {
- $this->headers[] = 'HTTP/1.1 ' . $partialHeader;
- }
- }
- }
- }
- /**
- * Loads data for this template
- */
- protected function LoadData()
- {
- $this->tpl->assign('message', $this->ExceptionToMessage($this->params['exception']));
- if (($this->params['exception'] instanceof GitPHP_MessageException) && ($this->params['exception']->Error)) {
- $this->tpl->assign('error', true);
- }
- if ($this->project) {
- try {
- $co = $this->GetProject()->GetCommit($this->params['hash']);
- if ($co) {
- $this->tpl->assign('commit', $co);
- }
- } catch (Exception $e) {
- }
- }
- }
- /**
- * Gets the user-displayed message for an exception
- *
- * @param Exception $exception exception
- * @return string message
- */
- private function ExceptionToMessage($exception)
- {
- if (!$exception)
- return;
- if ($exception instanceof GitPHP_InvalidProjectParameterException) {
- if ($this->resource)
- return sprintf($this->resource->translate('Invalid project %1$s'), $exception->Project);
- return sprintf('Invalid project %1$s', $exception->Project);
- }
-
- if ($exception instanceof GitPHP_MissingProjectParameterException) {
- if ($this->resource)
- return $this->resource->translate('Project is required');
- return 'Project is required';
- }
- if ($exception instanceof GitPHP_SearchDisabledException) {
- if ($exception->FileSearch) {
- if ($this->resource)
- return $this->resource->translate('File search has been disabled');
- return 'File search has been disabled';
- } else {
- if ($this->resource)
- return $this->resource->translate('Search has been disabled');
- return 'Search has been disabled';
- }
- }
- if ($exception instanceof GitPHP_InvalidSearchTypeException) {
- if ($this->resource)
- return $this->resource->translate('Invalid search type');
- return 'Invalid search type';
- }
- if ($exception instanceof GitPHP_SearchLengthException) {
- 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($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 ($this->resource)
- return sprintf($this->resource->translate('Invalid hash %1$s'), $exception->Hash);
- 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();
- }
- /**
- * Gets the header for an HTTP status code
- *
- * @param integer $code status code
- * @return string header
- */
- private function StatusCodeHeader($code)
- {
- switch ($code) {
- case 403:
- return '403 Forbidden';
- case 404:
- return '404 Not Found';
- case 500:
- return '500 Internal Server Error';
- }
- }
- }
|