<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
$url = substr($url,0,-9);
$installurl= $url.'index.php/install/';
define("install_path",$url);
define("install_url",$installurl);
class Install extends CI_Controller {

	function __construct(){
		parent::__construct();
        	$this->load->helper('url');
		$this->load->model('CsdjSkins');
                $this->load->helper('file');
	}

	public function index()
	{
            if(file_exists(FCPATH.'csdj/install/install.lock')){
                $data['install']='ok';
            }else{
                $data['install']='no';
            }
            $this->load->view('install/temp_1',$data);
        }

	public function save1()
	{
            $data='';
            if(file_exists(FCPATH.'csdj/install/install.lock')){
                $data['install']='ok';
                $this->load->view('install/temp_1',$data);
            }else{
                $this->load->view('install/temp_2',$data);
            }
        }

	public function save2()
	{
            $data='';
            if(file_exists(FCPATH.'csdj/install/install.lock')){
                $data['install']='ok';
                $this->load->view('install/temp_1',$data);
            }else{
                $this->load->view('install/temp_3',$data);
            }
        }

	public function save3()
	{
            $data='';
            if(file_exists(FCPATH.'csdj/install/install.lock')){
                $data['install']='ok';
                $this->load->view('install/temp_1',$data);
            }else{
 	        $dbhost = $this->input->post('dbhost');
 	        $dbuser = $this->input->post('dbuser');
 	        $dbpwd = $this->input->post('dbpwd');
 	        $dbname = $this->input->post('dbname');
 	        $dbprefix = $this->input->post('dbprefix');

                if(empty($dbhost)||empty($dbuser)||empty($dbpwd)||empty($dbname)||empty($dbprefix)) $this->CsdjSkins->Msg_url('<font color=red>请把数据填写完整！</font>','javascript:history.back();');

                $lnk=@mysql_connect($dbhost,$dbuser,$dbpwd);
                if(!$lnk) {
                         $data['errors']="抱歉，数据库连接失败，请检查数据库相关信息1！";
                         $this->load->view('install/temp_4',$data);
                }else{
                   if(!@mysql_select_db($dbname)){
                        if(!@mysql_query("CREATE DATABASE `".$dbname."`")){
                             $data['errors']="抱歉，".$dbname."数据库无权限创建，请先手工创建".$dbname."数据库后，再执行安装程序!！";
                             $this->load->view('install/temp_4',$data);
                        }
                   }
                   if(mysql_select_db($dbname) && empty($data['errors'])){
                        $this->load->helper('string');
                        $CS_Encryption_Key='cscms_'.random_string('alnum',10);
                        //修改数据库配置文件
	                $config=read_file("./csdj/lib/Cs_DB.php");
	                $config=preg_replace('/"CS_Sqlserver","(.*?)"/','"CS_Sqlserver","'.$dbhost.'"',$config);
	                $config=preg_replace('/"CS_Sqlname","(.*?)"/','"CS_Sqlname","'.$dbname.'"',$config);
	                $config=preg_replace('/"CS_Sqluid","(.*?)"/','"CS_Sqluid","'.$dbuser.'"',$config);
	                $config=preg_replace('/"CS_Sqlpwd","(.*?)"/','"CS_Sqlpwd","'.$dbpwd.'"',$config);
	                $config=preg_replace('/"CS_SqlPrefix","(.*?)"/','"CS_SqlPrefix","'.$dbprefix.'"',$config);
	                $config=preg_replace('/"CS_Encryption_Key","(.*?)"/','"CS_Encryption_Key","'.$CS_Encryption_Key.'"',$config);
	                if(!write_file('./csdj/lib/Cs_DB.php', $config)){
                                 $this->CsdjSkins->Msg_url('<font color=red>文件./csdj/lib/Cs_DB.php，没有写入权限！</font>','javascript:history.back();');
                                 exit();
                        }
	                @mysql_select_db($dbname,$lnk);
	                @mysql_query("SET NAMES gbk", $lnk);

                        //导入数据表字段
	                $sql=read_file("./csdj/install/cscms_table.sql");
                        $sql=str_replace('{Prefix}',$dbprefix,$sql);
	                $sqlarr=explode(";",$sql);
                        $str="";
	                for($i=1;$i<count($sqlarr);$i++){
                                $datasql=explode("--",$sqlarr[$i]);
                                $sql=explode("<cscms>",$sqlarr[$i]);
		                @mysql_query($sql[1], $lnk);
                                $str.=@$datasql[1];
	                }

                        //导入默认数据
	                $sql=read_file("./csdj/install/cscms_data.sql");
                        $sql=str_replace('{Prefix}',$dbprefix,$sql);
	                $sqlarr=explode("#cscms#",$sql);
	                for($i=0;$i<count($sqlarr);$i++){
		                @mysql_query($sqlarr[$i], $lnk);
	                }
                   
                        $data['str']=str_replace('cs_',$dbprefix,$str);
                        $this->load->view('install/temp_5',$data);
                   }
                }
            }
        }

