<?php namespace myretail\controllers;use Yii;use yii\web\Controller;use myretail\models\SyncConfig;class UpdateController extends Controller{public function actionIndex(){return $this->render('index');}public function updateDb($version){$db=Yii::$app->db;$dsn=$db->dsn;$dsn=explode(';',$dsn);$dbhost=$dsn[0];$dbhost=explode('=',$dbhost);$dbhost=$dbhost[1];$dbname=$dsn[1];$dbname=explode('=',$dbname);$dbname=$dbname[1];$dbuser=$db->username;$dbpass=$db->password;date_default_timezone_set("Asia/Jakarta");$conn=mysqli_connect($dbhost,$dbuser,$dbpass);if($conn->connect_error){die("Connection failed: ".$conn->connect_error);}$conn->close();$sql="USE `$dbname`; ";$sql.=file_get_contents('../sql/update'.$version.'.sql');Yii::$app->db->createCommand($sql)->execute();}public function actionUpdate(){$success=false;$data=$_POST['data'];$result=file_put_contents('../temp/temp.zip',fopen($data['zip_url'],'r'),LOCK_EX);if($result!==false){$zip=new \ZipArchive();if($zip->open('../temp/temp.zip')===true){$address=__DIR__.'/../../';$zip->extractTo($address);$zip->close();$success=true;}}if($data['changed_database']){self::updateDb($data['version']);}$file='../config/params-local.php';$fileContent=file_get_contents($file);$newVersion=$data['version'];$version=explode("'appVersion' => ",$fileContent);$version=explode(",",$version[1]);$version=$version[0];$version=trim($version,"'");$fileContent=str_replace("'appVersion' => '$version'","'appVersion' => '$newVersion'",$fileContent);file_put_contents($file,$fileContent);if($success)return 'success';else return 'fail';}}