Remove some unused constants
Remove some unused constants

<?php <?php
/** /**
* GitPHP Controller Snapshot * GitPHP Controller Snapshot
* *
* Controller for getting a snapshot * Controller for getting a snapshot
* *
* @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
*/ */
   
/** /**
* Snapshot controller class * Snapshot controller class
* *
* @package GitPHP * @package GitPHP
* @subpackage Controller * @subpackage Controller
*/ */
class GitPHP_Controller_Snapshot extends GitPHP_ControllerBase class GitPHP_Controller_Snapshot extends GitPHP_ControllerBase
{ {
   
/** /**
* archive * archive
* *
* Stores the archive object * Stores the archive object
* *
* @access private * @access private
*/ */
private $archive = null; private $archive = null;
   
/** /**
* __construct * __construct
* *
* Constructor * Constructor
* *
* @access public * @access public
* @return controller * @return controller
*/ */
public function __construct() public function __construct()
{ {
$this->config = GitPHP_Config::GetInstance(); $this->config = GitPHP_Config::GetInstance();
$this->projectList = GitPHP_ProjectList::GetInstance(); $this->projectList = GitPHP_ProjectList::GetInstance();
   
if (isset($_GET['p'])) { if (isset($_GET['p'])) {
$project = $this->projectList->GetProject(str_replace(chr(0), '', $_GET['p'])); $project = $this->projectList->GetProject(str_replace(chr(0), '', $_GET['p']));
if (!$project) { if (!$project) {
throw new GitPHP_MessageException(sprintf(__('Invalid project %1$s'), $_GET['p']), true); throw new GitPHP_MessageException(sprintf(__('Invalid project %1$s'), $_GET['p']), true);
} }
$this->project = $project->GetProject(); $this->project = $project->GetProject();
} }
   
if (!$this->project) { if (!$this->project) {
throw new GitPHP_MessageException(__('Project is required'), true); throw new GitPHP_MessageException(__('Project is required'), true);
} }
   
$this->preserveWhitespace = true; $this->preserveWhitespace = true;
   
$this->ReadQuery(); $this->ReadQuery();
} }
   
/** /**
* 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()
{ {
} }
   
/** /**
* 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['hash']) ? $this->params['hash'] : '') . '|' . (isset($this->params['path']) ? $this->params['path'] : '') . '|' . (isset($this->params['prefix']) ? $this->params['prefix'] : '') . '|' . $this->params['format']; return (isset($this->params['hash']) ? $this->params['hash'] : '') . '|' . (isset($this->params['path']) ? $this->params['path'] : '') . '|' . (isset($this->params['prefix']) ? $this->params['prefix'] : '') . '|' . $this->params['format'];
} }
   
/** /**
* 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 __('snapshot'); return __('snapshot');
} }
return 'snapshot'; return 'snapshot';
} }
   
/** /**
* ReadQuery * ReadQuery
* *
* Read query into parameters * Read query into parameters
* *
* @access protected * @access protected
*/ */
protected function ReadQuery() protected function ReadQuery()
{ {
if (isset($_GET['h'])) $this->params['hash'] = $_GET['h']; if (isset($_GET['h'])) $this->params['hash'] = $_GET['h'];
if (isset($_GET['f'])) $this->params['path'] = $_GET['f']; if (isset($_GET['f'])) $this->params['path'] = $_GET['f'];
if (isset($_GET['prefix'])) $this->params['prefix'] = $_GET['prefix']; if (isset($_GET['prefix'])) $this->params['prefix'] = $_GET['prefix'];
if (isset($_GET['fmt'])) if (isset($_GET['fmt']))
$this->params['format'] = $_GET['fmt']; $this->params['format'] = $_GET['fmt'];
else else
$this->params['format'] = $this->config->GetValue('compressformat', GITPHP_COMPRESS_ZIP); $this->params['format'] = $this->config->GetValue('compressformat', GITPHP_COMPRESS_ZIP);
GitPHP_DebugLog::GetInstance()->SetEnabled(false); GitPHP_DebugLog::GetInstance()->SetEnabled(false);
} }
   
/** /**
* LoadHeaders * LoadHeaders
* *
* Loads headers for this template * Loads headers for this template
* *
* @access protected * @access protected
*/ */
protected function LoadHeaders() protected function LoadHeaders()
{ {
$this->archive = new GitPHP_Archive($this->GetProject(), null, $this->params['format'], (isset($this->params['path']) ? $this->params['path'] : ''), (isset($this->params['prefix']) ? $this->params['prefix'] : '')); $this->archive = new GitPHP_Archive($this->GetProject(), null, $this->params['format'], (isset($this->params['path']) ? $this->params['path'] : ''), (isset($this->params['prefix']) ? $this->params['prefix'] : ''));
   
$commit = null; $commit = null;
   
if (!isset($this->params['hash'])) if (!isset($this->params['hash']))
$commit = $this->GetProject()->GetHeadCommit(); $commit = $this->GetProject()->GetHeadCommit();
else else
$commit = $this->GetProject()->GetCommit($this->params['hash']); $commit = $this->GetProject()->GetCommit($this->params['hash']);
   
$this->archive->SetObject($commit); $this->archive->SetObject($commit);
   
switch ($this->archive->GetFormat()) { switch ($this->archive->GetFormat()) {
case GITPHP_COMPRESS_TAR: case GITPHP_COMPRESS_TAR:
$this->headers[] = 'Content-Type: application/x-tar'; $this->headers[] = 'Content-Type: application/x-tar';
break; break;
case GITPHP_COMPRESS_BZ2: case GITPHP_COMPRESS_BZ2:
$this->headers[] = 'Content-Type: application/x-bzip2'; $this->headers[] = 'Content-Type: application/x-bzip2';
break; break;
case GITPHP_COMPRESS_GZ: case GITPHP_COMPRESS_GZ:
$this->headers[] = 'Content-Type: application/x-gzip'; $this->headers[] = 'Content-Type: application/x-gzip';
break; break;
case GITPHP_COMPRESS_ZIP: case GITPHP_COMPRESS_ZIP:
$this->headers[] = 'Content-Type: application/x-zip'; $this->headers[] = 'Content-Type: application/x-zip';
break; break;
default: default:
throw new Exception('Unknown compression type'); throw new Exception('Unknown compression type');
} }
   
$this->headers[] = 'Content-Disposition: attachment; filename=' . $this->archive->GetFilename(); $this->headers[] = 'Content-Disposition: attachment; filename=' . $this->archive->GetFilename();
} }
   
/** /**
* LoadData * LoadData
* *
* Loads data for this template * Loads data for this template
* *
* @access protected * @access protected
*/ */
protected function LoadData() protected function LoadData()
{ {
} }
   
/** /**
* Render * Render
* *
* Render this controller * Render this controller
* *
* @access public * @access public
*/ */
public function Render() public function Render()
{ {
$this->LoadData(); $this->LoadData();
   
$cache = $this->config->GetValue('cache', false); $cache = $this->config->GetValue('cache', false);
$cachehandle = false; $cachehandle = false;
$cachefile = ''; $cachefile = '';
if ($cache && is_dir(GITPHP_CACHE)) { if ($cache && is_dir(GITPHP_CACHEDIR)) {
$key = ($this->archive->GetObject() ? $this->archive->GetObject()->GetHash() : '') . '|' . (isset($this->params['path']) ? $this->params['path'] : '') . '|' . (isset($this->params['prefix']) ? $this->params['prefix'] : ''); $key = ($this->archive->GetObject() ? $this->archive->GetObject()->GetHash() : '') . '|' . (isset($this->params['path']) ? $this->params['path'] : '') . '|' . (isset($this->params['prefix']) ? $this->params['prefix'] : '');
$cachefile = sha1($key) . '-' . $this->archive->GetFilename(); $cachefile = sha1($key) . '-' . $this->archive->GetFilename();
$cachedfilepath = GITPHP_CACHE . $cachefile; $cachedfilepath = GITPHP_CACHEDIR . $cachefile;
   
if (file_exists($cachedfilepath)) { if (file_exists($cachedfilepath)) {
// read cached file // read cached file
$cachehandle = fopen($cachedfilepath, 'rb'); $cachehandle = fopen($cachedfilepath, 'rb');
if ($cachehandle) { if ($cachehandle) {
while (!feof($cachehandle)) { while (!feof($cachehandle)) {
print fread($cachehandle, 1048576); print fread($cachehandle, 1048576);
flush(); flush();
} }
fclose($cachehandle); fclose($cachehandle);
return; return;
} }
} }
} }
   
if ($this->archive->Open()) { if ($this->archive->Open()) {
   
$tmpcachefile = ''; $tmpcachefile = '';
   
if ($cache && !empty($cachefile)) { if ($cache && !empty($cachefile)) {
// write cached file too // write cached file too
$pid = 0; $pid = 0;
if (function_exists('posix_getpid')) if (function_exists('posix_getpid'))
$pid = posix_getpid(); $pid = posix_getpid();
else else
$pid = rand(); $pid = rand();
   
$tmpcachefile = 'tmp-' . $pid . '-' . $cachefile; $tmpcachefile = 'tmp-' . $pid . '-' . $cachefile;
$cachehandle = fopen(GITPHP_CACHE . $tmpcachefile, 'wb'); $cachehandle = fopen(GITPHP_CACHEDIR . $tmpcachefile, 'wb');
} }
   
while (($data = $this->archive->Read()) !== false) { while (($data = $this->archive->Read()) !== false) {
   
print $data; print $data;
flush(); flush();
   
if ($cache && $cachehandle) { if ($cache && $cachehandle) {
fwrite($cachehandle, $data); fwrite($cachehandle, $data);
} }
   
} }
$this->archive->Close(); $this->archive->Close();
   
if ($cachehandle) { if ($cachehandle) {
fclose($cachehandle); fclose($cachehandle);
sleep(1); sleep(1);
rename(GITPHP_CACHE . $tmpcachefile, GITPHP_CACHE . $cachefile); rename(GITPHP_CACHEDIR . $tmpcachefile, GITPHP_CACHEDIR . $cachefile);
} }
} }
} }
   
} }
   
