mirror of
https://github.com/slawkens/myaac.git
synced 2025-04-29 10:49:22 +02:00
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:
parent
de468a8dcd
commit
b46ddb43d0
@ -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
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user