Controller_Message.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /**
  3. * Controller for displaying a message page
  4. *
  5. * @author Christopher Han <xiphux@gmail.com>
  6. * @copyright Copyright (c) 2010 Christopher Han
  7. * @package GitPHP
  8. * @subpackage Controller
  9. */
  10. class GitPHP_Controller_Message extends GitPHP_ControllerBase
  11. {
  12. /**
  13. * Constructor
  14. */
  15. public function Initialize()
  16. {
  17. try {
  18. $this->InitializeConfig();
  19. } catch (Exception $e) {
  20. }
  21. $this->InitializeResource();
  22. $this->InitializeUserList();
  23. $this->InitializeGitExe(false);
  24. try {
  25. $this->InitializeProjectList();
  26. } catch (Exception $e) {
  27. }
  28. try {
  29. $this->InitializeSmarty();
  30. } catch (Exception $e) {
  31. }
  32. if (!empty($this->params['project']) && $this->projectList) {
  33. $project = $this->projectList->GetProject($this->params['project']);
  34. if ($project) {
  35. if ($this->userList && ($this->userList->GetCount() > 0)) {
  36. if ($project->UserCanAccess((!empty($_SESSION['gitphpuser']) ? $_SESSION['gitphpuser'] : null))) {
  37. $this->project = $project->GetProject();
  38. }
  39. } else {
  40. $this->project = $project->GetProject();
  41. }
  42. }
  43. }
  44. if (empty($this->params['hash']))
  45. $this->params['hash'] = 'HEAD';
  46. }
  47. /**
  48. * Gets the template for this controller
  49. *
  50. * @return string template filename
  51. */
  52. protected function GetTemplate()
  53. {
  54. if ($this->project)
  55. return 'projectmessage.tpl';
  56. return 'message.tpl';
  57. }
  58. /**
  59. * Gets the cache key for this controller
  60. *
  61. * @return string cache key
  62. */
  63. protected function GetCacheKey()
  64. {
  65. return sha1(serialize($this->params['exception']));
  66. }
  67. /**
  68. * Gets the name of this controller's action
  69. *
  70. * @param boolean $local true if caller wants the localized action name
  71. * @return string action name
  72. */
  73. public function GetName($local = false)
  74. {
  75. // This isn't a real controller
  76. return '';
  77. }
  78. /**
  79. * Loads headers for this template
  80. */
  81. protected function LoadHeaders()
  82. {
  83. parent::LoadHeaders();
  84. if (($this->params['exception'] instanceof GitPHP_MessageException) && ($this->params['exception']->StatusCode)) {
  85. $partialHeader = $this->StatusCodeHeader($this->params['exception']->StatusCode);
  86. if (!empty($partialHeader)) {
  87. if (substr(php_sapi_name(), 0, 8) == 'cgi-fcgi') {
  88. /*
  89. * FastCGI requires a different header
  90. */
  91. $this->headers[] = 'Status: ' . $partialHeader;
  92. } else {
  93. $this->headers[] = 'HTTP/1.1 ' . $partialHeader;
  94. }
  95. }
  96. }
  97. }
  98. /**
  99. * Loads data for this template
  100. */
  101. protected function LoadData()
  102. {
  103. $this->tpl->assign('message', $this->ExceptionToMessage($this->params['exception']));
  104. if (($this->params['exception'] instanceof GitPHP_MessageException) && ($this->params['exception']->Error)) {
  105. $this->tpl->assign('error', true);
  106. }
  107. if ($this->project) {
  108. try {
  109. $co = $this->GetProject()->GetCommit($this->params['hash']);
  110. if ($co) {
  111. $this->tpl->assign('commit', $co);
  112. }
  113. } catch (Exception $e) {
  114. }
  115. }
  116. }
  117. /**
  118. * Gets the user-displayed message for an exception
  119. *
  120. * @param Exception $exception exception
  121. * @return string message
  122. */
  123. private function ExceptionToMessage($exception)
  124. {
  125. if (!$exception)
  126. return;
  127. if ($exception instanceof GitPHP_InvalidProjectParameterException) {
  128. if ($this->resource)
  129. return sprintf($this->resource->translate('Invalid project %1$s'), $exception->Project);
  130. return sprintf('Invalid project %1$s', $exception->Project);
  131. }
  132. if ($exception instanceof GitPHP_MissingProjectParameterException) {
  133. if ($this->resource)
  134. return $this->resource->translate('Project is required');
  135. return 'Project is required';
  136. }
  137. if ($exception instanceof GitPHP_SearchDisabledException) {
  138. if ($exception->FileSearch) {
  139. if ($this->resource)
  140. return $this->resource->translate('File search has been disabled');
  141. return 'File search has been disabled';
  142. } else {
  143. if ($this->resource)
  144. return $this->resource->translate('Search has been disabled');
  145. return 'Search has been disabled';
  146. }
  147. }
  148. if ($exception instanceof GitPHP_InvalidSearchTypeException) {
  149. if ($this->resource)
  150. return $this->resource->translate('Invalid search type');
  151. return 'Invalid search type';
  152. }
  153. if ($exception instanceof GitPHP_SearchLengthException) {
  154. if ($this->resource)
  155. 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);
  156. 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);
  157. }
  158. if ($exception instanceof GitPHP_InvalidHashException) {
  159. if ($this->resource)
  160. return sprintf($this->resource->translate('Invalid hash %1$s'), $exception->Hash);
  161. return sprintf('Invalid hash %1$s', $exception->Hash);
  162. }
  163. if ($exception instanceof GitPHP_InvalidGitExecutableException) {
  164. if ($this->resource)
  165. 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');
  166. return sprintf('Could not run the git executable "%1$s". You may need to set the "%2$s" config value.', $exception->Executable, 'gitbin');
  167. }
  168. if ($exception instanceof GitPHP_MissingProjectrootException) {
  169. if ($this->resource)
  170. return $this->resource->translate('A projectroot must be set in the config');
  171. return 'A projectroot must be set in the config';
  172. }
  173. if ($exception instanceof GitPHP_MissingMemcacheException) {
  174. if ($this->resource)
  175. return $this->resource->translate('The Memcached or Memcache PHP extension is required for Memcache support');
  176. return 'The Memcached or Memcache PHP extension is required for Memcache support';
  177. }
  178. if (($exception instanceof GitPHP_InvalidDirectoryException) || ($exception instanceof GitPHP_InvalidDirectoryConfigurationException)) {
  179. if ($this->resource)
  180. return sprintf($this->resource->translate('%1$s is not a directory'), $exception->Directory);
  181. return sprintf('%1$s is not a directory', $exception->Directory);
  182. }
  183. if ($exception instanceof GitPHP_InvalidFileException) {
  184. if ($this->resource)
  185. return sprintf($this->resource->translate('%1$s is not a file'), $exception->File);
  186. return sprintf('%1$s is not a file', $exception->File);
  187. }
  188. if ($exception instanceof GitPHP_InvalidGitRepositoryException) {
  189. if ($this->resource)
  190. return sprintf($this->resource->translate('%1$s is not a git repository'), $exception->Repository);
  191. return sprintf('%1$s is not a git repository', $exception->Repository);
  192. }
  193. if ($exception instanceof GitPHP_ProjectListFileReadException) {
  194. if ($this->resource)
  195. return sprintf($this->resource->translate('Failed to open project list file %1$s'), $exception->File);
  196. return sprintf('Failed to open project list file %1$s', $exception->File);
  197. }
  198. if ($exception instanceof GitPHP_DirectoryTraversalException) {
  199. if ($this->resource)
  200. return sprintf($this->resource->translate('%1$s is attempting directory traversal'), $exception->Path);
  201. return sprintf('%1$s is attempting directory traversal', $exception->Path);
  202. }
  203. if ($exception instanceof GitPHP_ProjectrootBoundException) {
  204. if ($this->resource)
  205. return sprintf($this->resource->translate('%1$s is outside of the projectroot'), $exception->Path);
  206. return sprintf('%1$s is outside of the projectroot', $exception->Path);
  207. }
  208. if ($exception instanceof GitPHP_InvalidConfigFileException) {
  209. if ($this->resource)
  210. return sprintf($this->resource->translate('Could not load config file %1$s'), $exception->File);
  211. return sprintf('Could not load config file %1$s', $exception->File);
  212. }
  213. if ($exception instanceof GitPHP_AmbiguousHashException) {
  214. if ($this->resource)
  215. return sprintf($this->resource->translate('Ambiguous abbreviated hash %1$s'), $exception->Hash);
  216. return sprintf('Ambiguous abbreviated hash %1$s', $exception->Hash);
  217. }
  218. if ($exception instanceof GitPHP_DirectoryNotFoundException) {
  219. if ($this->resource)
  220. return sprintf($this->resource->translate('Directory %1$s not found'), $exception->Directory);
  221. return sprintf('Directory %1$s not found', $exception->Directory);
  222. }
  223. if ($exception instanceof GitPHP_FileNotFoundException) {
  224. if ($this->resource)
  225. return sprintf($this->resource->translate('File %1$s not found'), $exception->File);
  226. return sprintf('File %1$s not found', $exception->File);
  227. }
  228. if ($exception instanceof GitPHP_UnauthorizedProjectException) {
  229. if ($this->resource)
  230. return sprintf($this->resource->translate('You are not authorized to access project %1$s'), $exception->Project);
  231. return sprintf('You are not authorized to access project %1$s', $exception->Project);
  232. }
  233. if ($exception instanceof GitPHP_DisabledFunctionException) {
  234. if ($this->resource)
  235. return sprintf($this->resource->translate('Required function %1$s has been disabled'), $exception->Function);
  236. return sprintf('Required function %1$s has been disabled', $exception->Function);
  237. }
  238. return $exception->getMessage();
  239. }
  240. /**
  241. * Gets the header for an HTTP status code
  242. *
  243. * @param integer $code status code
  244. * @return string header
  245. */
  246. private function StatusCodeHeader($code)
  247. {
  248. switch ($code) {
  249. case 403:
  250. return '403 Forbidden';
  251. case 404:
  252. return '404 Not Found';
  253. case 500:
  254. return '500 Internal Server Error';
  255. }
  256. }
  257. }