From c6018d0a4d14d298f6bd85ce6e958a628abd7f40 Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 4 Oct 2018 22:30:55 +0200 Subject: [PATCH] New config system, define a root dir constant and deprecate more of Application. --- misuzu.php | 38 ++++++--- public/auth.php | 2 +- public/changelog.php | 2 +- public/comments.php | 2 +- public/forum/forum.php | 2 +- public/forum/index.php | 2 +- public/forum/posting.php | 2 +- public/forum/topic.php | 2 +- public/index.php | 11 ++- public/info.php | 4 +- public/manage/changelog.php | 2 +- public/manage/forum.php | 2 +- public/manage/index.php | 2 +- public/manage/users.php | 2 +- public/members.php | 2 +- public/news.php | 2 +- public/not-found.php | 2 +- public/profile.php | 7 +- public/relations.php | 2 +- public/settings.php | 2 +- src/Application.php | 155 +++++++++--------------------------- src/Database.php | 2 +- src/WhoopsReporter.php | 2 +- src/comments.php | 2 +- src/config.php | 23 ++++-- templates/home/index.twig | 10 +-- utility.php | 2 +- 27 files changed, 120 insertions(+), 168 deletions(-) diff --git a/misuzu.php b/misuzu.php index cfc06bbc..0bdbd776 100644 --- a/misuzu.php +++ b/misuzu.php @@ -2,14 +2,15 @@ namespace Misuzu; define('MSZ_STARTUP', microtime(true)); -define('MSZ_DEBUG', file_exists(__DIR__ . '/.debug')); +define('MSZ_ROOT', __DIR__); +define('MSZ_DEBUG', is_file(MSZ_ROOT . '/.debug')); error_reporting(MSZ_DEBUG ? -1 : 0); ini_set('display_errors', MSZ_DEBUG ? 'On' : 'Off'); date_default_timezone_set('UTC'); mb_internal_encoding('UTF-8'); -set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__); +set_include_path(get_include_path() . PATH_SEPARATOR . MSZ_ROOT); require_once 'vendor/autoload.php'; @@ -30,6 +31,7 @@ require_once 'src/audit_log.php'; require_once 'src/changelog.php'; require_once 'src/colour.php'; require_once 'src/comments.php'; +require_once 'src/config.php'; require_once 'src/csrf.php'; require_once 'src/general.php'; require_once 'src/git.php'; @@ -55,10 +57,15 @@ require_once 'src/Users/session.php'; require_once 'src/Users/user.php'; require_once 'src/Users/validation.php'; -$app = new Application(__DIR__ . '/config/config.ini'); +config_load(MSZ_ROOT . '/config/config.ini'); + +$app = new Application; if (!empty($errorReporter)) { - $errorReporter->setReportInfo(...$app->getReportInfo()); + $errorReporter->setReportInfo( + config_get('Exceptions', 'report_url'), + config_get('Exceptions', 'hash_key') + ); } $app->startDatabase(); @@ -135,7 +142,7 @@ if (PHP_SAPI === 'cli') { case 'migrate': $migrationTargets = [ - 'mysql-main' => __DIR__ . '/database', + 'mysql-main' => MSZ_ROOT . '/database', ]; $doRollback = !empty($argv[2]) && $argv[2] === 'rollback'; $targetDb = isset($argv[$doRollback ? 3 : 2]) ? $argv[$doRollback ? 3 : 2] : null; @@ -187,7 +194,7 @@ if (PHP_SAPI === 'cli') { } $filename = date('Y_m_d_His_') . trim($argv[2], '_') . '.php'; - $filepath = __DIR__ . '/database/' . $filename; + $filepath = MSZ_ROOT . '/database/' . $filename; $namespace = snake_to_camel($argv[2]); $template = <<canAccessStorage()) { + $mszStoragePath = $app->getStoragePath(); + if (!is_readable($mszStoragePath) || !is_writable($mszStoragePath)) { echo 'Cannot access storage directory.'; exit; } @@ -237,10 +245,15 @@ MIG; tpl_init([ 'debug' => MSZ_DEBUG, 'auto_reload' => MSZ_DEBUG, - 'cache' => MSZ_DEBUG ? false : create_directory(build_path(sys_get_temp_dir(), 'msz-tpl-cache-' . md5(__DIR__))), + 'cache' => MSZ_DEBUG ? false : create_directory(build_path(sys_get_temp_dir(), 'msz-tpl-cache-' . md5(MSZ_ROOT))), ]); - tpl_var('globals', $app->getSiteInfo()); + tpl_var('globals', [ + 'site_name' => config_get_default('Misuzu', 'Site', 'name'), + 'site_description' => config_get('Site', 'description'), + 'site_twitter' => config_get('Site', 'twitter'), + 'site_url' => config_get('Site', 'url'), + ]); tpl_add_function('json_decode', true); tpl_add_function('byte_symbol', true); @@ -273,7 +286,7 @@ MIG; }); tpl_add_function('sql_query_count', false, [Database::class, 'queryCount']); - tpl_add_path(__DIR__ . '/templates'); + tpl_add_path(MSZ_ROOT . '/templates'); $misuzuBypassLockdown = !empty($misuzuBypassLockdown); @@ -302,7 +315,10 @@ MIG; $userDisplayInfo = $getUserDisplayInfo->execute() ? $getUserDisplayInfo->fetch() : []; } - csrf_init($app->getCsrfSecretKey(), empty($userDisplayInfo) ? ip_remote_address() : $_COOKIE['msz_sid']); + csrf_init( + config_get_default('insecure', 'CSRF', 'secret_key'), + empty($userDisplayInfo) ? ip_remote_address() : $_COOKIE['msz_sid'] + ); $privateInfo = $app->getPrivateInfo(); diff --git a/public/auth.php b/public/auth.php index 1d40ebb5..9e7af21f 100644 --- a/public/auth.php +++ b/public/auth.php @@ -6,7 +6,7 @@ $isSubmission = !empty($_POST['auth']) && is_array($_POST['auth']); $authMode = $isSubmission ? ($_POST['auth']['mode'] ?? '') : ($_GET['m'] ?? 'login'); $misuzuBypassLockdown = $authMode === 'login' || $authMode === 'get_user'; -require_once __DIR__ . '/../misuzu.php'; +require_once '../misuzu.php'; $usernameValidationErrors = [ 'trim' => 'Your username may not start or end with spaces!', diff --git a/public/changelog.php b/public/changelog.php index eb0e39c5..dd5f3b34 100644 --- a/public/changelog.php +++ b/public/changelog.php @@ -1,7 +1,7 @@ getLinkedData()); +if (config_get_default(false, 'Site', 'embed_linked_data')) { + tpl_var('linked_data', [ + 'name' => config_get('Site', 'name'), + 'url' => config_get('Site', 'url'), + 'logo' => config_get('Site', 'external_logo'), + 'same_as' => explode(',', config_get_default('', 'Site', 'social_media')), + ]); +} $news = Database::query(' SELECT diff --git a/public/info.php b/public/info.php index fcd8328c..a53aceca 100644 --- a/public/info.php +++ b/public/info.php @@ -1,7 +1,7 @@ getDefaultAvatar(); + $avatar_filename = build_path( + MSZ_ROOT, + config_get_default('public/images/no-avatar.png', 'Avatar', 'default_path') + ); $user_avatar = "{$userId}.msz"; $cropped_avatar = build_path( create_directory(build_path($app->getStoragePath(), 'avatars/200x200')), diff --git a/public/relations.php b/public/relations.php index 509b22ab..51cc88ad 100644 --- a/public/relations.php +++ b/public/relations.php @@ -1,7 +1,7 @@ Swift_SendmailTransport::class, ]; - private $config = []; - private $mailerInstance = null; private $geoipInstance = null; - /** - * Constructor, called by ApplicationBase::start() which also passes the arguments through. - * @param null|string $configFile - * @param bool $debug - */ - public function __construct(?string $configFile = null) + public function __construct() { if (!empty(self::$instance)) { throw new UnexpectedValueException('An Application has already been set up.'); } self::$instance = $this; - $this->config = is_file($configFile) ? parse_ini_file($configFile, true, INI_SCANNER_TYPED) : []; - - if ($this->config === false) { - throw new UnexpectedValueException('Failed to parse configuration.'); - } - } - - public function getReportInfo(): array - { - return [ - $this->config['Exceptions']['report_url'] ?? null, - $this->config['Exceptions']['hash_key'] ?? null, - ]; - } - - /** - * Gets a storage path. - * @param string $path - * @return string - */ - public function getPath(string $path): string - { - if (!starts_with($path, '/') && mb_substr($path, 1, 2) !== ':\\') { - $path = __DIR__ . '/../' . $path; - } - - return fix_path_separator(rtrim($path, '/')); } /** @@ -83,13 +48,7 @@ final class Application */ public function getStoragePath(): string { - return create_directory($this->config['Storage']['path'] ?? __DIR__ . '/../store'); - } - - public function canAccessStorage(): bool - { - $path = $this->getStoragePath(); - return is_readable($path) && is_writable($path); + return create_directory(config_get_default(MSZ_ROOT . '/store', 'Storage', 'path')); } /** @@ -104,7 +63,7 @@ final class Application $connections = []; foreach (self::DATABASE_CONNECTIONS as $name) { - $connections[$name] = $this->config["Database.{$name}"] ?? []; + $connections[$name] = config_get_default([], "Database.{$name}"); } new Database($connections, self::DATABASE_CONNECTIONS[0]); @@ -120,11 +79,11 @@ final class Application } new Cache( - $this->config['Cache']['host'] ?? null, - $this->config['Cache']['port'] ?? null, - $this->config['Cache']['database'] ?? null, - $this->config['Cache']['password'] ?? null, - $this->config['Cache']['prefix'] ?? '' + config_get('Cache', 'host'), + config_get('Cache', 'port'), + config_get('Cache', 'database'), + config_get('Cache', 'password'), + config_get_default('', 'Cache', 'prefix') ); } @@ -134,11 +93,9 @@ final class Application return; } - if (array_key_exists('Mail', $this->config) && array_key_exists('method', $this->config['Mail'])) { - $method = mb_strtolower($this->config['Mail']['method'] ?? ''); - } + $method = mb_strtolower(config_get('Mail', 'method')); - if (empty($method) || !array_key_exists($method, self::MAIL_TRANSPORT)) { + if (!array_key_exists($method, self::MAIL_TRANSPORT)) { $method = 'null'; } @@ -147,25 +104,27 @@ final class Application switch ($method) { case 'sendmail': - if (array_key_exists('command', $this->config['Mail'])) { - $transport->setCommand($this->config['Mail']['command']); + $command = config_get('Mail', 'command'); + + if (!empty($command)) { + $transport->setCommand($command); } break; case 'smtp': - $transport->setHost($this->config['Mail']['host'] ?? ''); - $transport->setPort(intval($this->config['Mail']['port'] ?? 25)); + $transport->setHost(config_get_default('', 'Mail', 'host')); + $transport->setPort(intval(config_get_default(25, 'Mail', 'port'))); - if (array_key_exists('encryption', $this->config['Mail'])) { - $transport->setEncryption($this->config['Mail']['encryption']); - } + $extra = [ + 'setEncryption' => config_get('Mail', 'encryption'), + 'setUsername' => config_get('Mail', 'username'), + 'setPassword' => config_get('Mail', 'password'), + ]; - if (array_key_exists('username', $this->config['Mail'])) { - $transport->setUsername($this->config['Mail']['username']); - } - - if (array_key_exists('password', $this->config['Mail'])) { - $transport->setPassword($this->config['Mail']['password']); + foreach ($extra as $method => $value) { + if (!empty($value)) { + $transport->{$method}($value); + } } break; } @@ -190,7 +149,7 @@ final class Application public function getMailSender(): array { return [ - ($this->config['Mail']['sender_email'] ?? 'sys@msz.lh') => ($this->config['Mail']['sender_name'] ?? 'Misuzu System') + config_get_default('sys@msz.lh', 'Mail', 'sender_email') => config_get_default('Misuzu System', 'Mail', 'sender_name'), ]; } @@ -200,7 +159,7 @@ final class Application return; } - $this->geoipInstance = new GeoIP($this->config['GeoIP']['database_path'] ?? ''); + $this->geoipInstance = new GeoIP(config_get_default('', 'GeoIP', 'database_path')); } public function getGeoIP(): GeoIP @@ -220,79 +179,39 @@ final class Application public function getAvatarProps(): array { return [ - 'max_width' => intval($this->config['Avatar']['max_width'] ?? 4000), - 'max_height' => intval($this->config['Avatar']['max_height'] ?? 4000), - 'max_size' => intval($this->config['Avatar']['max_filesize'] ?? 1000000), + 'max_width' => intval(config_get_default(1000, 'Avatar', 'max_width')), + 'max_height' => intval(config_get_default(1000, 'Avatar', 'max_height')), + 'max_size' => intval(config_get_default(500000, 'Avatar', 'max_filesize')), ]; } public function getBackgroundProps(): array { return [ - 'max_width' => intval($this->config['Background']['max_width'] ?? 3840), - 'max_height' => intval($this->config['Background']['max_height'] ?? 2160), - 'max_size' => intval($this->config['Background']['max_filesize'] ?? 1000000), + 'max_width' => intval(config_get_default(3840, 'Avatar', 'max_width')), + 'max_height' => intval(config_get_default(2160, 'Avatar', 'max_height')), + 'max_size' => intval(config_get_default(1000000, 'Avatar', 'max_filesize')), ]; } public function underLockdown(): bool { - return boolval($this->config['Auth']['lockdown'] ?? false); + return boolval(config_get_default(false, 'Auth', 'lockdown')); } public function disableRegistration(): bool { return $this->underLockdown() || $this->getPrivateInfo()['enabled'] - || boolval($this->config['Auth']['prevent_registration'] ?? false); + || boolval(config_get_default(false, 'Auth', 'prevent_registration')); } public function getPrivateInfo(): array { - return !empty($this->config['Private']) && boolval($this->config['Private']['enabled']) - ? $this->config['Private'] - : ['enabled' => false]; + return config_get_default(['enabled' => false], 'Private'); } - public function getLinkedData(): array - { - if (!($this->config['Site']['embed_linked_data'] ?? false)) { - return ['embed_linked_data' => false]; - } - - return [ - 'embed_linked_data' => true, - 'embed_name' => $this->config['Site']['name'] ?? 'Flashii', - 'embed_url' => $this->config['Site']['url'] ?? '', - 'embed_logo' => $this->config['Site']['external_logo'] ?? '', - 'embed_same_as' => explode(',', $this->config['Site']['social_media'] ?? '') - ]; - } - - public function getSiteInfo(): array - { - return [ - 'site_name' => $this->config['Site']['name'] ?? 'Flashii', - 'site_description' => $this->config['Site']['description'] ?? '', - 'site_twitter' => $this->config['Site']['twitter'] ?? '', - 'site_url' => $this->config['Site']['url'] ?? '', - ]; - } - - public function getDefaultAvatar(): string - { - return $this->getPath($this->config['Avatar']['default_path'] ?? 'public/images/no-avatar.png'); - } - - public function getCsrfSecretKey(): string - { - return $this->config['CSRF']['secret_key'] ?? 'insecure'; - } - - /** - * Gets the currently active instance of Application - * @return Application - */ + // used in some of the user functions still, fix that public static function getInstance(): Application { if (empty(self::$instance)) { diff --git a/src/Database.php b/src/Database.php index 8c1f90fc..02f99728 100644 --- a/src/Database.php +++ b/src/Database.php @@ -131,7 +131,7 @@ final class Database if ($info['memory']) { $dsn .= ':memory:'; } else { - $databasePath = realpath($info['database'] ?? __DIR__ . '/../store/misuzu.db'); + $databasePath = realpath($info['database'] ?? MSZ_ROOT . '/store/misuzu.db'); if ($databasePath === false) { throw new UnexpectedValueException("Database does not exist."); diff --git a/src/WhoopsReporter.php b/src/WhoopsReporter.php index 6614724e..277a0ac7 100644 --- a/src/WhoopsReporter.php +++ b/src/WhoopsReporter.php @@ -74,7 +74,7 @@ HTML; ], 'misuzu' => [ 'trace_txt' => $this->getException()->getTraceAsString(), - 'directory' => dirname(__DIR__, 1), + 'directory' => MSZ_ROOT, ], 'exception' => Formatter::formatExceptionAsDataArray( $this->getInspector(), diff --git a/src/comments.php b/src/comments.php index 418b59f1..e2c2a457 100644 --- a/src/comments.php +++ b/src/comments.php @@ -1,7 +1,7 @@ - {% if embed_linked_data is defined and embed_linked_data %} + {% if linked_data is defined and linked_data is iterable %} diff --git a/utility.php b/utility.php index 92e5034a..945d3215 100644 --- a/utility.php +++ b/utility.php @@ -86,7 +86,7 @@ function check_mx_record(string $email): bool function asset_url(string $path): string { - $realPath = realpath(__DIR__ . '/public/' . $path); + $realPath = realpath(MSZ_ROOT . '/public/' . $path); if ($realPath === false || !file_exists($realPath)) { return $path;