Move admin code to App\Admin class

Removed old myaac_admin_menu code
Add logged() + accountLogged() functions
This commit is contained in:
slawkens 2025-03-16 19:18:25 +01:00
parent 18ac8537c7
commit 8e86b8c015
15 changed files with 138 additions and 125 deletions

View File

@ -1,79 +1,8 @@
<?php
use MyAAC\Services\LoginService;
use MyAAC\Services\StatusService;
// few things we'll need
require '../common.php';
require_once '../common.php';
require_once SYSTEM . 'functions.php';
const MYAAC_ADMIN = true;
if(file_exists(BASE . 'install') && (!isset($config['installed']) || !$config['installed'])) {
header('Location: ' . BASE_URL . 'install/');
exit;
}
$content = '';
// validate page
$page = $_GET['p'] ?? '';
if(empty($page) || preg_match("/[^a-zA-Z0-9_\-\/.]/", $page))
$page = 'dashboard';
$page = strtolower($page);
define('PAGE', $page);
require SYSTEM . 'functions.php';
require SYSTEM . 'init.php';
require __DIR__ . '/includes/debugbar.php';
$loginService = new LoginService();
$checkLogin = $loginService->checkLogin();
$logged = $checkLogin['logged'];
$account_logged = $checkLogin['account'];
app()->setLoggedIn($logged);
app()->setAccountLogged($account_logged);
$statusService = new StatusService();
$status = $statusService->checkStatus();
require __DIR__ . '/includes/functions.php';
$twig->addGlobal('config', $config);
$twig->addGlobal('status', $status);
if (ACTION == 'logout') {
require SYSTEM . 'logout.php';
}
// if we're not logged in - show login box
if(!logged() || !admin()) {
$page = 'login';
}
// include our page
$file = __DIR__ . '/pages/' . $page . '.php';
if(!@file_exists($file)) {
if (str_contains($page, 'plugins/')) {
$file = BASE . $page;
}
else {
$page = '404';
$file = SYSTEM . 'pages/404.php';
}
}
ob_start();
if($hooks->trigger(HOOK_ADMIN_BEFORE_PAGE)) {
require $file;
}
$content .= ob_get_contents();
ob_end_clean();
// template
$template_path = 'template/';
require __DIR__ . '/' . $template_path . 'template.php';
$admin = new \MyAAC\App\Admin();
$admin->run();

View File

