From 7819a43197d34ef7a6c5626e9e48d7db075c37c9 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Mon, 4 Nov 2019 17:45:15 +0100 Subject: Default or custom OPML (#2627) * Default or custom OPML Fix https://github.com/FreshRSS/FreshRSS/issues/2075 Replaces https://github.com/FreshRSS/FreshRSS/pull/2515 https://github.com/FreshRSS/FreshRSS/issues/2514 Uses the local ./data/opml.xml if it exists, otherwise ./opml.default.xml * Better message * Move to controller --- app/Models/UserDAO.php | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) (limited to 'app/Models') diff --git a/app/Models/UserDAO.php b/app/Models/UserDAO.php index 4e824cf01..266c8bc0e 100644 --- a/app/Models/UserDAO.php +++ b/app/Models/UserDAO.php @@ -1,34 +1,21 @@ pdo->dbType() . '.php'); try { $sql = $SQL_CREATE_TABLES . $SQL_CREATE_TABLE_ENTRYTMP . $SQL_CREATE_TABLE_TAGS; $ok = $this->pdo->exec($sql) !== false; //Note: Only exec() can take multiple statements safely. - if ($ok && $insertDefaultFeeds) { - $default_feeds = FreshRSS_Context::$system_conf->default_feeds; - $stm = $this->pdo->prepare($SQL_INSERT_FEED); - foreach ($default_feeds as $feed) { - $parameters = [ - ':url' => $feed['url'], - ':name' => $feed['name'], - ':website' => $feed['website'], - ':description' => $feed['description'], - ]; - $ok &= ($stm && $stm->execute($parameters)); - } - } } catch (Exception $e) { - Minz_Log::error('Error while creating database for user: ' . $e->getMessage()); + Minz_Log::error('Error while creating database for user ' . $this->current_user . ': ' . $e->getMessage()); } if ($ok) { return true; } else { $info = empty($stm) ? $this->pdo->errorInfo() : $stm->errorInfo(); - Minz_Log::error(__METHOD__ . ' error: ' . $info[2]); + Minz_Log::error(__METHOD__ . ' error: ' . json_encode($info)); return false; } } -- cgit v1.2.3 From 122e4b412a29c90ec69c2a3fb68b6d8b2b3df5c7 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Mon, 4 Nov 2019 18:13:13 +0100 Subject: Fix PostgreSQL size bug for uppercase (#2631) Crash for users with uppercase letters --- app/Models/DatabaseDAOPGSQL.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'app/Models') diff --git a/app/Models/DatabaseDAOPGSQL.php b/app/Models/DatabaseDAOPGSQL.php index 1a6b3599e..7ca7799ae 100644 --- a/app/Models/DatabaseDAOPGSQL.php +++ b/app/Models/DatabaseDAOPGSQL.php @@ -58,14 +58,17 @@ class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAOSQLite { $stm->execute(); } else { $sql = "SELECT " - . "pg_total_relation_size('{$this->pdo->prefix()}category') + " - . "pg_total_relation_size('{$this->pdo->prefix()}feed') + " - . "pg_total_relation_size('{$this->pdo->prefix()}entry') + " - . "pg_total_relation_size('{$this->pdo->prefix()}entrytmp') + " - . "pg_total_relation_size('{$this->pdo->prefix()}tag') + " - . "pg_total_relation_size('{$this->pdo->prefix()}entrytag')"; + . "pg_total_relation_size('`{$this->pdo->prefix()}category`') + " + . "pg_total_relation_size('`{$this->pdo->prefix()}feed`') + " + . "pg_total_relation_size('`{$this->pdo->prefix()}entry`') + " + . "pg_total_relation_size('`{$this->pdo->prefix()}entrytmp`') + " + . "pg_total_relation_size('`{$this->pdo->prefix()}tag`') + " + . "pg_total_relation_size('`{$this->pdo->prefix()}entrytag`')"; $stm = $this->pdo->query($sql); } + if ($stm == false) { + return 0; + } $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0); return $res[0]; } -- cgit v1.2.3 From 22030155f8fd5cab102ddd897a914cf1a0ffd183 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Wed, 6 Nov 2019 15:16:00 +0100 Subject: Fix database autocreate at install (#2635) * Fix database autocreate at install Several bugs prevented the auto-creation of the database in Web and CLI installs. Fix https://github.com/YunoHost-Apps/freshrss_ynh/issues/84#issuecomment-549818408 * initDb https://github.com/FreshRSS/FreshRSS/pull/2635#discussion_r343107795 --- Docker/README.md | 5 +++-- app/Models/FeedDAO.php | 4 +++- app/install.php | 10 ++++++++-- cli/do-install.php | 19 ++++++++++++++++++- lib/Minz/ModelPdo.php | 4 +++- lib/lib_install.php | 21 ++++++++++++++------- 6 files changed, 49 insertions(+), 14 deletions(-) (limited to 'app/Models') diff --git a/Docker/README.md b/Docker/README.md index d06016fb9..6920471df 100644 --- a/Docker/README.md +++ b/Docker/README.md @@ -162,10 +162,11 @@ docker build --pull --tag freshrss/freshrss -f Docker/Dockerfile . ## Command line ```sh -docker exec --user apache -it freshrss php ./cli/list-users.php +docker exec --user www-data -it freshrss php ./cli/list-users.php ``` See the [CLI documentation](../cli/) for all the other commands. +You might have to replace `--user www-data` by `--user apache` when using our images based on Linux Alpine. ## Debugging @@ -210,7 +211,7 @@ Remember not pass the `CRON_MIN` environment variable to your Docker run, to avo Example on Debian / Ubuntu: Create `/etc/cron.d/FreshRSS` with: ``` -7,37 * * * * root docker exec --user apache -it freshrss php ./app/actualize_script.php > /tmp/FreshRSS.log 2>&1 +7,37 * * * * root docker exec --user www-data -it freshrss php ./app/actualize_script.php > /tmp/FreshRSS.log 2>&1 ``` ### Option 3) Cron as another instance of the same FreshRSS Docker image diff --git a/app/Models/FeedDAO.php b/app/Models/FeedDAO.php index fa0001df7..2bff50d52 100644 --- a/app/Models/FeedDAO.php +++ b/app/Models/FeedDAO.php @@ -102,7 +102,9 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable { 'httpAuth' => $feed->httpAuth(), 'attributes' => $feed->attributes(), ); - if ($feed->mute() || $feed->ttl() != FreshRSS_Context::$user_conf->ttl_default) { + if ($feed->mute() || ( + FreshRSS_Context::$user_conf != null && //When creating a new user + $feed->ttl() != FreshRSS_Context::$user_conf->ttl_default)) { $values['ttl'] = $feed->ttl() * ($feed->mute() ? -1 : 1); } diff --git a/app/install.php b/app/install.php index 96bee34a1..557ae9eab 100644 --- a/app/install.php +++ b/app/install.php @@ -132,7 +132,7 @@ function saveStep2() { $config_array = [ 'salt' => generateSalt(), 'base_url' => $base_url, - 'default_user' => 'admin', + 'default_user' => '_', 'db' => [ 'type' => $_SESSION['bd_type'], 'host' => $_SESSION['bd_host'], @@ -154,12 +154,18 @@ function saveStep2() { @unlink(DATA_PATH . '/config.php'); //To avoid access-rights problems file_put_contents(DATA_PATH . '/config.php', "getMessage(); $ok = false; diff --git a/cli/do-install.php b/cli/do-install.php index dea5d235e..fa6bac8c8 100755 --- a/cli/do-install.php +++ b/cli/do-install.php @@ -82,7 +82,24 @@ if (file_put_contents(join_path(DATA_PATH, 'config.php'), fail('FreshRSS could not write configuration file!: ' . join_path(DATA_PATH, 'config.php')); } -if (!checkDb()) { +if (function_exists('opcache_reset')) { + opcache_reset(); +} + +Minz_Configuration::register('system', DATA_PATH . '/config.php', FRESHRSS_PATH . '/config.default.php'); +FreshRSS_Context::$system_conf = Minz_Configuration::get('system'); + +Minz_Session::_param('currentUser', $config['default_user']); + +$ok = false; +try { + $ok = initDb(); +} catch (Exception $ex) { + $_SESSION['bd_error'] = $ex->getMessage(); + $ok = false; +} + +if (!$ok) { @unlink(join_path(DATA_PATH, 'config.php')); fail('FreshRSS database error: ' . (empty($_SESSION['bd_error']) ? 'Unknown error' : $_SESSION['bd_error'])); } diff --git a/lib/Minz/ModelPdo.php b/lib/Minz/ModelPdo.php index 69785c253..6eb4881dc 100644 --- a/lib/Minz/ModelPdo.php +++ b/lib/Minz/ModelPdo.php @@ -28,6 +28,9 @@ class Minz_ModelPdo { if ($currentUser === null) { $currentUser = Minz_Session::param('currentUser'); } + if ($currentUser == '') { + throw new Minz_PDOConnectionException('Current user must not be empty!', '', Minz_Exception::ERROR); + } if ($currentPdo != null) { $this->pdo = $currentPdo; return; @@ -84,7 +87,6 @@ class Minz_ModelPdo { 'Invalid database type!', $db['user'], Minz_Exception::ERROR ); - break; } self::$sharedPdo = $this->pdo; } catch (Exception $e) { diff --git a/lib/lib_install.php b/lib/lib_install.php index ed361eb39..5ea1b4d2b 100644 --- a/lib/lib_install.php +++ b/lib/lib_install.php @@ -78,21 +78,28 @@ function generateSalt() { return sha1(uniqid(mt_rand(), true).implode('', stat(__FILE__))); } -function checkDb() { +function initDb() { $conf = FreshRSS_Context::$system_conf; $db = $conf->db; if (empty($db['pdo_options'])) { $db['pdo_options'] = []; } $db['pdo_options'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION; - $dbBase = isset($db['base']) ? $db['base'] : ''; + $conf->db = $db; //TODO: Remove this Minz limitation "Indirect modification of overloaded property" - $db['base'] = ''; //First connection without database name to create the database - Minz_ModelPdo::$usesSharedPdo = false; - $databaseDAO = FreshRSS_Factory::createDatabaseDAO(); - $databaseDAO->create(); + if ($db['type'] !== 'sqlite') { + Minz_ModelPdo::$usesSharedPdo = false; + $dbBase = isset($db['base']) ? $db['base'] : ''; + $db['base'] = ''; + $conf->db = $db; + //First connection without database name to create the database + $databaseDAO = FreshRSS_Factory::createDatabaseDAO(); + $db['base'] = $dbBase; + $conf->db = $db; + $databaseDAO->create(); + } - $db['base'] = $dbBase; //New connection with the database name + //New connection with the database name $databaseDAO = FreshRSS_Factory::createDatabaseDAO(); Minz_ModelPdo::$usesSharedPdo = true; return $databaseDAO->testConnection(); -- cgit v1.2.3 From 61878794a944d47bd3098ce0f18eaa303174fc67 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Wed, 6 Nov 2019 19:49:51 +0100 Subject: Fix name of default category (#2638) Fix https://github.com/FreshRSS/FreshRSS/issues/2637 --- app/Models/CategoryDAO.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'app/Models') diff --git a/app/Models/CategoryDAO.php b/app/Models/CategoryDAO.php index c1277751c..f4cef9726 100644 --- a/app/Models/CategoryDAO.php +++ b/app/Models/CategoryDAO.php @@ -45,6 +45,12 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable } else { $this->pdo->exec('DROP INDEX IF EXISTS feed_keep_history_index'); //SQLite at least drop index } + + $stm = $this->pdo->prepare('UPDATE `_category` SET name = :name WHERE id = :id'); + $stm->bindValue(':id', self::DEFAULTCATEGORYID, PDO::PARAM_INT); + $stm->bindValue(':name', 'Uncategorized'); + $stm->execute(); + return $ok; } } catch (Exception $e) { -- cgit v1.2.3 From 6d596e9e54308297d423b23bf65338d62eebc415 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Wed, 6 Nov 2019 20:49:04 +0100 Subject: More reset default category name (#2639) Improve https://github.com/FreshRSS/FreshRSS/pull/2638 --- CHANGELOG.md | 2 +- app/Models/CategoryDAO.php | 15 +++++++++++---- app/Models/DatabaseDAO.php | 3 +++ 3 files changed, 15 insertions(+), 5 deletions(-) (limited to 'app/Models') diff --git a/CHANGELOG.md b/CHANGELOG.md index e1ccec16d..9f5b590ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ * Bug fixing (regressions from 1.15.0) * Fix database auto-creation at install [#2635](https://github.com/FreshRSS/FreshRSS/pull/2635) * Fix bug in database size estimation with PostgreSQL for users with uppercase names [#2631](https://github.com/FreshRSS/FreshRSS/pull/2631) - * Reset name of default category (which cannot be customised anymore) [#2638](https://github.com/FreshRSS/FreshRSS/pull/2638) + * Reset name of default category (which cannot be customised anymore) [#2639](https://github.com/FreshRSS/FreshRSS/pull/2639) * Fix UI style details [#2634](https://github.com/FreshRSS/FreshRSS/pull/2634) * Security * Improve cookie security with policy `SameSite=Lax` [#2630](https://github.com/FreshRSS/FreshRSS/pull/2630) diff --git a/app/Models/CategoryDAO.php b/app/Models/CategoryDAO.php index f4cef9726..08dc4eef0 100644 --- a/app/Models/CategoryDAO.php +++ b/app/Models/CategoryDAO.php @@ -4,6 +4,16 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable const DEFAULTCATEGORYID = 1; + public function resetDefaultCategoryName() { + //FreshRSS 1.15.1 + $stm = $this->pdo->prepare('UPDATE `_category` SET name = :name WHERE id = :id'); + if ($stm) { + $stm->bindValue(':id', self::DEFAULTCATEGORYID, PDO::PARAM_INT); + $stm->bindValue(':name', 'Uncategorized'); + } + return $stm && $stm->execute(); + } + protected function addColumn($name) { Minz_Log::warning(__method__ . ': ' . $name); try { @@ -46,10 +56,7 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable $this->pdo->exec('DROP INDEX IF EXISTS feed_keep_history_index'); //SQLite at least drop index } - $stm = $this->pdo->prepare('UPDATE `_category` SET name = :name WHERE id = :id'); - $stm->bindValue(':id', self::DEFAULTCATEGORYID, PDO::PARAM_INT); - $stm->bindValue(':name', 'Uncategorized'); - $stm->execute(); + $this->resetDefaultCategoryName(); return $ok; } diff --git a/app/Models/DatabaseDAO.php b/app/Models/DatabaseDAO.php index a36b469b1..13330db23 100644 --- a/app/Models/DatabaseDAO.php +++ b/app/Models/DatabaseDAO.php @@ -178,6 +178,9 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo { } public function minorDbMaintenance() { + $catDAO = FreshRSS_Factory::createCategoryDao(); + $catDAO->resetDefaultCategoryName(); + $this->ensureCaseInsensitiveGuids(); } -- cgit v1.2.3