aboutsummaryrefslogtreecommitdiff
path: root/app/Models
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2021-01-02 21:20:19 +0100
committerGravatar GitHub <noreply@github.com> 2021-01-02 21:20:19 +0100
commit9c6682e7edf8cbad828088cbeeef66c7ecefdd9a (patch)
treec8c8a6ba34d49d22497a14ddaf18ae97d8cd5bb1 /app/Models
parent7bc2cc5825547f5b5cf15005fda937e06065b45d (diff)
Avoid manual initialisations of system or user configuration (#3070)
* Avoid manual intialisations of system or user configuration More consistent use of Context * Simplify FreshRSS_Context::initUser * Remove a few manual get_user_configuration * A bit of debugging * Fix context user init * Fix install * Fix concurrency Concurrent requests could lead to bad race condition * Fix actualize cron Fix case when system i initialised several times
Diffstat (limited to 'app/Models')
-rw-r--r--app/Models/Auth.php39
-rw-r--r--app/Models/Context.php60
-rw-r--r--app/Models/Entry.php7
-rw-r--r--app/Models/Factory.php18
-rw-r--r--app/Models/FormAuth.php12
5 files changed, 82 insertions, 54 deletions
diff --git a/app/Models/Auth.php b/app/Models/Auth.php
index 9b18b8259..709a80f84 100644
--- a/app/Models/Auth.php
+++ b/app/Models/Auth.php
@@ -22,9 +22,8 @@ class FreshRSS_Auth {
self::$login_ok = Minz_Session::param('loginOk', false);
$current_user = Minz_Session::param('currentUser', '');
- if ($current_user === '') {
- $conf = Minz_Configuration::get('system');
- $current_user = $conf->default_user;
+ if ($current_user == '') {
+ $current_user = FreshRSS_Context::$system_conf->default_user;
Minz_Session::_params([
'currentUser' => $current_user,
'csrf' => false,
@@ -51,7 +50,6 @@ class FreshRSS_Auth {
* @return boolean true if user can be connected, false else.
*/
private static function accessControl() {
- FreshRSS_Context::$system_conf = Minz_Configuration::get('system');
$auth_type = FreshRSS_Context::$system_conf->auth_type;
switch ($auth_type) {
case 'form':
@@ -103,19 +101,18 @@ class FreshRSS_Auth {
* Gives access to the current user.
*/
public static function giveAccess() {
- $current_user = Minz_Session::param('currentUser');
- $user_conf = get_user_configuration($current_user);
- if ($user_conf == null) {
+ FreshRSS_Context::initUser();
+ if (FreshRSS_Context::$user_conf == null) {
self::$login_ok = false;
return false;
}
- $system_conf = Minz_Configuration::get('system');
- switch ($system_conf->auth_type) {
+ switch (FreshRSS_Context::$system_conf->auth_type) {
case 'form':
- self::$login_ok = Minz_Session::param('passwordHash') === $user_conf->passwordHash;
+ self::$login_ok = Minz_Session::param('passwordHash') === FreshRSS_Context::$user_conf->passwordHash;
break;
case 'http_auth':
+ $current_user = Minz_Session::param('currentUser');
self::$login_ok = strcasecmp($current_user, httpAuthUser()) === 0;
break;
case 'none':
@@ -140,11 +137,12 @@ class FreshRSS_Auth {
* @return boolean true if user has corresponding access, false else.
*/
public static function hasAccess($scope = 'general') {
- $systemConfiguration = Minz_Configuration::get('system');
+ if (FreshRSS_Context::$user_conf == null) {
+ return false;
+ }
$currentUser = Minz_Session::param('currentUser');
- $userConfiguration = get_user_configuration($currentUser);
- $isAdmin = $userConfiguration && $userConfiguration->is_admin;
- $default_user = $systemConfiguration->default_user;
+ $isAdmin = FreshRSS_Context::$user_conf->is_admin;
+ $default_user = FreshRSS_Context::$system_conf->default_user;
$ok = self::$login_ok;
switch ($scope) {
case 'general':
@@ -168,7 +166,6 @@ class FreshRSS_Auth {
'csrf' => false,
'REMOTE_USER' => false,
]);
- $system_conf = Minz_Configuration::get('system');
$username = '';
$token_param = Minz_Request::param('token', '');
@@ -182,11 +179,11 @@ class FreshRSS_Auth {
}
}
if ($username == '') {
- $username = $system_conf->default_user;
+ $username = FreshRSS_Context::$system_conf->default_user;
}
Minz_Session::_param('currentUser', $username);
- switch ($system_conf->auth_type) {
+ switch (FreshRSS_Context::$system_conf->auth_type) {
case 'form':
Minz_Session::_param('passwordHash');
FreshRSS_FormAuth::deleteCookie();
@@ -204,18 +201,14 @@ class FreshRSS_Auth {
* Return if authentication is enabled on this instance of FRSS.
*/
public static function accessNeedsLogin() {
- $conf = Minz_Configuration::get('system');
- $auth_type = $conf->auth_type;
- return $auth_type !== 'none';
+ return FreshRSS_Context::$system_conf->auth_type !== 'none';
}
/**
* Return if authentication requires a PHP action.
*/
public static function accessNeedsAction() {
- $conf = Minz_Configuration::get('system');
- $auth_type = $conf->auth_type;
- return $auth_type === 'form';
+ return FreshRSS_Context::$system_conf->auth_type === 'form';
}
public static function csrfToken() {
diff --git a/app/Models/Context.php b/app/Models/Context.php
index 8be73f407..1ca99a26d 100644
--- a/app/Models/Context.php
+++ b/app/Models/Context.php
@@ -43,14 +43,58 @@ class FreshRSS_Context {
public static $isCli = false;
/**
- * Initialize the context.
- *
- * Set the correct configurations and $categories variables.
+ * Initialize the context for the global system.
*/
- public static function init() {
- // Init configuration.
- self::$system_conf = Minz_Configuration::get('system');
- self::$user_conf = Minz_Configuration::get('user');
+ public static function initSystem($reload = false) {
+ if ($reload || FreshRSS_Context::$system_conf == null) {
+ //TODO: Keep in session what we need instead of always reloading from disk
+ Minz_Configuration::register('system', DATA_PATH . '/config.php', FRESHRSS_PATH . '/config.default.php');
+ FreshRSS_Context::$system_conf = Minz_Configuration::get('system');
+ // Register the configuration setter for the system configuration
+ $configurationSetter = new FreshRSS_ConfigurationSetter();
+ FreshRSS_Context::$system_conf->_configurationSetter($configurationSetter);
+ }
+ return FreshRSS_Context::$system_conf;
+ }
+
+ /**
+ * Initialize the context for the current user.
+ */
+ public static function initUser($username = '') {
+ FreshRSS_Context::$user_conf = null;
+ if (!isset($_SESSION)) {
+ Minz_Session::init('FreshRSS');
+ }
+
+ Minz_Session::lock();
+ if ($username == '') {
+ $username = Minz_Session::param('currentUser', '');
+ }
+ if ($username === '_' || FreshRSS_user_Controller::checkUsername($username)) {
+ try {
+ //TODO: Keep in session what we need instead of always reloading from disk
+ Minz_Configuration::register('user',
+ USERS_PATH . '/' . $username . '/config.php',
+ FRESHRSS_PATH . '/config-user.default.php',
+ FreshRSS_Context::$system_conf->configurationSetter());
+
+ Minz_Session::_param('currentUser', $username);
+ FreshRSS_Context::$user_conf = Minz_Configuration::get('user');
+ } catch (Exception $ex) {
+ Minz_Log::warning($ex->getMessage(), USERS_PATH . '/_/log.txt');
+ }
+ }
+ if (FreshRSS_Context::$user_conf == null) {
+ Minz_Session::_params([
+ 'loginOk' => false,
+ 'currentUser' => false,
+ ]);
+ }
+ Minz_Session::unlock();
+
+ if (FreshRSS_Context::$user_conf == null) {
+ return false;
+ }
//Legacy
$oldEntries = (int)FreshRSS_Context::$user_conf->param('old_entries', 0);
@@ -74,6 +118,8 @@ class FreshRSS_Context {
if (!in_array(FreshRSS_Context::$user_conf->display_categories, [ 'active', 'remember', 'all', 'none' ], true)) {
FreshRSS_Context::$user_conf->display_categories = FreshRSS_Context::$user_conf->display_categories === true ? 'all' : 'active';
}
+
+ return FreshRSS_Context::$user_conf;
}
/**
diff --git a/app/Models/Entry.php b/app/Models/Entry.php
index ed0c1245c..96e1cf3a6 100644
--- a/app/Models/Entry.php
+++ b/app/Models/Entry.php
@@ -355,11 +355,10 @@ class FreshRSS_Entry extends Minz_Model {
}
public static function getContentByParsing($url, $path, $attributes = array(), $maxRedirs = 3) {
- $system_conf = Minz_Configuration::get('system');
- $limits = $system_conf->limits;
+ $limits = FreshRSS_Context::$system_conf->limits;
$feed_timeout = empty($attributes['timeout']) ? 0 : intval($attributes['timeout']);
- if ($system_conf->simplepie_syslog_enabled) {
+ if (FreshRSS_Context::$system_conf->simplepie_syslog_enabled) {
syslog(LOG_INFO, 'FreshRSS GET ' . SimplePie_Misc::url_remove_credentials($url));
}
@@ -377,7 +376,7 @@ class FreshRSS_Entry extends Minz_Model {
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => '', //Enable all encodings
]);
- curl_setopt_array($ch, $system_conf->curl_options);
+ curl_setopt_array($ch, FreshRSS_Context::$system_conf->curl_options);
if (isset($attributes['ssl_verify'])) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $attributes['ssl_verify'] ? 2 : 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $attributes['ssl_verify'] ? true : false);
diff --git a/app/Models/Factory.php b/app/Models/Factory.php
index 69885c205..308ff2864 100644
--- a/app/Models/Factory.php
+++ b/app/Models/Factory.php
@@ -7,8 +7,7 @@ class FreshRSS_Factory {
}
public static function createCategoryDao($username = null) {
- $conf = Minz_Configuration::get('system');
- switch ($conf->db['type']) {
+ switch (FreshRSS_Context::$system_conf->db['type']) {
case 'sqlite':
return new FreshRSS_CategoryDAOSQLite($username);
default:
@@ -17,8 +16,7 @@ class FreshRSS_Factory {
}
public static function createFeedDao($username = null) {
- $conf = Minz_Configuration::get('system');
- switch ($conf->db['type']) {
+ switch (FreshRSS_Context::$system_conf->db['type']) {
case 'sqlite':
return new FreshRSS_FeedDAOSQLite($username);
default:
@@ -27,8 +25,7 @@ class FreshRSS_Factory {
}
public static function createEntryDao($username = null) {
- $conf = Minz_Configuration::get('system');
- switch ($conf->db['type']) {
+ switch (FreshRSS_Context::$system_conf->db['type']) {
case 'sqlite':
return new FreshRSS_EntryDAOSQLite($username);
case 'pgsql':
@@ -39,8 +36,7 @@ class FreshRSS_Factory {
}
public static function createTagDao($username = null) {
- $conf = Minz_Configuration::get('system');
- switch ($conf->db['type']) {
+ switch (FreshRSS_Context::$system_conf->db['type']) {
case 'sqlite':
return new FreshRSS_TagDAOSQLite($username);
case 'pgsql':
@@ -51,8 +47,7 @@ class FreshRSS_Factory {
}
public static function createStatsDAO($username = null) {
- $conf = Minz_Configuration::get('system');
- switch ($conf->db['type']) {
+ switch (FreshRSS_Context::$system_conf->db['type']) {
case 'sqlite':
return new FreshRSS_StatsDAOSQLite($username);
case 'pgsql':
@@ -63,8 +58,7 @@ class FreshRSS_Factory {
}
public static function createDatabaseDAO($username = null) {
- $conf = Minz_Configuration::get('system');
- switch ($conf->db['type']) {
+ switch (FreshRSS_Context::$system_conf->db['type']) {
case 'sqlite':
return new FreshRSS_DatabaseDAOSQLite($username);
case 'pgsql':
diff --git a/app/Models/FormAuth.php b/app/Models/FormAuth.php
index 86742e2f2..1aca7c3d1 100644
--- a/app/Models/FormAuth.php
+++ b/app/Models/FormAuth.php
@@ -24,8 +24,7 @@ class FreshRSS_FormAuth {
$token_file = DATA_PATH . '/tokens/' . $token . '.txt';
$mtime = @filemtime($token_file);
- $conf = Minz_Configuration::get('system');
- $limits = $conf->limits;
+ $limits = FreshRSS_Context::$system_conf->limits;
$cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
if ($mtime + $cookie_duration < time()) {
// Token has expired (> cookie_duration) or does not exist.
@@ -43,8 +42,7 @@ class FreshRSS_FormAuth {
private static function renewCookie($token) {
$token_file = DATA_PATH . '/tokens/' . $token . '.txt';
if (touch($token_file)) {
- $conf = Minz_Configuration::get('system');
- $limits = $conf->limits;
+ $limits = FreshRSS_Context::$system_conf->limits;
$cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
$expire = time() + $cookie_duration;
Minz_Session::setLongTermCookie('FreshRSS_login', $token, $expire);
@@ -54,9 +52,8 @@ class FreshRSS_FormAuth {
}
public static function makeCookie($username, $password_hash) {
- $conf = Minz_Configuration::get('system');
do {
- $token = sha1($conf->salt . $username . uniqid(mt_rand(), true));
+ $token = sha1(FreshRSS_Context::$system_conf->salt . $username . uniqid(mt_rand(), true));
$token_file = DATA_PATH . '/tokens/' . $token . '.txt';
} while (file_exists($token_file));
@@ -80,8 +77,7 @@ class FreshRSS_FormAuth {
}
public static function purgeTokens() {
- $conf = Minz_Configuration::get('system');
- $limits = $conf->limits;
+ $limits = FreshRSS_Context::$system_conf->limits;
$cookie_duration = empty($limits['cookie_duration']) ? FreshRSS_Auth::DEFAULT_COOKIE_DURATION : $limits['cookie_duration'];
$oldest = time() - $cookie_duration;
foreach (new DirectoryIterator(DATA_PATH . '/tokens/') as $file_info) {