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

414 lines
15 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\controllers\admin;
use Yii;
use yii\web\Controller;
use app\models\common\Config;
class SiteController extends CommonController{
public $enableCsrfValidation = false;
public $enableCsr = 'abcd';
public function actionGetsite(){
$request = Yii::$app->request;
$re=YII::$app->db->createCommand("select * from {{%ybwm_site_set}}")->queryOne();
return $this->result(1,'成功',$re);
return $this->result(2,'请求异常');
}
//检查系统版本
public function actionSite_upgrade(){
//运行sql文件
// $sqlData=file_get_contents('update.sql');
// $connection=Yii::$app->db;
// $sql_arr=explode(';',$sqlData);
// foreach ($sql_arr as $v){
// //print_r($v);
// $connection->createCommand(trim($v))->execute();
// $connection->close();
// }
// unlink('update.sql');
$version_json= file_get_contents('web/version.json');
$version_arr=json_decode($version_json,true);
$version=$version_arr['version'];
$version_date=$version_arr['version_release'];
return $this->renderpartial('site_upgrade',['version'=>$version,'version_date'=>$version_date]);
}
public function actionSitesave(){
$request = Yii::$app->request;
if ($request->isPost){
$result=axios_request();
$re=YII::$app->db->createCommand("select * from {{%ybwm_site_set}}")->queryOne();
$data=array(
'status'=>$result['status'],
'site_title'=>$result['site_title'],
'site_keyword'=>$result['site_keyword'],
'site_description'=>$result['site_description'],
'copyright'=>$result['copyright'],
'copyright_str'=>$result['copyright_str'],
'record_number'=>$result['record_number'],
'site_logo'=>$result['site_logo'],
'site_image'=>$result['site_image'],
'site_backgroundpicture'=>$result['site_backgroundpicture'],
'site_name'=>$result['site_name'],
);
if($re){
$data['up_operdate']=date('Y-m-d H:i:s',time());
$res=YII::$app->db->createCommand()->update('{{%ybwm_site_set}}',$data, 'id=:id', ['id' =>$re['id']])->execute();
}else{
$data['dt_operdate']=date('Y-m-d H:i:s',time());
$res=YII::$app->db->createCommand()->insert('{{%ybwm_site_set}}',$data)->execute();
}
$row=Config::getSystemSet('superPower',0);
$row['site_logo']=$result['site_logo'];
$row['copyright']=$result['copyright'];
Config::saveSystemSet($row,'superPower',0,'超级权限');
if($res!==false){
return $this->result(1,'成功');
}else{
return $this->result(2,'网络异常,请稍后再试');
}
}
return $this->result(2,'请求异常');
}
public function actionSite_authorization()
{
return $this->renderpartial('site_authorization');
}
public function actionSite_environment()
{
return $this->renderpartial('site_environment');
}
public function actionSite_grant(){
$domianInfo=getSysInfo();
if($domianInfo['status']==1){
if($domianInfo['time_type']==1){
$domianInfo['endTime']="无限";
}else{
$domianInfo['endTime']=$domianInfo['time_start']."~ ".$domianInfo['time_end'];
}
}
if($domianInfo['account_type']==1){
$domianInfo['accountNum']="无限";
}else{
$domianInfo['accountNum']=$domianInfo['account_number'];
}
$re=(new \yii\db\Query())
->from('{{%ybwm_admin}}')
->select('id,domainName,phone')
->where(['userType'=>1])
->one();
$domianInfo['phone']=$domianInfo['phone']?:$re['phone'];
$domianInfo['domainName']=$domianInfo['domain_name']?:$re['domainName'];
$domianInfo['ip_address']=$re['joinip']?:gethostbyname($_SERVER['SERVER_NAME']);
$domianInfo['auth_code']=str_ireplace(substr($domianInfo['auth_code'],5,13),'*****',$domianInfo['auth_code']);
return $this->renderpartial('site_grant',['data'=>$domianInfo]);
}
public function actionSite_notice(){
return $this->renderpartial('site_notice');
}
public function actionSite_noticedetail()
{
return $this->renderpartial('site_noticedetail');
}
public function actionSite_setting()
{
return $this->renderpartial('site_setting');
}
//系统环境
public function actionSystem_variable(){
Yii::$app->db->open();
$url= Yii::$app->request->hostInfo;
$ip=gethostbyname(parse_url($url)['host']);
$system=PHP_OS;
if(PHP_SAPI=='apache2handler'){
$linux_version='Apache';
}else{
$linux_version='Nginx';
}
$php_version=PHP_VERSION;
//获取PHP模块
$php_module=implode(',',get_loaded_extensions());
//Yii::$app->db->open();
$mysql_version=Yii::$app->db->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION);
$array=array(
'code'=>1,
'domainName'=>$url,
'ip'=>$ip,
'system'=>$system,
'php_version'=>$php_version,
'mysql_version'=>$mysql_version,
'linux_version'=>$linux_version,
'size_max'=>ini_get('post_max_size'),
'version'=>json_decode(file_get_contents('./web/version.json'),true)['version'],
'php_module'=>$php_module
);
echo json_encode($array);
}
/**获取客户端ip
* @return string
*/
protected function getClientIp (){
$ip=FALSE;
//客户端IP 或 NONE
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
//多重代理服务器下的客户端真实IP地址可能伪造,如果没有使用代理,此字段为空
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
for ($i = 0; $i < count($ips); $i++) {
if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
$ip = $ips[$i];
break;
}
}
}
//客户端IP 或 (最后一个)代理服务器 IP
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
//获取远程服务器的版本
public function actionGetversion(){
$url='https://demo.b-ke.cn/version.txt';
//本地版本号
$version=json_decode(file_get_contents($url),true)['version'];
//dd($version);die;
//获取远程服务器的版本
$server_url="https://bkycms.com/addons/mg_jz/web/update/version.txt";
$server_demo=json_decode(file_get_contents($server_url),true);
$server_version=$server_demo['version'];
//dd(file_get_contents($server_url));die;
if($version&&$server_version&&$version<$server_version){
$server_dir=$server_demo['update_dir'];
//版本循环出来和当前版本对比一下,如果版本泰迪就进行升级
$result=explode(',',$server_dir);
dd($this->get_my_files("https://bkycms.com/addons/mg_jz/web/update"));die;
echo json_ecoce(['code'=>1,'old_version'=>$version,'new_version'=>$server_version]);
}else{
echo json_ecoce(['code'=>2,'获取版本失败']);
}
}
function get_my_files( $path ){
$list = array();
foreach( glob( $path . '/*') as $item ){
if( is_dir( $item ) ){
$list[] = $item;
}
}
return $list;
}
//网站升级方案一 md5_file比对文件拉文件覆盖
public function actionCover_comparison(){
Yii::$app->cache->flush();
echo json_encode(['code'=>'1','msg'=>'项目已成功部署']);die;
}
//网站升级方案二 覆盖MVC层和公共文件夹commom
public function actionCover_part(){
echo json_encode(['code'=>'1','msg'=>'项目已成功部署']);die;
}
//网站升级方案三 下载远程压缩包解压到本地服务器覆盖
public function actionCover_project(){
//获取项目根目录
$base_dir=dirname(Yii::$app->BasePath); //'/www/wwwroot'
$domain_url=Yii::$app->request->hostInfo;
//目标地址
$url = "https://cs.bkycms.com/cs.bkycms.com.zip";
//接收压缩包目录
$zipTo_dir =$base_dir;
$preg = "/^http(s)?:\\/\\/.+/";
//接受压缩包的自定义名称
$newzip = $domain_url.".zip";
if(preg_match($preg,$newzip)){
$newzip=str_replace("https://",'',$newzip);
}
//dd($url); dd($zipTo_dir);dd($newzip);die;
//接受压缩包的自定义名称
$res = getFile($url, $zipTo_dir, $newzip, 1);
/*-------------------------------在项目目录解压压缩包-----------------------------------------------------*/
unzipdir($zipTo_dir.'/'.$newzip,$base_dir);
//unlink($zipTo_dir.'/'.$newzip);
echo json_encode(['code'=>'1','msg'=>'项目已成功部署']);die;
}
function actionCoverceshi()
{
$url = "https://cs.bkycms.com/demo.b-ke.cn.zip";
$url=urldecode($url);
$file_name=basename("$url"); //返回路径中的文件名部分 fetion_sms.zip
$upload_dir="./uploads/";//上传的路径
$dir=$upload_dir.$file_name;//创建上传目录
//判断目录是否存在 不存在则创建
if(!file_exists($upload_dir)){
mkdir($upload_dir,0777,true);
}
$contents=$this->curl_download($url,$dir);
if($contents){
$this->unzip($dir,'../', true, false);
echo "下载成功";
}else{
echo "下载失败";
}
}
public function create_dirs($path){
if (!is_dir($path)){
$directory_path = "";
$directories = explode("/",$path);
array_pop($directories);
foreach($directories as $directory){
$directory_path .= $directory."/";
if (!is_dir($directory_path)){
mkdir($directory_path);
chmod($directory_path, 0777);
}
}
}
}
function curl_download($url, $dir) {
$ch = curl_init($url);
$fp = fopen($dir, "wb");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$res=curl_exec($ch);
curl_close($ch);
fclose($fp);
return $res;
}
/**
* 解压文件到指定目录
*
* @param string zip压缩文件的路径
* @param string 解压文件的目的路径
* @param boolean 是否以压缩文件的名字创建目标文件夹
* @param boolean 是否重写已经存在的文件
*
* @return boolean 返回成功 或失败
*/
public function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true){
if ($zip = zip_open($src_file)){
if ($zip){
$splitter = ($create_zip_name_dir === true) ? "." : "/";
if($dest_dir === false){
$dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";
}
// 如果不存在 创建目标解压目录
$this->create_dirs($dest_dir);
// 对每个文件进行解压
while ($zip_entry = zip_read($zip)){
// 文件不在根目录
$pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
if ($pos_last_slash !== false){
// 创建目录 在末尾带 /
$this->create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
}
// 打开包
if (zip_entry_open($zip,$zip_entry,"r")){
// 文件名保存在磁盘上
$file_name = $dest_dir.zip_entry_name($zip_entry);
// 检查文件是否需要重写
if ($overwrite === true || $overwrite === false && !is_file($file_name)){
// 读取压缩文件的内容
$fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
@file_put_contents($file_name, $fstream);
// 设置权限
chmod($file_name, 0777);
echo "save: ".$file_name."<br />";
}
// 关闭入口
zip_entry_close($zip_entry);
}
}
// 关闭压缩包
zip_close($zip);
}
}else{
return false;
}
return true;
}
//系統更新頁面
public function actionSite_cloud_upgrade(){
//数据库升级
return $this->renderpartial('site_cloud_upgrade');
}
public function actionCeshi(){
if (isset($_SERVER)) {
if($_SERVER['SERVER_ADDR']) {
$server_ip = $_SERVER['SERVER_ADDR'];
} else {
$server_ip = $_SERVER['LOCAL_ADDR'];
}
} else {
$server_ip = getenv('SERVER_ADDR');
}
dd(gethostbyname('demo.b-ke.cn'));
}
public function actionGetNoticeList(){
$request = Yii::$app->request;
if ($request->isPost) {
$result = axios_request();
$data = array('page' =>$result['page'],'type'=>$result['type']);
//获取项目的应用列表
$url = Yii::$app->params['domain_url'] . '/cloud/notice/getnoticelist';
$result= json_decode(httpRequest($url, $data));
//var_dump($result);die;
return $this->result(1,'成功',$result);
}
}
public function actionGetNoticeDetails(){
$request = Yii::$app->request;
$result = axios_request();
$data = array('id' =>$result['id'],'type'=>$result['type']);
//获取项目的应用列表
$url = Yii::$app->params['domain_url'] . '/cloud/notice/noticebyid';
$result= json_decode(httpRequest($url, $data));
return $this->result(1,'成功',$result);
}
public function actionSite_enclosure(){
return $this->renderpartial('site_enclosure');
}
// 下载压缩包
function actionDownRar(){
$url =Yii::$app->params['domain_url'].'/upgraded/master/upgraded.zip';
header("Content-type:application/octet-stream");
$filename = basename($url);
header("Content-Disposition:attachment;filename = ".$filename);
header("Accept-ranges:bytes");
header("Accept-length:".filesize($url));
readfile($url);
}
}