SystemUpgradeclient.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\admin\controller\AuthController;
  4. use crmeb\services\JsonService as Json;
  5. use crmeb\services\UpgradeService as uService;
  6. use think\facade\Db;
  7. /**
  8. * 在线升级控制器
  9. * Class SystemUpgradeclient
  10. * @package app\admin\controller\system
  11. *
  12. */
  13. class SystemUpgradeclient extends AuthController
  14. {
  15. protected $serverweb = array('version' => '1.0', 'version_code' => 0);//本站点信息
  16. public function initialize()
  17. {
  18. parent::initialize();
  19. //屏蔽所有错误避免操作文件夹发生错误提示
  20. ini_set('display_errors', 0);
  21. error_reporting(0);
  22. self::snyweninfo();//更新站点信息
  23. $this->assign(['auth' => self::isauth(), 'app' => uService::isWritable(app()->getRootPath()), 'extend' => uService::isWritable(EXTEND_PATH), 'public' => uService::isWritable(app()->getRootPath() . 'public')]);
  24. }
  25. //同步更新站点信息
  26. public function snyweninfo()
  27. {
  28. $this->serverweb['ip'] = $this->request->ip();
  29. $this->serverweb['host'] = $this->request->host();
  30. $this->serverweb['https'] = !empty($this->request->domain()) ? $this->request->domain() : sys_config('site_url');
  31. $this->serverweb['webname'] = sys_config('site_name');
  32. $local = uService::getVersion();
  33. if ($local['code'] == 200 && isset($local['msg']['version']) && isset($local['msg']['version_code'])) {
  34. $this->serverweb['version'] = uService::replace($local['msg']['version']);
  35. $this->serverweb['version_code'] = (int)uService::replace($local['msg']['version_code']);
  36. }
  37. uService::snyweninfo($this->serverweb);
  38. }
  39. //是否授权
  40. public function isauth()
  41. {
  42. return uService::isauth();
  43. }
  44. public function index()
  45. {
  46. $server = uService::start();
  47. $version = $this->serverweb['version'];
  48. $version_code = $this->serverweb['version_code'];
  49. $this->assign(compact('server', 'version', 'version_code'));
  50. return $this->fetch();
  51. }
  52. public function get_list()
  53. {
  54. $list = uService::request_post(uService::$isList, ['page' => input('post.page/d'), 'limit' => input('post.limit/d')]);
  55. if (is_array($list) && isset($list['code']) && isset($list['data']) && $list['code'] == 200) {
  56. $list = $list['data'];
  57. } else {
  58. $list = [];
  59. }
  60. Json::successful('ok', ['list' => $list, 'page' => input('post.page/d') + 1]);
  61. }
  62. //删除备份文件
  63. public function setcopydel()
  64. {
  65. $post = input('post.');
  66. if (!isset($post['id'])) Json::fail('删除备份文件失败,缺少参数ID');
  67. if (!isset($post['ids'])) Json::fail('删除备份文件失败,缺少参数IDS');
  68. $fileservice = new uService;
  69. if (is_array($post['ids'])) {
  70. foreach ($post['ids'] as $file) {
  71. $fileservice->del_dir(app()->getRootPath() . 'public' . DS . 'copyfile' . $file);
  72. }
  73. }
  74. if ($post['id']) {
  75. $copyFile = app()->getRootPath() . 'public' . DS . 'copyfile' . $post['id'];
  76. $fileservice->del_dir($copyFile);
  77. }
  78. Json::successful('删除成功');
  79. }
  80. public function get_new_version_conte()
  81. {
  82. $post = $this->request->post();
  83. if (!isset($post['id'])) Json::fail('缺少参数ID');
  84. $versionInfo = uService::request_post(uService::$NewVersionCount, ['id' => $post['id']]);
  85. if (isset($versionInfo['code']) && isset($versionInfo['data']['count']) && $versionInfo['code'] == 200) {
  86. return Json::successful(['count' => $versionInfo['data']['count']]);
  87. } else {
  88. return Json::fail('服务器异常');
  89. }
  90. }
  91. //一键升级
  92. public function auto_upgrad()
  93. {
  94. $prefix = config('database.prefix');
  95. $fileservice = new uService;
  96. $post = $this->request->post();
  97. if (!isset($post['id'])) Json::fail('缺少参数ID');
  98. $versionInfo = $fileservice->request_post(uService::$isNowVersion, ['id' => $post['id']]);
  99. if ($versionInfo === null) Json::fail('服务器异常,请稍后再试');
  100. if (isset($versionInfo['code']) && $versionInfo['code'] == 400) Json::fail(isset($versionInfo['msg']) ? $versionInfo['msg'] : '您暂时没有权限升级,请联系管理员!');
  101. if (is_array($versionInfo) && isset($versionInfo['data'])) {
  102. $list = $versionInfo['data'];
  103. $id = [];
  104. foreach ($list as $key => $val) {
  105. $savefile = app()->getRootPath() . 'public' . DS . 'upgrade_lv';
  106. //1,检查远程下载文件,并下载
  107. if (($save_path = $fileservice->check_remote_file_exists($val['zip_name'], $savefile)) === false) Json::fail('远程升级包不存在');
  108. //2,首先解压文件
  109. $savename = app()->getRootPath() . 'public' . DS . 'upgrade_lv' . DS . time();
  110. $fileservice->zipopen($save_path, $savename);
  111. //3,执行SQL文件
  112. Db::startTrans();
  113. try {
  114. //参数3不介意大小写的
  115. $sqlfile = $fileservice->list_dir_info($savename . DS, true, 'sql');
  116. if (is_array($sqlfile) && !empty($sqlfile)) {
  117. foreach ($sqlfile as $file) {
  118. if (file_exists($file)) {
  119. //为一键安装做工作记得表前缀要改为[#DB_PREFIX#]哦
  120. $execute_sql = explode(";\r", str_replace(['[#DB_PREFIX#]', "\n"], [$prefix, "\r"], file_get_contents($file)));
  121. foreach ($execute_sql as $_sql) {
  122. if ($query_string = trim(str_replace(array(
  123. "\r",
  124. "\n",
  125. "\t"
  126. ), '', $_sql))) Db::execute($query_string);
  127. }
  128. //执行完sql记得删掉哦
  129. $fileservice->unlink_file($file);
  130. }
  131. }
  132. }
  133. Db::commit();
  134. } catch (\Exception $e) {
  135. Db::rollback();
  136. //删除解压下的文件
  137. $fileservice->del_dir(app()->getRootPath() . 'public' . DS . 'upgrade_lv');
  138. //删除压缩包
  139. $fileservice->unlink_file($save_path);
  140. //升级失败发送错误信息
  141. $fileservice->request_post(uService::$isInsertLog, [
  142. 'content' => '升级失败,错误信息为:' . $e->getMessage(),
  143. 'add_time' => time(),
  144. 'ip' => $this->request->ip(),
  145. 'http' => $this->request->domain(),
  146. 'type' => 'error',
  147. 'version' => $val['version']
  148. ]);
  149. return Json::fail('升级失败SQL文件执行有误');
  150. }
  151. //4,备份文件
  152. $copyFile = app()->getRootPath() . 'public' . DS . 'copyfile' . $val['id'];
  153. $copyList = $fileservice->get_dirs($savename . DS);
  154. if (isset($copyList['dir'])) {
  155. if ($copyList['dir'][0] == '.' && $copyList['dir'][1] == '..') {
  156. array_shift($copyList['dir']);
  157. array_shift($copyList['dir']);
  158. }
  159. foreach ($copyList['dir'] as $dir) {
  160. if (file_exists(app()->getRootPath() . $dir, $copyFile . DS . $dir)) {
  161. $fileservice->copy_dir(app()->getRootPath() . $dir, $copyFile . DS . $dir);
  162. }
  163. }
  164. }
  165. //5,覆盖文件
  166. $fileservice->handle_dir($savename, app()->getRootPath());
  167. //6,删除升级生成的目录
  168. $fileservice->del_dir(app()->getRootPath() . 'public' . DS . 'upgrade_lv');
  169. //7,删除压缩包
  170. $fileservice->unlink_file($save_path);
  171. //8,改写本地升级文件
  172. $handle = fopen(app()->getRootPath() . '.version', 'w+');
  173. if ($handle === false) Json::fail(app()->getRootPath() . '.version' . '无法写入打开');
  174. $content = <<<EOT
  175. version={$val['version']}
  176. version_code={$val['id']}
  177. EOT;
  178. if (fwrite($handle, $content) === false) Json::fail('升级包写入失败');
  179. fclose($handle);
  180. //9,向服务端发送升级日志
  181. $posts = [
  182. 'ip' => $this->request->ip(),
  183. 'https' => $this->request->domain(),
  184. 'update_time' => time(),
  185. 'content' => '一键升级成功,升级版本号为:' . $val['version'] . '。版本code为:' . $val['id'],
  186. 'type' => 'log',
  187. 'versionbefor' => $this->serverweb['version'],
  188. 'versionend' => $val['version']
  189. ];
  190. $inset = $fileservice->request_post(uService::$isInsertLog, $posts);
  191. $id[] = $val['id'];
  192. }
  193. //10,升级完成
  194. Json::successful('升级成功', ['code' => end($id), 'version' => $val['version']]);
  195. } else {
  196. Json::fail('服务器异常,请稍后再试');
  197. }
  198. }
  199. }