<?php
//函数是否存在
function HaveFun($fun){
	if(function_exists($fun))
	{
		$word="支持";
	}
	else
	{
		$word="不支持";
	}
	return $word;
}
//返回符号
function ReturnResult($st){
	if($st==1)
	{
		$w="√";
	}
	elseif($st==2)
	{
		$w="---";
	}
	else
	{
		$w="<font color=red>×</font>";
	}
	return $w;
}
//取得php版本
function GetPhpVer(){
	$r['ver']=PHP_VERSION;
	if($r['ver'])
	{
		$r['result']=($r['ver']<"4.2.3")?ReturnResult(0):ReturnResult(1);
	}
	else
	{
		$r['ver']="---";
		$r['result']=ReturnResult(2);
	}
	return $r;
}
//取得php运行模式
function GetPhpMod(){
	$mod=strtoupper(php_sapi_name());
	if(empty($mod))
	{
		$mod="---";
	}
	return $mod;
}
//是否运行于安全模式
function GetPhpSafemod(){
	$phpsafemod=get_cfg_var("safe_mode");
	if($phpsafemod==1)
	{
		$r['word']="是";
		$r['result']=ReturnResult(0);
	}
	else
	{
		$r['word']="否";
		$r['result']=ReturnResult(1);
	}
	return $r;
}
//是否支持mysql
function CanMysql(){
	$r['can']=HaveFun("mysql_connect");
	$r['result']=$r[can]=="支持"?ReturnResult(1):ReturnResult(0);
	return $r;
}
//取得mysql版本
function GetMysqlVer(){
	$r['ver']=@mysql_get_server_info();
	if(empty($r['ver']))
	{
		$r['ver']="---";
		$r['result']=ReturnResult(2);
	}
	else
	{
		$r['result']=ReturnResult(1);
	}
	return $r;
}
//取得操作系统
function GetUseSys(){
	$phpos=explode(" ",php_uname());
	$sys=$phpos[0]."&nbsp;".$phpos[1];
	if(empty($phpos[0]))
	{
	$sys="---";
	}
	return $sys;
}
//是否支持zend
function GetZend(){
	@ob_start();
	@include("data/zend.php");
	$string=@ob_get_contents();
	@ob_end_clean();
	if($string=="www.phome.net"||strstr($string,"bytes in"))
	{
		$r['word']="支持";
		$r['result']=ReturnResult(1);
	}
	else
	{
		$r['word']="不支持";
		$r['result']=ReturnResult(0);
	}
	return $r;
}
//是否支持采集
function GetCj(){
	$cj=get_cfg_var("allow_url_fopen");
	if($cj==1)
	{
		$r['word']="支持";
		$r['result']=ReturnResult(1);
	}
	else
	{
		$r['word']="不支持";
		$r['result']=ReturnResult(0);
	}
	return $r;
}
//测试采集
function TestCj(){
	$r=@file("http://www.163.com");
	if($r[5])
	{
		echo"<br>测试结果：<b>支持采集</b>";
	}
	else
	{
		echo"<br>测试结果：<b>不支持采集</b>";
	}
	exit();
}
//是否支持gd库
function GetGd(){
	$r['can']=HaveFun("gd_info");
	$r['result']=$r[can]=="支持"?ReturnResult(1):ReturnResult(0);
	return $r;
}
//是否支持ICONV库
function GetIconv(){
	$r['can']=HaveFun("iconv");
	$r['result']=$r[can]=="支持"?ReturnResult(1):ReturnResult(0);
	return $r;
}