file:a/index.php -> file:b/index.php
<?php <?php
/** /**
* GitPHP * GitPHP
* *
* Index * Index
* *
* @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
*/ */
   
/** /**
* Use utf-8 encoding * Use utf-8 encoding
*/ */
if (function_exists('mb_internal_encoding')) { if (function_exists('mb_internal_encoding')) {
mb_internal_encoding("UTF-8"); mb_internal_encoding("UTF-8");
} }
   
/** /**
* Define start time / memory for benchmarking * Define start time / memory for benchmarking
*/ */
define('GITPHP_START_TIME', microtime(true)); define('GITPHP_START_TIME', microtime(true));
define('GITPHP_START_MEM', memory_get_usage()); define('GITPHP_START_MEM', memory_get_usage());
   
/** /**
* Define some paths * Define some paths
*/ */
define('GITPHP_BASEDIR', dirname(__FILE__) . '/'); define('GITPHP_BASEDIR', dirname(__FILE__) . '/');
define('GITPHP_CONFIGDIR', GITPHP_BASEDIR . 'config/'); define('GITPHP_CONFIGDIR', GITPHP_BASEDIR . 'config/');
define('GITPHP_INCLUDEDIR', GITPHP_BASEDIR . 'include/'); define('GITPHP_INCLUDEDIR', GITPHP_BASEDIR . 'include/');
define('GITPHP_GITOBJECTDIR', GITPHP_INCLUDEDIR . 'git/');  
define('GITPHP_CONTROLLERDIR', GITPHP_INCLUDEDIR . 'controller/');  
define('GITPHP_CACHEDIR', GITPHP_INCLUDEDIR . 'cache/');  
define('GITPHP_LOCALEDIR', GITPHP_BASEDIR . 'locale/'); define('GITPHP_LOCALEDIR', GITPHP_BASEDIR . 'locale/');
   
