SystemUpgradeClient.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace app\adminapi\controller\v1\system;
  3. use app\adminapi\controller\AuthController;
  4. use crmeb\services\UpgradeService as uService;
  5. use app\models\system\SystemConfig;
  6. use crmeb\services\UtilService;
  7. use think\facade\Db;
  8. /**
  9. * 在线升级控制器
  10. * Class SystemUpgradeclient
  11. * @package app\admin\controller\system
  12. *
  13. */
  14. class SystemUpgradeClient extends AuthController
  15. {
  16. protected $serverweb = array('version' => '1.0', 'version_code' => 0);//本站点信息
  17. public function initialize()
  18. {
  19. parent::initialize();
  20. //屏蔽所有错误避免操作文件夹发生错误提示
  21. ini_set('display_errors', 0);
  22. error_reporting(0);
  23. self::snyweninfo();//更新站点信息
  24. $this->assign(['auth' => self::isauth(), 'app' => uService::isWritable(app()->getRootPath()), 'extend' => uService::isWritable(EXTEND_PATH), 'public' => uService::isWritable(app()->getRootPath() . 'public')]);
  25. }
  26. //同步更新站点信息
  27. public function snyweninfo()
  28. {
  29. $this->serverweb['ip'] = $this->request->ip();
  30. $this->serverweb['host'] = $this->request->host();
  31. $this->serverweb['https'] = !empty($this->request->domain()) ? $this->request->domain() : SystemConfig::getConfigValue('site_url');
  32. $this->serverweb['webname'] = SystemConfig::getConfigValue('site_name');
  33. $local = uService::getVersion();
  34. if ($local['code'] == 200 && isset($local['msg']['version']) && isset($local['msg']['version_code'])) {
  35. $this->serverweb['version'] = uService::replace($local['msg']['version']);
  36. $this->serverweb['version_code'] = (int)uService::replace($local['msg']['version_code']);
  37. }
  38. uService::snyweninfo($this->serverweb);
  39. }
  40. //是否授权
  41. public function isauth()
  42. {
  43. return uService::isauth();
  44. }
  45. /**
  46. * 升级列表
  47. */
  48. public function index()
  49. {
  50. $where = UtilService::getMore([
  51. ['page', 1],
  52. ['limit', 20]
  53. ]);
  54. $list = uService::request_post(uService::$isList, ['page' => $where['page'], 'limit' => $where['limit']]);
  55. if (is_array($list) && isset($list['code']) && isset($list['data']) && $list['code'] == 200) {
  56. $list = $list['data'];
  57. } else {
  58. $list = [];
  59. }
  60. return $this->success($list);
  61. }
  62. //删除备份文件
  63. public function setcopydel()
  64. {
  65. $post = input('post.');
  66. if (!isset($post['id'])) $this->fail('删除备份文件失败,缺少参数ID');
  67. if (!isset($post['ids'])) $this->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. return $this->success('删除成功');
  79. }
  80. public function get_new_version_conte()
  81. {
  82. $post = $this->request->post();
  83. if (!isset($post['id'])) $this->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 $this->success(['count' => $versionInfo['data']['count']]);
  87. } else {
  88. return $this->fail('服务器异常');
  89. }
  90. }
  91. //一键升级
  92. public function auto_upgrade()
  93. {
  94. $prefix = config('database.prefix');
  95. $fileservice = new uService;
  96. $post = $this->request->post();
  97. if (!isset($post['id'])) return $this->fail('缺少参数ID');
  98. $versionInfo = $fileservice->request_post(uService::$isNowVersion, ['id' => $post['id']]);
  99. if ($versionInfo === null) return $this->fail('服务器异常,请稍后再试');
  100. if (isset($versionInfo['code']) && $versionInfo['code'] == 400) return $this->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) $this->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->listDirInfo($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->unlinkFile($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->unlinkFile($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 $this->fail('升级失败SQL文件执行有误');
  150. }
  151. //4,备份文件
  152. $copyFile = app()->getRootPath() . 'public' . DS . 'copyfile' . $val['id'];
  153. $copyList = $fileservice->getDirs($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->copyDir(app()->getRootPath() . $dir, $copyFile . DS . $dir);
  162. }
  163. }
  164. }
  165. //5,覆盖文件
  166. $fileservice->handleDir($savename, app()->getRootPath());
  167. //6,删除升级生成的目录
  168. $fileservice->del_dir(app()->getRootPath() . 'public' . DS . 'upgrade_lv');
  169. //7,删除压缩包
  170. $fileservice->unlinkFile($save_path);
  171. //8,改写本地升级文件
  172. $handle = fopen(app()->getRootPath() . '.version', 'w+');
  173. if ($handle === false) return $this->fail(app()->getRootPath() . '.version' . '无法写入打开');
  174. $content = <<<EOT
  175. version={$val['version']}
  176. version_code={$val['id']}
  177. EOT;
  178. if (fwrite($handle, $content) === false) return $this->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. return $this->success('升级成功', ['code' => end($id), 'version' => $val['version']]);
  195. } else {
  196. return $this->fail('服务器异常,请稍后再试');
  197. }
  198. }
  199. }