Consolidate copy-and-pasted diff mode cookie code
Consolidate copy-and-pasted diff mode cookie code

<?php <?php
/** /**
* GitPHP Controller Blobdiff * GitPHP Controller Blobdiff
* *
* Controller for displaying a blobdiff * Controller for displaying a blobdiff
* *
* @author Christopher Han <xiphux@gmail.com> * @author Christopher Han <xiphux@gmail.com>
* @copyright Copyright (c) 2010 Christopher Han * @copyright Copyright (c) 2010 Christopher Han
* @package GitPHP * @package GitPHP
* @subpackage Controller * @subpackage Controller
*/ */
   
/** require_once(GITPHP_CONTROLLERDIR . 'Controller_DiffBase.class.php');
* Constants for blobdiff modes  
*/  
define('GITPHP_BLOBDIFF_UNIFIED', 1);  
define('GITPHP_BLOBDIFF_SIDEBYSIDE', 2);  
   
/**  
* Constant of the blobdiff mode cookie in the user's browser  
*/  
define('GITPHP_BLOBDIFF_MODE_COOKIE', 'GitPHPBlobdiffMode');  
   
/**  
* Blobdiff mode cookie lifetime  
*/  
define('GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME', 60*60*24*365); // 1 year  
   
/** /**
* Blobdiff controller class * Blobdiff controller class
* *
* @package GitPHP * @package GitPHP
* @subpackage Controller * @subpackage Controller
*/ */
class GitPHP_Controller_Blobdiff extends GitPHP_ControllerBase class GitPHP_Controller_Blobdiff extends GitPHP_Controller_DiffBase
{ {
   
/** /**
* __construct * __construct
* *
* Constructor * Constructor
* *
* @access public * @access public
* @return controller * @return controller
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
if (!$this->project) { if (!$this->project) {
throw new GitPHP_MessageException(__('Project is required'), true); throw new GitPHP_MessageException(__('Project is required'), true);
} }
} }
   
/** /**
* GetTemplate * GetTemplate
* *
* Gets the template for this controller * Gets the template for this controller
* *
* @access protected * @access protected
* @return string template filename * @return string template filename
*/ */
protected function GetTemplate() protected function GetTemplate()
{ {
if (isset($this->params['plain']) && ($this->params['plain'] === true)) { if (isset($this->params['plain']) && ($this->params['plain'] === true)) {
return 'blobdiffplain.tpl'; return 'blobdiffplain.tpl';
} }
return 'blobdiff.tpl'; return 'blobdiff.tpl';
} }
   
/** /**
* GetCacheKey * GetCacheKey
* *
* Gets the cache key for this controller * Gets the cache key for this controller
* *
* @access protected * @access protected
* @return string cache key * @return string cache key
*/ */
protected function GetCacheKey() protected function GetCacheKey()
{ {
return (isset($this->params['hashbase']) ? $this->params['hashbase'] : '') . '|' . (isset($this->params['hash']) ? $this->params['hash'] : '') . '|' . (isset($this->params['hashparent']) ? $this->params['hashparent'] : '') . '|' . (isset($this->params['file']) ? sha1($this->params['file']) : '') . '|' . (isset($this->params['sidebyside']) && ($this->params['sidebyside'] === true) ? '1' : ''); return (isset($this->params['hashbase']) ? $this->params['hashbase'] : '') . '|' . (isset($this->params['hash']) ? $this->params['hash'] : '') . '|' . (isset($this->params['hashparent']) ? $this->params['hashparent'] : '') . '|' . (isset($this->params['file']) ? sha1($this->params['file']) : '') . '|' . (isset($this->params['sidebyside']) && ($this->params['sidebyside'] === true) ? '1' : '');
} }
   
/** /**
* GetName * GetName
* *
* Gets the name of this controller's action * Gets the name of this controller's action
* *
* @access public * @access public
* @param boolean $local true if caller wants the localized action name * @param boolean $local true if caller wants the localized action name
* @return string action name * @return string action name
*/ */
public function GetName($local = false) public function GetName($local = false)
{ {
if ($local) { if ($local) {
return __('blobdiff'); return __('blobdiff');
} }
return 'blobdiff'; return 'blobdiff';
} }
   
/** /**
* ReadQuery * ReadQuery
* *
* Read query into parameters * Read query into parameters
* *
* @access protected * @access protected
*/ */
protected function ReadQuery() protected function ReadQuery()
{ {
  parent::ReadQuery();
   
if (isset($_GET['f'])) if (isset($_GET['f']))
$this->params['file'] = $_GET['f']; $this->params['file'] = $_GET['f'];
if (isset($_GET['h'])) if (isset($_GET['h']))
$this->params['hash'] = $_GET['h']; $this->params['hash'] = $_GET['h'];
if (isset($_GET['hb'])) if (isset($_GET['hb']))
$this->params['hashbase'] = $_GET['hb']; $this->params['hashbase'] = $_GET['hb'];
if (isset($_GET['hp'])) if (isset($_GET['hp']))
$this->params['hashparent'] = $_GET['hp']; $this->params['hashparent'] = $_GET['hp'];
   
if (!isset($this->params['plain']) || $this->params['plain'] != true) {  
   
$mode = GITPHP_BLOBDIFF_UNIFIED; // default  
   
/*  
* Check cookie  
*/  
if (!empty($_COOKIE[GITPHP_BLOBDIFF_MODE_COOKIE])) {  
$mode = $_COOKIE[GITPHP_BLOBDIFF_MODE_COOKIE];  
} else {  
/*  
* Create cookie to prevent browser delay  
*/  
setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, $mode, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);  
}  
   
if (isset($_GET['o'])) {  
/*  
* User is choosing a new mode  
*/  
if ($_GET['o'] == 'sidebyside') {  
$mode = GITPHP_BLOBDIFF_SIDEBYSIDE;  
setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, GITPHP_BLOBDIFF_SIDEBYSIDE, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);  
} else if ($_GET['o'] == 'unified') {  
$mode = GITPHP_BLOBDIFF_UNIFIED;  
setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, GITPHP_BLOBDIFF_UNIFIED, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);  
}  
}  
   
if ($mode == GITPHP_BLOBDIFF_SIDEBYSIDE) {  
$this->params['sidebyside'] = true;  
}  
   
}  
}  
   
