Settings [WIP]

New Settings class
New Plugins::load() method
Move config.php to settings.php
MyAAC Settings will have plugin_name = 'core'
Add compat_config.php
This commit is contained in:
slawkens 2020-06-09 01:39:55 +02:00
parent 909bfffb51
commit fa0de1c413
11 changed files with 480 additions and 462 deletions

View File

@ -31,38 +31,8 @@ $config = array(
*/
'env' => 'prod', // 'prod' for production and 'dev' for development
'template' => 'kathrine', // template used by website (kathrine, tibiacom)
'template_allow_change' => true, // allow users to choose their own template while browsing website?
'vocations_amount' => 4, // how much basic vocations your server got (without promotion)
// what client version are you using on this OT?
// used for the Downloads page and some templates aswell
'client' => 1098, // 954 = client 9.54
'session_prefix' => 'myaac_', // must be unique for every site on your server
'friendly_urls' => false, // mod_rewrite is required for this, it makes links looks more elegant to eye, and also are SEO friendly (example: https://my-aac.org/guilds/Testing instead of https://my-aac.org/?subtopic=guilds&name=Testing). Remember to rename .htaccess.dist to .htaccess
'gzip_output' => false, // gzip page content before sending it to the browser, uses less bandwidth but more cpu cycles
// gesior backward support (templates & pages)
// allows using gesior templates and pages with myaac
// might bring some performance when disabled
'backward_support' => true,
// head options (html)
'meta_description' => 'Tibia is a free massive multiplayer online role playing game (MMORPG).', // description of the site
'meta_keywords' => 'free online game, free multiplayer game, ots, open tibia server', // keywords list separated by commas
// footer
'footer' => ''/*'<br/>Your Server &copy; 2016. All rights reserved.'*/,
'language' => 'en', // default language (currently only 'en' available)
'language_allow_change' => false,
'visitors_counter' => true,
'visitors_counter_ttl' => 10, // how long visitor will be marked as online (in minutes)
'views_counter' => true,
// cache system. by default file cache is used
'cache_engine' => 'auto', // apc, apcu, eaccelerator, xcache, file, auto, or blank to disable.
'cache_prefix' => 'myaac_', // have to be unique if running more MyAAC instances on the same server (except file system cache)
@ -84,10 +54,6 @@ $config = array(
//'2' => 'Your Second World Name'
),
// images
'outfit_images_url' => 'http://outfit-images.ots.me/outfit.php', // set to animoutfit.php for animated outfit
'item_images_url' => 'http://item-images.ots.me/1092/', // set to images/items if you host your own items in images folder
// account
'account_management' => true, // disable if you're using other method to manage users (fe. tfs account manager)
'account_create_auto_login' => false, // auto login after creating account?

25
system/compat_config.php Normal file
View File

@ -0,0 +1,25 @@
<?php
$deprecatedConfig = [
'template',
'template_allow_change',
'vocations_amount',
'client',
'session_prefix',
'friendly_urls',
'backward_support',
'charset',
'meta_description',
'meta_keywords',
'footer',
'language',
'visitors_counter',
'visitors_counter_ttl',
'views_counter',
'outfit_images_url',
'item_images_url',
];
foreach ($deprecatedConfig as $value) {
$config[$value] = $settings['core.'.$value]['value'];
}

View File

@ -77,27 +77,25 @@ function getFullLink($page, $name, $blank = false) {
function getLink($page, $action = null)
{
global $config;
return BASE_URL . ($config['friendly_urls'] ? '' : '?') . $page . ($action ? '/' . $action : '');
$settings = Settings::getInstance();
return BASE_URL . ($settings['core.friendly_urls']['value'] ? '' : '?') . $page . ($action ? '/' . $action : '');
}
function internalLayoutLink($page, $action = null) {return getLink($page, $action);}
function getForumThreadLink($thread_id, $page = NULL)
{
global $config;
return BASE_URL . ($config['friendly_urls'] ? '' : '?') . 'forum/thread/' . (int)$thread_id . (isset($page) ? '/' . $page : '');
$settings = Settings::getInstance();
return BASE_URL . ($settings['core.friendly_urls']['value'] ? '' : '?') . 'forum/thread/' . (int)$thread_id . (isset($page) ? '/' . $page : '');
}
function getForumBoardLink($board_id, $page = NULL)
{
global $config;
return BASE_URL . ($config['friendly_urls'] ? '' : '?') . 'forum/board/' . (int)$board_id . (isset($page) ? '/' . $page : '');
$settings = Settings::getInstance();
return BASE_URL . ($settings['core.friendly_urls']['value'] ? '' : '?') . 'forum/board/' . (int)$board_id . (isset($page) ? '/' . $page : '');
}
function getPlayerLink($name, $generate = true)
{
global $config;
if(is_numeric($name))
{
$player = new OTS_Player();
@ -106,7 +104,8 @@ function getPlayerLink($name, $generate = true)
$name = $player->getName();
}
$url = BASE_URL . ($config['friendly_urls'] ? '' : '?') . 'characters/' . urlencode($name);
$settings = Settings::getInstance();
$url = BASE_URL . ($settings['core.friendly_urls']['value'] ? '' : '?') . 'characters/' . urlencode($name);
if(!$generate) return $url;
return generateLink($url, $name);
@ -114,7 +113,7 @@ function getPlayerLink($name, $generate = true)
function getHouseLink($name, $generate = true)
{
global $db, $config;
global $db;
if(is_numeric($name))
{
@ -124,7 +123,8 @@ function getHouseLink($name, $generate = true)
$name = $house->fetchColumn();
}
$url = BASE_URL . ($config['friendly_urls'] ? '' : '?') . 'houses/' . urlencode($name);
$settings = Settings::getInstance();
$url = BASE_URL . ($settings['core.friendly_urls']['value'] ? '' : '?') . 'houses/' . urlencode($name);
if(!$generate) return $url;
return generateLink($url, $name);
@ -132,7 +132,7 @@ function getHouseLink($name, $generate = true)
function getGuildLink($name, $generate = true)
{
global $db, $config;
global $db;
if(is_numeric($name))
{
@ -142,7 +142,8 @@ function getGuildLink($name, $generate = true)
$name = $guild->fetchColumn();
}
$url = BASE_URL . ($config['friendly_urls'] ? '' : '?') . 'guilds/' . urlencode($name);
$settings = Settings::getInstance();
$url = BASE_URL . ($settings['core.friendly_urls']['value'] ? '' : '?') . 'guilds/' . urlencode($name);
if(!$generate) return $url;
return generateLink($url, $name);
@ -472,12 +473,12 @@ function template_place_holder($type)
*/
function template_header($is_admin = false)
{
global $title_full, $config;
$charset = isset($config['charset']) ? $config['charset'] : 'utf-8';
global $title_full;
$charset = config('charset') ? config('charset') : 'utf-8';
$ret = '
<meta charset="' . $charset . '">
<meta http-equiv="content-language" content="' . $config['language'] . '" />
<meta http-equiv="content-language" content="' . config('language') . '" />
<meta http-equiv="content-type" content="text/html; charset=' . $charset . '" />';
if(!$is_admin)
$ret .= '
@ -485,8 +486,8 @@ function template_header($is_admin = false)
<title>' . $title_full . '</title>';
$ret .= '
<meta name="description" content="' . $config['meta_description'] . '" />
<meta name="keywords" content="' . $config['meta_keywords'] . ', myaac, wodzaac" />
<meta name="description" content="' . config('meta_description') . '" />
<meta name="keywords" content="' . config('meta_keywords') . ', myaac, wodzaac" />
<meta name="generator" content="MyAAC" />
<link rel="stylesheet" type="text/css" href="' . BASE_URL . 'tools/css/messages.css" />
<script type="text/javascript" src="' . BASE_URL . 'tools/js/jquery.min.js"></script>
@ -496,7 +497,7 @@ function template_header($is_admin = false)
</noscript>
';
if($config['recaptcha_enabled'])
if(config('recaptcha_enabled'))
$ret .= "<script src='https://www.google.com/recaptcha/api.js'></script>";
return $ret;
}
@ -1081,6 +1082,9 @@ function deleteDirectory($dir, $ignore = array(), $contentOnly = false) {
function config($key) {
global $config;
if (is_array($key)) {
if (is_null($key[1])) {
unset($config[$key[0]]);
}
return $config[$key[0]] = $key[1];
}

View File

@ -88,9 +88,6 @@ if(isset($config['lua']['servername']))
if(isset($config['lua']['houserentperiod']))
$config['lua']['houseRentPeriod'] = $config['lua']['houserentperiod'];
if($config['item_images_url'][strlen($config['item_images_url']) - 1] !== '/')
$config['item_images_url'] .= '/';
// localize data/ directory based on data directory set in config.lua
foreach(array('dataDirectory', 'data_directory', 'datadir') as $key) {
if(!isset($config['lua'][$key][0])) {
@ -124,6 +121,20 @@ require_once SYSTEM . 'libs/pot/OTS.php';
$ots = POT::getInstance();
require_once SYSTEM . 'database.php';
// settings
require_once LIBS . 'Settings.php';
$settings = Settings::getInstance();
$settings->load();
// deprecated config values
require_once __DIR__ . '/compat_config.php';
$settingsItemImagesURL = $settings['core.item_images_url'];
if($settingsItemImagesURL['value'][strlen($settingsItemImagesURL['value']) - 1] !== '/') {
$settingsItemImagesURL['value'] .= '/';
$settings['core.item_images_url'] = $settingsItemImagesURL;
}
define('USE_ACCOUNT_NAME', $db->hasColumn('accounts', 'name'));
// load vocation names
$tmp = '';
@ -207,4 +218,4 @@ else {
config(['towns', $towns]);
//////////////////////////////////////////////
// END - load towns from database (TFS 1.3) //
//////////////////////////////////////////////
//////////////////////////////////////////////

View File

@ -10,35 +10,172 @@
class Settings implements ArrayAccess
{
private $container = array();
static private $instance;
private $plugins = [];
private $settings = [];
private $cache = [];
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->container[] = $value;
} else {
$this->container[$offset] = $value;
/**
* @return Settings
*/
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function load()
{
$cache = Cache::getInstance();
if ($cache->enabled()) {
$tmp = '';
if ($cache->fetch('settings', $tmp)) {
$this->settings = unserialize($tmp);
return;
}
}
global $db;
$settings = $db->query('SELECT * FROM `' . TABLE_PREFIX . 'settings`');
if($settings->rowCount() > 0) {
foreach ($settings->fetchAll(PDO::FETCH_ASSOC) as $setting) {
$this->settings[$setting['plugin_name']][$setting['key']] = $setting['value'];
}
}
if ($cache->enabled()) {
$cache->set('settings', serialize($this->settings), 600);
}
}
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
throw new \RuntimeException("Settings: You cannot set empty offset with value: $value!");
}
$pluginName = $offset;
if (strpos($offset, '.')) {
$explode = explode('.', $offset, 2);
$pluginName = $explode[0];
$key = $explode[1];
}
$this->loadPlugin($pluginName);
// 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]);
}
}*/
}
$this->settings[$pluginName][$key] = $value['value'];
}
public function offsetExists($offset) {
return isset($this->container[$offset]);
return isset($this->settings[$offset]);
}
public function offsetUnset($offset) {
unset($this->container[$offset]);
unset($this->settings[$offset]);
}
/**
* Get settings
* Usage: $setting['plugin_name.key']
* Example: $settings['shop_system.paypal_email']
*
* @param mixed $offset
* @return array|mixed
*/
public function offsetGet($offset)
{
if (!isset($this->container[$offset])) {
$file = PLUGINS . $offset . '/settings.php';
if(!file_exists($file)) {
throw new \RuntimeException('Failed to load settings file for plugin: ' . $offset);
}
$this->container[$offset] = require $file;
// try cache hit
if(isset($this->cache[$offset])) {
return $this->cache[$offset];
}
return $this->container[$offset];
$pluginName = $offset;
if (strpos($offset, '.')) {
$explode = explode('.', $offset, 2);
$pluginName = $explode[0];
$key = $explode[1];
}
$this->loadPlugin($pluginName);
// return specified plugin settings (all)
if(!isset($key)) {
return $this->plugins[$pluginName];
}
$ret = [];
if(isset($this->plugins[$pluginName][$key])) {
$ret = $this->plugins[$pluginName][$key];
}
if(isset($this->settings[$pluginName][$key])) {
$value = $this->settings[$pluginName][$key];
$ret['value'] = $value;
}
else {
$ret['value'] = $this->plugins[$pluginName][$key]['default'];
}
if(isset($ret['type'])) {
switch($ret['type']) {
case 'boolean':
$ret['value'] = $ret['value'] === 'true';
break;
case 'number':
$ret['value'] = (int)$ret['value'];
break;
default:
break;
}
}
$this->cache[$offset] = $ret;
return $ret;
}
private function loadPlugin($pluginName)
{
if (!isset($this->plugins[$pluginName])) {
if ($pluginName === 'core') {
$file = SYSTEM . 'settings.php';
} else {
$file = PLUGINS . $pluginName . '/settings.php';
}
if (!file_exists($file)) {
throw new \RuntimeException('Failed to load settings file for plugin: ' . $pluginName);
}
$this->plugins[$pluginName] = require $file;
}
}
}

View File

@ -44,6 +44,32 @@ class Plugins {
private static $warnings = array();
private static $error = null;
private static $plugin_json = array();
private static $plugins = [];
public static function load()
{
$cache = Cache::getInstance();
if ($cache->enabled()) {
$tmp = '';
if ($cache->fetch('plugins', $tmp)) {
self::$plugins = unserialize($tmp);
return;
}
}
foreach (get_plugins() as $filename) {
$plugin_json = self::getPluginJson($filename);
if (!$plugin_json) {
continue;
}
self::$plugins[$filename] = $plugin_json;
}
if ($cache->enabled()) {
$cache->set('hooks', serialize(self::$plugins), 600);
}
}
public static function getHooks()
{
@ -56,12 +82,7 @@ class Plugins {
}
$hooks = [];
foreach (get_plugins() as $filename) {
$plugin_json = self::getPluginJson($filename);
if (!$plugin_json) {
continue;
}
foreach (self::$plugins as $filename => $plugin_json) {
if (isset($plugin_json['hooks'])) {
foreach ($plugin_json['hooks'] as $_name => $info) {
if (defined('HOOK_'. $info['type'])) {
@ -81,18 +102,18 @@ class Plugins {
return $hooks;
}
public static function getPluginOptions($pluginName)
public static function getPluginSettings($pluginName)
{
$plugin_json = self::getPluginJson($pluginName);
if (!$plugin_json) {
return false;
}
if (!isset($plugin_json['options']) || !file_exists(BASE . $plugin_json['options'])) {
if (!isset($plugin_json['settings']) || !file_exists(BASE . $plugin_json['settings'])) {
return false;
}
return $plugin_json['options'];
return $plugin_json['settings'];
}
public static function getPluginJson($name = null)

View File

@ -1,309 +0,0 @@
<?php
/**
* Tools
*
* @package MyAAC
* @author Slawkens <slawkens@gmail.com>
* @copyright 2019 MyAAC
* @link https://my-aac.org
*/
defined('MYAAC') or die('Direct access not allowed!');
$title = 'Config Editor';
require_once SYSTEM . 'clients.conf.php';
$message = '';
$config_options = array(
'server_path' => array(
'name' => 'Server Path',
'type' => 'text',
'desc' => 'path to the server directory (same directory where config file is located)'
),
'env' => array(
'name' => 'Environment',
'type' => 'options',
'options' => array('prod' => 'Production', 'dev' => 'Development'),
'desc' => 'if you use this script on your live server - set to <i>Production</i><br/>
if you want to test and debug the script locally, or develop plugins, set to <i>Development</i><br/>
<strong>WARNING</strong>: on <i>Development</i> cache is disabled, so site will be significantly slower !!!<br/>
<strong>Recommended</strong>: <i>Production</i> cause of speed (page load time is better)'
),
'template' => array(
'name' => 'Template Name',
'type' => 'options',
'options' => '$templates',
'desc' => 'Name of the template used by website'
),
'template_allow_change' => array(
'name' => 'Template Allow Change',
'type' => 'boolean',
'desc' => 'Allow changing template of the website by showing a special select in the part of website'
),
'vocations_amount' => array(
'name' => 'Amount of Vocations',
'type' => 'number',
'desc' => 'how much basic vocations your server got (without promotion)'
),
'client' => array(
'name' => 'Client Version',
'type' => 'options',
'options' => '$clients',
'desc' => 'what client version are you using on this OT?<br/>
used for the Downloads page and some templates aswell'
),
'session_prefix' => array(
'name' => 'Session Prefix',
'type' => 'text',
'desc' => 'must be unique for every site on your server',
),
'friendly_urls' => array(
'name' => 'Friendly URLs',
'type' => 'boolean',
'desc' => 'mod_rewrite is required for this, it makes links looks more elegant to eye, and also are SEO friendly (example: https://my-aac.org/guilds/Testing instead of https://my-aac.org/?subtopic=guilds&name=Testing).<br/><strong>Remember to rename .htaccess.dist to .htaccess</strong>'
),
'gzip_output' => array(
'name' => 'GZIP Output',
'type' => 'boolean',
'desc' => 'gzip page content before sending it to the browser, uses less bandwidth but more cpu cycles'
),
'backward_support' => array(
'name' => 'Gesior Backward Support',
'type' => 'boolean',
'desc' => 'gesior backward support (templates & pages)<br/>
allows using gesior templates and pages with myaac<br/>
might bring some performance when disabled'
),
'meta_description' => array(
'name' => 'Meta Description',
'type' => 'textarea',
'desc' => 'description of the site in <meta>'
),
'meta_keywords' => array(
'name' => 'Meta Keywords',
'type' => 'textarea',
'desc' => 'keywords list separated by commas'
),
'title_separator' => array(
'name' => 'Title Separator',
'type' => 'text',
'desc' => 'Separator used in the title of the website'
),
'footer' => array(
'name' => 'Footer',
'type' => 'textarea',
'desc' => 'For example: "<br/>Your Server &copy; 2016. All rights reserved."'
),
'language' => array(
'name' => 'Language',
'type' => 'options',
'options' => array('en' => 'English'),
'desc' => 'default language (currently only English available)'
),
'visitors_counter' => array(
'name' => 'Visitors Counter',
'type' => 'boolean',
'desc' => 'Enable Visitors Counter? It will show list of online members on the website in Admin Panel'
),
'visitors_counter_ttl' => array(
'name' => 'Visitors Counter TTL',
'type' => 'number',
'desc' => 'Time To Live for Visitors Counter. In other words - how long user will be marked as online. In Minutes'
),
'views_counter' => array(
'name' => 'Views Counter',
'type' => 'boolean',
'desc' => 'Enable Views Counter? It will show how many times the website has been viewed by users'
),
'cache_engine' => array(
'name' => 'Cache Engine',
'type' => 'text',
'desc' => 'cache system. by default file cache is used.<br/>
Other available options: apc, apcu, eaccelerator, xcache, file, auto, or blank to disable'
),
'cache_prefix' => array(
'name' => 'Cache Prefix',
'type' => 'text',
'desc' => 'have to be unique if running more MyAAC instances on the same server (except file system cache)'
),
'database_log' => array(
'name' => 'Database Log',
'type' => 'boolean',
'desc' => 'Should database queries be logged and displayed in the page source? They will be included at the end of the .html source of the page, only for Super Admin'
),
'database_socket' => array(
'name' => 'Database Socket',
'type' => 'text',
'desc' => 'Set if you want to connect to database through socket (example: /var/run/mysqld/mysqld.sock)'
),
'database_persistent' => array(
'name' => 'Database Persistent',
'type' => 'boolean',
'desc' => 'Use database permanent connection (like server), may speed up your site'
),
'outfit_images_url' => array(
'name' => 'Outfit Images URL',
'type' => 'text',
'desc' => 'Set to animoutfit.php for animated outfit'
),
'item_images_url' => array(
'name' => 'Item Images URL',
'type' => 'text',
'desc' => 'Set to images/items if you host your own items in images folder'
),
);
if (isset($_POST['save'])) {
$content = '<?php' . PHP_EOL . PHP_EOL .
'// place for your configuration directives, so you can later easily update myaac' . PHP_EOL . PHP_EOL .
"\$config['installed'] = true;";
foreach($config_options as $key => $_config) {
$content .= PHP_EOL . "\$config['$key'] = ";
if (in_array($_config['type'], array('boolean', 'number'))) {
$content .= $_POST[$key];
}
else if (in_array($_config['type'], array('text', 'textarea'))) {
$content .= "'" . $_POST[$key] . "'";
}
else if($_config['type'] === 'options') {
if(is_numeric($_POST[$key])) {
$content .= $_POST[$key];
}
else {
$content .= "'" . $_POST[$key] . "'";
}
}
$content .= ';';
}
//$saved = file_put_contents(BASE . 'config.local.php', $content);
$saved = false;
ob_start();
if($saved) {
?>
<div class="alert alert-success">
Config has been successfully saved.
</div>
<?php
}
else {
?>
<div class="alert alert-error">
<?= BASE ?>config.local.php couldn't be opened. Please copy this content and paste there:
<br/>
<textarea class="form-control" cols="70" rows="10"><?= $content ?></textarea>
</div>
<?php
}
$message = ob_get_clean();
}
?>
<form method="post">
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">Configuration Editor</h3>
</div>
<div class="box-body">
<?= $message ?>
<button name="save" type="submit" class="btn btn-primary">Save</button>
</div>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th width="10%">Key</th>
<th width="10%">Name</th>
<th width="30%">Value</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
foreach($config_options as $key => $_config) {
?>
<tr>
<td><label for="<?= $key ?>" class="control-label"><?= $key ?></label></td>
<td><label for="<?= $key ?>" class="control-label"><?= $_config['name'] ?></label></td>
<td>
<?php
if ($_config['type'] === 'boolean') {
$_config['type'] = 'options';
$_config['options'] = array('true' => 'Yes', 'false' => 'No');
}
else if (in_array($_config['type'], array('text', 'number'))) {
echo '<input class="form-control" type="' . $_config['type'] . '" name="' . $key . '" value="' . config($key) . '" id="' . $key . '"/>';
}
else if($_config['type'] === 'textarea') {
echo '<textarea class="form-control" name="' . $key . '" id="' . $key . '">' . config($key) . '</textarea>';
}
if ($_config['type'] === 'options') {
if ($_config['options'] === '$templates') {
$templates = array();
foreach (get_templates() as $value) {
$templates[$value] = $value;
}
$_config['options'] = $templates;
}
else if($_config['options'] === '$clients') {
$clients = array();
foreach((array)config('clients') as $client) {
$client_version = (string)($client / 100);
if(strpos($client_version, '.') === false)
$client_version .= '.0';
$clients[$client] = $client_version;
}
$_config['options'] = $clients;
}
echo '<select class="form-control" name="' . $key . '" id="' . $key . '">';
foreach ($_config['options'] as $value => $option) {
if($value === 'true') {
$selected = config($key) === true;
}
else if($value === 'false') {
$selected = config($key) === false;
}
else {
$selected = config($key) == $value;
}
echo '<option value="' . $value . '" ' . ($selected ? 'selected' : '') . '>' . $option . '</option>';
}
echo '</select>';
}
?>
</td>
<td>
<div class="well">
<?= $_config['desc'] ?>
</div>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<div class="box-footer">
<button name="save" type="submit" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>
</form>

View File

@ -8,71 +8,76 @@
* @link https://my-aac.org
*/
defined('MYAAC') or die('Direct access not allowed!');
$title = 'Options';
$title = 'Settings';
$plugin = $_GET['plugin'];
if (!isset($plugin) || empty($plugin)) {
require_once SYSTEM . 'clients.conf.php';
if (!isset($_GET['plugin']) || empty($_GET['plugin'])) {
error('Please select plugin name from left Panel.');
return;
}
$pluginOptions = Plugins::getPluginOptions($plugin);
if (!$pluginOptions) {
error('This plugin does not exist or does not have options defined.');
return;
}
$plugin = $_GET['plugin'];
$message = '';
$optionsFile = require BASE . $pluginOptions;
if (!is_array($optionsFile)) {
return;
}
$name = $optionsFile['name'];
$options = $optionsFile['options'];
if (isset($_POST['save'])) {
foreach ($options as $key => $_config) {
// TODO:
// Save functionality
// Check if exist, then INSERT or UPDATE
/*$query = $db->query(
sprintf('SELECT `value` FROM `%s` WHERE `name` = %s AND `key` = %s',
TABLE_PREFIX . 'options_' . $table,
$name,
$key)
);*/
if($plugin != 'core') {
$pluginSettings = Plugins::getPluginSettings($plugin);
if (!$pluginSettings) {
error('This plugin does not exist or does not have options defined.');
return;
}
}
$optionsValues = [];
$optionsTypes = ['bool', 'double', 'int', 'text', 'varchar'];
foreach($optionsTypes as $type) {
$query = 'SELECT `key`, `value` FROM `' . TABLE_PREFIX . 'options_' . $type . '` WHERE `name` = ' . $db->quote($name) . ';';
$query = $db->query($query);
$optionsValues = $optionsValues + $query->fetchAll();
if($plugin === 'core') {
$settingsFile = require SYSTEM . 'settings.php';
}
else {
$settingsFile = require BASE . $pluginSettings;
}
if (!is_array($settingsFile)) {
return;
}
if (isset($_POST['save'])) {
$db->query('DELETE FROM `' . TABLE_PREFIX . 'settings` WHERE `plugin_name` = ' . $db->quote($plugin) . ';');
foreach ($_POST['settings'] as $key => $value) {
try {
$db->insert(TABLE_PREFIX . 'settings', ['plugin_name' => $plugin, 'key' => $key, 'value' => $value]);
} catch (PDOException $error) {
warning('Error while saving setting (' . $plugin . ' - ' . $key . '): ' . $error->getMessage());
}
}
$cache = Cache::getInstance();
if ($cache->enabled()) {
$cache->delete('settings');
}
success('Saved at ' . date('H:i'));
}
$title = ($plugin == 'core' ? 'MyAAC Settings' : 'Plugin Settings - ' . $plugin);
$query = 'SELECT `key`, `value` FROM `' . TABLE_PREFIX . 'settings` WHERE `plugin_name` = ' . $db->quote($plugin) . ';';
$query = $db->query($query);
$settingsDb = [];
if($query->rowCount() > 0) {
foreach($query->fetchAll(PDO::FETCH_ASSOC) as $value) {
$settingsDb[$value['key']] = $value['value'];
}
}
//var_dump($optionsValues);
?>
<form method="post">
<div class="row">
<div class="col-md-12">
<div class="box">
<div class="box-header">
<h3 class="box-title">Plugin Options - <?= $plugin ?></h3>
</div>
<div class="box-body">
<?= $message ?>
<button name="save" type="submit" class="btn btn-primary">Save</button>
</div>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th style="width: 10%">Key</th>
<th style="width: 10%">Name</th>
<th style="width: 30%">Value</th>
<th>Description</th>
@ -81,37 +86,76 @@ foreach($optionsTypes as $type) {
<tbody>
<?php
foreach($options as $key => $_config) {
$checkbox = function ($key, $type, $value) {
echo '<label><input type="radio" id="' . $key . '" name="settings[' . $key . ']" value="' . ($type ? 'true' : 'false') . '" ' . ($value === $type ? 'checked' : '') . '/>' . ($type ? 'Yes' : 'No') . '</label> ';
};
foreach($settingsFile as $key => $setting) {
?>
<tr>
<td><label for="<?= $key ?>" class="control-label"><?= $key ?></label></td>
<td><label for="<?= $key ?>" class="control-label"><?= $_config['name'] ?></label></td>
<td><label for="<?= $key ?>" class="control-label"><?= $setting['name'] ?></label></td>
<td>
<?php
if ($_config['type'] === 'boolean') {
$_config['type'] = 'options';
$_config['options'] = ['true' => 'Yes', 'false' => 'No'];
}
else if (in_array($_config['type'], ['varchar', 'number'])) {
echo '<input class="form-control" type="' . $_config['type'] . '" name="' . $key . '" value="' . (config($key) === null ? $_config['default'] : config($key)) . '" id="' . $key . '"/>';
}
else if($_config['type'] === 'textarea') {
echo '<textarea class="form-control" name="' . $key . '" id="' . $key . '">' . config($key) . '</textarea>';
}
if ($_config['type'] === 'options') {
echo '<select class="form-control" name="' . $key . '" id="' . $key . '">';
foreach ($_config['options'] as $value => $option) {
if($value === 'true') {
$selected = config($key) === true;
}
else if($value === 'false') {
$selected = config($key) === false;
if ($setting['type'] === 'boolean') {
if(isset($settingsDb[$key])) {
if($settingsDb[$key] === 'true') {
$value = true;
}
else {
$selected = config($key) == $value;
$value = false;
}
}
else {
$value = (isset($setting['default']) ? $setting['default'] : false);
}
$checkbox($key, true, $value);
$checkbox($key, false, $value);
}
else if (in_array($setting['type'], ['text', 'number'])) {
echo '<input class="form-control" type="' . $setting['type'] . '" name="settings[' . $key . ']" value="' . (isset($settingsDb[$key]) ? $settingsDb[$key] : (!empty($setting['default']) ? $setting['default'] : '')) . '" id="' . $key . '"/>';
}
else if($setting['type'] === 'textarea') {
echo '<textarea class="form-control" name="settings[' . $key . ']" id="' . $key . '">' . (isset($settingsDb[$key]) ? $settingsDb[$key] : (!empty($setting['default']) ? $setting['default'] : '')) . '</textarea>';
}
if ($setting['type'] === 'options') {
if ($setting['options'] === '$templates') {
$templates = array();
foreach (get_templates() as $value) {
$templates[$value] = $value;
}
$setting['options'] = $templates;
}
else if($setting['options'] === '$clients') {
$clients = array();
foreach((array)config('clients') as $client) {
$client_version = (string)($client / 100);
if(strpos($client_version, '.') === false)
$client_version .= '.0';
$clients[$client] = $client_version;
}
$setting['options'] = $clients;
}
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'] : ''));
if($value === 'true') {
$selected = $compareTo === true;
}
else if($value === 'false') {
$selected = $compareTo === false;
}
else {
$selected = $compareTo == $value;
}
echo '<option value="' . $value . '" ' . ($selected ? 'selected' : '') . '>' . $option . '</option>';
@ -122,7 +166,7 @@ foreach($optionsTypes as $type) {
</td>
<td>
<div class="well">
<?= $_config['desc'] ?>
<?= $setting['desc'] ?>
</div>
</td>
</tr>

View File

@ -344,8 +344,9 @@ 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 . ($config['friendly_urls'] ? '' : '?') . urlencode($player->getName()) . '.png';
$signature_url = BASE_URL . ($settings['core.friendly_urls']['value'] ? '' : '?') . urlencode($player->getName()) . '.png';
}
$hidden = $player->isHidden();

118
system/settings.php Normal file
View File

@ -0,0 +1,118 @@
<?php
return [
'template' => [
'name' => 'Template Name',
'type' => 'options',
'options' => '$templates',
'desc' => 'Name of the template used by website',
'default' => 'kathrine',
],
'template_allow_change' => [
'name' => 'Template Allow Change',
'type' => 'boolean',
'desc' => 'Allow changing template of the website by showing a special select in the part of website',
'default' => true,
],
'vocations_amount' => [
'name' => 'Amount of Vocations',
'type' => 'number',
'desc' => 'how much basic vocations your server got (without promotion)',
'default' => 4,
],
'client' => [
'name' => 'Client Version',
'type' => 'options',
'options' => '$clients',
'desc' => 'what client version are you using on this OT?<br/>
used for the Downloads page and some templates aswell',
'default' => 710
],
'session_prefix' => [
'name' => 'Session Prefix',
'type' => 'text',
'desc' => 'must be unique for every site on your server',
'default' => 'myaac_',
],
'friendly_urls' => [
'name' => 'Friendly URLs',
'type' => 'boolean',
'desc' => 'mod_rewrite is required for this, it makes links looks more elegant to eye, and also are SEO friendly (example: https://my-aac.org/guilds/Testing instead of https://my-aac.org/?subtopic=guilds&name=Testing).<br/><strong>Remember to rename .htaccess.dist to .htaccess</strong>',
'default' => false,
],
'backward_support' => [
'name' => 'Gesior Backward Support',
'type' => 'boolean',
'desc' => 'gesior backward support (templates & pages)<br/>
allows using gesior templates and pages with myaac<br/>
might bring some performance when disabled',
'default' => true,
],
'charset' => [
'name' => 'Meta Charset',
'type' => 'text',
'desc' => 'Charset used in <meta>',
'default' => 'utf-8',
],
'meta_description' => [
'name' => 'Meta Description',
'type' => 'textarea',
'desc' => 'description of the site in <meta>',
'default' => 'Tibia is a free massive multiplayer online role playing game (MMORPG).',
],
'meta_keywords' => [
'name' => 'Meta Keywords',
'type' => 'textarea',
'desc' => 'keywords list separated by commas',
'default' => 'free online game, free multiplayer game, ots, open tibia server',
],
'footer' => [
'name' => 'Footer',
'type' => 'textarea',
'desc' => 'For example: "' . htmlspecialchars('<br/>') . 'Your Server &copy; 2020. All rights reserved."',
'default' => '',
],
'language' => [
'name' => 'Language',
'type' => 'options',
'options' => ['en' => 'English'],
'desc' => 'default language (currently only English available)',
'default' => 'en',
],
/*'language_allow_change' => [
'name' => 'Language Allow Change',
'type' => 'boolean',
'default' => false,
'desc' => 'default language (currently only English available)'
],*/
'visitors_counter' => [
'name' => 'Visitors Counter',
'type' => 'boolean',
'desc' => 'Enable Visitors Counter? It will show list of online members on the website in Admin Panel',
'default' => true,
],
'visitors_counter_ttl' => [
'name' => 'Visitors Counter TTL',
'type' => 'number',
'desc' => 'Time To Live for Visitors Counter. In other words - how long user will be marked as online. In Minutes',
'default' => 10
],
'views_counter' => [
'name' => 'Views Counter',
'type' => 'boolean',
'desc' => 'Enable Views Counter? It will show how many times the website has been viewed by users',
'default' => true,
],
'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',
],
'item_images_url' => [
'name' => 'Item Images URL',
'type' => 'text',
'desc' => 'Set to images/items if you host your own items in images folder',
'default' => 'http://item-images.ots.me/1092/',
],
];

View File

@ -10,8 +10,8 @@
defined('MYAAC') or die('Direct access not allowed!');
// template
$template_name = $config['template'];
if($config['template_allow_change'])
$template_name = $settings['core.template']['value'];
if($settings['core.template_allow_change']['value'])
{
if(isset($_GET['template']))
{