mc-cms-namelessmc/modules/Core/widgets/ServerStatusWidget.php

89 lines
3.2 KiB
PHP
Raw Permalink Normal View History

<?php
/*
2022-05-27 11:54:24 +03:00
* Made by Aberdeener
* https://github.com/NamelessMC/Nameless/
* NamelessMC version 2.2.0
*
* Licence: MIT
*
* Profile Posts Widget
*/
2021-12-07 22:14:12 -08:00
2020-12-14 14:20:28 -08:00
class ServerStatusWidget extends WidgetBase {
2021-10-30 16:44:05 -07:00
private Cache $_cache;
private Language $_language;
2020-12-14 14:20:28 -08:00
public function __construct(TemplateEngine $engine, Language $language, Cache $cache) {
$this->_module = 'Core';
$this->_name = 'Server Status';
$this->_description = 'Display your Minecraft server status.';
$this->_engine = $engine;
Reactions revamp & profile widgets (#3272) * Initial work on reactions revamp and profile widgets * Remove unneeded changes * Remove unneeded change * fix style * Render emojis in reaction list StaffCP * wip minecraft account profile widget, sticky widgets on profile page * Add server name and IP to last seen * remove * rename to reaction score to match member list * align reactions table contents center * fix * wip - nicer abstraction, widgets never query db, skin 3d viewer (undecided) * wip - make reaction modals ajax, copy new reaction bar to profile posts * wip reaction modals * wip proper sorting * wip modals * wip get reaction submission working on profile wall posts * add back helpful and creative default reactions * reduce query amount on view topic * remove debug statement * wip * wip * simplify ProfileWidget pages * Revert forum dropdown * fix css syntax * fix emoji title and alt being strange * add back order input * sort widgets by order in list * fix settings link always showing * phpdoc * add widget name to exception * insert default widget data if not found * missing end if * properly cache forum news per-group * fix * fix cache key, news permission check * fix styling * wip * respect reaction ordering * wip * wip * support custom reaction scores, update member list to properly calculate * don't hardcode words * wip * allow multiple reactions * redirect to post * code style * phpstan * wip * remove hardcoded terms + fix copy * use npm for skinview3d * create ReactionContext and refactor * fix cache * docblocks * fix spelling * pr amendments
2023-06-21 10:19:46 -07:00
$this->_language = $language;
$this->_cache = $cache;
}
2021-10-30 16:44:05 -07:00
public function initialise(): void {
// Generate HTML code for widget
$this->_cache->setCache('server_status_widget');
2021-10-29 22:00:05 -07:00
$server_array = [];
2021-04-13 14:51:39 -07:00
if ($this->_cache->isCached('server_status')) {
$server_array = $this->_cache->retrieve('server_status');
} else {
$server = DB::getInstance()->query('SELECT * FROM nl2_mc_servers WHERE is_default = 1');
if ($server->count()) {
$server = $server->first();
$server_array_request = HttpClient::get(rtrim(URL::getSelfURL(), '/') . URL::build('/queries/server/', 'id=' . $server->id), [
'verify' => false,
]);
if (!$server_array_request->hasError()) {
$server_array = $server_array_request->json(true);
foreach ($server_array as $key => $value) {
// we have to NOT escape the player list or the formatted player list. luckily these are the only arrays
if (is_array($value)) {
$server_array[$key] = $value;
} else {
$server_array[$key] = Output::getClean($value);
}
}
} else {
$server_array = [
'status_value' => 0,
'status' => $this->_language->get('general', 'offline'),
'server_offline' => $this->_language->get('general', 'server_offline'),
];
}
$server_array['name'] = Output::getClean($server->name);
$server_array['join_at'] = Output::getClean($server->ip);
$this->_cache->store('server_status', $server_array, 120);
}
2021-12-07 22:14:12 -08:00
}
if (count($server_array) >= 1) {
$this->_engine->addVariables(
2021-10-29 22:00:05 -07:00
[
2020-12-19 19:25:59 +01:00
'SERVER' => $server_array,
'ONLINE' => $this->_language->get('general', 'online'),
'OFFLINE' => $this->_language->get('general', 'offline'),
'IP' => $this->_language->get('general', 'ip'),
'VERSION' => $this->_language->get('general', 'version'),
2021-10-29 22:00:05 -07:00
]
2020-12-19 19:25:59 +01:00
);
}
$this->_engine->addVariables(
2021-10-29 22:00:05 -07:00
[
2020-12-19 19:25:59 +01:00
'SERVER_STATUS' => $this->_language->get('general', 'server_status'),
'NO_SERVERS' => $this->_language->get('general', 'no_default_server')
2021-10-29 22:00:05 -07:00
]
2020-12-19 19:25:59 +01:00
);
$this->_content = $this->_engine->fetch('widgets/server_status');
}
}