Config class overhaul.

This commit is contained in:
flash 2023-07-18 21:48:44 +00:00
parent 2b92d00b4b
commit 8339892559
30 changed files with 838 additions and 396 deletions

View file

@ -5,7 +5,6 @@ use Index\Autoloader;
use Index\Environment;
use Index\Data\ConnectionFailedException;
use Index\Data\DbTools;
use Misuzu\Config\IConfig;
use Misuzu\Config\DbConfig;
use Misuzu\Users\User;
use Misuzu\Users\UserNotFoundException;
@ -47,7 +46,7 @@ set_exception_handler(function(\Throwable $ex) {
header('Content-Type: text/plain; charset=utf-8');
echo (string)$ex;
} else {
header('Content-Type: text/html; charset-utf-8');
header('Content-Type: text/html; charset=utf-8');
echo file_get_contents(MSZ_TEMPLATES . '/500.html');
}
}
@ -83,24 +82,8 @@ DB::init(DbTools::parse($dbConfig['dsn']));
DB::exec(MSZ_DB_INIT);
$cfg = new DbConfig($db);
$cfg->reload();
Config::init($cfg);
Mailer::init($cfg->getValue('mail.method', IConfig::T_STR), [
'host' => $cfg->getValue('mail.host', IConfig::T_STR),
'port' => $cfg->getValue('mail.port', IConfig::T_INT, 25),
'username' => $cfg->getValue('mail.username', IConfig::T_STR),
'password' => $cfg->getValue('mail.password', IConfig::T_STR),
'encryption' => $cfg->getValue('mail.encryption', IConfig::T_STR),
'sender_name' => $cfg->getValue('mail.sender.name', IConfig::T_STR),
'sender_addr' => $cfg->getValue('mail.sender.address', IConfig::T_STR),
]);
// replace this with a better storage mechanism
define('MSZ_STORAGE', $cfg->getValue('storage.path', IConfig::T_STR, MSZ_ROOT . '/store'));
if(!is_dir(MSZ_STORAGE))
mkdir(MSZ_STORAGE, 0775, true);
Mailer::init($cfg->scopeTo('mail'));
$msz = new MisuzuContext($db, $cfg);
@ -115,17 +98,12 @@ ob_start();
if(file_exists(MSZ_ROOT . '/.migrating')) {
http_response_code(503);
if(!isset($_GET['_check'])) {
header('Content-Type: text/html; charset-utf-8');
header('Content-Type: text/html; charset=utf-8');
echo file_get_contents(MSZ_TEMPLATES . '/503.html');
}
exit;
}
if(!is_readable(MSZ_STORAGE) || !is_writable(MSZ_STORAGE)) {
echo 'Cannot access storage directory.';
exit;
}
if(!MSZ_DEBUG) {
$twigCacheDirSfx = GitInfo::hash(true);
if(empty($twigCacheDirSfx))
@ -136,16 +114,27 @@ if(!MSZ_DEBUG) {
mkdir($twigCache, 0775, true);
}
$globals = $cfg->getValues([
['site.name:s', 'Misuzu'],
'site.desc:s',
'site.url:s',
'sockChat.chatPath.normal:s',
'eeprom.path:s',
'eeprom.app:s',
['auth.secret:s', 'meow'],
['csrf.secret:s', 'soup'],
]);
Template::init($msz, $twigCache ?? null, MSZ_DEBUG);
Template::set('globals', [
'site_name' => $cfg->getValue('site.name', IConfig::T_STR, 'Misuzu'),
'site_description' => $cfg->getValue('site.desc', IConfig::T_STR),
'site_url' => $cfg->getValue('site.url', IConfig::T_STR),
'site_chat' => $cfg->getValue('sockChat.chatPath.normal', IConfig::T_STR),
'site_name' => $globals['site.name'],
'site_description' => $globals['site.desc'],
'site_url' => $globals['site.url'],
'site_chat' => $globals['sockChat.chatPath.normal'],
'eeprom' => [
'path' => $cfg->getValue('eeprom.path', IConfig::T_STR),
'app' => $cfg->getValue('eeprom.app', IConfig::T_STR),
'path' => $globals['eeprom.path'],
'app' => $globals['eeprom.app'],
],
]);
@ -156,7 +145,7 @@ unset($mszAssetsInfo);
Template::addPath(MSZ_TEMPLATES);
AuthToken::setSecretKey($cfg->getValue('auth.secret', IConfig::T_STR, 'meow'));
AuthToken::setSecretKey($globals['auth.secret']);
if(isset($_COOKIE['msz_uid']) && isset($_COOKIE['msz_sid'])) {
$authToken = new AuthToken;
@ -220,22 +209,21 @@ if($authToken->isValid()) {
}
CSRF::init(
$cfg->getValue('csrf.secret', IConfig::T_STR, 'soup'),
$globals['csrf.secret'],
(UserSession::hasCurrent() ? UserSession::getCurrent()->getToken() : ($_SERVER['REMOTE_ADDR'] ?? '::1'))
);
function mszLockdown(): void {
global $misuzuBypassLockdown, $cfg;
if($cfg->getValue('private.enabled', IConfig::T_BOOL)) {
if($cfg->getBoolean('private.enabled')) {
$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 = $cfg->getValue('private.perm.cat', IConfig::T_STR);
$privatePermVal = $cfg->getValue('private.perm.val', IConfig::T_INT);
['private.perm.cat' => $privatePermCat, 'private.perm.val' => $privatePermVal] = $cfg->getValues(['private.perm.cat:s', 'private.perm.val:i']);
if(!empty($privatePermCat) && $privatePermVal > 0) {
if(!perms_check_user($privatePermCat, User::getCurrent()->getId(), $privatePermVal)) {
@ -244,7 +232,7 @@ function mszLockdown(): void {
User::unsetCurrent();
}
}
} elseif(!$onLoginPage && !($onPasswordPage && $cfg->getValue('private.allow_password_reset', IConfig::T_BOOL, true))) {
} elseif(!$onLoginPage && !($onPasswordPage && $cfg->getBoolean('private.allow_password_reset', true))) {
url_redirect('auth-login');
exit;
}