define('GITPHP_CACHE', GITPHP_BASEDIR . 'cache/'); define('GITPHP_CACHEDIR', GITPHP_BASEDIR . 'cache/');
   
include(GITPHP_INCLUDEDIR . 'version.php'); include(GITPHP_INCLUDEDIR . 'version.php');
   
require(GITPHP_INCLUDEDIR . 'AutoLoader.class.php'); require(GITPHP_INCLUDEDIR . 'AutoLoader.class.php');
   
spl_autoload_register(array('GitPHP_AutoLoader', 'AutoLoad')); spl_autoload_register(array('GitPHP_AutoLoader', 'AutoLoad'));
   
date_default_timezone_set('UTC'); date_default_timezone_set('UTC');
   
   
/* /*
* Set the locale based on the user's preference * Set the locale based on the user's preference
*/ */
if ((!isset($_COOKIE[GitPHP_Resource::LocaleCookie])) || empty($_COOKIE[GitPHP_Resource::LocaleCookie])) { if ((!isset($_COOKIE[GitPHP_Resource::LocaleCookie])) || empty($_COOKIE[GitPHP_Resource::LocaleCookie])) {
   
/* /*
* User's first time here, try by HTTP_ACCEPT_LANGUAGE * User's first time here, try by HTTP_ACCEPT_LANGUAGE
*/ */
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$httpAcceptLang = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); $httpAcceptLang = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$preferredLocale = GitPHP_Resource::FindPreferredLocale($_SERVER['HTTP_ACCEPT_LANGUAGE']); $preferredLocale = GitPHP_Resource::FindPreferredLocale($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (!empty($preferredLocale)) { if (!empty($preferredLocale)) {
setcookie(GitPHP_Resource::LocaleCookie, $preferredLocale, time()+GitPHP_Resource::LocaleCookieLifetime); setcookie(GitPHP_Resource::LocaleCookie, $preferredLocale, time()+GitPHP_Resource::LocaleCookieLifetime);
GitPHP_Resource::Instantiate($preferredLocale); GitPHP_Resource::Instantiate($preferredLocale);
} }
} }
   
