<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* 帐户控制器
*/
class Install extends MY_Controller {
public function __construct() {
parent::__construct();
if(file_exists("./data/install.lock")){
header("location:/");
}
// $this->load->model('party_model', 'party');
$this->load->model('common_model', 'common');
}
//首页
public function index() {
$this->display('install/index');
}
public function step4(){
if($_POST){
//创建数据库
$dbconfig['DB_TYPE']="mysql";
$dbhost=$dbconfig['DB_HOST']=$_POST['dbhost'];
$dbuser=$dbconfig['DB_USER']=$_POST['dbuser'];
$dbpw=$dbconfig['DB_PWD']=$_POST['dbpw'];
$dbname=$dbconfig['dbname']=strtolower($_POST['dbname']);
$data='<?php if ( ! defined(\'BASEPATH\')) exit(\'No direct script access allowed\');
$active_group = \'default\';
$active_record = TRUE;
//DB
$db[\'default\'][\'hostname\'] = \''.$dbhost.'\';
$db[\'default\'][\'port\'] = \'3306\';
$db[\'default\'][\'username\'] = \''.$dbuser.'\';
$db[\'default\'][\'password\'] = \''.$dbpw.'\';
$db[\'default\'][\'database\'] = \''.$dbname.'\';
$db[\'default\'][\'dbdriver\'] = \'mysql\';
$db[\'default\'][\'dbprefix\'] = \'\';
$db[\'default\'][\'pconnect\'] = TRUE;
$db[\'default\'][\'db_debug\'] = FALSE;
$db[\'default\'][\'cache_on\'] = FALSE;
$db[\'default\'][\'cachedir\'] = \'\';
$db[\'default\'][\'char_set\'] = \'utf8\';
$db[\'default\'][\'dbcollat\'] = \'utf8_general_ci\';
$db[\'default\'][\'swap_pre\'] = \'\';
$db[\'default\'][\'autoinit\'] = TRUE;
$db[\'default\'][\'stricton\'] = FALSE;
';
$file_path=APP."/application/config/database.php";
file_put_contents($file_path, $data);
// die;
// $con=@mysql_connect($dbconfig['DB_HOST'],$dbconfig['DB_USER'],$dbconfig['DB_PWD']);
// $sql = "CREATE DATABASE IF NOT EXISTS `{$dbname}` DEFAULT CHARACTER SET utf8";
// mysql_query($sql);
//
echo "<pre>";var_dump($res);exit;
//
// $this->sp_execute_sql($dbconfig,"ych3.sql");
$_SESSION['_install_step']=4;
//
// sleep(1);
go2("congratulations,you has success!","/install/step5");
}else{
exit;
}
}
function sp_execute_sql($dbconfig,$file){
$con=@mysql_connect($dbconfig['DB_HOST'],$dbconfig['DB_USER'],$dbconfig['DB_PWD']);
mysql_select_db($dbconfig['dbname'],$con);
//读取SQL文件
$sql = file_get_contents(WEBROOT . '/data/'.$file);
$sql = str_replace("\r", "\n", $sql);
$sql = explode(";\n", $sql);
//echo "<pre>";var_dump($sql);exit;
//替换表前缀
// $default_tablepre = "cmf_";
// $sql = str_replace(" `{$default_tablepre}", " `{$tablepre}", $sql);
//开始安装
// sp_show_msg('开始安装数据库...');
foreach ($sql as $item) {
$item = trim($item);
if(empty($item)) continue;
@preg_match('/CREATE TABLE `([^ ]*)`/', $item, $matches);
mysql_query($item);
// if($matches) {
// $table_name = $matches[1];
// $msg = "创建数据表{$table_name}";
// if(false !== mysql_query($item)){
// sp_show_msg($msg . ' 完成');
// } else {
// sp_show_msg($msg . ' 失败!', 'error');
// }
// } else {
// mysql_query($item);
$db->execute($item);
//
// }
}
mysql_close($con);
}
public function step5(){
if($_SESSION['_install_step']==4){
@touch('./data/install.lock');
// $this->view('install/step5');
$this->display('install/step5');
}else{
echo "<script>alert('非法安装!');history.go(-1);</script>";
// history_back();
// $this->error("非法安装!");
}
}
public function testdbpwd()
{
if ($_POST) {
$dbconfig = I("POST.");
$dbconfig['DB_TYPE'] = "mysql";
$db = Db::getInstance($dbconfig);
try {
$db->query("show databases;");
} catch (\Exception $e) {
die("");
}
exit("1");
} else {
exit("need post!");
}
}
}