Save config.php in Settings

Egg and hen problem solved :)
* Test database connection on save settings -> prevents from making website unusable if connection is wrong
* Test server_path -> same
There is no config.php anymore, just config.local.php, which can be edited manually and also from admin panel
This commit is contained in:
slawkens 2023-07-21 11:38:52 +02:00
parent 399f263b42
commit 1543dd864e
13 changed files with 336 additions and 74 deletions

View File

@ -6,10 +6,6 @@ require '../common.php';
const ADMIN_PANEL = true;
const MYAAC_ADMIN = true;
if(file_exists(BASE . 'config.local.php')) {
require_once BASE . 'config.local.php';
}
if(file_exists(BASE . 'install') && (!isset($config['installed']) || !$config['installed']))
{
header('Location: ' . BASE_URL . 'install/');
@ -34,12 +30,6 @@ if(!$db->hasTable('myaac_account_actions')) {
throw new RuntimeException('Seems that the table <strong>myaac_account_actions</strong> of MyAAC doesn\'t exist in the database. This is a fatal error. You can try to reinstall MyAAC by visiting <a href="' . BASE_URL . 'install">this</a> url.');
}
if(config('env') === 'dev') {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
// event system
require_once SYSTEM . 'hooks.php';
$hooks = new Hooks();

View File

@ -143,6 +143,23 @@ if(!IS_CLI) {
//define('CURRENT_URL', BASE_URL . $_SERVER['REQUEST_URI']);
}
require SYSTEM . 'config.php';
if (file_exists(BASE . 'config.local.php')) {
require BASE . 'config.local.php';
}
ini_set('log_errors', 1);
if(@$config['env'] === 'dev') {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
else {
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
}
$autoloadFile = VENDOR . 'autoload.php';
if (!is_file($autoloadFile)) {
throw new RuntimeException('The vendor folder is missing. Please download Composer: <a href="https://getcomposer.org/download">https://getcomposer.org/download</a>, install it and execute in the main MyAAC directory this command: <b>composer install</b>. Or download MyAAC from <a href="https://github.com/slawkens/myaac/releases">GitHub releases</a>, which includes Vendor folder.');

View File

@ -56,22 +56,6 @@ if(preg_match("/^(.*)\.(gif|jpg|png|jpeg|tiff|bmp|css|js|less|map|html|zip|rar|g
exit;
}
if(file_exists(BASE . 'config.local.php')) {
require_once BASE . 'config.local.php';
}
ini_set('log_errors', 1);
if(config('env') === 'dev') {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
else {
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT);
}
if((!isset($config['installed']) || !$config['installed']) && file_exists(BASE . 'install'))
{
header('Location: ' . BASE_URL . 'install/');
@ -162,7 +146,7 @@ if(setting('core.backward_support')) {
$config['site'] = &$config;
$config['server'] = &$config['lua'];
$config['site']['shop_system'] = $config['gifts_system'];
$config['site']['shop_system'] = setting('core.gifts_system');
$config['site']['gallery_page'] = true;
if(!isset($config['vdarkborder']))

View File

@ -12,9 +12,7 @@ require SYSTEM . 'functions.php';
require BASE . 'install/includes/functions.php';
require BASE . 'install/includes/locale.php';
require SYSTEM . 'clients.conf.php';
if(file_exists(BASE . 'config.local.php'))
require BASE . 'config.local.php';
require LIBS . 'settings.php';
// ignore undefined index from Twig autoloader
$config['env'] = 'prod';

View File

@ -11,16 +11,11 @@ if(!isset($_SESSION['var_server_path'])) {
}
if(!$error) {
$content = "<?php";
$content .= PHP_EOL;
$content .= '// place for your configuration directives, so you can later easily update myaac';
$content .= PHP_EOL;
$content .= '$config[\'installed\'] = true;';
$content .= PHP_EOL;
// by default, set env to prod
// user can disable when he wants
$content .= '$config[\'env\'] = \'prod\'; // dev or prod';
$content .= PHP_EOL;
$configToSave = [
// by default, set env to prod
// user can disable when he wants
'env' => 'prod',
];
foreach($_SESSION as $key => $value)
{
@ -34,12 +29,13 @@ if(!$error) {
}
if(!in_array($key, ['var_usage', 'var_date_timezone', 'var_client', 'var_account', 'var_account_id', 'var_password', 'var_password_confirm', 'var_step', 'var_email', 'var_player_name'], true)) {
$content .= '$config[\'' . str_replace('var_', '', $key) . '\'] = \'' . $value . '\';';
$content .= PHP_EOL;
$configToSave[str_replace('var_', '', $key)] = $value;
}
}
}
$configToSave['cache_prefix'] = 'myaac_' . generateRandomString(8, true, false, true);
require BASE . 'install/includes/config.php';
if(!$error) {
@ -76,13 +72,8 @@ if(!$error) {
'message' => $locale['loading_spinner']
));
$content .= '$config[\'cache_prefix\'] = \'myaac_' . generateRandomString(8, true, false, true, false) . '_\';';
$saved = true;
if(!$error) {
$saved = file_put_contents(BASE . 'config.local.php', $content);
}
$content = '';
$saved = Settings::saveConfig($configToSave, BASE . 'config.local.php', $content);
if($saved) {
success($locale['step_database_config_saved']);
if(!$error) {
@ -93,7 +84,7 @@ if(!$error) {
$_SESSION['config_content'] = $content;
unset($_SESSION['saved']);
$locale['step_database_error_file'] = str_replace('$FILE$', '<b>' . BASE . 'config.local.php</b>', $locale['step_database_error_file']);
$locale['step_database_error_file'] = str_replace('$FILE$', '<b>' . BASE . 'config.php</b>', $locale['step_database_error_file']);
error($locale['step_database_error_file'] . '<br/>
<textarea cols="70" rows="10">' . $content . '</textarea>');
}

View File

@ -11,11 +11,11 @@ ini_set('max_execution_time', 300);
ob_implicit_flush();
ob_end_flush();
header('X-Accel-Buffering: no');
/*
if(isset($config['installed']) && $config['installed'] && !isset($_SESSION['saved'])) {
warning($locale['already_installed']);
return;
}
}*/
require SYSTEM . 'init.php';

View File

@ -1,7 +1,5 @@
<?php
require_once 'common.php';
require_once 'config.php';
require_once 'config.local.php';
require_once SYSTEM . 'functions.php';
require_once SYSTEM . 'init.php';
require_once SYSTEM . 'status.php';

View File

@ -45,6 +45,7 @@ $deprecatedConfig = [
'signature_type',
'signature_cache_time',
'signature_browser_cache',
'gifts_system',
];
foreach ($deprecatedConfig as $key => $value) {

View File

@ -9,7 +9,11 @@
*/
defined('MYAAC') or die('Direct access not allowed!');
if(!isset($config['database_user'][0], $config['database_password'][0], $config['database_name'][0]))
if (!isset($config['database_overwrite'])) {
$config['database_overwrite'] = false;
}
if(!$config['database_overwrite'] && !isset($config['database_user'][0], $config['database_password'][0], $config['database_name'][0]))
{
if(isset($config['lua']['sqlType'])) {// tfs 0.3
if(isset($config['lua']['mysqlHost'])) {// tfs 0.2
@ -116,4 +120,4 @@ catch(PDOException $error) {
'<li>MySQL is not configured propertly in <i>config.lua</i>.</li>' .
'<li>MySQL server is not running.</li>' .
'</ul>' . $error->getMessage());
}
}

View File

@ -9,11 +9,6 @@
*/
defined('MYAAC') or die('Direct access not allowed!');
// load configuration
require_once BASE . 'config.php';
if(file_exists(BASE . 'config.local.php')) // user customizations
require BASE . 'config.local.php';
if(!isset($config['installed']) || !$config['installed']) {
throw new RuntimeException('MyAAC has not been installed yet or there was error during installation. Please install again.');
}
@ -22,12 +17,16 @@ if(config('env') === 'dev') {
require SYSTEM . 'exception.php';
}
if(empty($config['server_path'])) {
throw new RuntimeException('Server Path has been not set. Go to config.php and set it.');
}
// take care of trailing slash at the end
if($config['server_path'][strlen($config['server_path']) - 1] !== '/')
$config['server_path'] .= '/';
// enable gzip compression if supported by the browser
if($config['gzip_output'] && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false && function_exists('ob_gzhandler'))
if(isset($config['gzip_output']) && $config['gzip_output'] && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false && function_exists('ob_gzhandler'))
ob_start('ob_gzhandler');
// cache

View File

@ -53,11 +53,22 @@ class Settings implements ArrayAccess
}
}
public function save($pluginName, $settings) {
public function save($pluginName, $values) {
global $db;
if (!isset($this->settingsFile[$pluginName])) {
throw new RuntimeException('Error on save settings: plugin does not exist');
}
$settings = $this->settingsFile[$pluginName];
if (isset($settings['callbacks']['beforeSave'])) {
if (!$settings['callbacks']['beforeSave']($settings, $values)) {
return false;
}
}
$db->query('DELETE FROM `' . TABLE_PREFIX . 'settings` WHERE `name` = ' . $db->quote($pluginName) . ';');
foreach ($settings as $key => $value) {
foreach ($values as $key => $value) {
try {
$db->insert(TABLE_PREFIX . 'settings', ['name' => $pluginName, 'key' => $key, 'value' => $value]);
} catch (PDOException $error) {
@ -69,6 +80,8 @@ class Settings implements ArrayAccess
if ($cache->enabled()) {
$cache->delete('settings');
}
return true;
}
public function updateInDatabase($pluginName, $key, $value)
@ -103,6 +116,18 @@ class Settings implements ArrayAccess
}
}
$config = [];
require BASE . 'config.local.php';
foreach ($config as $key => $value) {
if (is_bool($value)) {
$settingsDb[$key] = $value ? 'true' : 'false';
}
else {
$settingsDb[$key] = (string)$value;
}
}
$javascript = '';
ob_start();
?>
@ -261,7 +286,7 @@ class Settings implements ArrayAccess
echo '<select class="form-control" name="settings[' . $key . ']" id="' . $key . '">';
foreach ($setting['options'] as $value => $option) {
$compareTo = (isset($settingsDb[$key]) ? $settingsDb[$key] : (isset($setting['default']) ? $setting['default'] : ''));
$compareTo = ($settingsDb[$key] ?? ($setting['default'] ?? ''));
if($value === 'true') {
$selected = $compareTo === true;
}
@ -283,9 +308,18 @@ class Settings implements ArrayAccess
<td>
<div class="well">
<?php
echo $setting['desc'];
echo ($setting['desc'] ?? '');
echo '<br/>';
echo '<strong>Default:</strong> ';
if (empty($setting['default'])) {
if ($setting['type'] === 'boolean') {
$setting['default'] = true;
}
else {
$setting['default'] = '';
}
}
if ($setting['type'] === 'boolean') {
echo ($setting['default'] ? 'Yes' : 'No');
}
@ -293,7 +327,9 @@ class Settings implements ArrayAccess
echo $setting['default'];
}
else if ($setting['type'] === 'options') {
echo $setting['options'][$setting['default']];
if (!empty($setting['default'])) {
echo $setting['options'][$setting['default']];
}
}
?>
</div>
@ -374,7 +410,7 @@ class Settings implements ArrayAccess
return;
}
unset($this->settingsFile[$pluginKeyName][$key]);
unset($this->settingsFile[$pluginKeyName]['settings'][$key]);
unset($this->settingsDatabase[$pluginKeyName][$key]);
$this->deleteFromDatabase($pluginKeyName, $key);
}
@ -402,12 +438,12 @@ class Settings implements ArrayAccess
// return specified plugin settings (all)
if(!isset($key)) {
return $this->settingsFile[$pluginKeyName];
return $this->settingsFile[$pluginKeyName]['settings'];
}
$ret = [];
if(isset($this->settingsFile[$pluginKeyName][$key])) {
$ret = $this->settingsFile[$pluginKeyName][$key];
if(isset($this->settingsFile[$pluginKeyName]['settings'][$key])) {
$ret = $this->settingsFile[$pluginKeyName]['settings'][$key];
}
if(isset($this->settingsDatabase[$pluginKeyName][$key])) {
@ -416,7 +452,7 @@ class Settings implements ArrayAccess
$ret['value'] = $value;
}
else {
$ret['value'] = $this->settingsFile[$pluginKeyName][$key]['default'];
$ret['value'] = $this->settingsFile[$pluginKeyName]['settings'][$key]['default'];
}
if(isset($ret['type'])) {
@ -485,8 +521,67 @@ class Settings implements ArrayAccess
throw new \RuntimeException('Failed to load settings file for plugin: ' . $pluginKeyName);
}
$tmp = require $settingsFilePath;
$this->settingsFile[$pluginKeyName] = $tmp['settings'];
$this->settingsFile[$pluginKeyName] = require $settingsFilePath;
}
}
public static function saveConfig($config, $filename, &$content = '')
{
$content = "<?php" . PHP_EOL .
"\$config['installed'] = true;" . PHP_EOL;
foreach ($config as $key => $value) {
$content .= "\$config['$key'] = ";
$content .= var_export($value, true);
$content .= ';' . PHP_EOL;
}
$success = file_put_contents($filename, $content);
// we saved new config.php, need to revalidate cache (only if opcache is enabled)
if (function_exists('opcache_invalidate')) {
opcache_invalidate($filename);
}
return $success;
}
public static function testDatabaseConnection($config): bool
{
$user = null;
$password = null;
$dns = [];
if( isset($config['database_name']) ) {
$dns[] = 'dbname=' . $config['database_name'];
}
if( isset($config['database_user']) ) {
$user = $config['database_user'];
}
if( isset($config['database_password']) ) {
$password = $config['database_password'];
}
if( isset($config['database_host']) ) {
$dns[] = 'host=' . $config['database_host'];
}
if( isset($config['database_port']) ) {
$dns[] = 'port=' . $config['database_port'];
}
try {
$connectionTest = new PDO('mysql:' . implode(';', $dns), $user, $password);
$connectionTest->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $error) {
error('MySQL connection failed. Settings has been reverted.');
error($error->getMessage());
return false;
}
return true;
}
}

View File

@ -18,6 +18,47 @@ return [
'type' => 'section',
'title' => 'General'
],
'env' => [
'name' => 'App Environment',
'type' => 'options',
'options' => ['prod' => 'Production', 'dev' => 'Development'],
'desc' => 'if you use this script on your live server - set production<br/>
* if you want to test and debug the script locally, or develop plugins, set to development<br/>
* WARNING: on "development" cache is disabled, so site will be significantly slower !!!<br/>
* WARNING2: on "development" all PHP errors/warnings are displayed<br/>
* Recommended: "production" cause of speed (page load time is better)',
'default' => 'prod',
'is_config' => true,
],
'server_path' => [
'name' => 'Server Path',
'type' => 'text',
'desc' => 'Path to the server directory (same directory where config file is located)',
'default' => '',
'is_config' => true,
],
'gzip_output' => [
'name' => 'gzip Output',
'type' => 'boolean',
'desc' => 'gzip page content before sending it to the browser, uses less bandwidth but more cpu cycles',
'default' => false,
'is_config' => true,
],
'cache_engine' => [
'name' => 'Cache Engine',
'type' => 'options',
'options' => ['auto' => 'Auto', 'file' => 'Files', 'apc' => 'APC', 'apcu' => 'APCu', 'eaccelerator' => 'eAccelerator', 'disable' => 'Disable'],
'desc' => 'Auto is most reasonable. It will detect the best cache engine',
'default' => 'auto',
'is_config' => true,
],
'cache_prefix' => [
'name' => 'Cache Prefix',
'type' => 'text',
'desc' => 'Have to be unique if running more MyAAC instances on the same server (except file system cache)',
'default' => 'myaac_' . generateRandomString(8, true, false, true),
'is_config' => true,
],
'date_timezone' => [
'name' => 'Date Timezone',
'type' => 'options',
@ -47,6 +88,82 @@ return [
},
],
],
[
'type' => 'section',
'title' => 'Database',
],
'database_overwrite' => [
'name' => 'Database Manual',
'type' => 'boolean',
'desc' => 'Manual database configuration. Enable if you want to manually enter database details. If set to no - it will get from config.lua',
'default' => false,
'is_config' => true,
],
'database_host' => [
'name' => 'Database Host',
'type' => 'text',
'default' => '127.0.0.1',
'show_if' => [
'database_overwrite', '=', 'true'
],
'is_config' => true,
],
'database_port' => [
'name' => 'Database Port',
'type' => 'number',
'default' => 3306,
'show_if' => [
'database_overwrite', '=', 'true'
],
'is_config' => true,
],
'database_user' => [
'name' => 'Database User',
'type' => 'text',
'show_if' => [
'database_overwrite', '=', 'true'
],
'is_config' => true,
],
'database_password' => [
'name' => 'Database Password',
'type' => 'text',
'show_if' => [
'database_overwrite', '=', 'true'
],
'is_config' => true,
],
'database_name' => [
'name' => 'Database Name',
'type' => 'text',
'show_if' => [
'database_overwrite', '=', 'true'
],
'is_config' => true,
],
'database_socket' => [
'name' => 'Database Socket',
'desc' => 'Set if you want to connect to database through socket (example: /var/run/mysqld/mysqld.sock)',
'type' => 'text',
'show_if' => [
'database_overwrite', '=', 'true'
],
'is_config' => true,
],
'database_log' => [
'name' => 'Database Log',
'desc' => 'Should database queries be logged and saved into system/logs/database.log?',
'type' => 'boolean',
'default' => false,
'is_config' => true,
],
'database_persistent' => [
'name' => 'Database Persistent Connection',
'desc' => 'Use database permanent connection (like server), may speed up your site',
'type' => 'boolean',
'default' => false,
'is_config' => true,
],
[
'type' => 'section',
'title' => 'Template'
@ -814,6 +931,16 @@ Sent by MyAAC,<br/>
'default' => 20,
'desc' => '',
],
[
'type' => 'section',
'title' => 'Gifts/shop system'
],
'gifts_system' => [
'name' => 'Enable gifts system',
'desc' => 'Plugin needs to be installed',
'type' => 'boolean',
'default' => false,
],
[
'type' => 'section',
'title' => 'Experience Table Page'
@ -1038,4 +1165,62 @@ Sent by MyAAC,<br/>
],
],
],
'callbacks' => [
'beforeSave' => function(&$settings, &$values) {
global $config;
$configToSave = [];
$server_path = '';
$database = [];
foreach ($settings['settings'] as $key => $value) {
if (isset($value['is_config']) && getBoolean($value['is_config'])) {
if ($value['type'] === 'boolean') {
$values[$key] = ($values[$key] === 'true');
}
elseif ($value['type'] === 'number') {
$values[$key] = (int)$values[$key];
}
//elseif ($value['type'] === 'options') {
//
//}
$configToSave[$key] = $values[$key];
if ($key == 'server_path') {
$server_path = $values[$key];
}
elseif (strpos($key, 'database_') !== false) {
$database[$key] = $values[$key];
}
unset($settings[$key]);
unset($values[$key]);
}
}
if($server_path[strlen($server_path) - 1] != '/')
$server_path .= '/';
// test config.lua existence
// if fail - revert the setting and inform the user
if (!file_exists($server_path . 'config.lua')) {
error('Server Path is invalid - cannot find config.lua in the directory. Setting have been reverted.');
$configToSave['server_path'] = $config['server_path'];
}
// test database connection
// if fail - revert the setting and inform the user
if ($database['database_overwrite'] && !Settings::testDatabaseConnection($database)) {
foreach ($database as $key => $value) {
if (!in_array($key, ['database_log', 'database_persistent'])) { // ignore these two
$configToSave[$key] = $config[$key];
}
}
}
return Settings::saveConfig($configToSave, BASE . 'config.local.php');
},
],
];