Twig session(key) function + reworked session functions to accept multi-array like in Laravel

Important: getSession returns NULL now instead of false if session value not found
This commit is contained in:
slawkens 2025-02-02 16:16:59 +01:00
parent de468a8dcd
commit b46ddb43d0
5 changed files with 36 additions and 7 deletions

View File

@ -1,5 +1,10 @@
# Changelog # Changelog
## [master - 02.02.2025]
### Added
* Twig session(key) function + reworked session functions to accept multi-array like in Laravel
## [1.1 - 27.01.2025] ## [1.1 - 27.01.2025]
### Changed ### Changed

View File

@ -1057,17 +1057,36 @@ function get_browser_real_ip() {
return '0'; return '0';
} }
function setSession($key, $data): void { function setSession($key, $value = null): void {
$_SESSION[setting('core.session_prefix') . $key] = $data; if (!is_array($key)) {
$key = [$key => $value];
}
foreach ($key as $arrayKey => $arrayValue) {
if (is_null($arrayValue)) {
unsetSession($arrayKey);
}
else {
$_SESSION[setting('core.session_prefix') . $arrayKey] = $arrayValue;
}
}
} }
function getSession($key) { function getSession($key) {
$key = setting('core.session_prefix') . $key; return $_SESSION[setting('core.session_prefix') . $key] ?? null;
return $_SESSION[$key] ?? false;
} }
function unsetSession($key): void { function unsetSession($key): void {
unset($_SESSION[setting('core.session_prefix') . $key]); unset($_SESSION[setting('core.session_prefix') . $key]);
} }
function session($key): mixed {
if (is_array($key)) {
setSession($key);
return null;
}
return getSession($key);
}
function csrf(bool $return = false): string { function csrf(bool $return = false): string {
return CsrfToken::create($return); return CsrfToken::create($return);
} }

View File

@ -14,12 +14,12 @@ $account_logged = new OTS_Account();
// stay-logged with sessions // stay-logged with sessions
$current_session = getSession('account'); $current_session = getSession('account');
if($current_session !== false) if($current_session)
{ {
$account_logged->load($current_session); $account_logged->load($current_session);
if($account_logged->isLoaded() && $account_logged->getPassword() == getSession('password') if($account_logged->isLoaded() && $account_logged->getPassword() == getSession('password')
//&& (!isset($_SESSION['admin']) || admin()) //&& (!isset($_SESSION['admin']) || admin())
&& (getSession('remember_me') !== false || getSession('last_visit') > time() - 15 * 60)) { // login for 15 minutes if "remember me" is not used && (getSession('remember_me') || getSession('last_visit') > time() - 15 * 60)) { // login for 15 minutes if "remember me" is not used
$logged = true; $logged = true;
} }
else { else {

View File

@ -41,7 +41,7 @@ if(setting('core.template_allow_change'))
} }
else { else {
$template_session = getSession('template'); $template_session = getSession('template');
if ($template_session !== false) { if ($template_session) {
if (!preg_match("/[^A-z0-9_\-]/", $template_session)) { if (!preg_match("/[^A-z0-9_\-]/", $template_session)) {
$template_name = $template_session; $template_name = $template_session;
} }

View File

@ -140,6 +140,11 @@ $function = new TwigFunction('csrfToken', function () {
}); });
$twig->addFunction($function); $twig->addFunction($function);
$function = new TwigFunction('session', function ($key) {
return session($key);
});
$twig->addFunction($function);
$filter = new TwigFilter('urlencode', function ($s) { $filter = new TwigFilter('urlencode', function ($s) {
return urlencode($s); return urlencode($s);
}); });