From 73175e2b64c85c7a8c528c76452cd82ffa99f925 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Mon, 28 Aug 2017 16:21:10 +0200 Subject: #337: Added routing --- src/Http/Request.php | 108 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 103 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/Http/Request.php b/src/Http/Request.php index 2efd1e1d..ded1c95b 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -9,19 +9,73 @@ class Request /** @var self */ protected static $instance; + /** @var array of GET data */ + protected $query; + /** @var array of POST data */ protected $request; - /** @var array of GET data */ - protected $query; + /** @var array of SERVER data */ + protected $server; + + /** @var string */ + protected $scheme; + + /** @var string */ + protected $host; + + /** @var string */ + protected $baseUrl = ''; + + /** @var string */ + protected $path; /** * Initialize request + * + * @param array $query The GET data + * @param array $request the POST data + * @param array $server the SERVER data + * @param string $baseUrl base url to use for links */ - public function create() + public function create(array $query, array $request, array $server, $baseUrl = null) { - $this->request = $_POST; - $this->query = $_GET; + $this->query = $query; + $this->request = $request; + $this->server = array_merge([ + 'SERVER_NAME' => 'localhost', + 'HTTP_HOST' => 'localhost', + 'SERVER_PORT' => 80, + 'REQUEST_URI' => '/', + ], $server); + + if (isset($this->server['HTTPS']) && $this->server['HTTPS'] == 'off') { + unset($this->server['HTTPS']); + } + + $uri = $this->server['REQUEST_URI']; + $uri = '/' . ltrim($uri, '/'); + $uri = explode('?', $uri); + $this->path = array_shift($uri); + + $components = parse_url($baseUrl); + if (!$components) { + $components = []; + } + + $this->scheme = (isset($components['scheme']) ? $components['scheme'] : ($this->isSecure() ? 'https' : 'http')); + $this->host = (isset($components['host']) ? $components['host'] : $this->server['SERVER_NAME']); + + if (isset($components['path'])) { + $this->baseUrl = '/' . ltrim($components['path'], '/'); + $this->path = preg_replace('~^' . preg_quote($this->baseUrl, '~') . '~i', '', $this->path); + $this->path = '/' . ltrim($this->path, '/'); + } + } + + public function isSecure() + { + return isset($this->server['HTTPS']); } /** @@ -87,6 +141,50 @@ class Request return !empty($value); } + /** + * Get the requested path + * + * @return string + */ + public function path() + { + // @TODO: base uri? + return $this->path; + } + + public function url() + { + return $this->getSchemeAndHttpHost() . $this->getBaseUrl() . '/' . $this->path(); + } + + /** + * @return string + */ + public function root() + { + return $this->baseUrl; + } + + public function getSchemeAndHttpHost() + { + return $this->getScheme() . '://' . $this->getHttpHost(); + } + + public function getScheme() + { + return $this->scheme; + } + + public function getHttpHost() + { + return $this->host; + } + + public function getBaseUrl() + { + return $this->baseUrl; + } + /** * @return self * @throws ErrorException -- cgit v1.2.3-54-g00ecf From cc01c906ba63b3797bf2b9ef92a6854fe2ddbefb Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 29 Aug 2017 16:21:25 +0200 Subject: #336: Integration of symfony/http-foundation request --- composer.json | 5 +- config/config.default.php | 5 +- includes/controller/angeltypes_controller.php | 2 +- includes/controller/shift_entries_controller.php | 2 +- includes/controller/shifts_controller.php | 25 ++-- includes/controller/users_controller.php | 12 +- includes/engelsystem_provider.php | 5 +- includes/helper/internationalization_helper.php | 14 ++- includes/pages/admin_groups.php | 5 +- includes/pages/admin_news.php | 7 +- includes/pages/admin_rooms.php | 13 ++- includes/pages/admin_shifts.php | 10 +- includes/pages/admin_user.php | 40 +++---- includes/pages/guest_login.php | 8 +- includes/pages/user_atom.php | 17 +-- includes/pages/user_news.php | 10 +- includes/pages/user_settings.php | 8 +- includes/pages/user_shifts.php | 4 +- includes/sys_form.php | 9 +- includes/sys_menu.php | 18 +-- includes/sys_page.php | 14 ++- phpunit.xml | 15 +-- public/index.php | 8 +- src/Exceptions/Handler.php | 56 ++++++++- src/Http/Request.php | 138 ++--------------------- src/Routing/UrlGenerator.php | 27 +++++ src/helpers.php | 11 ++ 27 files changed, 230 insertions(+), 258 deletions(-) create mode 100644 src/Routing/UrlGenerator.php (limited to 'src') diff --git a/composer.json b/composer.json index d2e0108b..e6d20108 100644 --- a/composer.json +++ b/composer.json @@ -16,10 +16,11 @@ "require": { "php": ">=5.6.4", "erusev/parsedown": "1.6.*", - "twbs/bootstrap": "^3.3" + "twbs/bootstrap": "^3.3", + "symfony/http-foundation": "^3.3" }, "require-dev": { - "phpunit/phpunit": "^6.2" + "phpunit/phpunit": "^6.3" }, "autoload": { "psr-4": { diff --git a/config/config.default.php b/config/config.default.php index c3a939cd..a0303b15 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -20,9 +20,6 @@ return [ // Set to development to enable debugging messages 'environment' => 'production', - // Site URL, used to generate links on page (https://example.com/[sub-dir/]) - 'url' => null, - // URL to the angel faq and job description 'faq_url' => 'https://events.ccc.de/congress/2013/wiki/Static:Volunteers', @@ -58,7 +55,7 @@ return [ // Blowfish '$2y$13' // SHA-256 '$5$rounds=5000' // SHA-512 '$6$rounds=5000' - 'crypt_alg' => '$6$rounds=5000', // SHA-512 + 'crypt_alg' => '$6$rounds=5000', 'min_password_length' => 8, diff --git a/includes/controller/angeltypes_controller.php b/includes/controller/angeltypes_controller.php index d60b6fc7..8c1cbe5d 100644 --- a/includes/controller/angeltypes_controller.php +++ b/includes/controller/angeltypes_controller.php @@ -127,7 +127,7 @@ function angeltype_edit_controller() if (!$supporter_mode) { if ($request->has('name')) { - $result = AngelType_validate_name($request->get('name'), $angeltype); + $result = AngelType_validate_name($request->postData('name'), $angeltype); $angeltype['name'] = $result->getValue(); if (!$result->isValid()) { $valid = false; diff --git a/includes/controller/shift_entries_controller.php b/includes/controller/shift_entries_controller.php index 38aad5bb..3890241d 100644 --- a/includes/controller/shift_entries_controller.php +++ b/includes/controller/shift_entries_controller.php @@ -27,10 +27,10 @@ function shift_entry_add_controller() } $shift = Shift($shift_id); - $shift['Name'] = $room_array[$shift['RID']]; if ($shift == null) { redirect(page_link_to('user_shifts')); } + $shift['Name'] = $room_array[$shift['RID']]; $type_id = 0; if ($request->has('type_id') && preg_match('/^\d*$/', $request->input('type_id'))) { diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index f4f3f119..f68f64fe 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -62,7 +62,7 @@ function shift_edit_controller() $angeltypes = select_array(AngelTypes(), 'id', 'name'); $shifttypes = select_array(ShiftTypes(), 'id', 'name'); - $needed_angel_types = select_array(NeededAngelTypes_by_shift($shift_id), 'id', 'count'); + $needed_angel_types = select_array(NeededAngelTypes_by_shift($shift_id), 'angel_type_id', 'count'); foreach (array_keys($angeltypes) as $angeltype_id) { if (!isset($needed_angel_types[$angeltype_id])) { $needed_angel_types[$angeltype_id] = 0; @@ -117,15 +117,20 @@ function shift_edit_controller() $msg .= error(_('The ending time has to be after the starting time.'), true); } - foreach ($needed_angel_types as $needed_angeltype_id => $needed_angeltype_name) { - if ($request->has('type_' . $needed_angeltype_id) && test_request_int('type_' . $needed_angeltype_id)) { - $needed_angel_types[$needed_angeltype_id] = trim($request->input('type_' . $needed_angeltype_id)); - } else { - $valid = false; - $msg .= error(sprintf( - _('Please check your input for needed angels of type %s.'), - $needed_angeltype_name - ), true); + foreach ($needed_angel_types as $needed_angeltype_id => $count) { + $needed_angel_types[$needed_angeltype_id] = 0; + + $queryKey = 'type_' . $needed_angeltype_id; + if ($request->has($queryKey)) { + if (test_request_int($queryKey)) { + $needed_angel_types[$needed_angeltype_id] = trim($request->input($queryKey)); + } else { + $valid = false; + $msg .= error(sprintf( + _('Please check your input for needed angels of type %s.'), + $angeltypes[$needed_angeltype_id] + ), true); + } } } diff --git a/includes/controller/users_controller.php b/includes/controller/users_controller.php index 6dc74d68..b8a1fdbd 100644 --- a/includes/controller/users_controller.php +++ b/includes/controller/users_controller.php @@ -47,7 +47,7 @@ function user_delete_controller() $request = request(); if ($request->has('user_id')) { - $user_source = User($request->get('user_id')); + $user_source = User($request->query->get('user_id')); } else { $user_source = $user; } @@ -68,7 +68,7 @@ function user_delete_controller() if ( !( $request->has('password') - && verify_password($request->post('password'), $user['Passwort'], $user['UID']) + && verify_password($request->postData('password'), $user['Passwort'], $user['UID']) ) ) { $valid = false; @@ -307,9 +307,9 @@ function user_password_recovery_set_new_controller() if ( $request->has('password') - && strlen($request->post('password')) >= config('min_password_length') + && strlen($request->postData('password')) >= config('min_password_length') ) { - if ($request->post('password') != $request->post('password2')) { + if ($request->postData('password') != $request->postData('password2')) { $valid = false; error(_('Your passwords don\'t match.')); } @@ -319,7 +319,7 @@ function user_password_recovery_set_new_controller() } if ($valid) { - set_password($user_source['UID'], $request->post('password')); + set_password($user_source['UID'], $request->postData('password')); success(_('Password saved.')); redirect(page_link_to('login')); } @@ -363,7 +363,7 @@ function user_password_recovery_start_controller() _('Password recovery'), sprintf( _('Please visit %s to recover your password.'), - page_link_to_absolute('user_password_recovery', ['token' => $token]) + page_link_to('user_password_recovery', ['token' => $token]) ) ); success(_('We sent an email containing your password recovery link.')); diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index c065d332..e6b457d9 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -32,9 +32,10 @@ date_default_timezone_set($config->get('timezone')); /** * Initialize Request + * + * @var Request $request */ -$request = new Request(); -$request->create($_GET, $_POST, $_SERVER, config('url')); +$request = Request::createFromGlobals(); $request::setInstance($request); /** diff --git a/includes/helper/internationalization_helper.php b/includes/helper/internationalization_helper.php index d2dbcdbd..83faabb0 100644 --- a/includes/helper/internationalization_helper.php +++ b/includes/helper/internationalization_helper.php @@ -1,5 +1,7 @@ 0 ? '&' : '?') . 'set_locale='; + $request = Request::getInstance(); $items = []; foreach (config('locales') as $locale => $name) { + $url = url($request->getPathInfo(), ['set_locale' => $locale]); + $items[] = toolbar_item_link( - htmlspecialchars($url) . $locale, + htmlspecialchars($url), '', - '' . $name . ' ' . $name + sprintf( + '%s %2$s', + url('pic/flag/' . $locale . '.png'), + $name + ) ); } return $items; diff --git a/includes/pages/admin_groups.php b/includes/pages/admin_groups.php index d64afe76..1de8bfb7 100644 --- a/includes/pages/admin_groups.php +++ b/includes/pages/admin_groups.php @@ -81,7 +81,8 @@ function admin_groups() 'privileges[]', $privilege['desc'] . ' (' . $privilege['name'] . ')', $privilege['group_id'] != '', - $privilege['id'] + $privilege['id'], + 'privilege-' . $privilege['name'] ); $privileges_html .= sprintf( ' %s %s', @@ -112,7 +113,7 @@ function admin_groups() } $group = DB::select('SELECT * FROM `Groups` WHERE `UID`=? LIMIT 1', [$group_id]); - $privileges = $request->get('privileges'); + $privileges = $request->postData('privileges'); if (!is_array($privileges)) { $privileges = []; } diff --git a/includes/pages/admin_news.php b/includes/pages/admin_news.php index a5354da7..64a54f4b 100644 --- a/includes/pages/admin_news.php +++ b/includes/pages/admin_news.php @@ -62,14 +62,15 @@ function admin_news() ', [ time(), - $request->post('eBetreff'), - $request->post('eText'), + $request->postData('eBetreff'), + $request->postData('eText'), $user['UID'], $request->has('eTreffen') ? 1 : 0, $news_id ] ); - engelsystem_log('News updated: ' . $request->post('eBetreff')); + + engelsystem_log('News updated: ' . $request->postData('eBetreff')); success(_('News entry updated.')); redirect(page_link_to('news')); break; diff --git a/includes/pages/admin_rooms.php b/includes/pages/admin_rooms.php index 8a7720d8..457114a0 100644 --- a/includes/pages/admin_rooms.php +++ b/includes/pages/admin_rooms.php @@ -110,11 +110,14 @@ function admin_rooms() } foreach ($angeltypes as $angeltype_id => $angeltype) { - if ( - $request->has('angeltype_count_' . $angeltype_id) - && preg_match('/^\d{1,4}$/', $request->input('angeltype_count_' . $angeltype_id)) - ) { - $angeltypes_count[$angeltype_id] = $request->input('angeltype_count_' . $angeltype_id); + $angeltypes_count[$angeltype_id] = 0; + $queryKey = 'angeltype_count_' . $angeltype_id; + if (!$request->has($queryKey)) { + continue; + } + + if (preg_match('/^\d{1,4}$/', $request->input($queryKey))) { + $angeltypes_count[$angeltype_id] = $request->input($queryKey); } else { $valid = false; $msg .= error(sprintf(_('Please enter needed angels for type %s.'), $angeltype), true); diff --git a/includes/pages/admin_shifts.php b/includes/pages/admin_shifts.php index 5b53f9cd..d36635f7 100644 --- a/includes/pages/admin_shifts.php +++ b/includes/pages/admin_shifts.php @@ -135,16 +135,14 @@ function admin_shifts() } elseif ($request->input('angelmode') == 'manually') { $angelmode = 'manually'; foreach ($types as $type) { - if ( - $request->has('type_' . $type['id']) - && preg_match('/^\d+$/', trim($request->input('type_' . $type['id']))) - ) { - $needed_angel_types[$type['id']] = trim($request->input('type_' . $type['id'])); + if (preg_match('/^\d+$/', trim($request->input('type_' . $type['id'], 0)))) { + $needed_angel_types[$type['id']] = trim($request->input('type_' . $type['id'], 0)); } else { $valid = false; error(sprintf(_('Please check the needed angels for team %s.'), $type['name'])); } } + if (array_sum($needed_angel_types) == 0) { $valid = false; error(_('There are 0 angels needed. Please enter the amounts of needed angels.')); @@ -306,7 +304,7 @@ function admin_shifts() } } elseif ($request->has('submit')) { if ( - !$request->has('admin_shifts_shifts') + !isset($_SESSION['admin_shifts_shifts']) || !isset($_SESSION['admin_shifts_types']) || !is_array($_SESSION['admin_shifts_shifts']) || !is_array($_SESSION['admin_shifts_types']) diff --git a/includes/pages/admin_user.php b/includes/pages/admin_user.php index 00113507..aea68f52 100644 --- a/includes/pages/admin_user.php +++ b/includes/pages/admin_user.php @@ -261,7 +261,7 @@ function admin_user() `Handy` = ?, `Alter` =?, `DECT` = ?, - ' . ($user_source['email_by_human_allowed'] ? '`email` = ' . DB::getPdo()->quote($request->post('eemail')) . ',' : '') . ' + ' . ($user_source['email_by_human_allowed'] ? '`email` = ' . DB::getPdo()->quote($request->postData('eemail')) . ',' : '') . ' `jabber` = ?, `Size` = ?, `Gekommen`= ?, @@ -272,34 +272,34 @@ function admin_user() WHERE `UID` = ? LIMIT 1'; DB::update($sql, [ - $request->post('eNick'), - $request->post('eName'), - $request->post('eVorname'), - $request->post('eTelefon'), - $request->post('eHandy'), - $request->post('eAlter'), - $request->post('eDECT'), - $request->post('ejabber'), - $request->post('eSize'), - $request->post('eGekommen'), - $request->post('eAktiv'), + $request->postData('eNick'), + $request->postData('eName'), + $request->postData('eVorname'), + $request->postData('eTelefon'), + $request->postData('eHandy'), + $request->postData('eAlter'), + $request->postData('eDECT'), + $request->postData('ejabber'), + $request->postData('eSize'), + $request->postData('eGekommen'), + $request->postData('eAktiv'), $force_active, - $request->post('eTshirt'), - $request->post('Hometown'), + $request->postData('eTshirt'), + $request->postData('Hometown'), $user_id, ]); engelsystem_log( - 'Updated user: ' . $request->post('eNick') . ', ' . $request->post('eSize') - . ', arrived: ' . $request->post('eVorname') - . ', active: ' . $request->post('eAktiv') - . ', tshirt: ' . $request->post('eTshirt') + 'Updated user: ' . $request->postData('eNick') . ', ' . $request->postData('eSize') + . ', arrived: ' . $request->postData('eVorname') + . ', active: ' . $request->postData('eAktiv') + . ', tshirt: ' . $request->postData('eTshirt') ); $html .= success('Änderung wurde gespeichert...' . "\n", true); break; case 'change_pw': - if ($request->post('new_pw') != '' && $request->post('new_pw') == $request->post('new_pw2')) { - set_password($user_id, $request->post('new_pw')); + if ($request->postData('new_pw') != '' && $request->postData('new_pw') == $request->postData('new_pw2')) { + set_password($user_id, $request->postData('new_pw')); $user_source = User($user_id); engelsystem_log('Set new password for ' . User_Nick_render($user_source)); $html .= success('Passwort neu gesetzt.', true); diff --git a/includes/pages/guest_login.php b/includes/pages/guest_login.php index b83b8382..3966b55c 100644 --- a/includes/pages/guest_login.php +++ b/includes/pages/guest_login.php @@ -127,8 +127,8 @@ function guest_register() } } - if ($request->has('password') && strlen($request->post('password')) >= $min_password_length) { - if ($request->post('password') != $request->post('password2')) { + if ($request->has('password') && strlen($request->postData('password')) >= $min_password_length) { + if ($request->postData('password') != $request->postData('password2')) { $valid = false; $msg .= error(_('Your passwords don\'t match.'), true); } @@ -234,7 +234,7 @@ function guest_register() // Assign user-group and set password $user_id = DB::getPdo()->lastInsertId(); DB::insert('INSERT INTO `UserGroups` (`uid`, `group_id`) VALUES (?, -2)', [$user_id]); - set_password($user_id, $request->post('password')); + set_password($user_id, $request->postData('password')); // Assign angel-types $user_angel_types_info = []; @@ -403,7 +403,7 @@ function guest_login() if (count($login_user) > 0) { $login_user = $login_user[0]; if ($request->has('password')) { - if (!verify_password($request->post('password'), $login_user['Passwort'], $login_user['UID'])) { + if (!verify_password($request->postData('password'), $login_user['Passwort'], $login_user['UID'])) { $valid = false; error(_('Your password is incorrect. Please try it again.')); } diff --git a/includes/pages/user_atom.php b/includes/pages/user_atom.php index 98ace9cc..2991bdbf 100644 --- a/includes/pages/user_atom.php +++ b/includes/pages/user_atom.php @@ -1,6 +1,7 @@ Engelsystem - ' . $_SERVER['HTTP_HOST'] + ' . $request->getHttpHost() . htmlspecialchars(preg_replace( '#[&?]key=[a-f\d]{32}#', '', - $_SERVER['REQUEST_URI'] + $request->getRequestUri() )) . ' ' . date('Y-m-d\TH:i:sP', $news_entries[0]['Datum']) . '' . "\n"; @@ -64,11 +66,12 @@ function make_atom_entries_from_news($news_entries) function make_atom_entry_from_news($news_entry) { - return ' + return ' + ' . htmlspecialchars($news_entry['Betreff']) . ' - - ' . preg_replace('#^https?://#', '', page_link_to_absolute('news')) . '-' . $news_entry['ID'] . ' - ' . date('Y-m-d\TH:i:sP', $news_entry['Datum']) . ' + + ' . preg_replace('#^https?://#', '', page_link_to('news_comments', ['nid' => $news_entry['ID']])) . ' + ' . date('Y-m-d\TH:i:sP', $news_entry['Datum']) . ' ' . htmlspecialchars($news_entry['Text']) . ' - ' . "\n"; + ' . "\n"; } diff --git a/includes/pages/user_news.php b/includes/pages/user_news.php index 2dd141ec..bdbb0645 100644 --- a/includes/pages/user_news.php +++ b/includes/pages/user_news.php @@ -186,9 +186,9 @@ function user_news() $html = '

