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 | <?php /** * Base controller for diff-type views * * @author Christopher Han <xiphux@gmail.com> * @copyright Copyright (c) 2011 Christopher Han * @package GitPHP * @subpackage Controller */ abstract class GitPHP_Controller_DiffBase extends GitPHP_ControllerBase { /** * Unified diff mode * * @var int */ const UnifiedDiff = 1; /** * Side by side diff mode * * @var int */ const SideBySideDiff = 2; /** * Diff mode cookie name * * @var string */ const DiffModeCookie = 'GitPHPDiffMode'; /** * Diff mode cookie lifetime * * @var int */ const DiffModeCookieLifetime = 31536000; // 1 year /** * Initialize controller */ public function Initialize() { parent::Initialize(); if (!$this->Plain()) { if ($this->DiffMode(isset($this->params['diffmode']) ? $this->params['diffmode'] : '') == GitPHP_Controller_DiffBase::SideBySideDiff) { $this->params['sidebyside'] = true; } } } /** * Determines the diff mode to use * * @param string $overrideMode mode overridden by the user */ protected function DiffMode($overrideMode = '') { $mode = GitPHP_Controller_DiffBase::UnifiedDiff; // default $baseurl = GitPHP_Util::BaseUrl(); /* * Check cookie */ if (!empty($_COOKIE[GitPHP_Controller_DiffBase::DiffModeCookie])) { $mode = $_COOKIE[GitPHP_Controller_DiffBase::DiffModeCookie]; } else { /* * Create cookie to prevent browser delay */ setcookie(GitPHP_Controller_DiffBase::DiffModeCookie, $mode, time()+GitPHP_Controller_DiffBase::DiffModeCookieLifetime, $baseurl); } if (!empty($overrideMode)) { /* * User is choosing a new mode */ if ($overrideMode == 'sidebyside') { $mode = GitPHP_Controller_DiffBase::SideBySideDiff; setcookie(GitPHP_Controller_DiffBase::DiffModeCookie, GitPHP_Controller_DiffBase::SideBySideDiff, time()+GitPHP_Controller_DiffBase::DiffModeCookieLifetime, $baseurl); } else if ($overrideMode == 'unified') { $mode = GitPHP_Controller_DiffBase::UnifiedDiff; setcookie(GitPHP_Controller_DiffBase::DiffModeCookie, GitPHP_Controller_DiffBase::UnifiedDiff, time()+GitPHP_Controller_DiffBase::DiffModeCookieLifetime, $baseurl); } } return $mode; } /** * Loads headers for this template */ protected function LoadHeaders() { if ($this->Plain()) { $this->DisableLogging(); $this->headers[] = 'Content-type: text/plain; charset=UTF-8'; } else { parent::LoadHeaders(); } } /** * Tests if this is a plaintext diff * * @return boolean true if plaintext */ protected function Plain() { if (isset($this->params['output']) && ($this->params['output'] == 'plain')) return true; return false; } } |