Pārlūkot izejas kodu

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
Alexandre Alapetite 6 gadi atpakaļ
vecāks
revīzija
22030155f8
6 mainītis faili ar 49 papildinājumiem un 14 dzēšanām
  1. 3 2
      Docker/README.md
  2. 3 1
      app/Models/FeedDAO.php
  3. 8 2
      app/install.php
  4. 18 1
      cli/do-install.php
  5. 3 1
      lib/Minz/ModelPdo.php
  6. 14 7
      lib/lib_install.php

+ 3 - 2
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

+ 3 - 1
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);
 			}
 

+ 8 - 2
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', "<?php\n return " . var_export($config_array, true) . ";\n");
 
+		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');
 
 		$ok = false;
 		try {
-			$ok = checkDb();
+			Minz_Session::_param('currentUser', $config_array['default_user']);
+			$ok = initDb();
+			Minz_Session::_param('currentUser');
 		} catch (Exception $ex) {
 			$_SESSION['bd_error'] = $ex->getMessage();
 			$ok = false;

+ 18 - 1
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']));
 }

+ 3 - 1
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) {

+ 14 - 7
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();