123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace app\admin\controller\system;
- use app\admin\controller\AuthController;
- use crmeb\services\JsonService as Json;
- use crmeb\services\UpgradeService as uService;
- use think\facade\Db;
- class SystemUpgradeclient extends AuthController
- {
- protected $serverweb = array('version' => '1.0', 'version_code' => 0);
- public function initialize()
- {
- parent::initialize();
-
- ini_set('display_errors', 0);
- error_reporting(0);
- self::snyweninfo();
- $this->assign(['auth' => self::isauth(), 'app' => uService::isWritable(app()->getRootPath()), 'extend' => uService::isWritable(EXTEND_PATH), 'public' => uService::isWritable(app()->getRootPath() . 'public')]);
- }
-
- public function snyweninfo()
- {
- $this->serverweb['ip'] = $this->request->ip();
- $this->serverweb['host'] = $this->request->host();
- $this->serverweb['https'] = !empty($this->request->domain()) ? $this->request->domain() : sys_config('site_url');
- $this->serverweb['webname'] = sys_config('site_name');
- $local = uService::getVersion();
- if ($local['code'] == 200 && isset($local['msg']['version']) && isset($local['msg']['version_code'])) {
- $this->serverweb['version'] = uService::replace($local['msg']['version']);
- $this->serverweb['version_code'] = (int)uService::replace($local['msg']['version_code']);
- }
- uService::snyweninfo($this->serverweb);
- }
-
- public function isauth()
- {
- return uService::isauth();
- }
- public function index()
- {
- $server = uService::start();
- $version = $this->serverweb['version'];
- $version_code = $this->serverweb['version_code'];
- $this->assign(compact('server', 'version', 'version_code'));
- return $this->fetch();
- }
- public function get_list()
- {
- $list = uService::request_post(uService::$isList, ['page' => input('post.page/d'), 'limit' => input('post.limit/d')]);
- if (is_array($list) && isset($list['code']) && isset($list['data']) && $list['code'] == 200) {
- $list = $list['data'];
- } else {
- $list = [];
- }
- Json::successful('ok', ['list' => $list, 'page' => input('post.page/d') + 1]);
- }
-
- public function setcopydel()
- {
- $post = input('post.');
- if (!isset($post['id'])) Json::fail('删除备份文件失败,缺少参数ID');
- if (!isset($post['ids'])) Json::fail('删除备份文件失败,缺少参数IDS');
- $fileservice = new uService;
- if (is_array($post['ids'])) {
- foreach ($post['ids'] as $file) {
- $fileservice->del_dir(app()->getRootPath() . 'public' . DS . 'copyfile' . $file);
- }
- }
- if ($post['id']) {
- $copyFile = app()->getRootPath() . 'public' . DS . 'copyfile' . $post['id'];
- $fileservice->del_dir($copyFile);
- }
- Json::successful('删除成功');
- }
- public function get_new_version_conte()
- {
- $post = $this->request->post();
- if (!isset($post['id'])) Json::fail('缺少参数ID');
- $versionInfo = uService::request_post(uService::$NewVersionCount, ['id' => $post['id']]);
- if (isset($versionInfo['code']) && isset($versionInfo['data']['count']) && $versionInfo['code'] == 200) {
- return Json::successful(['count' => $versionInfo['data']['count']]);
- } else {
- return Json::fail('服务器异常');
- }
- }
-
- public function auto_upgrad()
- {
- $prefix = config('database.prefix');
- $fileservice = new uService;
- $post = $this->request->post();
- if (!isset($post['id'])) Json::fail('缺少参数ID');
- $versionInfo = $fileservice->request_post(uService::$isNowVersion, ['id' => $post['id']]);
- if ($versionInfo === null) Json::fail('服务器异常,请稍后再试');
- if (isset($versionInfo['code']) && $versionInfo['code'] == 400) Json::fail(isset($versionInfo['msg']) ? $versionInfo['msg'] : '您暂时没有权限升级,请联系管理员!');
- if (is_array($versionInfo) && isset($versionInfo['data'])) {
- $list = $versionInfo['data'];
- $id = [];
- foreach ($list as $key => $val) {
- $savefile = app()->getRootPath() . 'public' . DS . 'upgrade_lv';
-
- if (($save_path = $fileservice->check_remote_file_exists($val['zip_name'], $savefile)) === false) Json::fail('远程升级包不存在');
-
- $savename = app()->getRootPath() . 'public' . DS . 'upgrade_lv' . DS . time();
- $fileservice->zipopen($save_path, $savename);
-
- Db::startTrans();
- try {
-
- $sqlfile = $fileservice->list_dir_info($savename . DS, true, 'sql');
- if (is_array($sqlfile) && !empty($sqlfile)) {
- foreach ($sqlfile as $file) {
- if (file_exists($file)) {
-
- $execute_sql = explode(";\r", str_replace(['[#DB_PREFIX#]', "\n"], [$prefix, "\r"], file_get_contents($file)));
- foreach ($execute_sql as $_sql) {
- if ($query_string = trim(str_replace(array(
- "\r",
- "\n",
- "\t"
- ), '', $_sql))) Db::execute($query_string);
- }
-
- $fileservice->unlink_file($file);
- }
- }
- }
- Db::commit();
- } catch (\Exception $e) {
- Db::rollback();
-
- $fileservice->del_dir(app()->getRootPath() . 'public' . DS . 'upgrade_lv');
-
- $fileservice->unlink_file($save_path);
-
- $fileservice->request_post(uService::$isInsertLog, [
- 'content' => '升级失败,错误信息为:' . $e->getMessage(),
- 'add_time' => time(),
- 'ip' => $this->request->ip(),
- 'http' => $this->request->domain(),
- 'type' => 'error',
- 'version' => $val['version']
- ]);
- return Json::fail('升级失败SQL文件执行有误');
- }
-
- $copyFile = app()->getRootPath() . 'public' . DS . 'copyfile' . $val['id'];
- $copyList = $fileservice->get_dirs($savename . DS);
- if (isset($copyList['dir'])) {
- if ($copyList['dir'][0] == '.' && $copyList['dir'][1] == '..') {
- array_shift($copyList['dir']);
- array_shift($copyList['dir']);
- }
- foreach ($copyList['dir'] as $dir) {
- if (file_exists(app()->getRootPath() . $dir, $copyFile . DS . $dir)) {
- $fileservice->copy_dir(app()->getRootPath() . $dir, $copyFile . DS . $dir);
- }
- }
- }
-
- $fileservice->handle_dir($savename, app()->getRootPath());
-
- $fileservice->del_dir(app()->getRootPath() . 'public' . DS . 'upgrade_lv');
-
- $fileservice->unlink_file($save_path);
-
- $handle = fopen(app()->getRootPath() . '.version', 'w+');
- if ($handle === false) Json::fail(app()->getRootPath() . '.version' . '无法写入打开');
- $content = <<<EOT
- version={$val['version']}
- version_code={$val['id']}
- EOT;
- if (fwrite($handle, $content) === false) Json::fail('升级包写入失败');
- fclose($handle);
-
- $posts = [
- 'ip' => $this->request->ip(),
- 'https' => $this->request->domain(),
- 'update_time' => time(),
- 'content' => '一键升级成功,升级版本号为:' . $val['version'] . '。版本code为:' . $val['id'],
- 'type' => 'log',
- 'versionbefor' => $this->serverweb['version'],
- 'versionend' => $val['version']
- ];
- $inset = $fileservice->request_post(uService::$isInsertLog, $posts);
- $id[] = $val['id'];
- }
-
- Json::successful('升级成功', ['code' => end($id), 'version' => $val['version']]);
- } else {
- Json::fail('服务器异常,请稍后再试');
- }
- }
- }
|