diff options
Diffstat (limited to 'classes/Config.php')
| -rw-r--r-- | classes/Config.php | 692 |
1 files changed, 346 insertions, 346 deletions
diff --git a/classes/Config.php b/classes/Config.php index 1129117..b0a6e12 100644 --- a/classes/Config.php +++ b/classes/Config.php @@ -1,356 +1,356 @@ <?php class Config { - private const _ENVVAR_PREFIX = "EPUBE_"; - - const T_BOOL = 1; - const T_STRING = 2; - const T_INT = 3; - - // override defaults, defined below in _DEFAULTS[], via environment: DB_TYPE becomes EPUBE_DB_TYPE, etc - - const DB_TYPE = "DB_TYPE"; - const DB_HOST = "DB_HOST"; - const DB_USER = "DB_USER"; - const DB_NAME = "DB_NAME"; - const DB_PASS = "DB_PASS"; - const DB_PORT = "DB_PORT"; - - const SCRATCH_DB = "SCRATCH_DB"; - const CALIBRE_DB = "CALIBRE_DB"; - const BOOKS_DIR = "BOOKS_DIR"; - const DICT_SERVER = "DICT_SERVER"; - const SESSION_LIFETIME = "SESSION_LIFETIME"; - const SESSION_NAME = "SESSION_NAME"; - const OIDC_NAME = "OIDC_NAME"; - const OIDC_URL = "OIDC_URL"; - const OIDC_CLIENT_ID = "OIDC_CLIENT_ID"; - const OIDC_CLIENT_SECRET = "OIDC_CLIENT_SECRET"; - - private const _DEFAULTS = [ - Config::DB_TYPE => [ "sqlite", Config::T_STRING ], - Config::DB_HOST => [ "db", Config::T_STRING ], - Config::DB_USER => [ "", Config::T_STRING ], - Config::DB_NAME => [ "", Config::T_STRING ], - Config::DB_PASS => [ "", Config::T_STRING ], - Config::DB_PORT => [ "5432", Config::T_STRING ], - Config::SCRATCH_DB => [ "db/scratch.db", Config::T_STRING ], - Config::CALIBRE_DB => [ "", Config::T_STRING ], - Config::BOOKS_DIR => [ "", Config::T_STRING ], - Config::DICT_SERVER => [ "", Config::T_STRING ], - Config::SESSION_LIFETIME => [ 86400*30, Config::T_INT ], - Config::SESSION_NAME => [ "epube_sid", Config::T_STRING ], - Config::OIDC_NAME => [ "OIDC", Config::T_STRING ], - Config::OIDC_URL => [ "", Config::T_STRING ], - Config::OIDC_CLIENT_ID => [ "", Config::T_STRING ], - Config::OIDC_CLIENT_SECRET => [ "", Config::T_STRING ], - ]; - - private static ?Config $instance = null; - - /** @var array<string, array<bool|int|string>> */ - private array $params = []; - - /** @var array<string, mixed> */ - private array $version = []; - - private Db_Migrations $migrations; - - public static function get_instance() : Config { - if (self::$instance == null) - self::$instance = new self(); - - return self::$instance; - } - - private function __clone() { - // - } - - /** load data from environment variables */ - function reload(): void { - $ref = new ReflectionClass(get_class($this)); - - foreach ($ref->getConstants() as $const => $cvalue) { - if (isset(self::_DEFAULTS[$const])) { - $override = getenv(self::_ENVVAR_PREFIX . $const); - - list ($defval, $deftype) = self::_DEFAULTS[$const]; - - $this->params[$cvalue] = [ self::cast_to($override !== false ? $override : $defval, $deftype), $deftype ]; - } - } - } - - /** this is primarily needed for tests, normally global config is not changed at runtime */ - static function set(string $key, mixed $value): bool { - if (!getenv('IS_INTEGRATION_TESTING') && !getenv('IS_TESTING')) { - user_error("This method is only available for tests.", E_USER_WARNING); - - return false; - } if (array_key_exists($key, self::_DEFAULTS)) { - list ($defval, $deftype) = self::_DEFAULTS[$key]; - - self::get_instance()->params[$key] = [self::cast_to($value, $deftype), $deftype]; - - return true; - } else { - user_error("Requested to set unknown global config variable: {$key}", E_USER_WARNING); - - return false; - } - } - - function __construct() { - $this->reload(); - } - - /* package maintainers who don't use git: if version_static.txt exists in tt-rss root - directory, its contents are displayed instead of git commit-based version, this could be generated - based on source git tree commit used when creating the package */ - - /** @return string|array<string, mixed> */ - static function get_version(bool $as_string = true) : mixed { - return self::get_instance()->_get_version($as_string); - } - - /** @return string|array<string, mixed> */ - private function _get_version(bool $as_string = true) : mixed { - $root_dir = dirname(__DIR__); - - if (empty($this->version)) { - $this->version["status"] = -1; - - if (getenv("CI_COMMIT_SHORT_SHA") && getenv("CI_COMMIT_TIMESTAMP")) { - - $this->version["branch"] = getenv("CI_COMMIT_BRANCH"); - $this->version["timestamp"] = strtotime(getenv("CI_COMMIT_TIMESTAMP")); - $this->version["version"] = sprintf("%s-%s", date("y.m", $this->version["timestamp"]), getenv("CI_COMMIT_SHORT_SHA")); - $this->version["commit"] = getenv("CI_COMMIT_SHORT_SHA"); - $this->version["status"] = 0; - - } else if (PHP_OS === "Darwin") { - $this->version["version"] = "UNKNOWN (Unsupported, Darwin)"; - } else if (file_exists("$root_dir/version_static.txt")) { - $this->version["version"] = trim(file_get_contents("$root_dir/version_static.txt")) . " (Unsupported)"; - } else if (is_dir("$root_dir/.git")) { - $this->version = self::get_version_from_git($root_dir); - - if ($this->version["status"] != 0) { - user_error("Unable to determine version: " . $this->version["version"], E_USER_WARNING); - - $this->version["version"] = "UNKNOWN (Unsupported, Git error)"; - } - } else { - $this->version["version"] = "UNKNOWN (Unsupported)"; - } - } + private const _ENVVAR_PREFIX = "EPUBE_"; + + const T_BOOL = 1; + const T_STRING = 2; + const T_INT = 3; + + // override defaults, defined below in _DEFAULTS[], via environment: DB_TYPE becomes EPUBE_DB_TYPE, etc + + const DB_TYPE = "DB_TYPE"; + const DB_HOST = "DB_HOST"; + const DB_USER = "DB_USER"; + const DB_NAME = "DB_NAME"; + const DB_PASS = "DB_PASS"; + const DB_PORT = "DB_PORT"; + + const SCRATCH_DB = "SCRATCH_DB"; + const CALIBRE_DB = "CALIBRE_DB"; + const BOOKS_DIR = "BOOKS_DIR"; + const DICT_SERVER = "DICT_SERVER"; + const SESSION_LIFETIME = "SESSION_LIFETIME"; + const SESSION_NAME = "SESSION_NAME"; + const OIDC_NAME = "OIDC_NAME"; + const OIDC_URL = "OIDC_URL"; + const OIDC_CLIENT_ID = "OIDC_CLIENT_ID"; + const OIDC_CLIENT_SECRET = "OIDC_CLIENT_SECRET"; + + private const _DEFAULTS = [ + Config::DB_TYPE => [ "sqlite", Config::T_STRING ], + Config::DB_HOST => [ "db", Config::T_STRING ], + Config::DB_USER => [ "", Config::T_STRING ], + Config::DB_NAME => [ "", Config::T_STRING ], + Config::DB_PASS => [ "", Config::T_STRING ], + Config::DB_PORT => [ "5432", Config::T_STRING ], + Config::SCRATCH_DB => [ "db/scratch.db", Config::T_STRING ], + Config::CALIBRE_DB => [ "", Config::T_STRING ], + Config::BOOKS_DIR => [ "", Config::T_STRING ], + Config::DICT_SERVER => [ "", Config::T_STRING ], + Config::SESSION_LIFETIME => [ 86400*30, Config::T_INT ], + Config::SESSION_NAME => [ "epube_sid", Config::T_STRING ], + Config::OIDC_NAME => [ "OIDC", Config::T_STRING ], + Config::OIDC_URL => [ "", Config::T_STRING ], + Config::OIDC_CLIENT_ID => [ "", Config::T_STRING ], + Config::OIDC_CLIENT_SECRET => [ "", Config::T_STRING ], + ]; + + private static ?Config $instance = null; + + /** @var array<string, array<bool|int|string>> */ + private array $params = []; + + /** @var array<string, mixed> */ + private array $version = []; + + private Db_Migrations $migrations; + + public static function get_instance() : Config { + if (self::$instance == null) + self::$instance = new self(); + + return self::$instance; + } + + private function __clone() { + // + } + + /** load data from environment variables */ + function reload(): void { + $ref = new ReflectionClass(get_class($this)); + + foreach ($ref->getConstants() as $const => $cvalue) { + if (isset(self::_DEFAULTS[$const])) { + $override = getenv(self::_ENVVAR_PREFIX . $const); + + list ($defval, $deftype) = self::_DEFAULTS[$const]; + + $this->params[$cvalue] = [ self::cast_to($override !== false ? $override : $defval, $deftype), $deftype ]; + } + } + } + + /** this is primarily needed for tests, normally global config is not changed at runtime */ + static function set(string $key, mixed $value): bool { + if (!getenv('IS_INTEGRATION_TESTING') && !getenv('IS_TESTING')) { + user_error("This method is only available for tests.", E_USER_WARNING); + + return false; + } if (array_key_exists($key, self::_DEFAULTS)) { + list ($defval, $deftype) = self::_DEFAULTS[$key]; + + self::get_instance()->params[$key] = [self::cast_to($value, $deftype), $deftype]; + + return true; + } else { + user_error("Requested to set unknown global config variable: {$key}", E_USER_WARNING); + + return false; + } + } + + function __construct() { + $this->reload(); + } + + /* package maintainers who don't use git: if version_static.txt exists in tt-rss root + directory, its contents are displayed instead of git commit-based version, this could be generated + based on source git tree commit used when creating the package */ + + /** @return string|array<string, mixed> */ + static function get_version(bool $as_string = true) : mixed { + return self::get_instance()->_get_version($as_string); + } + + /** @return string|array<string, mixed> */ + private function _get_version(bool $as_string = true) : mixed { + $root_dir = dirname(__DIR__); + + if (empty($this->version)) { + $this->version["status"] = -1; + + if (getenv("CI_COMMIT_SHORT_SHA") && getenv("CI_COMMIT_TIMESTAMP")) { + + $this->version["branch"] = getenv("CI_COMMIT_BRANCH"); + $this->version["timestamp"] = strtotime(getenv("CI_COMMIT_TIMESTAMP")); + $this->version["version"] = sprintf("%s-%s", date("y.m", $this->version["timestamp"]), getenv("CI_COMMIT_SHORT_SHA")); + $this->version["commit"] = getenv("CI_COMMIT_SHORT_SHA"); + $this->version["status"] = 0; + + } else if (PHP_OS === "Darwin") { + $this->version["version"] = "UNKNOWN (Unsupported, Darwin)"; + } else if (file_exists("$root_dir/version_static.txt")) { + $this->version["version"] = trim(file_get_contents("$root_dir/version_static.txt")) . " (Unsupported)"; + } else if (is_dir("$root_dir/.git")) { + $this->version = self::get_version_from_git($root_dir); + + if ($this->version["status"] != 0) { + user_error("Unable to determine version: " . $this->version["version"], E_USER_WARNING); + + $this->version["version"] = "UNKNOWN (Unsupported, Git error)"; + } + } else { + $this->version["version"] = "UNKNOWN (Unsupported)"; + } + } - return $as_string ? $this->version["version"] : $this->version; - } + return $as_string ? $this->version["version"] : $this->version; + } - /** @return array<string, mixed> */ - static function get_version_from_git(string $dir) : array { - $descriptorspec = [ - 1 => ["pipe", "w"], // STDOUT - 2 => ["pipe", "w"], // STDERR - ]; + /** @return array<string, mixed> */ + static function get_version_from_git(string $dir) : array { + $descriptorspec = [ + 1 => ["pipe", "w"], // STDOUT + 2 => ["pipe", "w"], // STDERR + ]; - $rv = [ - "status" => -1, - "version" => "", - "commit" => "", - "branch" => "", - "timestamp" => 0, - ]; - - $proc = proc_open("git --no-pager log --pretty=\"version-%ct-%h\" -n1 HEAD", - $descriptorspec, $pipes, $dir); + $rv = [ + "status" => -1, + "version" => "", + "commit" => "", + "branch" => "", + "timestamp" => 0, + ]; + + $proc = proc_open("git --no-pager log --pretty=\"version-%ct-%h\" -n1 HEAD", + $descriptorspec, $pipes, $dir); - if (is_resource($proc)) { - $stdout = trim(stream_get_contents($pipes[1])); - $stderr = trim(stream_get_contents($pipes[2])); - $status = proc_close($proc); - - $rv["status"] = $status; - - list($check, $timestamp, $commit) = explode("-", $stdout); - - if ($check == "version") { - - $rv["version"] = strftime("%y.%m", (int)$timestamp) . "-$commit"; - $rv["commit"] = $commit; - $rv["timestamp"] = $timestamp; - - // proc_close() may return -1 even if command completed successfully - // so if it looks like we got valid data, we ignore it - - if ($rv["status"] == -1) - $rv["status"] = 0; + if (is_resource($proc)) { + $stdout = trim(stream_get_contents($pipes[1])); + $stderr = trim(stream_get_contents($pipes[2])); + $status = proc_close($proc); + + $rv["status"] = $status; + + list($check, $timestamp, $commit) = explode("-", $stdout); + + if ($check == "version") { + + $rv["version"] = strftime("%y.%m", (int)$timestamp) . "-$commit"; + $rv["commit"] = $commit; + $rv["timestamp"] = $timestamp; + + // proc_close() may return -1 even if command completed successfully + // so if it looks like we got valid data, we ignore it + + if ($rv["status"] == -1) + $rv["status"] = 0; - } else { - $rv["version"] = T_sprintf("Git error [RC=%d]: %s", $status, $stderr); - } - } + } else { + $rv["version"] = T_sprintf("Git error [RC=%d]: %s", $status, $stderr); + } + } - return $rv; - } + return $rv; + } - static function get_migrations() : Db_Migrations { - return self::get_instance()->_get_migrations(); - } - - private function _get_migrations() : Db_Migrations { - if (empty($this->migrations)) { - $this->migrations = new Db_Migrations(); - $this->migrations->initialize(dirname(__DIR__) . "/sql", "epube_migrations", true); - } - - return $this->migrations; - } - - static function is_migration_needed() : bool { - return self::get_migrations()->is_migration_needed(); - } - - static function get_schema_version() : int { - return self::get_migrations()->get_version(); - } - - /** @return int|bool|string */ - static function cast_to(string $value, int $type_hint) : mixed { - switch ($type_hint) { - case self::T_BOOL: - return sql_bool_to_bool($value); - case self::T_INT: - return (int) $value; - default: - return $value; - } - } - - /** @return int|bool|string */ - private function _get(string $param) : mixed { - list ($value, $type_hint) = $this->params[$param]; - - return $this->cast_to($value, $type_hint); - } - - private function _add(string $param, string $default, int $type_hint) : void { - $override = getenv($this::_ENVVAR_PREFIX . $param); - - $this->params[$param] = [ self::cast_to(!empty($override) ? $override : $default, $type_hint), $type_hint ]; - } - - static function add(string $param, string $default, int $type_hint = Config::T_STRING) : void { - $instance = self::get_instance(); - - $instance->_add($param, $default, $type_hint); - } - - /** @return int|bool|string */ - static function get(string $param) { - $instance = self::get_instance(); - - return $instance->_get($param); - } - - static function is_server_https() : bool { - return (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] != 'off')) || - (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'); - } - - /** generates reference self_url_path (no trailing slash) */ - static function get_self_url() : string { - $proto = self::is_server_https() ? 'https' : 'http'; - $self_url_path = $proto . '://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; - - $self_url_path = preg_replace("/\w+\.php(\?.*$)?$/", "", $self_url_path); - - if (substr($self_url_path, -1) === "/") { - return substr($self_url_path, 0, -1); - } else { - return $self_url_path; - } - } - - /** also initializes Db and ORM */ - static function sanity_check() : void { - - /* - we don't actually need the DB object right now but some checks below might use ORM which won't be initialized - because it is set up in the Db constructor, which is why it's a good idea to invoke it as early as possible - - it is a bit of a hack, maybe ORM should be initialized somewhere else (functions.php?) - */ - - $pdo = Db::pdo(); - - $errors = []; - - if (function_exists('posix_getuid') && posix_getuid() == 0) { - array_push($errors, "Please don't run this script as root."); - } - - if (version_compare(PHP_VERSION, '7.1.0', '<')) { - array_push($errors, "PHP version 7.1.0 or newer required. You're using " . PHP_VERSION . "."); - } - - if (php_sapi_name() != "cli") { - if (self::get_schema_version() < 0) { - array_push($errors, "Base database schema is missing. Either load it manually or perform a migration (<code>update.php --update-schema</code>)"); - } - } - - if (count($errors) > 0 && php_sapi_name() != "cli") { ?> - <!DOCTYPE html> - <html> - <head> - <meta name="viewport" content="width=device-width, initial-scale=1.0"> - <link href="lib/bootstrap/v3/css/bootstrap.min.css" rel="stylesheet" media="screen"> - <link href="lib/bootstrap/v3/css/bootstrap-theme.min.css" rel="stylesheet" media="screen"> - <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> - <script src="dist/app-libs.min.js"></script> - <title>Startup failed</title> - <link type="text/css" rel="stylesheet" media="screen" href="dist/app.min.css" /> - <link rel="shortcut icon" type="image/png" href="img/favicon.png" /> - <link rel="manifest" href="manifest.json"> - <meta name="mobile-web-app-capable" content="yes"> - <script type="text/javascript"> - $(document).ready(function() { - /* global EpubeApp */ - - if (typeof EpubeApp != "undefined") { - EpubeApp.setPage("PAGE_LOGIN"); - } - }); - </script> - </head> - <body class="epube-sanity-failed"> - <div class="container"> - <h1>Startup failed</h1> - - <p>Please fix errors indicated by the following messages:</p> - - <?php foreach ($errors as $error) { echo self::format_error($error); } ?> - - </div> - </body> - </html> - - <?php - die; - } else if (count($errors) > 0) { - echo "Please fix errors indicated by the following messages:\n\n"; - - foreach ($errors as $error) { - echo " * " . strip_tags($error)."\n"; - } - - exit(1); - } - } - - private static function format_error(string $msg) : string { - return "<div class=\"alert alert-danger\">$msg</div>"; - } + static function get_migrations() : Db_Migrations { + return self::get_instance()->_get_migrations(); + } + + private function _get_migrations() : Db_Migrations { + if (empty($this->migrations)) { + $this->migrations = new Db_Migrations(); + $this->migrations->initialize(dirname(__DIR__) . "/sql", "epube_migrations", true); + } + + return $this->migrations; + } + + static function is_migration_needed() : bool { + return self::get_migrations()->is_migration_needed(); + } + + static function get_schema_version() : int { + return self::get_migrations()->get_version(); + } + + /** @return int|bool|string */ + static function cast_to(string $value, int $type_hint) : mixed { + switch ($type_hint) { + case self::T_BOOL: + return sql_bool_to_bool($value); + case self::T_INT: + return (int) $value; + default: + return $value; + } + } + + /** @return int|bool|string */ + private function _get(string $param) : mixed { + list ($value, $type_hint) = $this->params[$param]; + + return $this->cast_to($value, $type_hint); + } + + private function _add(string $param, string $default, int $type_hint) : void { + $override = getenv($this::_ENVVAR_PREFIX . $param); + + $this->params[$param] = [ self::cast_to(!empty($override) ? $override : $default, $type_hint), $type_hint ]; + } + + static function add(string $param, string $default, int $type_hint = Config::T_STRING) : void { + $instance = self::get_instance(); + + $instance->_add($param, $default, $type_hint); + } + + /** @return int|bool|string */ + static function get(string $param) { + $instance = self::get_instance(); + + return $instance->_get($param); + } + + static function is_server_https() : bool { + return (!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] != 'off')) || + (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'); + } + + /** generates reference self_url_path (no trailing slash) */ + static function get_self_url() : string { + $proto = self::is_server_https() ? 'https' : 'http'; + $self_url_path = $proto . '://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; + + $self_url_path = preg_replace("/\w+\.php(\?.*$)?$/", "", $self_url_path); + + if (substr($self_url_path, -1) === "/") { + return substr($self_url_path, 0, -1); + } else { + return $self_url_path; + } + } + + /** also initializes Db and ORM */ + static function sanity_check() : void { + + /* + we don't actually need the DB object right now but some checks below might use ORM which won't be initialized + because it is set up in the Db constructor, which is why it's a good idea to invoke it as early as possible + + it is a bit of a hack, maybe ORM should be initialized somewhere else (functions.php?) + */ + + $pdo = Db::pdo(); + + $errors = []; + + if (function_exists('posix_getuid') && posix_getuid() == 0) { + array_push($errors, "Please don't run this script as root."); + } + + if (version_compare(PHP_VERSION, '7.1.0', '<')) { + array_push($errors, "PHP version 7.1.0 or newer required. You're using " . PHP_VERSION . "."); + } + + if (php_sapi_name() != "cli") { + if (self::get_schema_version() < 0) { + array_push($errors, "Base database schema is missing. Either load it manually or perform a migration (<code>update.php --update-schema</code>)"); + } + } + + if (count($errors) > 0 && php_sapi_name() != "cli") { ?> + <!DOCTYPE html> + <html> + <head> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <link href="lib/bootstrap/v3/css/bootstrap.min.css" rel="stylesheet" media="screen"> + <link href="lib/bootstrap/v3/css/bootstrap-theme.min.css" rel="stylesheet" media="screen"> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> + <script src="dist/app-libs.min.js"></script> + <title>Startup failed</title> + <link type="text/css" rel="stylesheet" media="screen" href="dist/app.min.css" /> + <link rel="shortcut icon" type="image/png" href="img/favicon.png" /> + <link rel="manifest" href="manifest.json"> + <meta name="mobile-web-app-capable" content="yes"> + <script type="text/javascript"> + $(document).ready(function() { + /* global EpubeApp */ + + if (typeof EpubeApp != "undefined") { + EpubeApp.setPage("PAGE_LOGIN"); + } + }); + </script> + </head> + <body class="epube-sanity-failed"> + <div class="container"> + <h1>Startup failed</h1> + + <p>Please fix errors indicated by the following messages:</p> + + <?php foreach ($errors as $error) { echo self::format_error($error); } ?> + + </div> + </body> + </html> + + <?php + die; + } else if (count($errors) > 0) { + echo "Please fix errors indicated by the following messages:\n\n"; + + foreach ($errors as $error) { + echo " * " . strip_tags($error)."\n"; + } + + exit(1); + } + } + + private static function format_error(string $msg) : string { + return "<div class=\"alert alert-danger\">$msg</div>"; + } } |
