Return HTTP 500 on configuration error
Return HTTP 500 on configuration error

<?php <?php
/** /**
* GitPHP Config * GitPHP Config
* *
* Configfile reader class * Configfile reader class
* *
* @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
*/ */
   
/** /**
* Config class * Config class
* *
* @package GitPHP * @package GitPHP
*/ */
class GitPHP_Config class GitPHP_Config
{ {
/** /**
* instance * instance
* *
* Stores the singleton instance * Stores the singleton instance
* *
* @access protected * @access protected
* @static * @static
*/ */
protected static $instance; protected static $instance;
   
/** /**
* values * values
* *
* Stores the config values * Stores the config values
* *
* @access protected * @access protected
*/ */
protected $values = array(); protected $values = array();
   
/** /**
* configs * configs
* *
* Stores the config files * Stores the config files
* *
* @access protected * @access protected
*/ */
protected $configs = array(); protected $configs = array();
   
/** /**
* GetInstance * GetInstance
* *
* Returns the singleton instance * Returns the singleton instance
* *
* @access public * @access public
* @static * @static
* @return mixed instance of config class * @return mixed instance of config class
*/ */
public static function GetInstance() public static function GetInstance()
{ {
if (!self::$instance) { if (!self::$instance) {
self::$instance = new GitPHP_Config(); self::$instance = new GitPHP_Config();
} }
return self::$instance; return self::$instance;
} }
   
/** /**
* LoadConfig * LoadConfig
* *
* Loads a config file * Loads a config file
* *
* @access public * @access public
* @param string $configFile config file to load * @param string $configFile config file to load
* @throws Exception on failure * @throws Exception on failure
*/ */
public function LoadConfig($configFile) public function LoadConfig($configFile)
{ {
// backwards compatibility for people who have been // backwards compatibility for people who have been
// making use of these variables in their title // making use of these variables in their title
global $gitphp_version, $gitphp_appstring; global $gitphp_version, $gitphp_appstring;
   
if (!is_file($configFile)) { if (!is_file($configFile)) {
throw new Exception('Could not load config file ' . $configFile); throw new GitPHP_MessageException('Could not load config file ' . $configFile, true, 500);
} }
   
if (!include($configFile)) { if (!include($configFile)) {
throw new Exception('Could not read config file ' . $configFile); throw new GitPHP_MessageException('Could not read config file ' . $configFile, true, 500);
} }
   
if (isset($gitphp_conf) && is_array($gitphp_conf)) if (isset($gitphp_conf) && is_array($gitphp_conf))
$this->values = array_merge($this->values, $gitphp_conf); $this->values = array_merge($this->values, $gitphp_conf);
   
$this->configs[] = $configFile; $this->configs[] = $configFile;
} }
   
/** /**
* ClearConfig * ClearConfig
* *
* Clears all config values * Clears all config values
* *
* @access public * @access public
*/ */
public function ClearConfig() public function ClearConfig()
{ {
$this->values = array(); $this->values = array();
$this->configs = array(); $this->configs = array();
} }
   
/** /**
* GetValue * GetValue
* *
* Gets a config value * Gets a config value
* *
* @access public * @access public
* @param $key config key to fetch * @param $key config key to fetch
* @param $default default config value to return * @param $default default config value to return
* @return mixed config value * @return mixed config value
*/ */
public function GetValue($key, $default = null) public function GetValue($key, $default = null)
{ {
if ($this->HasKey($key)) { if ($this->HasKey($key)) {
return $this->values[$key]; return $this->values[$key];
} }
return $default; return $default;
} }
   
/** /**
* SetValue * SetValue
* *
* Sets a config value * Sets a config value
* *
* @access public * @access public
* @param string $key config key to set * @param string $key config key to set
* @param mixed $value value to set * @param mixed $value value to set
*/ */
public function SetValue($key, $value) public function SetValue($key, $value)
{ {
if (empty($key)) { if (empty($key)) {
return; return;
} }
if (empty($value)) { if (empty($value)) {
unset($this->values[$key]); unset($this->values[$key]);
return; return;
} }
$this->values[$key] = $value; $this->values[$key] = $value;
} }
   
/** /**
* HasKey * HasKey
* *
* Tests if the config has specified this key * Tests if the config has specified this key
* *
* @access public * @access public
* @param string $key config key to find * @param string $key config key to find
* @return boolean true if key exists * @return boolean true if key exists
*/ */
public function HasKey($key) public function HasKey($key)
{ {
if (empty($key)) { if (empty($key)) {
return false; return false;
} }
return isset($this->values[$key]); return isset($this->values[$key]);
} }
   
} }
   
