canyin-project/ybcy/controllers/admin/InstallController.php

294 lines
11 KiB
PHP
Raw Normal View History

2024-11-01 16:07:54 +08:00
<?php
namespace app\controllers\admin;
use Yii;
use yii\web\Controller;
use Mrgoon\AliSms\AliSms;
use yii\db\Connection;
class InstallController extends Controller{
public $enableCsrfValidation = false;
public function init(){
parent::init();
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
throw new \Exception('建议使用 Linux 系统以提升程序性能');die;
}
}
public function actionLogin(){
$Information=getSysInfo();
if($Information['authData']){
Yii::$app->response->redirect(['index.php/admin/login/index'], 301)->send();die;
}else{
//二次安装查询授权后台是否安装过
$url=Yii::$app->params['domain_url'].'/cloud/auth/installAuth';
$data=['domain_url'=>Yii::$app->request->hostInfo,'auth_type'=>1];
$result=httpRequest($url,$data);
$data=json_decode($result,true);
if($data['code']==1){
//file_put_contents('./web/secret.json',$data['authData']);
Yii::$app->session->set('vip_no',$data['phone']);
Yii::$app->response->redirect(['index.php/admin/install/monitor'], 301)->send();die;
}
}
return $this->renderpartial('login');
}
public function actionComplete(){
$url=Yii::$app->params['domain_url'].'/cloud/auth/installAuth';
$data=['domain_url'=>Yii::$app->request->hostInfo];
$result=httpRequest($url,$data);
$data=json_decode($result,true);
if($data['code']==1){
file_put_contents('./web/secret.json',$data['authData']);
}
return $this->renderpartial('complete');
}
public function actionMonitor(){
return $this->renderpartial('monitor');
}
public function actionMysql_install(){
return $this->renderpartial('mysql_install');
}
public function actionRegistered_account(){
$phone=Yii::$app->session->get('vip_no');
return $this->renderpartial('registered_account',['phone'=>$phone]);
}
public function actionDekjo(){
dd(\Yii::$app->db);die;
}
public function actionReset_mysql(){
if(YII::$app->request->isPost){
// echo date("Y-m-d H:i:s",time());
$data=axios_request();
$db=trim($data['databaseName']);
$username=trim($data['databaseUser']);
$password=trim($data['databasePassword']);
$dbprefix=trim($data['databasePrefix']);
if(strpos(trim($data['databaseHost']),'127.0.0.1')!==false){
$dbIp='localhost';
}else{
$dbIp=trim($data['databaseHost']);
}
if($db&&$username&&$password&&$dbprefix){
if(substr($dbprefix, -1)!=='_'){
$this->result(2,'数据表前缀不符合规范');die;
}
if (!preg_match("/^[a-zA-Z]/",$dbprefix)){
$this->result(2,'数据表前缀不符合规范');die;
}
$content="<?php return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=$dbIp;dbname=$db',
'username' => '$username',
'password' => '$password',
'charset' => 'utf8',
'tablePrefix'=> '$dbprefix',
];";
$config_file=\Yii::$app->basePath.'/config/db.php';
chmod($config_file,0777,true);
$write_bool=file_put_contents($config_file,$content);
// dd($write_bool);die;
if($write_bool){
set_time_limit(0);
$connection = new Connection([
'dsn' => 'mysql:host=localhost;dbname='.$db,
'username' => $username,
'password' => $password,
'tablePrefix'=>$dbprefix,
]);
//打开
$connection->open();
//导入sql文件
$filename=\Yii::$app->basePath.'/web/backsql.sql';
if(!file_exists($filename)){
return $this->result(2,'导入的sql文件不存在');
}
$sql =file_get_contents($filename);
$sql_arr=explode(';',$sql);
foreach ($sql_arr as $k=>$v){
if($dbprefix!=='ims_'){
$v=str_replace("ims_",$dbprefix,trim($v));
}
if(trim($v)){
$connection->createCommand(trim($v))->execute();
}
}
$connection->close();
return $this->result(1,'数据库创建成功');
}
}
//echo date("Y-m-d H:i:s",time());die;
}
$this->result(2,'请求异常');
}
public function actionGetvesion(){
$request = Yii::$app->request;
if ($request->isPost){
if(axios_request()['install_type']=='get_version'){
//echo 111;die;
$data=array(
'os_version'=>PHP_OS,//服务器操作系统
'nginx_version'=>php_uname('s').php_uname('r'),//服务器版本
'php_version'=>PHP_VERSION,//php版本
'total'=>disk_total_space(dirname(__FILE__)),//磁盘空间
);
if(!extension_loaded("pdo")){
$data['module'][]='pdo';
}
if(!extension_loaded("openssl")){
$data['module'][]='openssl';
}
if(!extension_loaded("gd")){
$data['module'][]='gd';
}
if(!extension_loaded("redis")){
$data['module'][]='redis';
}
if(!extension_loaded("curl")){
$data['module'][]='curl';
}
if($data['module']){
$data['status']='fail';
}
$this->result(1,'成功',$data);die;
}
}else{
$this->result(2,'请求异常');
}
}
public function actionUser_add(){
$arr=axios_request();
//$url=parse_url($_SERVER['HTTP_REFERER'])['host'];
// if(!preg_match("/^[A-Za-z0-9]+$/",trim($arr['webPassword']))){
// return $this->result(2, '密码不能包含中文和特殊字符!');die;
// }
$salt=randoms(8);
$auth_key=Yii::$app->params['authkey'];
$password=checkPassWord($salt,$auth_key,trim($arr['webPassword']));
$data=array(
'domainName'=>$arr['webName'],
'username'=>$arr['webAccount'],
'password'=>$password,
'salt'=>$salt,
'phone'=>Yii::$app->session->get('vip_no'),
'createdAt'=>time(),
'joinip'=>getIps(),
'userType'=>1
);
//dd($data);die;
$re= YII::$app->db->createCommand('select * from {{%ybwm_admin}} where username=:username', ['username' =>$arr['webAccount']])->queryAll();
if($re){
return $this->result(2,'用户已存在');
}else{
$res=Yii::$app->db->createCommand()->insert('{{%ybwm_admin}}', $data)->execute();
if($res){
Yii::$app->session->remove('vip_no');
$installData=array(
'domain_url'=>Yii::$app->request->hostInfo,
'success_time'=>time(),
'joinip'=>getIps(),
);
$row=ybwmEncrypt(json_encode($installData),Yii::$app->params['channel_model_name']);
file_put_contents('install.lock',$row);
$this->result(1,'成功');
}else{
$this->result(2,'网络异常,请稍后再试');
}
}
}
public function result($code=1,$msg='成功',$data=[],$count=null)
{
echo json_encode([
'code'=>$code,
'msg'=>$msg,
'data'=>$data,
'count'=>$count
]);
}
public function actionGetcode(){
if(YII::$app->request->isPost){
$data=axios_request();
$code=generate_code(4);
$aliSms = new AliSms();
$phone=$data['phone'];
Yii::$app->session->set('vip_no',$phone);
$response = object_array($aliSms->sendSms($phone, Yii::$app->params['smsconfig']['smsCode'], ['code'=>$code], Yii::$app->params['smsconfig']));
if($response['Message']=='OK'&&$response['Code']=='OK'){
Yii::$app->session->remove('smscode');
Yii::$app->session->set('smscode',$code);
$this->result(1,'成功');
}else{
$this->result(2,$response['Message']);
}
}
}
public function actionCheckcode(){
if(YII::$app->request->isPost){
return $this->result(1,'验证成功');
$data=axios_request();
if($data['code']==Yii::$app->session->get('smscode')){
$datas['auth_code']=$data['activeCode'];
$datas['domain_url']=$data['domain_url'];
$datas['code_type']=$data['code_type'];
$datas['phone']=$data['phone'];
$datas['dev']=Yii::$app->params['isDev']?1:2;
if($data['activeCode']){
$url=Yii::$app->params['domain_url'].'/cloud/auth/checkAuth';
$result=httpRequest($url,$datas);
$authData=json_decode($result,true)['authData'];
//var_dump($result);die;
if($authData){
//var_dump($authData);die;
$row=ybwmEncrypt(json_encode($authData),Yii::$app->params['channel_model_name']);
touch('./web/secret.json');
chmod('./web/secret.json',0777);
file_put_contents('./web/secret.json',$row);
if($result){
$this->result(1,'验证成功');
}else{
$this->result(2,'验证失败,请保证数据合法性!');
}
}else{
$this->result(2,'激活码校验错误,请联系官方客服获取!');
}
}else{
$this->result(2,'请输入激活码');
}
}else{
$this->result(2,'验证码错误');
}
}
}
public function actionNoticeList(){
if(YII::$app->request->isPost){
$data['page']=Yii::$app->request->get('page');
$url=Yii::$app->params['domain_url'].'/admin/news/getnotice';
$result=httpRequest($url);
if($result){
$this->result(1,'成功',$result);
}else{
$this->result(2,'网络异常,请稍后再试');
}
}
$this->result(2,'请求异常');
}
}