update.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\admin;
  3. use PclZip;
  4. class update extends Base
  5. {
  6. public function initialize()
  7. {
  8. parent::initialize();
  9. $this->Administrator(5);
  10. $this->header();
  11. ?><style>
  12. .content{
  13. width: 90px;
  14. margin: 5px auto;
  15. text-align: left;
  16. }
  17. </style>
  18. <script>
  19. var filesize = 0;
  20. function setsize(fsize) {
  21. filesize = fsize;
  22. }
  23. function setdown(dlen) {
  24. if (filesize > 0) {
  25. var percent = Math.round(dlen * 100 / filesize);
  26. document.getElementById("progressbar").style.width = (percent + "%");
  27. if (percent > 0) {
  28. document.getElementById("progressbar").innerHTML = percent + "%";
  29. document.getElementById("progressText").innerHTML = "";
  30. } else {
  31. document.getElementById("progressText").innerHTML = percent + "%";
  32. }
  33. if (percent > 99) {
  34. document.getElementById("progressbar").innerHTML = "请稍等...";
  35. setTimeout("location.href='?c=update&a=replace_file';", 3e3);
  36. }
  37. }
  38. }
  39. </script><div class="container">
  40. <script>parent.document.title = '<?php echo IN_NAME;?>管理中心 - 程序升级';if(parent.$('#admincpnav')) parent.$('#admincpnav').innerHTML='程序升级';</script>
  41. <a class="layui-btn layui-btn-primary layui-btn-small" style="line-height:1.6em;margin-top:3px;float:left" href="?c=update&a=index" title="刷新">
  42. <i class="layui-icon" style="line-height:38px">ဂ</i></a>
  43. <div class="itemtitle"><ul class="tab1" style="margin-right:10px"></ul><ul class="stepstat">
  44. <?php
  45. if ($this->action == "down_file") {
  46. ?><li class="current"><?php
  47. } else {
  48. ?><li><?php
  49. }
  50. ?>1.下载文件</li>
  51. <?php
  52. if ($this->action == "replace_file") {
  53. ?><li class="current"><?php
  54. } else {
  55. ?><li><?php
  56. }
  57. ?>2.更新文件</li>
  58. <?php
  59. if ($this->action == "change_data") {
  60. ?><li class="current"><?php
  61. } else {
  62. ?><li><?php
  63. }
  64. ?>3.更新数据</li>
  65. </ul><ul class="tab1"></ul></div>
  66. <table class="tb tb2">
  67. <tr><th class="partition">技巧提示</th></tr>
  68. <tr><td class="tipsblock"><ul>
  69. <li>升级前请先关闭站点并备份数据。如遇升级失败,请检查相关函数是否开启及文件目录是否具有写入权限;</li>
  70. <li>更新包较大时可能会出现下载缓慢。如遇无法升级,请前往官网下载补丁并手动覆盖更新!</li>
  71. </ul></td></tr>
  72. </table>
  73. <h3>提示</h3><?php
  74. }
  75. public function __destruct()
  76. {
  77. ?></div><?php
  78. $this->footer();
  79. }
  80. function index($_var_0 = [])
  81. {
  82. if (IN_UPDATE_API) {
  83. ?><div class="infobox"><p class="margintop"><input type="button" class="btn" value="检测更新" onclick="location.href='?c=update&a=checkup';"></p></div><?php
  84. } else {
  85. ?><div class="infobox"><h4 class="infotitle2">未配置线上更新接口</h4></div><?php
  86. }
  87. }
  88. function checkup()
  89. {
  90. $_var_1 = get_headers(IN_UPDATE_API);
  91. if ($_var_1[0] == "HTTP/1.1 200 OK") {
  92. creatdir("data/tmp");
  93. $_var_2 = @file_get_contents(IN_UPDATE_API);
  94. fwrite(fopen(IN_TMP_PATH . "update.json", "w+"), $_var_2);
  95. $_var_2 = json_decode($_var_2, true);
  96. $_var_3 = trim($_var_2["data"]["grade"]);
  97. $_var_4 = trim($_var_2["data"]["version"]);
  98. $_var_5 = intval(trim($_var_2["data"]["build"]));
  99. $_var_6 = detect_encoding(rawurldecode(trim($_var_2["data"]["log"])));
  100. $_var_7 = detect_encoding(rawurldecode(trim($_var_2["data"]["shuoming"])));
  101. if ($_var_3) {
  102. if ($_var_5 > IN_BUILD) {
  103. ?><div class="infobox">
  104. <div style="color:#C00">
  105. <h4 class="marginbot normal">发现可用更新 [<?php echo $_var_4;?>] [<?php echo $_var_5;?>]</h4>
  106. <h5><strong>最近一次更新日志:</strong>[<?php echo $_var_6;?>]</h5>
  107. <br>
  108. <h5><strong>本次更新:</strong><font size="3"><?php echo $_var_7;?></font></h5>
  109. </div>
  110. <p class="margintop"><input type="button" class="btn" value="开始下载更新" onclick="location.href='?c=update&a=down_file';"> &nbsp; <input type="button" class="btn" value="取消" onclick="history.go(-1);"></p>
  111. </div><?php
  112. } else {
  113. ?><div class="infobox"><h4 class="infotitle2">已经是最新版本了</h4></div><?php
  114. }
  115. } else {
  116. ?><div class="infobox"><h4 class="infotitle3">您已更新,请勿重复更新</h4></div><?php
  117. }
  118. } else {
  119. ?><div class="infobox"><p class="margintop"><img src="static/admincp/image/loading.gif" /></p></div><?php
  120. }
  121. }
  122. function down_file()
  123. {
  124. ?><div class="infobox"><table class="tb tb2" style="border:1px solid #09C"><tr><td><div id="progressbar" style="float:left;width:1px;text-align:center;color:#FFFFFF;background-color:#09C"></div><div id="progressText" style="float:left">0%</div></td></tr></table></div><?php
  125. ob_start();
  126. @set_time_limit(0);
  127. $_var_7 = @file_get_contents(IN_TMP_PATH . "update.json");
  128. $_var_7 = json_decode($_var_7, true);
  129. $_var_8 = pack("H*", trim($_var_7["data"]["zip"]));
  130. $_var_8 = trim($_var_7["data"]["zip"]);
  131. $_var_9 = fopen($_var_8, "rb");
  132. if ($_var_9) {
  133. $_var_10 = get_headers($_var_8, 1);
  134. if (array_key_exists("Content-Length", $_var_10)) {
  135. $_var_11 = $_var_10["Content-Length"];
  136. } else {
  137. $_var_11 = strlen(@file_get_contents($_var_8));
  138. }
  139. ?><script>setsize(<?php echo $_var_11;?>);</script><?php
  140. $_var_12 = fopen(IN_TMP_PATH . "patch.zip", "wb");
  141. $_var_13 = 0;
  142. if ($_var_12) {
  143. while (!feof($_var_9)) {
  144. $_var_14 = fread($_var_9, 8192);
  145. $_var_13 += strlen($_var_14);
  146. fwrite($_var_12, $_var_14, 8192);
  147. ?><script>setdown(<?php echo $_var_13;?>);</script><?php
  148. ob_flush();
  149. flush();
  150. }
  151. }
  152. if ($_var_9) {
  153. fclose($_var_9);
  154. }
  155. if ($_var_12) {
  156. fclose($_var_12);
  157. }
  158. }
  159. }
  160. function replace_file()
  161. {
  162. $_var_15 = IN_TMP_PATH . "patch.zip";
  163. if (is_file($_var_15)) {
  164. $_var_16 = new PclZip($_var_15);
  165. if (($_var_17 = $_var_16->listContent()) == 0) {
  166. exit("<div class=\"infobox\"><h4 class=\"marginbot normal\" style=\"color:#C00\">" . $_var_16->errorInfo(true) . "</h4><p class=\"margintop\"><input type=\"button\" class=\"btn\" value=\"重新升级\" onclick=\"history.back(1);\"></p></div></div></body></html>");
  167. }
  168. $_var_16->extract(PCLZIP_OPT_PATH, IN_ROOT, PCLZIP_OPT_REPLACE_NEWER);
  169. ?><div class="infobox"><h4 class="infotitle1">即将开始更新数据,请稍候......</h4><img src="static/admincp/image/loader.gif" class="marginbot" /></div><script>setTimeout("location.href='?c=update&a=change_data';", 1e3);</script><?php
  170. }
  171. }
  172. function change_data()
  173. {
  174. @unlink(IN_TMP_PATH . "patch.zip");
  175. $json = @file_get_contents(IN_TMP_PATH . "update.json");
  176. $json = json_decode($json, true);
  177. $version = trim($json["data"]["version"]);
  178. $build = intval(trim($json["data"]["build"]));
  179. db("config")->where("name", "IN_VERSION")->update(["value" => $version]);
  180. db("config")->where("name", "IN_BUILD")->update(["value" => $build]);
  181. db("update")->strict(false)->insert(array_merge($json["data"], ["create_time" => time()]));
  182. @unlink(IN_TMP_PATH . "update.json");
  183. include IN_TMP_PATH . "update.php";
  184. @unlink(IN_TMP_PATH . "update.php");
  185. ?><div class="infobox"><h4 class="infotitle2">恭喜!顺利升级完成!</h4><p class="margintop"><input type="button" class="btn" value="完成" onclick="parent.location.href='?c=index';"></p></div><?php
  186. }
  187. }