mirror of
https://github.com/grocy/grocy.git
synced 2025-08-20 04:12:59 +00:00
Added configuration validator class (#1215)
* Added configuration validator class * Review Co-authored-by: Bernd Bestel <bernd@berrnd.de>
This commit is contained in:
11
app.php
11
app.php
@@ -12,6 +12,7 @@ require_once __DIR__ . '/vendor/autoload.php';
|
|||||||
// Load config files
|
// Load config files
|
||||||
require_once GROCY_DATAPATH . '/config.php';
|
require_once GROCY_DATAPATH . '/config.php';
|
||||||
require_once __DIR__ . '/config-dist.php'; // For not in own config defined values we use the default ones
|
require_once __DIR__ . '/config-dist.php'; // For not in own config defined values we use the default ones
|
||||||
|
require_once __DIR__ . '/helpers/ConfigurationValidator.php';
|
||||||
|
|
||||||
// Definitions for dev/demo/prerelease mode
|
// Definitions for dev/demo/prerelease mode
|
||||||
if ((GROCY_MODE === 'dev' || GROCY_MODE === 'demo' || GROCY_MODE === 'prerelease') && !defined('GROCY_USER_ID'))
|
if ((GROCY_MODE === 'dev' || GROCY_MODE === 'demo' || GROCY_MODE === 'prerelease') && !defined('GROCY_USER_ID'))
|
||||||
@@ -31,6 +32,16 @@ if (GROCY_DISABLE_AUTH === true)
|
|||||||
define('GROCY_SHOW_AUTH_VIEWS', false);
|
define('GROCY_SHOW_AUTH_VIEWS', false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if any invalid entries in config.php have been made
|
||||||
|
try
|
||||||
|
{
|
||||||
|
(new ConfigurationValidator())->validateConfig();
|
||||||
|
}
|
||||||
|
catch (EInvalidConfig $ex)
|
||||||
|
{
|
||||||
|
exit('Invalid setting in config.php: ' . $ex->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
// Setup base application
|
// Setup base application
|
||||||
AppFactory::setContainer(new DI\Container());
|
AppFactory::setContainer(new DI\Container());
|
||||||
$app = AppFactory::create();
|
$app = AppFactory::create();
|
||||||
|
84
helpers/ConfigurationValidator.php
Normal file
84
helpers/ConfigurationValidator.php
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class EInvalidConfig extends Exception
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
class ConfigurationValidator
|
||||||
|
{
|
||||||
|
public function validateConfig()
|
||||||
|
{
|
||||||
|
self::checkMode();
|
||||||
|
self::checkDefaultLocale();
|
||||||
|
self::checkCurrencyFormat();
|
||||||
|
self::checkFirstDayOfWeek();
|
||||||
|
self::checkEntryPage();
|
||||||
|
self::checkMealplanFirstDayOfWeek();
|
||||||
|
self::checkAutoNightModeRange();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function checkMode()
|
||||||
|
{
|
||||||
|
$allowedModes = ['production', 'dev', 'demo', 'prerelease'];
|
||||||
|
if (!in_array(GROCY_MODE, $allowedModes))
|
||||||
|
{
|
||||||
|
throw new EInvalidConfig('Invalid mode "' . GROCY_MODE . '" set, only ' . implode(', ', $allowedModes) . ' allowed');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function checkDefaultLocale()
|
||||||
|
{
|
||||||
|
if (!file_exists(__DIR__ . '/../localization/' . GROCY_DEFAULT_LOCALE))
|
||||||
|
{
|
||||||
|
throw new EInvalidConfig('Invalid locale "' . GROCY_DEFAULT_LOCALE . '" set, locale needs to exist in folder localization');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function checkFirstDayOfWeek()
|
||||||
|
{
|
||||||
|
if (!(GROCY_CALENDAR_FIRST_DAY_OF_WEEK == '' ||
|
||||||
|
(is_numeric(GROCY_CALENDAR_FIRST_DAY_OF_WEEK) && GROCY_CALENDAR_FIRST_DAY_OF_WEEK >= 0 && GROCY_CALENDAR_FIRST_DAY_OF_WEEK <= 6)))
|
||||||
|
{
|
||||||
|
throw new EInvalidConfig('Invalid value for CALENDAR_FIRST_DAY_OF_WEEK');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function checkCurrencyFormat()
|
||||||
|
{
|
||||||
|
if (!(preg_match('/^([A-z]){3}$/', GROCY_CURRENCY)))
|
||||||
|
{
|
||||||
|
throw new EInvalidConfig('CURRENCY is not in ISO 4217 format (three letter code)');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function checkEntryPage()
|
||||||
|
{
|
||||||
|
$allowedPages = ['stock', 'shoppinglist', 'recipes', 'chores', 'tasks', 'batteries', 'equipment', 'calendar', 'mealplan'];
|
||||||
|
if (!in_array(GROCY_ENTRY_PAGE, $allowedPages))
|
||||||
|
{
|
||||||
|
throw new EInvalidConfig('Invalid entry page "' . GROCY_ENTRY_PAGE . '" set, only ' . implode(', ', $allowedPages) . ' allowed');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function checkMealplanFirstDayOfWeek()
|
||||||
|
{
|
||||||
|
if (!(GROCY_MEAL_PLAN_FIRST_DAY_OF_WEEK == '' ||
|
||||||
|
(is_numeric(GROCY_MEAL_PLAN_FIRST_DAY_OF_WEEK) && GROCY_MEAL_PLAN_FIRST_DAY_OF_WEEK >= 0 && GROCY_MEAL_PLAN_FIRST_DAY_OF_WEEK <= 6)))
|
||||||
|
{
|
||||||
|
throw new EInvalidConfig('Invalid value for MEAL_PLAN_FIRST_DAY_OF_WEEK');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function checkAutoNightModeRange()
|
||||||
|
{
|
||||||
|
global $GROCY_DEFAULT_USER_SETTINGS;
|
||||||
|
if (!(preg_match('/^(?:2[0-3]|[01][0-9]):[0-5][0-9]$/', $GROCY_DEFAULT_USER_SETTINGS['auto_night_mode_time_range_from'])))
|
||||||
|
{
|
||||||
|
throw new EInvalidConfig('auto_night_mode_time_range_from is not in HH:mm format (' . $GROCY_DEFAULT_USER_SETTINGS['auto_night_mode_time_range_from'] . ')');
|
||||||
|
}
|
||||||
|
if (!(preg_match('/^(?:2[0-3]|[01][0-9]):[0-5][0-9]$/', $GROCY_DEFAULT_USER_SETTINGS['auto_night_mode_time_range_to'])))
|
||||||
|
{
|
||||||
|
throw new EInvalidConfig('auto_night_mode_time_range_to is not in HH:mm format (' . $GROCY_DEFAULT_USER_SETTINGS['auto_night_mode_time_range_to'] . ')');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user