@ -110,7 +110,7 @@ if($action == 'edit' || $action == 'new') {
$player->load($player_id);
}
$account_players = $account_logged->getPlayersList();
$account_players = accountLogged()->getPlayersList();
$account_players->orderBy('group_id', POT::ORDER_DESC);
$twig->display('admin.changelog.form.html.twig', array(
'action' => $action,

View File

@ -7,6 +7,8 @@ use MyAAC\Models\Monster;
use MyAAC\Models\Player;
defined('MYAAC') or die('Direct access not allowed!');
global $eloquentConnection;
$count = $eloquentConnection->query()
->select([
'total_accounts' => Account::selectRaw('COUNT(id)'),

View File

@ -50,7 +50,7 @@ if(!empty($action))
if (isRequestMethod('post')) {
if ($action == 'new') {
if (isset($forum_section) && $forum_section != '-1') {
$forum_add = Forum::add_thread($p_title, $body, $forum_section, $player_id, $account_logged->getId(), $errors);
$forum_add = Forum::add_thread($p_title, $body, $forum_section, $player_id, accountLogged()->getId(), $errors);
}
if (isset($p_title) && News::add($p_title, $body, $type, $category, $player_id, isset($forum_add) && $forum_add != 0 ? $forum_add : 0, $article_text, $article_image, $errors)) {
@ -113,7 +113,7 @@ if($action == 'edit' || $action == 'new') {
$player->load($player_id);
}
$account_players = $account_logged->getPlayersList();
$account_players = accountLogged()->getPlayersList();
$account_players->orderBy('group_id', POT::ORDER_DESC);
$twig->display('admin.news.form.html.twig', array(
'action' => $action,

View File

@ -15,21 +15,18 @@ $title = 'Notepad';
csrfProtect();
/**
* @var OTS_Account $account_logged
*/
$_content = '';
$notepad = ModelsNotepad::where('account_id', $account_logged->getId())->first();
$notepad = ModelsNotepad::where('account_id', accountLogged()->getId())->first();
if (isset($_POST['content'])) {
$_content = html_entity_decode(stripslashes($_POST['content']));
if (!$notepad) {
ModelsNotepad::create([
'account_id' => $account_logged->getId(),
'account_id' => accountLogged()->getId(),
'content' => $_content
]);
}
else {
ModelsNotepad::where('account_id', $account_logged->getId())->update(['content' => $_content]);
ModelsNotepad::where('account_id', accountLogged()->getId())->update(['content' => $_content]);
}
success('Saved at ' . date('H:i'));

View File

@ -1,5 +1,7 @@
<?php
global $menus;
$menus = [
['name' => 'Dashboard', 'icon' => 'tachometer-alt', 'order' => 10, 'link' => 'dashboard'],
['name' => 'Settings', 'icon' => 'edit', 'order' => 19, 'link' =>

View File

@ -21,7 +21,7 @@
</head>
<body class="sidebar-mini ">
<?php $hooks->trigger(HOOK_ADMIN_BODY_START); ?>
<?php if (logged() && admin()) { ?>
<?php if (admin()) { ?>
<div class="wrapper">
<nav class="main-header navbar navbar-expand navbar-white navbar-light">
<ul class="navbar-nav">
@ -40,7 +40,7 @@
</nav>
<aside class="main-sidebar sidebar-dark-info elevation-4">
<a href="<?php echo ADMIN_URL; ?>" class="brand-link navbar-info">
<img src="<?php echo ADMIN_URL; ?>images/logo.png" class="brand-image img-circle elevation-3" style="opacity: .8">
<img src="<?php echo ADMIN_URL; ?>images/logo.png" class="brand-image img-circle elevation-3" style="opacity: .8" alt="MyAAC">
<span class="brand-text"><b>My</b>AAC</span>
</a>
<div class="sidebar">
@ -97,20 +97,6 @@
<?php
}
}
$query = $db->query('SELECT `name`, `page`, `flags` FROM `' . TABLE_PREFIX . 'admin_menu` ORDER BY `ordering`');
$menu_db = $query->fetchAll();
foreach ($menu_db as $item) {
if ($item['flags'] == 0 || hasFlag($item['flags'])) { ?>
<li class="nav-item">
<a class="nav-link<?php echo($page == $item['page'] ? ' active' : '') ?>" href="?p=<?php echo $item['page'] ?>">
<i class="nav-icon fas fa-link"></i>
<p><?php echo $item['name'] ?></p>
</a>
</li>
<?php
}
}
?>
</ul>
</nav>
@ -122,7 +108,7 @@
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h3 class="m-0 text-dark"><?php echo(isset($title) ? $title : ''); ?><small> - Admin Panel</small></h3>
<h3 class="m-0 text-dark"><?php echo($title ?? ''); ?><small> - Admin Panel</small></h3>
</div>
<div class="col-sm-6">
<div class="float-sm-right d-none d-sm-inline">
@ -182,12 +168,9 @@
}
?>
<?php
/**
* @var OTS_Account $account_logged
*/
if (logged() && admin()) {
if (admin()) {
$twig->display('admin-bar.html.twig', [
'username' => USE_ACCOUNT_NAME ? $account_logged->getName() : $account_logged->getId()
'username' => USE_ACCOUNT_NAME ? accountLogged()->getName() : accountLogged()->getId()
]);
}
?>

View File

@ -1,15 +1,22 @@
<?php
use MyAAC\Services\LoginService;
define('MYAAC_ADMIN', true);
require '../../common.php';
require SYSTEM . 'functions.php';
require SYSTEM . 'init.php';
require SYSTEM . 'login.php';
if(!admin())
$loginService = new LoginService();
$loginService->checkLogin();
if(!admin()) {
die('Access denied.');
}
if(!function_exists('phpinfo'))
if(!function_exists('phpinfo')) {
die('phpinfo() disabled on this web server.');
}
phpinfo();

View File

@ -24,16 +24,20 @@
*/
use MyAAC\DataLoader;
use MyAAC\Services\LoginService;
const MYAAC_ADMIN = true;
require '../../common.php';
require SYSTEM . 'functions.php';
require SYSTEM . 'init.php';
require SYSTEM . 'login.php';
if (!admin())
$loginService = new LoginService();
$loginService->checkLogin();
if (!admin()) {
die('Access denied.');
}
ini_set('max_execution_time', 300);
ob_implicit_flush();

View File

@ -1,6 +1,6 @@
<?php
use MyAAC\Hooks;
use MyAAC\Services\LoginService;
use MyAAC\Settings;
const MYAAC_ADMIN = true;
@ -8,7 +8,9 @@ const MYAAC_ADMIN = true;
require '../../common.php';
require SYSTEM . 'functions.php';
require SYSTEM . 'init.php';
require SYSTEM . 'login.php';
$loginService = new LoginService();
$loginService->checkLogin();
if(!admin()) {
http_response_code(500);
@ -27,7 +29,7 @@ if (!isset($_POST['settings'])) {
die('Please enter settings.');
}
$settings = Settings::getInstance();
$settings = app()->get('settings');
$success = $settings->save($_REQUEST['plugin'], $_POST['settings']);

View File

@ -1,14 +1,20 @@
<?php
use MyAAC\Services\LoginService;
define('MYAAC_ADMIN', true);
require '../../common.php';
require SYSTEM . 'init.php';
require SYSTEM . 'functions.php';
require SYSTEM . 'status.php';
require SYSTEM . 'login.php';
if(!admin())
$loginService = new LoginService();
$loginService->checkLogin();
if(!admin()) {
die('Access denied.');
}
if(!$status['online'])
die('Offline');

View File

@ -1,13 +1,19 @@
<?php
use MyAAC\Services\LoginService;
define('MYAAC_ADMIN', true);
require '../../common.php';
require SYSTEM . 'functions.php';
require SYSTEM . 'init.php';
require SYSTEM . 'login.php';
if(!admin())
$loginService = new LoginService();
$loginService->checkLogin();
if(!admin()) {
die('Access denied.');
}
// Don't attempt to process the upload on an OPTIONS request
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

View File

@ -2,7 +2,75 @@
namespace MyAAC\App;
use MyAAC\Services\LoginService;
use MyAAC\Services\StatusService;
class Admin
{
public function run(): void
{
App::preInstallCheck();
$content = '';
// validate page
$page = $_GET['p'] ?? '';
if(empty($page) || preg_match("/[^a-zA-Z0-9_\-\/.]/", $page)) {
$page = 'dashboard';
}
$page = strtolower($page);
define('PAGE', $page);
require_once SYSTEM . 'init.php';
require_once ADMIN . 'includes/debugbar.php';
$loginService = new LoginService();
$loginService->checkLogin();
$statusService = new StatusService();
$status = $statusService->checkStatus();
require ADMIN . '/includes/functions.php';
global $config;
$twig = app()->get('twig');
$twig->addGlobal('config', $config);
$twig->addGlobal('status', $status);
if (ACTION == 'logout') {
require SYSTEM . 'logout.php';
}
// if we're not logged in - show login box
if(!logged() || !admin()) {
$page = 'login';
}
// include our page
$file = ADMIN . '/pages/' . $page . '.php';
if(!@file_exists($file)) {
if (str_contains($page, 'plugins/')) {
$file = BASE . $page;
}
else {
$page = '404';
$file = SYSTEM . 'pages/404.php';
}
}
$hooks = app()->get('hooks');
ob_start();
if($hooks->trigger(HOOK_ADMIN_BEFORE_PAGE)) {
require $file;
}
$content .= ob_get_contents();
ob_end_clean();
// template
$template_path = 'template/';
require ADMIN . '/' . $template_path . 'template.php';
}
}

View File

@ -21,11 +21,7 @@ class App
public function run(): void
{
$configInstalled = config('installed');
if((!isset($configInstalled) || !$configInstalled) && file_exists(BASE . 'install')) {
header('Location: ' . BASE_URL . 'install/');
exit();
}
self::preInstallCheck();
$template_place_holders = [];
@ -34,9 +30,8 @@ class App
$loginService = new LoginService();
$checkLogin = $loginService->checkLogin();
$this->accountLogged = $checkLogin['account'];
$this->isLoggedIn = $checkLogin['logged'];
// TODO: Remove those globals, once plugins migrated
global $logged, $account_logged, $logged_flags;
$logged = $this->isLoggedIn;
$account_logged = $this->accountLogged;
@ -168,4 +163,13 @@ class App
return $this->instances[$what];
}
public static function preInstallCheck(): void
{
$configInstalled = config('installed');
if((!isset($configInstalled) || !$configInstalled) && file_exists(BASE . 'install')) {
header('Location: ' . BASE_URL . 'install/');
exit();
}
}
}

View File

@ -39,6 +39,9 @@ class LoginService
}
setSession('last_uri', $_SERVER['REQUEST_URI']);
app()->setLoggedIn($logged);
app()->setAccountLogged($account_logged);
return [
'logged' => $logged,
'account' => $account_logged,