From 7929967ddc713297d48fa4d5e5e15d2d5f47ba1d Mon Sep 17 00:00:00 2001 From: slawkens Date: Mon, 15 May 2023 23:14:13 +0200 Subject: [PATCH] [WIP] More work on settings Move more config to settings (mainly mail_* + some other) Remove mail_admin, wasnt used anywhere Add return type to some functions Add Twig settings(key) function Possibility to save setting to db --- admin/pages/accounts.php | 2 +- admin/pages/mailer.php | 2 +- admin/pages/settings.php | 2 +- admin/pages/visitors.php | 9 +- admin/template/menus.php | 2 +- config.php | 22 --- index.php | 11 +- install/index.php | 4 - install/steps/5-database.php | 5 - system/compat/config.php | 4 +- system/functions.php | 158 +++++++++------- system/init.php | 14 +- system/libs/Settings.php | 157 ++++++++++----- system/pages/account/change_password.php | 2 +- system/pages/account/create.php | 6 +- system/pages/account/lost.php | 2 +- system/pages/account/manage.php | 2 +- system/pages/account/register.php | 2 +- system/pages/account/register_new.php | 2 +- system/pages/characters.php | 2 +- system/pages/experience_table.php | 4 +- system/settings.php | 178 ++++++++++++++++-- system/template.php | 4 +- system/templates/account.create.html.twig | 4 +- system/templates/experience_table.html.twig | 6 +- system/templates/install.config.html.twig | 6 +- .../templates/mail.password_changed.html.twig | 2 +- system/twig.php | 5 + templates/tibiacom/index.php | 2 +- 29 files changed, 403 insertions(+), 218 deletions(-) diff --git a/admin/pages/accounts.php b/admin/pages/accounts.php index 759b9361..24338fd8 100644 --- a/admin/pages/accounts.php +++ b/admin/pages/accounts.php @@ -361,7 +361,7 @@ else if (isset($_REQUEST['search'])) {
- getEMail() . '">Send Mail)' : ''); ?> + getEMail() . '">Send Mail)' : ''); ?>
diff --git a/admin/pages/mailer.php b/admin/pages/mailer.php index ea45eeb0..7d12f14b 100644 --- a/admin/pages/mailer.php +++ b/admin/pages/mailer.php @@ -15,7 +15,7 @@ if (!hasFlag(FLAG_CONTENT_MAILER) && !superAdmin()) { return; } -if (!config('mail_enabled')) { +if (!setting('core.mail_enabled')) { echo 'Mail support disabled in config.'; return; } diff --git a/admin/pages/settings.php b/admin/pages/settings.php index c33e76ef..a65af8ff 100644 --- a/admin/pages/settings.php +++ b/admin/pages/settings.php @@ -61,7 +61,7 @@ if (isset($_POST['save'])) { $title = ($plugin == 'core' ? 'Settings' : 'Plugin Settings - ' . $plugin); -$settings = Settings::parse($plugin, $settingsFile['settings']); +$settings = Settings::display($plugin, $settingsFile['settings']); $twig->display('admin.settings.html.twig', [ 'settings' => $settings, diff --git a/admin/pages/visitors.php b/admin/pages/visitors.php index 77626eb0..8ab78521 100644 --- a/admin/pages/visitors.php +++ b/admin/pages/visitors.php @@ -16,7 +16,7 @@ use DeviceDetector\Parser\OperatingSystem; $title = 'Visitors'; $use_datatable = true; -if (!$config['visitors_counter']): ?> +if (!setting('core.visitors_counter')): ?> Visitors counter is disabled.
You can enable it by editing this configurable in config.local.php file:

$config['visitors_counter'] = true;

@@ -25,10 +25,9 @@ if (!$config['visitors_counter']): ?> endif; require SYSTEM . 'libs/visitors.php'; -$visitors = new Visitors($config['visitors_counter_ttl']); +$visitors = new Visitors(setting('core.visitors_counter_ttl')); -function compare($a, $b) -{ +function compare($a, $b): int { return $a['lastvisit'] > $b['lastvisit'] ? -1 : 1; } @@ -61,7 +60,7 @@ foreach ($tmp as &$visitor) { } $twig->display('admin.visitors.html.twig', array( - 'config_visitors_counter_ttl' => $config['visitors_counter_ttl'], + 'config_visitors_counter_ttl' => setting('core.visitors_counter_ttl'), 'visitors' => $tmp )); ?> diff --git a/admin/template/menus.php b/admin/template/menus.php index 526a528d..5e60b39e 100644 --- a/admin/template/menus.php +++ b/admin/template/menus.php @@ -17,7 +17,7 @@ return [ ['name' => 'Add', 'link' => 'changelog&action=new', 'icon' => 'plus', 'order' => 20], ], ], - ['name' => 'Mailer', 'icon' => 'envelope', 'order' => 40, 'link' => 'mailer', 'disabled' => !config('mail_enabled')], + ['name' => 'Mailer', 'icon' => 'envelope', 'order' => 40, 'link' => 'mailer', 'disabled' => !setting('core.mail_enabled')], ['name' => 'Pages', 'icon' => 'book', 'order' => 50, 'link' => [ ['name' => 'View', 'link' => 'pages', 'icon' => 'list', 'order' => 10], diff --git a/config.php b/config.php index b9acd74d..f6298198 100644 --- a/config.php +++ b/config.php @@ -61,23 +61,6 @@ $config = array( 'account_change_character_sex_points' => 30, // cost of sex change 'characters_per_account' => 10, // max. number of characters per account - // mail - 'mail_enabled' => false, // is aac maker configured to send e-mails? - 'mail_address' => 'no-reply@your-server.org', // server e-mail address (from:) - 'mail_admin' => 'your-address@your-server.org', // admin email address, where mails from contact form will be sent - 'mail_signature' => array( // signature that will be included at the end of every message sent using _mail function - 'plain' => ""/*"--\nMy Server,\nhttp://www.myserver.com"*/, - 'html' => ''/*'
My Server,\nmyserver.com'*/ - ), - 'smtp_enabled' => false, // send by smtp or mail function (set false if use mail function, set to true if you use GMail or Microsoft Outlook) - 'smtp_host' => '', // mail host. smtp.gmail.com for GMail / smtp-mail.outlook.com for Microsoft Outlook - 'smtp_port' => 25, // 25 (default) / 465 (ssl, GMail) / 587 (tls, Microsoft Outlook) - 'smtp_auth' => true, // need authorization? - 'smtp_user' => 'admin@example.org', // here your email username - 'smtp_pass' => '', - 'smtp_secure' => '', // What kind of encryption to use on the SMTP connection. Options: '', 'ssl' (GMail) or 'tls' (Microsoft Outlook) - 'smtp_debug' => false, // set true to debug (you will see more info in error.log) - // 'generate_new_reckey' => true, // let player generate new recovery key, he will receive e-mail with new rec key (not display on page, hacker can't generate rec key) 'generate_new_reckey_price' => 20, // price for new recovery key @@ -217,13 +200,8 @@ $config = array( 'admin_panel_modules' => 'statistics,web_status,server_status,lastlogin,created,points,coins,balance', // default - statistics,web_status,server_status,lastlogin,created,points,coins,balance // other - 'anonymous_usage_statistics' => true, 'email_lai_sec_interval' => 60, // time in seconds between e-mails to one account from lost account interface, block spam 'google_analytics_id' => '', // e.g.: UA-XXXXXXX-X - 'experiencetable_columns' => 3, // how many columns to display in experience table page. * experiencetable_rows, 5 = 500 (will show up to 500 level) - 'experiencetable_rows' => 200, // till how many levels in one column - 'date_timezone' => 'Europe/Berlin', // more info at http://php.net/manual/en/timezones.php - 'footer_show_load_time' => true, // display load time of the page in the footer 'npc' => array() ); diff --git a/index.php b/index.php index 70bfdebf..5f887570 100644 --- a/index.php +++ b/index.php @@ -104,7 +104,7 @@ $hooks->trigger(HOOK_STARTUP); // anonymous usage statistics // sent only when user agrees -if(isset($config['anonymous_usage_statistics']) && $config['anonymous_usage_statistics']) { +if(setting('core.anonymous_usage_statistics')) { $report_time = 30 * 24 * 60 * 60; // report one time per 30 days $should_report = true; @@ -137,17 +137,16 @@ if(isset($config['anonymous_usage_statistics']) && $config['anonymous_usage_stat } } -if($config['views_counter']) +if(setting('core.views_counter')) require_once SYSTEM . 'counter.php'; -if($config['visitors_counter']) -{ +if(setting('core.visitors_counter')) { require_once SYSTEM . 'libs/visitors.php'; - $visitors = new Visitors($config['visitors_counter_ttl']); + $visitors = new Visitors(setting('core.visitors_counter_ttl')); } // backward support for gesior -if($config['backward_support']) { +if(setting('core.backward_support')) { define('INITIALIZED', true); $SQL = $db; $layout_header = template_header(); diff --git a/install/index.php b/install/index.php index 1c008c38..7de02a06 100644 --- a/install/index.php +++ b/install/index.php @@ -91,10 +91,6 @@ if($step == 'database') { break; } } - else if($key == 'mail_admin' && !Validator::email($value)) { - $errors[] = $locale['step_config_mail_admin_error']; - break; - } else if($key == 'timezone' && !in_array($value, DateTimeZone::listIdentifiers())) { $errors[] = $locale['step_config_timezone_error']; break; diff --git a/install/steps/5-database.php b/install/steps/5-database.php index 13a79d56..7fb1969b 100644 --- a/install/steps/5-database.php +++ b/install/steps/5-database.php @@ -79,11 +79,6 @@ if(!$error) { 'message' => $locale['loading_spinner'] )); - if(!Validator::email($_SESSION['var_mail_admin'])) { - error($locale['step_config_mail_admin_error']); - $error = true; - } - $content .= '$config[\'session_prefix\'] = \'myaac_' . generateRandomString(8, true, false, true, false) . '_\';'; $content .= PHP_EOL; $content .= '$config[\'cache_prefix\'] = \'myaac_' . generateRandomString(8, true, false, true, false) . '_\';'; diff --git a/system/compat/config.php b/system/compat/config.php index 2529442f..39cb5833 100644 --- a/system/compat/config.php +++ b/system/compat/config.php @@ -30,7 +30,7 @@ foreach ($deprecatedConfig as $value) { config( [ $value, - $settings['core.'.$value]['value'] + setting('core.'.$value) ] ); @@ -41,7 +41,7 @@ $vocationsParsed = array_map( function(string $value): string { return trim($value); }, - explode(',', $settings['core.vocations']['value']) + explode(',', setting('core.vocations')) ); config(['vocations', $vocationsParsed]); diff --git a/system/functions.php b/system/functions.php index 475fa174..1ad9be6e 100644 --- a/system/functions.php +++ b/system/functions.php @@ -32,52 +32,48 @@ function message($message, $type, $return) return true; } function success($message, $return = false) { - return message($message, 'success', $return); + return message($message, 'success', $return); } function warning($message, $return = false) { - return message($message, 'warning', $return); + return message($message, 'warning', $return); } function note($message, $return = false) { - return message($message, 'note', $return); + return message($message, 'note', $return); } function error($message, $return = false) { - return message($message, ((defined('MYAAC_INSTALL') || defined('MYAAC_ADMIN')) ? 'danger' : 'error'), $return); + return message($message, ((defined('MYAAC_INSTALL') || defined('MYAAC_ADMIN')) ? 'danger' : 'error'), $return); } -function longToIp($ip) +function longToIp($ip): string { $exp = explode(".", long2ip($ip)); return $exp[3].".".$exp[2].".".$exp[1].".".$exp[0]; } -function generateLink($url, $name, $blank = false) { +function generateLink($url, $name, $blank = false): string { return '' . $name . ''; } -function getFullLink($page, $name, $blank = false) { +function getFullLink($page, $name, $blank = false): string { return generateLink(getLink($page), $name, $blank); } -function getLink($page, $action = null) -{ - $settings = Settings::getInstance(); - return BASE_URL . ($settings['core.friendly_urls']['value'] ? '' : 'index.php/') . $page . ($action ? '/' . $action : ''); +function getLink($page, $action = null): string { + return BASE_URL . (setting('core.friendly_urls') ? '' : 'index.php/') . $page . ($action ? '/' . $action : ''); } -function internalLayoutLink($page, $action = null) {return getLink($page, $action);} - -function getForumThreadLink($thread_id, $page = NULL) -{ - $settings = Settings::getInstance(); - return BASE_URL . ($settings['core.friendly_urls']['value'] ? '' : 'index.php/') . 'forum/thread/' . (int)$thread_id . (isset($page) ? '/' . $page : ''); +function internalLayoutLink($page, $action = null): string { + return getLink($page, $action); } -function getForumBoardLink($board_id, $page = NULL) -{ - $settings = Settings::getInstance(); - return BASE_URL . ($settings['core.friendly_urls']['value'] ? '' : 'index.php/') . 'forum/board/' . (int)$board_id . (isset($page) ? '/' . $page : ''); +function getForumThreadLink($thread_id, $page = NULL): string { + return BASE_URL . (setting('core.friendly_urls') ? '' : 'index.php/') . 'forum/thread/' . (int)$thread_id . (isset($page) ? '/' . $page : ''); } -function getPlayerLink($name, $generate = true) +function getForumBoardLink($board_id, $page = NULL): string { + return BASE_URL . (setting('core.friendly_urls') ? '' : 'index.php/') . 'forum/board/' . (int)$board_id . (isset($page) ? '/' . $page : ''); +} + +function getPlayerLink($name, $generate = true): string { if(is_numeric($name)) { @@ -87,23 +83,21 @@ function getPlayerLink($name, $generate = true) $name = $player->getName(); } - $settings = Settings::getInstance(); - $url = BASE_URL . ($settings['core.friendly_urls']['value'] ? '' : 'index.php/') . 'characters/' . urlencode($name); + $url = BASE_URL . (setting('core.friendly_urls') ? '' : 'index.php/') . 'characters/' . urlencode($name); if(!$generate) return $url; return generateLink($url, $name); } -function getMonsterLink($name, $generate = true) +function getMonsterLink($name, $generate = true): string { - $settings = Settings::getInstance(); - $url = BASE_URL . ($settings['core.friendly_urls']['value'] ? '' : 'index.php/') . 'creatures/' . urlencode($name); + $url = BASE_URL . (setting('core.friendly_urls') ? '' : 'index.php/') . 'creatures/' . urlencode($name); if(!$generate) return $url; return generateLink($url, $name); } -function getHouseLink($name, $generate = true) +function getHouseLink($name, $generate = true): string { global $db; @@ -115,17 +109,14 @@ function getHouseLink($name, $generate = true) $name = $house->fetchColumn(); } - $settings = Settings::getInstance(); - $url = BASE_URL . ($settings['core.friendly_urls']['value'] ? '' : 'index.php/') . 'houses/' . urlencode($name); + $url = BASE_URL . (setting('core.friendly_urls') ? '' : 'index.php/') . 'houses/' . urlencode($name); if(!$generate) return $url; return generateLink($url, $name); } -function getGuildLink($name, $generate = true) +function getGuildLink($name, $generate = true): string { - global $config; - if(is_numeric($name)) { $name = getGuildNameById($name); if ($name === false) { @@ -133,8 +124,7 @@ function getGuildLink($name, $generate = true) } } - $settings = Settings::getInstance(); - $url = BASE_URL . ($settings['core.friendly_urls']['value'] ? '' : 'index.php/') . 'guilds/' . urlencode($name); + $url = BASE_URL . (setting('core.friendly_urls') ? '' : 'index.php/') . 'guilds/' . urlencode($name); if(!$generate) return $url; return generateLink($url, $name); @@ -180,7 +170,7 @@ function getItemRarity($chance) { return ''; } -function getFlagImage($country) +function getFlagImage($country): string { if(!isset($country[0])) return ''; @@ -202,7 +192,7 @@ function getFlagImage($country) * @param mixed $v Variable to check. * @return bool Value boolean status. */ -function getBoolean($v) +function getBoolean($v): bool { if(is_bool($v)) { return $v; @@ -225,7 +215,7 @@ function getBoolean($v) * @param bool $special Should special characters by used? * @return string Generated string. */ -function generateRandomString($length, $lowCase = true, $upCase = false, $numeric = false, $special = false) +function generateRandomString($length, $lowCase = true, $upCase = false, $numeric = false, $special = false): string { $characters = ''; if($lowCase) @@ -465,7 +455,7 @@ function tickers() * Types: head_start, head_end, body_start, body_end, center_top * */ -function template_place_holder($type) +function template_place_holder($type): string { global $twig, $template_place_holders; $ret = ''; @@ -489,7 +479,7 @@ function template_place_holder($type) /** * Returns content to be used by templates. */ -function template_header($is_admin = false) +function template_header($is_admin = false): string { global $title_full, $config, $twig; $charset = isset($config['charset']) ? $config['charset'] : 'utf-8'; @@ -506,29 +496,32 @@ function template_header($is_admin = false) /** * Returns footer content to be used by templates. */ -function template_footer() +function template_footer(): string { - global $config, $views_counter; + global $views_counter; $ret = ''; - if(admin()) + if(admin()) { $ret .= generateLink(ADMIN_URL, 'Admin Panel', true); + } - if($config['visitors_counter']) - { + if(setting('core.visitors_counter')) { global $visitors; $amount = $visitors->getAmountVisitors(); $ret .= '
Currently there ' . ($amount > 1 ? 'are' : 'is') . ' ' . $amount . ' visitor' . ($amount > 1 ? 's' : '') . '.'; } - if($config['views_counter']) + if(setting('core.views_counter')) { $ret .= '
Page has been viewed ' . $views_counter . ' times.'; + } - if(config('footer_show_load_time')) { + if(setting('core.footer_load_time')) { $ret .= '
Load time: ' . round(microtime(true) - START_TIME, 4) . ' seconds.'; } - if(isset($config['footer'][0])) - $ret .= '
' . $config['footer']; + $settingFooter = setting('core.footer'); + if(isset($settingFooter[0])) { + $ret .= '
' . $settingFooter; + } // please respect my work and help spreading the word, thanks! return $ret . '
' . base64_decode('UG93ZXJlZCBieSA8YSBocmVmPSJodHRwOi8vbXktYWFjLm9yZyIgdGFyZ2V0PSJfYmxhbmsiPk15QUFDLjwvYT4='); @@ -822,7 +815,7 @@ function getWorldName($id) /** * Mailing users. - * $config['mail_enabled'] have to be enabled. + * Mailing has to be enabled in settings (in Admin Panel). * * @param string $to Recipient email address. * @param string $subject Subject of the message. @@ -834,8 +827,9 @@ function _mail($to, $subject, $body, $altBody = '', $add_html_tags = true) { global $mailer, $config; - if (!config('mail_enabled')) { - log_append('mailer-error.log', '_mail() function has been used, but config.mail_enabled is disabled.'); + if (!setting('core.mail_enabled')) { + log_append('mailer-error.log', '_mail() function has been used, but Mail Support is disabled.'); + return false; } if(!$mailer) @@ -847,47 +841,60 @@ function _mail($to, $subject, $body, $altBody = '', $add_html_tags = true) $mailer->clearAllRecipients(); } - $signature_html = ''; - if(isset($config['mail_signature']['html'])) - $signature_html = $config['mail_signature']['html']; - + $signature_html = setting('core.mail_signature_html'); if($add_html_tags && isset($body[0])) $tmp_body = '' . $body . '

' . $signature_html . ''; else $tmp_body = $body . '

' . $signature_html; - if($config['smtp_enabled']) + define('MAIL_MAIL', 0); + define('MAIL_SMTP', 1); + + $mailOption = setting('core.mail_option'); + if($mailOption == MAIL_SMTP) { $mailer->isSMTP(); - $mailer->Host = $config['smtp_host']; - $mailer->Port = (int)$config['smtp_port']; - $mailer->SMTPAuth = $config['smtp_auth']; - $mailer->Username = $config['smtp_user']; - $mailer->Password = $config['smtp_pass']; - $mailer->SMTPSecure = isset($config['smtp_secure']) ? $config['smtp_secure'] : ''; + $mailer->Host = setting('core.smtp_host'); + $mailer->Port = setting('core.smtp_port'); + $mailer->SMTPAuth = setting('core.smtp_auth'); + $mailer->Username = setting('core.smtp_user'); + $mailer->Password = setting('core.smtp_pass'); + + define('SMTP_SECURITY_NONE', 0); + define('SMTP_SECURITY_SSL', 1); + define('SMTP_SECURITY_TLS', 2); + + $security = setting('core.smtp_security'); + + $tmp = ''; + if ($security === SMTP_SECURITY_SSL) { + $tmp = 'ssl'; + } + else if ($security == SMTP_SECURITY_TLS) { + $tmp = 'tls'; + } + + $mailer->SMTPSecure = $tmp; } else { $mailer->isMail(); } $mailer->isHTML(isset($body[0]) > 0); - $mailer->From = $config['mail_address']; - $mailer->Sender = $config['mail_address']; + $mailer->From = setting('core.mail_address'); + $mailer->Sender = setting('core.mail_address'); $mailer->CharSet = 'utf-8'; $mailer->FromName = $config['lua']['serverName']; $mailer->Subject = $subject; $mailer->addAddress($to); $mailer->Body = $tmp_body; - if(config('smtp_debug')) { + if(setting('core.smtp_debug')) { $mailer->SMTPDebug = 2; $mailer->Debugoutput = 'echo'; } - $signature_plain = ''; - if(isset($config['mail_signature']['plain'])) - $signature_plain = $config['mail_signature']['plain']; - + $signature_plain = setting('core.mail_signature_plain'); if(isset($altBody[0])) { $mailer->AltBody = $altBody . $signature_plain; } @@ -1121,7 +1128,16 @@ function configLua($key) { function setting($key) { $settings = Settings::getInstance(); - return $settings[$key]; + + if (is_array($key)) { + if (is_null($key[1])) { + unset($settings[$key[0]]); + } + + return $settings[$key[0]] = $key[1]; + } + + return $settings[$key]['value']; } function clearCache() diff --git a/system/init.php b/system/init.php index 0821d621..8e963db1 100644 --- a/system/init.php +++ b/system/init.php @@ -18,7 +18,6 @@ if(!isset($config['installed']) || !$config['installed']) { throw new RuntimeException('MyAAC has not been installed yet or there was error during installation. Please install again.'); } -date_default_timezone_set($config['date_timezone']); // take care of trailing slash at the end if($config['server_path'][strlen($config['server_path']) - 1] !== '/') $config['server_path'] .= '/'; @@ -120,7 +119,7 @@ if(!isset($config['highscores_ids_hidden']) || count($config['highscores_ids_hid $config['highscores_ids_hidden'] = array(0); } -$config['account_create_character_create'] = config('account_create_character_create') && (!config('mail_enabled') || !config('account_mail_verify')); +$config['account_create_character_create'] = config('account_create_character_create') && (!setting('core.mail_enabled') || !config('account_mail_verify')); // POT require_once SYSTEM . 'libs/pot/OTS.php'; @@ -136,12 +135,13 @@ $settings = Settings::getInstance(); $settings->load(); // deprecated config values -require_once __DIR__ . '/compat_config.php'; +require_once SYSTEM . 'compat/config.php'; -$settingsItemImagesURL = $settings['core.item_images_url']; -if($settingsItemImagesURL['value'][strlen($settingsItemImagesURL['value']) - 1] !== '/') { - $settingsItemImagesURL['value'] .= '/'; - $settings['core.item_images_url'] = $settingsItemImagesURL; +date_default_timezone_set(setting('core.date_timezone')); + +$settingsItemImagesURL = setting('core.item_images_url'); +if($settingsItemImagesURL[strlen($settingsItemImagesURL) - 1] !== '/') { + setting(['core.item_images_url', $settingsItemImagesURL . '/']); } define('USE_ACCOUNT_NAME', $db->hasColumn('accounts', 'name')); diff --git a/system/libs/Settings.php b/system/libs/Settings.php index b784e6dd..ff75a627 100644 --- a/system/libs/Settings.php +++ b/system/libs/Settings.php @@ -11,9 +11,10 @@ class Settings implements ArrayAccess { static private $instance; - private $plugins = []; + private $settingsArray = []; private $settings = []; private $cache = []; + private $valuesAsked = []; /** * @return Settings @@ -52,7 +53,25 @@ class Settings implements ArrayAccess } } - public static function parse($plugin, $settings): string + public function updateInDatabase($pluginName, $key, $value) + { + global $db; + $db->update(TABLE_PREFIX . 'settings', ['value' => $value], ['plugin_name' => $pluginName, 'key' => $key]); + } + + public function deleteFromDatabase($pluginName, $key = null) + { + global $db; + + if (!isset($key)) { + $db->delete(TABLE_PREFIX . 'settings', ['plugin_name' => $pluginName], -1); + } + else { + $db->delete(TABLE_PREFIX . 'settings', ['plugin_name' => $pluginName, 'key' => $key]); + } + } + + public static function display($plugin, $settings): string { global $db; @@ -93,7 +112,7 @@ class Settings implements ArrayAccess $j = 0; foreach($settings as $key => $setting) { if ($setting['type'] === 'category') { - if ($j++ !== 0) { + if ($j++ !== 0) { // close previous category echo '
'; } ?> @@ -103,7 +122,7 @@ class Settings implements ArrayAccess } if ($setting['type'] === 'section') { - if ($i++ !== 0) { + if ($i++ !== 0) { // close previous section echo ''; } ?> @@ -190,6 +209,14 @@ class Settings implements ArrayAccess $setting['options'] = $clients; } + else if ($setting['options'] == '$timezones') { + $timezones = []; + foreach (DateTimeZone::listIdentifiers() as $value) { + $timezones[$value] = $value; + } + + $setting['options'] = $timezones; + } else { if (is_string($setting['options'])) { @@ -263,47 +290,61 @@ class Settings implements ArrayAccess throw new \RuntimeException("Settings: You cannot set empty offset with value: $value!"); } - $pluginName = $offset; - if (strpos($offset, '.')) { - $explode = explode('.', $offset, 2); + $this->loadPlugin($offset); - $pluginName = $explode[0]; - $key = $explode[1]; - } - - $this->loadPlugin($pluginName); + $pluginName = $this->valuesAsked['pluginName']; + $key = $this->valuesAsked['key']; // remove whole plugin settings - if (!isset($key)) { - $this->plugins[$pluginName] = []; - - // remove from settings - if (isset($this->settings[$pluginName])) { - unset($this->settings[$pluginName]); - } - - // remove from cache - if (isset($this->cache[$pluginName])) { - unset($this->cache[$pluginName]); - } - /*foreach ($this->cache as $_key => $value) { - if (strpos($_key, $pluginName) !== false) { - unset($this->cache[$_key]); - } - }*/ + if (!isset($value)) { + $this->offsetUnset($offset); + $this->deleteFromDatabase($pluginName, $key); + return; } - $this->settings[$pluginName][$key] = $value['value']; + $this->settings[$pluginName][$key] = $value; + $this->updateInDatabase($pluginName, $key, $value); } #[\ReturnTypeWillChange] - public function offsetExists($offset) { - return isset($this->settings[$offset]); + public function offsetExists($offset): bool + { + $this->loadPlugin($offset); + + $pluginName = $this->valuesAsked['pluginName']; + $key = $this->valuesAsked['key']; + + // remove specified plugin settings (all) + if(is_null($key)) { + return isset($this->settings[$offset]); + } + + return isset($this->settings[$pluginName][$key]); } #[\ReturnTypeWillChange] - public function offsetUnset($offset) { - unset($this->settings[$offset]); + public function offsetUnset($offset) + { + $this->loadPlugin($offset); + + $pluginName = $this->valuesAsked['pluginName']; + $key = $this->valuesAsked['key']; + + if (isset($this->cache[$offset])) { + unset($this->cache[$offset]); + } + + // remove specified plugin settings (all) + if(!isset($key)) { + unset($this->settingsArray[$pluginName]); + unset($this->settings[$pluginName]); + $this->deleteFromDatabase($pluginName); + return; + } + + unset($this->settingsArray[$pluginName][$key]); + unset($this->settings[$pluginName][$key]); + $this->deleteFromDatabase($pluginName, $key); } /** @@ -322,24 +363,19 @@ class Settings implements ArrayAccess return $this->cache[$offset]; } - $pluginName = $offset; - if (strpos($offset, '.')) { - $explode = explode('.', $offset, 2); + $this->loadPlugin($offset); - $pluginName = $explode[0]; - $key = $explode[1]; - } - - $this->loadPlugin($pluginName); + $pluginName = $this->valuesAsked['pluginName']; + $key = $this->valuesAsked['key']; // return specified plugin settings (all) if(!isset($key)) { - return $this->plugins[$pluginName]; + return $this->settingsArray[$pluginName]; } $ret = []; - if(isset($this->plugins[$pluginName][$key])) { - $ret = $this->plugins[$pluginName][$key]; + if(isset($this->settingsArray[$pluginName][$key])) { + $ret = $this->settingsArray[$pluginName][$key]; } if(isset($this->settings[$pluginName][$key])) { @@ -348,7 +384,7 @@ class Settings implements ArrayAccess $ret['value'] = $value; } else { - $ret['value'] = $this->plugins[$pluginName][$key]['default']; + $ret['value'] = $this->settingsArray[$pluginName][$key]['default']; } if(isset($ret['type'])) { @@ -370,15 +406,36 @@ class Settings implements ArrayAccess return $ret; } - private function loadPlugin($pluginName) + private function updateValuesAsked($offset) { - if (!isset($this->plugins[$pluginName])) { + $pluginName = $offset; + if (strpos($offset, '.')) { + $explode = explode('.', $offset, 2); + + $pluginName = $explode[0]; + $key = $explode[1]; + + $this->valuesAsked = ['pluginName' => $pluginName, 'key' => $key]; + } + else { + $this->valuesAsked = ['pluginName' => $pluginName, 'key' => null]; + } + } + + private function loadPlugin($offset) + { + $this->updateValuesAsked($offset); + + $pluginName = $this->valuesAsked['pluginName']; + $key = $this->valuesAsked['key']; + + if (!isset($this->settingsArray[$pluginName])) { if ($pluginName === 'core') { $settingsFilePath = SYSTEM . 'settings.php'; } else { $pluginSettings = Plugins::getPluginSettings($pluginName); if (!$pluginSettings) { - error('This plugin does not exist or does not have settings defined.'); + warning("Setting $pluginName does not exist or does not have settings defined."); return; } @@ -390,7 +447,7 @@ class Settings implements ArrayAccess } $tmp = require $settingsFilePath; - $this->plugins[$pluginName] = $tmp['settings']; + $this->settingsArray[$pluginName] = $tmp['settings']; } } } diff --git a/system/pages/account/change_password.php b/system/pages/account/change_password.php index 01190497..aa5f19e7 100644 --- a/system/pages/account/change_password.php +++ b/system/pages/account/change_password.php @@ -69,7 +69,7 @@ else $account_logged->logAction('Account password changed.'); $message = ''; - if($config['mail_enabled'] && $config['send_mail_when_change_password']) { + if(setting('core.mail_enabled') && $config['send_mail_when_change_password']) { $mailBody = $twig->render('mail.password_changed.html.twig', array( 'new_password' => $org_pass, 'ip' => get_browser_real_ip(), diff --git a/system/pages/account/create.php b/system/pages/account/create.php index dd7c7713..2aa7c544 100644 --- a/system/pages/account/create.php +++ b/system/pages/account/create.php @@ -219,7 +219,7 @@ if($save) $tmp_account = (USE_ACCOUNT_NAME ? $account_name : $account_id); } - if($config['mail_enabled'] && $config['account_mail_verify']) + if(setting('core.mail_enabled') && $config['account_mail_verify']) { $hash = md5(generateRandomString(16, true, true) . $email); $new_account->setCustomField('email_hash', $hash); @@ -294,7 +294,7 @@ if($save) 'custom_buttons' => config('account_create_character_create') ? '' : null )); - if($config['mail_enabled'] && $config['account_welcome_mail']) + if(setting('core.mail_enabled') && $config['account_welcome_mail']) { $mailBody = $twig->render('account.welcome_mail.html.twig', array( 'account' => $tmp_account @@ -313,7 +313,7 @@ if($save) } $country_recognized = null; -if($config['account_country_recognize']) { +if(setting('core.account_country_recognize')) { $country_session = getSession('country'); if($country_session !== false) { // get from session $country_recognized = $country_session; diff --git a/system/pages/account/lost.php b/system/pages/account/lost.php index a474540c..7cef82cd 100644 --- a/system/pages/account/lost.php +++ b/system/pages/account/lost.php @@ -11,7 +11,7 @@ defined('MYAAC') or die('Direct access not allowed!'); $title = 'Lost Account Interface'; -if(!$config['mail_enabled']) +if(!setting('core.mail_enabled')) { echo 'Account maker is not configured to send e-mails, you can\'t use Lost Account Interface. Contact with admin to get help.'; return; diff --git a/system/pages/account/manage.php b/system/pages/account/manage.php index b2af3cee..038bb15e 100644 --- a/system/pages/account/manage.php +++ b/system/pages/account/manage.php @@ -35,7 +35,7 @@ if(empty($recovery_key)) $account_registered = 'No'; else { - if($config['generate_new_reckey'] && $config['mail_enabled']) + if($config['generate_new_reckey'] && setting('core.mail_enabled')) $account_registered = 'Yes ( Buy new Recovery Key )'; else $account_registered = 'Yes'; diff --git a/system/pages/account/register.php b/system/pages/account/register.php index 1d16d905..2d024dd7 100644 --- a/system/pages/account/register.php +++ b/system/pages/account/register.php @@ -31,7 +31,7 @@ if(isset($_POST['registeraccountsave']) && $_POST['registeraccountsave'] == "1") $account_logged->logAction('Generated recovery key.'); $message = ''; - if($config['mail_enabled'] && $config['send_mail_when_generate_reckey']) + if(setting('core.mail_enabled') && $config['send_mail_when_generate_reckey']) { $mailBody = $twig->render('mail.account.register.html.twig', array( 'recovery_key' => $new_rec_key diff --git a/system/pages/account/register_new.php b/system/pages/account/register_new.php index fb2e8ea3..7a5b43dc 100644 --- a/system/pages/account/register_new.php +++ b/system/pages/account/register_new.php @@ -21,7 +21,7 @@ if(isset($_POST['reg_password'])) $reg_password = encrypt((USE_ACCOUNT_SALT ? $account_logged->getCustomField('salt') : '') . $_POST['reg_password']); $reckey = $account_logged->getCustomField('key'); -if((!$config['generate_new_reckey'] || !$config['mail_enabled']) || empty($reckey)) { +if((!$config['generate_new_reckey'] || !setting('core.mail_enabled')) || empty($reckey)) { $errors[] = 'You cant get new recovery key.'; $twig->display('error_box.html.twig', array('errors' => $errors)); } diff --git a/system/pages/characters.php b/system/pages/characters.php index be817fe4..9eb4218b 100644 --- a/system/pages/characters.php +++ b/system/pages/characters.php @@ -342,7 +342,7 @@ WHERE killers.death_id = '".$death['id']."' ORDER BY killers.final_hit DESC, kil // signature $settings = Settings::getInstance(); if($config['signature_enabled']) { - $signature_url = BASE_URL . ($settings['core.friendly_urls']['value'] ? '' : 'index.php/') . urlencode($player->getName()) . '.png'; + $signature_url = BASE_URL . (setting('core.friendly_urls') ? '' : 'index.php/') . urlencode($player->getName()) . '.png'; } $hidden = $player->isHidden(); diff --git a/system/pages/experience_table.php b/system/pages/experience_table.php index d8ed39dc..e45b7714 100644 --- a/system/pages/experience_table.php +++ b/system/pages/experience_table.php @@ -11,9 +11,9 @@ defined('MYAAC') or die('Direct access not allowed!'); $title = 'Experience Table'; $experience = array(); -$columns = $config['experiencetable_columns']; +$columns = setting('core.experience_table_columns'); for($i = 0; $i < $columns; $i++) { - for($level = $i * $config['experiencetable_rows'] + 1; $level < $i * $config['experiencetable_rows'] + ($config['experiencetable_rows'] + 1); $level++) { + for($level = $i * setting('core.experience_table_rows') + 1; $level < $i * setting('core.experience_table_rows') + (setting('core.experience_table_rows') + 1); $level++) { $experience[$level] = OTS_Toolbox::experienceForLevel($level); } } diff --git a/system/settings.php b/system/settings.php index 80954cab..4c0c410f 100644 --- a/system/settings.php +++ b/system/settings.php @@ -14,6 +14,17 @@ return [ 'type' => 'category', 'title' => 'General' ], + [ + 'type' => 'section', + 'title' => 'General' + ], + 'date_timezone' => [ + 'name' => 'Date Timezone', + 'type' => 'options', + 'options' => '$timezones', + 'desc' => 'Timezone of the server, more info at http://php.net/manual/en/timezones.php', + 'default' => 'Europe/Warsaw', + ], [ 'type' => 'section', 'title' => 'Template' @@ -63,12 +74,18 @@ return [ 'desc' => 'Text displayed in the footer.
For example: ' . escapeHtml('
') . 'Your Server © 2023. All rights reserved.
', 'default' => '', ], - // do we really want this? + 'footer_load_time' => [ + 'name' => 'Load Time', + 'type' => 'boolean', + 'desc' => 'Display load time of the page in the footer', + 'default' => true, + ], + // do we really want this? I'm leaving it for consideration /* 'footer_powered_by' => [ 'name' => 'Display Powered by MyAAC', 'type' => 'boolean', - 'desc' => 'Do you want to show powered by myaac slogan on the footer?', + 'desc' => 'Do you want to show Powered by MyAAC slogan in the footer?', 'default' => true, ], */ @@ -154,6 +171,108 @@ return [ might bring some performance when disabled', 'default' => true, ], + 'anonymous_usage_statistics' => [ + 'name' => 'Anonymous Usage Statistics', + 'type' => 'boolean', + 'desc' => 'Allow MyAAC to report anonymous usage statistics to developers? The data is sent only once per 30 days and is fully confidential. It won\'t affect the performance of your website', + 'default' => true, + ], + [ + 'type' => 'category', + 'title' => 'Mailing', + ], + [ + 'type' => 'section', + 'title' => 'Mailing' + ], + 'mail_enabled' => [ + 'name' => 'Mailing enabled', + 'type' => 'boolean', + 'desc' => 'Is AAC configured to send e-mails?', + 'default' => false, + ], + 'mail_address' => [ + 'name' => 'Mail Address', + 'type' => 'email', + 'desc' => 'Server e-mail address (from:)', + 'default' => 'no-reply@your-server.org', + ], + /*'mail_admin' => [ + 'name' => 'Mail Admin Address', + 'type' => 'email', + 'desc' => 'Admin email address, where mails from contact form will be sent', + 'default' => 'your-address@your-server.org', + ],*/ + 'mail_signature_plain' => [ + 'name' => 'Mail Signature (Plain)', + 'type' => 'textarea', + 'desc' => 'Signature that will be included at the end of every message sent.
In Normal Format!', + 'default' => '-- +Sent by MyAAC, +https://my-aac.org', + ], + 'mail_signature_html' => [ + 'name' => 'Mail Signature (HTML)', + 'type' => 'textarea', + 'desc' => 'Signature that will be included at the end of every message sent.
In HTML Format!', + 'default' => escapeHtml('
+Sent by MyAAC,
+my-aac.org'), + ], + [ + 'type' => 'section', + 'title' => 'SMTP (Mail Server)' + ], + 'mail_option' => [ + 'name' => 'Mail Option', + 'type' => 'options', + 'options' => [0 => 'Mail (PHP Built-in)', 1 => 'SMTP (Gmail or Microsoft Outlook)'], + 'desc' => 'Mail sender. Set to SMTP if using Gmail or Microsoft Outlook, or any other provider', + 'default' => 0, + ], + 'smtp_host' => [ + 'name' => 'SMTP Host', + 'type' => 'text', + 'desc' => 'SMTP mail host. smtp.gmail.com for GMail / smtp-mail.outlook.com for Microsoft Outlook', + 'default' => '', + ], + 'smtp_port' => [ + 'name' => 'SMTP Host', + 'type' => 'number', + 'desc' => '25 (default) / 465 (ssl, GMail) / 587 (tls, Microsoft Outlook)', + 'default' => 25, + ], + 'smtp_auth' => [ + 'name' => 'SMTP Auth', + 'type' => 'boolean', + 'desc' => 'Need authorization for Server? In normal situation, almost always Yes.', + 'default' => true, + ], + 'smtp_user' => [ + 'name' => 'SMTP Username', + 'type' => 'text', + 'desc' => 'Here your email username to authenticate with SMTP', + 'default' => 'admin@example.org', + ], + 'smtp_pass' => [ + 'name' => 'SMTP Password', + 'type' => 'password', + 'desc' => 'Here your email password to authenticate with SMTP', + 'default' => '', + ], + 'smtp_security' => [ + 'name' => 'SMTP Security', + 'type' => 'options', + 'options' => ['None', 'SSL', 'TLS'], + 'desc' => 'What kind of encryption to use on the SMTP connection', + 'default' => '', + ], + 'smtp_debug' => [ + 'name' => 'SMTP Debug', + 'type' => 'boolean', + 'desc' => 'Activate to see more logs about mailing errors in error.log', + 'default' => false, + ], [ 'type' => 'category', 'title' => 'Accounts', @@ -407,25 +526,29 @@ return [ 'desc' => '', 'default' => true, ], + [ + 'type' => 'section', + 'title' => 'Experience Table Page' + ], + 'experience_table_columns' => [ + 'name' => 'Columns', + 'type' => 'number', + 'desc' => 'How many columns to display in experience table page. * experiencetable_rows, 5 = 500 (will show up to 500 level)', + 'default' => 3, + ], + 'experience_table_rows' => [ + 'name' => 'Rows', + 'type' => 'number', + 'desc' => 'Till how many levels in one column', + 'default' => 200, + ], [ 'type' => 'category', 'title' => 'Images', ], [ 'type' => 'section', - 'title' => 'Item and Outfit Images' - ], - 'outfit_images_url' => [ - 'name' => 'Outfit Images URL', - 'type' => 'text', - 'desc' => 'Set to animoutfit.php for animated outfit', - 'default' => 'http://outfit-images.ots.me/outfit.php', - ], - 'outfit_images_wrong_looktypes' => [ - 'name' => 'Outfit Images Wrong Looktypes', - 'type' => 'text', - 'desc' => 'This looktypes needs to have different margin-top and margin-left because they are wrong positioned', - 'default' => '75, 126, 127, 266, 302', + 'title' => 'Item Images' ], 'item_images_url' => [ 'name' => 'Item Images URL', @@ -441,7 +564,23 @@ return [ ], [ 'type' => 'section', - 'title' => 'Monsters Images' + 'title' => 'Outfit Images' + ], + 'outfit_images_url' => [ + 'name' => 'Outfit Images URL', + 'type' => 'text', + 'desc' => 'Set to animoutfit.php for animated outfit', + 'default' => 'http://outfit-images.ots.me/outfit.php', + ], + 'outfit_images_wrong_looktypes' => [ + 'name' => 'Outfit Images Wrong Looktypes', + 'type' => 'text', + 'desc' => 'This looktypes needs to have different margin-top and margin-left because they are wrong positioned', + 'default' => '75, 126, 127, 266, 302', + ], + [ + 'type' => 'section', + 'title' => 'Monster Images' ], 'monsters_images_url' => [ 'name' => 'Monsters Images URL', @@ -456,23 +595,24 @@ return [ 'default' => '.gif', ], 'monsters_images_preview' => [ - 'name' => 'Item Images URL', + 'name' => 'Monsters Images Preview', 'type' => 'boolean', 'desc' => 'Set to true to allow picture previews for creatures', 'default' => false, ], 'monsters_items_url' => [ - 'name' => 'Creatures Items URL', + 'name' => 'Monsters Items URL', 'type' => 'text', 'desc' => 'Set to website which shows details about items', 'default' => 'https://tibia.fandom.com/wiki/', ], 'monsters_loot_percentage' => [ - 'name' => 'Creatures Items URL', + 'name' => 'Monsters Items URL', 'type' => 'boolean', 'desc' => 'Set to true to show the loot tooltip percent', 'default' => true, ], + // this is hidden, because no implemented yet 'multiworld' => [ 'hidden' => true, 'type' => 'boolean', diff --git a/system/template.php b/system/template.php index d3dc5cb9..5aeb04ed 100644 --- a/system/template.php +++ b/system/template.php @@ -10,8 +10,8 @@ defined('MYAAC') or die('Direct access not allowed!'); // template -$template_name = $settings['core.template']['value']; -if($settings['core.template_allow_change']['value']) +$template_name = setting('core.template'); +if(setting('core.template_allow_change')) { if(isset($_GET['template'])) { diff --git a/system/templates/account.create.html.twig b/system/templates/account.create.html.twig index 8e885b43..f73cec0d 100644 --- a/system/templates/account.create.html.twig +++ b/system/templates/account.create.html.twig @@ -59,7 +59,7 @@ {% if errors.email is defined %}{{ errors.email }}{% endif %} - {% if config.mail_enabled and config.account_mail_verify %} + {% if setting('core.mail_enabled') and config.account_mail_verify %} Please use real address!
We will send a link to validate your Email.
{% endif %} @@ -119,7 +119,7 @@ {{ hook('HOOK_ACCOUNT_CREATE_BETWEEN_BOXES_1') }} - {% if (not config.mail_enabled or not config.account_mail_verify) and config.account_create_character_create %} + {% if (not setting('core.mail_enabled') or not config.account_mail_verify) and config.account_create_character_create %}
diff --git a/system/templates/experience_table.html.twig b/system/templates/experience_table.html.twig index 03efc671..c7bce536 100644 --- a/system/templates/experience_table.html.twig +++ b/system/templates/experience_table.html.twig @@ -6,14 +6,14 @@ Remember you can also check the respective skill bar in your skill window of the Experience Table - {% for i in 0..config.experiencetable_columns-1 %} + {% for i in 0..setting('core.experience_table_columns')-1 %} - {% for level in i * config.experiencetable_rows + 1..i * config.experiencetable_rows + (config.experiencetable_rows + 1) - 1 %} + {% for level in i * setting('core.experience_table_rows') + 1..i * setting('core.experience_table_rows') + (setting('core.experience_table_rows') + 1) - 1 %} @@ -23,4 +23,4 @@ Remember you can also check the respective skill bar in your skill window of the {% endfor %} -
Level Experience
{{ level }} {{ experience[level] }}
\ No newline at end of file + diff --git a/system/templates/install.config.html.twig b/system/templates/install.config.html.twig index a6ba24f7..33ec71ff 100644 --- a/system/templates/install.config.html.twig +++ b/system/templates/install.config.html.twig @@ -9,7 +9,7 @@
- {% for value in ['server_path', 'mail_admin'] %} + {% for value in ['server_path'] %}
@@ -43,9 +43,9 @@ {{ locale.step_config_usage_desc }}
- +
{{ buttons|raw }}
-
\ No newline at end of file + diff --git a/system/templates/mail.password_changed.html.twig b/system/templates/mail.password_changed.html.twig index d87601f0..a43af3a2 100644 --- a/system/templates/mail.password_changed.html.twig +++ b/system/templates/mail.password_changed.html.twig @@ -4,4 +4,4 @@ The request was made on {{ "now"|date("F j, Y, g:i a") }} by a user with the IP:

The new password is: {{ new_password }}

-If this was you, please ignore this email. If it was not you, please contact our support department at {{ config.mail_admin }}. \ No newline at end of file +If this was you, please ignore this email. If it was not you, please contact our support department. diff --git a/system/twig.php b/system/twig.php index 65e6ded0..c4eed802 100644 --- a/system/twig.php +++ b/system/twig.php @@ -106,6 +106,11 @@ $function = new TwigFunction('config', function ($key) { }); $twig->addFunction($function); +$function = new TwigFunction('setting', function ($key) { + return setting($key); +}); +$twig->addFunction($function); + $function = new TwigFunction('getCustomPage', function ($name) { $success = false; return getCustomPage($name, $success); diff --git a/templates/tibiacom/index.php b/templates/tibiacom/index.php index 989a05ca..863477ab 100644 --- a/templates/tibiacom/index.php +++ b/templates/tibiacom/index.php @@ -289,7 +289,7 @@ if(isset($config['boxes'])) logoartwork logoartwork logoartwork - logoartwork + logoartwork