if (!GitPHP_Resource::Instantiated()) { if (!GitPHP_Resource::Instantiated()) {
/* /*
* Create a dummy cookie to prevent browser delay * Create a dummy cookie to prevent browser delay
*/ */
setcookie(GitPHP_Resource::LocaleCookie, 0, time()+GitPHP_Resource::LocaleCookieLifetime); setcookie(GitPHP_Resource::LocaleCookie, 0, time()+GitPHP_Resource::LocaleCookieLifetime);
} }
   
} else if (isset($_GET['l']) && !empty($_GET['l'])) { } else if (isset($_GET['l']) && !empty($_GET['l'])) {
   
/* /*
* User picked something * User picked something
*/ */
setcookie(GitPHP_Resource::LocaleCookie, $_GET['l'], time()+GitPHP_Resource::LocaleCookieLifetime); setcookie(GitPHP_Resource::LocaleCookie, $_GET['l'], time()+GitPHP_Resource::LocaleCookieLifetime);
GitPHP_Resource::Instantiate($_GET['l']); GitPHP_Resource::Instantiate($_GET['l']);
   
} else if (isset($_COOKIE[GitPHP_Resource::LocaleCookie]) && !empty($_COOKIE[GitPHP_Resource::LocaleCookie])) { } else if (isset($_COOKIE[GitPHP_Resource::LocaleCookie]) && !empty($_COOKIE[GitPHP_Resource::LocaleCookie])) {
   
/* /*
* Returning visitor with a preference * Returning visitor with a preference
*/ */
GitPHP_Resource::Instantiate($_COOKIE[GitPHP_Resource::LocaleCookie]); GitPHP_Resource::Instantiate($_COOKIE[GitPHP_Resource::LocaleCookie]);
   
} }
   
   
try { try {
   
/* /*
* Configuration * Configuration
*/ */
GitPHP_Config::GetInstance()->LoadConfig(GITPHP_CONFIGDIR . 'gitphp.conf.php'); GitPHP_Config::GetInstance()->LoadConfig(GITPHP_CONFIGDIR . 'gitphp.conf.php');
   
/* /*
* Use the default language in the config if user has no preference * Use the default language in the config if user has no preference
* with en_US as the fallback * with en_US as the fallback
*/ */
if (!GitPHP_Resource::Instantiated()) { if (!GitPHP_Resource::Instantiated()) {
GitPHP_Resource::Instantiate(GitPHP_Config::GetInstance()->GetValue('locale', 'en_US')); GitPHP_Resource::Instantiate(GitPHP_Config::GetInstance()->GetValue('locale', 'en_US'));
} }
   
/* /*
* Debug * Debug
*/ */
if (GitPHP_DebugLog::GetInstance()->GetEnabled()) { if (GitPHP_DebugLog::GetInstance()->GetEnabled()) {
GitPHP_DebugLog::GetInstance()->SetStartTime(GITPHP_START_TIME); GitPHP_DebugLog::GetInstance()->SetStartTime(GITPHP_START_TIME);
GitPHP_DebugLog::GetInstance()->SetStartMemory(GITPHP_START_MEM); GitPHP_DebugLog::GetInstance()->SetStartMemory(GITPHP_START_MEM);
} }
   
/* /*
* Check for required executables * Check for required executables
*/ */
if (!GitPHP_GitExe::GetInstance()->Valid()) { if (!GitPHP_GitExe::GetInstance()->Valid()) {
throw new GitPHP_MessageException(sprintf(__('Could not run the git executable "%1$s". You may need to set the "%2$s" config value.'), GitPHP_GitExe::GetInstance()->GetBinary(), 'gitbin'), true, 500); throw new GitPHP_MessageException(sprintf(__('Could not run the git executable "%1$s". You may need to set the "%2$s" config value.'), GitPHP_GitExe::GetInstance()->GetBinary(), 'gitbin'), true, 500);
} }
   
/* /*
* Project list * Project list
*/ */
if (file_exists(GITPHP_CONFIGDIR . 'projects.conf.php')) { if (file_exists(GITPHP_CONFIGDIR . 'projects.conf.php')) {
GitPHP_ProjectList::Instantiate(GITPHP_CONFIGDIR . 'projects.conf.php', false); GitPHP_ProjectList::Instantiate(GITPHP_CONFIGDIR . 'projects.conf.php', false);
} else { } else {
GitPHP_ProjectList::Instantiate(GITPHP_CONFIGDIR . 'gitphp.conf.php', true); GitPHP_ProjectList::Instantiate(GITPHP_CONFIGDIR . 'gitphp.conf.php', true);
} }
   
$controller = GitPHP_Controller::GetController((isset($_GET['a']) ? $_GET['a'] : null)); $controller = GitPHP_Controller::GetController((isset($_GET['a']) ? $_GET['a'] : null));
if ($controller) { if ($controller) {
$controller->RenderHeaders(); $controller->RenderHeaders();
$controller->Render(); $controller->Render();
} }
unset($controller); unset($controller);
   
} catch (Exception $e) { } catch (Exception $e) {
   
if (GitPHP_Config::GetInstance()->GetValue('debug', false)) { if (GitPHP_Config::GetInstance()->GetValue('debug', false)) {
throw $e; throw $e;
} }
   
if (!GitPHP_Resource::Instantiated()) { if (!GitPHP_Resource::Instantiated()) {
/* /*
* In case an error was thrown before instantiating * In case an error was thrown before instantiating
* the resource manager * the resource manager
*/ */
GitPHP_Resource::Instantiate('en_US'); GitPHP_Resource::Instantiate('en_US');
} }
   
$controller = new GitPHP_Controller_Message(); $controller = new GitPHP_Controller_Message();
$controller->SetParam('message', $e->getMessage()); $controller->SetParam('message', $e->getMessage());
if ($e instanceof GitPHP_MessageException) { if ($e instanceof GitPHP_MessageException) {
$controller->SetParam('error', $e->Error); $controller->SetParam('error', $e->Error);
$controller->SetParam('statuscode', $e->StatusCode); $controller->SetParam('statuscode', $e->StatusCode);
} else { } else {
$controller->SetParam('error', true); $controller->SetParam('error', true);
} }
$controller->RenderHeaders(); $controller->RenderHeaders();
$controller->Render(); $controller->Render();
   
unset($controller); unset($controller);
   
} }
   
