Use a function to load system configs

This commit is contained in:
Dmitriy Simushev 2014-07-02 09:26:55 +00:00
parent dfeadec80b
commit ef958483bb
4 changed files with 64 additions and 20 deletions

View File

@ -30,3 +30,39 @@ function read_config_file($file)
return parse_ini_file($file, true); return parse_ini_file($file, true);
} }
/**
* Loads system configurations.
*
* The configs are cached inside the function.
*
* @return array Associative array of system configs.
*/
function load_system_configs()
{
static $configs = null;
if (is_null($configs)) {
// Load and "parse" configs file. While configs are written in a php
// file include is the only option to load and parse them.
include(MIBEW_FS_ROOT . "/libs/config.php");
$configs = array(
'mibew_root' => $mibewroot,
'database' => array(
'host' => $mysqlhost,
'db' => $mysqldb,
'login' => $mysqllogin,
'pass' => $mysqlpass,
'tables_prefix' => $mysqlprefix,
'use_persistent_connection' => $use_persistent_connection,
),
'mailbox' => $mibew_mailbox,
'home_locale' => $home_locale,
'default_locale' => $default_locale,
'plugins' => $plugins_list,
);
}
return $configs;
}

View File

@ -34,15 +34,20 @@ define('FEATURES_VERSION', '2.0');
* Prefix for session variables. * Prefix for session variables.
* Provide an ability to instal several mibew instances on one server. * Provide an ability to instal several mibew instances on one server.
*/ */
define('SESSION_PREFIX', md5($mysqlhost . '##' . $mysqldb . '##' . $mysqlprefix) . '_'); define('SESSION_PREFIX', md5(
$configs['database']['host'] . '##'
. $configs['database']['db']. '##'
. $configs['database']['tables_prefix']
) . '_');
/** /**
* Default value for cron security key. * Default value for cron security key.
* Another value can be set at operator/settings page. * Another value can be set at operator/settings page.
*/ */
define('DEFAULT_CRON_KEY', md5( define('DEFAULT_CRON_KEY', md5(
$mysqlhost . '##' . $mysqldb . '##' . $mysqllogin . '##' $configs['database']['host'] . '##' . $configs['database']['db'] . '##'
. $mysqlpass . '##' . $mysqlprefix . '##' . $configs['database']['login'] . '##' . $configs['database']['pass'] . '##'
. $configs['database']['tables_prefix'] . '##'
)); ));
/** /**
@ -59,4 +64,4 @@ define('USERNAME_COOKIE_NAME', 'MIBEW_Data');
/** /**
* Mailbox of the current installation * Mailbox of the current installation
*/ */
define('MIBEW_MAILBOX', $mibew_mailbox); define('MIBEW_MAILBOX', $configs['mailbox']);

View File

@ -16,7 +16,6 @@
*/ */
use Mibew\Database; use Mibew\Database;
use Mibew\Plugin\Manager as PluginManager;
use Symfony\Component\Translation\Loader\PoFileLoader; use Symfony\Component\Translation\Loader\PoFileLoader;
/** /**
@ -32,7 +31,9 @@ define('LOCALE_COOKIE_NAME', 'mibew_locale');
*/ */
define( define(
'DEFAULT_LOCALE', 'DEFAULT_LOCALE',
locale_pattern_check($default_locale) && locale_exists($default_locale) ? $default_locale : 'en' (locale_pattern_check($configs['default_locale']) && locale_exists($configs['default_locale'])
? $configs['default_locale']
: 'en')
); );
/** /**
@ -41,7 +42,9 @@ define(
*/ */
define( define(
'HOME_LOCALE', 'HOME_LOCALE',
locale_pattern_check($home_locale) && locale_exists($home_locale) ? $home_locale : 'en' (locale_pattern_check($configs['home_locale']) && locale_exists($configs['home_locale'])
? $configs['home_locale']
: 'en')
); );
/** /**

View File

@ -20,15 +20,16 @@
*/ */
define('MIBEW_FS_ROOT', dirname(dirname(__FILE__))); define('MIBEW_FS_ROOT', dirname(dirname(__FILE__)));
// Include configuration file // Load system configurations
require_once(MIBEW_FS_ROOT . '/libs/config.php'); require_once(MIBEW_FS_ROOT . '/libs/common/configurations.php');
$configs = load_system_configs();
// Sanitize path to application and remove extra slashes // Sanitize path to application and remove extra slashes
$mibewroot = join( $mibewroot = join(
"/", "/",
array_map( array_map(
"rawurlencode", "rawurlencode",
preg_split('/\//', preg_replace('/\/+$/', '', preg_replace('/\/{2,}/', '/', '/' . $mibewroot))) preg_split('/\//', preg_replace('/\/+$/', '', preg_replace('/\/{2,}/', '/', '/' . $configs['mibew_root'])))
) )
); );
@ -51,7 +52,6 @@ Mibew\Autoloader::register(MIBEW_FS_ROOT . '/plugins');
require_once(MIBEW_FS_ROOT . '/vendor/autoload.php'); require_once(MIBEW_FS_ROOT . '/vendor/autoload.php');
// Include common libs // Include common libs
require_once(MIBEW_FS_ROOT . '/libs/common/configurations.php');
require_once(MIBEW_FS_ROOT . '/libs/common/verification.php'); require_once(MIBEW_FS_ROOT . '/libs/common/verification.php');
require_once(MIBEW_FS_ROOT . '/libs/common/locale.php'); require_once(MIBEW_FS_ROOT . '/libs/common/locale.php');
require_once(MIBEW_FS_ROOT . '/libs/common/csrf.php'); require_once(MIBEW_FS_ROOT . '/libs/common/csrf.php');
@ -74,12 +74,12 @@ session_start();
// Initialize the database // Initialize the database
\Mibew\Database::initialize( \Mibew\Database::initialize(
$mysqlhost, $configs['database']['host'],
$mysqllogin, $configs['database']['login'],
$mysqlpass, $configs['database']['pass'],
$use_persistent_connection, $configs['database']['use_persistent_connection'],
$mysqldb, $configs['database']['db'],
$mysqlprefix $configs['database']['tables_prefix']
); );
if (function_exists("date_default_timezone_set")) { if (function_exists("date_default_timezone_set")) {
@ -88,9 +88,9 @@ if (function_exists("date_default_timezone_set")) {
@date_default_timezone_set(function_exists("date_default_timezone_get") ? @date_default_timezone_get() : "GMT"); @date_default_timezone_set(function_exists("date_default_timezone_get") ? @date_default_timezone_get() : "GMT");
} }
if (!empty($plugins_list)) { if (!empty($configs['plugins'])) {
// Variable $plugins_config defined in libs/config.php // A list of plugins is defined in $plugins_list variable in libs/config.php
\Mibew\Plugin\Manager::loadPlugins($plugins_list); \Mibew\Plugin\Manager::loadPlugins($configs['plugins']);
} }
// Load all other libraries // Load all other libraries