Controller_DiffBase.class.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Base controller for diff-type views
  4. *
  5. * @author Christopher Han <xiphux@gmail.com>
  6. * @copyright Copyright (c) 2011 Christopher Han
  7. * @package GitPHP
  8. * @subpackage Controller
  9. */
  10. abstract class GitPHP_Controller_DiffBase extends GitPHP_ControllerBase
  11. {
  12. /**
  13. * Unified diff mode
  14. *
  15. * @var int
  16. */
  17. const UnifiedDiff = 1;
  18. /**
  19. * Side by side diff mode
  20. *
  21. * @var int
  22. */
  23. const SideBySideDiff = 2;
  24. /**
  25. * Diff mode cookie name
  26. *
  27. * @var string
  28. */
  29. const DiffModeCookie = 'GitPHPDiffMode';
  30. /**
  31. * Diff mode cookie lifetime
  32. *
  33. * @var int
  34. */
  35. const DiffModeCookieLifetime = 31536000; // 1 year
  36. /**
  37. * Initialize controller
  38. */
  39. public function Initialize()
  40. {
  41. parent::Initialize();
  42. if (!$this->Plain()) {
  43. if ($this->DiffMode(isset($this->params['diffmode']) ? $this->params['diffmode'] : '') == GitPHP_Controller_DiffBase::SideBySideDiff) {
  44. $this->params['sidebyside'] = true;
  45. }
  46. }
  47. }
  48. /**
  49. * Determines the diff mode to use
  50. *
  51. * @param string $overrideMode mode overridden by the user
  52. */
  53. protected function DiffMode($overrideMode = '')
  54. {
  55. $mode = GitPHP_Controller_DiffBase::UnifiedDiff; // default
  56. $baseurl = GitPHP_Util::BaseUrl();
  57. /*
  58. * Check cookie
  59. */
  60. if (!empty($_COOKIE[GitPHP_Controller_DiffBase::DiffModeCookie])) {
  61. $mode = $_COOKIE[GitPHP_Controller_DiffBase::DiffModeCookie];
  62. } else {
  63. /*
  64. * Create cookie to prevent browser delay
  65. */
  66. setcookie(GitPHP_Controller_DiffBase::DiffModeCookie, $mode, time()+GitPHP_Controller_DiffBase::DiffModeCookieLifetime, $baseurl);
  67. }
  68. if (!empty($overrideMode)) {
  69. /*
  70. * User is choosing a new mode
  71. */
  72. if ($overrideMode == 'sidebyside') {
  73. $mode = GitPHP_Controller_DiffBase::SideBySideDiff;
  74. setcookie(GitPHP_Controller_DiffBase::DiffModeCookie, GitPHP_Controller_DiffBase::SideBySideDiff, time()+GitPHP_Controller_DiffBase::DiffModeCookieLifetime, $baseurl);
  75. } else if ($overrideMode == 'unified') {
  76. $mode = GitPHP_Controller_DiffBase::UnifiedDiff;
  77. setcookie(GitPHP_Controller_DiffBase::DiffModeCookie, GitPHP_Controller_DiffBase::UnifiedDiff, time()+GitPHP_Controller_DiffBase::DiffModeCookieLifetime, $baseurl);
  78. }
  79. }
  80. return $mode;
  81. }
  82. /**
  83. * Loads headers for this template
  84. */
  85. protected function LoadHeaders()
  86. {
  87. if ($this->Plain()) {
  88. $this->DisableLogging();
  89. $this->headers[] = 'Content-type: text/plain; charset=UTF-8';
  90. } else {
  91. parent::LoadHeaders();
  92. }
  93. }
  94. /**
  95. * Tests if this is a plaintext diff
  96. *
  97. * @return boolean true if plaintext
  98. */
  99. protected function Plain()
  100. {
  101. if (isset($this->params['output']) && ($this->params['output'] == 'plain'))
  102. return true;
  103. return false;
  104. }
  105. }