Use a configuration parser instead of hardcoded configuration variables

develop
Sven Slootweg 12 years ago
parent 2e5590ddeb
commit 52b6216054

@ -13,7 +13,7 @@
require("include.constants.php"); require("include.constants.php");
require("cphp/config.php"); require("include.config.php");
require("include.dependencies.php"); require("include.dependencies.php");
require("include.exceptions.php"); require("include.exceptions.php");
@ -28,11 +28,6 @@ require("include.csrf.php");
require("class.templater.php"); require("class.templater.php");
require("class.localizer.php"); require("class.localizer.php");
$locale = new Localizer();
$locale->Load($cphp_locale_name);
setlocale(LC_ALL, $locale->locale);
if(empty($not_html)) if(empty($not_html))
{ {
header("Content-Type:text/html; charset=UTF-8"); header("Content-Type:text/html; charset=UTF-8");
@ -41,7 +36,7 @@ if(empty($not_html))
require("class.base.php"); require("class.base.php");
require("class.databaserecord.php"); require("class.databaserecord.php");
foreach($cphp_components as $component) foreach($cphp_config->components as $component)
{ {
require("components/component.{$component}.php"); require("components/component.{$component}.php");
} }

@ -31,6 +31,11 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
public function __construct($uDataSource, $defaultable = null) public function __construct($uDataSource, $defaultable = null)
{ {
if(!isset($cphp_config->class_map))
{
die("No class map was specified. Refer to the CPHP manual for instructions.");
}
$this->ConstructDataset($uDataSource, $defaultable); $this->ConstructDataset($uDataSource, $defaultable);
$this->EventConstructed(); $this->EventConstructed();
} }
@ -173,7 +178,7 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
public function BindDataset($type, $dataset, $defaultable) public function BindDataset($type, $dataset, $defaultable)
{ {
global $cphp_class_map; global $cphp_config;
if(is_array($dataset)) if(is_array($dataset))
{ {
@ -191,7 +196,7 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
public function SetField($type, $variable_name, $column_name) public function SetField($type, $variable_name, $column_name)
{ {
global $cphp_class_map; global $cphp_config;
if(!isset($this->uData[$column_name])) if(!isset($this->uData[$column_name]))
{ {
@ -236,7 +241,7 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
break; break;
default: default:
$found = false; $found = false;
foreach($cphp_class_map as $class_type => $class_name) foreach(get_object_vars($cphp_config->class_map) as $class_type => $class_name)
{ {
if($type == $class_type) if($type == $class_type)
{ {
@ -477,13 +482,13 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass
{ {
// Not done yet! // Not done yet!
if(!isset($cphp_class_map[$type])) if(!isset($cphp_config->class_map->$type))
{ {
$classname = get_class($this); $classname = get_class($this);
throw new NotFoundException("Non-existent 'type' argument passed on to {$classname}.RetrieveChildren function."); throw new NotFoundException("Non-existent 'type' argument passed on to {$classname}.RetrieveChildren function.");
} }
$parent_type = get_parent_class($cphp_class_map[$type]); $parent_type = get_parent_class($cphp_config->class_map->$type);
if($parent_type !== "CPHPDatabaseRecordClass") if($parent_type !== "CPHPDatabaseRecordClass")
{ {
$parent_type = ($parent_type === false) ? "NONE" : $parent_type; $parent_type = ($parent_type === false) ? "NONE" : $parent_type;

@ -0,0 +1,26 @@
<?php
/*
* CPHP is more free software. It is licensed under the WTFPL, which
* allows you to do pretty much anything with it, without having to
* ask permission. Commercial use is allowed, and no attribution is
* required. We do politely request that you share your modifications
* to benefit other developers, but you are under no enforced
* obligation to do so :)
*
* Please read the accompanying LICENSE document for the full WTFPL
* licensing text.
*/
if($_CPHP !== true) { die(); }
if(empty($_CPHP_CONFIG))
{
die("No valid CPHP configuration path was specified. Refer to the CPHP manual for instructions.");
}
$cphp_config = json_decode(file_get_contents($_CPHP_CONFIG));
if(json_last_error() != JSON_ERROR_NONE)
{
die("Failed to parse CPHP configuration. Refer to the CPHP manual for instructions.");
}

@ -13,6 +13,15 @@
if($_CPHP !== true) { die(); } if($_CPHP !== true) { die(); }
if(!empty($cphp_config->locale->default_timezone))
{
$user_preferences[CPHP_SETTING_TIMEZONE] = $cphp_config->locale->default_timezone;
}
else
{
die("No default timezone was specified. Refer to the CPHP manual for instructions.");
}
$timezones = array( $timezones = array(
'Pacific/Kwajalein' => '(GMT-12:00) International Date Line West', 'Pacific/Kwajalein' => '(GMT-12:00) International Date Line West',
'Pacific/Midway' => '(GMT-11:00) Midway Island', 'Pacific/Midway' => '(GMT-11:00) Midway Island',

@ -0,0 +1,26 @@
<?php
/*
* CPHP is more free software. It is licensed under the WTFPL, which
* allows you to do pretty much anything with it, without having to
* ask permission. Commercial use is allowed, and no attribution is
* required. We do politely request that you share your modifications
* to benefit other developers, but you are under no enforced
* obligation to do so :)
*
* Please read the accompanying LICENSE document for the full WTFPL
* licensing text.
*/
if($_CPHP !== true) { die(); }
if(!empty($cphp_config->locale->default_locale))
{
$locale = new Localizer();
$locale->Load($cphp_config->locale->default_locale);
setlocale(LC_ALL, $locale->locale);
}
else
{
die("No default locale was specified. Refer to the CPHP manual for instructions.");
}

@ -13,10 +13,10 @@
if($_CPHP !== true) { die(); } if($_CPHP !== true) { die(); }
if($cphp_memcache_enabled) if(!empty($cphp_config->memcache->enabled))
{ {
$cphp_memcache = new Memcache; $cphp_memcache = new Memcache;
$cphp_memcache_established = $cphp_memcache->connect($cphp_memcache_server, $cphp_memcache_port); $cphp_memcache_established = $cphp_memcache->connect($cphp_config->memcache->hostname, $cphp_config->memcache->port);
if($cphp_memcache_established !== false) if($cphp_memcache_established !== false)
{ {
@ -30,9 +30,9 @@ if($cphp_memcache_enabled)
function mc_get($key) function mc_get($key)
{ {
global $cphp_memcache_enabled, $cphp_memcache_connected, $cphp_memcache; global $cphp_config, $cphp_memcache_connected, $cphp_memcache;
if($cphp_memcache_enabled === false || $cphp_memcache_connected === false) if(empty($cphp_config->memcache->enabled) || $cphp_memcache_connected === false)
{ {
return false; return false;
} }
@ -52,15 +52,15 @@ function mc_get($key)
function mc_set($key, $value, $expiry) function mc_set($key, $value, $expiry)
{ {
global $cphp_memcache_enabled, $cphp_memcache_connected, $cphp_memcache_compressed, $cphp_memcache; global $cphp_config, $cphp_memcache_connected, $cphp_memcache;
if($cphp_memcache_enabled === false || $cphp_memcache_connected === false) if(empty($cphp_config->memcache->enabled) || $cphp_memcache_connected === false)
{ {
return false; return false;
} }
else else
{ {
if($cphp_memcache_compressed === true) if(!empty($cphp_config->memcache->compressed) === true)
{ {
$flag = MEMCACHE_COMPRESSED; $flag = MEMCACHE_COMPRESSED;
} }
@ -76,9 +76,9 @@ function mc_set($key, $value, $expiry)
function mc_delete($key) function mc_delete($key)
{ {
global $cphp_memcache_enabled, $cphp_memcache_connected, $cphp_memcache; global $cphp_config, $cphp_memcache_connected, $cphp_memcache;
if($cphp_memcache_enabled === false || $cphp_memcache_connected === false) if(empty($cphp_config->memcache->enabled) || $cphp_memcache_connected === false)
{ {
return false; return false;
} }

@ -15,13 +15,26 @@ if($_CPHP !== true) { die(); }
$cphp_mysql_connected = false; $cphp_mysql_connected = false;
if($cphp_mysql_enabled === true) if(!empty($cphp_config->database->driver))
{ {
if(mysql_connect($cphp_mysql_host, $cphp_mysql_user, $cphp_mysql_pass)) if(empty($cphp_config->database->database))
{ {
if(mysql_select_db($cphp_mysql_db)) die("No database was configured. Refer to the CPHP manual for instructions.");
}
if(mysql_connect($cphp_config->database->hostname, $cphp_config->database->username, $cphp_config->database->password))
{
if(mysql_select_db($cphp_config->database->database))
{ {
$cphp_mysql_connected = true; $cphp_mysql_connected = true;
} }
else
{
die("Could not connect to the specified database. Refer to the CPHP manual for instructions.");
}
}
else
{
die("Could not connect to the specified database server. Refer to the CPHP manual for instructions.");
} }
} }

Loading…
Cancel
Save