1
0
mirror of https://github.com/ZeroDream-CN/PHPMC7 synced 2024-11-27 23:02:52 +08:00

7.3.1709 发布

增加更新检测功能,现在可以自动下载最新版本的 PHPMC 了。
修正部分逻辑判断问题。
修改了部分 AJAX 请求超时时间
增加操作过渡动画效果
This commit is contained in:
Akkariin Meiko 2018-09-15 05:31:23 +08:00
parent 9aac12cfc8
commit b0bb258d07
9 changed files with 170 additions and 31 deletions

View File

@ -1,3 +1,6 @@
// 此处设置 AJAX 延迟,可根据自己的情况进行调整
// 单位毫秒,例如 1000 就代表 1 秒
var ajaxtimeout = 1000;
var oldlog; var oldlog;
var ConnectURL; var ConnectURL;
var server; var server;
@ -11,7 +14,7 @@ function ajaxload() {
try { try {
$(document).ready(function(){ $(document).ready(function(){
var start = new Date(); var start = new Date();
var htmlobj = $.ajax({url:ConnectURL, async:true, timeout:5000, error: function(){ var htmlobj = $.ajax({url:ConnectURL, async:true, timeout:10000, error: function(){
$("#ping").html("连接超时"); $("#ping").html("连接超时");
window.parent.frames.showmsg("与 Daemon 服务器的连接已断开。"); window.parent.frames.showmsg("与 Daemon 服务器的连接已断开。");
clearInterval(Interval); clearInterval(Interval);
@ -20,18 +23,18 @@ function ajaxload() {
$("#ping").html(end + " 毫秒"); $("#ping").html(end + " 毫秒");
if(oldlog != htmlobj.responseText) { if(oldlog != htmlobj.responseText) {
$("#debug").html("<code style='color: #FFF;background-color: none;padding: 0px;'>" $("#debug").html("<code style='color: #FFF;background-color: none;padding: 0px;'>"
+ htmlobj.responseText.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g,"<br />") + htmlobj.responseText.replace("<", "&lt;").replace(">", "&gt;").replace("\n","<br />")
.replace(/INFO\]/g, "<span style='color: #00B100'>信息</span>]").replace(/WARN\]/g, "<span style='color: #FF8700'>警告</span>]") .replace("INFO]", "<span style='color: #00B100'>信息</span>]").replace("WARN]", "<span style='color: #FF8700'>警告</span>]")
.replace(/ERROR\]/g, "<span style='color: #FF0000'>错误</span>]").replace(/\[Server/g, "[服务器").replace(/thread\//g, "主线程/") .replace("ERROR]", "<span style='color: #FF0000'>错误</span>]").replace("[Server", "[服务器").replace("thread/", "主线程/")
.replace(/Done \(/g, "<span style='color: #00B100'>启动完成,耗时 (") .replace("Done (", "启动完成,耗时 (").replace("s)! For help, type \"help\" or \"?\"", " 秒)!需要帮助,请输入 “help” 或 “?”")
.replace(/s\)\! For help\, type \"help\" or \"\?\"/g, " 秒)!需要帮助,请输入 “help” 或 “?”</span>") .replace("Unknown command. Type \"/help\" for help.", "未知命令,请输入 “help” 查看帮助。")
.replace(/Unknown command\. Type \"\/help\" for help\./g, "未知命令,请输入 “help” 查看帮助。") .replace("Usage:", "使用方法:").replace("Stopping the server", "正在关闭服务器")
.replace(/Usage\:/g, "使用方法:").replace(/Stopping the server/g, "正在关闭服务器") .replace("You need to agree to the EULA in order to run the server. Go to eula.txt for more info.",
.replace(/You need to agree to the EULA in order to run the server. Go to eula.txt for more info./,
"<span style='color: #FF8700'>你需要接受 EULA 协议才能开启服务器,编辑服务端的 eula.txt ,将 eula=false 改为 eula=true 并保存即可。</span>") "<span style='color: #FF8700'>你需要接受 EULA 协议才能开启服务器,编辑服务端的 eula.txt ,将 eula=false 改为 eula=true 并保存即可。</span>")
.replace(/Stopping server/, "正在终止服务器进程").replace(/Loading properties/, "正在加载配置文件") .replace("Stopping server", "正在终止服务器进程").replace("Loading properties", "正在加载配置文件")
.replace(/Failed to load/, "无法加载").replace(/Starting minecraft server version/, "正在启动 Minecraft 服务器,版本:") .replace("Failed to load", "无法加载").replace("Starting minecraft server version", "正在启动 Minecraft 服务器,版本:")
.replace(/Default game type:/, "默认游戏模式:") + "</code>"); .replace("Default game type:", "默认游戏模式:").replace("Container not found", "提示:服务器未在运行状态")
.replace("Token Error", "错误:授权验证失败,请检查 Daemon 设置。") + "</code>");
if(autoflush.checked == true) { if(autoflush.checked == true) {
debug.scrollTop = debug.scrollHeight; debug.scrollTop = debug.scrollHeight;
} }
@ -61,7 +64,7 @@ window.onkeydown = function(event){
}; };
function sendCommand(cmd) { function sendCommand(cmd) {
var htmlobj = $.ajax({url:"?action=sendcommand&id=" + server + "&cmd=" + encodeURIComponent(cmd), async:true, timeout:5000, error: function(){ var htmlobj = $.ajax({url:"?action=sendcommand&id=" + server + "&cmd=" + encodeURIComponent(cmd), async:true, timeout:10000, error: function(){
window.parent.frames.showmsg(htmlobj.responseText); window.parent.frames.showmsg(htmlobj.responseText);
}}); }});
} }
@ -73,31 +76,32 @@ window.onload = function() {
}; };
function startServer() { function startServer() {
var htmlobj = $.ajax({url:"?action=start&id=" + server, async:true, timeout:5000, error: function(){ var htmlobj = $.ajax({url:"?action=start&id=" + server, async:true, timeout:10000, error: function(){
window.parent.frames.showmsg(htmlobj.responseText); window.parent.frames.showmsg(htmlobj.responseText);
}}); }});
}; };
function stopServer() { function stopServer() {
var htmlobj = $.ajax({url:"?action=stop&id=" + server, async:true, timeout:5000, error: function(){ var htmlobj = $.ajax({url:"?action=stop&id=" + server, async:true, timeout:10000, error: function(){
window.parent.frames.showmsg(htmlobj.responseText); window.parent.frames.showmsg(htmlobj.responseText);
}}); }});
}; };
function restartServer() { function restartServer() {
var htmlobj = $.ajax({url:"?action=restart&id=" + server, async:true, timeout:5000, error: function(){ var htmlobj = $.ajax({url:"?action=restart&id=" + server, async:true, timeout:10000, error: function(){
window.parent.frames.showmsg(htmlobj.responseText); window.parent.frames.showmsg(htmlobj.responseText);
}}); }});
}; };
function selectServer(id, element) { function selectServer(id, element) {
window.parent.frames.progressshow("请稍后,正在加载...");
clearInterval(Interval); clearInterval(Interval);
$(".server-hover").attr("style", ""); $(".server-hover").attr("style", "");
element.style.border = "1px solid rgba(255,255,255,0.3)"; element.style.border = "1px solid rgba(255,255,255,0.3)";
var htmlobj = $.ajax({ var htmlobj = $.ajax({
url:"?action=getserver&id=" + id, url:"?action=getserver&id=" + id,
async:true, async:true,
timeout:5000, timeout:10000,
error: function() { error: function() {
window.parent.frames.showmsg(htmlobj.responseText); window.parent.frames.showmsg(htmlobj.responseText);
}, },
@ -111,14 +115,15 @@ function selectServer(id, element) {
ftppass.innerHTML = obj.ftppass; ftppass.innerHTML = obj.ftppass;
oldlog = ""; oldlog = "";
ConnectURL = obj.host + "?action=getlogs&token=" + obj.token + "&name=" + obj.uuid; ConnectURL = obj.host + "?action=getlogs&token=" + obj.token + "&name=" + obj.uuid;
Interval = setInterval("ajaxload()", 1000); window.parent.frames.progressunshow();
Interval = setInterval("ajaxload()", ajaxtimeout);
return; return;
} }
}); });
}; };
function serverStatus() { function serverStatus() {
var htmlobjs = $.ajax({url:"?action=status&id=" + server, async:true, timeout:5000, success: function(){ var htmlobjs = $.ajax({url:"?action=status&id=" + server, async:true, timeout:10000, success: function(){
var rpt = htmlobjs.responseText; var rpt = htmlobjs.responseText;
var fallback = rpt.split("\/"); var fallback = rpt.split("\/");
$("#online").html(fallback[0]); $("#online").html(fallback[0]);

View File

@ -99,6 +99,7 @@
<script type="text/javascript"> <script type="text/javascript">
var selected; var selected;
function selectDaemon(id, element) { function selectDaemon(id, element) {
window.parent.frames.progressshow("请稍后,正在加载...");
$(".server-hover").attr("style", ""); $(".server-hover").attr("style", "");
element.style.border = "1px solid rgba(255,255,255,0.3)"; element.style.border = "1px solid rgba(255,255,255,0.3)";
var htmlobj = $.ajax({ var htmlobj = $.ajax({
@ -119,6 +120,7 @@
$("#Type_" + obj.type).attr("selected", "selected"); $("#Type_" + obj.type).attr("selected", "selected");
$("#changedaemon").fadeIn(); $("#changedaemon").fadeIn();
selected = id; selected = id;
window.parent.frames.progressunshow();
return; return;
} }
}); });

View File

@ -113,6 +113,7 @@
<script type="text/javascript"> <script type="text/javascript">
var selected; var selected;
function selectServer(id, element) { function selectServer(id, element) {
window.parent.frames.progressshow("请稍后,正在加载...");
$(".server-hover").attr("style", ""); $(".server-hover").attr("style", "");
element.style.border = "1px solid rgba(255,255,255,0.3)"; element.style.border = "1px solid rgba(255,255,255,0.3)";
var htmlobj = $.ajax({ var htmlobj = $.ajax({
@ -136,6 +137,7 @@
$("#User_" + obj.owner).attr("selected", "selected"); $("#User_" + obj.owner).attr("selected", "selected");
$("#changeserver").fadeIn(); $("#changeserver").fadeIn();
selected = id; selected = id;
window.parent.frames.progressunshow();
return; return;
} }
}); });

View File

@ -51,32 +51,60 @@
</div> </div>
</div> </div>
</div> </div>
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>检查更新</h5>
</div>
<div class="ibox-content addfrp" id="update">
<p>请稍后,正在检查软件更新...</p>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
var selected; var version = "{{ PHPMC_VERSION; }}";
function saveConfig() { var newversion;
function checkUpdate() {
var htmlobj = $.ajax({ var htmlobj = $.ajax({
url: "?action=saveconfig", url: "https://www.phpmc.cn/update.php?version={{ PHPMC_VERSION; }}",
async:true, async: true,
timeout:5000, timeout:5000,
data: { error: function(e) {
SiteName: $("#SiteName").val(), window.parent.frames.showmsg("检查更新错误:" + htmlobj.responseText);
Description: $("#Description").val(),
Theme: $("#Theme").val()
}, },
error: function() { success: function() {
console.log(htmlobj.responseText);
var data = JSON.parse(htmlobj.responseText);
newversion = data.version;
if(version == newversion) {
$("#update").html("<p>本地版本:" + version + "</p><p>最新版本:" + newversion + "</p><p>您的 PHPMC 已经是最新版本,无需更新。</p>");
return;
}
$("#update").html("<p>本地版本:" + version + "</p><p>最新版本:" + newversion + "</p><p>更新内容:<br>" + data.feature + "</p><p><button class='btn btn-primary' onclick='update()'>立即更新</button></p>");
return;
}
});
}
function update() {
window.parent.frames.showmsg("请稍后,正在执行系统更新...");
var htmlobj = $.ajax({
url: "?action=update",
async:true,
timeout:100000,
error: function(e) {
window.parent.frames.showmsg(htmlobj.responseText); window.parent.frames.showmsg(htmlobj.responseText);
}, },
success: function() { success: function() {
console.log(htmlobj.responseText); console.log(htmlobj.responseText);
window.parent.frames.showmsg(htmlobj.responseText); window.parent.frames.showmsg(htmlobj.responseText);
location = '?page=setting'
return; return;
} }
}); });
} }
window.onload = function() {
checkUpdate();
}
</script> </script>
</body> </body>

View File

@ -436,4 +436,10 @@ class Event {
echo "系统设置更改成功!"; echo "系统设置更改成功!";
exit; exit;
} }
public function updateEvent() {
if(PHPMC::Update()->checkUpdate()) {
PHPMC::Update()->updateExecute();
}
}
} }

View File

@ -1,5 +1,5 @@
<?php <?php
define("PHPMC_VERSION", "7.2.1926-Beta"); // Don't Change This! define("PHPMC_VERSION", "7.3.1709"); // Don't Change This!
include(ROOT . "/include/core/PHPMC/Event.php"); include(ROOT . "/include/core/PHPMC/Event.php");
include(ROOT . "/include/core/PHPMC/User.php"); include(ROOT . "/include/core/PHPMC/User.php");
include(ROOT . "/include/core/PHPMC/Utils.php"); include(ROOT . "/include/core/PHPMC/Utils.php");
@ -11,6 +11,7 @@ include(ROOT . "/include/core/PHPMC/System.php");
include(ROOT . "/include/core/PHPMC/Http.php"); include(ROOT . "/include/core/PHPMC/Http.php");
include(ROOT . "/include/core/PHPMC/Option.php"); include(ROOT . "/include/core/PHPMC/Option.php");
include(ROOT . "/include/core/PHPMC/Permission.php"); include(ROOT . "/include/core/PHPMC/Permission.php");
include(ROOT . "/include/core/PHPMC/Update.php");
class PHPMC { class PHPMC {
public static function Event() { public static function Event() {
return new Event(); return new Event();
@ -44,6 +45,10 @@ class PHPMC {
return new Permission(); return new Permission();
} }
public static function Update() {
return new Update();
}
public static function Error() { public static function Error() {
return new WebError(); return new WebError();
} }

View File

@ -0,0 +1,87 @@
<?php
class Update {
public function checkUpdate() {
$data = json_decode(PHPMC::Http()->Request("https://www.phpmc.cn/update.php?version=" . PHPMC_VERSION), true);
if(!$data) {
return false;
} else {
if($data['version'] == PHPMC_VERSION) {
return false;
} else {
return true;
}
}
}
public function getUpdateInfo() {
$data = json_decode(PHPMC::Http()->Request("https://www.phpmc.cn/update.php?version=" . PHPMC_VERSION), true);
if(!$data) {
return false;
} else {
if($data['version'] == PHPMC_VERSION) {
return false;
} else {
return $data;
}
}
}
public function updateExecute() {
$data = $this->getUpdateInfo();
if(!$data) {
PHPMC::Error()->Println("无法更新,请检查网络是否正常。");
} elseif(!$this->checkPermission("./")) {
PHPMC::Error()->Println("网站目录不可写,请修改权限或手动更新。");
} elseif(!class_exists("ZipArchive")) {
PHPMC::Error()->Println("未检测到 ZipArchive 组件,请先修改 php.ini 启用 php_zip 扩展。");
} else {
$file = @PHPMC::Http()->Request($data['download']);
if(strlen($file) == 0) {
PHPMC::Error()->Println("下载的文件长度为 0请检查网络是否正常。");
} elseif(file_put_contents('update-temp.zip', $file) === false) {
PHPMC::Error()->Println("写入文件时发生错误,请检查目录是否有读写权限。");
} elseif(md5_file('update-temp.zip') !== $data['filemd5']) {
@unlink('update-temp.zip');
PHPMC::Error()->Println("文件 MD5 验证失败,请尝试重新更新。");
} else {
if($this->unzipUpdateFiles('update-temp.zip', './')) {
@unlink('update-temp.zip');
PHPMC::Error()->Println("PHPMC 更新成功,请刷新网页。");
} else {
@unlink('update-temp.zip');
PHPMC::Error()->Println("解压文件时发生错误,无法打开文件或解压失败。");
}
}
}
}
public function checkPermission($file) {
if(is_dir($file)){
$dir = $file;
if($fp = @fopen("{$dir}/.writetest", 'w')) {
@fclose($fp);
@unlink("{$dir}/.writetest");
return true;
} else {
return false;
}
} else {
if($fp = @fopen($file, 'a+')) {
@fclose($fp);
return true;
} else {
return false;
}
}
}
public function unzipUpdateFiles($fileName, $unzipPath) {
$zip = new ZipArchive();
$open = $zip->open($fileName);
if($open === true) {
return $zip->extractTo($unzipPath);
}
return false;
}
}

View File

@ -164,6 +164,10 @@ class Loader {
PHPMC::Permission()->checkSession("action:" . $_GET['action']); PHPMC::Permission()->checkSession("action:" . $_GET['action']);
PHPMC::Event()->deleteUserEvent($_GET); PHPMC::Event()->deleteUserEvent($_GET);
break; break;
case 'update':
PHPMC::Permission()->checkSession("action:" . $_GET['action']);
PHPMC::Event()->updateEvent();
break;
default: default:
echo $this::loadPage("404.html", ROOT . "/content/" . $Option->getOption("Theme") . "/error/"); echo $this::loadPage("404.html", ROOT . "/content/" . $Option->getOption("Theme") . "/error/");
} }

View File

@ -1,5 +1,5 @@
<?php <?php
set_time_limit(10); set_time_limit(60);
error_reporting(0); error_reporting(0);
$pathinfo = pathinfo($_SERVER['PHP_SELF']); $pathinfo = pathinfo($_SERVER['PHP_SELF']);
$path = str_replace("/" . $pathinfo['basename'], "", $_SERVER['PHP_SELF']); $path = str_replace("/" . $pathinfo['basename'], "", $_SERVER['PHP_SELF']);