GitPHP_DebugLog::GetInstance()->Log('MemoryCache count: ' . GitPHP_MemoryCache::GetInstance()->GetCount()); GitPHP_DebugLog::GetInstance()->Log('MemoryCache count: ' . GitPHP_MemoryCache::GetInstance()->GetCount());
   
GitPHP_ProjectList::DestroyInstance(); GitPHP_ProjectList::DestroyInstance();
GitPHP_MemoryCache::DestroyInstance(); GitPHP_MemoryCache::DestroyInstance();
GitPHP_Resource::DestroyInstance(); GitPHP_Resource::DestroyInstance();
GitPHP_Config::DestroyInstance(); GitPHP_Config::DestroyInstance();
GitPHP_GitExe::DestroyInstance(); GitPHP_GitExe::DestroyInstance();
   
if (GitPHP_DebugLog::GetInstance()->GetEnabled()) { if (GitPHP_DebugLog::GetInstance()->GetEnabled()) {
$entries = GitPHP_DebugLog::GetInstance()->GetEntries(); $entries = GitPHP_DebugLog::GetInstance()->GetEntries();
foreach ($entries as $logline) { foreach ($entries as $logline) {
echo "<br />\n" . htmlspecialchars($logline, ENT_QUOTES, 'UTF-8', true); echo "<br />\n" . htmlspecialchars($logline, ENT_QUOTES, 'UTF-8', true);
} }
unset($logline); unset($logline);
unset($entries); unset($entries);
} }
   
GitPHP_DebugLog::DestroyInstance(); GitPHP_DebugLog::DestroyInstance();
   
?> ?>
   
comments