/**  
* LoadHeaders  
*  
* Loads headers for this template  
*  
* @access protected  
*/  
protected function LoadHeaders()  
{  
if (isset($this->params['plain']) && ($this->params['plain'] === true)) {  
$this->headers[] = 'Content-type: text/plain; charset=UTF-8';  
}  
} }
   
/** /**
* LoadData * LoadData
* *
* Loads data for this template * Loads data for this template
* *
* @access protected * @access protected
*/ */
protected function LoadData() protected function LoadData()
{ {
if (isset($this->params['file'])) if (isset($this->params['file']))
$this->tpl->assign('file', $this->params['file']); $this->tpl->assign('file', $this->params['file']);
   
$filediff = new GitPHP_FileDiff($this->project, $this->params['hashparent'], $this->params['hash']); $filediff = new GitPHP_FileDiff($this->project, $this->params['hashparent'], $this->params['hash']);
$this->tpl->assign('filediff', $filediff); $this->tpl->assign('filediff', $filediff);
   
if (isset($this->params['plain']) && ($this->params['plain'] === true)) { if (isset($this->params['plain']) && ($this->params['plain'] === true)) {
return; return;
} }
   
if (isset($this->params['sidebyside']) && ($this->params['sidebyside'] === true)) { if (isset($this->params['sidebyside']) && ($this->params['sidebyside'] === true)) {
$this->tpl->assign('sidebyside', true); $this->tpl->assign('sidebyside', true);
} }
   
$commit = $this->project->GetCommit($this->params['hashbase']); $commit = $this->project->GetCommit($this->params['hashbase']);
$this->tpl->assign('commit', $commit); $this->tpl->assign('commit', $commit);
   
$blobparent = $this->project->GetBlob($this->params['hashparent']); $blobparent = $this->project->GetBlob($this->params['hashparent']);
$blobparent->SetCommit($commit); $blobparent->SetCommit($commit);
$blobparent->SetPath($this->params['file']); $blobparent->SetPath($this->params['file']);
$this->tpl->assign('blobparent', $blobparent); $this->tpl->assign('blobparent', $blobparent);
   
$blob = $this->project->GetBlob($this->params['hash']); $blob = $this->project->GetBlob($this->params['hash']);
$blob->SetPath($this->params['file']); $blob->SetPath($this->params['file']);
$this->tpl->assign('blob', $blob); $this->tpl->assign('blob', $blob);
   
$tree = $commit->GetTree(); $tree = $commit->GetTree();
$this->tpl->assign('tree', $tree); $this->tpl->assign('tree', $tree);
} }
   
} }
   
