Switched to new configuration format.

This commit is contained in:
flash 2023-10-31 16:10:32 +00:00
parent f4c3d34b70
commit 4836a52548
9 changed files with 38 additions and 75 deletions

1
.gitignore vendored
View file

@ -4,6 +4,7 @@
/public/data/* /public/data/*
/public/thumb/* /public/thumb/*
/public/robots.txt /public/robots.txt
/config.cfg
/config.ini /config.ini
/.debug /.debug
/vendor /vendor

15
config.example.cfg Normal file
View file

@ -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

View file

@ -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

View file

@ -2,8 +2,8 @@
namespace EEPROM; namespace EEPROM;
use Index\Environment; use Index\Environment;
use Index\Data\ConnectionFailedException;
use Index\Data\DbTools; use Index\Data\DbTools;
use Syokuhou\SharpConfig;
define('PRM_STARTUP', microtime(true)); define('PRM_STARTUP', microtime(true));
define('PRM_ROOT', __DIR__); define('PRM_ROOT', __DIR__);
@ -18,10 +18,11 @@ define('PRM_THUMBS', PRM_PUBLIC . '/thumb');
require_once PRM_ROOT . '/vendor/autoload.php'; require_once PRM_ROOT . '/vendor/autoload.php';
Environment::setDebug(PRM_DEBUG); Environment::setDebug(PRM_DEBUG);
mb_internal_encoding('utf-8'); mb_internal_encoding('utf-8');
date_default_timezone_set('utc'); date_default_timezone_set('utc');
$cfg = SharpConfig::fromFile(PRM_ROOT . '/config.cfg');
set_exception_handler(function(\Throwable $ex) { set_exception_handler(function(\Throwable $ex) {
http_response_code(500); http_response_code(500);
header('Content-Type: text/plain; charset=utf-8'); header('Content-Type: text/plain; charset=utf-8');
@ -40,19 +41,5 @@ if(!is_dir(PRM_UPLOADS))
if(!is_dir(PRM_THUMBS)) if(!is_dir(PRM_THUMBS))
mkdir(PRM_THUMBS, 0775, true); mkdir(PRM_THUMBS, 0775, true);
$configPath = PRM_ROOT . '/config.ini'; $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\';');
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 '<h3>Unable to connect to database</h3>';
die($ex->getMessage());
}

View file

@ -6,19 +6,21 @@ use Index\Http\HttpFx;
require_once __DIR__ . '/../eeprom.php'; require_once __DIR__ . '/../eeprom.php';
function eepromOriginAllowed(string $origin): bool { function eepromOriginAllowed(string $origin): bool {
global $cfg;
$origin = mb_strtolower(parse_url($origin, PHP_URL_HOST)); $origin = mb_strtolower(parse_url($origin, PHP_URL_HOST));
if($origin === $_SERVER['HTTP_HOST']) if($origin === $_SERVER['HTTP_HOST'])
return true; return true;
$allowed = Config::get('CORS', 'origins', []); $allowed = $cfg->getArray('cors:origins');
if(empty($allowed)) if(empty($allowed))
return true; return true;
return in_array($origin, $allowed); 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 = new HttpFx;
$router->use('/', function($response) { $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'); $auth = $request->getHeaderLine('Authorization');
if(empty($auth)) { if(empty($auth)) {
$mszAuth = (string)$request->getCookie('msz_auth'); $mszAuth = (string)$request->getCookie('msz_auth');
@ -64,7 +66,7 @@ if($isApiDomain) {
$authMethod = strval($authParts[0] ?? ''); $authMethod = strval($authParts[0] ?? '');
$authToken = strval($authParts[1] ?? ''); $authToken = strval($authParts[1] ?? '');
$authClients = Config::get('Auth', 'clients', []); $authClients = $cfg->getArray('auth:clients');
foreach($authClients as $client) { foreach($authClients as $client) {
$client = new $client; $client = new $client;

View file

@ -9,8 +9,10 @@ class MisuzuAuth implements IAuth {
private $secretKey = ''; private $secretKey = '';
public function __construct() { public function __construct() {
$this->endPoint = Config::get('Misuzu', 'endpoint', ''); global $cfg;
$this->secretKey = Config::get('Misuzu', 'secret', '');
$this->endPoint = $cfg->getString('misuzu:endpoint');
$this->secretKey = $cfg->getString('misuzu:secret');
} }
public function getName(): string { return 'Misuzu'; } public function getName(): string { return 'Misuzu'; }

View file

@ -8,7 +8,9 @@ class NabuccoAuth implements IAuth {
private $secretKey = ''; private $secretKey = '';
public function __construct() { public function __construct() {
$this->secretKey = Config::get('Nabucco', 'secret', ''); global $cfg;
$this->secretKey = $cfg->getString('nabucco:secret');
} }
public function getName(): string { return 'Nabucco'; } public function getName(): string { return 'Nabucco'; }

View file

@ -1,25 +0,0 @@
<?php
namespace EEPROM;
final class Config {
private static array $config = [];
public static function load(string $path): void {
$config = parse_ini_file($path, true, INI_SCANNER_TYPED);
if(!empty($config))
self::$config = array_merge(self::$config, $config);
}
public static function get(string $section, string $key, $default = null) {
if(!self::has($section, $key))
return $default;
return self::$config[$section][$key];
}
public static function has(string $section, string $key) {
return array_key_exists($section, self::$config)
&& array_key_exists($key, self::$config[$section])
&& !empty(self::$config[$section][$key]);
}
}

View file

@ -45,8 +45,10 @@ final class Upload implements JsonSerializable {
return '/uploads/' . $this->id; return '/uploads/' . $this->id;
} }
public function getPublicUrl(bool $forceReal = false): string { public function getPublicUrl(bool $forceReal = false): string {
if(!$forceReal && Config::has('Uploads', 'short_domain')) global $cfg;
return '//' . Config::get('Uploads', 'short_domain') . '/' . $this->id;
if(!$forceReal && $cfg->hasValues('domain:short'))
return '//' . $cfg->getString('domain:short') . '/' . $this->id;
return '//' . $_SERVER['HTTP_HOST'] . $this->getRemotePath(); return '//' . $_SERVER['HTTP_HOST'] . $this->getRemotePath();
} }
public function getPublicThumbUrl(bool $forceReal = false): string { public function getPublicThumbUrl(bool $forceReal = false): string {