<?php
/*********************/
/*                   */
/*  Version : 5.1.0  */
/*  Author  : RM     */
/*  Comment : 071223 */
/*                   */
/*********************/

include_once( "top.php" );
if ( file_exists( "install.lock" ) )
{
		echo "已经安装过了如果想在次安装请删除安装目录下的 install.lock文件";
		exit( );
}
$step = $_GET['step'];
if ( empty( $step ) )
{
		$step = 1;
}
if ( $step == 1 )
{
		$pass = "<font color=green>[√]On</font>";
		$error = "<font color=red>[×]Off</font>";
		echo "<div id=\"main\">\r\n      <form action=\"?step=2\" name=\"reg\" method=\"post\">\r\n        <div class=\"account\">\r\n          <div class=\"account_title\">服务器信息</div>\r\n          <ul>\r\n            <li class=\"tit\">服务器域名/IP地址：</li>\r\n            <li class=\"inpt\">";
		echo $_SERVER['SERVER_NAME'];
		echo " | ";
		echo empty( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_HOST'] : $_SERVER['REMOTE_ADDR'];
		echo "</li>\r\n          </ul>\r\n\t\t  <ul>\r\n            <li class=\"tit\">服务器操作系统：</li>\r\n            <li class=\"inpt\">";
		echo strtolower( isset( $_ENV['OS'] ) ? $_ENV['OS'] : getenv( "OS" ) );
		echo "</li>\r\n          </ul>\r\n\t\t  <ul>\r\n            <li class=\"tit\">服务器解译引擎：</li>\r\n            <li class=\"inpt\">";
		echo $_SERVER['SERVER_SOFTWARE'];
		echo "</li>\r\n          </ul>\r\n\t\t  <ul>\r\n            <li class=\"tit\">PHP版本：</li>\r\n            <li class=\"inpt\">";
		echo phpversion( );
		echo "</li>\r\n          </ul>\r\n\t\t  <ul>\r\n            <li class=\"tit\">站点物理路径：</li>\r\n            <li class=\"inpt\">";
		echo dirname( __FILE__ );
		echo "</li>\r\n          </ul>\r\n\t\t  <ul>\r\n            <li class=\"tit\">脚本超时时间：</li>\r\n            <li class=\"inpt\">";
		echo ini_get( "max_execution_time" );
		echo " 秒</li>\r\n          </ul>\r\n        </div>\r\n\t\t<div class=\"account\">\r\n          <div class=\"account_title\">系统环境要求</div>\r\n          <ul>\r\n            <li class=\"tit\">allow_url_fopen：</li>\r\n            <li class=\"inpt\">";
		echo ini_get( "allow_url_fopen" ) ? $pass : $error;
		echo "</li>\r\n          </ul>\r\n\t\t  <p>不符合要求将导致采集、远程资料本地化等功能无法应用</p>\r\n\t\t  <ul>\r\n            <li class=\"tit\">file_get_contents：</li>\r\n            <li class=\"inpt\">";
		echo function_exists( file_get_contents ) ? $pass : $error;
		echo "</li>\r\n          </ul>\r\n\t\t  <p>不符合要求将导致采集、远程资料本地化等功能无法应用</p>\r\n\t\t  <ul>\r\n            <li class=\"tit\">MySQL 支持：</li>\r\n            <li class=\"inpt\">";
		echo function_exists( "mysql_connect" ) ? $pass : $error;
		echo "</li>\r\n          </ul>\r\n\t\t  <p>不支持无法使用本系统</p>\r\n        </div>\r\n        <div class=\"bont_z\">\r\n          <input id=\"submit_button\" value=\"下一步\" type=\"submit\">\r\n        </div>\r\n        </form>\r\n      </div>\r\n";
}
else if ( $step == 2 )
{
		if ( !empty( $_SERVER['REQUEST_URI'] ) )
		{
				$scriptName = $_SERVER['REQUEST_URI'];
		}
		else
		{
				$scriptName = $_SERVER['PHP_SELF'];
		}
		$lfpath = ereg_replace( "install/index\\.php(.*)\$", "", $scriptName );
		if ( empty( $_SERVER['HTTP_HOST'] ) )
		{
				$baseurl = "http://".$_SERVER['HTTP_HOST'];
		}
		else
		{
				$baseurl = "http://".$_SERVER['SERVER_NAME'];
		}
		echo "<s";
		echo "cript type=\"text/javascript\">\r\nfunction \$(el)\r\n{\r\nreturn document.getElementById(el);\r\n}\r\n\r\nfunction check(obj)\r\n{\r\n\tajaxEnd({name: obj.name, message : '本项不能为空!'});\r\n}\r\n\r\nfunction ajaxEnd(data)\r\n{\r\n\tif (\$(data['name']).value){\r\n\t\t\$(data['name'] + '_message').innerHTML = \"\";\r\n\t}else{\r\n\t\t\$(data['name'] + '_message').innerHTML = data['message'];\r\n\t}\r\n}\r\n\r\nfunction checkSubmit()\r\n{\r\n\tfor(i=0;i<re";
		echo "g.elements.length;i++)  \r\n\t{\r\n\t\t\tif(!document.reg[i].value){\r\n\t\t\tcheck(document.reg[i]);\r\n\t\t\tdocument.reg[i].focus();\r\n\t\t\treturn false;\r\n\t\t\t}\r\n\t}\r\n}\r\n    </script>\r\n      <div id=\"main\">\r\n      <form action=\"?step=3\" name=\"reg\" method=\"post\" onsubmit=\"return checkSubmit();\">\r\n        <div class=\"account\">\r\n          <div class=\"account_title\">数据库设定</div>\r\n          <ul>\r\n            <li class=\"tit\">";
		echo "<s";
		echo "pan>*</span>数据库主机：</li>\r\n            <li class=\"inp\">\r\n              <input name=\"dbhost\" value=\"localhost\" onfocus=\"this.className = 'focus'\" onblur=\"this.className = 'blur'; check(this);\" class=\"blur\" type=\"text\">\r\n            </li>\r\n            <li class=\"red\" id=\"dbhost_message\"></li>\r\n          </ul>\r\n          <p>一般为localhost</p>\r\n          <ul>\r\n            <li class=\"tit\">";
		echo "<s";
		echo "pan>*</span>数据库名称：</li>\r\n            <li class=\"inp\">\r\n              <input name=\"dbname\" value=\"gy_game\" onfocus=\"this.className = 'focus'\" onblur=\"this.className = 'blur'; check(this);\" class=\"blur\" type=\"text\">\r\n            </li>\r\n            <li class=\"red\" id=\"dbname_message\"></li>\r\n          </ul>\r\n          <p>建立的数据库名称</p>\r\n\t\t  <ul>\r\n            <li class=\"tit\">";
		echo "<s";
		echo "pan>*</span>数据库用户：</li>\r\n            <li class=\"inp\">\r\n              <input name=\"dbuser\" value=\"root\" onfocus=\"this.className = 'focus'\" onblur=\"this.className = 'blur'; check(this);\" class=\"blur\" type=\"text\">\r\n            </li>\r\n            <li class=\"red\" id=\"dbuser_message\"></li>\r\n          </ul>\r\n          <p>mysql数据库连接用户</p>\r\n\t\t  <ul>\r\n            <li class=\"tit\">";
		echo "<s";
		echo "pan>*</span>数据库密码：</li>\r\n            <li class=\"inp\">\r\n              <input name=\"dbpwd\" onfocus=\"this.className = 'focus'\" onblur=\"this.className = 'blur'; check(this);\" class=\"blur\" type=\"text\">\r\n            </li>\r\n            <li class=\"red\" id=\"dbpwd_message\"></li>\r\n          </ul>\r\n          <p>mysql数据库连接密码</p>\r\n\t\t  <ul>\r\n            <li class=\"tit\">";
		echo "<s";
		echo "pan>*</span>数据表前缀：</li>\r\n            <li class=\"inp\">\r\n              <input name=\"dbprefix\" value=\"gy_\" onfocus=\"this.className = 'focus'\" onblur=\"this.className = 'blur'; check(this);\" class=\"blur\" type=\"text\">\r\n            </li>\r\n            <li class=\"red\" id=\"dbprefix_message\"></li>\r\n          </ul>\r\n          <p>建立的数据库表名的前缀</p>\r\n        </div>\r\n\t\t<div class=\"account\">\r\n          <div clas";
		echo "s=\"account_title\">管理员初始密码</div>\r\n          <ul>\r\n            <li class=\"tit\">";
		echo "<s";
		echo "pan>*</span>用户名：</li>\r\n            <li class=\"inp\">\r\n              <input name=\"adminuser\" value=\"admin\" onfocus=\"this.className = 'focus'\" onblur=\"this.className = 'blur'; check(this);\" class=\"blur\" type=\"text\">\r\n            </li>\r\n            <li class=\"red\" id=\"adminuser_message\"></li>\r\n          </ul>\r\n          <p>只能用'0-9'、'a-z'、'A-Z'、'.'、'@'、'_'、'-'、'!'以内范围的字符</p>\r\n          <ul>\r\n";
		echo "            <li class=\"tit\">";
		echo "<s";
		echo "pan>*</span>密　码：</li>\r\n            <li class=\"inp\">\r\n              <input name=\"adminpwd\" value=\"admin\" onfocus=\"this.className = 'focus'\" onblur=\"this.className = 'blur'; check(this);\" class=\"blur\" type=\"text\">\r\n            </li>\r\n            <li class=\"red\" id=\"adminpwd_message\"></li>\r\n          </ul>\r\n          <p>3～16位，区分大小写</p>\r\n        </div>\r\n        <div class=\"account\">\r\n          <div cla";
		echo "ss=\"account_title\">网站设置</div>\r\n          <ul>\r\n            <li class=\"tit\">";
		echo "<s";
		echo "pan>*</span>网站名称：</li>\r\n            <li class=\"inp\">\r\n              <input name=\"web_name\" value=\"光影游戏积分网\" onfocus=\"this.className = 'focus'\" onblur=\"this.className = 'blur'; check(this);\" class=\"blur\" type=\"text\">\r\n            </li>\r\n            <li class=\"red\" id=\"web_name_message\"></li>\r\n          </ul>\r\n          <p>建立网站的名称</p>\r\n          <ul>\r\n            <li class=\"tit\">";
		echo "<s";
		echo "pan>*</span>网站地址：</li>\r\n            <li class=\"inp\">\r\n              <input name=\"web_url\" value=\"";
		echo $baseurl;
		echo "\" onfocus=\"this.className = 'focus'\" onblur=\"this.className = 'blur'; check(this);\" class=\"blur\" type=\"text\">\r\n            </li>\r\n            <li class=\"red\" id=\"web_url_message\"></li>\r\n          </ul>\r\n          <p>以http开头</p>\r\n          <ul>\r\n            <li class=\"tit\">";
		echo "<s";
		echo "pan>*</span>网站目录：</li>\r\n            <li class=\"inp\">\r\n              <input name=\"web_dir\" value=\"/\" onfocus=\"this.className = 'focus'\" onblur=\"this.className = 'blur'; check(this);\" class=\"blur\" type=\"text\">\r\n            </li>\r\n            <li class=\"red\" id=\"web_dir_message\"></li>\r\n          </ul>\r\n          <p>一般不必理会系统会自动检测 如检测结果和安装目录不符可以手动修改</p>\r\n        </div>\r\n        <div class=\"bont_z\">\r\n          <input id=\"submit_button\" value=\"安装\" type=\"submit\">\r\n        </div>";
		echo "\r\n        </form>\r\n      </div>\r\n";
}
else if ( $step == 3 )
{
		$configstr = "<?php\r\n";
		$configstr .= "\$web_datahost=\"".$_POST['dbhost']."\";\r\n";
		$configstr .= "\$web_database=\"".$_POST['dbname']."\";\r\n";
		$configstr .= "\$web_datauser=\"".$_POST['dbuser']."\";\r\n";
		$configstr .= "\$web_datapassword=\"".$_POST['dbpwd']."\";\r\n";
		$configstr .= "\$web_pconnect=\"0\";\r\n";
		$configstr .= "\$web_dbcharset=\"gbk\";\r\n";
		$configstr .= "\$web_dbtop=\"".$_POST['dbprefix']."\";\r\n";
		$configstr .= "\$web_name=\"".$_POST['web_name']."\";\r\n";
		$configstr .= "\$web_url=\"".$_POST['web_url']."\";\r\n";
		$configstr .= "\$web_logo=\"logo.gif\";\r\n";
		$configstr .= "\$web_keywords=\"".$_POST['web_name']."\";\r\n";
		$configstr .= "\$web_description=\"".$_POST['web_name']."\";\r\n";
		$configstr .= "\$web_beian=\"\";\r\n";
		$configstr .= "\$web_statistics=\"\";\r\n";
		$configstr .= "\$web_commentskey=\"艳照门|法轮功|毒品|强奸|做爱|台独|藏独|胡锦涛|江泽民\";\r\n";
		$configstr .= "\$web_templates=\"default/\";\r\n";
		$configstr .= "\$web_dir=\"/\";\r\n";
		$configstr .= "\$web_picdir=\"uppic/shoppic/\";\r\n";
		$configstr .= "\$web_adpicdir=\"uppic/adpic/\";\r\n";
		$configstr .= "\$web_activitiesdir=\"uppic/activitiespic/\";\r\n";
		$configstr .= "\$web_businessdir=\"uppic/businesspic/\";\r\n";
		$configstr .= "\$web_carddir=\"uppic/cardpic/\";\r\n";
		$configstr .= "\$web_slidedir=\"uppic/slide/\";\r\n";
		$configstr .= "\$web_headdir=\"uppic/userspic/head/\";\r\n";
		$configstr .= "\$web_cardiddir=\"uppic/userspic/cardpic/\";\r\n";
		$configstr .= "\$web_moneypic=\"images/jd.gif\";\r\n";
		$configstr .= "\$web_moneyname=\"金币\";\r\n";
		$configstr .= "\$web_ck_card=\"1\";\r\n";
		$configstr .= "\$reg_points=\"100\";\r\n";
		$configstr .= "\$web_authentication=\"500\";\r\n";
		$configstr .= "\$web_linknum=\"10\";\r\n";
		$configstr .= "\$web_linkpoints=\"50\";\r\n";
		$configstr .= "\$web_loginperience=\"10\";\r\n";
		$configstr .= "\$web_exchanged_num=\"5\";\r\n";
		$configstr .= "\$web_exchanged_num_vip=\"100\";\r\n";
		$configstr .= "?>";
		$errstr = "写入配置 inc/config.php 失败，请检查 inc文件夹 是否可读写！";
		if ( !( $fp = fopen( "../inc/config.php", "w" ) ) )
		{
				exit( $errstr );
		}
		fwrite( $fp, stripcslashes( $configstr ) );
		fclose( $fp );
		$conn = @mysql_connect( $_POST['dbhost'], $_POST['dbuser'], $_POST['dbpwd'] );
		if ( !$conn )
		{
				echo "数据库服务器或登录密码无效，\\n\\n无法连接数据库，请重新设定！";
				exit( );
		}
		$rs = mysql_select_db( $_POST['dbname'], $conn );
		if ( !$rs )
		{
				$rs = mysql_query( " CREATE DATABASE `{$_POST['dbname']}`; ", $conn );
				if ( !$rs )
				{
						$errstr = "数据库 ".$_POST['dbname']." 不存在，也没权限创建新的数据库！";
						echo $errstr;
						exit( );
				}
				else
				{
						$rs = mysql_select_db( $_POST['dbname'], $conn );
						if ( !$rs )
						{
								$errstr = "你对数据库 ".$_POST['dbname']." 没权限！";
								echo $errstr;
								exit( );
						}
				}
		}
		mysql_query( "SET NAMES gbk;", $conn );
		$rs = mysql_query( "SELECT VERSION();", $conn );
		$row = mysql_fetch_array( $rs );
		$mysql_version = $row[0];
		$mysql_versions = explode( ".", trim( $mysql_version ) );
		$mysql_version = $mysql_versions[0].".".$mysql_versions[1];
		$sql = file_get_contents( "setup_data.sql" );
		$sql = str_replace( "lf_", $_POST['dbprefix'], $sql );
		if ( "4.1" < $mysql_version )
		{
				$sql = preg_replace( "/TYPE=(InnoDB|MyISAM)( DEFAULT CHARSET=[^; ]+)?/", "TYPE=\\1 DEFAULT CHARSET=GBK", $sql );
		}
		$sql = str_replace( "\r", "\n", $sql );
		$ret = array( );
		$num = 0;
		$queriesarray = explode( ";\n", trim( $sql ) );
		unset( $sql );
		foreach ( $queriesarray as $query )
		{
				$ret[$num] = "";
				$queries = explode( "\n", trim( $query ) );
				$queries = array_filter( $queries );
				foreach ( $queries as $query )
				{
						$str1 = substr( $query, 0, 1 );
						if ( $str1 != "#" && $str1 != "-" )
						{
								$ret[$num] .= $query;
						}
				}
				++$num;
		}
		if ( is_array( $ret ) )
		{
				foreach ( $ret as $sql )
				{
						if ( trim( $sql ) != "" )
						{
								mysql_query( $sql, $conn );
						}
				}
		}
		else
		{
				mysql_query( $ret, $conn );
		}
		mysql_query( "INSERT INTO `".$_POST['dbprefix']."admin` (name,password,groupbox) VALUES ('".$_POST['adminuser']."','".md5( md5( $_POST['adminpwd'] ) )."','system,gamegl,gametj,adgl,adff,adtj,jpgl,djgl,users,sms,hdgl,bbs,business,card,pay,dbgl,admingl')", $conn );
		$errstr = "install.lock 失败，请检查 文件夹 是否可读写！";
		if ( !( $fp = fopen( "install.lock", "w" ) ) )
		{
				exit( $errstr );
		}
		fwrite( $fp, "安装完成" );
		fclose( $fp );
		echo "<div class=\"main\">";
		echo "<p><span>程序已经安装成功：</span><br /><span>5秒后自动跳转到后台登录页</span>&nbsp;&nbsp;<a href=\"../admin/index.php\">立即转到登录页</a></p>";
		echo "<meta http-equiv=\"refresh\" content=\"5;URL=../admin/index.php\">";
		echo "</div>";
}
echo "</body>\r\n</html>";
?>
