From 4836a52548f18768ed272ac801258afe1dbe1f50 Mon Sep 17 00:00:00 2001 From: flashwave Date: Tue, 31 Oct 2023 16:10:32 +0000 Subject: [PATCH] Switched to new configuration format. --- .gitignore | 1 + config.example.cfg | 15 +++++++++++++++ config.example.ini | 23 ----------------------- eeprom.php | 23 +++++------------------ public/index.php | 10 ++++++---- src/Auth/MisuzuAuth.php | 6 ++++-- src/Auth/NabuccoAuth.php | 4 +++- src/Config.php | 25 ------------------------- src/Upload.php | 6 ++++-- 9 files changed, 38 insertions(+), 75 deletions(-) create mode 100644 config.example.cfg delete mode 100644 config.example.ini delete mode 100644 src/Config.php diff --git a/.gitignore b/.gitignore index 1e54788..d95e4b9 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ /public/data/* /public/thumb/* /public/robots.txt +/config.cfg /config.ini /.debug /vendor diff --git a/config.example.cfg b/config.example.cfg new file mode 100644 index 0000000..3475f45 --- /dev/null +++ b/config.example.cfg @@ -0,0 +1,15 @@ +database:dsn mariadb://user:password@:unix:/eeprom?socket=/var/run/mysqld/mysqld.sock&charset=utf8mb4 + +; Must be implementations of \EEPROM\Auth\IAuth +auth:clients \EEPROM\Auth\MisuzuAuth \EEPROM\Auth\NabuccoAuth + +misuzu:secret woomy +misuzu:endpoint https://flashii.net/_sockchat/verify + +nabucco:secret secret key + +domain:short i.flashii.net +domain:api eeprom.flashii.net + +; List of allowed remote domains +cors:origins flashii.net chat.flashii.net sockchat.flashii.net diff --git a/config.example.ini b/config.example.ini deleted file mode 100644 index b83af44..0000000 --- a/config.example.ini +++ /dev/null @@ -1,23 +0,0 @@ -[Database] -dsn = "mariadb://user:password@:unix:/eeprom?socket=/var/run/mysqld/mysqld.sock&charset=utf8mb4&init=SET SESSION time_zone = '+00:00', sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'" - -[Auth] -; Must be implementations of \EEPROM\Auth\IAuth -clients[] = \EEPROM\Auth\MisuzuAuth -clients[] = \EEPROM\Auth\SockChatAuth - -[Misuzu] -secret = woomy -endpoint = https://flashii.net/_sockchat/verify - -[Nabucco] -secret = secret key - -[Uploads] -short_domain = i.eeprom.domain - -[CORS] -; List of allowed remote domains -origins[] = flashii.net -origins[] = chat.flashii.net -origins[] = sockchat.flashii.net diff --git a/eeprom.php b/eeprom.php index fd5b76a..ff0ef44 100644 --- a/eeprom.php +++ b/eeprom.php @@ -2,8 +2,8 @@ namespace EEPROM; use Index\Environment; -use Index\Data\ConnectionFailedException; use Index\Data\DbTools; +use Syokuhou\SharpConfig; define('PRM_STARTUP', microtime(true)); define('PRM_ROOT', __DIR__); @@ -18,10 +18,11 @@ define('PRM_THUMBS', PRM_PUBLIC . '/thumb'); require_once PRM_ROOT . '/vendor/autoload.php'; Environment::setDebug(PRM_DEBUG); - mb_internal_encoding('utf-8'); date_default_timezone_set('utc'); +$cfg = SharpConfig::fromFile(PRM_ROOT . '/config.cfg'); + set_exception_handler(function(\Throwable $ex) { http_response_code(500); header('Content-Type: text/plain; charset=utf-8'); @@ -40,19 +41,5 @@ if(!is_dir(PRM_UPLOADS)) if(!is_dir(PRM_THUMBS)) mkdir(PRM_THUMBS, 0775, true); -$configPath = PRM_ROOT . '/config.ini'; - -if(!is_file($configPath)) - die('EEPROM configuration is missing.'); - -Config::load($configPath); - -if(!Config::has('Database', 'dsn')) - die('EEPROM database is not configured.'); - -try { - $db = DbTools::create(Config::get('Database', 'dsn')); -} catch(ConnectionFailedException $ex) { - echo '

Unable to connect to database

'; - die($ex->getMessage()); -} +$db = DbTools::create($cfg->getString('database:dsn', 'null:')); +$db->execute('SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';'); diff --git a/public/index.php b/public/index.php index dd6f3ea..6952e2b 100644 --- a/public/index.php +++ b/public/index.php @@ -6,19 +6,21 @@ use Index\Http\HttpFx; require_once __DIR__ . '/../eeprom.php'; function eepromOriginAllowed(string $origin): bool { + global $cfg; + $origin = mb_strtolower(parse_url($origin, PHP_URL_HOST)); if($origin === $_SERVER['HTTP_HOST']) return true; - $allowed = Config::get('CORS', 'origins', []); + $allowed = $cfg->getArray('cors:origins'); if(empty($allowed)) return true; return in_array($origin, $allowed); } -$isApiDomain = $_SERVER['HTTP_HOST'] === Config::get('Uploads', 'api_domain'); +$isApiDomain = $_SERVER['HTTP_HOST'] === $cfg->getString('domain:api'); $router = new HttpFx; $router->use('/', function($response) { @@ -51,7 +53,7 @@ if($isApiDomain) { } }); - $router->use('/', function($response, $request) use ($db) { + $router->use('/', function($response, $request) use ($db, $cfg) { $auth = $request->getHeaderLine('Authorization'); if(empty($auth)) { $mszAuth = (string)$request->getCookie('msz_auth'); @@ -64,7 +66,7 @@ if($isApiDomain) { $authMethod = strval($authParts[0] ?? ''); $authToken = strval($authParts[1] ?? ''); - $authClients = Config::get('Auth', 'clients', []); + $authClients = $cfg->getArray('auth:clients'); foreach($authClients as $client) { $client = new $client; diff --git a/src/Auth/MisuzuAuth.php b/src/Auth/MisuzuAuth.php index 01cebcf..1022e66 100644 --- a/src/Auth/MisuzuAuth.php +++ b/src/Auth/MisuzuAuth.php @@ -9,8 +9,10 @@ class MisuzuAuth implements IAuth { private $secretKey = ''; public function __construct() { - $this->endPoint = Config::get('Misuzu', 'endpoint', ''); - $this->secretKey = Config::get('Misuzu', 'secret', ''); + global $cfg; + + $this->endPoint = $cfg->getString('misuzu:endpoint'); + $this->secretKey = $cfg->getString('misuzu:secret'); } public function getName(): string { return 'Misuzu'; } diff --git a/src/Auth/NabuccoAuth.php b/src/Auth/NabuccoAuth.php index 03abcd8..15bb371 100644 --- a/src/Auth/NabuccoAuth.php +++ b/src/Auth/NabuccoAuth.php @@ -8,7 +8,9 @@ class NabuccoAuth implements IAuth { private $secretKey = ''; public function __construct() { - $this->secretKey = Config::get('Nabucco', 'secret', ''); + global $cfg; + + $this->secretKey = $cfg->getString('nabucco:secret'); } public function getName(): string { return 'Nabucco'; } diff --git a/src/Config.php b/src/Config.php deleted file mode 100644 index cde78f1..0000000 --- a/src/Config.php +++ /dev/null @@ -1,25 +0,0 @@ -id; } public function getPublicUrl(bool $forceReal = false): string { - if(!$forceReal && Config::has('Uploads', 'short_domain')) - return '//' . Config::get('Uploads', 'short_domain') . '/' . $this->id; + global $cfg; + + if(!$forceReal && $cfg->hasValues('domain:short')) + return '//' . $cfg->getString('domain:short') . '/' . $this->id; return '//' . $_SERVER['HTTP_HOST'] . $this->getRemotePath(); } public function getPublicThumbUrl(bool $forceReal = false): string {