From b0bb258d074cda823c09db60a307a02ab8a10ae0 Mon Sep 17 00:00:00 2001 From: KasuganoSoras Date: Sat, 15 Sep 2018 05:31:23 +0800 Subject: [PATCH] =?UTF-8?q?7.3.1709=20=E5=8F=91=E5=B8=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加更新检测功能,现在可以自动下载最新版本的 PHPMC 了。 修正部分逻辑判断问题。 修改了部分 AJAX 请求超时时间 增加操作过渡动画效果 --- content/PHPMC7/assets/js/console.js | 43 +++++++------- content/PHPMC7/daemon-list.html | 2 + content/PHPMC7/server-list.html | 2 + content/PHPMC7/setting.html | 48 ++++++++++++---- include/core/PHPMC/Event.php | 6 ++ include/core/PHPMC/Main.php | 7 ++- include/core/PHPMC/Update.php | 87 +++++++++++++++++++++++++++++ include/loader.php | 4 ++ index.php | 2 +- 9 files changed, 170 insertions(+), 31 deletions(-) create mode 100644 include/core/PHPMC/Update.php diff --git a/content/PHPMC7/assets/js/console.js b/content/PHPMC7/assets/js/console.js index 59a61ab..57dad88 100644 --- a/content/PHPMC7/assets/js/console.js +++ b/content/PHPMC7/assets/js/console.js @@ -1,3 +1,6 @@ +// 此处设置 AJAX 延迟,可根据自己的情况进行调整 +// 单位毫秒,例如 1000 就代表 1 秒 +var ajaxtimeout = 1000; var oldlog; var ConnectURL; var server; @@ -11,7 +14,7 @@ function ajaxload() { try { $(document).ready(function(){ 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("连接超时"); window.parent.frames.showmsg("与 Daemon 服务器的连接已断开。"); clearInterval(Interval); @@ -20,18 +23,18 @@ function ajaxload() { $("#ping").html(end + " 毫秒"); if(oldlog != htmlobj.responseText) { $("#debug").html("" - + htmlobj.responseText.replace(//g, ">").replace(/\n/g,"
") - .replace(/INFO\]/g, "信息]").replace(/WARN\]/g, "警告]") - .replace(/ERROR\]/g, "错误]").replace(/\[Server/g, "[服务器").replace(/thread\//g, "主线程/") - .replace(/Done \(/g, "启动完成,耗时 (") - .replace(/s\)\! For help\, type \"help\" or \"\?\"/g, " 秒)!需要帮助,请输入 “help” 或 “?”") - .replace(/Unknown command\. Type \"\/help\" for help\./g, "未知命令,请输入 “help” 查看帮助。") - .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./, + + htmlobj.responseText.replace("<", "<").replace(">", ">").replace("\n","
") + .replace("INFO]", "信息]").replace("WARN]", "警告]") + .replace("ERROR]", "错误]").replace("[Server", "[服务器").replace("thread/", "主线程/") + .replace("Done (", "启动完成,耗时 (").replace("s)! For help, type \"help\" or \"?\"", " 秒)!需要帮助,请输入 “help” 或 “?”") + .replace("Unknown command. Type \"/help\" for help.", "未知命令,请输入 “help” 查看帮助。") + .replace("Usage:", "使用方法:").replace("Stopping the server", "正在关闭服务器") + .replace("You need to agree to the EULA in order to run the server. Go to eula.txt for more info.", "你需要接受 EULA 协议才能开启服务器,编辑服务端的 eula.txt ,将 eula=false 改为 eula=true 并保存即可。") - .replace(/Stopping server/, "正在终止服务器进程").replace(/Loading properties/, "正在加载配置文件") - .replace(/Failed to load/, "无法加载").replace(/Starting minecraft server version/, "正在启动 Minecraft 服务器,版本:") - .replace(/Default game type:/, "默认游戏模式:") + "
"); + .replace("Stopping server", "正在终止服务器进程").replace("Loading properties", "正在加载配置文件") + .replace("Failed to load", "无法加载").replace("Starting minecraft server version", "正在启动 Minecraft 服务器,版本:") + .replace("Default game type:", "默认游戏模式:").replace("Container not found", "提示:服务器未在运行状态") + .replace("Token Error", "错误:授权验证失败,请检查 Daemon 设置。") + ""); if(autoflush.checked == true) { debug.scrollTop = debug.scrollHeight; } @@ -61,7 +64,7 @@ window.onkeydown = function(event){ }; 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); }}); } @@ -73,31 +76,32 @@ window.onload = function() { }; 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); }}); }; 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); }}); }; 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); }}); }; function selectServer(id, element) { + window.parent.frames.progressshow("请稍后,正在加载..."); clearInterval(Interval); $(".server-hover").attr("style", ""); element.style.border = "1px solid rgba(255,255,255,0.3)"; var htmlobj = $.ajax({ url:"?action=getserver&id=" + id, async:true, - timeout:5000, + timeout:10000, error: function() { window.parent.frames.showmsg(htmlobj.responseText); }, @@ -111,14 +115,15 @@ function selectServer(id, element) { ftppass.innerHTML = obj.ftppass; oldlog = ""; ConnectURL = obj.host + "?action=getlogs&token=" + obj.token + "&name=" + obj.uuid; - Interval = setInterval("ajaxload()", 1000); + window.parent.frames.progressunshow(); + Interval = setInterval("ajaxload()", ajaxtimeout); return; } }); }; 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 fallback = rpt.split("\/"); $("#online").html(fallback[0]); diff --git a/content/PHPMC7/daemon-list.html b/content/PHPMC7/daemon-list.html index 6893afe..3d7a992 100644 --- a/content/PHPMC7/daemon-list.html +++ b/content/PHPMC7/daemon-list.html @@ -99,6 +99,7 @@ diff --git a/include/core/PHPMC/Event.php b/include/core/PHPMC/Event.php index dff2a1c..8d77d57 100644 --- a/include/core/PHPMC/Event.php +++ b/include/core/PHPMC/Event.php @@ -436,4 +436,10 @@ class Event { echo "系统设置更改成功!"; exit; } + + public function updateEvent() { + if(PHPMC::Update()->checkUpdate()) { + PHPMC::Update()->updateExecute(); + } + } } \ No newline at end of file diff --git a/include/core/PHPMC/Main.php b/include/core/PHPMC/Main.php index e1ee74d..348f058 100644 --- a/include/core/PHPMC/Main.php +++ b/include/core/PHPMC/Main.php @@ -1,5 +1,5 @@ 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; + } +} \ No newline at end of file diff --git a/include/loader.php b/include/loader.php index 57bbd67..5f52b7b 100644 --- a/include/loader.php +++ b/include/loader.php @@ -164,6 +164,10 @@ class Loader { PHPMC::Permission()->checkSession("action:" . $_GET['action']); PHPMC::Event()->deleteUserEvent($_GET); break; + case 'update': + PHPMC::Permission()->checkSession("action:" . $_GET['action']); + PHPMC::Event()->updateEvent(); + break; default: echo $this::loadPage("404.html", ROOT . "/content/" . $Option->getOption("Theme") . "/error/"); } diff --git a/index.php b/index.php index a26002c..bbffd84 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,5 @@