diff options
| author | 2023-03-26 14:17:22 +0200 | |
|---|---|---|
| committer | 2023-03-26 14:17:22 +0200 | |
| commit | 5f898dcc5ee244e4adbd6aa83b607c844d432fb6 (patch) | |
| tree | 3511bcb3f78d053f4488e70000ab55fee7bb688a /lib | |
| parent | df24fa2207f56909084c613495b6f235b351c640 (diff) | |
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 <sanchezluc+freshrss@gmail.com>
Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/Minz/Extension.php | 6 | ||||
| -rw-r--r-- | lib/Minz/Log.php | 7 | ||||
| -rw-r--r-- | lib/Minz/ModelPdo.php | 2 | ||||
| -rw-r--r-- | lib/Minz/User.php | 27 | ||||
| -rw-r--r-- | lib/lib_rss.php | 4 |
5 files changed, 34 insertions, 12 deletions
diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index a35d7d8b6..a280676c1 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -155,7 +155,7 @@ abstract class Minz_Extension { $file_name_url = urlencode("{$dir}/static/{$filename}"); $mtime = @filemtime("{$this->path}/static/{$filename}"); } else { - $username = Minz_Session::param('currentUser'); + $username = Minz_User::name(); $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}/{$filename}"; $file_name_url = urlencode("{$username}/{$this->config_key}/{$this->getName()}/{$filename}"); $mtime = @filemtime($path); @@ -343,7 +343,7 @@ abstract class Minz_Extension { } public function saveFile(string $filename, string $content) { - $username = Minz_Session::param('currentUser'); + $username = Minz_User::name(); $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}"; if (!file_exists($path)) { @@ -354,7 +354,7 @@ abstract class Minz_Extension { } public function removeFile(string $filename) { - $username = Minz_Session::param('currentUser'); + $username = Minz_User::name(); $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}/{$filename}"; if (file_exists($path)) { diff --git a/lib/Minz/Log.php b/lib/Minz/Log.php index 43f3c9d6d..117f231ac 100644 --- a/lib/Minz/Log.php +++ b/lib/Minz/Log.php @@ -31,14 +31,9 @@ class Minz_Log { } if (! ($env === 'silent' || ($env === 'production' && ($level >= LOG_NOTICE)))) { - $username = Minz_Session::param('currentUser', ''); - if ($username == '') { - $username = '_'; - } + $username = Minz_User::name() ?? Minz_User::INTERNAL_USER; if ($file_name == null) { $file_name = join_path(USERS_PATH, $username, LOG_FILENAME); - } else { - $username = '_'; } switch ($level) { diff --git a/lib/Minz/ModelPdo.php b/lib/Minz/ModelPdo.php index 3bcf87247..b8c1ad89b 100644 --- a/lib/Minz/ModelPdo.php +++ b/lib/Minz/ModelPdo.php @@ -103,7 +103,7 @@ class Minz_ModelPdo { */ public function __construct($currentUser = null, $currentPdo = null) { if ($currentUser === null) { - $currentUser = Minz_Session::param('currentUser'); + $currentUser = Minz_User::name(); } if ($currentPdo !== null) { $this->pdo = $currentPdo; diff --git a/lib/Minz/User.php b/lib/Minz/User.php new file mode 100644 index 000000000..252584e83 --- /dev/null +++ b/lib/Minz/User.php @@ -0,0 +1,27 @@ +<?php + +/** + * The Minz_User class handles the user information. + */ +final class Minz_User { + + public const INTERNAL_USER = '_'; + + public const CURRENT_USER = 'currentUser'; + + /** + * @return string the name of the current user, or null if there is none + */ + public static function name(): ?string { + $currentUser = trim(Minz_Session::param(Minz_User::CURRENT_USER, '')); + return $currentUser === '' ? null : $currentUser; + } + + /** + * @param string $name the name of the new user. Set to empty string to clear the user. + */ + public static function change(string $name = ''): void { + $name = trim($name); + Minz_Session::_param(Minz_User::CURRENT_USER, $name === '' ? false : $name); + } +} diff --git a/lib/lib_rss.php b/lib/lib_rss.php index 76e7b92cd..1babc8d63 100644 --- a/lib/lib_rss.php +++ b/lib/lib_rss.php @@ -547,7 +547,7 @@ function uTimeString(): string { function invalidateHttpCache(string $username = ''): bool { if (!FreshRSS_user_Controller::checkUsername($username)) { Minz_Session::_param('touch', uTimeString()); - $username = Minz_Session::param('currentUser', '_'); + $username = Minz_User::name() ?? Minz_User::INTERNAL_USER; } $ok = @touch(DATA_PATH . '/users/' . $username . '/' . LOG_FILENAME); //if (!$ok) { @@ -564,7 +564,7 @@ function listUsers(): array { $base_path = join_path(DATA_PATH, 'users'); $dir_list = array_values(array_diff( scandir($base_path) ?: [], - ['..', '.', '_'] + ['..', '.', Minz_User::INTERNAL_USER] )); foreach ($dir_list as $file) { if ($file[0] !== '.' && is_dir(join_path($base_path, $file)) && file_exists(join_path($base_path, $file, 'config.php'))) { |
