Use symfony session

main
Igor Scheller 7 years ago
parent 96f1d9fd54
commit 2bd127c011

@ -6,6 +6,7 @@ use Engelsystem\Exceptions\Handler as ExceptionHandler;
use Engelsystem\Http\Request; use Engelsystem\Http\Request;
use Engelsystem\Renderer\HtmlEngine; use Engelsystem\Renderer\HtmlEngine;
use Engelsystem\Renderer\Renderer; use Engelsystem\Renderer\Renderer;
use Symfony\Component\HttpFoundation\Session\Session;
/** /**
* This file includes all needed functions, connects to the db etc. * This file includes all needed functions, connects to the db etc.
@ -169,7 +170,9 @@ foreach ($includeFiles as $file) {
/** /**
* Init application * Init application
*/ */
session_start(); $session = new Session();
$session->start();
$request->setSession($session);
gettext_init(); gettext_init();

@ -9,7 +9,7 @@ use Engelsystem\Http\Request;
*/ */
function locale() function locale()
{ {
return $_SESSION['locale']; return session()->get('locale');
} }
/** /**
@ -29,11 +29,12 @@ function gettext_init()
{ {
$locales = config('locales'); $locales = config('locales');
$request = request(); $request = request();
$session = session();
if ($request->has('set_locale') && isset($locales[$request->input('set_locale')])) { if ($request->has('set_locale') && isset($locales[$request->input('set_locale')])) {
$_SESSION['locale'] = $request->input('set_locale'); $session->set('locale', $request->input('set_locale'));
} elseif (!isset($_SESSION['locale'])) { } elseif (!$session->has('locale')) {
$_SESSION['locale'] = config('default_locale'); $session->set('locale', config('default_locale'));
} }
gettext_locale(); gettext_locale();
@ -50,7 +51,7 @@ function gettext_init()
function gettext_locale($locale = null) function gettext_locale($locale = null)
{ {
if ($locale == null) { if ($locale == null) {
$locale = $_SESSION['locale']; $locale = session()->get('locale');
} }
putenv('LC_ALL=' . $locale); putenv('LC_ALL=' . $locale);

@ -7,12 +7,12 @@
*/ */
function msg() function msg()
{ {
if (!isset($_SESSION['msg'])) { $session = session();
return '';
} $message = $session->get('msg', '');
$msg = $_SESSION['msg']; $session->set('msg', '');
$_SESSION['msg'] = '';
return $msg; return $message;
} }
/** /**
@ -61,6 +61,8 @@ function success($msg, $immediately = false)
*/ */
function alert($class, $msg, $immediately = false) function alert($class, $msg, $immediately = false)
{ {
$session = session();
if ($immediately) { if ($immediately) {
if ($msg == '') { if ($msg == '') {
return ''; return '';
@ -68,10 +70,9 @@ function alert($class, $msg, $immediately = false)
return '<div class="alert alert-' . $class . '">' . $msg . '</div>'; return '<div class="alert alert-' . $class . '">' . $msg . '</div>';
} }
if (!isset($_SESSION['msg'])) { $message = $session->get('msg', '');
$_SESSION['msg'] = ''; $message .= alert($class, $msg, true);
} $session->set('msg', $message);
$_SESSION['msg'] .= alert($class, $msg, true);
return null; return null;
} }

@ -19,6 +19,7 @@ function admin_shifts()
{ {
$valid = true; $valid = true;
$request = request(); $request = request();
$session = session();
$start = parse_date('Y-m-d H:i', date('Y-m-d') . ' 00:00'); $start = parse_date('Y-m-d H:i', date('Y-m-d') . ' 00:00');
$end = $start; $end = $start;
$mode = 'single'; $mode = 'single';
@ -270,8 +271,8 @@ function admin_shifts()
} }
// Fürs Anlegen zwischenspeichern: // Fürs Anlegen zwischenspeichern:
$_SESSION['admin_shifts_shifts'] = $shifts; $session->set('admin_shifts_shifts', $shifts);
$_SESSION['admin_shifts_types'] = $needed_angel_types; $session->set('admin_shifts_types', $needed_angel_types);
$hidden_types = ''; $hidden_types = '';
foreach ($needed_angel_types as $type_id => $count) { foreach ($needed_angel_types as $type_id => $count) {
@ -301,16 +302,14 @@ function admin_shifts()
} }
} elseif ($request->has('submit')) { } elseif ($request->has('submit')) {
if ( if (
!isset($_SESSION['admin_shifts_shifts']) !is_array($session->get('admin_shifts_shifts'))
|| !isset($_SESSION['admin_shifts_types']) || !is_array($session->get('admin_shifts_types'))
|| !is_array($_SESSION['admin_shifts_shifts'])
|| !is_array($_SESSION['admin_shifts_types'])
) { ) {
redirect(page_link_to('admin_shifts')); redirect(page_link_to('admin_shifts'));
} }
$needed_angel_types_info = []; $needed_angel_types_info = [];
foreach ($_SESSION['admin_shifts_shifts'] as $shift) { foreach ($session->get('admin_shifts_shifts', []) as $shift) {
$shift['URL'] = null; $shift['URL'] = null;
$shift['PSID'] = null; $shift['PSID'] = null;
$shift_id = Shift_create($shift); $shift_id = Shift_create($shift);
@ -322,7 +321,7 @@ function admin_shifts()
. ' to ' . date('Y-m-d H:i', $shift['end']) . ' to ' . date('Y-m-d H:i', $shift['end'])
); );
foreach ($_SESSION['admin_shifts_types'] as $type_id => $count) { foreach ($session->get('admin_shifts_types', []) as $type_id => $count) {
$angel_type_source = DB::selectOne(' $angel_type_source = DB::selectOne('
SELECT * SELECT *
FROM `AngelTypes` FROM `AngelTypes`
@ -348,8 +347,8 @@ function admin_shifts()
success('Schichten angelegt.'); success('Schichten angelegt.');
redirect(page_link_to('admin_shifts')); redirect(page_link_to('admin_shifts'));
} else { } else {
unset($_SESSION['admin_shifts_shifts']); $session->remove('admin_shifts_shifts');
unset($_SESSION['admin_shifts_types']); $session->remove('admin_shifts_types');
} }
$rid = null; $rid = null;

@ -39,6 +39,7 @@ function guest_register()
$min_password_length = config('min_password_length'); $min_password_length = config('min_password_length');
$event_config = EventConfig(); $event_config = EventConfig();
$request = request(); $request = request();
$session = session();
$msg = ''; $msg = '';
$nick = ''; $nick = '';
@ -226,7 +227,7 @@ function guest_register()
$password_hash, $password_hash,
$comment, $comment,
$hometown, $hometown,
$_SESSION['locale'], $session->get('locale'),
$planned_arrival_date, $planned_arrival_date,
] ]
); );
@ -377,25 +378,36 @@ function guest_register()
]); ]);
} }
/**
* @return string
*/
function entry_required() function entry_required()
{ {
return '<span class="text-info glyphicon glyphicon-warning-sign"></span>'; return '<span class="text-info glyphicon glyphicon-warning-sign"></span>';
} }
/**
* @return bool
*/
function guest_logout() function guest_logout()
{ {
session_destroy(); session()->invalidate();
redirect(page_link_to('start')); redirect(page_link_to('start'));
return true; return true;
} }
/**
* @return string
*/
function guest_login() function guest_login()
{ {
$nick = ''; $nick = '';
$request = request(); $request = request();
unset($_SESSION['uid']); $session = session();
$valid = true; $valid = true;
$session->remove('uid');
if ($request->has('submit')) { if ($request->has('submit')) {
if ($request->has('nick') && strlen(User_validate_Nick($request->input('nick'))) > 0) { if ($request->has('nick') && strlen(User_validate_Nick($request->input('nick'))) > 0) {
$nick = User_validate_Nick($request->input('nick')); $nick = User_validate_Nick($request->input('nick'));
@ -420,8 +432,8 @@ function guest_login()
} }
if ($valid && !empty($login_user)) { if ($valid && !empty($login_user)) {
$_SESSION['uid'] = $login_user['UID']; $session->set('uid', $login_user['UID']);
$_SESSION['locale'] = $login_user['Sprache']; $session->set('locale', $login_user['Sprache']);
redirect(page_link_to('news')); redirect(page_link_to('news'));
} }
@ -477,6 +489,9 @@ function guest_login()
]); ]);
} }
/**
* @return string
*/
function get_register_hint() function get_register_hint()
{ {
global $privileges; global $privileges;

@ -164,6 +164,7 @@ function user_settings_locale($user_source, $locales)
{ {
$valid = true; $valid = true;
$request = request(); $request = request();
$session = session();
if ($request->has('language') && isset($locales[$request->input('language')])) { if ($request->has('language') && isset($locales[$request->input('language')])) {
$user_source['Sprache'] = $request->input('language'); $user_source['Sprache'] = $request->input('language');
@ -182,7 +183,7 @@ function user_settings_locale($user_source, $locales)
$user_source['UID'], $user_source['UID'],
] ]
); );
$_SESSION['locale'] = $user_source['Sprache']; $session->set('locale', $user_source['Sprache']);
success('Language changed.'); success('Language changed.');
redirect(page_link_to('user_settings')); redirect(page_link_to('user_settings'));

@ -167,20 +167,23 @@ function view_user_shifts()
{ {
global $user, $privileges, $ical_shifts; global $user, $privileges, $ical_shifts;
$session = session();
$ical_shifts = []; $ical_shifts = [];
$days = load_days(); $days = load_days();
$rooms = load_rooms(); $rooms = load_rooms();
$types = load_types(); $types = load_types();
if (!isset($_SESSION['ShiftsFilter'])) { if (!$session->has('ShiftsFilter')) {
$room_ids = [ $room_ids = [
$rooms[0]['id'] $rooms[0]['id']
]; ];
$type_ids = array_map('get_ids_from_array', $types); $type_ids = array_map('get_ids_from_array', $types);
$_SESSION['ShiftsFilter'] = new ShiftsFilter(in_array('user_shifts_admin', $privileges), $room_ids, $type_ids); $shiftsFilter = new ShiftsFilter(in_array('user_shifts_admin', $privileges), $room_ids, $type_ids);
$session->set('ShiftsFilter', $shiftsFilter);
} }
update_ShiftsFilter($_SESSION['ShiftsFilter'], in_array('user_shifts_admin', $privileges), $days);
$shiftsFilter = $_SESSION['ShiftsFilter']; $shiftsFilter = $session->get('ShiftsFilter');
update_ShiftsFilter($shiftsFilter, in_array('user_shifts_admin', $privileges), $days);
$shiftCalendarRenderer = shiftCalendarRendererByShiftFilter($shiftsFilter); $shiftCalendarRenderer = shiftCalendarRendererByShiftFilter($shiftsFilter);

@ -10,8 +10,10 @@ function load_auth()
global $user, $privileges; global $user, $privileges;
$user = null; $user = null;
if (isset($_SESSION['uid'])) { $session = session();
$user = DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$_SESSION['uid']]);
if ($session->has('uid')) {
$user = DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$session->get('uid')]);
if (!empty($user)) { if (!empty($user)) {
// User ist eingeloggt, Datensatz zur Verfügung stellen und Timestamp updaten // User ist eingeloggt, Datensatz zur Verfügung stellen und Timestamp updaten
DB::update(' DB::update('
@ -21,12 +23,13 @@ function load_auth()
LIMIT 1 LIMIT 1
', [ ', [
time(), time(),
$_SESSION['uid'], $session->get('uid'),
]); ]);
$privileges = privileges_for_user($user['UID']); $privileges = privileges_for_user($user['UID']);
return; return;
} }
unset($_SESSION['uid']);
$session->remove('uid');
} }
// guest privileges // guest privileges

@ -5,6 +5,7 @@ use Engelsystem\Config\Config;
use Engelsystem\Http\Request; use Engelsystem\Http\Request;
use Engelsystem\Renderer\Renderer; use Engelsystem\Renderer\Renderer;
use Engelsystem\Routing\UrlGenerator; use Engelsystem\Routing\UrlGenerator;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/** /**
* Get or set config values * Get or set config values
@ -42,6 +43,22 @@ function request($key = null, $default = null)
return $request->input($key, $default); return $request->input($key, $default);
} }
/**
* @param string $key
* @param mixed $default
* @return SessionInterface|mixed
*/
function session($key = null, $default = null)
{
$session = request()->getSession();
if (is_null($key)) {
return $session;
}
return $session->get($key, $default);
}
/** /**
* @param string $template * @param string $template
* @param mixed[] $data * @param mixed[] $data

Loading…
Cancel
Save