SystemUpgradeclient.php 9.3 KB

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