From 9aa4e308c1ff8c672d3b409c6d16775c52d418ef Mon Sep 17 00:00:00 2001 From: slawkens Date: Fri, 3 Nov 2017 09:43:47 +0100 Subject: [PATCH] * reverted removing base href in html head * added anonymous usage statistics reporting * (fix) don't show templates that doesn't exist in Menus option in Admin Panel * (fix) menu ordering by category * (fix) showing changelog with urls in Admin Panel * (internal) moved uninstall logic to Plugins class --- config.php | 1 + index.php | 71 +++++++++++++++++------ install/steps/database.php | 7 ++- system/functions.php | 23 ++++++++ system/libs/plugins.php | 46 +++++++++++++++ system/libs/usage_statistics.php | 4 +- system/locale/en/install.php | 2 + system/pages/admin/changelog.php | 4 +- system/pages/admin/menus.php | 12 +++- system/pages/admin/plugins.php | 57 +----------------- system/pages/bugtracker.php | 18 +++--- system/pages/creatures.php | 2 +- system/template.php | 4 +- system/templates/install.config.html.twig | 12 ++++ templates/kathrine/template.php | 5 +- tools/signature/index.php | 4 +- 16 files changed, 175 insertions(+), 97 deletions(-) diff --git a/config.php b/config.php index 40cb210e..088e5e83 100644 --- a/config.php +++ b/config.php @@ -228,6 +228,7 @@ $config = array( 'status_port' => '', // other + 'anonymous_usage_statistics' => false, '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' => 5, // how many columns to display in experience table page. * experiencetable_rows, 5 = 500 (will show up to 500 level) diff --git a/index.php b/index.php index a206587a..0f48faa5 100644 --- a/index.php +++ b/index.php @@ -30,20 +30,7 @@ // ini_set('display_startup_errors', 1); // error_reporting(E_ALL); -if(preg_match("/^(.*)\.(gif|jpg|jpeg|tiff|bmp|css|js|less|map|html|php|zip|rar|gz)$/i", $_SERVER['REQUEST_URI'])) { - header("HTTP/1.0 404 Not Found"); - exit; -} - require_once('common.php'); -require_once(BASE . 'config.local.php'); - -if(file_exists(BASE . 'install') && (!isset($config['installed']) || !$config['installed'])) -{ - header('Location: ' . BASE_URL . 'install/'); - die('Setup detected that install/ directory exists. Please visit this url to start MyAAC Installation.
Delete install/ directory if you already installed MyAAC.
Remember to REFRESH this page when you\'re done!'); -} - require_once(SYSTEM . 'functions.php'); $uri = $_SERVER['REQUEST_URI']; @@ -57,12 +44,7 @@ else $uri = str_replace(array('index.php/', '?'), '', $uri); define('URI', $uri); -$found = false; -if(empty($uri) || isset($_REQUEST['template'])) { - $_REQUEST['p'] = 'news'; - $found = true; -} -else if(preg_match("/^[A-Za-z0-9-_%\'+]+\.png$/i", $uri)) { +if(preg_match("/^[A-Za-z0-9-_%\'+]+\.png$/i", $uri)) { $tmp = explode('.', $uri); $_REQUEST['name'] = urldecode($tmp[0]); @@ -70,6 +52,23 @@ else if(preg_match("/^[A-Za-z0-9-_%\'+]+\.png$/i", $uri)) { include(TOOLS . 'signature/index.php'); exit(); } +else if(preg_match("/^(.*)\.(gif|jpg|png|jpeg|tiff|bmp|css|js|less|map|html|php|zip|rar|gz)$/i", $_SERVER['REQUEST_URI'])) { + header("HTTP/1.0 404 Not Found"); + exit; +} + +require_once(BASE . 'config.local.php'); +if(file_exists(BASE . 'install') && (!isset($config['installed']) || !$config['installed'])) +{ + header('Location: ' . BASE_URL . 'install/'); + die('Setup detected that install/ directory exists. Please visit this url to start MyAAC Installation.
Delete install/ directory if you already installed MyAAC.
Remember to REFRESH this page when you\'re done!'); +} + +$found = false; +if(empty($uri) || isset($_REQUEST['template'])) { + $_REQUEST['p'] = 'news'; + $found = true; +} else if(!preg_match('/[^A-z0-9_\-]/', $uri) && file_exists(SYSTEM . 'pages/' . $uri . '.php')) { $_REQUEST['p'] = $uri; $found = true; @@ -193,6 +192,40 @@ $hooks = new Hooks(); $hooks->load(); $hooks->trigger(HOOK_STARTUP); +// anonymous usage statistics +// sent only when user agrees +if(isset($config['anonymous_usage_statistics']) && $config['anonymous_usage_statistics']) { + $report_time = 30 * 24 * 60 * 60; // report one time per 30 days + $should_report = true; + + $value = ''; + if($cache->enabled() && $cache->fetch('last_usage_report', $value)) { + $should_report = time() > (int)$value + $report_time; + } + else { + $value = ''; + if(fetchDatabaseConfig('last_usage_report', $value)) { + $should_report = time() > (int)$value + $report_time; + if($cache->enabled()) { + $cache->set('last_usage_report', $value); + } + } + else { + registerDatabaseConfig('last_usage_report', time()); + } + } + + if($should_report) { + require_once(LIBS . 'usage_statistics.php'); + Usage_Statistics::report(); + + updateDatabaseConfig('last_usage_report', time()); + if($cache->enabled()) { + $cache->set('last_usage_report', time()); + } + } +} + if($config['views_counter']) require_once(SYSTEM . 'counter.php'); diff --git a/install/steps/database.php b/install/steps/database.php index 85216c6e..62ef1e70 100644 --- a/install/steps/database.php +++ b/install/steps/database.php @@ -23,7 +23,11 @@ if(!$error) { $value .= "/"; } - if($key != 'var_account' && $key != 'var_account_id' && $key != 'var_password') { + if($key == 'var_usage') { + $content .= '$config[\'anonymous_usage_statistics\'] = ' . ((int)$value == 1 ? 'true' : 'false') . ';'; + $content .= PHP_EOL; + } + else if($key != 'var_account' && $key != 'var_account_id' && $key != 'var_password') { $content .= '$config[\'' . str_replace('var_', '', $key) . '\'] = \'' . $value . '\';'; $content .= PHP_EOL; } @@ -238,6 +242,7 @@ if(!$error) { $content .= '// place for your configuration directives, so you can later easily update myaac'; $content .= PHP_EOL; $content .= "?>"; + $file = fopen(BASE . 'config.local.php', 'a+'); if($file) { if(!$error) { diff --git a/system/functions.php b/system/functions.php index 9c6b018c..58e10322 100644 --- a/system/functions.php +++ b/system/functions.php @@ -460,6 +460,7 @@ function template_header($is_admin = false) '; if(!$is_admin) $ret .= ' + ' . $title_full . ''; $ret .= ' @@ -983,6 +984,28 @@ function getTopPlayers($limit = 5) { return $players; } +function deleteDirectory($dir) { + if(!file_exists($dir)) { + return true; + } + + if(!is_dir($dir)) { + return unlink($dir); + } + + foreach(scandir($dir) as $item) { + if($item == '.' || $item == '..') { + continue; + } + + if(!deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) { + return false; + } + } + + return rmdir($dir); +} + // validator functions require_once(LIBS . 'validator.php'); require_once(SYSTEM . 'compat.php'); diff --git a/system/libs/plugins.php b/system/libs/plugins.php index 0c59991a..5e7d62c1 100644 --- a/system/libs/plugins.php +++ b/system/libs/plugins.php @@ -138,6 +138,52 @@ class Plugins { return false; } + public static function uninstall($plugin_name) { + global $cache; + + $filename = BASE . 'plugins/' . $plugin_name . '.json'; + if(!file_exists($filename)) { + self::$error = 'Plugin ' . $plugin_name . ' does not exist.'; + return false; + } + else { + $string = file_get_contents($filename); + $plugin_info = json_decode($string, true); + if($plugin_info == false) { + self::$error = 'Cannot load plugin info ' . $plugin_name . '.json'; + return false; + } + else { + if(!isset($plugin_info['uninstall'])) { + self::$error = "Plugin doesn't have uninstall options defined. Skipping..."; + return false; + } + else { + $success = true; + foreach($plugin_info['uninstall'] as $file) { + $file = BASE . $file; + if(!deleteDirectory($file)) { + $success = false; + } + } + + if($success) { + if($cache->enabled()) { + $cache->delete('templates'); + } + + return true; + } + else { + self::$error = error_get_last(); + } + } + } + } + + return false; + } + public static function getWarnings() { return self::$warnings; } diff --git a/system/libs/usage_statistics.php b/system/libs/usage_statistics.php index 44fec9a0..28be9843 100644 --- a/system/libs/usage_statistics.php +++ b/system/libs/usage_statistics.php @@ -11,7 +11,7 @@ defined('MYAAC') or die('Direct access not allowed!'); class Usage_Statistics { - private static $report_url = 'http://my-aac.org/report_usage.php'; + private static $report_url = 'https://my-aac.org/report_usage.php'; public static function report() { $data = json_encode(self::getStats()); @@ -20,7 +20,6 @@ class Usage_Statistics { 'http' => array( 'header' => 'Content-type: application/json' . "\r\n" . 'Content-Length: ' . strlen($data) . "\r\n", - 'method' => 'POST', 'content' => $data ) ); @@ -28,7 +27,6 @@ class Usage_Statistics { $context = stream_context_create($options); $result = file_get_contents(self::$report_url, false, $context); - //var_dump($result); return $result !== false; } diff --git a/system/locale/en/install.php b/system/locale/en/install.php index 620eaad0..e5c93343 100644 --- a/system/locale/en/install.php +++ b/system/locale/en/install.php @@ -51,6 +51,8 @@ $locale['step_config_mail_address_desc'] = 'Address which will be used for outgo $locale['step_config_mail_address_error'] = 'Server E-Mail is not correct.'; $locale['step_config_client'] = 'Client version'; $locale['step_config_client_desc'] = 'Used for download page and some templates'; +$locale['step_config_usage'] = 'Usage Statistics'; +$locale['step_config_usage_desc'] = 'Allow MyAAC to report anonymous usage statistics? The data is sent only once per 30 days and is fully confidential.'; // database $locale['step_database'] = 'Import schema'; diff --git a/system/pages/admin/changelog.php b/system/pages/admin/changelog.php index 3298bd84..d8ddc6fb 100644 --- a/system/pages/admin/changelog.php +++ b/system/pages/admin/changelog.php @@ -17,10 +17,12 @@ if(!file_exists(BASE . 'CHANGELOG')) { } $changelog = file_get_contents(BASE . 'CHANGELOG'); -$changelog = nl2br(htmlspecialchars($changelog)); +$changelog = htmlspecialchars($changelog); // replace URLs with elements $changelog = preg_replace('/\s(\w+:\/\/)(\S+)/', ' \\1\\2', $changelog); +$changelog = nl2br($changelog); + echo '
' . $changelog . '
'; ?> diff --git a/system/pages/admin/menus.php b/system/pages/admin/menus.php index a77fd3de..4c27e678 100644 --- a/system/pages/admin/menus.php +++ b/system/pages/admin/menus.php @@ -29,16 +29,16 @@ if(isset($_REQUEST['template'])) { } $db->query('DELETE FROM `' . TABLE_PREFIX . 'menu` WHERE `template` = ' . $db->quote($template)); - foreach($post_menu as $id => $menus) { + foreach($post_menu as $category => $menus) { foreach($menus as $i => $menu) { if(empty($menu)) // don't save empty menu item continue; try { - $db->insert(TABLE_PREFIX . 'menu', array('template' => $template, 'name' => $menu, 'link' => $post_menu_link[$id][$i], 'category' => $id, 'ordering' => $i)); + $db->insert(TABLE_PREFIX . 'menu', array('template' => $template, 'name' => $menu, 'link' => $post_menu_link[$category][$i], 'category' => $category, 'ordering' => $i)); } catch(PDOException $error) { - warning('Error while adding menu item (' . $name . '): ' . $error->getMessage()); + warning('Error while adding menu item (' . $menu . '): ' . $error->getMessage()); } } } @@ -97,6 +97,12 @@ if(isset($_REQUEST['template'])) { } else { $templates = $db->query('SELECT `template` FROM `' . TABLE_PREFIX . 'menu` GROUP BY `template`;')->fetchAll(); + foreach($templates as $key => $value) { + $file = TEMPLATES . $value['template'] . '/config.php'; + if(!file_exists($file)) { + unset($templates[$key]); + } + } echo $twig->render('admin.menus.form.html.twig', array( 'templates' => $templates diff --git a/system/pages/admin/plugins.php b/system/pages/admin/plugins.php index fa51e108..f6bcd1ca 100644 --- a/system/pages/admin/plugins.php +++ b/system/pages/admin/plugins.php @@ -14,67 +14,16 @@ $title = 'Plugin manager'; require(SYSTEM . 'hooks.php'); require(LIBS . 'plugins.php'); -function deleteDirectory($dir) { - if(!file_exists($dir)) { - return true; - } - - if(!is_dir($dir)) { - return unlink($dir); - } - - foreach(scandir($dir) as $item) { - if($item == '.' || $item == '..') { - continue; - } - - if(!deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) { - return false; - } - } - - return rmdir($dir); -} - echo $twig->render('admin.plugins.form.html.twig'); if(isset($_REQUEST['uninstall'])){ $uninstall = $_REQUEST['uninstall']; - $filename = BASE . 'plugins/' . $uninstall . '.json'; - if(!file_exists($filename)) { - error('Plugin ' . $uninstall . ' does not exist.'); + if(Plugins::uninstall($uninstall)) { + success('Successfully uninstalled plugin ' . $uninstall); } else { - $string = file_get_contents($filename); - $plugin_info = json_decode($string, true); - if($plugin_info == false) { - error('Cannot load plugin info ' . $uninstall . '.json'); - } - else { - if(!isset($plugin_info['uninstall'])) { - error("Plugin doesn't have uninstall options defined. Skipping..."); - } - else { - $success = true; - foreach($plugin_info['uninstall'] as $file) { - $file = BASE . $file; - if(!deleteDirectory($file)) { - $success = false; - } - } - - if($success) { - if($cache->enabled()) { - $cache->delete('templates'); - } - success('Successfully uninstalled plugin ' . $uninstall); - } - else { - error('Error while uninstalling plugin ' . $uninstall . ': ' . error_get_last()); - } - } - } + error('Error while uninstalling plugin ' . $plugin_name . ': ' . Plugins::getError()); } } else if(isset($_FILES["plugin"]["name"])) diff --git a/system/pages/bugtracker.php b/system/pages/bugtracker.php index 6d0acb20..fe0c6524 100644 --- a/system/pages/bugtracker.php +++ b/system/pages/bugtracker.php @@ -83,7 +83,7 @@ $showed = $post = $reply = false; echo ''; } if($bug[2]['status'] != 3) - echo '
[REPLY]'; + echo '
[REPLY]'; } else { @@ -112,7 +112,7 @@ $showed = $post = $reply = false; $type = 2; $INSERT = $db->query('INSERT INTO `' . TABLE_PREFIX . 'bugtracker` (`account`,`id`,`text`,`reply`,`type`, `who`) VALUES ('.$db->quote($_REQUEST['acc']).','.$db->quote($_REQUEST['id']).','.$db->quote($_POST['text']).','.$db->quote($reply).','.$db->quote($type).','.$db->quote(1).')'); $UPDATE = $db->query('UPDATE `' . TABLE_PREFIX . 'bugtracker` SET `status` = '.$_POST['status'].' where `account` = '.$_REQUEST['acc'].' and `id` = '.$_REQUEST['id'].''); - header('Location: index.php?subtopic=bugtracker&control=true&id='.$_REQUEST['id'].'&acc='.$_REQUEST['acc'].''); + header('Location: ?subtopic=bugtracker&control=true&id='.$_REQUEST['id'].'&acc='.$_REQUEST['acc'].''); } } echo '
Description
Status[OPEN]
Status[CLOSED]

'; @@ -138,7 +138,7 @@ $showed = $post = $reply = false; elseif($report['status'] == 1) $value = "[NEW ANSWER]"; - echo ''.$tags[$report['tag']].' '.$report['subject'].''.$value.''; + echo ''.$tags[$report['tag']].' '.$report['subject'].''.$value.''; $showed=true; $i++; @@ -202,7 +202,7 @@ $showed = $post = $reply = false; echo ''; } if($bug[2]['status'] != 3) - echo '
[REPLY]'; + echo '
[REPLY]'; } else { @@ -231,7 +231,7 @@ $showed = $post = $reply = false; $type = 2; $INSERT = $db->query('INSERT INTO `myaac_bugtracker` (`account`,`id`,`text`,`reply`,`type`) VALUES ('.$db->quote($acc).','.$db->quote($id).','.$db->quote($_POST['text']).','.$db->quote($reply).','.$db->quote($type).')'); $UPDATE = $db->query('UPDATE `myaac_bugtracker` SET `status` = 1 where `account` = '.$acc.' and `id` = '.$id.''); - header('Location: index.php?subtopic=bugtracker&id='.$id.''); + header('Location: ?subtopic=bugtracker&id='.$id.''); } } echo '
Description

'; @@ -275,7 +275,7 @@ $showed = $post = $reply = false; $bgcolor = $light; } - echo ''.$tags[$report['tag']].' '.$report['subject'].''.$value.''; + echo ''.$tags[$report['tag']].' '.$report['subject'].''.$value.''; $showed=true; } @@ -286,7 +286,7 @@ $showed = $post = $reply = false; } echo ''; - echo '
[ADD REPORT]'; + echo '
[ADD REPORT]'; } elseif(isset($_REQUEST['add']) && $_REQUEST['add'] == TRUE) { @@ -320,7 +320,7 @@ $showed = $post = $reply = false; $type = 1; $status = 1; $INSERT = $db->query('INSERT INTO `' . TABLE_PREFIX . 'bugtracker` (`account`,`id`,`text`,`type`,`subject`, `reply`,`status`,`tag`) VALUES ('.$db->quote($acc).','.$db->quote($id_next).','.$db->quote($_POST['text']).','.$db->quote($type).','.$db->quote($_POST['subject']).', 0,'.$db->quote($status).','.$db->quote($_POST['tags']).')'); - header('Location: index.php?subtopic=bugtracker&id='.$id_next.''); + header('Location: ?subtopic=bugtracker&id='.$id_next.''); } } @@ -338,6 +338,6 @@ $showed = $post = $reply = false; if(admin() and empty($_REQUEST['control'])) { - echo '

[ADMIN PANEL]'; + echo '

[ADMIN PANEL]'; } ?> diff --git a/system/pages/creatures.php b/system/pages/creatures.php index 846f2c39..e6a9769e 100644 --- a/system/pages/creatures.php +++ b/system/pages/creatures.php @@ -38,7 +38,7 @@ if(isset($_POST['reload_monsters']) && $canEdit) if($canEdit) { ?> -
+
diff --git a/system/template.php b/system/template.php index a68545f1..d61c0d4d 100644 --- a/system/template.php +++ b/system/template.php @@ -43,7 +43,7 @@ if(!file_exists($template_path . '/index.php') && !file_exists($template_path . '/layout.php')) { $template_name = 'kathrine'; - $template_path = 'templates/' . $template_name; + $template_path = TEMPLATES . $template_name; } $file = $template_path . '/config.ini'; @@ -106,7 +106,7 @@ function get_template_menus() { global $db, $template_name; $menus = array(); - $query = $db->query('SELECT `name`, `link`, `category` FROM `' . TABLE_PREFIX . 'menu` WHERE `template` = ' . $db->quote($template_name) . ' ORDER BY `ordering` ASC'); + $query = $db->query('SELECT `name`, `link`, `category` FROM `' . TABLE_PREFIX . 'menu` WHERE `template` = ' . $db->quote($template_name) . ' ORDER BY `category`, `ordering` ASC'); foreach($query->fetchAll() as $menu) { $menus[$menu['category']][] = array('name' => $menu['name'], 'link' => $menu['link']); } diff --git a/system/templates/install.config.html.twig b/system/templates/install.config.html.twig index 3ba53a83..5a15246c 100644 --- a/system/templates/install.config.html.twig +++ b/system/templates/install.config.html.twig @@ -31,6 +31,18 @@ {{ locale.step_config_client_desc }} + + + +
+ + + + {{ locale.step_config_usage_desc }} + + {{ buttons|raw }} diff --git a/templates/kathrine/template.php b/templates/kathrine/template.php index 0e59f62b..f29218fd 100644 --- a/templates/kathrine/template.php +++ b/templates/kathrine/template.php @@ -75,11 +75,12 @@ defined('MYAAC') or die('Direct access not allowed!');
$menu) { - echo '
'; if(!isset($menus[$category])) { - return; + continue; } + echo '
'; + $size = count($menus[$category]); $i = 0; diff --git a/tools/signature/index.php b/tools/signature/index.php index 70a999bf..a42b41db 100644 --- a/tools/signature/index.php +++ b/tools/signature/index.php @@ -26,7 +26,7 @@ $file = trim(strtolower($config['signature_type'])) . '.php'; if(!file_exists($file)) - die('ERROR: Wrong signature type in config.'); + die('ERROR: Wrong signature_type in config.'); putenv('GDFONTPATH=' . SIGNATURES_FONTS); @@ -52,7 +52,7 @@ } $cached = SIGNATURES_CACHE.$player->getId() . '.png'; - if(file_exists($cached) and (time() < (filemtime($cached) + (60 * $config['signature_cache_time'])))) + if(file_exists($cached) && (time() < (filemtime($cached) + (60 * $config['signature_cache_time'])))) { header( 'Content-type: image/png' ); readfile( SIGNATURES_CACHE.$player->getId().'.png' );