<?php <?php
/** /**
* GitPHP Controller Commitdiff * GitPHP Controller Commitdiff
* *
* Controller for displaying a commitdiff * Controller for displaying a commitdiff
* *
* @author Christopher Han <xiphux@gmail.com> * @author Christopher Han <xiphux@gmail.com>
* @copyright Copyright (c) 2010 Christopher Han * @copyright Copyright (c) 2010 Christopher Han
* @package GitPHP * @package GitPHP
* @subpackage Controller * @subpackage Controller
*/ */
   
/** require_once(GITPHP_CONTROLLERDIR . 'Controller_DiffBase.class.php');
* Constants for blobdiff modes  
*/  
define('GITPHP_BLOBDIFF_UNIFIED', 1);  
define('GITPHP_BLOBDIFF_SIDEBYSIDE', 2);  
   
/**  
* Constant of the blobdiff mode cookie in the user's browser  
*/  
define('GITPHP_BLOBDIFF_MODE_COOKIE', 'GitPHPBlobdiffMode');  
   
/**  
* Blobdiff mode cookie lifetime  
*/  
define('GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME', 60*60*24*365); // 1 year  
   
/** /**
* Commitdiff controller class * Commitdiff controller class
* *
* @package GitPHP * @package GitPHP
* @subpackage Controller * @subpackage Controller
*/ */
class GitPHP_Controller_Commitdiff extends GitPHP_ControllerBase class GitPHP_Controller_Commitdiff extends GitPHP_Controller_DiffBase
{ {
   
/** /**
* __construct * __construct
* *
* Constructor * Constructor
* *
* @access public * @access public
* @return controller * @return controller
*/ */
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();
if (!$this->project) { if (!$this->project) {
throw new GitPHP_MessageException(__('Project is required'), true); throw new GitPHP_MessageException(__('Project is required'), true);
} }
} }
   
/** /**
* GetTemplate * GetTemplate
* *
* Gets the template for this controller * Gets the template for this controller
* *
* @access protected * @access protected
* @return string template filename * @return string template filename
*/ */
protected function GetTemplate() protected function GetTemplate()
{ {
if (isset($this->params['plain']) && ($this->params['plain'] === true)) { if (isset($this->params['plain']) && ($this->params['plain'] === true)) {
return 'commitdiffplain.tpl'; return 'commitdiffplain.tpl';
} }
return 'commitdiff.tpl'; return 'commitdiff.tpl';
} }
   
/** /**
* GetCacheKey * GetCacheKey
* *
* Gets the cache key for this controller * Gets the cache key for this controller
* *
* @access protected * @access protected
* @return string cache key * @return string cache key
*/ */
protected function GetCacheKey() protected function GetCacheKey()
{ {
$key = (isset($this->params['hash']) ? $this->params['hash'] : '') $key = (isset($this->params['hash']) ? $this->params['hash'] : '')
. '|' . (isset($this->params['hashparent']) ? $this->params['hashparent'] : '') . '|' . (isset($this->params['hashparent']) ? $this->params['hashparent'] : '')
. '|' . (isset($this->params['sidebyside']) && ($this->params['sidebyside'] === true) ? '1' : ''); . '|' . (isset($this->params['sidebyside']) && ($this->params['sidebyside'] === true) ? '1' : '');
   
return $key; return $key;
} }
   