<?php <?php
/** /**
* GitPHP Controller Message * GitPHP Controller Message
* *
* Controller for displaying a message page * Controller for displaying a message page
* *
* @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
*/ */
   
/** /**
* Message controller class * Message controller class
* *
* @package GitPHP * @package GitPHP
* @subpackage Controller * @subpackage Controller
*/ */
class GitPHP_Controller_Message extends GitPHP_ControllerBase class GitPHP_Controller_Message extends GitPHP_ControllerBase
{ {
/** /**
* __construct * __construct
* *
* Constructor * Constructor
* *
* @access public * @access public
* @return controller * @return controller
*/ */
public function __construct() public function __construct()
{ {
try { try {
parent::__construct(); parent::__construct();
} catch (Exception $e) { } catch (Exception $e) {
} }
} }
   
/** /**
* 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()
{ {
return 'message.tpl'; return 'message.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 sha1($this->params['message']) . '|' . ($this->params['error'] ? '1' : '0');; return sha1($this->params['message']) . '|' . ($this->params['error'] ? '1' : '0');;
} }
   
/** /**
* 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)
{ {
// This isn't a real controller // This isn't a real controller
return ''; return '';
} }
   
/** /**
* ReadQuery * ReadQuery
* *
* Read query into parameters * Read query into parameters
* *
* @access protected * @access protected
*/ */
protected function ReadQuery() protected function ReadQuery()
{ {
} }
   
/** /**
* LoadHeaders * LoadHeaders
* *
* Loads headers for this template * Loads headers for this template
* *
* @access protected * @access protected
*/ */
protected function LoadHeaders() protected function LoadHeaders()
{ {
if (isset($this->params['statuscode']) && !empty($this->params['statuscode'])) { if (isset($this->params['statuscode']) && !empty($this->params['statuscode'])) {
$partialHeader = $this->StatusCodeHeader($this->params['statuscode']); $partialHeader = $this->StatusCodeHeader($this->params['statuscode']);
if (!empty($partialHeader)) { if (!empty($partialHeader)) {
if (substr(php_sapi_name(), 0, 8) == 'cgi-fcgi') { if (substr(php_sapi_name(), 0, 8) == 'cgi-fcgi') {
/* /*
* FastCGI requires a different header * FastCGI requires a different header
*/ */
$this->headers[] = 'Status: ' . $partialHeader; $this->headers[] = 'Status: ' . $partialHeader;
} else { } else {
$this->headers[] = 'HTTP/1.1 ' . $partialHeader; $this->headers[] = 'HTTP/1.1 ' . $partialHeader;
} }
} }
} }
} }
   
/** /**
* LoadData * LoadData
* *
* Loads data for this template * Loads data for this template
* *
* @access protected * @access protected
*/ */
protected function LoadData() protected function LoadData()
{ {
$this->tpl->assign('message', $this->params['message']); $this->tpl->assign('message', $this->params['message']);
if (isset($this->params['error']) && ($this->params['error'])) { if (isset($this->params['error']) && ($this->params['error'])) {
$this->tpl->assign('error', true); $this->tpl->assign('error', true);
} }
} }
   
/** /**
* StatusCodeHeader * StatusCodeHeader
* *
* Gets the header for an HTTP status code * Gets the header for an HTTP status code
* *
* @access private * @access private
* @param integer $code status code * @param integer $code status code
* @return string header * @return string header
*/ */
private function StatusCodeHeader($code) private function StatusCodeHeader($code)
{ {
switch ($code) { switch ($code) {
  case 500:
  return '500 Internal Server Error';
} }
} }
   
} }
   
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
*/ */
   
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_GITOBJECTDIR', GITPHP_INCLUDEDIR . 'git/');
define('GITPHP_CONTROLLERDIR', GITPHP_INCLUDEDIR . 'controller/'); define('GITPHP_CONTROLLERDIR', GITPHP_INCLUDEDIR . 'controller/');
define('GITPHP_LOCALEDIR', GITPHP_BASEDIR . 'locale/'); define('GITPHP_LOCALEDIR', GITPHP_BASEDIR . 'locale/');
   
include_once(GITPHP_INCLUDEDIR . 'version.php'); include_once(GITPHP_INCLUDEDIR . 'version.php');
   
require_once(GITPHP_INCLUDEDIR . 'Config.class.php'); require_once(GITPHP_INCLUDEDIR . 'Config.class.php');
   
