SystemUpgradeClient.php 9.9 KB

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