From 33ac39c33547984a9f32115155e435df1abc59c1 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 17 Oct 2022 21:11:00 +0000 Subject: [PATCH 1/3] The user related functions moved to the session class --- boot.php | 50 +++-------------------------------- src/BaseModule.php | 2 +- src/Core/Session.php | 62 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 47 deletions(-) diff --git a/boot.php b/boot.php index 24486f413..32a1494c6 100644 --- a/boot.php +++ b/boot.php @@ -27,7 +27,7 @@ * easily as email does today. */ -use Friendica\Model\Contact; +use Friendica\Core\Session; /** * Constant with a HTML line break. @@ -55,22 +55,6 @@ if (!defined('SIGTERM')) { define('SIGTERM', 15); } -/** - * Depending on the PHP version this constant does exist - or not. - * See here: http://php.net/manual/en/curl.constants.php#117928 - */ -if (!defined('CURLE_OPERATION_TIMEDOUT')) { - define('CURLE_OPERATION_TIMEDOUT', CURLE_OPERATION_TIMEOUTED); -} - -if (!function_exists('exif_imagetype')) { - function exif_imagetype($file) - { - $size = getimagesize($file); - return $size[2]; - } -} - /** * Returns the user id of locally logged in user or false. * @@ -78,11 +62,7 @@ if (!function_exists('exif_imagetype')) { */ function local_user() { - if (!empty($_SESSION['authenticated']) && !empty($_SESSION['uid'])) { - return intval($_SESSION['uid']); - } - - return false; + return Session::getLocalUser(); } /** @@ -92,21 +72,7 @@ function local_user() */ function public_contact() { - static $public_contact_id = false; - - if (!$public_contact_id && !empty($_SESSION['authenticated'])) { - if (!empty($_SESSION['my_address'])) { - // Local user - $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, false)); - } elseif (!empty($_SESSION['visitor_home'])) { - // Remote user - $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, false)); - } - } elseif (empty($_SESSION['authenticated'])) { - $public_contact_id = false; - } - - return $public_contact_id; + return Session::getPublicContact(); } /** @@ -116,13 +82,5 @@ function public_contact() */ function remote_user() { - if (empty($_SESSION['authenticated'])) { - return false; - } - - if (!empty($_SESSION['visitor_id'])) { - return intval($_SESSION['visitor_id']); - } - - return false; + return Session::getRemoteUser(); } diff --git a/src/BaseModule.php b/src/BaseModule.php index e532b9536..09107f047 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -389,7 +389,7 @@ abstract class BaseModule implements ICanHandleRequests public static function getFormSecurityStandardErrorMessage(): string { - return DI::l10n()->t("The form security token was not correct. This probably happened because the form has been opened for too long \x28>3 hours\x29 before submitting it.") . EOL; + return DI::l10n()->t("The form security token was not correct. This probably happened because the form has been opened for too long \x28>3 hours\x29 before submitting it."); } public static function checkFormSecurityTokenRedirectOnError(string $err_redirect, string $typename = '', string $formname = 'form_security_token') diff --git a/src/Core/Session.php b/src/Core/Session.php index 059cd499c..859dd8aa2 100644 --- a/src/Core/Session.php +++ b/src/Core/Session.php @@ -69,6 +69,68 @@ class Session DI::session()->clear(); } + /** + * Returns the user id of locally logged in user or false. + * + * @return int|bool user id or false + */ + public static function getLocalUser() + { + $session = DI::session(); + + if (!empty($session->get('authenticated')) && !empty($session->get('uid'))) { + return intval($session->get('uid')); + } + + return false; + } + + /** + * Returns the public contact id of logged in user or false. + * + * @return int|bool public contact id or false + */ + public static function getPublicContact() + { + static $public_contact_id = false; + + $session = DI::session(); + + if (!$public_contact_id && !empty($session->get('authenticated'))) { + if (!empty($session->get('my_address'))) { + // Local user + $public_contact_id = intval(Contact::getIdForURL($session->get('my_address'), 0, false)); + } elseif (!empty($session->get('visitor_home'))) { + // Remote user + $public_contact_id = intval(Contact::getIdForURL($session->get('visitor_home'), 0, false)); + } + } elseif (empty($session->get('authenticated'))) { + $public_contact_id = false; + } + + return $public_contact_id; + } + + /** + * Returns public contact id of authenticated site visitor or false + * + * @return int|bool visitor_id or false + */ + public static function getRemoteUser() + { + $session = DI::session(); + + if (empty($session->get('authenticated'))) { + return false; + } + + if (!empty($session->get('visitor_id'))) { + return intval($session->get('visitor_id')); + } + + return false; + } + /** * Return the user contact ID of a visitor for the given user ID they are visiting * From 27e9f2b223176e4d26c00e98f9741eca82f4bb63 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 18 Oct 2022 04:35:06 +0000 Subject: [PATCH 2/3] SIGTERM moved --- boot.php | 7 +------ src/App.php | 5 +++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/boot.php b/boot.php index 32a1494c6..d43ef8b0d 100644 --- a/boot.php +++ b/boot.php @@ -36,7 +36,7 @@ use Friendica\Core\Session; * feed for the source. * This can be used in HTML and JavaScript where needed a line break. */ -define('EOL', "
\r\n"); +define('EOL', "
\r\n"); /** * @name Gravity @@ -50,11 +50,6 @@ define('GRAVITY_COMMENT', 6); define('GRAVITY_UNKNOWN', 9); /* @}*/ -// Normally this constant is defined - but not if "pcntl" isn't installed -if (!defined('SIGTERM')) { - define('SIGTERM', 15); -} - /** * Returns the user id of locally logged in user or false. * diff --git a/src/App.php b/src/App.php index d46f69843..55b3e72e0 100644 --- a/src/App.php +++ b/src/App.php @@ -351,6 +351,11 @@ class App { set_time_limit(0); + // Normally this constant is defined - but not if "pcntl" isn't installed + if (!defined('SIGTERM')) { + define('SIGTERM', 15); + } + // Ensure that all "strtotime" operations do run timezone independent date_default_timezone_set('UTC'); From 810ea228f0cbcecf19d64ead7e39997976cff15c Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Tue, 18 Oct 2022 07:57:59 +0200 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Hypolite Petovan --- boot.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/boot.php b/boot.php index d43ef8b0d..36a0745d6 100644 --- a/boot.php +++ b/boot.php @@ -54,6 +54,7 @@ define('GRAVITY_UNKNOWN', 9); * Returns the user id of locally logged in user or false. * * @return int|bool user id or false + * @deprecated since version 2022.12, use Core\Session::getLocalUser() instead */ function local_user() { @@ -64,6 +65,7 @@ function local_user() * Returns the public contact id of logged in user or false. * * @return int|bool public contact id or false + * @deprecated since version 2022.12, use Core\Session:: getPublicContact() instead */ function public_contact() { @@ -74,6 +76,7 @@ function public_contact() * Returns public contact id of authenticated site visitor or false * * @return int|bool visitor_id or false + * @deprecated since version 2022.12, use Core\Session:: getRemoteUser() instead */ function remote_user() {