Controller_Blob.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Controller for displaying a blob
  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_Blob extends GitPHP_ControllerBase
  11. {
  12. /**
  13. * Initialize controller
  14. */
  15. public function Initialize()
  16. {
  17. parent::Initialize();
  18. if (empty($this->params['hashbase']))
  19. $this->params['hashbase'] = 'HEAD';
  20. }
  21. /**
  22. * Gets the template for this controller
  23. *
  24. * @return string template filename
  25. */
  26. protected function GetTemplate()
  27. {
  28. if ($this->Plain())
  29. return 'blobplain.tpl';
  30. return 'blob.tpl';
  31. }
  32. /**
  33. * Gets the cache key for this controller
  34. *
  35. * @return string cache key
  36. */
  37. protected function GetCacheKey()
  38. {
  39. return (isset($this->params['hashbase']) ? $this->params['hashbase'] : '') . '|' . (isset($this->params['hash']) ? $this->params['hash'] : '') . '|' . (isset($this->params['file']) ? sha1($this->params['file']) : '');
  40. }
  41. /**
  42. * Gets the name of this controller's action
  43. *
  44. * @param boolean $local true if caller wants the localized action name
  45. * @return string action name
  46. */
  47. public function GetName($local = false)
  48. {
  49. if ($local && $this->resource) {
  50. return $this->resource->translate('blob');
  51. }
  52. return 'blob';
  53. }
  54. /**
  55. * Loads headers for this template
  56. */
  57. protected function LoadHeaders()
  58. {
  59. if ($this->Plain()) {
  60. $this->DisableLogging();
  61. $this->preserveWhitespace = true;
  62. // XXX: Nasty hack to cache headers
  63. if (!$this->tpl->isCached('blobheaders.tpl', $this->GetFullCacheKey())) {
  64. if (isset($this->params['file']))
  65. $saveas = GitPHP_Util::BaseName($this->params['file']);
  66. else
  67. $saveas = $this->params['hash'] . ".txt";
  68. $headers = array();
  69. $mime = null;
  70. if ($this->config->GetValue('filemimetype')) {
  71. if ((!isset($this->params['hash'])) && (isset($this->params['file']))) {
  72. $commit = $this->GetProject()->GetCommit($this->params['hashbase']);
  73. $this->params['hash'] = $commit->GetTree()->PathToHash($this->params['file']);
  74. if (empty($this->params['hash']))
  75. throw new GitPHP_FileNotFoundException($this->params['file']);
  76. }
  77. $blob = $this->GetProject()->GetObjectManager()->GetBlob($this->params['hash']);
  78. if (!empty($this->params['file']))
  79. $blob->SetPath($this->params['file']);
  80. $mimeReader = new GitPHP_FileMimeTypeReader($blob, $this->GetMimeStrategy());
  81. $mime = trim($mimeReader->GetMimeType());
  82. }
  83. if ($mime)
  84. $headers[] = "Content-type: $mime; charset=UTF-8";
  85. else
  86. $headers[] = "Content-type: text/plain; charset=UTF-8";
  87. $headers[] = "Content-disposition: inline; filename=\"" . $saveas . "\"";
  88. $this->tpl->assign("blobheaders", serialize($headers));
  89. }
  90. $out = $this->tpl->fetch('blobheaders.tpl', $this->GetFullCacheKey());
  91. $this->headers = unserialize(trim($out));
  92. } else {
  93. parent::LoadHeaders();
  94. }
  95. }
  96. /**
  97. * Loads data for this template
  98. */
  99. protected function LoadData()
  100. {
  101. $commit = $this->GetProject()->GetCommit($this->params['hashbase']);
  102. $this->tpl->assign('commit', $commit);
  103. $tree = $commit->GetTree();
  104. $this->tpl->assign('tree', $commit->GetTree());
  105. if ((!isset($this->params['hash'])) && (isset($this->params['file']))) {
  106. $this->params['hash'] = $tree->PathToHash($this->params['file']);
  107. if (empty($this->params['hash']))
  108. throw new GitPHP_FileNotFoundException($this->params['file']);
  109. }
  110. $blob = $this->GetProject()->GetObjectManager()->GetBlob($this->params['hash']);
  111. if (!empty($this->params['file']))
  112. $blob->SetPath($this->params['file']);
  113. $blob->SetCommit($commit);
  114. $this->tpl->assign('blob', $blob);
  115. if ($this->Plain()) {
  116. return;
  117. }
  118. $head = $this->GetProject()->GetHeadCommit();
  119. $this->tpl->assign('head', $head);
  120. if ($this->config->GetValue('filemimetype')) {
  121. $mimeReader = new GitPHP_FileMimeTypeReader($blob, $this->GetMimeStrategy());
  122. $mimetype = $mimeReader->GetMimeType(true);
  123. if ($mimetype == 'image') {
  124. $this->tpl->assign('datatag', true);
  125. $this->tpl->assign('mime', $mimeReader->GetMimeType());
  126. $this->tpl->assign('data', base64_encode($blob->GetData()));
  127. return;
  128. }
  129. }
  130. if ($this->config->GetValue('geshi')) {
  131. include_once(GITPHP_GESHIDIR . "geshi.php");
  132. if (class_exists('GeSHi')) {
  133. $geshi = new GeSHi("",'php');
  134. if ($geshi) {
  135. $lang = GitPHP_Util::GeshiFilenameToLanguage($blob->GetName());
  136. if (empty($lang)) {
  137. $lang = $geshi->get_language_name_from_extension(substr(strrchr($blob->GetName(),'.'),1));
  138. }
  139. if (!empty($lang)) {
  140. $geshi->enable_classes();
  141. $geshi->enable_strict_mode(GESHI_MAYBE);
  142. $geshi->set_source($blob->GetData());
  143. $geshi->set_language($lang);
  144. $geshi->set_header_type(GESHI_HEADER_PRE_TABLE);
  145. $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
  146. $geshi->set_overall_id('blobData');
  147. $this->tpl->assign('geshiout', $geshi->parse_code());
  148. $this->tpl->assign('geshicss', $geshi->get_stylesheet());
  149. $this->tpl->assign('geshi', true);
  150. return;
  151. }
  152. }
  153. }
  154. }
  155. $this->tpl->assign('bloblines', $blob->GetData(true));
  156. }
  157. /**
  158. * Get valid mime strategy
  159. */
  160. private function GetMimeStrategy()
  161. {
  162. $strategy = new GitPHP_FileMimeType_Fileinfo($this->config->GetValue('magicdb'));
  163. if ($strategy->Valid())
  164. return $strategy;
  165. $strategy = new GitPHP_FileMimeType_FileExe();
  166. if ($strategy->Valid())
  167. return $strategy;
  168. $strategy = new GitPHP_FileMimeType_Extension();
  169. if ($strategy->Valid())
  170. return $strategy;
  171. }
  172. /**
  173. * Tests whether we are outputting a plaintext blob
  174. *
  175. * @return boolean true if plaintext blob
  176. */
  177. public function Plain()
  178. {
  179. if (isset($this->params['output']) && ($this->params['output'] == 'plain'))
  180. return true;
  181. return false;
  182. }
  183. }