/** /**
* GetName * GetName
* *
* Gets the name of this controller's action * Gets the name of this controller's action
* *
* @access public * @access public
* @param boolean $local true if caller wants the localized action name * @param boolean $local true if caller wants the localized action name
* @return string action name * @return string action name
*/ */
public function GetName($local = false) public function GetName($local = false)
{ {
if ($local) { if ($local) {
return __('commitdiff'); return __('commitdiff');
} }
return 'commitdiff'; return 'commitdiff';
} }
   
/** /**
* ReadQuery * ReadQuery
* *
* Read query into parameters * Read query into parameters
* *
* @access protected * @access protected
*/ */
protected function ReadQuery() protected function ReadQuery()
{ {
  parent::ReadQuery();
   
if (isset($_GET['h'])) if (isset($_GET['h']))
$this->params['hash'] = $_GET['h']; $this->params['hash'] = $_GET['h'];
if (isset($_GET['hp'])) if (isset($_GET['hp']))
$this->params['hashparent'] = $_GET['hp']; $this->params['hashparent'] = $_GET['hp'];
   
if (!isset($this->params['plain']) || $this->params['plain'] != true) {  
   
$mode = GITPHP_BLOBDIFF_UNIFIED; // default  
   
   
/*  
* Check cookie  
*/  
if (!empty($_COOKIE[GITPHP_BLOBDIFF_MODE_COOKIE])) {  
$mode = $_COOKIE[GITPHP_BLOBDIFF_MODE_COOKIE];  
} else {  
/*  
* Create cookie to prevent browser delay  
*/  
setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, $mode, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);  
}  
   
if (isset($_GET['o'])) {  
/*  
* User is choosing a new mode  
*/  
if ($_GET['o'] == 'sidebyside') {  
$mode = GITPHP_BLOBDIFF_SIDEBYSIDE;  
setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, GITPHP_BLOBDIFF_SIDEBYSIDE, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);  
} else if ($_GET['o'] == 'unified') {  
$mode = GITPHP_BLOBDIFF_UNIFIED;  
setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, GITPHP_BLOBDIFF_UNIFIED, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);  
}  
}  
   
if ($mode == GITPHP_BLOBDIFF_SIDEBYSIDE) {  
$this->params['sidebyside'] = true;  
}  
}  
} }
   
/** /**
* LoadHeaders * LoadHeaders
* *
* Loads headers for this template * Loads headers for this template
* *
* @access protected * @access protected
*/ */
protected function LoadHeaders() protected function LoadHeaders()
{ {
  parent::LoadHeaders();
   
if (isset($this->params['plain']) && ($this->params['plain'] === true)) { if (isset($this->params['plain']) && ($this->params['plain'] === true)) {
GitPHP_Log::GetInstance()->SetEnabled(false);  
   
$this->headers[] = 'Content-type: text/plain; charset=UTF-8';  
$this->headers[] = 'Content-disposition: inline; filename="git-' . $this->params['hash'] . '.patch"'; $this->headers[] = 'Content-disposition: inline; filename="git-' . $this->params['hash'] . '.patch"';
} }
} }
   