	public function save4()
	{
            $data='';
            if(file_exists(FCPATH.'csdj/install/install.lock')){
                $data['install']='ok';
                $this->load->view('install/temp_1',$data);
            }else{
                $path=str_replace('index.php/install/save4','',$_SERVER['PHP_SELF']);
                $path=str_replace('index.php','',$path);
                $path=str_replace('install/save4','',$path);
                $data['web_path']=$path;
                $this->load->view('install/temp_6',$data);
            }
        }

	public function save5()
	{
            $data='';
            if(file_exists(FCPATH.'csdj/install/install.lock')){
                $data['install']='ok';
                $this->load->view('install/temp_1',$data);
            }else{
 	        $web_name = $this->input->post('web_name');
 	        $web_url = $this->input->post('web_url');
 	        $web_path = $this->input->post('web_path');
 	        $web_mode = $this->input->post('web_mode');
 	        $admin_name = $this->input->post('admin_name');
 	        $admin_pass = $this->input->post('admin_pass');
 	        $admin_code = $this->input->post('admin_code');

                if(empty($web_name)||empty($web_url)||empty($web_path)||empty($admin_name)||empty($admin_pass)||empty($admin_code)) $this->CsdjSkins->Msg_url('<font color=red>请把数据填写完整！</font>','javascript:history.back();');

                        //修改配置文件
	                $config=read_file("./csdj/lib/Cs_Config.php");
	                $config=preg_replace('/"Web_Name","(.*?)"/','"Web_Name","'.$web_name.'"',$config);
	                $config=preg_replace('/"Web_Url","(.*?)"/','"Web_Url","'.$web_url.'"',$config);
	                $config=preg_replace('/"Web_Path","(.*?)"/','"Web_Path","'.$web_path.'"',$config);
	                $config=preg_replace('/"Web_Mode",(.*?)\)/','"Web_Mode",'.$web_mode.')',$config);
	                $config=preg_replace('/"Admin_Code","(.*?)"/','"Admin_Code","'.$admin_code.'"',$config);
	                if(!write_file('./csdj/lib/Cs_Config.php', $config)){
                                 $this->CsdjSkins->Msg_url('<font color=red>文件./csdj/lib/Cs_Config.php，没有写入权限！</font>','javascript:history.back();');
                                 exit();
                        }
                        //写入管理员
		        $this->load->model('CsdjDB');
                        $data['CS_AdminName']=$admin_name;
                        $data['CS_AdminPass']=md5($admin_pass);
                        $data['CS_Quanx']="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50";

                        $this->CsdjDB->get_insert('admin',$data);

	         if(!write_file('./csdj/install/install.lock', 'cscms3.5')){
                      $this->CsdjSkins->Msg_url('<font color=red>目录./csdj/，没有写入权限！</font>','javascript:history.back();');
                      exit();
                 }

                $this->load->view('install/temp_7');
            }
        }
}