//提示信息
function InstallShowMsg($msg){
	echo"<script>alert('".$msg."');history.go(-1);</script>";
	exit();
}
//返回目录权限结果
function ReturnPathLevelResult($path){
	$testfile=$path."/test.test";
	$fp=@fopen($testfile,"wb");
	if($fp)
	{
		@fclose($fp);
		@unlink($testfile);
		return 1;
	}
	else
	{
		return 0;
	}
}
//返回文件权限结果
function ReturnFileLevelResult($filename){
	return is_writable($filename);
}
//检测目录权限
function CheckFileMod($filename,$smallfile=""){
	$succ="√";
	$error="<font color=red>×</font>";
	if(!file_exists($filename)||($smallfile&&!file_exists($smallfile)))
	{
		return $error;
	}
	if(is_dir($filename))//目录
	{
		if(!ReturnPathLevelResult($filename))
		{
			return $error;
		}
		//子目录
		if($smallfile)
		{
			if(is_dir($smallfile))
			{
				if(!ReturnPathLevelResult($smallfile))
				{
					return $error;
				}
			}
			else//文件
			{
				if(!ReturnFileLevelResult($smallfile))
				{
					return $error;
				}
			}
		}
	}
	else//文件
	{
		if(!ReturnFileLevelResult($filename))
		{
			return $error;
		}
		if($smallfile)
		{
			if(!ReturnFileLevelResult($smallfile))
			{
				return $error;
			}
		}
	}
	return $succ;
}
//建表
function DoCreateTable($sql,$mysqlver,$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).
		($mysqlver>='4.1'?" ENGINE=$type DEFAULT CHARSET=$dbcharset":" TYPE=$type");
}
//运行SQL
function DoRunQuery($sql,$mydbchar,$mydbtbpre,$mydbver){
	$sql=str_replace("\r","\n",str_replace(' `phome_',' `'.$mydbtbpre,$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;
		}
		$num++;
	}
	unset($sql);
	foreach($ret as $query)
	{
		$query=trim($query);
		if($query)
		{
			if(substr($query,0,12)=='CREATE TABLE')
			{
				$name=preg_replace("/CREATE TABLE `([a-z0-9_]+)` .*/is","\\1",$query);
				echo"建立数据表: <b>".$name."</b> 完毕......<br>";
				mysql_query(DoCreateTable($query,$mydbver,$mydbchar)) or die(mysql_error()."<br>".$query);
			}
			else
			{
				mysql_query($query) or die(mysql_error()."<br>".$query);
			}
		}
	}
}
//初使化管理员
function FirstAdmin($add){
	if(!trim($add['username'])||!trim($add['password']))
	{
		InstallShowMsg('请输入管理员用户名与密码');
	}
	if($add['password']!=$add['repassword'])
	{
		InstallShowMsg('两次输入的密码不一致，请重新输入');
	}
	//链接数据库
	@include("../class/config.php");
	$link=InstallConnectDb($phome_use_dbver,$phome_db_server,$phome_db_port,$phome_db_username,$phome_db_password,$phome_db_dbname,$phome_db_char,$phome_db_dbchar);
	$sql=mysql_query("INSERT INTO `".$dbtbpre."enewsuser`(userid,username,password,rnd,adminclass,groupid,checked,styleid) VALUES (1,'$add[username]','".md5($add['password'])."','nw37Zgfdsfdsa','',1,0,2);");
	mysql_close();
	if($sql)
	{
		echo"初始化管理员账号完毕!<script>self.location.href='index.php?enews=success&f=6';</script>";
		exit();
	}
	else
	{
		InstallShowMsg('初使化管理员不成功，意外出错，请重新安装一次.');
	}
}
//链接数据库
function InstallConnectDb($phome_use_dbver,$phome_db_server,$phome_db_port,$phome_db_username,$phome_db_password,$phome_db_dbname,$phome_db_char,$phome_db_dbchar){
	$dblocalhost=$phome_db_server;
	//端口
	if($phome_db_port)
	{
		$dblocalhost.=":".$phome_db_port;
	}
	$link=@mysql_connect($dblocalhost,$phome_db_username,$phome_db_password);
	if(!$link)
	{
		InstallShowMsg('您的数据库用户名或密码有误，链接不上MYSQL数据库');
	}
	//编码
	if($phome_use_dbver>='4.1')
	{
		if($phome_db_char)
		{
			@mysql_query("SET names '".$phome_db_char."';");
		}
		if($phome_use_dbver>='5.0')
		{
			@mysql_query("SET sql_mode=''");
		}
	}
	$db=@mysql_select_db($phome_db_dbname);
	//数据库不存在
	if(!$db)
	{
		if($phome_use_dbver>='4.1')
		{
			$createdb=@mysql_query("CREATE DATABASE IF NOT EXISTS ".$phome_db_dbname." DEFAULT CHARACTER SET ".$phome_db_dbchar);
		}
		else
		{
			$createdb=@mysql_query("CREATE DATABASE IF NOT EXISTS ".$phome_db_dbname);
		}
		if(!$createdb)
		{
			InstallShowMsg('您输入的数据库不存在，并且您输入的用户权限不足以建立新的数据库');
		}
		@mysql_select_db($phome_db_dbname);
	}
	return $link;
}
//配置数据库
function SetDb($add){
	if(!$add['mydbver']||!$add['mydbhost']||!$add['mydbname']||!$add['mydbtbpre']||!$add['mycookievarpre'])
	{
		InstallShowMsg('带*项不能为空');
	}
	//链接数据库
	$link=InstallConnectDb($add['mydbver'],$add['mydbhost'],$add['mydbport'],$add['mydbusername'],$add['mydbpassword'],$add['mydbname'],$add['mysetchar'],$add['mydbchar']);
	//初使化网站信息
	$siteurl=ReturnEcmsSiteUrl();
	//配置文件
	RepEcmsConfig($add,$siteurl);
	//执行SQL语句
	DoRunQuery(ReturnInstallSql(),$add['mydbchar'],$add['mydbtbpre'],$add['mydbver']);
	@mysql_query("update ".$add['mydbtbpre']."enewspublic set newsurl='$siteurl',siteurl='$siteurl',fileurl='".$siteurl."d/file/' limit 1");
	@mysql_close();
	echo"配置数据库完毕!<script>self.location.href='index.php?enews=firstadmin&f=5';</script>";
	exit();
}
//处理配置文件
function RepEcmsConfig($add,$siteurl){
	//初使化配置文件
	$fp=@fopen("data/config.php","r");
	if(!$fp)
	{
		InstallShowMsg('请检查 /e/install/data/config.php 文件是否存在!');
	}
	$data=@fread($fp,filesize("data/config.php"));
	fclose($fp);
	$data=str_replace('<!--dbtype.phome.net-->',$add['mydbtype'],$data);
	$data=str_replace('<!--dbver.phome.net-->',$add['mydbver'],$data);
	$data=str_replace('<!--host.phome.net-->',$add['mydbhost'],$data);
	$data=str_replace('<!--port.phome.net-->',$add['mydbport'],$data);
	$data=str_replace('<!--username.phome.net-->',$add['mydbusername'],$data);
	$data=str_replace('<!--password.phome.net-->',$add['mydbpassword'],$data);
	$data=str_replace('<!--name.phome.net-->',$add['mydbname'],$data);
	$data=str_replace('<!--char.phome.net-->',$add['mysetchar'],$data);
	$data=str_replace('<!--dbchar.phome.net-->',$add['mydbchar'],$data);
	$data=str_replace('<!--tbpre.phome.net-->',$add['mydbtbpre'],$data);
	$data=str_replace('<!--cookiepre.phome.net-->',$add['mycookievarpre'],$data);
	$data=str_replace('<!--ecms.siteurl-->',$siteurl,$data);
	$data=str_replace('<!--ecms.newsurl-->',$siteurl,$data);
	$data=str_replace('<!--ecms.fileurl-->',$siteurl."d/file/",$data);
	//写入配置文件
	$fp1=@fopen("../class/config.php","w");
	if(!$fp1)
	{
		InstallShowMsg(' /e/class/config.php 文件权限没有设为0777，配置数据库不成功');
	}
	@fputs($fp1,$data);
	@fclose($fp1);
}
//返回SQL语句
function ReturnInstallSql(){
	$sqlfile="data/empirecms.sql";
	$fp=fopen($sqlfile,'r');
	$sql=fread($fp,filesize($sqlfile));
	fclose($fp);
	if(empty($sql))
	{
		InstallShowMsg(' /e/install/data/empirecms.sql 文件丢失,安装不成功');
	}
	return $sql;
}
//取得网站地址
function ReturnEcmsSiteUrl(){
	return str_replace('e/install/index.php','',$_SERVER['PHP_SELF']);
}
?>