' . news_title() . '

' . msg(); - $isMeeting = $request->post('treffen'); + $isMeeting = $request->postData('treffen'); if ($request->has('text') && $request->has('betreff') && in_array('admin_news', $privileges)) { - if (!$request->has('treffen') || !in_array('admin_news', $privileges)) { + if (!$request->has('treffen')) { $isMeeting = 0; } DB::insert(' @@ -197,13 +197,13 @@ function user_news() ', [ time(), - $request->post('betreff'), - $request->post('text'), + $request->postData('betreff'), + $request->postData('text'), $user['UID'], $isMeeting, ] ); - engelsystem_log('Created news: ' . $_POST['betreff'] . ', treffen: ' . $isMeeting); + engelsystem_log('Created news: ' . $request->postData('betreff') . ', treffen: ' . $isMeeting); success(_('Entry saved.')); redirect(page_link_to('news')); } diff --git a/includes/pages/user_settings.php b/includes/pages/user_settings.php index 667e73d9..9a43f5ce 100644 --- a/includes/pages/user_settings.php +++ b/includes/pages/user_settings.php @@ -101,14 +101,14 @@ function user_settings_password($user_source) $request = request(); if ( !$request->has('password') - || !verify_password($request->post('password'), $user_source['Passwort'], $user_source['UID']) + || !verify_password($request->postData('password'), $user_source['Passwort'], $user_source['UID']) ) { error(_('-> not OK. Please try again.')); - } elseif (strlen($request->post('new_password')) < config('min_password_length')) { + } elseif (strlen($request->postData('new_password')) < config('min_password_length')) { error(_('Your password is to short (please use at least 6 characters).')); - } elseif ($request->post('new_password') != $request->post('new_password2')) { + } elseif ($request->postData('new_password') != $request->postData('new_password2')) { error(_('Your passwords don\'t match.')); - } elseif (set_password($user_source['UID'], $request->post('new_password'))) { + } elseif (set_password($user_source['UID'], $request->postData('new_password'))) { success(_('Password saved.')); } else { error(_('Failed setting password.')); diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php index cd18a037..db0bb193 100644 --- a/includes/pages/user_shifts.php +++ b/includes/pages/user_shifts.php @@ -229,8 +229,8 @@ function view_user_shifts() 'shifts_table' => msg() . $shiftCalendarRenderer->render(), 'ical_text' => '

' . _('iCal export') . '

' . sprintf( _('Export of shown shifts. iCal format or JSON format available (please keep secret, otherwise reset the api key).'), - page_link_to_absolute('ical', ['key' => $user['api_key']]), - page_link_to_absolute('shifts_json_export', ['key' => $user['api_key']]), + page_link_to('ical', ['key' => $user['api_key']]), + page_link_to('shifts_json_export', ['key' => $user['api_key']]), page_link_to('user_myshifts', ['reset' => 1]) ) . '

', 'filter' => _('Filter') diff --git a/includes/sys_form.php b/includes/sys_form.php index 936e3203..78e97792 100644 --- a/includes/sys_form.php +++ b/includes/sys_form.php @@ -144,10 +144,15 @@ function form_multi_checkboxes($names, $label, $items, $selected, $disabled = [] * @param string $label * @param string $selected * @param string $value + * @param string $id * @return string */ -function form_checkbox($name, $label, $selected, $value = 'checked') +function form_checkbox($name, $label, $selected, $value = 'checked', $id = null) { + if (is_null($id)) { + $id = $name; + } + return '
-- cgit v1.2.3-54-g00ecf From 2bd127c011846aad69731d1d63535a3d4f100af0 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 30 Aug 2017 19:57:01 +0200 Subject: Use symfony session --- includes/engelsystem_provider.php | 5 ++++- includes/helper/internationalization_helper.php | 11 ++++++----- includes/helper/message_helper.php | 21 +++++++++++---------- includes/pages/admin_shifts.php | 19 +++++++++---------- includes/pages/guest_login.php | 25 ++++++++++++++++++++----- includes/pages/user_settings.php | 3 ++- includes/pages/user_shifts.php | 11 +++++++---- includes/sys_auth.php | 11 +++++++---- src/helpers.php | 17 +++++++++++++++++ 9 files changed, 83 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index 65a319e9..aed331d4 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -6,6 +6,7 @@ use Engelsystem\Exceptions\Handler as ExceptionHandler; use Engelsystem\Http\Request; use Engelsystem\Renderer\HtmlEngine; use Engelsystem\Renderer\Renderer; +use Symfony\Component\HttpFoundation\Session\Session; /** * This file includes all needed functions, connects to the db etc. @@ -169,7 +170,9 @@ foreach ($includeFiles as $file) { /** * Init application */ -session_start(); +$session = new Session(); +$session->start(); +$request->setSession($session); gettext_init(); diff --git a/includes/helper/internationalization_helper.php b/includes/helper/internationalization_helper.php index 83faabb0..131941e9 100644 --- a/includes/helper/internationalization_helper.php +++ b/includes/helper/internationalization_helper.php @@ -9,7 +9,7 @@ use Engelsystem\Http\Request; */ function locale() { - return $_SESSION['locale']; + return session()->get('locale'); } /** @@ -29,11 +29,12 @@ function gettext_init() { $locales = config('locales'); $request = request(); + $session = session(); if ($request->has('set_locale') && isset($locales[$request->input('set_locale')])) { - $_SESSION['locale'] = $request->input('set_locale'); - } elseif (!isset($_SESSION['locale'])) { - $_SESSION['locale'] = config('default_locale'); + $session->set('locale', $request->input('set_locale')); + } elseif (!$session->has('locale')) { + $session->set('locale', config('default_locale')); } gettext_locale(); @@ -50,7 +51,7 @@ function gettext_init() function gettext_locale($locale = null) { if ($locale == null) { - $locale = $_SESSION['locale']; + $locale = session()->get('locale'); } putenv('LC_ALL=' . $locale); diff --git a/includes/helper/message_helper.php b/includes/helper/message_helper.php index 1f429c27..7a42a7b7 100644 --- a/includes/helper/message_helper.php +++ b/includes/helper/message_helper.php @@ -7,12 +7,12 @@ */ function msg() { - if (!isset($_SESSION['msg'])) { - return ''; - } - $msg = $_SESSION['msg']; - $_SESSION['msg'] = ''; - return $msg; + $session = session(); + + $message = $session->get('msg', ''); + $session->set('msg', ''); + + return $message; } /** @@ -61,6 +61,8 @@ function success($msg, $immediately = false) */ function alert($class, $msg, $immediately = false) { + $session = session(); + if ($immediately) { if ($msg == '') { return ''; @@ -68,10 +70,9 @@ function alert($class, $msg, $immediately = false) return '
' . $msg . '
'; } - if (!isset($_SESSION['msg'])) { - $_SESSION['msg'] = ''; - } - $_SESSION['msg'] .= alert($class, $msg, true); + $message = $session->get('msg', ''); + $message .= alert($class, $msg, true); + $session->set('msg', $message); return null; } diff --git a/includes/pages/admin_shifts.php b/includes/pages/admin_shifts.php index 04d88a4f..c77bd46d 100644 --- a/includes/pages/admin_shifts.php +++ b/includes/pages/admin_shifts.php @@ -19,6 +19,7 @@ function admin_shifts() { $valid = true; $request = request(); + $session = session(); $start = parse_date('Y-m-d H:i', date('Y-m-d') . ' 00:00'); $end = $start; $mode = 'single'; @@ -270,8 +271,8 @@ function admin_shifts() } // Fürs Anlegen zwischenspeichern: - $_SESSION['admin_shifts_shifts'] = $shifts; - $_SESSION['admin_shifts_types'] = $needed_angel_types; + $session->set('admin_shifts_shifts', $shifts); + $session->set('admin_shifts_types', $needed_angel_types); $hidden_types = ''; foreach ($needed_angel_types as $type_id => $count) { @@ -301,16 +302,14 @@ function admin_shifts() } } elseif ($request->has('submit')) { if ( - !isset($_SESSION['admin_shifts_shifts']) - || !isset($_SESSION['admin_shifts_types']) - || !is_array($_SESSION['admin_shifts_shifts']) - || !is_array($_SESSION['admin_shifts_types']) + !is_array($session->get('admin_shifts_shifts')) + || !is_array($session->get('admin_shifts_types')) ) { redirect(page_link_to('admin_shifts')); } $needed_angel_types_info = []; - foreach ($_SESSION['admin_shifts_shifts'] as $shift) { + foreach ($session->get('admin_shifts_shifts', []) as $shift) { $shift['URL'] = null; $shift['PSID'] = null; $shift_id = Shift_create($shift); @@ -322,7 +321,7 @@ function admin_shifts() . ' 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(' SELECT * FROM `AngelTypes` @@ -348,8 +347,8 @@ function admin_shifts() success('Schichten angelegt.'); redirect(page_link_to('admin_shifts')); } else { - unset($_SESSION['admin_shifts_shifts']); - unset($_SESSION['admin_shifts_types']); + $session->remove('admin_shifts_shifts'); + $session->remove('admin_shifts_types'); } $rid = null; diff --git a/includes/pages/guest_login.php b/includes/pages/guest_login.php index 4a77b40c..9179c6c4 100644 --- a/includes/pages/guest_login.php +++ b/includes/pages/guest_login.php @@ -39,6 +39,7 @@ function guest_register() $min_password_length = config('min_password_length'); $event_config = EventConfig(); $request = request(); + $session = session(); $msg = ''; $nick = ''; @@ -226,7 +227,7 @@ function guest_register() $password_hash, $comment, $hometown, - $_SESSION['locale'], + $session->get('locale'), $planned_arrival_date, ] ); @@ -377,25 +378,36 @@ function guest_register() ]); } +/** + * @return string + */ function entry_required() { return ''; } +/** + * @return bool + */ function guest_logout() { - session_destroy(); + session()->invalidate(); redirect(page_link_to('start')); return true; } +/** + * @return string + */ function guest_login() { $nick = ''; $request = request(); - unset($_SESSION['uid']); + $session = session(); $valid = true; + $session->remove('uid'); + if ($request->has('submit')) { if ($request->has('nick') && strlen(User_validate_Nick($request->input('nick'))) > 0) { $nick = User_validate_Nick($request->input('nick')); @@ -420,8 +432,8 @@ function guest_login() } if ($valid && !empty($login_user)) { - $_SESSION['uid'] = $login_user['UID']; - $_SESSION['locale'] = $login_user['Sprache']; + $session->set('uid', $login_user['UID']); + $session->set('locale', $login_user['Sprache']); redirect(page_link_to('news')); } @@ -477,6 +489,9 @@ function guest_login() ]); } +/** + * @return string + */ function get_register_hint() { global $privileges; diff --git a/includes/pages/user_settings.php b/includes/pages/user_settings.php index 03621a45..0ba8bbcb 100644 --- a/includes/pages/user_settings.php +++ b/includes/pages/user_settings.php @@ -164,6 +164,7 @@ function user_settings_locale($user_source, $locales) { $valid = true; $request = request(); + $session = session(); if ($request->has('language') && isset($locales[$request->input('language')])) { $user_source['Sprache'] = $request->input('language'); @@ -182,7 +183,7 @@ function user_settings_locale($user_source, $locales) $user_source['UID'], ] ); - $_SESSION['locale'] = $user_source['Sprache']; + $session->set('locale', $user_source['Sprache']); success('Language changed.'); redirect(page_link_to('user_settings')); diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php index db0bb193..30abbde6 100644 --- a/includes/pages/user_shifts.php +++ b/includes/pages/user_shifts.php @@ -167,20 +167,23 @@ function view_user_shifts() { global $user, $privileges, $ical_shifts; + $session = session(); $ical_shifts = []; $days = load_days(); $rooms = load_rooms(); $types = load_types(); - if (!isset($_SESSION['ShiftsFilter'])) { + if (!$session->has('ShiftsFilter')) { $room_ids = [ $rooms[0]['id'] ]; $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); diff --git a/includes/sys_auth.php b/includes/sys_auth.php index 36f0f935..4242261b 100644 --- a/includes/sys_auth.php +++ b/includes/sys_auth.php @@ -10,8 +10,10 @@ function load_auth() global $user, $privileges; $user = null; - if (isset($_SESSION['uid'])) { - $user = DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$_SESSION['uid']]); + $session = session(); + + if ($session->has('uid')) { + $user = DB::selectOne('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$session->get('uid')]); if (!empty($user)) { // User ist eingeloggt, Datensatz zur Verfügung stellen und Timestamp updaten DB::update(' @@ -21,12 +23,13 @@ function load_auth() LIMIT 1 ', [ time(), - $_SESSION['uid'], + $session->get('uid'), ]); $privileges = privileges_for_user($user['UID']); return; } - unset($_SESSION['uid']); + + $session->remove('uid'); } // guest privileges diff --git a/src/helpers.php b/src/helpers.php index af0e802b..24f93f2c 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -5,6 +5,7 @@ use Engelsystem\Config\Config; use Engelsystem\Http\Request; use Engelsystem\Renderer\Renderer; use Engelsystem\Routing\UrlGenerator; +use Symfony\Component\HttpFoundation\Session\SessionInterface; /** * Get or set config values @@ -42,6 +43,22 @@ function request($key = null, $default = null) 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 mixed[] $data -- cgit v1.2.3-54-g00ecf