require_once(GITPHP_INCLUDEDIR . 'Resource.class.php'); require_once(GITPHP_INCLUDEDIR . 'Resource.class.php');
   
require_once(GITPHP_INCLUDEDIR . 'Log.class.php'); require_once(GITPHP_INCLUDEDIR . 'Log.class.php');
   
require_once(GITPHP_GITOBJECTDIR . 'ProjectList.class.php'); require_once(GITPHP_GITOBJECTDIR . 'ProjectList.class.php');
   
require_once(GITPHP_INCLUDEDIR . 'MessageException.class.php'); require_once(GITPHP_INCLUDEDIR . 'MessageException.class.php');
   
require_once(GITPHP_CONTROLLERDIR . 'Controller.class.php'); require_once(GITPHP_CONTROLLERDIR . 'Controller.class.php');
   
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_LOCALE_COOKIE])) || empty($_COOKIE[GITPHP_LOCALE_COOKIE])) { if ((!isset($_COOKIE[GITPHP_LOCALE_COOKIE])) || empty($_COOKIE[GITPHP_LOCALE_COOKIE])) {
   
/* /*
* 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_LOCALE_COOKIE, $preferredLocale); setcookie(GITPHP_LOCALE_COOKIE, $preferredLocale);
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_LOCALE_COOKIE, 0); setcookie(GITPHP_LOCALE_COOKIE, 0);
} }
   
} else if (isset($_GET['l']) && !empty($_GET['l'])) { } else if (isset($_GET['l']) && !empty($_GET['l'])) {
   
/* /*
* User picked something * User picked something
*/ */
setcookie(GITPHP_LOCALE_COOKIE, $_GET['l']); setcookie(GITPHP_LOCALE_COOKIE, $_GET['l']);
GitPHP_Resource::Instantiate($_GET['l']); GitPHP_Resource::Instantiate($_GET['l']);
   
} else if (isset($_COOKIE[GITPHP_LOCALE_COOKIE]) && !empty($_COOKIE[GITPHP_LOCALE_COOKIE])) { } else if (isset($_COOKIE[GITPHP_LOCALE_COOKIE]) && !empty($_COOKIE[GITPHP_LOCALE_COOKIE])) {
   
/* /*
* Returning visitor with a preference * Returning visitor with a preference
*/ */
GitPHP_Resource::Instantiate($_COOKIE[GITPHP_LOCALE_COOKIE]); GitPHP_Resource::Instantiate($_COOKIE[GITPHP_LOCALE_COOKIE]);
   
} }
   
   
try { try {
   
// Define these here because these get used in the config file // Define these here because these get used in the config file
define('GITPHP_COMPRESS_BZ2', 1); define('GITPHP_COMPRESS_BZ2', 1);
define('GITPHP_COMPRESS_GZ', 2); define('GITPHP_COMPRESS_GZ', 2);
define('GITPHP_COMPRESS_ZIP', 3); define('GITPHP_COMPRESS_ZIP', 3);
   
/* /*
* 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_Log::GetInstance()->GetEnabled()) { if (GitPHP_Log::GetInstance()->GetEnabled()) {
GitPHP_Log::GetInstance()->SetStartTime(GITPHP_START_TIME); GitPHP_Log::GetInstance()->SetStartTime(GITPHP_START_TIME);
GitPHP_Log::GetInstance()->SetStartMemory(GITPHP_START_MEM); GitPHP_Log::GetInstance()->SetStartMemory(GITPHP_START_MEM);
} }
   
if (!GitPHP_Config::GetInstance()->GetValue('projectroot', null)) { if (!GitPHP_Config::GetInstance()->GetValue('projectroot', null)) {
throw new GitPHP_MessageException(__('A projectroot must be set in the config'), true); throw new GitPHP_MessageException(__('A projectroot must be set in the config'), 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();
} }
   
} 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');
} }
   
require_once(GITPHP_CONTROLLERDIR . 'Controller_Message.class.php'); require_once(GITPHP_CONTROLLERDIR . 'Controller_Message.class.php');
$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();
   
} }
   
if (GitPHP_Log::GetInstance()->GetEnabled()) { if (GitPHP_Log::GetInstance()->GetEnabled()) {
$entries = GitPHP_Log::GetInstance()->GetEntries(); $entries = GitPHP_Log::GetInstance()->GetEntries();
foreach ($entries as $logline) { foreach ($entries as $logline) {
echo "\n" . $logline; echo "\n" . $logline;
} }
} }
   
?> ?>
   
comments