canyin-project/ybcy/controllers/admin/UpgradeController.php
2024-11-01 16:07:54 +08:00

267 lines
11 KiB
PHP

<?php
namespace app\controllers\admin;
use Yii;
use yii\web\Controller;
use Mysqldump;
use Zipdump;
class UpgradeController extends CommonController{
public $enableCsrfValidation = false;
//admin/install/get-upgrade-info
//检查版本
public function actionGetUpgradeInfo(){
$request = Yii::$app->request;
if ($request->isPost){
$url=Yii::$app->params['domain_url'].'/cloud/upgraded/getUpgradeInfo';
$version_json= file_get_contents(Yii::$app->basePath.'/web/version.json');
$version_arr=json_decode($version_json,true);
$domain_url = preg_replace("(^https?://)", "", Yii::$app->request->hostInfo);
$version_arr['ip_address']=gethostbyname($domain_url);
$version_arr['domain_url']=$domain_url;
$version_arr['auth_type']=Yii::$app->params['auth_type'];
$file_arr=httpRequest($url,$version_arr);
echo $file_arr;die;
}
}
public function deep_in_array($value, $array) {
foreach($array as $item) {
if(!is_array($item)) {
if ($item == $value) {
return true;
} else {
continue;
}
}
if(in_array($value, $item)) {
return true;
} else if($this->deep_in_array($value, $item)) {
return true;
}
}
return false;
}
//回滚版本
public function actionBackUp(){
$info=file_get_contents(Yii::$app->basePath.'/web/version.json');
$version_arr=json_decode($info,true);
$version_arr['auth_type']=Yii::$app->params['auth_type'];
$url=Yii::$app->params['domain_url'].'/cloud/upgraded/getOldVersion';
$file_arr=httpRequest($url,$version_arr);
$data=json_decode($file_arr,true)['data'];
// var_dump($data);die;
$json=json_encode(['version'=>$data['version_code'],'version_release'=>$data['version_date']]);
file_put_contents(Yii::$app->basePath.'/web/version.json',$json);
echo json_encode(['code'=>1,'msg'=>'已撤回到最近的版本'.$data['version_code']]);die;
}
public function actionCompressPackage(){
$request = Yii::$app->request;
if($request->isGet){
$new_version=Yii::$app->request->get('version');
$version_date=Yii::$app->request->get('version_date');
if(Yii::$app->request->get('domain_url')=='v2.bkycms.com'||Yii::$app->request->get('domain_url')=='bkycms.com'){
return '禁止更新';die;
}
$url=Yii::$app->params['domain_url'].'/cloud/upgraded/getUpgradeInfo';
$version_json= file_get_contents(Yii::$app->basePath.'/web/version.json');
$version_arr=json_decode($version_json,true);
$version_arr['auth_type']=Yii::$app->params['auth_type'];
$version_arr['domain_url']=preg_replace("(^https?://)", "", Yii::$app->request->hostInfo);
$file_arr=httpRequest($url,$version_arr);
$result=json_decode($file_arr,true);
if($result['code']==1&&$result['msg']=='版本升级信息已获取'){
if($new_version&&$version_date){
// dd($new_version); dd($version_date);die;
$url=Yii::$app->params['update_url'].'/upgraded/master/upgraded.zip';
// dd($url);die;
$save_dir=Yii::$app->basePath.'/web/upgraded';
if(file_exists($save_dir)){
removeDir($save_dir);
}
mkdir($save_dir);
chmod($save_dir,0755);
$filename='upgrade.zip';
//下载远程升級包
getFile($url, $save_dir , $filename);
$outPath = Yii::$app->basePath;
if(file_exists($save_dir.'/'.$filename)){
unzip($save_dir.'/'.$filename,$outPath, true, true);
unlink($save_dir.'/'.$filename);
//运行sql文件
if(file_exists('update.sql')){
$sqlData=file_get_contents('update.sql');
$sql_arr=explode(';',$sqlData);
foreach ($sql_arr as $v){
if(trim($v)){
Yii::$app->runAction('admin/update/sql-dump-load',['sql'=>trim($v)]);
}
}
unlink('update.sql');
}
//記錄版本升級日誌
$data=array(
'old_version'=>$version_arr['version'],
'old_release'=>$version_arr['version_release'],
'new_version'=>$new_version,
'new_release'=>$version_date,
'dt_operdate'=>date('Y-m-d H:i:s',time()),
);
Yii::$app->db->createCommand()->insert('{{%ybwm_version_log}}', $data)->execute();
if(!is_writable(Yii::$app->basePath.'/web/version.json')){
chmod('web/version.json',0755);
}
$json=json_encode(['version'=>$new_version,'version_release'=>$version_date]);
file_put_contents(Yii::$app->basePath.'/web/version.json',$json);
echo json_encode(['code'=>1,'msg'=>'系统升级成功']);
}
}
}else{
echo json_encode(['code'=>2,'msg'=>'暂无可更新的版本']);
}
}
}
//在线数据库备份
public function actionMysqldump(){
//1.extend/Mysqldump.php
// 在config/web.php 注册服务Yii::$classMap['Mysqldump'] = '@app/extend/Mysqldump.php';
//2.控制器use Mysqldump; 引用extend/Mysqldump.php
Mysqldump::back();
echo json_encode(
array(
'code'=>200,
'msg'=>'数据库备份成功'
)
);
}
//备份整个项目目录
public function actionZipdumpdir(){
if(!file_exists('oldversion')){
mkdir('oldversion');
chmod('oldversion',0777,true);
}
$from =Yii::$app->basePath;
//dd($from);die;
$to = 'oldversion/zip'.date('Y-m-d H:i:s',time()).'.zip';
if(!file_exists($to)){
touch($to);
chmod($to,0777,true);
}
$zip_result = Zipdump::createZip($from, $to);
if($zip_result['success']=1&&$zip_result['message']==''){
echo json_encode(
['code'=>200,
'msg'=>'备份完成'
]
);
}else{
echo json_encode(
['code'=>500,
'msg'=>$zip_result['message']
]
);
}
}
//下载压缩包文件
public function actionUnziptodir(){
$request = Yii::$app->request;
if ($request->isGet){
$destination=Yii::$app->request->get()['downname'];
//下载前判断文件是否打包
if(!file_exists($destination)){
echo json_encode(['code'=>500,'msg'=>'压缩包不存在']);die;
}
//下载zip
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($destination)); //文件名
header("Content-Type: application/zip"); //zip格式的
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
header('Content-Length: '. filesize($destination)); //告诉浏览器,文件大小
readfile($destination);
exit();
}else{
return false;
}
}
//安装应用
public function actionAppInstall(){
$request = Yii::$app->request;
if ($request->isPost){
$result=axios_request();
$url=Yii::$app->params['domain_url'].'/cloud/upgraded/app_install';
$data=array(
'domain_url'=>Yii::$app->request->hostInfo,
'id'=>axios_request()['id']
);
//dd($data);die;
$file_arr=httpRequest($url,$data);
Yii::$app->db->createCommand()->update('{{%ybwm_app_list}}', ['status'=>1], ['id'=>$result['id']])->execute();
echo $file_arr;
}
}
//应用升级
public function actionAppUpgrade(){
$request = Yii::$app->request;
if ($request->isPost){
$result=axios_request();
$texArr=file_get_contents('web/channel/version.json');
$url=Yii::$app->params['domain_url'].'/cloud/upgraded/appUpgrade';
$data=array(
'version'=>json_decode($texArr)->version,
'id'=>$result['id'],
'version_date'=>json_decode($texArr)->version_release,
);
if(Yii::$app->request->hostInfo=="https://bkycms.com"||Yii::$app->request->hostInfo=="https://v2.bkycms.com"){
echo json_encode(['code'=>2,'msg'=>'测试域名禁止升级']);die;
}
$file_arr=httpRequest($url,$data);
if(json_decode($file_arr,true)['code']==1&&json_decode($file_arr,true)['data']){
$zip_url=Yii::$app->params['domain_url'].'/channel/master/channel.zip';
$save_dir=Yii::$app->basePath.'/web/channel';
if(!file_exists($save_dir)){
mkdir($save_dir);
chmod($save_dir,0755);
}
$filename='channel.zip';
//下载远程升級包
getFile($zip_url,$save_dir,$filename);
//解压压缩包到指定目录覆盖文件
unzip($save_dir.'/'.$filename,Yii::$app->basePath,true,true);
unlink(Yii::$app->basePath.'/web/channel/channel.zip');
$new_data=array(
'app_version'=>json_decode($file_arr,true)['data']['version'],
'changedAt'=>time()
);
$re=YII::$app->db->createCommand()->update('{{%ybwm_app_list}}',$new_data, 'id=:id', ['id' =>$result['id']])->execute();
echo json_encode(['code'=>1,'msg'=>'应用升级成功']);die;
}else{
echo json_encode(['code'=>1,'msg'=>json_decode($file_arr,true)['msg']]);die;
}
}
}
public function actionBasic(){
$url=Yii::$app->params['domain_url'].'/cloud/upgraded/getSecret';
$json=httpRequest($url);
file_put_contents('web/secret.json',$json);
}
}