From 2b8b845abe7c13ecbb266613910484310cffe8e1 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 5 Mar 2021 21:14:35 +0300 Subject: * use ORM for trivial queries * environment-based configuration * useradm.php -> update.php with new options * support for schema migrations * various fixes --- .eslintrc.js | 26 +- .gitignore | 13 +- .vscode/launch.json | 24 + backend.php | 215 ++- classes/config.php | 404 +++++ classes/db.php | 33 + classes/db/migrations.php | 189 +++ classes/debug.php | 91 + common.php | 26 - composer.json | 5 + composer.lock | 85 + config.php-dist | 12 - css/app.less | 5 + db.php | 26 - dist/app.min.css | 2 +- include/autoload.php | 16 + include/common.php | 34 + include/sessions.php | 48 + index.php | 45 +- login.php | 135 +- logout.php | 12 +- phpstan.neon | 17 + schema.sql | 39 - sessions.php | 35 - sql/sqlite/schema.sql | 23 + update.php | 121 ++ useradm.php | 82 - vendor/autoload.php | 7 + vendor/composer/ClassLoader.php | 479 ++++++ vendor/composer/InstalledVersions.php | 292 ++++ vendor/composer/LICENSE | 21 + vendor/composer/autoload_classmap.php | 15 + vendor/composer/autoload_namespaces.php | 9 + vendor/composer/autoload_psr4.php | 9 + vendor/composer/autoload_real.php | 57 + vendor/composer/autoload_static.php | 25 + vendor/composer/installed.json | 75 + vendor/composer/installed.php | 33 + vendor/composer/platform_check.php | 26 + vendor/j4mie/idiorm/CONTRIBUTING.md | 16 + vendor/j4mie/idiorm/README.markdown | 239 +++ vendor/j4mie/idiorm/composer.json | 49 + vendor/j4mie/idiorm/demo.php | 81 + vendor/j4mie/idiorm/docs/Makefile | 153 ++ vendor/j4mie/idiorm/docs/conf.py | 242 +++ vendor/j4mie/idiorm/docs/configuration.rst | 378 +++++ vendor/j4mie/idiorm/docs/connections.rst | 80 + vendor/j4mie/idiorm/docs/index.rst | 29 + vendor/j4mie/idiorm/docs/installation.rst | 19 + vendor/j4mie/idiorm/docs/make.bat | 190 +++ vendor/j4mie/idiorm/docs/models.rst | 161 ++ vendor/j4mie/idiorm/docs/philosophy.rst | 34 + vendor/j4mie/idiorm/docs/querying.rst | 896 ++++++++++ vendor/j4mie/idiorm/docs/transactions.rst | 21 + vendor/j4mie/idiorm/idiorm.php | 2539 ++++++++++++++++++++++++++++ 55 files changed, 7484 insertions(+), 454 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 classes/config.php create mode 100644 classes/db.php create mode 100644 classes/db/migrations.php create mode 100644 classes/debug.php delete mode 100644 common.php create mode 100644 composer.json create mode 100644 composer.lock delete mode 100644 config.php-dist delete mode 100644 db.php create mode 100644 include/autoload.php create mode 100644 include/common.php create mode 100644 include/sessions.php create mode 100644 phpstan.neon delete mode 100644 schema.sql delete mode 100644 sessions.php create mode 100644 sql/sqlite/schema.sql create mode 100644 update.php delete mode 100644 useradm.php create mode 100644 vendor/autoload.php create mode 100644 vendor/composer/ClassLoader.php create mode 100644 vendor/composer/InstalledVersions.php create mode 100644 vendor/composer/LICENSE create mode 100644 vendor/composer/autoload_classmap.php create mode 100644 vendor/composer/autoload_namespaces.php create mode 100644 vendor/composer/autoload_psr4.php create mode 100644 vendor/composer/autoload_real.php create mode 100644 vendor/composer/autoload_static.php create mode 100644 vendor/composer/installed.json create mode 100644 vendor/composer/installed.php create mode 100644 vendor/composer/platform_check.php create mode 100644 vendor/j4mie/idiorm/CONTRIBUTING.md create mode 100644 vendor/j4mie/idiorm/README.markdown create mode 100644 vendor/j4mie/idiorm/composer.json create mode 100644 vendor/j4mie/idiorm/demo.php create mode 100644 vendor/j4mie/idiorm/docs/Makefile create mode 100644 vendor/j4mie/idiorm/docs/conf.py create mode 100644 vendor/j4mie/idiorm/docs/configuration.rst create mode 100644 vendor/j4mie/idiorm/docs/connections.rst create mode 100644 vendor/j4mie/idiorm/docs/index.rst create mode 100644 vendor/j4mie/idiorm/docs/installation.rst create mode 100644 vendor/j4mie/idiorm/docs/make.bat create mode 100644 vendor/j4mie/idiorm/docs/models.rst create mode 100644 vendor/j4mie/idiorm/docs/philosophy.rst create mode 100644 vendor/j4mie/idiorm/docs/querying.rst create mode 100644 vendor/j4mie/idiorm/docs/transactions.rst create mode 100644 vendor/j4mie/idiorm/idiorm.php diff --git a/.eslintrc.js b/.eslintrc.js index e13969d..d2f24d6 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -2,12 +2,12 @@ module.exports = { "env": { "browser": true, "es6": true, - "jquery": true, - "webextensions": true + "jquery": false, + "webextensions": false }, "extends": "eslint:recommended", "parserOptions": { - "ecmaVersion": 2016 + "ecmaVersion": 2018 }, "rules": { "accessor-pairs": "error", @@ -59,7 +59,7 @@ module.exports = { "id-blacklist": "error", "id-length": "off", "id-match": "error", - "implicit-arrow-linebreak": "error", + "implicit-arrow-linebreak": "off", "indent": "off", "indent-legacy": "off", "init-declarations": "off", @@ -88,10 +88,10 @@ module.exports = { "max-nested-callbacks": "error", "max-params": "off", "max-statements": "off", - "max-statements-per-line": "error", + "max-statements-per-line": [ "warn", { "max" : 2 } ], "multiline-comment-style": "off", "multiline-ternary": "off", - "new-cap": "error", + "new-cap": "warn", "new-parens": "error", "newline-after-var": "off", "newline-before-return": "off", @@ -99,14 +99,14 @@ module.exports = { "no-alert": "off", "no-array-constructor": "error", "no-async-promise-executor": "off", - "no-await-in-loop": "error", + "no-await-in-loop": "warn", "no-bitwise": "off", "no-buffer-constructor": "error", "no-caller": "error", "no-catch-shadow": "off", "no-confusing-arrow": "error", "no-continue": "off", - "no-console": "off", + "no-console": "off", "no-div-regex": "error", "no-duplicate-imports": "error", "no-else-return": "off", @@ -150,7 +150,7 @@ module.exports = { "no-negated-condition": "off", "no-negated-in-lhs": "error", "no-nested-ternary": "error", - "no-new": "error", + "no-new": "warn", "no-new-func": "error", "no-new-object": "off", "no-new-require": "error", @@ -162,7 +162,7 @@ module.exports = { "no-process-env": "error", "no-process-exit": "error", "no-proto": "error", - "no-prototype-builtins": "error", + "no-prototype-builtins": "warn", "no-restricted-globals": "error", "no-restricted-imports": "error", "no-restricted-modules": "error", @@ -187,7 +187,7 @@ module.exports = { "no-trailing-spaces": "error", "no-undef-init": "error", "no-undefined": "off", - "no-undef": "warn", + "no-undef": "warn", "no-underscore-dangle": "off", "no-unmodified-loop-condition": "error", "no-unneeded-ternary": [ @@ -197,7 +197,7 @@ module.exports = { } ], "no-unused-expressions": "off", - "no-unused-vars": "warn", + "no-unused-vars": "warn", "no-use-before-define": "off", "no-useless-call": "error", "no-useless-computed-key": "error", @@ -244,7 +244,7 @@ module.exports = { "as-needed" ], "require-atomic-updates": "off", - "require-await": "error", + "require-await": "warn", "require-jsdoc": "off", "require-unicode-regexp": "off", "rest-spread-spacing": "error", diff --git a/.gitignore b/.gitignore index 8a740b2..61ae359 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,9 @@ -config.php -lib/fonts/*.ttf -db/*.db -sessions/* -/tags -/.tags* -/.gutentags +/.app_is_ready +/config.php +/lib/fonts/*.ttf +/db/*.db +/sessions/* .idea/* /node_modules +/vendor/**/.git /package-lock.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..09bacd2 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Listen for XDebug", + "type": "php", + "request": "launch", + "pathMappings": { + "/var/www/html/books": "${workspaceRoot}", + }, + "port": 9000 + }, + { + "name": "Launch Chrome", + "request": "launch", + "type": "chrome", + "pathMapping": { + "/tt-rss/": "${workspaceFolder}" + }, + "urlFilter": "*/books/*", + "runtimeExecutable": "chrome.exe", + } + ] +} diff --git a/backend.php b/backend.php index a585c10..42fc5e9 100644 --- a/backend.php +++ b/backend.php @@ -1,39 +1,42 @@ prepare("SELECT has_cover, path FROM books WHERE id = ?"); $sth->execute([$id]); while ($line = $sth->fetch()) { - $filename = BOOKS_DIR . "/" . $line["path"] . "/" . "cover.jpg"; + $filename = Config::get(Config::BOOKS_DIR) . "/" . $line["path"] . "/" . "cover.jpg"; if (file_exists($filename)) { $base_filename = basename($filename); @@ -50,17 +53,18 @@ echo "File not found."; } } - break; + case "getowner": print json_encode(["owner" => $owner]); break; + case "getinfo": $id = (int) $_REQUEST["id"]; - $db = new PDO('sqlite:' . CALIBRE_DB); + $caldb = new PDO('sqlite:' . Config::get(Config::CALIBRE_DB)); - $sth = $db->prepare("SELECT books.*, s.name AS series_name, + $sth = $caldb->prepare("SELECT books.*, s.name AS series_name, (SELECT text FROM comments WHERE book = books.id) AS comment, (SELECT id FROM data WHERE book = books.id AND format = 'EPUB' LIMIT 1) AS epub_id FROM books LEFT JOIN books_series_link AS bsl ON (bsl.book = books.id) @@ -68,49 +72,49 @@ WHERE books.id = ?"); $sth->execute([$id]); - if ($line = $sth->fetch()) { - print json_encode($line); + if ($row = $sth->fetch()) { + print json_encode($row); + } else { + header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); + echo "Not found."; } - break; case "togglefav": - $id = (int) $_REQUEST["id"]; - - $sth = $ldb->prepare("SELECT id FROM epube_favorites WHERE bookid = ? - AND owner = ? LIMIT 1"); - $sth->execute([$id, $owner]); + $bookid = (int) $_REQUEST["id"]; - $found_id = false; - $status = -1; + $fav = ORM::for_table('epube_favorites') + ->where('bookid', $bookid) + ->where('owner', $owner) + ->find_one(); - while ($line = $sth->fetch()) { - $found_id = $line["id"]; - } - - if ($found_id) { - $sth = $ldb->prepare("DELETE FROM epube_favorites WHERE id = ?"); - $sth->execute([$found_id]); + if ($fav) { + $fav->delete(); $status = 0; } else { - $sth = $ldb->prepare("INSERT INTO epube_favorites (bookid, owner) VALUES (?, ?)"); - $sth->execute([$id, $owner]); + $fav = ORM::for_table('epube_favorites') + ->create(); + + $fav->bookid = $bookid; + $fav->owner = $owner; + $fav->save(); $status = 1; } - print json_encode(["id" => $id, "status" => $status]); + print json_encode(["id" => $bookid, "status" => $status]); + break; case "download": - $id = (int) $_REQUEST["id"]; + $bookid = (int) $_REQUEST["id"]; - $db = new PDO('sqlite:' . CALIBRE_DB); - $sth = $db->prepare("SELECT path, name, format FROM data LEFT JOIN books ON (data.book = books.id) WHERE data.id = ?"); - $sth->execute([$id]); + $caldb = new PDO('sqlite:' . Config::get(Config::CALIBRE_DB)); + $sth = $caldb->prepare("SELECT path, name, format FROM data LEFT JOIN books ON (data.book = books.id) WHERE data.id = ?"); + $sth->execute([$bookid]); - while ($line = $sth->fetch()) { - $filename = BOOKS_DIR . "/" . $line["path"] . "/" . $line["name"] . "." . strtolower($line["format"]); + while ($row = $sth->fetch()) { + $filename = Config::get(Config::BOOKS_DIR) . "/" . $row["path"] . "/" . $row["name"] . "." . strtolower($row["format"]); if (file_exists($filename)) { $base_filename = basename($filename); @@ -124,54 +128,49 @@ echo "File not found."; } } - break; case "getpagination": $bookid = (int) $_REQUEST["id"]; if ($bookid) { - $sth = $ldb->prepare("SELECT pagination FROM epube_pagination WHERE bookid = ? LIMIT 1"); - $sth->execute([$bookid]); + $pag = ORM::for_table('epube_pagination') + ->where('bookid', $bookid) + ->find_one(); - if ($line = $sth->fetch()) { - print $line["pagination"]; + if ($pag) { + print $pag->pagination; } else { header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); echo "File not found."; } } - break; + case "storepagination": $bookid = (int) $_REQUEST["id"]; $payload = $_REQUEST["payload"]; - $total_pages = (int) $_REQUEST["total"]; - if ($bookid && $payload && $total_pages) { + if ($bookid && $payload) { - $ldb->beginTransaction(); + $pag = ORM::for_table('epube_pagination') + ->where('bookid', $bookid) + ->find_one(); - $sth = $ldb->prepare("SELECT id FROM epube_pagination WHERE bookid = ? LIMIT 1"); - $sth->execute([$bookid]); + if (!$pag) { + $pag = ORM::for_table('epube_pagination') + ->create(); - if ($line = $sth->fetch()) { - $id = $line["id"]; - - $sth = $ldb->prepare("UPDATE epube_pagination SET pagination = ?, - total_pages = ? WHERE id = ?"); - $sth->execute([$payload, $total_pages, $id]); - - } else { - $sth = $ldb->prepare("INSERT INTO epube_pagination (bookid, pagination, total_pages) VALUES - (?, ?, ?)"); - $sth->execute([$bookid, $payload, $total_pages]); + $pag->bookid = $bookid; } - $ldb->commit(); - } + $pag->pagination = $payload; + $pag->total_pages = 100; + $pag->save(); + } break; + case "getlastread": $bookid = (int) $_REQUEST["id"]; $lastread = 0; @@ -180,19 +179,22 @@ if ($bookid) { - $sth = $ldb->prepare("SELECT b.lastread, b.lastcfi, b.lastts FROM epube_books AS b, epube_pagination AS p - WHERE b.bookid = p.bookid AND b.bookid = ? AND b.owner = ? LIMIT 1"); - $sth->execute([$bookid, $owner]); - - if ($line = $sth->fetch()) { - $lastread = (int) $line["lastread"]; - $lastcfi = $line["lastcfi"]; - $lastts = (int) $line["lastts"]; + $book = ORM::for_table('epube_books') + ->where('bookid', $bookid) + ->where('owner', $owner) + ->find_one(); + + if ($book) { + print json_encode([ + "page" => (int)$book->lastread, + "cfi" => $book->lastcfi, + "total" => 100, + "timestamp" => (int)$book->lastts]); + } else { + header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); + echo "Not found."; } } - - print json_encode(["page" => $lastread, "cfi" => $lastcfi, "total" => 100, "timestamp" => $lastts]); - break; case "storelastread": @@ -203,36 +205,35 @@ if ($bookid) { - $ldb->beginTransaction(); - - $sth = $ldb->prepare("SELECT id, lastread, lastcfi, lastts FROM epube_books - WHERE bookid = ? AND owner = ? LIMIT 1"); - $sth->execute([$bookid, $owner]); - - if ($line = $sth->fetch()) { - $id = $line["id"]; - $last_timestamp = (int) $line["lastts"]; - $last_page = (int) $line["lastread"]; - - if (($timestamp >= $last_timestamp) && ($page >= $last_page || $page == PAGE_RESET_PROGRESS)) { - - if ($page == PAGE_RESET_PROGRESS) - $page = 0; - - $sth = $ldb->prepare("UPDATE epube_books SET lastread = ?, lastcfi = ?, lastts = ? WHERE id = ?"); - $sth->execute([$page, $cfi, $timestamp, $id]); + $book = ORM::for_table('epube_books') + ->where('bookid', $bookid) + ->where('owner', $owner) + ->find_one(); + + if ($book) { + if (($timestamp >= $book->lastts) && ($page >= $book->lastread || $page == PAGE_RESET_PROGRESS)) { + $book->set([ + 'lastread' => $page, + 'lastcfi' => $cfi, + 'lastts' => $timestamp, + ]); } } else { - $sth = $ldb->prepare("INSERT INTO epube_books (bookid, owner, lastread, lastcfi, lastts) VALUES - (?, ?, ?, ?, ?)"); - $sth->execute([$bookid, $owner, $page, $cfi, $timestamp]); + $book = ORM::for_table('epube_books')->create(); + + $book->set([ + 'bookid' => $bookid, + 'owner' => $owner, + 'lastread' => $page, + 'lastcfi' => $cfi, + 'lastts' => $timestamp, + ]); } - $ldb->commit(); + $book->save(); } print json_encode(["page" => $page, "cfi" => $cfi]); - break; case "wikisearch": @@ -242,10 +243,10 @@ if ($resp = file_get_contents($url)) { print $resp; } - break; + case "define": - if (defined('DICT_ENABLED') && DICT_ENABLED) { + if (Config::get(Config::DICT_SERVER)) { function parse_dict_reply($reply) { $tmp = []; @@ -267,7 +268,7 @@ for ($i = 0; $i < 3; $i++) { $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, sprintf("dict://%s/define:%s", DICT_SERVER, $word)); + curl_setopt($ch, CURLOPT_URL, sprintf("dict://%s/define:%s", Config::get(Config::DICT_SERVER), $word)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $dict_reply = curl_exec($ch); @@ -297,13 +298,9 @@ } else { print json_encode(["result" => ["Dictionary lookups are disabled."]]); } - break; default: header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found"); echo "Method not found."; } - - -?> diff --git a/classes/config.php b/classes/config.php new file mode 100644 index 0000000..95b0ccc --- /dev/null +++ b/classes/config.php @@ -0,0 +1,404 @@ + [ "sqlite", 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 ], + ]; + + private static $instance; + + private $params = []; + private $schema_version = null; + private $version = []; + + /** @var Db_Migrations $migrations */ + private $migrations; + + public static function get_instance() : Config { + if (self::$instance == null) + self::$instance = new self(); + + return self::$instance; + } + + private function __clone() { + // + } + + function __construct() { + $ref = new ReflectionClass(get_class($this)); + + foreach ($ref->getConstants() as $const => $cvalue) { + if (isset($this::_DEFAULTS[$const])) { + $override = getenv($this::_ENVVAR_PREFIX . $const); + + list ($defval, $deftype) = $this::_DEFAULTS[$const]; + + $this->params[$cvalue] = [ self::cast_to(!empty($override) ? $override : $defval, $deftype), $deftype ]; + } + } + } + + /* 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 */ + + static function get_version(bool $as_string = true) { + return self::get_instance()->_get_version($as_string); + } + + private function _get_version(bool $as_string = true) { + $root_dir = dirname(__DIR__); + + if (empty($this->version)) { + $this->version["status"] = -1; + + if (PHP_OS === "Darwin") { + $ttrss_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; + } + + static function get_version_from_git(string $dir) { + $descriptorspec = [ + 1 => ["pipe", "w"], // STDOUT + 2 => ["pipe", "w"], // STDERR + ]; + + $rv = [ + "status" => -1, + "version" => "", + "commit" => "", + "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; + + } else { + $rv["version"] = T_sprintf("Git error [RC=%d]: %s", $status, $stderr); + } + } + + 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(); + } + + static function cast_to(string $value, int $type_hint) { + switch ($type_hint) { + case self::T_BOOL: + return sql_bool_to_bool($value); + case self::T_INT: + return (int) $value; + default: + return $value; + } + } + + private function _get(string $param) { + list ($value, $type_hint) = $this->params[$param]; + + return $this->cast_to($value, $type_hint); + } + + private function _add(string $param, string $default, int $type_hint) { + $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) { + $instance = self::get_instance(); + + return $instance->_add($param, $default, $type_hint); + } + + 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 make_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() { + + /* + 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 (strpos(self::get(Config::PLUGINS), "auth_") === false) { + array_push($errors, "Please enable at least one authentication module via PLUGINS"); + }*/ + + 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 . "."); + } + + // TODO: add some relevant stuff + + /*if (!class_exists("UConverter")) { + array_push($errors, "PHP UConverter class is missing, it's provided by the Internationalization (intl) module."); + } + + if (!is_writable(self::get(Config::CACHE_DIR) . "/images")) { + array_push($errors, "Image cache is not writable (chmod -R 777 ".self::get(Config::CACHE_DIR)."/images)"); + } + + if (!is_writable(self::get(Config::CACHE_DIR) . "/upload")) { + array_push($errors, "Upload cache is not writable (chmod -R 777 ".self::get(Config::CACHE_DIR)."/upload)"); + } + + if (!is_writable(self::get(Config::CACHE_DIR) . "/export")) { + array_push($errors, "Data export cache is not writable (chmod -R 777 ".self::get(Config::CACHE_DIR)."/export)"); + } + + if (self::get(Config::SINGLE_USER_MODE) && class_exists("PDO")) { + if (UserHelper::get_login_by_id(1) != "admin") { + array_push($errors, "SINGLE_USER_MODE is enabled but default admin account (ID: 1) is not found."); + } + }*/ + + 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 (update.php --update-schema)"); + } + +/* $ref_self_url_path = self::make_self_url(); + + if ($ref_self_url_path) { + $ref_self_url_path = preg_replace("/\w+\.php$/", "", $ref_self_url_path); + } + + if (self::get_self_url() == "http://example.org/tt-rss") { + $hint = $ref_self_url_path ? "(possible value: $ref_self_url_path)" : ""; + array_push($errors, + "Please set SELF_URL_PATH to the correct value for your server: $hint"); + } + + if (self::get_self_url() != $ref_self_url_path) { + array_push($errors, + "Please set SELF_URL_PATH to the correct value detected for your server: $ref_self_url_path (you're using: " . self::get_self_url() . ")"); + } */ + } + + /* if (!is_writable(self::get(Config::ICONS_DIR))) { + array_push($errors, "ICONS_DIR defined in config.php is not writable (chmod -R 777 ".self::get(Config::ICONS_DIR).").\n"); + } + + if (!is_writable(self::get(Config::LOCK_DIRECTORY))) { + array_push($errors, "LOCK_DIRECTORY is not writable (chmod -R 777 ".self::get(Config::LOCK_DIRECTORY).").\n"); + } + + if (!function_exists("curl_init") && !ini_get("allow_url_fopen")) { + array_push($errors, "PHP configuration option allow_url_fopen is disabled, and CURL functions are not present. Either enable allow_url_fopen or install PHP extension for CURL."); + } + + if (!function_exists("json_encode")) { + array_push($errors, "PHP support for JSON is required, but was not found."); + } + + if (!class_exists("PDO")) { + array_push($errors, "PHP support for PDO is required but was not found."); + } + + if (!function_exists("mb_strlen")) { + array_push($errors, "PHP support for mbstring functions is required but was not found."); + } + + if (!function_exists("hash")) { + array_push($errors, "PHP support for hash() function is required but was not found."); + } + + if (ini_get("safe_mode")) { + array_push($errors, "PHP safe mode setting is obsolete and not supported by tt-rss."); + } + + if (!function_exists("mime_content_type")) { + array_push($errors, "PHP function mime_content_type() is missing, try enabling fileinfo module."); + } + + if (!class_exists("DOMDocument")) { + array_push($errors, "PHP support for DOMDocument is required, but was not found."); + } + + if (self::get(Config::DB_TYPE) == "mysql") { + $bad_tables = self::check_mysql_tables(); + + if (count($bad_tables) > 0) { + $bad_tables_fmt = []; + + foreach ($bad_tables as $bt) { + array_push($bad_tables_fmt, sprintf("%s (%s)", $bt['table_name'], $bt['engine'])); + } + + $msg = "

The following tables use an unsupported MySQL engine: " . + implode(", ", $bad_tables_fmt) . ".

"; + + $msg .= "

The only supported engine on MySQL is InnoDB. MyISAM lacks functionality to run + tt-rss. + Please backup your data (via OPML) and re-import the schema before continuing.

+

WARNING: importing the schema would mean LOSS OF ALL YOUR DATA.

"; + + + array_push($errors, $msg); + } + } */ + + if (count($errors) > 0 && php_sapi_name() != "cli") { ?> + + + + + + + + + Startup failed + + + + + + + +
+

Startup failed

+ +

Please fix errors indicated by the following messages:

+ + + +
+ + + + 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($msg) { + return "
$msg
"; + } +} diff --git a/classes/db.php b/classes/db.php new file mode 100644 index 0000000..cc835b7 --- /dev/null +++ b/classes/db.php @@ -0,0 +1,33 @@ +pdo = new PDO(self::get_dsn()); + } catch (Exception $e) { + die("Unable to initialize database driver (SQLite): $e"); + } + //$this->dbh->busyTimeout(30*1000); + $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $this->pdo->query('PRAGMA journal_mode = wal;'); + + ORM::configure(self::get_dsn()); + ORM::configure('return_result_sets', true); + } + + public static function get_dsn() { + return Config::get(Config::DB_TYPE) . ':' . Config::get(Config::SCRATCH_DB); + } + + public static function pdo() : PDO { + if (self::$instance == null) + self::$instance = new self(); + + return self::$instance->pdo; + } + +}; + +?> diff --git a/classes/db/migrations.php b/classes/db/migrations.php new file mode 100644 index 0000000..495dfc6 --- /dev/null +++ b/classes/db/migrations.php @@ -0,0 +1,189 @@ +pdo = Db::pdo(); + } + + function initialize(string $root_path, string $migrations_table, bool $base_is_latest = true, int $max_version_override = 0) { + $this->base_path = "$root_path/" . Config::get(Config::DB_TYPE); + $this->migrations_path = $this->base_path . "/migrations"; + $this->migrations_table = $migrations_table; + $this->base_is_latest = $base_is_latest; + $this->max_version_override = $max_version_override; + } + + private function set_version(int $version) { + Debug::log("Updating table {$this->migrations_table} with version ${version}...", Debug::LOG_EXTENDED); + + $sth = $this->pdo->query("SELECT * FROM {$this->migrations_table}"); + + if ($res = $sth->fetch()) { + $sth = $this->pdo->prepare("UPDATE {$this->migrations_table} SET schema_version = ?"); + } else { + $sth = $this->pdo->prepare("INSERT INTO {$this->migrations_table} (schema_version) VALUES (?)"); + } + + $sth->execute([$version]); + + $this->cached_version = $version; + } + + function get_version() : int { + if (isset($this->cached_version)) + return $this->cached_version; + + try { + $sth = $this->pdo->query("SELECT * FROM {$this->migrations_table}"); + + if ($res = $sth->fetch()) { + return (int) $res['schema_version']; + } else { + return -1; + } + } catch (PDOException $e) { + $this->create_migrations_table(); + + return -1; + } + } + + private function create_migrations_table() { + $this->pdo->query("CREATE TABLE IF NOT EXISTS {$this->migrations_table} (schema_version integer not null)"); + } + + private function migrate_to(int $version) { + try { + if ($version <= $this->get_version()) { + Debug::log("Refusing to apply version $version: current version is higher", Debug::LOG_VERBOSE); + return false; + } + + if ($version == 0) + Debug::log("Loading base database schema...", Debug::LOG_VERBOSE); + else + Debug::log("Starting migration to $version...", Debug::LOG_VERBOSE); + + $lines = $this->get_lines($version); + + if (count($lines) > 0) { + // mysql doesn't support transactions for DDL statements + if (Config::get(Config::DB_TYPE) != "mysql") + $this->pdo->beginTransaction(); + + foreach ($lines as $line) { + Debug::log($line, Debug::LOG_EXTENDED); + try { + $this->pdo->query($line); + } catch (PDOException $e) { + Debug::log("Failed on line: $line", Debug::LOG_VERBOSE); + throw $e; + } + } + + if ($version == 0 && $this->base_is_latest) + $this->set_version($this->get_max_version()); + else + $this->set_version($version); + + if (Config::get(Config::DB_TYPE) != "mysql") + $this->pdo->commit(); + + Debug::log("Migration finished, current version: " . $this->get_version(), Debug::LOG_VERBOSE); + } else { + Debug::log("Migration failed: schema file is empty or missing.", Debug::LOG_VERBOSE); + } + + } catch (PDOException $e) { + Debug::log("Migration failed: " . $e->getMessage(), Debug::LOG_VERBOSE); + try { + $this->pdo->rollback(); + } catch (PDOException $ie) { + // + } + throw $e; + } + } + + function get_max_version() : int { + if ($this->max_version_override > 0) + return $this->max_version_override; + + if (isset($this->cached_max_version)) + return $this->cached_max_version; + + $migrations = glob("{$this->migrations_path}/*.sql"); + + if (count($migrations) > 0) { + natsort($migrations); + + $this->cached_max_version = (int) basename(array_pop($migrations), ".sql"); + + } else { + $this->cached_max_version = 0; + } + + return $this->cached_max_version; + } + + function is_migration_needed() : bool { + return $this->get_version() != $this->get_max_version(); + } + + function migrate() : bool { + + if ($this->get_version() == -1) { + try { + $this->migrate_to(0); + } catch (PDOException $e) { + user_error("Failed to load base schema for {$this->migrations_table}: " . $e->getMessage(), E_USER_WARNING); + return false; + } + } + + for ($i = $this->get_version() + 1; $i <= $this->get_max_version(); $i++) { + try { + $this->migrate_to($i); + } catch (PDOException $e) { + user_error("Failed to apply migration ${i} for {$this->migrations_table}: " . $e->getMessage(), E_USER_WARNING); + return false; + //throw $e; + } + } + + return !$this->is_migration_needed(); + } + + private function get_lines(int $version) : array { + if ($version > 0) + $filename = "{$this->migrations_path}/${version}.sql"; + else + $filename = "{$this->base_path}/{$this->base_filename}"; + + if (file_exists($filename)) { + $lines = array_filter(preg_split("/[\r\n]/", file_get_contents($filename)), + function ($line) { + return strlen(trim($line)) > 0 && strpos($line, "--") !== 0; + }); + + return array_filter(explode(";", implode("", $lines)), function ($line) { + return strlen(trim($line)) > 0 && !in_array(strtolower($line), ["begin", "commit"]); + }); + + } else { + user_error("Requested schema file ${filename} not found.", E_USER_ERROR); + return []; + } + } +} diff --git a/classes/debug.php b/classes/debug.php new file mode 100644 index 0000000..a0dcac3 --- /dev/null +++ b/classes/debug.php @@ -0,0 +1,91 @@ +query("SELECT id FROM epube_users LIMIT 1"); - - if (!$res) { - die("Test query failed, is schema installed? (sqlite3 " . SCRATCH_DB . "< schema.sql)"); - } - - } catch (Exception $e) { - die($e); - } - -} \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..f9759d5 --- /dev/null +++ b/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "j4mie/idiorm": "^1.5" + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..9a8c1d0 --- /dev/null +++ b/composer.lock @@ -0,0 +1,85 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "40874589866abd68fbda2924125357d3", + "packages": [ + { + "name": "j4mie/idiorm", + "version": "v1.5.7", + "source": { + "type": "git", + "url": "https://github.com/j4mie/idiorm.git", + "reference": "d23f97053ef5d0b988a02c6a71eb5c6118b2f5b4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/j4mie/idiorm/zipball/d23f97053ef5d0b988a02c6a71eb5c6118b2f5b4", + "reference": "d23f97053ef5d0b988a02c6a71eb5c6118b2f5b4", + "shasum": "" + }, + "require": { + "php": ">=5.2.0" + }, + "require-dev": { + "ext-pdo_sqlite": "*", + "phpunit/phpunit": "^4.8" + }, + "type": "library", + "autoload": { + "classmap": [ + "idiorm.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause", + "BSD-3-Clause", + "BSD-4-Clause" + ], + "authors": [ + { + "name": "Jamie Matthews", + "email": "jamie.matthews@gmail.com", + "homepage": "http://j4mie.org", + "role": "Developer" + }, + { + "name": "Simon Holywell", + "email": "treffynnon@php.net", + "homepage": "http://simonholywell.com", + "role": "Maintainer" + }, + { + "name": "Durham Hale", + "email": "me@durhamhale.com", + "homepage": "http://durhamhale.com", + "role": "Maintainer" + } + ], + "description": "A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5", + "homepage": "http://j4mie.github.com/idiormandparis", + "keywords": [ + "idiorm", + "orm", + "query builder" + ], + "support": { + "issues": "https://github.com/j4mie/idiorm/issues", + "source": "https://github.com/j4mie/idiorm" + }, + "time": "2020-04-29T00:37:09+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.0.0" +} diff --git a/config.php-dist b/config.php-dist deleted file mode 100644 index ff4c32a..0000000 --- a/config.php-dist +++ /dev/null @@ -1,12 +0,0 @@ -dbh = new PDO('sqlite:' . SCRATCH_DB); - } catch (Exception $e) { - die("Unable to initialize database driver (SQLite): $e"); - } - //$this->dbh->busyTimeout(30*1000); - $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $this->dbh->query('PRAGMA journal_mode = wal;'); - } - - public static function get() { - if (self::$instance == null) - self::$instance = new self(); - - return self::$instance->dbh; - } - -}; - -?> diff --git a/dist/app.min.css b/dist/app.min.css index b03d4cf..7896317 100644 --- a/dist/app.min.css +++ b/dist/app.min.css @@ -1 +1 @@ -body.epube-index .caption div,body.epube-reader .footer .chapter_wrapper,body.epube-reader .header .title{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}body.epube-index .display-flex{display:flex;flex-wrap:wrap}body.epube-index .separate-search{padding:8px}body.epube-index .row>div{padding-top:10px}body.epube-index .row .thumbnail{height:200px;width:140px;margin-bottom:0}@media (max-width:641px){body.epube-index .row .col-xxs-6{width:50%}}body.epube-index .row .thumbnail img{width:100%;height:100%;background-size:cover;background-position:center;background-repeat:no-repeat}body.epube-index .row .thumbnail.read img{opacity:.5;filter:grayscale(100%)}body.epube-index .epube-app-filler{height:60px;display:none}body.epube-index .in_progress{font-style:italic}body.epube-index ::selection{background:#007d71;color:#fff}body.epube-index ::-webkit-scrollbar{width:4px}body.epube-index ::-webkit-scrollbar-thumb{background-color:#007d71}body.epube-index ::-webkit-scrollbar-track{background-color:#eee}body.epube-reader .loading{position:absolute;color:#999;top:0;left:0;height:100%;width:100%;z-index:32;text-align:center;background:#fff;padding:16px;font-size:18px;display:table}body.epube-reader .loading>div{display:table-cell;vertical-align:middle}body.epube-reader .notice{width:120px;padding-left:5px}body.epube-reader .header{top:5px;left:0;width:100%;height:24px}body.epube-reader .footer{bottom:5px;left:0;width:100%;height:24px}body.epube-reader .footer,body.epube-reader .header{position:absolute;font-size:13px;display:flex;padding-left:10px;padding-right:10px;color:#999;align-items:center}body.epube-reader .header .title{margin-left:10px;flex-grow:2;text-align:center;font-weight:700}body.epube-reader .footer .chapter_wrapper{padding-right:10px;cursor:pointer}body.epube-reader .footer .spacer{flex-grow:2;position:relative;height:12px}body.epube-reader .footer .spacer .toc-bar-entry{position:absolute;background:#999;top:0;bottom:0;width:1px}body.epube-reader .footer .spacer .toc-bar-entry.current-position{background:#C33}body.epube-reader .footer .location{cursor:pointer;text-align:right}@media (max-width:539px){body.epube-reader .spacer>.toc-bar-entry{display:none}body.epube-reader .footer{width:100%}body.epube-reader .footer .chapter_wrapper,body.epube-reader .footer .location{min-width:30%;max-width:70%}}@media (min-width:540px){body.epube-reader .footer .chapter_wrapper,body.epube-reader .footer .location{min-width:25%;max-width:30%}}body.epube-reader .toolbar{text-align:right;display:flex;flex-direction:row}body.epube-reader .toolbar>*{cursor:pointer;margin:0 4px;padding:0 2px}body.epube-reader .toolbar>:last-child{margin-right:0}body.epube-reader #reader{position:absolute;top:32px;bottom:32px;left:32px;right:32px}body.epube-reader #left,body.epube-reader #right{position:absolute;top:48px;bottom:48px;width:0;z-index:10}body.epube-reader #left{left:0}body.epube-reader #right{right:0}@media (max-width:576px){body.epube-reader #reader{left:24px;right:24px}}body.epube-reader .chapter{cursor:pointer}body.epube-reader .dict_result,body.epube-reader .search_results,body.epube-reader .toc_list{max-height:300px;height:auto;overflow:auto}body.epube-reader .dict_result h1,body.epube-reader .dict_result h2,body.epube-reader .dict_result h3{font-size:14px;margin-top:0}body.epube-reader .dict_result h2{font-weight:700}body.epube-reader .search_results b.pull-right,body.epube-reader .toc_list b.pull-right{margin-right:8px;margin-left:16px}body.epube-reader .toc_sublist{margin-left:16px}body.epube-reader ::-webkit-scrollbar{width:4px}body.epube-reader ::-webkit-scrollbar-thumb{background-color:rgba(0,0,0,.2)}body.epube-reader ::-webkit-scrollbar-track{background-color:rgba(0,0,0,.1)}body.is-epube-app.epube-reader .header .toolbar,body.is-epube-app.epube-reader .header a{display:none}.theme-night-common,.theme-night-common .loading,.theme-night-common html:-webkit-full-screen{background:#222;color:#d7cbc1}.theme-night-common a,.theme-night-common a:hover{color:#00bdaa}.theme-night-common .modal{color:#333}.theme-night-common .footer .spacer .toc-bar-entry.current-position{background:#8b0000}.theme-night-common img{filter:invert(1) contrast(.73);transition:filter linear .1s}.theme-night-common img:hover{filter:none}.theme-night-common .modal-dialog .modal-content{background:#222;color:#ccc}.theme-night-common .modal-dialog .modal-content button.close{color:#fff}.theme-night-common .modal-dialog .modal-content .form-control{background:#333;color:#ccc;border-color:#555}.theme-night-common .modal-dialog .modal-footer,.theme-night-common .modal-dialog .modal-header,.theme-night-common .modal-dialog hr{border-color:#444}@media (prefers-color-scheme:dark){body.theme-default,body.theme-default .loading,body.theme-default html:-webkit-full-screen{background:#222;color:#d7cbc1}body.theme-default a,body.theme-default a:hover{color:#00bdaa}body.theme-default .modal{color:#333}body.theme-default .footer .spacer .toc-bar-entry.current-position{background:#8b0000}body.theme-default img{filter:invert(1) contrast(.73);transition:filter linear .1s}body.theme-default img:hover{filter:none}body.theme-default .modal-dialog .modal-content{background:#222;color:#ccc}body.theme-default .modal-dialog .modal-content button.close{color:#fff}body.theme-default .modal-dialog .modal-content .form-control{background:#333;color:#ccc;border-color:#555}body.theme-default .modal-dialog .modal-footer,body.theme-default .modal-dialog .modal-header,body.theme-default .modal-dialog hr{border-color:#444}}body.theme-gray,body.theme-gray .loading,body.theme-gray html:-webkit-full-screen{background:#eee;color:#424242}body.theme-gray .footer,body.theme-gray .header{color:#B85C57}body.theme-gray .footer .spacer .toc-bar-entry.current-position{background:#B85C57}body.theme-light .no-op{color:#fff}body.theme-mocca,body.theme-mocca .loading,body.theme-mocca html:-webkit-full-screen{background:#3B3228;color:#D0C8C6}body.theme-mocca a{color:#8AB3B5}body.theme-mocca a:hover{color:#7BBDA4}body.theme-mocca .footer,body.theme-mocca .header{color:#F4BC87}body.theme-mocca .footer .location{color:#BEB55B}body.theme-mocca .header button.btn{background:#BB9584;color:#534636;text-shadow:#534636 0 0;border-color:#534636}body.theme-mocca .footer .spacer .toc-bar-entry.current-position{background:#F4BC87}body.theme-night,body.theme-night .loading,body.theme-night html:-webkit-full-screen{background:#222;color:#d7cbc1}body.theme-night a,body.theme-night a:hover{color:#00bdaa}body.theme-night .modal{color:#333}body.theme-night .footer .spacer .toc-bar-entry.current-position{background:#8b0000}body.theme-night img{filter:invert(1) contrast(.73);transition:filter linear .1s}body.theme-night img:hover{filter:none}body.theme-night .modal-dialog .modal-content{background:#222;color:#ccc}body.theme-night .modal-dialog .modal-content button.close{color:#fff}body.theme-night .modal-dialog .modal-content .form-control{background:#333;color:#ccc;border-color:#555}body.theme-night .modal-dialog .modal-footer,body.theme-night .modal-dialog .modal-header,body.theme-night .modal-dialog hr{border-color:#444}body.theme-plan9,body.theme-plan9 .loading,body.theme-plan9 html:-webkit-full-screen{background:#FFFFE8;color:#424242}body.theme-plan9 a{color:#22b9b2}body.theme-plan9 a:hover{color:#28d7cf}body.theme-plan9 .footer,body.theme-plan9 .header{color:#B85C57}body.theme-plan9 .footer .spacer .toc-bar-entry.current-position{background:#B85C57}body.theme-sepia,body.theme-sepia .loading,body.theme-sepia html:-webkit-full-screen{background:#FAEFDB;color:#4a422b}body.theme-sepia .footer,body.theme-sepia .header{color:#B85C57}body.theme-sepia .footer .spacer .toc-bar-entry.current-position{background:#B85C57} \ No newline at end of file +body.epube-index .caption div,body.epube-reader .footer .chapter_wrapper,body.epube-reader .header .title{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}body.epube-index .display-flex{display:flex;flex-wrap:wrap}body.epube-index .separate-search{padding:8px}body.epube-index .row>div{padding-top:10px}body.epube-index .row .thumbnail{height:200px;width:140px;margin-bottom:0}@media (max-width:641px){body.epube-index .row .col-xxs-6{width:50%}}body.epube-index .row .thumbnail img{width:100%;height:100%;background-size:cover;background-position:center;background-repeat:no-repeat}body.epube-index .row .thumbnail.read img{opacity:.5;filter:grayscale(100%)}body.epube-index .epube-app-filler{height:60px;display:none}body.epube-index .in_progress{font-style:italic}body.epube-index ::selection{background:#007d71;color:#fff}body.epube-index ::-webkit-scrollbar{width:4px}body.epube-index ::-webkit-scrollbar-thumb{background-color:#007d71}body.epube-index ::-webkit-scrollbar-track{background-color:#eee}body.epube-reader .loading{position:absolute;color:#999;top:0;left:0;height:100%;width:100%;z-index:32;text-align:center;background:#fff;padding:16px;font-size:18px;display:table}body.epube-reader .loading>div{display:table-cell;vertical-align:middle}body.epube-reader .notice{width:120px;padding-left:5px}body.epube-reader .header{top:5px;left:0;width:100%;height:24px}body.epube-reader .footer{bottom:5px;left:0;width:100%;height:24px}body.epube-reader .footer,body.epube-reader .header{position:absolute;font-size:13px;display:flex;padding-left:10px;padding-right:10px;color:#999;align-items:center}body.epube-reader .header .title{margin-left:10px;flex-grow:2;text-align:center;font-weight:700}body.epube-reader .footer .chapter_wrapper{padding-right:10px;cursor:pointer}body.epube-reader .footer .spacer{flex-grow:2;position:relative;height:12px}body.epube-reader .footer .spacer .toc-bar-entry{position:absolute;background:#999;top:0;bottom:0;width:1px}body.epube-reader .footer .spacer .toc-bar-entry.current-position{background:#C33}body.epube-reader .footer .location{cursor:pointer;text-align:right}@media (max-width:539px){body.epube-reader .spacer>.toc-bar-entry{display:none}body.epube-reader .footer{width:100%}body.epube-reader .footer .chapter_wrapper,body.epube-reader .footer .location{min-width:30%;max-width:70%}}@media (min-width:540px){body.epube-reader .footer .chapter_wrapper,body.epube-reader .footer .location{min-width:25%;max-width:30%}}body.epube-reader .toolbar{text-align:right;display:flex;flex-direction:row}body.epube-reader .toolbar>*{cursor:pointer;margin:0 4px;padding:0 2px}body.epube-reader .toolbar>:last-child{margin-right:0}body.epube-reader #reader{position:absolute;top:32px;bottom:32px;left:32px;right:32px}body.epube-reader #left,body.epube-reader #right{position:absolute;top:48px;bottom:48px;width:0;z-index:10}body.epube-reader #left{left:0}body.epube-reader #right{right:0}@media (max-width:576px){body.epube-reader #reader{left:24px;right:24px}}body.epube-reader .chapter{cursor:pointer}body.epube-reader .dict_result,body.epube-reader .search_results,body.epube-reader .toc_list{max-height:300px;height:auto;overflow:auto}body.epube-reader .dict_result h1,body.epube-reader .dict_result h2,body.epube-reader .dict_result h3{font-size:14px;margin-top:0}body.epube-reader .dict_result h2{font-weight:700}body.epube-reader .search_results b.pull-right,body.epube-reader .toc_list b.pull-right{margin-right:8px;margin-left:16px}body.epube-reader .toc_sublist{margin-left:16px}body.epube-reader ::-webkit-scrollbar{width:4px}body.epube-reader ::-webkit-scrollbar-thumb{background-color:rgba(0,0,0,.2)}body.epube-reader ::-webkit-scrollbar-track{background-color:rgba(0,0,0,.1)}body.is-epube-app.epube-reader .header .toolbar,body.is-epube-app.epube-reader .header a{display:none}body.epube-sanity-failed{background:#900;color:#fff}.theme-night-common,.theme-night-common .loading,.theme-night-common html:-webkit-full-screen{background:#222;color:#d7cbc1}.theme-night-common a,.theme-night-common a:hover{color:#00bdaa}.theme-night-common .modal{color:#333}.theme-night-common .footer .spacer .toc-bar-entry.current-position{background:#8b0000}.theme-night-common img{filter:invert(1) contrast(.73);transition:filter linear .1s}.theme-night-common img:hover{filter:none}.theme-night-common .modal-dialog .modal-content{background:#222;color:#ccc}.theme-night-common .modal-dialog .modal-content button.close{color:#fff}.theme-night-common .modal-dialog .modal-content .form-control{background:#333;color:#ccc;border-color:#555}.theme-night-common .modal-dialog .modal-footer,.theme-night-common .modal-dialog .modal-header,.theme-night-common .modal-dialog hr{border-color:#444}@media (prefers-color-scheme:dark){body.theme-default,body.theme-default .loading,body.theme-default html:-webkit-full-screen{background:#222;color:#d7cbc1}body.theme-default a,body.theme-default a:hover{color:#00bdaa}body.theme-default .modal{color:#333}body.theme-default .footer .spacer .toc-bar-entry.current-position{background:#8b0000}body.theme-default img{filter:invert(1) contrast(.73);transition:filter linear .1s}body.theme-default img:hover{filter:none}body.theme-default .modal-dialog .modal-content{background:#222;color:#ccc}body.theme-default .modal-dialog .modal-content button.close{color:#fff}body.theme-default .modal-dialog .modal-content .form-control{background:#333;color:#ccc;border-color:#555}body.theme-default .modal-dialog .modal-footer,body.theme-default .modal-dialog .modal-header,body.theme-default .modal-dialog hr{border-color:#444}}body.theme-gray,body.theme-gray .loading,body.theme-gray html:-webkit-full-screen{background:#eee;color:#424242}body.theme-gray .footer,body.theme-gray .header{color:#B85C57}body.theme-gray .footer .spacer .toc-bar-entry.current-position{background:#B85C57}body.theme-light .no-op{color:#fff}body.theme-mocca,body.theme-mocca .loading,body.theme-mocca html:-webkit-full-screen{background:#3B3228;color:#D0C8C6}body.theme-mocca a{color:#8AB3B5}body.theme-mocca a:hover{color:#7BBDA4}body.theme-mocca .footer,body.theme-mocca .header{color:#F4BC87}body.theme-mocca .footer .location{color:#BEB55B}body.theme-mocca .header button.btn{background:#BB9584;color:#534636;text-shadow:#534636 0 0;border-color:#534636}body.theme-mocca .footer .spacer .toc-bar-entry.current-position{background:#F4BC87}body.theme-night,body.theme-night .loading,body.theme-night html:-webkit-full-screen{background:#222;color:#d7cbc1}body.theme-night a,body.theme-night a:hover{color:#00bdaa}body.theme-night .modal{color:#333}body.theme-night .footer .spacer .toc-bar-entry.current-position{background:#8b0000}body.theme-night img{filter:invert(1) contrast(.73);transition:filter linear .1s}body.theme-night img:hover{filter:none}body.theme-night .modal-dialog .modal-content{background:#222;color:#ccc}body.theme-night .modal-dialog .modal-content button.close{color:#fff}body.theme-night .modal-dialog .modal-content .form-control{background:#333;color:#ccc;border-color:#555}body.theme-night .modal-dialog .modal-footer,body.theme-night .modal-dialog .modal-header,body.theme-night .modal-dialog hr{border-color:#444}body.theme-plan9,body.theme-plan9 .loading,body.theme-plan9 html:-webkit-full-screen{background:#FFFFE8;color:#424242}body.theme-plan9 a{color:#22b9b2}body.theme-plan9 a:hover{color:#28d7cf}body.theme-plan9 .footer,body.theme-plan9 .header{color:#B85C57}body.theme-plan9 .footer .spacer .toc-bar-entry.current-position{background:#B85C57}body.theme-sepia,body.theme-sepia .loading,body.theme-sepia html:-webkit-full-screen{background:#FAEFDB;color:#4a422b}body.theme-sepia .footer,body.theme-sepia .header{color:#B85C57}body.theme-sepia .footer .spacer .toc-bar-entry.current-position{background:#B85C57} \ No newline at end of file diff --git a/include/autoload.php b/include/autoload.php new file mode 100644 index 0000000..de80241 --- /dev/null +++ b/include/autoload.php @@ -0,0 +1,16 @@ +where('user', $_SESSION['owner']) + ->find_one(); + + if ($user && sha1($user->pass) == $_SESSION['pass_hash']) { + return true; + } + } + + return false; + } + + function logout_user() { + if (session_status() == PHP_SESSION_ACTIVE) { + session_destroy(); + + if (isset($_COOKIE[session_name()])) { + setcookie(session_name(), '', time()-42000, '/'); + } + + session_commit(); + } + } + + register_shutdown_function('session_write_close'); + + if (isset($_COOKIE[session_name()])) { + if (session_status() != PHP_SESSION_ACTIVE) + session_start(); + } +?> diff --git a/index.php b/index.php index 85e19a1..b2d5fef 100644 --- a/index.php +++ b/index.php @@ -1,43 +1,29 @@ $carry ? $item : $carry; }, 0); - @$mode = htmlspecialchars($_REQUEST["mode"]); + $mode = htmlspecialchars($_REQUEST["mode"] ?? ""); - $ldb = Db::get(); + $ldb = Db::pdo(); ?> @@ -109,7 +95,7 @@