diff --git a/admin/includes/settings_menus.php b/admin/includes/settings_menus.php index 2ce784fc..e612a888 100644 --- a/admin/includes/settings_menus.php +++ b/admin/includes/settings_menus.php @@ -1,5 +1,7 @@ array('Fist fighting', 'fist'), diff --git a/admin/pages/plugins.php b/admin/pages/plugins.php index 6eb0862e..f130a15c 100644 --- a/admin/pages/plugins.php +++ b/admin/pages/plugins.php @@ -7,6 +7,9 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ + +use MyAAC\Plugins; + defined('MYAAC') or die('Direct access not allowed!'); $title = 'Plugin manager'; @@ -14,8 +17,6 @@ csrfProtect(); $use_datatable = true; -require_once LIBS . 'plugins.php'; - if (!getBoolean(setting('core.admin_plugins_manage_enable'))) { warning('Plugin installation and management is disabled in Settings.
If you wish to enable, go to Settings and enable Enable Plugins Manage.'); } diff --git a/admin/pages/settings.php b/admin/pages/settings.php index 5b354c99..4533b33b 100644 --- a/admin/pages/settings.php +++ b/admin/pages/settings.php @@ -7,6 +7,10 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ + +use MyAAC\Plugins; +use MyAAC\Settings; + defined('MYAAC') or die('Direct access not allowed!'); $title = 'Settings'; diff --git a/admin/pages/visitors.php b/admin/pages/visitors.php index 8ab78521..9e83d339 100644 --- a/admin/pages/visitors.php +++ b/admin/pages/visitors.php @@ -12,6 +12,7 @@ defined('MYAAC') or die('Direct access not allowed!'); use DeviceDetector\DeviceDetector; use DeviceDetector\Parser\Client\Browser; use DeviceDetector\Parser\OperatingSystem; +use MyAAC\Visitors; $title = 'Visitors'; $use_datatable = true; @@ -24,7 +25,6 @@ if (!setting('core.visitors_counter')): ?> return; endif; -require SYSTEM . 'libs/visitors.php'; $visitors = new Visitors(setting('core.visitors_counter_ttl')); function compare($a, $b): int { diff --git a/admin/tools/reload_data.php b/admin/tools/reload_data.php index 79740d31..cb0a1cec 100644 --- a/admin/tools/reload_data.php +++ b/admin/tools/reload_data.php @@ -22,6 +22,9 @@ * @copyright 2020 MyAAC * @link https://my-aac.org */ + +use MyAAC\DataLoader; + define('MYAAC_ADMIN', true); require '../../common.php'; @@ -37,8 +40,6 @@ ob_implicit_flush(); ob_end_flush(); header('X-Accel-Buffering: no'); -require LIBS . 'DataLoader.php'; - require LOCALE . 'en/main.php'; require LOCALE . 'en/install.php'; diff --git a/admin/tools/settings_save.php b/admin/tools/settings_save.php index 500c83bb..7ed43093 100644 --- a/admin/tools/settings_save.php +++ b/admin/tools/settings_save.php @@ -1,4 +1,8 @@ load(); diff --git a/composer.json b/composer.json index f022369e..ee65ddbe 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "autoload": { "psr-4": { "MyAAC\\": "system/src" - } + }, + "files": ["system/src/global.php"] } } diff --git a/index.php b/index.php index 39f457cb..56237bfd 100644 --- a/index.php +++ b/index.php @@ -24,6 +24,9 @@ * @link https://my-aac.org */ +use MyAAC\UsageStatistics; +use MyAAC\Visitors; + require_once 'common.php'; require_once SYSTEM . 'functions.php'; @@ -146,8 +149,7 @@ if(setting('core.anonymous_usage_statistics')) { } if($should_report) { - require_once LIBS . 'usage_statistics.php'; - Usage_Statistics::report(); + UsageStatistics::report(); updateDatabaseConfig('last_usage_report', time()); if($cache->enabled()) { @@ -160,7 +162,6 @@ if(setting('core.views_counter')) require_once SYSTEM . 'counter.php'; if(setting('core.visitors_counter')) { - require_once SYSTEM . 'libs/visitors.php'; $visitors = new Visitors(setting('core.visitors_counter_ttl')); } diff --git a/install/index.php b/install/index.php index 9c2e5048..d6043d42 100644 --- a/install/index.php +++ b/install/index.php @@ -12,7 +12,6 @@ require SYSTEM . 'functions.php'; require BASE . 'install/includes/functions.php'; require BASE . 'install/includes/locale.php'; require SYSTEM . 'clients.conf.php'; -require LIBS . 'Settings.php'; // ignore undefined index from Twig autoloader $config['env'] = 'prod'; diff --git a/install/steps/5-database.php b/install/steps/5-database.php index b75608fc..6ee7d031 100644 --- a/install/steps/5-database.php +++ b/install/steps/5-database.php @@ -1,4 +1,7 @@ 'What is this?', diff --git a/system/compat/base.php b/system/compat/base.php index 40d2617e..1a606be3 100644 --- a/system/compat/base.php +++ b/system/compat/base.php @@ -9,10 +9,12 @@ */ defined('MYAAC') or die('Direct access not allowed!'); +class Validator extends \MyAAC\Validator {} + function check_name($name, &$errors = '') { if(Validator::characterName($name)) return true; - + $errors = Validator::getLastError(); return false; } @@ -20,7 +22,7 @@ function check_name($name, &$errors = '') { function check_account_id($id, &$errors = '') { if(Validator::accountId($id)) return true; - + $errors = Validator::getLastError(); return false; } @@ -28,7 +30,7 @@ function check_account_id($id, &$errors = '') { function check_account_name($name, &$errors = '') { if(Validator::accountName($name)) return true; - + $errors = Validator::getLastError(); return false; } @@ -36,7 +38,7 @@ function check_account_name($name, &$errors = '') { function check_name_new_char($name, &$errors = '') { if(Validator::newCharacterName($name)) return true; - + $errors = Validator::getLastError(); return false; } @@ -44,7 +46,7 @@ function check_name_new_char($name, &$errors = '') { function check_rank_name($name, &$errors = '') { if(Validator::rankName($name)) return true; - + $errors = Validator::getLastError(); return false; } @@ -52,7 +54,7 @@ function check_rank_name($name, &$errors = '') { function check_guild_name($name, &$errors = '') { if(Validator::guildName($name)) return true; - + $errors = Validator::getLastError(); return false; } @@ -72,4 +74,4 @@ function fieldExist($field, $table) global $db; return $db->hasColumn($table, $field); } -?> \ No newline at end of file +?> diff --git a/system/counter.php b/system/counter.php index 63905ba2..2c2512d3 100644 --- a/system/counter.php +++ b/system/counter.php @@ -7,6 +7,9 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ + +use MyAAC\Cache\Cache; + defined('MYAAC') or die('Direct access not allowed!'); define('COUNTER_SYNC', 10); // how often counter is synchronized with database (each x site refreshes) diff --git a/system/exception.php b/system/exception.php index 077d0f3f..94b60c47 100644 --- a/system/exception.php +++ b/system/exception.php @@ -8,6 +8,8 @@ * @link https://my-aac.org */ +use MyAAC\Exceptions\SensitiveException; + if (class_exists(\Whoops\Run::class)) { $whoops = new \Whoops\Run; if(IS_CLI) { @@ -21,8 +23,6 @@ if (class_exists(\Whoops\Run::class)) { return; } -require LIBS . 'SensitiveException.php'; - /** * @param Exception $exception */ diff --git a/system/functions.php b/system/functions.php index 5c18b67c..df619acf 100644 --- a/system/functions.php +++ b/system/functions.php @@ -9,12 +9,17 @@ */ defined('MYAAC') or die('Direct access not allowed!'); +use MyAAC\Cache\Cache; use MyAAC\CsrfToken; +use MyAAC\Items; use MyAAC\Models\Config; use MyAAC\Models\Guild; use MyAAC\Models\House; use MyAAC\Models\Pages; use MyAAC\Models\Player; +use MyAAC\News; +use MyAAC\Plugins; +use MyAAC\Settings; use PHPMailer\PHPMailer\PHPMailer; use Twig\Loader\ArrayLoader as Twig_ArrayLoader; @@ -137,7 +142,6 @@ function getGuildLink($name, $generate = true): string } function getItemNameById($id) { - require_once LIBS . 'items.php'; $item = Items::get($id); return !empty($item['name']) ? $item['name'] : ''; } @@ -1195,7 +1199,6 @@ function setting($key) function clearCache() { - require_once LIBS . 'news.php'; News::clearCache(); $cache = Cache::getInstance(); @@ -1689,7 +1692,6 @@ function makeLinksClickable($text, $blank = true) { } // validator functions -require_once LIBS . 'validator.php'; require_once SYSTEM . 'compat/base.php'; // custom functions diff --git a/system/init.php b/system/init.php index 0bf55c24..d4867a91 100644 --- a/system/init.php +++ b/system/init.php @@ -8,7 +8,12 @@ * @link https://my-aac.org */ +use DebugBar\StandardDebugBar; +use MyAAC\Cache\Cache; use MyAAC\CsrfToken; +use MyAAC\Hooks; +use MyAAC\Settings; +use MyAAC\Towns; defined('MYAAC') or die('Direct access not allowed!'); @@ -16,8 +21,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.'); } -use DebugBar\StandardDebugBar; - if(config('env') === 'dev') { require SYSTEM . 'exception.php'; } @@ -39,11 +42,9 @@ if(isset($config['gzip_output']) && $config['gzip_output'] && isset($_SERVER['HT ob_start('ob_gzhandler'); // cache -require_once SYSTEM . 'libs/cache.php'; $cache = Cache::getInstance(); // event system -require_once SYSTEM . 'hooks.php'; $hooks = new Hooks(); $hooks->load(); @@ -145,7 +146,6 @@ require_once SYSTEM . 'database.php'; require SYSTEM . 'migrate.php'; // settings -require_once LIBS . 'Settings.php'; $settings = Settings::getInstance(); $settings->load(); @@ -176,5 +176,4 @@ define('USE_ACCOUNT_NAME', $db->hasColumn('accounts', 'name')); define('USE_ACCOUNT_NUMBER', $db->hasColumn('accounts', 'number')); define('USE_ACCOUNT_SALT', $db->hasColumn('accounts', 'salt')); -require LIBS . 'Towns.php'; Towns::load(); diff --git a/system/libs/SensitiveException.php b/system/libs/SensitiveException.php deleted file mode 100644 index 58978198..00000000 --- a/system/libs/SensitiveException.php +++ /dev/null @@ -1,3 +0,0 @@ -enabled()) { + if(class_exists('MyAAC\Cache\Cache') && ($cache = Cache::getInstance()) && $cache->enabled()) { $tmp = null; $need_revalidation = true; if($cache->fetch('database_checksum', $tmp) && $tmp) { @@ -145,7 +147,7 @@ class OTS_DB_MySQL extends OTS_Base_DB { global $config; - if(class_exists('Cache') && ($cache = Cache::getInstance()) && $cache->enabled()) { + if(class_exists('MyAAC\Cache\Cache') && ($cache = Cache::getInstance()) && $cache->enabled()) { $cache->set('database_tables', serialize($this->has_table_cache), 3600); $cache->set('database_columns', serialize($this->has_column_cache), 3600); $cache->set('database_checksum', serialize(sha1($config['database_host'] . '.' . $config['database_name'])), 3600); diff --git a/system/libs/pot/OTS_Groups_List.php b/system/libs/pot/OTS_Groups_List.php index 868180dd..af4df170 100644 --- a/system/libs/pot/OTS_Groups_List.php +++ b/system/libs/pot/OTS_Groups_List.php @@ -8,6 +8,8 @@ * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public License, Version 3 */ +use MyAAC\Cache\Cache; + /** * List of groups. * diff --git a/system/migrations/17.php b/system/migrations/17.php index 8c7cf8a0..73e3828c 100644 --- a/system/migrations/17.php +++ b/system/migrations/17.php @@ -1,5 +1,7 @@ hasTable('myaac_menu')) { $db->query(" CREATE TABLE `myaac_menu` @@ -16,7 +18,6 @@ CREATE TABLE `myaac_menu` "); } -require_once LIBS . 'plugins.php'; Plugins::installMenus('kathrine', require TEMPLATES . 'kathrine/menus.php'); Plugins::installMenus('tibiacom', require TEMPLATES . 'tibiacom/menus.php'); diff --git a/system/migrations/20.php b/system/migrations/20.php index 45dc582d..e7b5ee0a 100644 --- a/system/migrations/20.php +++ b/system/migrations/20.php @@ -1,6 +1,6 @@ query("SELECT `id` FROM `players` WHERE (`name` = " . $db->quote("Rook Sample") . " OR `name` = " . $db->quote("Sorcerer Sample") . " OR `name` = " . $db->quote("Druid Sample") . " OR `name` = " . $db->quote("Paladin Sample") . " OR `name` = " . $db->quote("Knight Sample") . " OR `name` = " . $db->quote("Account Manager") . ") ORDER BY `id`;"); diff --git a/system/migrations/28.php b/system/migrations/28.php index fd6943cf..d06487c8 100644 --- a/system/migrations/28.php +++ b/system/migrations/28.php @@ -1,8 +1,10 @@ exec('DROP TABLE IF EXISTS `' . TABLE_PREFIX . 'hooks`;'); $cache = Cache::getInstance(); if($cache->enabled()) { $cache->delete('hooks'); -} \ No newline at end of file +} diff --git a/system/pages/account/create.php b/system/pages/account/create.php index 0f190bb6..58e72356 100644 --- a/system/pages/account/create.php +++ b/system/pages/account/create.php @@ -8,6 +8,9 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ + +use MyAAC\CreateCharacter; + defined('MYAAC') or die('Direct access not allowed!'); $title = 'Create Account'; @@ -21,7 +24,6 @@ if($logged) } if(setting('core.account_create_character_create')) { - require_once LIBS . 'CreateCharacter.php'; $createCharacter = new CreateCharacter(); } diff --git a/system/pages/account/create_character.php b/system/pages/account/create_character.php index 3216a71a..e68c8773 100644 --- a/system/pages/account/create_character.php +++ b/system/pages/account/create_character.php @@ -8,6 +8,9 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ + +use MyAAC\CreateCharacter; + defined('MYAAC') or die('Direct access not allowed!'); $title = 'Create Character'; @@ -30,7 +33,6 @@ $character_created = false; $save = isset($_POST['save']) && $_POST['save'] == 1; $errors = array(); if($save) { - require_once LIBS . 'CreateCharacter.php'; $createCharacter = new CreateCharacter(); $character_created = $createCharacter->doCreate($character_name, $character_sex, $character_vocation, $character_town, $account_logged, $errors); diff --git a/system/pages/forum.php b/system/pages/forum.php index e5b68ed0..c00b7f4b 100644 --- a/system/pages/forum.php +++ b/system/pages/forum.php @@ -8,6 +8,9 @@ * @copyright 2021 MyAAC * @link https://my-aac.org */ + +use MyAAC\Forum; + defined('MYAAC') or exit; $ret = require __DIR__ . '/forum/base.php'; diff --git a/system/pages/forum/admin.php b/system/pages/forum/admin.php index e9aa18b2..046452b2 100644 --- a/system/pages/forum/admin.php +++ b/system/pages/forum/admin.php @@ -8,6 +8,9 @@ * @copyright 2021 MyAAC * @link https://my-aac.org */ + +use MyAAC\Forum; + defined('MYAAC') or exit('Direct access not allowed!'); $canEdit = Forum::isModerator(); diff --git a/system/pages/forum/base.php b/system/pages/forum/base.php index de449692..2ede7f2e 100644 --- a/system/pages/forum/base.php +++ b/system/pages/forum/base.php @@ -11,7 +11,7 @@ defined('MYAAC') or die('Direct access not allowed!'); $title = 'Forum'; -require_once LIBS . 'forum.php'; +class_exists('MyAAC\Forum'); $forumSetting = setting('core.forum'); if(strtolower($forumSetting) != 'site') { diff --git a/system/pages/forum/edit_post.php b/system/pages/forum/edit_post.php index eec5b43f..042081de 100644 --- a/system/pages/forum/edit_post.php +++ b/system/pages/forum/edit_post.php @@ -8,6 +8,9 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ + +use MyAAC\Forum; + defined('MYAAC') or die('Direct access not allowed!'); $ret = require __DIR__ . '/base.php'; diff --git a/system/pages/forum/move_thread.php b/system/pages/forum/move_thread.php index 45d14683..a66ddfce 100644 --- a/system/pages/forum/move_thread.php +++ b/system/pages/forum/move_thread.php @@ -8,6 +8,9 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ + +use MyAAC\Forum; + defined('MYAAC') or die('Direct access not allowed!'); $ret = require __DIR__ . '/base.php'; diff --git a/system/pages/forum/new_post.php b/system/pages/forum/new_post.php index 49db8af0..9be11343 100644 --- a/system/pages/forum/new_post.php +++ b/system/pages/forum/new_post.php @@ -8,6 +8,9 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ + +use MyAAC\Forum; + defined('MYAAC') or die('Direct access not allowed!'); $ret = require __DIR__ . '/base.php'; diff --git a/system/pages/forum/new_thread.php b/system/pages/forum/new_thread.php index 3882f895..0ce1270b 100644 --- a/system/pages/forum/new_thread.php +++ b/system/pages/forum/new_thread.php @@ -8,6 +8,9 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ + +use MyAAC\Forum; + defined('MYAAC') or die('Direct access not allowed!'); $ret = require __DIR__ . '/base.php'; diff --git a/system/pages/forum/remove_post.php b/system/pages/forum/remove_post.php index c3f2d46f..73535eae 100644 --- a/system/pages/forum/remove_post.php +++ b/system/pages/forum/remove_post.php @@ -8,6 +8,9 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ + +use MyAAC\Forum; + defined('MYAAC') or die('Direct access not allowed!'); $ret = require __DIR__ . '/base.php'; diff --git a/system/pages/forum/show_board.php b/system/pages/forum/show_board.php index 4e37984c..710a0f05 100644 --- a/system/pages/forum/show_board.php +++ b/system/pages/forum/show_board.php @@ -8,6 +8,9 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ + +use MyAAC\Forum; + defined('MYAAC') or die('Direct access not allowed!'); $ret = require __DIR__ . '/base.php'; diff --git a/system/pages/forum/show_thread.php b/system/pages/forum/show_thread.php index 124928bc..51dcd096 100644 --- a/system/pages/forum/show_thread.php +++ b/system/pages/forum/show_thread.php @@ -8,6 +8,9 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ + +use MyAAC\Forum; + defined('MYAAC') or die('Direct access not allowed!'); $ret = require __DIR__ . '/base.php'; diff --git a/system/pages/gallery.php b/system/pages/gallery.php index 69ad2194..f44bd91f 100644 --- a/system/pages/gallery.php +++ b/system/pages/gallery.php @@ -8,6 +8,7 @@ * @link https://my-aac.org */ +use MyAAC\Cache\Cache; use MyAAC\Models\Gallery as ModelsGallery; defined('MYAAC') or die('Direct access not allowed!'); diff --git a/system/pages/highscores.php b/system/pages/highscores.php index e42e10dd..ff2fbd40 100644 --- a/system/pages/highscores.php +++ b/system/pages/highscores.php @@ -9,6 +9,7 @@ * @link https://my-aac.org */ +use MyAAC\Cache\Cache; use MyAAC\Models\Player; use MyAAC\Models\PlayerDeath; use MyAAC\Models\PlayerKillers; diff --git a/system/pages/news.php b/system/pages/news.php index cd39e89f..7dbd3f3c 100644 --- a/system/pages/news.php +++ b/system/pages/news.php @@ -8,10 +8,11 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ -defined('MYAAC') or die('Direct access not allowed!'); -require_once LIBS . 'forum.php'; -require_once LIBS . 'news.php'; +use MyAAC\Cache\Cache; +use MyAAC\News; + +defined('MYAAC') or die('Direct access not allowed!'); $canEdit = hasFlag(FLAG_CONTENT_NEWS) || superAdmin(); if(isset($_GET['archive'])) diff --git a/system/router.php b/system/router.php index 99a07f91..0f90071d 100644 --- a/system/router.php +++ b/system/router.php @@ -9,6 +9,7 @@ */ use MyAAC\Models\Pages; +use MyAAC\Plugins; defined('MYAAC') or die('Direct access not allowed!'); diff --git a/system/settings.php b/system/settings.php index ee779316..2d54a37e 100644 --- a/system/settings.php +++ b/system/settings.php @@ -10,6 +10,9 @@ * Additional options * - for number: min, max, step */ + +use MyAAC\Settings; + return [ 'name' => 'MyAAC', 'settings' => diff --git a/system/libs/cache_apc.php b/system/src/Cache/APC.php similarity index 76% rename from system/libs/cache_apc.php rename to system/src/Cache/APC.php index a997e6e5..b831a5d3 100644 --- a/system/libs/cache_apc.php +++ b/system/src/Cache/APC.php @@ -8,9 +8,10 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ -defined('MYAAC') or die('Direct access not allowed!'); -class Cache_APC +namespace MyAAC\Cache; + +class APC { private $prefix; private $enabled; @@ -31,22 +32,25 @@ class Cache_APC public function get($key) { $tmp = ''; - if($this->fetch($this->prefix . $key, $tmp)) { + if ($this->fetch($this->prefix . $key, $tmp)) { return $tmp; } return ''; } - public function fetch($key, &$var) { + public function fetch($key, &$var) + { return ($var = apc_fetch($this->prefix . $key)) !== false; } - public function delete($key) { + public function delete($key) + { apc_delete($this->prefix . $key); } - public function enabled() { + public function enabled() + { return $this->enabled; } -} \ No newline at end of file +} diff --git a/system/libs/cache_apcu.php b/system/src/Cache/APCu.php similarity index 77% rename from system/libs/cache_apcu.php rename to system/src/Cache/APCu.php index d4979f23..7f6500bf 100644 --- a/system/libs/cache_apcu.php +++ b/system/src/Cache/APCu.php @@ -8,9 +8,10 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ -defined('MYAAC') or die('Direct access not allowed!'); -class Cache_APCu +namespace MyAAC\Cache; + +class APCu { private $prefix; private $enabled; @@ -31,22 +32,25 @@ class Cache_APCu public function get($key) { $tmp = ''; - if($this->fetch($this->prefix . $key, $tmp)) { + if ($this->fetch($this->prefix . $key, $tmp)) { return $tmp; } return ''; } - public function fetch($key, &$var) { + public function fetch($key, &$var) + { return ($var = apcu_fetch($this->prefix . $key)) !== false; } - public function delete($key) { + public function delete($key) + { apcu_delete($this->prefix . $key); } - public function enabled() { + public function enabled() + { return $this->enabled; } } diff --git a/system/libs/cache.php b/system/src/Cache/Cache.php similarity index 64% rename from system/libs/cache.php rename to system/src/Cache/Cache.php index 55c17a17..b9981dce 100644 --- a/system/libs/cache.php +++ b/system/src/Cache/Cache.php @@ -8,7 +8,8 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ -defined('MYAAC') or die('Direct access not allowed!'); + +namespace MyAAC\Cache; /** * Class Cache @@ -41,40 +42,34 @@ class Cache */ public static function generateInstance($engine = '', $prefix = '') { - if(config('env') === 'dev') { + if (config('env') === 'dev') { self::$instance = new self(); return self::$instance; } - switch(strtolower($engine)) { + switch (strtolower($engine)) { case 'apc': - require 'cache_apc.php'; - self::$instance = new Cache_APC($prefix); + self::$instance = new APC($prefix); break; case 'apcu': - require 'cache_apcu.php'; - self::$instance = new Cache_APCu($prefix); + self::$instance = new APCu($prefix); break; case 'eaccelerator': - require 'cache_eaccelerator.php'; - self::$instance = new Cache_eAccelerator($prefix); + self::$instance = new eAccelerator($prefix); break; case 'xcache': - require 'cache_xcache.php'; - self::$instance = new Cache_XCache($prefix); + self::$instance = new XCache($prefix); break; case 'file': - require 'cache_file.php'; - self::$instance = new Cache_File($prefix, CACHE); + self::$instance = new File($prefix, CACHE); break; case 'php': - require 'cache_php.php'; - self::$instance = new Cache_PHP($prefix, CACHE); + self::$instance = new PHP($prefix, CACHE); break; case 'auto': @@ -94,13 +89,13 @@ class Cache */ public static function detect() { - if(function_exists('apc_fetch')) + if (function_exists('apc_fetch')) return 'apc'; - else if(function_exists('apcu_fetch')) + else if (function_exists('apcu_fetch')) return 'apcu'; - else if(function_exists('eaccelerator_get')) + else if (function_exists('eaccelerator_get')) return 'eaccelerator'; - else if(function_exists('xcache_get') && ini_get('xcache.var_size')) + else if (function_exists('xcache_get') && ini_get('xcache.var_size')) return 'xcache'; return 'file'; @@ -109,12 +104,15 @@ class Cache /** * @return bool */ - public function enabled() {return false;} + public function enabled() + { + return false; + } public static function remember($key, $ttl, $callback) { $cache = self::getInstance(); - if(!$cache->enabled()) { + if (!$cache->enabled()) { return $callback(); } @@ -124,7 +122,7 @@ class Cache } $value = $callback(); - $cache->set($key, serialize($value),$ttl); + $cache->set($key, serialize($value), $ttl); return $value; } } diff --git a/system/libs/cache_eaccelerator.php b/system/src/Cache/EAccelerator.php similarity index 72% rename from system/libs/cache_eaccelerator.php rename to system/src/Cache/EAccelerator.php index e5285a64..58adc6d3 100644 --- a/system/libs/cache_eaccelerator.php +++ b/system/src/Cache/EAccelerator.php @@ -8,14 +8,16 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ -defined('MYAAC') or die('Direct access not allowed!'); -class Cache_eAccelerator +namespace MyAAC\Cache; + +class EAccelerator { private $prefix; private $enabled; - public function __construct($prefix = '') { + public function __construct($prefix = '') + { $this->prefix = $prefix; $this->enabled = function_exists('eaccelerator_get'); } @@ -30,22 +32,25 @@ class Cache_eAccelerator public function get($key) { $tmp = ''; - if($this->fetch($this->prefix . $key, $tmp)) { + if ($this->fetch($this->prefix . $key, $tmp)) { return $tmp; } return ''; } - public function fetch($key, &$var) { + public function fetch($key, &$var) + { return ($var = eaccelerator_get($this->prefix . $key)) !== null; } - public function delete($key) { + public function delete($key) + { eaccelerator_rm($this->prefix . $key); } - public function enabled() { + public function enabled() + { return $this->enabled; } } diff --git a/system/libs/cache_file.php b/system/src/Cache/File.php similarity index 76% rename from system/libs/cache_file.php rename to system/src/Cache/File.php index a4c897c8..8d93d495 100644 --- a/system/libs/cache_file.php +++ b/system/src/Cache/File.php @@ -7,15 +7,17 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ -defined('MYAAC') or die('Direct access not allowed!'); -class Cache_File +namespace MyAAC\Cache; + +class File { private $prefix; private $dir; private $enabled; - public function __construct($prefix = '', $dir = '') { + public function __construct($prefix = '', $dir = '') + { $this->prefix = $prefix; $this->dir = $dir; $this->enabled = (file_exists($this->dir) && is_dir($this->dir) && is_writable($this->dir)); @@ -36,7 +38,7 @@ class Cache_File public function get($key) { $tmp = ''; - if($this->fetch($key, $tmp)) { + if ($this->fetch($key, $tmp)) { return $tmp; } @@ -46,7 +48,7 @@ class Cache_File public function fetch($key, &$var) { $file = $this->_name($key); - if(!file_exists($file) || filemtime($file) < time()) { + if (!file_exists($file) || filemtime($file) < time()) { return false; } @@ -57,16 +59,18 @@ class Cache_File public function delete($key) { $file = $this->_name($key); - if(file_exists($file)) { + if (file_exists($file)) { unlink($file); } } - public function enabled() { + public function enabled() + { return $this->enabled; } - private function _name($key) { + private function _name($key) + { return sprintf('%s%s%s', $this->dir, $this->prefix, sha1($key)); } -} \ No newline at end of file +} diff --git a/system/libs/cache_php.php b/system/src/Cache/PHP.php similarity index 79% rename from system/libs/cache_php.php rename to system/src/Cache/PHP.php index 5d35ceef..e42bd928 100644 --- a/system/libs/cache_php.php +++ b/system/src/Cache/PHP.php @@ -7,15 +7,17 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ -defined('MYAAC') or die('Direct access not allowed!'); -class Cache_PHP +namespace MyAAC\Cache; + +class PHP { private $prefix; private $dir; private $enabled; - public function __construct($prefix = '', $dir = '') { + public function __construct($prefix = '', $dir = '') + { $this->prefix = $prefix; $this->dir = $dir; $this->enabled = (file_exists($this->dir) && is_dir($this->dir) && is_writable($this->dir)); @@ -42,7 +44,7 @@ class Cache_PHP public function get($key) { $tmp = ''; - if($this->fetch($key, $tmp)) { + if ($this->fetch($key, $tmp)) { return $tmp; } @@ -52,7 +54,7 @@ class Cache_PHP public function fetch($key, &$var) { $file = $this->_name($key); - if(!file_exists($file) || filemtime($file) < time()) { + if (!file_exists($file) || filemtime($file) < time()) { return false; } @@ -64,16 +66,18 @@ class Cache_PHP public function delete($key) { $file = $this->_name($key); - if(file_exists($file)) { + if (file_exists($file)) { unlink($file); } } - public function enabled() { + public function enabled() + { return $this->enabled; } - private function _name($key) { + private function _name($key) + { return sprintf('%s%s%s', $this->dir, $this->prefix, sha1($key) . '.php'); } -} \ No newline at end of file +} diff --git a/system/libs/cache_xcache.php b/system/src/Cache/XCache.php similarity index 75% rename from system/libs/cache_xcache.php rename to system/src/Cache/XCache.php index bb1c054c..0bcf05d6 100644 --- a/system/libs/cache_xcache.php +++ b/system/src/Cache/XCache.php @@ -8,14 +8,16 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ -defined('MYAAC') or die('Direct access not allowed!'); -class Cache_XCache +namespace MyAAC\Cache; + +class XCache { private $prefix; private $enabled; - public function __construct($prefix = '') { + public function __construct($prefix = '') + { $this->prefix = $prefix; $this->enabled = function_exists('xcache_get') && ini_get('xcache.var_size'); } @@ -30,7 +32,7 @@ class Cache_XCache public function get($key) { $tmp = ''; - if($this->fetch($this->prefix . $key, $tmp)) { + if ($this->fetch($this->prefix . $key, $tmp)) { return $tmp; } @@ -40,7 +42,7 @@ class Cache_XCache public function fetch($key, &$var) { $key = $this->prefix . $key; - if(!xcache_isset($key)) { + if (!xcache_isset($key)) { return false; } @@ -48,11 +50,13 @@ class Cache_XCache return true; } - public function delete($key) { + public function delete($key) + { xcache_unset($this->prefix . $key); } - public function enabled() { + public function enabled() + { return $this->enabled; } } diff --git a/system/libs/changelog.php b/system/src/Changelog.php similarity index 98% rename from system/libs/changelog.php rename to system/src/Changelog.php index f6d2011b..c2827451 100644 --- a/system/libs/changelog.php +++ b/system/src/Changelog.php @@ -1,5 +1,8 @@ find($char_to_copy_name); if(!$char_to_copy->isLoaded()) $errors[] = 'Wrong characters configuration. Try again or contact with admin. ADMIN: Go to Admin Panel -> Settings -> Create Character and set valid characters to copy names. Character to copy: '.$char_to_copy_name.' doesn\'t exist.'; @@ -162,7 +164,7 @@ class CreateCharacter if($sex == "0") $char_to_copy->setLookType(136); - $player = new OTS_Player(); + $player = new \OTS_Player(); $player->setName($name); $player->setAccount($account); $player->setGroupId(1); @@ -194,7 +196,7 @@ class CreateCharacter $player->setManaSpent($char_to_copy->getManaSpent()); $player->setSoul($char_to_copy->getSoul()); - for($skill = POT::SKILL_FIRST; $skill <= POT::SKILL_LAST; $skill++) { + for($skill = \POT::SKILL_FIRST; $skill <= \POT::SKILL_LAST; $skill++) { $value = 10; if (setting('core.use_character_sample_skills')) { $value = $char_to_copy->getSkill($skill); @@ -231,7 +233,7 @@ class CreateCharacter $player->save(); $player->setCustomField('created', time()); - $player = new OTS_Player(); + $player = new \OTS_Player(); $player->find($name); if(!$player->isLoaded()) { @@ -240,7 +242,7 @@ class CreateCharacter } if($db->hasTable('player_skills')) { - for($skill = POT::SKILL_FIRST; $skill <= POT::SKILL_LAST; $skill++) { + for($skill = \POT::SKILL_FIRST; $skill <= \POT::SKILL_LAST; $skill++) { $value = 10; if (setting('core.use_character_sample_skills')) { $value = $char_to_copy->getSkill($skill); diff --git a/system/libs/creatures.php b/system/src/Creatures.php similarity index 93% rename from system/libs/creatures.php rename to system/src/Creatures.php index 42be67ad..0e888eab 100644 --- a/system/libs/creatures.php +++ b/system/src/Creatures.php @@ -9,14 +9,13 @@ * @link https://my-aac.org */ +namespace MyAAC; + use MyAAC\Models\Monster; -defined('MYAAC') or die('Direct access not allowed!'); - -require_once LIBS . 'items.php'; class Creatures { /** - * @var OTS_MonstersList + * @var \OTS_MonstersList */ private static $monstersList; private static $lastError = ''; @@ -24,7 +23,7 @@ class Creatures { public static function loadFromXML($show = false) { try { Monster::query()->delete(); - } catch(Exception $error) {} + } catch(\Exception $error) {} if($show) { echo '

Reload monsters.

'; @@ -32,9 +31,9 @@ class Creatures { } try { - self::$monstersList = new OTS_MonstersList(config('data_path') . 'monster/'); + self::$monstersList = new \OTS_MonstersList(config('data_path') . 'monster/'); } - catch(Exception $e) { + catch(\Exception $e) { self::$lastError = $e->getMessage(); return false; } @@ -119,7 +118,7 @@ class Creatures { $summons = $monster->getSummons(); $loot = $monster->getLoot(); foreach($loot as &$item) { - if(!Validator::number($item['id'])) { + if(!\Validator::number($item['id'])) { if(isset($items[$item['id']])) { $item['id'] = $items[$item['id']]; } @@ -161,7 +160,7 @@ class Creatures { success('Added: ' . $name . '
'); } } - catch(Exception $error) { + catch(\Exception $error) { if($show) { warning('Error while adding monster (' . $name . '): ' . $error->getMessage()); } diff --git a/system/libs/data.php b/system/src/Data.php similarity index 92% rename from system/libs/data.php rename to system/src/Data.php index af667448..6d264c46 100644 --- a/system/libs/data.php +++ b/system/src/Data.php @@ -7,7 +7,8 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ -defined('MYAAC') or die('Direct access not allowed!'); + +namespace MyAAC; class Data { diff --git a/system/libs/DataLoader.php b/system/src/DataLoader.php similarity index 93% rename from system/libs/DataLoader.php rename to system/src/DataLoader.php index 85245a6b..cfe9d502 100644 --- a/system/libs/DataLoader.php +++ b/system/src/DataLoader.php @@ -23,9 +23,8 @@ * @link https://my-aac.org */ -/** - * Class DataLoader - */ +namespace MyAAC; + class DataLoader { private static $locale; @@ -38,7 +37,6 @@ class DataLoader { self::$startTime = microtime(true); - require_once LIBS . 'items.php'; if(Items::loadFromXML()) { success(self::$locale['step_database_loaded_items'] . self::getLoadedTime()); } @@ -48,7 +46,6 @@ class DataLoader self::$startTime = microtime(true); - require LIBS . 'creatures.php'; if(Creatures::loadFromXML()) { success(self::$locale['step_database_loaded_monsters'] . self::getLoadedTime()); @@ -63,7 +60,6 @@ class DataLoader self::$startTime = microtime(true); - require_once LIBS . 'npc.php'; if(NPCs::loadFromXML()) { success(self::$locale['step_database_loaded_npcs'] . self::getLoadedTime()); } @@ -73,7 +69,6 @@ class DataLoader self::$startTime = microtime(true); - require LIBS . 'spells.php'; if(Spells::loadFromXML()) { success(self::$locale['step_database_loaded_spells'] . self::getLoadedTime()); } @@ -92,7 +87,6 @@ class DataLoader self::$startTime = microtime(true); - require LIBS . 'weapons.php'; if(Weapons::loadFromXML()) { success(self::$locale['step_database_loaded_weapons'] . self::getLoadedTime()); } diff --git a/system/src/Exceptions/SensitiveException.php b/system/src/Exceptions/SensitiveException.php new file mode 100644 index 00000000..71c8d7e7 --- /dev/null +++ b/system/src/Exceptions/SensitiveException.php @@ -0,0 +1,7 @@ + 0) { if($logged) { - $guild = new OTS_Guild(); + $guild = new \OTS_Guild(); $guild->load($section['guild']); $status = false; if($guild->isLoaded()) { @@ -322,4 +323,3 @@ class Forum return $hasAccess; } } -?> diff --git a/system/src/Hook.php b/system/src/Hook.php new file mode 100644 index 00000000..73e378fd --- /dev/null +++ b/system/src/Hook.php @@ -0,0 +1,42 @@ +_name = $name; + $this->_type = $type; + $this->_file = $file; + } + + public function execute($params) + { + global $db, $config, $template_path, $ots, $content, $twig; + + if(is_callable($this->_file)) + { + $params['db'] = $db; + $params['config'] = $config; + $params['template_path'] = $template_path; + $params['ots'] = $ots; + $params['content'] = $content; + $params['twig'] = $twig; + + $tmp = $this->_file; + $ret = $tmp($params); + } + else { + extract($params); + + $ret = include BASE . $this->_file; + } + + return !isset($ret) || $ret == 1 || $ret; + } + + public function name() {return $this->_name;} + public function type() {return $this->_type;} +} diff --git a/system/src/Hooks.php b/system/src/Hooks.php new file mode 100644 index 00000000..ee9f3b76 --- /dev/null +++ b/system/src/Hooks.php @@ -0,0 +1,44 @@ +type()][] = $hook; + } + + public function trigger($type, $params = array()) + { + $ret = true; + if(isset(self::$_hooks[$type])) + { + foreach(self::$_hooks[$type] as $name => $hook) { + /** @var $hook Hook */ + if (!$hook->execute($params)) { + $ret = false; + } + } + } + + return $ret; + } + + public function exist($type) { + return isset(self::$_hooks[$type]); + } + + public function load() + { + foreach(Plugins::getHooks() as $hook) { + $this->register($hook['name'], $hook['type'], $hook['file']); + } + + Plugins::clearWarnings(); + } +} diff --git a/system/libs/items.php b/system/src/Items.php similarity index 89% rename from system/libs/items.php rename to system/src/Items.php index 3d44f295..977aa602 100644 --- a/system/libs/items.php +++ b/system/src/Items.php @@ -8,7 +8,11 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ -defined('MYAAC') or die('Direct access not allowed!'); + +namespace MyAAC; + +use MyAAC\Cache\PHP as CachePHP; +use MyAAC\Models\Spell; class Items { @@ -23,7 +27,7 @@ class Items return false; } - $xml = new DOMDocument; + $xml = new \DOMDocument; $xml->load($file_path); $items = array(); @@ -39,8 +43,7 @@ class Items } } - require_once LIBS . 'cache_php.php'; - $cache_php = new Cache_PHP(config('cache_prefix'), CACHE . 'persistent/'); + $cache_php = new CachePHP(config('cache_prefix'), CACHE . 'persistent/'); $cache_php->set('items', $items, 5 * 365 * 24 * 60 * 60); return true; } @@ -67,8 +70,7 @@ class Items return; } - require_once LIBS . 'cache_php.php'; - $cache_php = new Cache_PHP(config('cache_prefix'), CACHE . 'persistent/'); + $cache_php = new CachePHP(config('cache_prefix'), CACHE . 'persistent/'); self::$items = $cache_php->get('items'); } @@ -110,7 +112,7 @@ class Items $s .= 'an item of type ' . $item['id']; if(isset($attr['type']) && strtolower($attr['type']) == 'rune') { - $item = Spells::where('item_id', $id)->first(); + $item = Spell::where('item_id', $id)->first(); if($item) { if($item->level > 0 && $item->maglevel > 0) { $s .= '. ' . ($count > 1 ? "They" : "It") . ' can only be used by '; diff --git a/system/src/Models/AccountVipList.php b/system/src/Models/AccountVipList.php index 676ddc05..309b343a 100644 --- a/system/src/Models/AccountVipList.php +++ b/system/src/Models/AccountVipList.php @@ -1,6 +1,7 @@ load($npc_path . $npc); if ($xml) { @@ -40,8 +43,7 @@ class NPCs return false; } - require_once LIBS . 'cache_php.php'; - $cache_php = new Cache_PHP(config('cache_prefix'), CACHE . 'persistent/'); + $cache_php = new CachePHP(config('cache_prefix'), CACHE . 'persistent/'); $cache_php->set('npcs', $npcs, 5 * 365 * 24 * 60 * 60); return true; } @@ -52,8 +54,7 @@ class NPCs return; } - require_once LIBS . 'cache_php.php'; - $cache_php = new Cache_PHP(config('cache_prefix'), CACHE . 'persistent/'); + $cache_php = new CachePHP(config('cache_prefix'), CACHE . 'persistent/'); self::$npcs = $cache_php->get('npcs'); } } diff --git a/system/libs/news.php b/system/src/News.php similarity index 98% rename from system/libs/news.php rename to system/src/News.php index b42fefc6..1a6b88d2 100644 --- a/system/libs/news.php +++ b/system/src/News.php @@ -1,5 +1,8 @@ - * @author Slawkens - * @copyright 2019 MyAAC - * @link https://my-aac.org - */ -defined('MYAAC') or die('Direct access not allowed!'); -function is_sub_dir($path = NULL, $parent_folder = BASE) { - - //Get directory path minus last folder - $dir = dirname($path); - $folder = substr($path, strlen($dir)); - - //Check the the base dir is valid - $dir = realpath($dir); - - //Only allow valid filename characters - $folder = preg_replace('/[^a-z0-9\.\-_]/i', '', $folder); - - //If this is a bad path or a bad end folder name - if( !$dir OR !$folder OR $folder === '.') { - return FALSE; - } - - //Rebuild path - $path = $dir. '/' . $folder; - - //If this path is higher than the parent folder - if( strcasecmp($path, $parent_folder) > 0 ) { - return $path; - } - - return FALSE; -} +namespace MyAAC; use Composer\Semver\Semver; +use MyAAC\Cache\Cache; use MyAAC\Models\Menu; class Plugins { @@ -123,17 +88,17 @@ class Plugins { } } } -/* - usort($routes, function ($a, $b) - { - // key 3 is priority - if ($a[3] == $b[3]) { - return 0; - } + /* + usort($routes, function ($a, $b) + { + // key 3 is priority + if ($a[3] == $b[3]) { + return 0; + } - return ($a[3] > $b[3]) ? -1 : 1; - }); -*/ + return ($a[3] > $b[3]) ? -1 : 1; + }); + */ // cleanup before passing back // priority is not needed anymore foreach ($routes as &$route) { @@ -312,11 +277,11 @@ class Plugins { { global $db; - if(!\class_exists('ZipArchive')) { - throw new RuntimeException('Please install PHP zip extension. Plugins upload disabled until then.'); + if(!\class_exists('\ZipArchive')) { + throw new \RuntimeException('Please install PHP zip extension. Plugins upload disabled until then.'); } - $zip = new ZipArchive(); + $zip = new \ZipArchive(); if($zip->open($file) !== true) { self::$error = 'There was a problem with opening zip archive.'; return false; diff --git a/system/libs/Settings.php b/system/src/Settings.php similarity index 96% rename from system/libs/Settings.php rename to system/src/Settings.php index 29b4f906..71e4fdbc 100644 --- a/system/libs/Settings.php +++ b/system/src/Settings.php @@ -1,17 +1,11 @@ - * @copyright 2020 MyAAC - * @link https://my-aac.org - */ - -class Settings implements ArrayAccess +class Settings implements \ArrayAccess { static private $instance; private $settingsFile = []; @@ -59,7 +53,7 @@ class Settings implements ArrayAccess $this->loadPlugin($pluginName); if (!isset($this->settingsFile[$pluginName])) { - throw new RuntimeException("Error on save settings: plugin $pluginName does not exist"); + throw new \RuntimeException("Error on save settings: plugin $pluginName does not exist"); } $settings = $this->settingsFile[$pluginName]; @@ -94,7 +88,7 @@ class Settings implements ArrayAccess 'key' => $key, 'value' => $value ]); - } catch (PDOException $error) { + } catch (\PDOException $error) { $this->errors[] = 'Error while saving setting (' . $pluginName . ' - ' . $key . '): ' . $error->getMessage(); } } @@ -296,7 +290,7 @@ class Settings implements ArrayAccess } else if ($setting['options'] == '$timezones') { $timezones = []; - foreach (DateTimeZone::listIdentifiers() as $value) { + foreach (\DateTimeZone::listIdentifiers() as $value) { $timezones[$value] = $value; } @@ -455,7 +449,7 @@ class Settings implements ArrayAccess // return specified plugin settings (all) if(!isset($key)) { if (!isset($this->settingsFile[$pluginKeyName]['settings'])) { - throw new RuntimeException('Unknown plugin settings: ' . $pluginKeyName); + throw new \RuntimeException('Unknown plugin settings: ' . $pluginKeyName); } return $this->settingsFile[$pluginKeyName]['settings']; } @@ -595,10 +589,10 @@ class Settings implements ArrayAccess } try { - $connectionTest = new PDO('mysql:' . implode(';', $dns), $user, $password); - $connectionTest->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $connectionTest = new \PDO('mysql:' . implode(';', $dns), $user, $password); + $connectionTest->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } - catch(PDOException $error) { + catch(\PDOException $error) { error('MySQL connection failed. Settings has been reverted.'); error($error->getMessage()); return false; diff --git a/system/libs/spells.php b/system/src/Spells.php similarity index 93% rename from system/libs/spells.php rename to system/src/Spells.php index 9a5d30d7..c187af2a 100644 --- a/system/libs/spells.php +++ b/system/src/Spells.php @@ -9,9 +9,9 @@ * @link https://my-aac.org */ -use MyAAC\Models\Spell; +namespace MyAAC; -defined('MYAAC') or die('Direct access not allowed!'); +use MyAAC\Models\Spell; class Spells { private static $spellsList = null; @@ -38,7 +38,7 @@ class Spells { try { Spell::query()->delete(); - } catch(Exception $error) {} + } catch(\Exception $error) {} if($show) { echo '

Reload spells.

'; @@ -46,9 +46,9 @@ class Spells { } try { - self::$spellsList = new OTS_SpellsList($config['data_path'].'spells/spells.xml'); + self::$spellsList = new \OTS_SpellsList($config['data_path'].'spells/spells.xml'); } - catch(Exception $e) { + catch(\Exception $e) { self::$lastError = $e->getMessage(); return false; } @@ -88,7 +88,7 @@ class Spells { success('Added: ' . $name . '
'); } } - catch(PDOException $error) { + catch(\PDOException $error) { if($show) { warning('Error while adding spell (' . $name . '): ' . $error->getMessage()); } @@ -128,7 +128,7 @@ class Spells { success('Added: ' . $name . '
'); } } - catch(PDOException $error) { + catch(\PDOException $error) { if($show) { warning('Error while adding spell (' . $name . '): ' . $error->getMessage()); } @@ -166,7 +166,7 @@ class Spells { success('Added: ' . $name . '
'); } } - catch(PDOException $error) { + catch(\PDOException $error) { if($show) { warning('Error while adding spell (' . $name . '): ' . $error->getMessage()); } diff --git a/system/libs/timer.php b/system/src/Timer.php similarity index 93% rename from system/libs/timer.php rename to system/src/Timer.php index cb14c008..6f7a8664 100644 --- a/system/libs/timer.php +++ b/system/src/Timer.php @@ -7,7 +7,8 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ -defined('MYAAC') or die('Direct access not allowed!'); + +namespace MyAAC; class Timer { diff --git a/system/libs/Towns.php b/system/src/Towns.php similarity index 97% rename from system/libs/Towns.php rename to system/src/Towns.php index edecd11d..e8a24359 100644 --- a/system/libs/Towns.php +++ b/system/src/Towns.php @@ -23,11 +23,10 @@ * @link https://my-aac.org */ +namespace MyAAC; + use MyAAC\Models\Town; -/** - * Class Towns - */ class Towns { /** @@ -109,7 +108,6 @@ class Towns if (file_exists($mapFile)) { ini_set('memory_limit', '-1'); - require LIBS . 'TownsReader.php'; $townsReader = new TownsReader($mapFile); $townsReader->load(); diff --git a/system/libs/TownsReader.php b/system/src/TownsReader.php similarity index 96% rename from system/libs/TownsReader.php rename to system/src/TownsReader.php index 71c00798..b1323c31 100644 --- a/system/libs/TownsReader.php +++ b/system/src/TownsReader.php @@ -19,8 +19,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +namespace MyAAC; + /* - This code bases on oryginal OTServ code for .otbm files - file iomapotbm.cpp rev.2141 + This code bases on original OTServ code for .otbm files - file iomapotbm.cpp rev.2141 */ class TownsReader { @@ -79,4 +81,4 @@ class TownsReader public function get() { return $this->towns; } -} \ No newline at end of file +} diff --git a/system/libs/usage_statistics.php b/system/src/UsageStatistics.php similarity index 97% rename from system/libs/usage_statistics.php rename to system/src/UsageStatistics.php index 29afc51b..1fd45b33 100644 --- a/system/libs/usage_statistics.php +++ b/system/src/UsageStatistics.php @@ -7,9 +7,12 @@ * @copyright 2019 MyAAC * @link https://my-aac.org */ -defined('MYAAC') or die('Direct access not allowed!'); -class Usage_Statistics { +namespace MyAAC; + +use MyAAC\Cache\Cache; + +class UsageStatistics { private static $report_url = 'https://my-aac.org/report_usage.php'; public static function report() { diff --git a/system/libs/validator.php b/system/src/Validator.php similarity index 98% rename from system/libs/validator.php rename to system/src/Validator.php index cc2002dd..2466a370 100644 --- a/system/libs/validator.php +++ b/system/src/Validator.php @@ -8,11 +8,11 @@ * @link https://my-aac.org */ +namespace MyAAC; + use MyAAC\Models\Monster; use MyAAC\Models\Spell; -defined('MYAAC') or die('Direct access not allowed!'); - class Validator { private static $lastError = ''; @@ -332,9 +332,8 @@ class Validator $npcCheck = setting('core.create_character_name_npc_check'); if ($npcCheck) { - require_once LIBS . 'npc.php'; - NPCS::load(); - if(NPCS::$npcs) { + NPCs::load(); + if(NPCs::$npcs) { foreach (NPCs::$npcs as $npc) { if(strpos($name_lower, $npc) !== false) { self::$lastError = 'Your name cannot contains NPC name.'; diff --git a/system/libs/visitors.php b/system/src/Visitors.php similarity index 97% rename from system/libs/visitors.php rename to system/src/Visitors.php index 7afd47df..32220c6a 100644 --- a/system/libs/visitors.php +++ b/system/src/Visitors.php @@ -8,9 +8,10 @@ * @link https://my-aac.org */ -use MyAAC\Models\Visitor; +namespace MyAAC; -defined('MYAAC') or die('Direct access not allowed!'); +use MyAAC\Cache\Cache; +use MyAAC\Models\Visitor; class Visitors { diff --git a/system/libs/weapons.php b/system/src/Weapons.php similarity index 96% rename from system/libs/weapons.php rename to system/src/Weapons.php index 83dca70b..9c819e34 100644 --- a/system/libs/weapons.php +++ b/system/src/Weapons.php @@ -9,6 +9,8 @@ * @link https://my-aac.org */ +namespace MyAAC; + use MyAAC\Models\Weapon; defined('MYAAC') or die('Direct access not allowed!'); @@ -22,7 +24,7 @@ class Weapons { try { Weapon::query()->delete(); - } catch (PDOException $error) { + } catch (\PDOException $error) { } $file_path = $config['data_path'] . 'weapons/weapons.xml'; @@ -31,7 +33,7 @@ class Weapons { return false; } - $xml = new DOMDocument; + $xml = new \DOMDocument; $xml->load($file_path); foreach ($xml->getElementsByTagName('wand') as $weapon) { diff --git a/system/hooks.php b/system/src/global.php similarity index 64% rename from system/hooks.php rename to system/src/global.php index cd23b6e7..34f13a8b 100644 --- a/system/hooks.php +++ b/system/src/global.php @@ -1,15 +1,7 @@ - * @copyright 2019 MyAAC - * @link https://my-aac.org - */ -defined('MYAAC') or die('Direct access not allowed!'); $i = 0; + define('HOOK_STARTUP', ++$i); define('HOOK_BEFORE_PAGE', ++$i); define('HOOK_AFTER_PAGE', ++$i); @@ -82,83 +74,30 @@ define('HOOK_TWIG', ++$i); const HOOK_FIRST = HOOK_STARTUP; define('HOOK_LAST', $i); -require_once LIBS . 'plugins.php'; -class Hook +function is_sub_dir($path = NULL, $parent_folder = BASE): bool|string { - private $_name, $_type, $_file; + //Get directory path minus last folder + $dir = dirname($path); + $folder = substr($path, strlen($dir)); - public function __construct($name, $type, $file) { - $this->_name = $name; - $this->_type = $type; - $this->_file = $file; + //Check the base dir is valid + $dir = realpath($dir); + + //Only allow valid filename characters + $folder = preg_replace('/[^a-z0-9\.\-_]/i', '', $folder); + + //If this is a bad path or a bad end folder name + if( !$dir OR !$folder OR $folder === '.') { + return false; } - public function execute($params) - { - global $db, $config, $template_path, $ots, $content, $twig; + //Rebuild path + $path = $dir. '/' . $folder; - if(is_callable($this->_file)) - { - $params['db'] = $db; - $params['config'] = $config; - $params['template_path'] = $template_path; - $params['ots'] = $ots; - $params['content'] = $content; - $params['twig'] = $twig; - - $tmp = $this->_file; - $ret = $tmp($params); - } - else { - extract($params); - - $ret = include BASE . $this->_file; - } - - return !isset($ret) || $ret == 1 || $ret; + //If this path is higher than the parent folder + if( strcasecmp($path, $parent_folder) > 0 ) { + return $path; } - public function name() {return $this->_name;} - public function type() {return $this->_type;} -} - -class Hooks -{ - private static $_hooks = array(); - - public function register($hook, $type = '', $file = null) { - if(!($hook instanceof Hook)) - $hook = new Hook($hook, $type, $file); - - self::$_hooks[$hook->type()][] = $hook; - } - - public function trigger($type, $params = array()) - { - $ret = true; - if(isset(self::$_hooks[$type])) - { - foreach(self::$_hooks[$type] as $name => $hook) { - /** @var $hook Hook */ - if (!$hook->execute($params)) { - $ret = false; - } - } - } - - return $ret; - } - - public function exist($type) { - return isset(self::$_hooks[$type]); - } - - public function load() - { - foreach(Plugins::getHooks() as $hook) { - $this->register($hook['name'], $hook['type'], $hook['file']); - } - - Plugins::clearWarnings(); - } + return false; } diff --git a/system/status.php b/system/status.php index d55f68e1..5ecffcec 100644 --- a/system/status.php +++ b/system/status.php @@ -8,6 +8,7 @@ * @link https://my-aac.org */ +use MyAAC\Cache\Cache; use MyAAC\Models\Config; use MyAAC\Models\PlayerOnline; diff --git a/system/template.php b/system/template.php index 0a47aa90..01eefc5c 100644 --- a/system/template.php +++ b/system/template.php @@ -8,7 +8,9 @@ * @link https://my-aac.org */ +use MyAAC\Cache\Cache; use MyAAC\Models\Menu; +use MyAAC\Plugins; defined('MYAAC') or die('Direct access not allowed!'); diff --git a/tools/validate.php b/tools/validate.php index 78140085..0eb76e0c 100644 --- a/tools/validate.php +++ b/tools/validate.php @@ -9,6 +9,7 @@ * @link https://my-aac.org */ +use MyAAC\CreateCharacter; use MyAAC\Models\Account; // we need some functions @@ -67,7 +68,6 @@ else if(isset($_GET['name'])) if(!admin() && !Validator::newCharacterName($name)) error_(Validator::getLastError()); - require_once LIBS . 'CreateCharacter.php'; $createCharacter = new CreateCharacter(); if (!$createCharacter->checkName($name, $errors)) { error_($errors['name']);