<?php
//2011-1-2 
@ini_set('display_errors','0');
@error_reporting (E_ALL & ~E_NOTICE & ~E_WARNING);
@set_time_limit(0);
if(PHP_VERSION<'5.0')
{
	@set_magic_quotes_runtime(0);
}
else
{
	@ini_set('magic_quotes_runtime',0);
}
header("Content-type:text/html;charset=gbk");
PHP_VERSION<'5.0' && exit('<script languge="javascript" charset="utf-8">alert("您的PHP版本(现有版本:'.PHP_VERSION.')过低，无法安装Dircms!\n\n升级PHP版本或者登录http://bbs.dircms.net寻求帮助!");</script>');
define(DIRCMS_ROOT, substr(str_replace('\\','/',dirname(__FILE__)),0,-7));
if(file_exists(DIRCMS_ROOT.'data/dircms.lock') &&  $step!='installok')exit('您已经安装过DIRCMS,如果需要重新安装，请删除 ./data/dircms.lock 文件!');

include(DIRCMS_ROOT.'config.inc.php');
set_include_path(DIRCMS_ROOT.'include/');
@extract($_REQUEST);
include('global.func.php');
include('admin/global.func.php');
include('mysql.class.php');
require_once(DIRCMS_ROOT.'include/cache.class.php');
require_once(DIRCMS_ROOT.'include/version.inc.php');
if($step)
{
	switch($step)
	{
		case '2':
			include(DIRCMS_ROOT.'data/cache/common.cache.php');
			
			$PHP_SELF=isset($_SERVER['PHP_SELF'])?$_SERVER['PHP_SELF']:(isset($_SERVER['SCRIPT_NAME'])?$_SERVER['SCRIPT_NAME']:$_SERVER['ORIG_PATH_INFO']);
			$config['dircms_path']=str_replace("\\","/",dirname($PHP_SELF));
			$config['dircms_path']=substr($config['dircms_path'],0,-8);
			$config['dircms_path']=strlen($config['dircms_path'])>1?$config['dircms_path']."/" : "/";
			if(!defined('DIRCMS_PATH'))define('DIRCMS_PATH',$config['dircms_path']);
			set_config($config);
			unset($DIRCMS['db_host'],$DIRCMS['db_user'],$DIRCMS['db_psw'],$DIRCMS['db_name'],$DIRCMS['db_pre']);
			$config=$DIRCMS;
			include('template/install.step'.$step.'.tlp.php');
			break;
		case '3':
			$gd_enabled=extension_loaded('gd')&&function_exists('imagecreate')?'√支持GD库':'×不支持GD库';
			$gzip_enabled=extension_loaded('zlib') && function_exists('ob_gzhandler')?'√支持Gzip压缩':'×不支持Gzip压缩';
			$mysql_enabled=extension_loaded('mysql') &&function_exists('mysql_connect')?'√支持Mysql':'×不支持Mysql';
			$iconv_enabled=extension_loaded('iconv')?'√支持iconv拓展':'×不支持iconv拓展';
			include('template/install.step'.$step.'.tlp.php');
			break;
		case '4':
			$a=glob(DIRCMS_ROOT.'*');
			foreach($a as $t)
			{
				if(is_dir($t))$patharray[]=$t;
			}
			include('template/install.step'.$step.'.tlp.php');
			break;
		case '5':
			include('template/install.step'.$step.'.tlp.php');
			break;
		case '6':
			//网站配置文件

			include(DIRCMS_ROOT.'data/cache/common.cache.php');
			foreach($DIRCMS as $key=>$value)
			{
				if(!in_array($key,array('db_host','db_user','db_psw','db_name','db_pconnect')))$config[$key]=$value;
			}
			$config['password_key']=$password_key;
			$config['cookie_pre']=hash_str(10);
			$config['auth_key']=hash_str(20);
			$config['site_url']=getServerName().DIRCMS_PATH;

			$db=new mysql($config['db_host'],$config['db_user'],$config['db_psw'],$config['db_name'],$config['db_pconnect'],'gbk');
			$filepath=DIRCMS_ROOT.'install/sql/install.sql';
			
			if(!file_exists($filepath))$msg='安装文件已损坏，请重新下载!';
			else{
				$sqls=str_replace('#install_websiteurl#',$config['site_url'],file_get_contents($filepath));
				$sqls=explode(";\n",trim($sqls));
				foreach($sqls as $sql)
				{
					if($config['db_pre']!='dircms_')$sql=str_replace('dircms_',$config['db_pre'],$sql);
					$db->query($sql,true);
				}

				$timenow=time();

				$userid=$db->insert($config['db_pre'].'member',array('modelid'=>1,'vipstart'=>$timenow,'vipto'=>$timenow,'groupid'=>1,'areaid'=>0,'username'=>$username,'password'=>md5(md5($password_key).$password_key.md5($password)),'email'=>$email,'regtime'=>$timenow,'logintime'=>$timenow,'loginip'=>getIp(),'ispassed'=>1,'islocked'=>0));
				$db->insert($config['db_pre'].'member_cache',array('id'=>$userid,'modelid'=>1,'vipstart'=>$timenow,'vipto'=>$timenow,'groupid'=>1,'areaid'=>0,'username'=>$username,'password'=>md5(md5($password_key).$password_key.md5($password)),'email'=>$email,'regtime'=>$timenow,'logintime'=>$timenow,'loginip'=>getIp(),'ispassed'=>1,'islocked'=>0));
				$spaceurl=$config['site_url'].'space/index.php?id='.$userid;
				$companyurl=$config['site_url'].'company/company_'.$userid.'.html';
				$db->query("UPDATE `".$config['db_pre']."member` SET `".$config['db_pre']."member`.`spaceurl`='{$spaceurl}',`".$config['db_pre']."member`.`gradeid`=10,`".$config['db_pre']."member`.`companyurl`='{$companyurl}' WHERE `".$config['db_pre']."member`.`id`={$userid}",true);
				$db->query("UPDATE `".$config['db_pre']."member_cache` SET `".$config['db_pre']."member_cache`.`spaceurl`='{$spaceurl}',`".$config['db_pre']."member_cache`.`gradeid`=10,`".$config['db_pre']."member_cache`.`companyurl`='{$companyurl}' WHERE `".$config['db_pre']."member_cache`.`id`={$userid}",true);
				$db->insert($config['db_pre'].'admin',array('memberid'=>$userid,'username'=>$username,'allowmultilogin'=>0,'disabled'=>0,'roleid'=>1));
				$db->insert($config['db_pre'].'nomalmember',array('userid'=>$userid));
				$config['admin_founders']=$userid;
				$config['ishtml']=1;
				$config['tpl_name']=$config['tlp_name']='2010';
				set_config($config);
				if(!@file_put_contents(DIRCMS_ROOT.'data/dircms.lock',hash_str(10)))
				{
					$msg='<img src="template/images/err.gif" width="50" height="46" align="absmiddle" /> Dircms安装失败!请确保您网站目录的读写权限。更多技术支持请访问：<a href="http://bbs.dircms.net" target="_blank">http://bbs.dircms.net</a>';
				}
				else
				{
					@unlink(DIRCMS_ROOT.'data/dircms.lock');
					$msg='<img src="template/images/right.gif" width="50" height="46" align="absmiddle" /> Dircms即将安装完毕!全部完成后，请删除install/文件夹。<br /><input onClick="window.location.href=\'install.php?step=installok&action=manage\';" type="button" name="step" value="完成最后一步"style="background:url(template/images/bgbtn.gif);width:108px;height:27px;max-height:27px; line-height:27px;border:0px;cursor:pointer;font-size:13px;margin-top:1px; text-align:center; color:#FFF;"/>&nbsp;&nbsp;<input onClick="window.open(\'http://www.dircms.net/\');" type="button" name="step" value="获取技术支持"style="background:url(template/images/bgbtn.gif);width:108px;height:27px;max-height:27px; line-height:27px;border:0px;cursor:pointer;font-size:13px;margin-top:1px; text-align:center; color:#FFF;"/>';
				}
			}
			include('template/install.step'.$step.'.tlp.php');
			break;
		case 'installok':
			if(!is_writeable(DIRCMS_ROOT.'data/'))exit('安装失败!请将根目录以及about,company,data,html,upload目录设置为0777后重新安装.');
			include(DIRCMS_ROOT.'data/cache/common.cache.php');
			$db=new mysql(DB_HOST,DB_USER,DB_PSW,DB_NAME,DB_PRE,'gbk');
			$c=new cache();
			$c->cache_all(); 
			@file_put_contents(DIRCMS_ROOT.'data/dircms.lock',hash_str(10));
			if($action=='index')header('location:../');
			else header('location:../admin.php');
			exit();
			break;
		case 'checksubmit':
			if(!@mysql_connect($dbhost, $dbuser, $dbpw))
			{
				exit('2');
			}
			$server_info = mysql_get_server_info();
			if($server_info < '5.0') exit('6');
			if(!mysql_select_db($dbname))
			{
				if(!@mysql_query("CREATE DATABASE `$dbname`")) exit('3');
				mysql_select_db($dbname);
			}
			$tables = array();
			$query = mysql_list_tables($dbname);
			while($r = mysql_fetch_row($query))
			{
				$tables[] = $r[0];
			}
			if($tables && in_array($tablepre.'admin', $tables))
			{
				exit('0');
			}
			else
			{
				exit('1');
			}
			
			break;
	}
}
else
{
	include('template/install.tlp.php');
}

function getServerName() 
{
	$port=$_SERVER["SERVER_PORT"]=='80'?'':':'.$_SERVER["SERVER_PORT"];
	$ServerName = strtolower($_SERVER['SERVER_NAME']?$_SERVER['SERVER_NAME']:$_SERVER['HTTP_HOST']); 
	if(strpos($ServerName,'http://'))
	{   
		$ServerName=str_replace('http://','',$ServerName);
	}  
	return strpos($ServerName,':')?'http://'.$ServerName:'http://'.$ServerName.$port;
}
?>