From 5f898dcc5ee244e4adbd6aa83b607c844d432fb6 Mon Sep 17 00:00:00 2001 From: Luc SANCHEZ <4697568+ColonelMoutarde@users.noreply.github.com> Date: Sun, 26 Mar 2023 14:17:22 +0200 Subject: Modernize Constants and use new constant for string 'currentUser' (#5089) * Modernize Constants and use new constant 'currentUser' * Add FreshRSS_Context::currentUser() function and use * Add FreshRSS_Context::currentUser() function and use * Add FreshRSS_Context::currentUser() function and use * Add FreshRSS_Context::currentUser() function and use * Add FreshRSS_Context::currentUser() function and use * Update app/Controllers/userController.php * Update app/Controllers/userController.php * Update app/Controllers/userController.php * Update app/Models/Auth.php * Update p/api/greader.php * Update p/api/greader.php * Update p/api/greader.php * Update app/Models/Context.php * Update app/Models/LogDAO.php * Update lib/Minz/Log.php * Update p/api/greader.php * Update app/layout/header.phtml * Update app/views/helpers/export/articles.phtml * Update cli/do-install.php * Remarque's from Alkarex * Remarque's from Alkarex * Refactor using new Minz_User class * Consistent naming of public constants --------- Co-authored-by: Luc Co-authored-by: Alexandre Alapetite --- app/Controllers/apiController.php | 2 +- app/Controllers/authController.php | 6 ++--- app/Controllers/importExportController.php | 2 +- app/Controllers/userController.php | 10 ++++----- app/Models/Auth.php | 16 +++++++------- app/Models/Context.php | 21 ++++++++++-------- app/Models/Feed.php | 2 +- app/Models/LogDAO.php | 14 ++++++++---- app/Models/UserDAO.php | 2 +- app/install.php | 6 ++--- app/layout/aside_configure.phtml | 2 +- app/layout/header.phtml | 4 ++-- app/layout/nav_menu.phtml | 6 ++--- app/layout/simple.phtml | 2 +- app/views/helpers/export/articles.phtml | 2 +- app/views/user/profile.phtml | 6 ++--- cli/do-install.php | 3 +-- constants.php | 35 +++++++++++++++--------------- lib/Minz/Extension.php | 6 ++--- lib/Minz/Log.php | 7 +----- lib/Minz/ModelPdo.php | 2 +- lib/Minz/User.php | 27 +++++++++++++++++++++++ lib/lib_rss.php | 4 ++-- p/api/fever.php | 4 ++-- p/api/greader.php | 22 +++++++++---------- p/i/index.php | 4 ++-- tests/bootstrap.php | 2 +- 27 files changed, 125 insertions(+), 94 deletions(-) create mode 100644 lib/Minz/User.php diff --git a/app/Controllers/apiController.php b/app/Controllers/apiController.php index ba3f76869..53ae2858c 100644 --- a/app/Controllers/apiController.php +++ b/app/Controllers/apiController.php @@ -11,7 +11,7 @@ class FreshRSS_api_Controller extends FreshRSS_ActionController { * @return false|string */ public static function updatePassword(string $apiPasswordPlain) { - $username = Minz_Session::param('currentUser'); + $username = Minz_User::name(); $userConfig = FreshRSS_Context::$user_conf; $apiPasswordHash = FreshRSS_password_Util::hash($apiPasswordPlain); diff --git a/app/Controllers/authController.php b/app/Controllers/authController.php index 03e223375..30fd705f5 100644 --- a/app/Controllers/authController.php +++ b/app/Controllers/authController.php @@ -72,7 +72,7 @@ class FreshRSS_auth_Controller extends FreshRSS_ActionController { } $auth_type = FreshRSS_Context::$system_conf->auth_type; - FreshRSS_Context::initUser('_', false); + FreshRSS_Context::initUser(Minz_User::INTERNAL_USER, false); switch ($auth_type) { case 'form': Minz_Request::forward(array('c' => 'auth', 'a' => 'formLogin')); @@ -145,7 +145,7 @@ class FreshRSS_auth_Controller extends FreshRSS_ActionController { if ($ok) { // Set session parameter to give access to the user. Minz_Session::_params([ - 'currentUser' => $username, + Minz_User::CURRENT_USER => $username, 'passwordHash' => FreshRSS_Context::$user_conf->passwordHash, 'csrf' => false, ]); @@ -195,7 +195,7 @@ class FreshRSS_auth_Controller extends FreshRSS_ActionController { unset($password); if ($ok) { Minz_Session::_params([ - 'currentUser' => $username, + Minz_User::CURRENT_USER => $username, 'passwordHash' => $s, 'csrf' => false, ]); diff --git a/app/Controllers/importExportController.php b/app/Controllers/importExportController.php index 5066ff124..14adc793d 100644 --- a/app/Controllers/importExportController.php +++ b/app/Controllers/importExportController.php @@ -586,7 +586,7 @@ class FreshRSS_importExport_Controller extends FreshRSS_ActionController { ); } - $username = Minz_Session::param('currentUser'); + $username = Minz_User::name(); $export_service = new FreshRSS_Export_Service($username); $export_opml = Minz_Request::param('export_opml', false); diff --git a/app/Controllers/userController.php b/app/Controllers/userController.php index 01656099c..89489e590 100644 --- a/app/Controllers/userController.php +++ b/app/Controllers/userController.php @@ -68,7 +68,7 @@ class FreshRSS_user_Controller extends FreshRSS_ActionController { )); if ($ok) { - $isSelfUpdate = Minz_Session::param('currentUser', '_') === $username; + $isSelfUpdate = Minz_User::name() === $username; if ($passwordPlain == '' || !$isSelfUpdate) { Minz_Request::good(_t('feedback.user.updated', $username), array('c' => 'user', 'a' => 'manage')); } else { @@ -124,7 +124,7 @@ class FreshRSS_user_Controller extends FreshRSS_ActionController { } $ok = self::updateUser( - Minz_Session::param('currentUser'), + Minz_User::name(), $email, $passwordPlain, array( @@ -359,7 +359,7 @@ class FreshRSS_user_Controller extends FreshRSS_ActionController { if ($ok && !FreshRSS_Auth::hasAccess('admin')) { $user_conf = get_user_configuration($new_user_name); Minz_Session::_params([ - 'currentUser' => $new_user_name, + Minz_User::CURRENT_USER => $new_user_name, 'passwordHash' => $user_conf->passwordHash, 'csrf' => false, ]); @@ -487,7 +487,7 @@ class FreshRSS_user_Controller extends FreshRSS_ActionController { Minz_Error::error(404); } - $username = Minz_Session::param('currentUser', '_'); + $username = Minz_User::name(); $user_config = FreshRSS_Context::$user_conf; if ($user_config->email_validation_token === '') { @@ -524,7 +524,7 @@ class FreshRSS_user_Controller extends FreshRSS_ActionController { */ public function deleteAction() { $username = Minz_Request::param('username'); - $self_deletion = Minz_Session::param('currentUser', '_') === $username; + $self_deletion = Minz_User::name() === $username; if (!FreshRSS_Auth::hasAccess('admin') && !$self_deletion) { Minz_Error::error(403); diff --git a/app/Models/Auth.php b/app/Models/Auth.php index 90fb128d4..65e761fb8 100644 --- a/app/Models/Auth.php +++ b/app/Models/Auth.php @@ -21,11 +21,11 @@ class FreshRSS_Auth { } self::$login_ok = Minz_Session::param('loginOk', false); - $current_user = Minz_Session::param('currentUser', ''); - if ($current_user == '') { + $current_user = Minz_User::name(); + if ($current_user === null) { $current_user = FreshRSS_Context::$system_conf->default_user; Minz_Session::_params([ - 'currentUser' => $current_user, + Minz_User::CURRENT_USER => $current_user, 'csrf' => false, ]); } @@ -58,7 +58,7 @@ class FreshRSS_Auth { if (isset($credentials[1])) { $current_user = trim($credentials[0]); Minz_Session::_params([ - 'currentUser' => $current_user, + Minz_User::CURRENT_USER => $current_user, 'passwordHash' => trim($credentials[1]), 'csrf' => false, ]); @@ -84,7 +84,7 @@ class FreshRSS_Auth { } if ($login_ok) { Minz_Session::_params([ - 'currentUser' => $current_user, + Minz_User::CURRENT_USER => $current_user, 'csrf' => false, ]); } @@ -112,7 +112,7 @@ class FreshRSS_Auth { self::$login_ok = Minz_Session::param('passwordHash') === FreshRSS_Context::$user_conf->passwordHash; break; case 'http_auth': - $current_user = Minz_Session::param('currentUser'); + $current_user = Minz_User::name(); self::$login_ok = strcasecmp($current_user, httpAuthUser()) === 0; break; case 'none': @@ -140,7 +140,7 @@ class FreshRSS_Auth { if (FreshRSS_Context::$user_conf == null) { return false; } - $currentUser = Minz_Session::param('currentUser'); + $currentUser = Minz_User::name(); $isAdmin = FreshRSS_Context::$user_conf->is_admin; $default_user = FreshRSS_Context::$system_conf->default_user; $ok = self::$login_ok; @@ -181,7 +181,7 @@ class FreshRSS_Auth { if ($username == '') { $username = FreshRSS_Context::$system_conf->default_user; } - Minz_Session::_param('currentUser', $username); + Minz_User::change($username); switch (FreshRSS_Context::$system_conf->auth_type) { case 'form': diff --git a/app/Models/Context.php b/app/Models/Context.php index 734458d7f..35bd192d7 100644 --- a/app/Models/Context.php +++ b/app/Models/Context.php @@ -4,7 +4,7 @@ * The context object handles the current configuration file and different * useful functions associated to the current view state. */ -class FreshRSS_Context { +final class FreshRSS_Context { /** * @var FreshRSS_UserConfiguration|null @@ -55,7 +55,7 @@ class FreshRSS_Context { /** * Initialize the context for the global system. */ - public static function initSystem($reload = false) { + public static function initSystem(bool $reload = false): FreshRSS_SystemConfiguration { if ($reload || FreshRSS_Context::$system_conf == null) { //TODO: Keep in session what we need instead of always reloading from disk FreshRSS_Context::$system_conf = FreshRSS_SystemConfiguration::init(DATA_PATH . '/config.php', FRESHRSS_PATH . '/config.default.php'); @@ -68,8 +68,9 @@ class FreshRSS_Context { /** * Initialize the context for the current user. + * @return FreshRSS_UserConfiguration|false */ - public static function initUser($username = '', $userMustExist = true) { + public static function initUser(string $username = '', bool $userMustExist = true) { FreshRSS_Context::$user_conf = null; if (!isset($_SESSION)) { Minz_Session::init('FreshRSS'); @@ -77,9 +78,9 @@ class FreshRSS_Context { Minz_Session::lock(); if ($username == '') { - $username = Minz_Session::param('currentUser', ''); + $username = Minz_User::name() ?? ''; } - if (($username === '_' || FreshRSS_user_Controller::checkUsername($username)) && + if (($username === Minz_User::INTERNAL_USER || FreshRSS_user_Controller::checkUsername($username)) && (!$userMustExist || FreshRSS_user_Controller::userExists($username))) { try { //TODO: Keep in session what we need instead of always reloading from disk @@ -88,7 +89,7 @@ class FreshRSS_Context { FRESHRSS_PATH . '/config-user.default.php', FreshRSS_Context::$system_conf->configurationSetter()); - Minz_Session::_param('currentUser', $username); + Minz_User::change($username); } catch (Exception $ex) { Minz_Log::warning($ex->getMessage(), USERS_PATH . '/_/' . LOG_FILENAME); } @@ -96,7 +97,7 @@ class FreshRSS_Context { if (FreshRSS_Context::$user_conf == null) { Minz_Session::_params([ 'loginOk' => false, - 'currentUser' => false, + Minz_User::CURRENT_USER => false, ]); } Minz_Session::unlock(); @@ -212,6 +213,7 @@ class FreshRSS_Context { * * If $array is true, the first item of the returned value is 'f' or 'c' and * the second is the id. + * @return string|array{string,bool|int} */ public static function currentGet($array = false) { if (self::$current_get['all']) { @@ -239,6 +241,7 @@ class FreshRSS_Context { } elseif (self::$current_get['tags']) { return 'T'; } + return ''; } /** @@ -272,8 +275,8 @@ class FreshRSS_Context { /** * @return bool true if $get parameter correspond to the $current_get attribute. */ - public static function isCurrentGet($get): bool { - $type = $get[0]; + public static function isCurrentGet(string $get): bool { + $type = substr($get, 0, 1); $id = substr($get, 2); switch($type) { diff --git a/app/Models/Feed.php b/app/Models/Feed.php index 7c46199a5..0b16bc796 100644 --- a/app/Models/Feed.php +++ b/app/Models/Feed.php @@ -1011,7 +1011,7 @@ class FreshRSS_Feed extends Minz_Model { Minz_Log::debug($text); Minz_Log::debug($text, PSHB_LOG); } - $currentUser = Minz_Session::param('currentUser'); + $currentUser = Minz_User::name(); if (FreshRSS_user_Controller::checkUsername($currentUser) && !file_exists($path . '/' . $currentUser . '.txt')) { touch($path . '/' . $currentUser . '.txt'); } diff --git a/app/Models/LogDAO.php b/app/Models/LogDAO.php index 787945af4..6e5bf171b 100644 --- a/app/Models/LogDAO.php +++ b/app/Models/LogDAO.php @@ -1,9 +1,15 @@ */ + public static function lines(): array { $logs = array(); - $handle = @fopen(join_path(DATA_PATH, 'users', Minz_Session::param('currentUser', '_'), LOG_FILENAME), 'r'); + $handle = @fopen(self::logPath(), 'r'); if ($handle) { while (($line = fgets($handle)) !== false) { if (preg_match('/^\[([^\[]+)\] \[([^\[]+)\] --- (.*)$/', $line, $matches)) { @@ -19,8 +25,8 @@ class FreshRSS_LogDAO { return array_reverse($logs); } - public static function truncate() { - file_put_contents(join_path(DATA_PATH, 'users', Minz_Session::param('currentUser', '_'), LOG_FILENAME), ''); + public static function truncate(): void { + file_put_contents(self::logPath(), ''); if (FreshRSS_Auth::hasAccess('admin')) { file_put_contents(ADMIN_LOG, ''); file_put_contents(API_LOG, ''); diff --git a/app/Models/UserDAO.php b/app/Models/UserDAO.php index 67498f5cd..9f91df80e 100644 --- a/app/Models/UserDAO.php +++ b/app/Models/UserDAO.php @@ -44,7 +44,7 @@ class FreshRSS_UserDAO extends Minz_ModelPdo { public static function touch($username = '') { if (!FreshRSS_user_Controller::checkUsername($username)) { - $username = Minz_Session::param('currentUser', '_'); + $username = Minz_User::name() ?? Minz_User::INTERNAL_USER; } return touch(USERS_PATH . '/' . $username . '/config.php'); } diff --git a/app/install.php b/app/install.php index a5e2ac263..b43aa81bb 100644 --- a/app/install.php +++ b/app/install.php @@ -77,7 +77,7 @@ function saveStep1() { Minz_Session::_params([ 'title' => FreshRSS_Context::$system_conf->title, 'auth_type' => FreshRSS_Context::$system_conf->auth_type, - 'default_user' => Minz_Session::param('currentUser'), + 'default_user' => Minz_User::name(), 'passwordHash' => FreshRSS_Context::$user_conf->passwordHash, 'bd_type' => FreshRSS_Context::$system_conf->db['type'], 'bd_host' => FreshRSS_Context::$system_conf->db['host'], @@ -164,9 +164,9 @@ function saveStep2() { $ok = false; try { - Minz_Session::_param('currentUser', $config_array['default_user']); + Minz_User::change($config_array['default_user']); $error = initDb(); - Minz_Session::_param('currentUser'); + Minz_User::change(); if ($error != '') { Minz_Session::_param('bd_error', $error); } else { diff --git a/app/layout/aside_configure.phtml b/app/layout/aside_configure.phtml index 6acb08f02..e179ef121 100644 --- a/app/layout/aside_configure.phtml +++ b/app/layout/aside_configure.phtml @@ -3,7 +3,7 @@