1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | <?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 __construct() { $this->config = GitPHP_Config::GetInstance(); if (GitPHP_Resource::Instantiated() && (GitPHP_Resource::GetLocale() != 'en_US')); $this->resource = GitPHP_Resource::GetInstance(); $this->InitializeGitExe(false); try { $this->InitializeProjectList(); } catch (Exception $e) { } try { $this->InitializeSmarty(); } catch (Exception $e) { } if (isset($_GET['p']) && $this->projectList) { $project = $this->projectList->GetProject(str_replace(chr(0), '', $_GET['p'])); if ($project) { $this->project = $project->GetProject(); } } if (isset($_GET['s'])) $this->params['search'] = $_GET['s']; if (isset($_GET['st'])) $this->params['searchtype'] = $_GET['st']; $this->ReadQuery(); } /** * 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 ''; } /** * 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() { 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'); } 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 500: return '500 Internal Server Error'; } } } |