diff --git a/composer.json b/composer.json index bb21de23..7655275c 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,9 @@ "autoload": { "psr-4": { "Engelsystem\\": "src/" - } + }, + "files": [ + "src/helpers.php" + ] } } diff --git a/config/config.default.php b/config/config.default.php index bcfcc89c..437399e3 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -1,70 +1,106 @@ [ + 'host' => 'localhost', + 'user' => 'root', + 'pw' => '', + 'db' => 'engelsystem', + ], -// contact email address, linked on every page -$contact_email = 'mailto:ticket@c3heaven.de'; + // For accessing stats + 'api_key' => '', -// Default-Theme auf der Startseite, 1=style1.css usw. -$default_theme = 1; + // Enable maintenance mode (show a static page) + 'maintenance' => false, -// Anzahl der News, die auf einer Seite ausgeben werden koennen... -$display_news = 6; + // Set to development to enable debugging messages + 'environment' => 'production', -// Anzahl Stunden bis zum Austragen eigener Schichten -$last_unsubscribe = 3; + // URL to the angel faq and job description + 'faq_url' => 'https://events.ccc.de/congress/2013/wiki/Static:Volunteers', -// Setzt den zu verwendenden Crypto-Algorismus (entsprechend der Dokumentation von crypt()). -// Falls ein Benutzerpasswort in einem anderen Format gespeichert ist, -// wird es bei der ersten Benutzung des Klartext-Passworts in das neue Format -// konvertiert. -// $crypt_alg = '$1'; // MD5 -// $crypt_alg = '$2y$13'; // Blowfish -// $crypt_alg = '$5$rounds=5000'; // SHA-256 -$crypt_alg = '$6$rounds=5000'; // SHA-512 + // Contact email address, linked on every page + 'contact_email' => 'mailto:ticket@c3heaven.de', -$min_password_length = 8; + // Default theme of the start page, 1=style1.css + 'default_theme' => 1, -// Wenn Engel beim Registrieren oder in ihrem Profil eine T-Shirt Größe angeben sollen, auf true setzen: -$enable_tshirt_size = true; + // Number of News shown on one site + 'display_news' => 6, -// Number of shifts to freeload until angel is locked for shift signup. -$max_freeloadable_shifts = 2; + // Anzahl Stunden bis zum Austragen eigener Schichten + 'last_unsubscribe' => 3, -// local timezone -date_default_timezone_set('Europe/Berlin'); + // Setzt den zu verwendenden Crypto-Algorismus (entsprechend der Dokumentation von crypt()). + // Falls ein Benutzerpasswort in einem anderen Format gespeichert ist, + // wird es bei der ersten Benutzung des Klartext-Passworts in das neue Format + // konvertiert. + // MD5 '$1' + // Blowfish '$2y$13' + // SHA-256 '$5$rounds=5000' + // SHA-512 '$6$rounds=5000' + 'crypt_alg' => '$6$rounds=5000', // SHA-512 -// multiply 'night shifts' and freeloaded shifts (start or end between 2 and 6 exclusive) by 2 -$shift_sum_formula = 'SUM( - (1+( - (HOUR(FROM_UNIXTIME(`Shifts`.`end`)) > 2 AND HOUR(FROM_UNIXTIME(`Shifts`.`end`)) < 6) - OR (HOUR(FROM_UNIXTIME(`Shifts`.`start`)) > 2 AND HOUR(FROM_UNIXTIME(`Shifts`.`start`)) < 6) - OR (HOUR(FROM_UNIXTIME(`Shifts`.`start`)) <= 2 AND HOUR(FROM_UNIXTIME(`Shifts`.`end`)) >= 6) - ))*(`Shifts`.`end` - `Shifts`.`start`)*(1 - 3 * `ShiftEntry`.`freeloaded`) -)'; + 'min_password_length' => 8, -// voucher calculation -$voucher_settings = [ - 'initial_vouchers' => 2, - 'shifts_per_voucher' => 1 -]; - -// weigh every shift the same -// $shift_sum_formula = 'SUM(`end` - `start`)'; - -// For accessing stats -$api_key = ''; - -// MySQL-Connection Settings -$config = [ - 'host' => 'localhost', - 'user' => 'root', - 'pw' => '', - 'db' => 'engelsystem' + // Wenn Engel beim Registrieren oder in ihrem Profil eine T-Shirt Größe angeben sollen, auf true setzen: + 'enable_tshirt_size' => true, + + // Number of shifts to freeload until angel is locked for shift signup. + 'max_freeloadable_shifts' => 2, + + // local timezone + 'timezone' => 'Europe/Berlin', + + // multiply 'night shifts' and freeloaded shifts (start or end between 2 and 6 exclusive) by 2 + 'shift_sum_formula' => ' + SUM( + (1 + + ( + (HOUR(FROM_UNIXTIME(`Shifts`.`end`)) > 2 AND HOUR(FROM_UNIXTIME(`Shifts`.`end`)) < 6) + OR (HOUR(FROM_UNIXTIME(`Shifts`.`start`)) > 2 AND HOUR(FROM_UNIXTIME(`Shifts`.`start`)) < 6) + OR (HOUR(FROM_UNIXTIME(`Shifts`.`start`)) <= 2 AND HOUR(FROM_UNIXTIME(`Shifts`.`end`)) >= 6) + ) + ) + * (`Shifts`.`end` - `Shifts`.`start`) + * (1 - 3 * `ShiftEntry`.`freeloaded`) + ) + ', + // weigh every shift the same + //'shift_sum_formula' => 'SUM(`end` - `start`)', + + // voucher calculation + 'voucher_settings' => [ + 'initial_vouchers' => 2, + 'shifts_per_voucher' => 1, + ], + + // Available locales in /locale/ + 'locales' => [ + 'de_DE.UTF-8' => 'Deutsch', + 'en_US.UTF-8' => 'English', + ], + + 'default_locale' => 'en_US.UTF-8', + + // Available T-Shirt sizes, set value to null if not available + 'tshirt_sizes' => [ + '' => _('Please select...'), + 'S' => 'S', + 'M' => 'M', + 'L' => 'L', + 'XL' => 'XL', + '2XL' => '2XL', + '3XL' => '3XL', + '4XL' => '4XL', + '5XL' => '5XL', + 'S-G' => 'S Girl', + 'M-G' => 'M Girl', + 'L-G' => 'L Girl', + 'XL-G' => 'XL Girl', + ], ]; diff --git a/includes/controller/shifts_controller.php b/includes/controller/shifts_controller.php index 71459a10..a1801de6 100644 --- a/includes/controller/shifts_controller.php +++ b/includes/controller/shifts_controller.php @@ -329,9 +329,9 @@ function shift_next_controller() */ function shifts_json_export_all_controller() { - global $api_key; + $api_key = config('api_key'); - if ($api_key == '') { + if (empty($api_key)) { engelsystem_error('Config contains empty apikey.'); } diff --git a/includes/controller/users_controller.php b/includes/controller/users_controller.php index b80fdb4d..84b6bbda 100644 --- a/includes/controller/users_controller.php +++ b/includes/controller/users_controller.php @@ -282,7 +282,6 @@ function users_list_controller() */ function user_password_recovery_set_new_controller() { - global $min_password_length; $user_source = User_by_password_recovery_token($_REQUEST['token']); if ($user_source == null) { error(_('Token is not correct.')); @@ -292,7 +291,10 @@ function user_password_recovery_set_new_controller() if (isset($_REQUEST['submit'])) { $valid = true; - if (isset($_REQUEST['password']) && strlen($_REQUEST['password']) >= $min_password_length) { + if ( + isset($_REQUEST['password']) + && strlen($_REQUEST['password']) >= config('min_password_length') + ) { if ($_REQUEST['password'] != $_REQUEST['password2']) { $valid = false; error(_('Your passwords don\'t match.')); diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php index 3537f100..8a5723ef 100644 --- a/includes/engelsystem_provider.php +++ b/includes/engelsystem_provider.php @@ -1,5 +1,6 @@ set(require __DIR__ . '/../config/config.default.php'); + +if (file_exists(__DIR__ . '/../config/config.php')) { + $config->set(array_replace_recursive( + $config->get(null), + require __DIR__ . '/../config/config.php' + )); +} + +date_default_timezone_set($config->get('timezone')); + + +/** + * Check for maintenance + */ +if ($config->get('maintenance')) { + echo file_get_contents(__DIR__ . '/../public/maintenance.html'); + die(); +} + + +/** + * Register error handler + */ +$errorHandler = new ExceptionHandler(); +if (config('environment') == 'development') { + $errorHandler->setEnvironment(ExceptionHandler::ENV_DEVELOPMENT); + ini_set('display_errors', true); + error_reporting(E_ALL); +} else { + ini_set('display_errors', false); +} + + +/** + * Connect to database + */ +Db::connect( + 'mysql:host=' . config('database')['host'] . ';dbname=' . config('database')['db'] . ';charset=utf8', + config('database')['user'], + config('database')['pw'] +) || die('Error: Unable to connect to database'); +Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + +/** + * Include legacy code + */ require_once realpath(__DIR__ . '/../includes/sys_auth.php'); require_once realpath(__DIR__ . '/../includes/sys_form.php'); require_once realpath(__DIR__ . '/../includes/sys_log.php'); @@ -71,17 +126,6 @@ require_once realpath(__DIR__ . '/../includes/helper/email_helper.php'); require_once realpath(__DIR__ . '/../includes/mailer/shifts_mailer.php'); require_once realpath(__DIR__ . '/../includes/mailer/users_mailer.php'); -$config = []; -require_once realpath(__DIR__ . '/../config/config.default.php'); -if (file_exists(realpath(__DIR__ . '/../config/config.php'))) { - require_once realpath(__DIR__ . '/../config/config.php'); -} - -if ($maintenance_mode) { - echo file_get_contents(__DIR__ . '/../public/maintenance.html'); - die(); -} - require_once realpath(__DIR__ . '/../includes/pages/admin_active.php'); require_once realpath(__DIR__ . '/../includes/pages/admin_arrive.php'); require_once realpath(__DIR__ . '/../includes/pages/admin_free.php'); @@ -100,20 +144,10 @@ require_once realpath(__DIR__ . '/../includes/pages/user_questions.php'); require_once realpath(__DIR__ . '/../includes/pages/user_settings.php'); require_once realpath(__DIR__ . '/../includes/pages/user_shifts.php'); -$errorHandler = new ExceptionHandler( - ($environment == 'development' - ? ExceptionHandler::ENV_DEVELOPMENT - : ExceptionHandler::ENV_PRODUCTION - ) -); - -Db::connect( - 'mysql:host=' . $config['host'] . ';dbname=' . $config['db'] . ';charset=utf8', - $config['user'], - $config['pw'] -) || die('Error: Unable to connect to database'); -Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); +/** + * Init application + */ session_start(); gettext_init(); diff --git a/includes/helper/internationalization_helper.php b/includes/helper/internationalization_helper.php index 7c04ebbd..ed16de15 100644 --- a/includes/helper/internationalization_helper.php +++ b/includes/helper/internationalization_helper.php @@ -1,10 +1,4 @@ 'Deutsch', - 'en_US.UTF-8' => 'English' -]; - -$default_locale = 'en_US.UTF-8'; /** * Return currently active locale @@ -31,7 +25,8 @@ function locale_short() */ function gettext_init() { - global $locales, $default_locale; + $locales = config('locales'); + $default_locale = config('default_locale'); if (isset($_REQUEST['set_locale']) && isset($locales[$_REQUEST['set_locale']])) { $_SESSION['locale'] = $_REQUEST['set_locale']; @@ -67,11 +62,10 @@ function gettext_locale($locale = null) */ function make_langselect() { - global $locales; $url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], '?') > 0 ? '&' : '?') . 'set_locale='; $items = []; - foreach ($locales as $locale => $name) { + foreach (config('locales') as $locale => $name) { $items[] = toolbar_item_link( htmlspecialchars($url) . $locale, '', diff --git a/includes/model/User_model.php b/includes/model/User_model.php index 097e8faf..53b4ce1e 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -215,9 +215,9 @@ function Users($order_by = 'Nick') */ function User_is_freeloader($user) { - global $max_freeloadable_shifts, $user; + global $user; - return count(ShiftEntries_freeloaded_by_user($user)) >= $max_freeloadable_shifts; + return count(ShiftEntries_freeloaded_by_user($user)) >= config('max_freeloadable_shifts'); } /** @@ -542,8 +542,7 @@ function User_generate_password_recovery_token(&$user) */ function User_get_eligable_voucher_count(&$user) { - global $voucher_settings; - + $voucher_settings = config('voucher_settings'); $shifts_done = count(ShiftEntries_finished_by_user($user)); $earned_vouchers = $user['got_voucher'] - $voucher_settings['initial_vouchers']; diff --git a/includes/pages/admin_active.php b/includes/pages/admin_active.php index 275f50ba..8cb66e6e 100644 --- a/includes/pages/admin_active.php +++ b/includes/pages/admin_active.php @@ -15,7 +15,8 @@ function admin_active_title() */ function admin_active() { - global $tshirt_sizes, $shift_sum_formula; + $tshirt_sizes = config('tshirt_sizes'); + $shift_sum_formula = config('shift_sum_formula'); $msg = ''; $search = ''; @@ -208,7 +209,7 @@ function admin_active() $shirt_statistics = []; foreach (array_keys($tshirt_sizes) as $size) { - if ($size != '') { + if (!empty($size)) { $sc = DB::select( 'SELECT count(*) FROM `User` WHERE `Size`=? AND `Gekommen`=1', [$size] diff --git a/includes/pages/admin_user.php b/includes/pages/admin_user.php index 192becb0..2ab40cca 100644 --- a/includes/pages/admin_user.php +++ b/includes/pages/admin_user.php @@ -15,7 +15,14 @@ function admin_user_title() */ function admin_user() { - global $user, $tshirt_sizes, $privileges; + global $user, $privileges; + $tshirt_sizes = config('tshirt_sizes'); + + foreach ($tshirt_sizes as $key => $size) { + if (empty($size)) { + unset($tshirt_sizes[$key]); + } + } $html = ''; diff --git a/includes/pages/guest_login.php b/includes/pages/guest_login.php index 66a06116..2ffa4b98 100644 --- a/includes/pages/guest_login.php +++ b/includes/pages/guest_login.php @@ -33,8 +33,10 @@ function logout_title() */ function guest_register() { - global $tshirt_sizes, $enable_tshirt_size, $default_theme, $user, $min_password_length; - + global $user; + $tshirt_sizes = config('tshirt_sizes'); + $enable_tshirt_size = config('enable_tshirt_size'); + $min_password_length = config('min_password_length'); $event_config = EventConfig(); $msg = ''; @@ -65,6 +67,12 @@ function guest_register() } } + foreach ($tshirt_sizes as $key => $size) { + if (empty($size)) { + unset($tshirt_sizes[$key]); + } + } + if (isset($_REQUEST['submit'])) { $valid = true; @@ -201,7 +209,7 @@ function guest_register() VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?, NULL, ?) ', [ - $default_theme, + config('default_theme'), $nick, $preName, $lastName, diff --git a/includes/pages/guest_stats.php b/includes/pages/guest_stats.php index 4add3e97..6b6f0572 100644 --- a/includes/pages/guest_stats.php +++ b/includes/pages/guest_stats.php @@ -4,10 +4,10 @@ use Engelsystem\Database\DB; function guest_stats() { - global $api_key; + $apiKey = config('api_key'); if (isset($_REQUEST['api_key'])) { - if ($_REQUEST['api_key'] == $api_key) { + if ($_REQUEST['api_key'] == $apiKey && !empty($apiKey)) { $stats = []; list($user_count) = DB::select('SELECT count(*) AS `user_count` FROM `User`'); diff --git a/includes/pages/user_atom.php b/includes/pages/user_atom.php index 3c4b631d..5574e8eb 100644 --- a/includes/pages/user_atom.php +++ b/includes/pages/user_atom.php @@ -7,7 +7,7 @@ use Engelsystem\Database\DB; */ function user_atom() { - global $user, $display_news; + global $user; if (!isset($_REQUEST['key']) || !preg_match('/^[\da-f]{32}$/', $_REQUEST['key'])) { engelsystem_error('Missing key.'); @@ -27,7 +27,7 @@ function user_atom() FROM `News` ' . (empty($_REQUEST['meetings']) ? '' : 'WHERE `Treffen` = 1 ') . ' ORDER BY `ID` - DESC LIMIT ' . (int)$display_news + DESC LIMIT ' . (int)config('display_news') ); $output = make_atom_entries_from_news($news); diff --git a/includes/pages/user_myshifts.php b/includes/pages/user_myshifts.php index acb78875..62d87d27 100644 --- a/includes/pages/user_myshifts.php +++ b/includes/pages/user_myshifts.php @@ -17,7 +17,6 @@ function myshifts_title() */ function user_myshifts() { - global $last_unsubscribe; global $user, $privileges; if ( @@ -144,7 +143,10 @@ function user_myshifts() ); if (count($shift) > 0) { $shift = array_shift($shift); - if (($shift['start'] > time() + $last_unsubscribe * 3600) || in_array('user_shifts_admin', $privileges)) { + if ( + ($shift['start'] > time() + config('last_unsubscribe') * 3600) + || in_array('user_shifts_admin', $privileges) + ) { $result = ShiftEntry_delete($user_id); if ($result === false) { engelsystem_error('Unable to delete shift entry.'); diff --git a/includes/pages/user_news.php b/includes/pages/user_news.php index 69d20e69..b1e337b6 100644 --- a/includes/pages/user_news.php +++ b/includes/pages/user_news.php @@ -31,8 +31,7 @@ function meetings_title() */ function user_meetings() { - global $display_news; - + $display_news = config('display_news'); $html = '
' . _('Here is the list of teams and their tasks. If you have questions, read the FAQ.') . '
', '