<?php
/*
秀影自动安装向导程序。页面仿Discuz安装界面
系统版本5.0
*/
@error_reporting(E_ALL ^ E_NOTICE);
@ini_set("display_errors",On);
@ini_set('memory_limit', '128M');
ini_set("register_globals", 0);
ini_set("magic_quotes_gpc" , 0);
set_magic_quotes_runtime(0);
define("ROOT",dirname(__FILE__).'/');
set_include_path(ROOT.PATH_SEPARATOR.ROOT.'Library'.PATH_SEPARATOR.
ROOT.'Application/Controllers'.PATH_SEPARATOR.
ROOT.'Application/Models/'.PATH_SEPARATOR.get_include_path());
header("Content-Type:text/html;charset=gb2312");
$mod = trim($_GET['mod']) ? trim($_GET['mod']) : "setup_1";
function dir_writeable($dir) {
	$dir = ROOT.$dir;
	if(!is_dir($dir)) {
		@mkdir($dir, 0777);
	}
	if(is_dir($dir)) {
		if($fp = @fopen("$dir/test.txt", 'w')) {
			@fclose($fp);
			@unlink("$dir/test.txt");
			$writeable = "可写";
		} else {
			$writeable = "不可写";
		}
	}else{
		$writeable = is_writable($dir) ? "可写" : "不可写";
	}
	return $writeable;
}

function runquery($sql) {
	global $database;

	$sql = str_replace("\r", "\n", str_replace('vodcms_', ''.$database['tblpre'], $sql));
	$ret = array();
	$num = 0;
	foreach(explode(";\n", trim($sql)) as $query) {
		$queries = explode("\n", trim($query));
		foreach($queries as $query) {
			$ret[$num] .= $query[0] == '#' || $query[0].$query[1] == '--' ? '' : $query;
			//echo $query."<br>";
		}
		$num++;
	}
	unset($sql);

	foreach($ret as $query) {
		$query = trim($query);
		if($query) {
			if(substr($query, 0, 12) == 'CREATE TABLE') {
				preg_match("/CREATE TABLE `(.+?)`/is",$query,$txt);
				$name = $txt[1];
				echo "<script language='javascript'>showmessage('建立表>>$name ... 成功')</script>\n";
				echo '<script>parent.window.scrollTo(0,document.body.scrollHeight);</script>';
				mysql_query(createtable($query, $database['dbchar'])) or die("建立表失败!".mysql_error());
				
			} else {
				mysql_query($query);//echo $query.'<br>';
			}
		}
	}
}
function createtable($sql, $dbcharset) {
	$type = strtoupper(preg_replace("/^\s*CREATE TABLE\s+.+\s+\(.+?\).*(ENGINE|TYPE)\s*=\s*([a-z]+?).*$/isU", "\\2", $sql));
	$type = in_array($type, array('MYISAM', 'HEAP')) ? $type : 'MYISAM';
	return preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql).
		(mysql_get_server_info() > '4.1' ? " ENGINE=$type DEFAULT CHARSET=$dbcharset" : " TYPE=$type");
}
function createdatebase($dbname,$dbcharset)
{
	if(mysql_get_server_info() > '4.1') {
			mysql_query("CREATE DATABASE $dbname DEFAULT CHARACTER SET $dbcharset");
		} else {
			exit('您的数据库版本太低!');
		}
}
function CheckZend(){
	global $q;
	if(defined('OPTIMIZER_VERSION')){
		echo "您机器的版本为:<font color=red>".OPTIMIZER_VERSION."</font>";
		if (OPTIMIZER_VERSION<3.0){
			$q=1;
			echo "<br><a href=http://www.vodcms.com/soft/ZendOptimizer-3.2.2-Windows-i386.exe>您的系统zend optimizer版本过低！系统要求最低版本为3.0.点击这里下载新版本</a>";
		}
	}else{
		echo "<a href=http://www.vodcms.com/soft/ZendOptimizer-3.2.2-Windows-i386.exe>您的系统没有安装zend optimizer。点击这里下载安装</a>";
	}
}
function chkfunction($str)
{
	global $q;
	if(function_exists($str))
	{
		return "支持";
	}else{
		$q=1;
		return "<font color=red><b>不支持</b></font>";
		
	}
}
$dbcharset="GBK";
$os = PHP_OS;
$maxsize = @ini_get(upload_max_filesize);
$maxsize = $maxsize ? "最大允许传".$maxsize : "不允许";
if (function_exists("gd_info"))
{
	$gd = @gd_info();
}
switch($mod)
{
	case "setup_4":
		$error = false;
		$array = null;
		$array['dbhost'] = trim($_POST['dbhost']);
		$array['dbname'] = trim($_POST['dbname']);
		$array['dbuser'] = trim($_POST['dbuser']);
		$array['dbpass'] = trim($_POST['dbpass']);
		$array['tblpre'] = trim($_POST['tblpre']);
		if (! ($conn = @mysql_connect($array['dbhost'],$array['dbuser'], $array['dbpass'])))
		{
			$error = mysql_error();
		}else{
			$db_list = mysql_query("SHOW DATABASES");
			while ($row = mysql_fetch_array($db_list)) {
				$dbs[] = $row[0];
			}
			if (in_array($array['dbname'], $dbs)===false)
			{
				if (!mysql_query("CREATE DATABASE `".$array['dbname']."`"))
				{
					$error = "创建数据库失败!您可能没有权限!".mysql_error();
				}
				//echo 'd';
			}
			if ( !@mysql_select_db($array['dbname'], $conn))
			{
				$error.= mysql_error();
			}else{
				$filepath = ROOT.'Config/database.inc.php';
				$text	=file_get_contents($filepath);
				foreach ($array as $key=>$value){
					$text = preg_replace("/[$]database\[\'$key\'](\s+)\=(.+?);/$is",
					"\$database['$key']$1= '".$value."';",$text);
				}
				$fp = fopen($filepath, "w");
				if (fwrite($fp, $text)===false)
				{
					$error = "写入配置文件".$filepath."失败";
				}
				unset($text);
				
			}
		}
		if ($error)
		{
			$mod = "setup_3";
		}
		$result = @mysql_query("SHOW TABLES FROM ".$array['dbname']);
		while($rs = @mysql_fetch_array($result))
		{
			$tablearray[] = $rs[0];
		}
		if (is_array($tablearray)){
			if (array_search($array['tblpre']."admin", $tablearray)!==false)
			{
				$error = "<li><font color=red>系统已经被安装过秀影vodcms系统!继续安装会清空已有数据!</font></li>";
				$errorjs = "onclick=\"return confirm('系统已经安装过vodcms系统!继续会清空已有数据!确认继续吗?')\"";
			}
		}
	break;
	case "setup_5":
		$error = null;
		$username = trim($_POST['username']);
		$password1 = md5(strtolower(trim($_POST['password1'])));
		$password2= md5(strtolower(trim($_POST['password2'])));
		if ($password1 != $password2)
		{
			$error = "两次密码输入不一致!";
		}
		if ($error)
		{
			$mod = "setup_4";
		}
	break;
}
if (version_compare(PHP_VERSION,"5.0.0")==-1) {
	echo '您的php版本太低!请安装php5.0';
	exit;
}
if (file_exists(ROOT."Config/install.lock")){
	$mod = "exit";
}
?><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>VODCMS Installation Wizard 6.0 简体中文版</title>
<link rel="stylesheet" type="text/css" id="css" href="install/style.css">
</head>
<body bgcolor="#3A4273" text="#000000">
<table width="95%" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" align="center">
  <tr>
    <td><?php require_once("install/$mod.php");?></td>
  </tr>
</table>
