diff options
author | Andrew Dolgov <[email protected]> | 2021-02-23 08:34:37 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2021-02-23 08:34:37 +0300 |
commit | 5229cc58b269bd04b2be7768107697063d95736e (patch) | |
tree | 6520dfaf81ec8eb818c4b66f3f348024466c6e00 /include | |
parent | 4ed91619ddefcaa2bf758361aaccf0844465228b (diff) | |
parent | cae54dad564bc1372f0f6cc11101b6377caef9a9 (diff) |
Merge branch 'wip-config-object'
Diffstat (limited to 'include')
-rw-r--r-- | include/autoload.php | 2 | ||||
-rw-r--r-- | include/db-prefs.php | 8 | ||||
-rw-r--r-- | include/functions.php | 87 | ||||
-rwxr-xr-x | include/login_form.php | 10 | ||||
-rwxr-xr-x | include/sanity_check.php | 58 | ||||
-rw-r--r-- | include/sanity_config.php | 3 | ||||
-rw-r--r-- | include/sessions.php | 8 |
7 files changed, 54 insertions, 122 deletions
diff --git a/include/autoload.php b/include/autoload.php index c02923dba..19e00b9ea 100644 --- a/include/autoload.php +++ b/include/autoload.php @@ -1,6 +1,4 @@ <?php - require_once "functions.php"; - spl_autoload_register(function($class) { $namespace = ''; $class_name = $class; diff --git a/include/db-prefs.php b/include/db-prefs.php deleted file mode 100644 index ce5753638..000000000 --- a/include/db-prefs.php +++ /dev/null @@ -1,8 +0,0 @@ -<?php - function get_pref($pref_name, $user_id = false, $die_on_error = false) { - return Db_Prefs::get()->read($pref_name, $user_id, $die_on_error); - } - - function set_pref($pref_name, $value, $user_id = false, $strip_tags = true) { - return Db_Prefs::get()->write($pref_name, $value, $user_id, $strip_tags); - }
\ No newline at end of file diff --git a/include/functions.php b/include/functions.php index 7c4e32963..a698fa79d 100644 --- a/include/functions.php +++ b/include/functions.php @@ -1,16 +1,9 @@ <?php - define('EXPECTED_CONFIG_VERSION', 26); define('SCHEMA_VERSION', 140); define('LABEL_BASE_INDEX', -1024); define('PLUGIN_FEED_BASE_INDEX', -128); - define('COOKIE_LIFETIME_LONG', 86400*365); - - // this CSS file is included for everyone (if it exists in themes.local) - // on login, registration, and main (index and prefs) pages - define('LOCAL_OVERRIDE_STYLESHEET', '.local-overrides.css'); - $fetch_last_error = false; $fetch_last_error_code = false; $fetch_last_content_type = false; @@ -37,55 +30,26 @@ ini_set('display_errors', "false"); ini_set('display_startup_errors', "false"); - require_once 'config.php'; - - /* Some tunables you can override in config.php using define(): */ - - if (!defined('FEED_FETCH_TIMEOUT')) define('FEED_FETCH_TIMEOUT', 45); - // How may seconds to wait for response when requesting feed from a site - if (!defined('FEED_FETCH_NO_CACHE_TIMEOUT')) define('FEED_FETCH_NO_CACHE_TIMEOUT', 15); - // How may seconds to wait for response when requesting feed from a - // site when that feed wasn't cached before - if (!defined('FILE_FETCH_TIMEOUT')) define('FILE_FETCH_TIMEOUT', 45); - // Default timeout when fetching files from remote sites - if (!defined('FILE_FETCH_CONNECT_TIMEOUT')) define('FILE_FETCH_CONNECT_TIMEOUT', 15); - // How many seconds to wait for initial response from website when - // fetching files from remote sites - if (!defined('DAEMON_UPDATE_LOGIN_LIMIT')) define('DAEMON_UPDATE_LOGIN_LIMIT', 30); - // stop updating feeds if users haven't logged in for X days - if (!defined('DAEMON_FEED_LIMIT')) define('DAEMON_FEED_LIMIT', 500); - // feed limit for one update batch - if (!defined('DAEMON_SLEEP_INTERVAL')) define('DAEMON_SLEEP_INTERVAL', 120); - // default sleep interval between feed updates (sec) - if (!defined('MAX_CACHE_FILE_SIZE')) define('MAX_CACHE_FILE_SIZE', 64*1024*1024); - // do not cache files larger than that (bytes) - if (!defined('MAX_DOWNLOAD_FILE_SIZE')) define('MAX_DOWNLOAD_FILE_SIZE', 16*1024*1024); - // do not download general files larger than that (bytes) - if (!defined('MAX_FAVICON_FILE_SIZE')) define('MAX_FAVICON_FILE_SIZE', 1*1024*1024); - // do not download favicon files larger than that (bytes) - if (!defined('CACHE_MAX_DAYS')) define('CACHE_MAX_DAYS', 7); - // max age in days for various automatically cached (temporary) files - if (!defined('MAX_CONDITIONAL_INTERVAL')) define('MAX_CONDITIONAL_INTERVAL', 3600*12); - // max interval between forced unconditional updates for servers - // not complying with http if-modified-since (seconds) - // if (!defined('MAX_FETCH_REQUESTS_PER_HOST')) define('MAX_FETCH_REQUESTS_PER_HOST', 25); - // a maximum amount of allowed HTTP requests per destination host - // during a single update (i.e. within PHP process lifetime) - // this is used to not cause excessive load on the origin server on - // e.g. feed subscription when all articles are being processes - // (not implemented) - if (!defined('DAEMON_UNSUCCESSFUL_DAYS_LIMIT')) define('DAEMON_UNSUCCESSFUL_DAYS_LIMIT', 30); - // automatically disable updates for feeds which failed to - // update for this amount of days; 0 disables - - /* tunables end here */ - - if (DB_TYPE == "pgsql") { + // config.php is optional + if (stream_resolve_include_path("config.php")) + require_once "config.php"; + + require_once "autoload.php"; + + if (Config::get(Config::DB_TYPE) == "pgsql") { define('SUBSTRING_FOR_DATE', 'SUBSTRING_FOR_DATE'); } else { define('SUBSTRING_FOR_DATE', 'SUBSTRING'); } + function get_pref($pref_name, $user_id = false, $die_on_error = false) { + return Db_Prefs::get()->read($pref_name, $user_id, $die_on_error); + } + + function set_pref($pref_name, $value, $user_id = false, $strip_tags = true) { + return Db_Prefs::get()->write($pref_name, $value, $user_id, $strip_tags); + } + function get_translations() { $t = array( "auto" => __("Detect automatically"), @@ -196,7 +160,6 @@ } } - require_once 'db-prefs.php'; require_once 'controls.php'; require_once 'controls_compat.php'; @@ -375,9 +338,9 @@ } function file_is_locked($filename) { - if (file_exists(LOCK_DIRECTORY . "/$filename")) { + if (file_exists(Config::get(Config::LOCK_DIRECTORY) . "/$filename")) { if (function_exists('flock')) { - $fp = @fopen(LOCK_DIRECTORY . "/$filename", "r"); + $fp = @fopen(Config::get(Config::LOCK_DIRECTORY) . "/$filename", "r"); if ($fp) { if (flock($fp, LOCK_EX | LOCK_NB)) { flock($fp, LOCK_UN); @@ -397,11 +360,11 @@ } function make_lockfile($filename) { - $fp = fopen(LOCK_DIRECTORY . "/$filename", "w"); + $fp = fopen(Config::get(Config::LOCK_DIRECTORY) . "/$filename", "w"); if ($fp && flock($fp, LOCK_EX | LOCK_NB)) { $stat_h = fstat($fp); - $stat_f = stat(LOCK_DIRECTORY . "/$filename"); + $stat_f = stat(Config::get(Config::LOCK_DIRECTORY) . "/$filename"); if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { if ($stat_h["ino"] != $stat_f["ino"] || @@ -444,15 +407,15 @@ } function is_prefix_https() { - return parse_url(SELF_URL_PATH, PHP_URL_SCHEME) == 'https'; + return parse_url(Config::get(Config::SELF_URL_PATH), PHP_URL_SCHEME) == 'https'; } - // this returns SELF_URL_PATH sans ending slash + // this returns Config::get(Config::SELF_URL_PATH) sans ending slash function get_self_url_prefix() { - if (strrpos(SELF_URL_PATH, "/") === strlen(SELF_URL_PATH)-1) { - return substr(SELF_URL_PATH, 0, strlen(SELF_URL_PATH)-1); + if (strrpos(Config::get(Config::SELF_URL_PATH), "/") === strlen(Config::get(Config::SELF_URL_PATH))-1) { + return substr(Config::get(Config::SELF_URL_PATH), 0, strlen(Config::get(Config::SELF_URL_PATH))-1); } else { - return SELF_URL_PATH; + return Config::get(Config::SELF_URL_PATH); } } @@ -467,7 +430,7 @@ } // function encrypt_password function init_plugins() { - PluginHost::getInstance()->load(PLUGINS, PluginHost::KIND_ALL); + PluginHost::getInstance()->load(Config::get(Config::PLUGINS), PluginHost::KIND_ALL); return true; } diff --git a/include/login_form.php b/include/login_form.php index 211302a87..168fe50aa 100755 --- a/include/login_form.php +++ b/include/login_form.php @@ -16,8 +16,8 @@ } ?> - <?php if (theme_exists(LOCAL_OVERRIDE_STYLESHEET)) { - echo stylesheet_tag(get_theme_path(LOCAL_OVERRIDE_STYLESHEET)); + <?php if (theme_exists(Config::get(Config::LOCAL_OVERRIDE_STYLESHEET))) { + echo stylesheet_tag(get_theme_path(Config::get(Config::LOCAL_OVERRIDE_STYLESHEET))); } ?> <style type="text/css"> @@ -79,7 +79,7 @@ }, bwLimitChange: function(elem) { Cookie.set("ttrss_bwlimit", elem.checked, - <?php print SESSION_COOKIE_LIFETIME ?>); + <?php print Config::get(Config::SESSION_COOKIE_LIFETIME) ?>); } }; @@ -122,7 +122,7 @@ onblur="UtilityApp.fetchProfiles()" value="<?= $_SESSION["fake_password"] ?? "" ?>"/> </fieldset> - <?php if (strpos(PLUGINS, "auth_internal") !== false) { ?> + <?php if (strpos(Config::get(Config::PLUGINS), "auth_internal") !== false) { ?> <fieldset class="align-right"> <a href="public.php?op=forgotpass"><?= __("I forgot my password") ?></a> </fieldset> @@ -161,7 +161,7 @@ <div dojoType="dijit.Tooltip" connectId="safe_mode_label" position="below" style="display:none"> <?= __("Uses default theme and prevents all plugins from loading."); ?> </div> - <?php if (SESSION_COOKIE_LIFETIME > 0) { ?> + <?php if (Config::get(Config::SESSION_COOKIE_LIFETIME) > 0) { ?> <fieldset class="narrow"> <label> </label> diff --git a/include/sanity_check.php b/include/sanity_check.php index 2786f012f..4831209ba 100755 --- a/include/sanity_check.php +++ b/include/sanity_check.php @@ -21,7 +21,7 @@ $sth = $pdo->prepare("SELECT engine, table_name FROM information_schema.tables WHERE table_schema = ? AND table_name LIKE 'ttrss_%' AND engine != 'InnoDB'"); - $sth->execute([DB_NAME]); + $sth->execute([Config::get(Config::DB_NAME)]); $bad_tables = []; @@ -44,8 +44,8 @@ array_push($errors, "Please copy config.php-dist to config.php"); } - if (strpos(PLUGINS, "auth_") === false) { - array_push($errors, "Please enable at least one authentication module via PLUGINS constant in config.php"); + if (strpos(Config::get(Config::PLUGINS), "auth_") === false) { + array_push($errors, "Please enable at least one authentication module via Config::get(Config::PLUGINS) constant in config.php"); } if (function_exists('posix_getuid') && posix_getuid() == 0) { @@ -60,43 +60,25 @@ array_push($errors, "PHP UConverter class is missing, it's provided by the Internationalization (intl) module."); } - if (CONFIG_VERSION != EXPECTED_CONFIG_VERSION) { - array_push($errors, "Configuration file (config.php) has incorrect version. Update it with new options from config.php-dist and set CONFIG_VERSION to the correct value."); + if (!is_writable(Config::get(Config::CACHE_DIR) . "/images")) { + array_push($errors, "Image cache is not writable (chmod -R 777 ".Config::get(Config::CACHE_DIR)."/images)"); } - if (!is_writable(CACHE_DIR . "/images")) { - array_push($errors, "Image cache is not writable (chmod -R 777 ".CACHE_DIR."/images)"); + if (!is_writable(Config::get(Config::CACHE_DIR) . "/upload")) { + array_push($errors, "Upload cache is not writable (chmod -R 777 ".Config::get(Config::CACHE_DIR)."/upload)"); } - if (!is_writable(CACHE_DIR . "/upload")) { - array_push($errors, "Upload cache is not writable (chmod -R 777 ".CACHE_DIR."/upload)"); + if (!is_writable(Config::get(Config::CACHE_DIR) . "/export")) { + array_push($errors, "Data export cache is not writable (chmod -R 777 ".Config::get(Config::CACHE_DIR)."/export)"); } - if (!is_writable(CACHE_DIR . "/export")) { - array_push($errors, "Data export cache is not writable (chmod -R 777 ".CACHE_DIR."/export)"); - } - - require_once "sanity_config.php"; - - if (GENERATED_CONFIG_CHECK != EXPECTED_CONFIG_VERSION) { - array_push($errors, - "Configuration option checker sanity_config.php is outdated, please recreate it using ./utils/regen_config_checks.sh"); - } - - foreach (get_required_defines() as $d) { - if (!defined($d)) { - array_push($errors, - "Required configuration file parameter $d is not defined in config.php. You might need to copy it from config.php-dist."); - } - } - - if (SINGLE_USER_MODE && class_exists("PDO")) { + if (Config::get(Config::SINGLE_USER_MODE) && class_exists("PDO")) { $pdo = Db::pdo(); $res = $pdo->query("SELECT id FROM ttrss_users WHERE id = 1"); if (!$res->fetch()) { - array_push($errors, "SINGLE_USER_MODE is enabled in config.php but default admin account is not found."); + array_push($errors, "Config::get(Config::SINGLE_USER_MODE) is enabled in config.php but default admin account is not found."); } } @@ -107,26 +89,26 @@ $ref_self_url_path = preg_replace("/\w+\.php$/", "", $ref_self_url_path); } - if (SELF_URL_PATH == "http://example.org/tt-rss/") { + if (Config::get(Config::SELF_URL_PATH) == "http://example.org/tt-rss/") { $hint = $ref_self_url_path ? "(possible value: <b>$ref_self_url_path</b>)" : ""; array_push($errors, - "Please set SELF_URL_PATH to the correct value for your server: $hint"); + "Please set Config::get(Config::SELF_URL_PATH) to the correct value for your server: $hint"); } if ($ref_self_url_path && (!defined('_SKIP_SELF_URL_PATH_CHECKS') || !_SKIP_SELF_URL_PATH_CHECKS) && - SELF_URL_PATH != $ref_self_url_path && SELF_URL_PATH != mb_substr($ref_self_url_path, 0, mb_strlen($ref_self_url_path)-1)) { + Config::get(Config::SELF_URL_PATH) != $ref_self_url_path && Config::get(Config::SELF_URL_PATH) != mb_substr($ref_self_url_path, 0, mb_strlen($ref_self_url_path)-1)) { array_push($errors, - "Please set SELF_URL_PATH to the correct value detected for your server: <b>$ref_self_url_path</b> (you're using: <b>" . SELF_URL_PATH . "</b>)"); + "Please set Config::get(Config::SELF_URL_PATH) to the correct value detected for your server: <b>$ref_self_url_path</b> (you're using: <b>" . Config::get(Config::SELF_URL_PATH) . "</b>)"); } } - if (!is_writable(ICONS_DIR)) { - array_push($errors, "ICONS_DIR defined in config.php is not writable (chmod -R 777 ".ICONS_DIR.").\n"); + if (!is_writable(Config::get(Config::ICONS_DIR))) { + array_push($errors, "ICONS_DIR defined in config.php is not writable (chmod -R 777 ".Config::get(Config::ICONS_DIR).").\n"); } - if (!is_writable(LOCK_DIRECTORY)) { - array_push($errors, "LOCK_DIRECTORY defined in config.php is not writable (chmod -R 777 ".LOCK_DIRECTORY.").\n"); + if (!is_writable(Config::get(Config::LOCK_DIRECTORY))) { + array_push($errors, "Config::get(Config::LOCK_DIRECTORY) defined in config.php is not writable (chmod -R 777 ".Config::get(Config::LOCK_DIRECTORY).").\n"); } if (!function_exists("curl_init") && !ini_get("allow_url_fopen")) { @@ -161,7 +143,7 @@ array_push($errors, "PHP support for DOMDocument is required, but was not found."); } - if (DB_TYPE == "mysql") { + if (Config::get(Config::DB_TYPE) == "mysql") { $bad_tables = check_mysql_tables(); if (count($bad_tables) > 0) { diff --git a/include/sanity_config.php b/include/sanity_config.php deleted file mode 100644 index b304adf54..000000000 --- a/include/sanity_config.php +++ /dev/null @@ -1,3 +0,0 @@ -<?php # This file has been generated at: Mon Feb 22 14:17:27 MSK 2021 -define('GENERATED_CONFIG_CHECK', 26); -function get_required_defines() { return [ 'DB_TYPE', 'DB_HOST', 'DB_USER', 'DB_NAME', 'DB_PASS', 'MYSQL_CHARSET', 'SELF_URL_PATH', 'SINGLE_USER_MODE', 'SIMPLE_UPDATE_MODE', 'PHP_EXECUTABLE', 'LOCK_DIRECTORY', 'CACHE_DIR', 'ICONS_DIR', 'ICONS_URL', 'AUTH_AUTO_CREATE', 'AUTH_AUTO_LOGIN', 'FORCE_ARTICLE_PURGE', 'SESSION_COOKIE_LIFETIME', 'SMTP_FROM_NAME', 'SMTP_FROM_ADDRESS', 'DIGEST_SUBJECT', 'CHECK_FOR_UPDATES', 'PLUGINS', 'LOG_DESTINATION', 'CONFIG_VERSION']; } diff --git a/include/sessions.php b/include/sessions.php index 5b372612b..8b7de0ac7 100644 --- a/include/sessions.php +++ b/include/sessions.php @@ -3,12 +3,12 @@ // Original from http://www.daniweb.com/code/snippet43.html - require_once "config.php"; require_once "autoload.php"; + require_once "functions.php"; require_once "errorhandler.php"; require_once "lib/gettext/gettext.inc.php"; - $session_expire = min(2147483647 - time() - 1, max(SESSION_COOKIE_LIFETIME, 86400)); + $session_expire = min(2147483647 - time() - 1, max(\Config::get(\Config::SESSION_COOKIE_LIFETIME), 86400)); $session_name = (!defined('TTRSS_SESSION_NAME')) ? "ttrss_sid" : TTRSS_SESSION_NAME; if (is_server_https()) { @@ -37,7 +37,7 @@ } function validate_session() { - if (SINGLE_USER_MODE) return true; + if (\Config::get(\Config::SINGLE_USER_MODE)) return true; if (isset($_SESSION["ref_schema_version"]) && $_SESSION["ref_schema_version"] != session_get_schema_version()) { $_SESSION["login_error_msg"] = @@ -144,7 +144,7 @@ return true; } - if (!SINGLE_USER_MODE /* && DB_TYPE == "pgsql" */) { + if (!\Config::get(\Config::SINGLE_USER_MODE)) { session_set_save_handler('\Sessions\ttrss_open', '\Sessions\ttrss_close', '\Sessions\ttrss_read', '\Sessions\ttrss_write', '\Sessions\ttrss_destroy', |