<?
error_reporting(E_ALL & ~E_NOTICE);
if (!file_exists('../file/install.phpleft')){
define('ROOT_PHPLEFT', true);
header("Content-Type:text/html;charset=gbk");
$file_dir = dirname(__FILE__);
$urls =  str_replace('install/index.php','','http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
$action = $_GET['action'];
$action = $action?$action:'0';
if ($action=='2'){
$host = $_POST['host'];
$user = $_POST['user'];
$pwd = $_POST['pwd'];
$data_base = $_POST['data_base'];
$file_name = 'company.sql';
$conn = mysql_connect($host,$user,$pwd);
if (!$conn)
  {
  die('MYSQL错误无法连接: ' . mysql_error());
  exit();
}else{
mysql_query("CREATE DATABASE ".$data_base,$conn);
$result = mysql_query("show table status from $data_base",$conn);
while($data=mysql_fetch_array($result)) {
if(strpos('*'.$data[0],$_POST['qz']) > 0){
mysql_query("drop table $data[0]");
}
}
mysql_query('SET NAMES gb2312');
mysql_select_db($data_base,$conn);
$get_sql_data = file_get_contents($file_name,$file_dir);
$get_sql_data = str_replace('phpleft_',$_POST['qz'],$get_sql_data);
$explode = explode(";",$get_sql_data);
$cnt = count($explode);
for($i=0;$i<$cnt;$i++){
    $sql = $explode[$i];
    $result = mysql_query($sql);
}
include '../include/global.func.php';	
$path = '../config.inc.php';
$html = file_get($path);
$config['db_host'] = $host;
$config['db_user'] = $user;
$config['db_pass'] = $pwd;
$config['db_name'] = $data_base;
$config['url'] = $_POST['url'];
$config['tb_pre'] = $_POST['qz'];
foreach($config as $k=>$v)	{
			$html = preg_replace("/[$]PL_CFG\['$k'\]\s*\=\s*[\"'].*?[\"']/is", "\$PL_CFG['$k'] = '$v'", $html);
}
MakeHtmlFile($path, $html, 1);
MakeHtmlFile('../file/install.phpleft', '$install = 1;', 1);
  }
}
include 'install.php';
}else{
echo  '<h1 style="font-size:18px; color:#FF0000">已经安装过一次,请删除file目录下的install.phpleft 文件</h1>';	
}

?>