/** /**
* LoadData * LoadData
* *
* Loads data for this template * Loads data for this template
* *
* @access protected * @access protected
*/ */
protected function LoadData() protected function LoadData()
{ {
$co = $this->project->GetCommit($this->params['hash']); $co = $this->project->GetCommit($this->params['hash']);
$this->tpl->assign('commit', $co); $this->tpl->assign('commit', $co);
   
if (isset($this->params['hashparent'])) { if (isset($this->params['hashparent'])) {
$this->tpl->assign("hashparent", $this->params['hashparent']); $this->tpl->assign("hashparent", $this->params['hashparent']);
} }
   
if (isset($this->params['sidebyside']) && ($this->params['sidebyside'] === true)) { if (isset($this->params['sidebyside']) && ($this->params['sidebyside'] === true)) {
$this->tpl->assign('sidebyside', true); $this->tpl->assign('sidebyside', true);
} }
   
$treediff = new GitPHP_TreeDiff($this->project, $this->params['hash'], (isset($this->params['hashparent']) ? $this->params['hashparent'] : '')); $treediff = new GitPHP_TreeDiff($this->project, $this->params['hash'], (isset($this->params['hashparent']) ? $this->params['hashparent'] : ''));
$this->tpl->assign('treediff', $treediff); $this->tpl->assign('treediff', $treediff);
} }
   
} }
   
  <?php
  /**
  * GitPHP Controller DiffBase
  *
  * Base controller for diff-type views
  *
  * @author Christopher Han <xiphux@gmail.com>
  * @copyright Copyright (c) 2011 Christopher Han
  * @package GitPHP
  * @subpackage Controller
  */
 
 
  /**
  * Constants for diff modes
  */
  define('GITPHP_DIFF_UNIFIED', 1);
  define('GITPHP_DIFF_SIDEBYSIDE', 2);
 
  /**
  * Constant of the diff mode cookie in the user's browser
  */
  define('GITPHP_DIFF_MODE_COOKIE', 'GitPHPDiffMode');
 
  /**
  * Diff mode cookie lifetime
  */
  define('GITPHP_DIFF_MODE_COOKIE_LIFETIME', 60*60*24*365); // 1 year
 
  /**
  * DiffBase controller class
  *
  * @package GitPHP
  * @subpackage Controller
  */
  abstract class GitPHP_Controller_DiffBase extends GitPHP_ControllerBase
  {
 
  /**
  * ReadQuery
  *
  * Read query into parameters
  *
  * @access protected
  */
  protected function ReadQuery()
  {
  if (!isset($this->params['plain']) || $this->params['plain'] != true) {
 
  if ($this->DiffMode(isset($_GET['o']) ? $_GET['o'] : '') == GITPHP_DIFF_SIDEBYSIDE) {
  $this->params['sidebyside'] = true;
  }
 
  }
  }
 
  /**
  * DiffMode
  *
  * Determines the diff mode to use
  *
  * @param string $overrideMode mode overridden by the user
  * @access protected
  */
  protected function DiffMode($overrideMode = '')
  {
  $mode = GITPHP_DIFF_UNIFIED; // default
 
  /*
  * Check cookie
  */
  if (!empty($_COOKIE[GITPHP_DIFF_MODE_COOKIE])) {
  $mode = $_COOKIE[GITPHP_DIFF_MODE_COOKIE];
  } else {
  /*
  * Create cookie to prevent browser delay
  */
  setcookie(GITPHP_DIFF_MODE_COOKIE, $mode, time()+GITPHP_DIFF_MODE_COOKIE_LIFETIME);
  }
 
  if (!empty($overrideMode)) {
  /*
  * User is choosing a new mode
  */
  if ($overrideMode == 'sidebyside') {
  $mode = GITPHP_DIFF_SIDEBYSIDE;
  setcookie(GITPHP_DIFF_MODE_COOKIE, GITPHP_DIFF_SIDEBYSIDE, time()+GITPHP_DIFF_MODE_COOKIE_LIFETIME);
  } else if ($overrideMode == 'unified') {
  $mode = GITPHP_DIFF_UNIFIED;
  setcookie(GITPHP_DIFF_MODE_COOKIE, GITPHP_DIFF_UNIFIED, time()+GITPHP_DIFF_MODE_COOKIE_LIFETIME);
  }
  }
 
  return $mode;
  }
 
  /**
  * LoadHeaders
  *
  * Loads headers for this template
  *
  * @access protected
  */
  protected function LoadHeaders()
  {
  if (isset($this->params['plain']) && ($this->params['plain'] === true)) {
  GitPHP_Log::GetInstance()->SetEnabled(false);
  $this->headers[] = 'Content-type: text/plain; charset=UTF-8';
  }
  }
 
  }
 
comments