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);
}
/**
* 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.
* 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.
* Another value can be set at operator/settings page.
*/
define('DEFAULT_CRON_KEY', md5(
$mysqlhost . '##' . $mysqldb . '##' . $mysqllogin . '##'
. $mysqlpass . '##' . $mysqlprefix . '##'
$configs['database']['host'] . '##' . $configs['database']['db'] . '##'
. $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
*/
define('MIBEW_MAILBOX', $mibew_mailbox);
define('MIBEW_MAILBOX', $configs['mailbox']);

View File

@ -16,7 +16,6 @@
*/
use Mibew\Database;
use Mibew\Plugin\Manager as PluginManager;
use Symfony\Component\Translation\Loader\PoFileLoader;
/**
@ -32,7 +31,9 @@ define('LOCALE_COOKIE_NAME', 'mibew_locale');
*/
define(
'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(
'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__)));
// Include configuration file
require_once(MIBEW_FS_ROOT . '/libs/config.php');
// Load system configurations
require_once(MIBEW_FS_ROOT . '/libs/common/configurations.php');
$configs = load_system_configs();
// Sanitize path to application and remove extra slashes
$mibewroot = join(
"/",
array_map(
"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');
// 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/locale.php');
require_once(MIBEW_FS_ROOT . '/libs/common/csrf.php');
@ -74,12 +74,12 @@ session_start();
// Initialize the database
\Mibew\Database::initialize(
$mysqlhost,
$mysqllogin,
$mysqlpass,
$use_persistent_connection,
$mysqldb,
$mysqlprefix
$configs['database']['host'],
$configs['database']['login'],
$configs['database']['pass'],
$configs['database']['use_persistent_connection'],
$configs['database']['db'],
$configs['database']['tables_prefix']
);
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");
}
if (!empty($plugins_list)) {
// Variable $plugins_config defined in libs/config.php
\Mibew\Plugin\Manager::loadPlugins($plugins_list);
if (!empty($configs['plugins'])) {
// A list of plugins is defined in $plugins_list variable in libs/config.php
\Mibew\Plugin\Manager::loadPlugins($configs['plugins']);
}
// Load all other libraries