diff --git a/CHANGELOG.md b/CHANGELOG.md index 497e042a..a90861f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # 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] ### Changed diff --git a/system/functions.php b/system/functions.php index d25dc5ae..77b33415 100644 --- a/system/functions.php +++ b/system/functions.php @@ -1057,17 +1057,36 @@ function get_browser_real_ip() { return '0'; } -function setSession($key, $data): void { - $_SESSION[setting('core.session_prefix') . $key] = $data; +function setSession($key, $value = null): void { + 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) { - $key = setting('core.session_prefix') . $key; - return $_SESSION[$key] ?? false; + return $_SESSION[setting('core.session_prefix') . $key] ?? null; } function unsetSession($key): void { 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 { return CsrfToken::create($return); } diff --git a/system/login.php b/system/login.php index e018c043..42a96111 100644 --- a/system/login.php +++ b/system/login.php @@ -14,12 +14,12 @@ $account_logged = new OTS_Account(); // stay-logged with sessions $current_session = getSession('account'); -if($current_session !== false) +if($current_session) { $account_logged->load($current_session); if($account_logged->isLoaded() && $account_logged->getPassword() == getSession('password') //&& (!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; } else { diff --git a/system/template.php b/system/template.php index 62d3e29b..0434dbac 100644 --- a/system/template.php +++ b/system/template.php @@ -41,7 +41,7 @@ if(setting('core.template_allow_change')) } else { $template_session = getSession('template'); - if ($template_session !== false) { + if ($template_session) { if (!preg_match("/[^A-z0-9_\-]/", $template_session)) { $template_name = $template_session; } diff --git a/system/twig.php b/system/twig.php index 1268f292..fc3125fe 100644 --- a/system/twig.php +++ b/system/twig.php @@ -140,6 +140,11 @@ $function = new TwigFunction('csrfToken', function () { }); $twig->addFunction($function); +$function = new TwigFunction('session', function ($key) { + return session($key); +}); +$twig->addFunction($function); + $filter = new TwigFilter('urlencode', function ($s) { return urlencode($s); });