From efd196839a20ae7e38be227c62b9c134ddec4bea Mon Sep 17 00:00:00 2001
From: Andrew Dolgov <noreply@fakecake.org>
Date: Thu, 25 Feb 2021 15:28:27 +0300
Subject: [PATCH] stop caching schema version entirely, fix some
 session_start() related warnings

---
 api/index.php              |  2 +-
 classes/api.php            |  8 ++++++--
 classes/handler/public.php |  3 ++-
 classes/userhelper.php     |  4 +++-
 include/functions.php      | 20 ++++++--------------
 include/sessions.php       |  3 ++-
 6 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/api/index.php b/api/index.php
index d85a1103c..430082f16 100644
--- a/api/index.php
+++ b/api/index.php
@@ -23,7 +23,7 @@
 
 	if (!empty($_REQUEST["sid"])) {
 		session_id($_REQUEST["sid"]);
-		@session_start();
+		session_start();
 	}
 
 	startup_gettext();
diff --git a/classes/api.php b/classes/api.php
index 7ca8ec8ba..3c6795327 100755
--- a/classes/api.php
+++ b/classes/api.php
@@ -59,8 +59,12 @@ class API extends Handler {
 	}
 
 	function login() {
-		@session_destroy();
-		@session_start();
+
+		if (session_status() == PHP_SESSION_ACTIVE) {
+			session_destroy();
+		}
+
+		session_start();
 
 		$login = clean($_REQUEST["user"]);
 		$password = clean($_REQUEST["password"]);
diff --git a/classes/handler/public.php b/classes/handler/public.php
index cc5d35079..bf0160db6 100755
--- a/classes/handler/public.php
+++ b/classes/handler/public.php
@@ -390,7 +390,8 @@ class Handler_Public extends Handler {
 			} else {
 
 				// start an empty session to deliver login error message
-				@session_start();
+				if (session_status() != PHP_SESSION_ACTIVE)
+					session_start();
 
 				if (!isset($_SESSION["login_error_msg"]))
 					$_SESSION["login_error_msg"] = __("Incorrect username or password");
diff --git a/classes/userhelper.php b/classes/userhelper.php
index d46d75bf4..8d9d483a8 100644
--- a/classes/userhelper.php
+++ b/classes/userhelper.php
@@ -87,7 +87,9 @@ class UserHelper {
 		$pdo = Db::pdo();
 
 		if (Config::get(Config::SINGLE_USER_MODE)) {
-			@session_start();
+			if (session_status() != PHP_SESSION_ACTIVE)
+					session_start();
+
 			self::authenticate("admin", null);
 			startup_gettext();
 			self::load_user_plugins($_SESSION["uid"]);
diff --git a/include/functions.php b/include/functions.php
index 9b5661383..746f8d39e 100644
--- a/include/functions.php
+++ b/include/functions.php
@@ -305,22 +305,14 @@
 		return $s ? 1 : 0;
 	}
 
-	// Session caching removed due to causing wrong redirects to upgrade
-	// script when get_schema_version() is called on an obsolete session
-	// created on a previous schema version.
-	function get_schema_version($nocache = false) {
-		global $schema_version;
-
+	function get_schema_version() {
 		$pdo = Db::pdo();
 
-		if (!$schema_version && !$nocache) {
-			$row = $pdo->query("SELECT schema_version FROM ttrss_version")->fetch();
-			$version = $row["schema_version"];
-			$schema_version = $version;
-			return $version;
-		} else {
-			return $schema_version;
-		}
+		$row = $pdo->query("SELECT schema_version FROM ttrss_version")->fetch();
+		$version = $row["schema_version"];
+		$schema_version = $version;
+
+		return $version;
 	}
 
 	function file_is_locked($filename) {
diff --git a/include/sessions.php b/include/sessions.php
index d4f21d8cd..23815e182 100644
--- a/include/sessions.php
+++ b/include/sessions.php
@@ -152,6 +152,7 @@
 
 	if (!defined('NO_SESSION_AUTOSTART')) {
 		if (isset($_COOKIE[session_name()])) {
-			@session_start();
+			if (session_status() != PHP_SESSION_ACTIVE)
+					session_start();
 		}
 	}
-- 
GitLab