Restructured config handler.

This commit is contained in:
flash 2023-01-01 20:23:53 +00:00
parent 46b30d5470
commit 5631f464d2
27 changed files with 324 additions and 167 deletions

View file

@ -6,6 +6,8 @@ use Index\Autoloader;
use Index\Environment;
use Index\Data\ConnectionFailedException;
use Index\Data\DbTools;
use Misuzu\Config\CfgType;
use Misuzu\Config\DbConfig;
use Misuzu\Net\GeoIP;
use Misuzu\Net\IPAddress;
use Misuzu\Users\User;
@ -87,19 +89,23 @@ $dbConfig = $dbConfig['Database'] ?? $dbConfig['Database.mysql-main'] ?? [];
DB::init(DB::buildDSN($dbConfig), $dbConfig['username'] ?? '', $dbConfig['password'] ?? '', DB::ATTRS);
DB::exec(MSZ_DB_INIT);
Config::init();
Mailer::init(Config::get('mail.method', Config::TYPE_STR), [
'host' => Config::get('mail.host', Config::TYPE_STR),
'port' => Config::get('mail.port', Config::TYPE_INT, 25),
'username' => Config::get('mail.username', Config::TYPE_STR),
'password' => Config::get('mail.password', Config::TYPE_STR),
'encryption' => Config::get('mail.encryption', Config::TYPE_STR),
'sender_name' => Config::get('mail.sender.name', Config::TYPE_STR),
'sender_addr' => Config::get('mail.sender.address', Config::TYPE_STR),
$cfg = new DbConfig($db);
$cfg->reload();
Config::init($cfg);
Mailer::init($cfg->getValue('mail.method', CfgType::T_STR), [
'host' => $cfg->getValue('mail.host', CfgType::T_STR),
'port' => $cfg->getValue('mail.port', CfgType::T_INT, 25),
'username' => $cfg->getValue('mail.username', CfgType::T_STR),
'password' => $cfg->getValue('mail.password', CfgType::T_STR),
'encryption' => $cfg->getValue('mail.encryption', CfgType::T_STR),
'sender_name' => $cfg->getValue('mail.sender.name', CfgType::T_STR),
'sender_addr' => $cfg->getValue('mail.sender.address', CfgType::T_STR),
]);
// replace this with a better storage mechanism
define('MSZ_STORAGE', Config::get('storage.path', Config::TYPE_STR, MSZ_ROOT . '/store'));
define('MSZ_STORAGE', $cfg->getValue('storage.path', CfgType::T_STR, MSZ_ROOT . '/store'));
if(!is_dir(MSZ_STORAGE))
mkdir(MSZ_STORAGE, 0775, true);
@ -113,7 +119,7 @@ if(MSZ_CLI) { // Temporary backwards compatibility measure, remove this later
return;
}
$ctx = new MszContext($db);
$ctx = new MszContext($db, $cfg);
// Everything below here should eventually be moved to index.php, probably only initialised when required.
// Serving things like the css/js doesn't need to initialise sessions.
@ -134,7 +140,7 @@ if(!is_readable(MSZ_STORAGE) || !is_writable(MSZ_STORAGE)) {
exit;
}
GeoIP::init(Config::get('geoip.database', Config::TYPE_STR, '/var/lib/GeoIP/GeoLite2-Country.mmdb'));
GeoIP::init($cfg->getValue('geoip.database', CfgType::T_STR, '/var/lib/GeoIP/GeoLite2-Country.mmdb'));
if(!MSZ_DEBUG) {
$twigCache = sys_get_temp_dir() . '/msz-tpl-cache-' . md5(MSZ_ROOT);
@ -145,10 +151,10 @@ if(!MSZ_DEBUG) {
Template::init($ctx, $twigCache ?? null, MSZ_DEBUG);
Template::set('globals', [
'site_name' => Config::get('site.name', Config::TYPE_STR, 'Misuzu'),
'site_description' => Config::get('site.desc', Config::TYPE_STR),
'site_url' => Config::get('site.url', Config::TYPE_STR),
'site_twitter' => Config::get('social.twitter', Config::TYPE_STR),
'site_name' => $cfg->getValue('site.name', CfgType::T_STR, 'Misuzu'),
'site_description' => $cfg->getValue('site.desc', CfgType::T_STR),
'site_url' => $cfg->getValue('site.url', CfgType::T_STR),
'site_twitter' => $cfg->getValue('social.twitter', CfgType::T_STR),
]);
Template::addPath(MSZ_TEMPLATES);
@ -200,21 +206,21 @@ if($authToken->isValid()) {
}
}
CSRF::setGlobalSecretKey(Config::get('csrf.secret', Config::TYPE_STR, 'soup'));
CSRF::setGlobalSecretKey($cfg->getValue('csrf.secret', CfgType::T_STR, 'soup'));
CSRF::setGlobalIdentity(UserSession::hasCurrent() ? UserSession::getCurrent()->getToken() : IPAddress::remote());
function mszLockdown(): void {
global $misuzuBypassLockdown;
global $misuzuBypassLockdown, $cfg;
if(Config::get('private.enabled', Config::TYPE_BOOL)) {
if($cfg->getValue('private.enabled', CfgType::T_BOOL)) {
$onLoginPage = $_SERVER['PHP_SELF'] === url('auth-login');
$onPasswordPage = parse_url($_SERVER['PHP_SELF'], PHP_URL_PATH) === url('auth-forgot');
$misuzuBypassLockdown = !empty($misuzuBypassLockdown) || $onLoginPage;
if(!$misuzuBypassLockdown) {
if(UserSession::hasCurrent()) {
$privatePermCat = Config::get('private.perm.cat', Config::TYPE_STR);
$privatePermVal = Config::get('private.perm.val', Config::TYPE_INT);
$privatePermCat = $cfg->getValue('private.perm.cat', CfgType::T_STR);
$privatePermVal = $cfg->getValue('private.perm.val', CfgType::T_INT);
if(!empty($privatePermCat) && $privatePermVal > 0) {
if(!perms_check_user($privatePermCat, User::getCurrent()->getId(), $privatePermVal)) {
@ -223,7 +229,7 @@ function mszLockdown(): void {
User::unsetCurrent();
}
}
} elseif(!$onLoginPage && !($onPasswordPage && Config::get('private.allow_password_reset', Config::TYPE_BOOL, true))) {
} elseif(!$onLoginPage && !($onPasswordPage && $cfg->getValue('private.allow_password_reset', CfgType::T_BOOL, true))) {
url_redirect('auth-login');
exit;
}