<?php
error_reporting(0);
@set_time_limit(1000);
//@set_magic_quotes_runtime(0);    //php5.3
ini_set("magic_quotes_runtime",0); //php7
if(PHP_VERSION < '4.1.0') {
	$_GET = &$HTTP_GET_VARS;
	$_POST = &$HTTP_POST_VARS;
}
$installfile = basename(__FILE__);

$sqlfile = './install/ftsucai.sql';
$lockfile = './cache/install.lock';
$quit = FALSE;

@include './install/install.lang.php';
@include './install/global.func.php';
@include './common/config.php';
@include './classes/dbclass.php';

$inslang = defined('INSTALL_LANG') ? INSTALL_LANG : '';
$version = 'X3.0'.$lang[$inslang];

if (file_exists($lockfile)){
  instmsg('lock_exists');
}

if(!get_cfg_var('zend_loader.enable')){
    //instmsg('zend_loader');
}elseif(!defined('INSTALL_LANG') || !function_exists('instmsg') || !is_readable($sqlfile)) {
	exit("Please upload all files to install FTDMS<br>&#x5b89;&#x88c5; FTDMS &#x8bba;&#x575b;&#x60a8;&#x5fc5;&#x987b;&#x4e0a;&#x4f20;&#x6240;&#x6709;&#x6587;&#x4ef6;&#xff0c;&#x5426;&#x5219;&#x65e0;&#x6cd5;&#x7ee7;&#x7eed;");
}elseif(!isset($dbhost) || !isset($cookieprename)) {
	instmsg('config_nonexistence');
}

if(function_exists('instheader')) {
	instheader();
}

if(empty($dbcharset) && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8'))) {
	$dbcharset = str_replace('-', '', $charset);
}

$action = @$_POST['action'] ? @$_POST['action'] : @$_GET['action'];
if(in_array($action, array('check', 'config'))) {
	if(is_writeable('./common/config.php')) {
		$writeable['config'] = result(1, 0);
		$write_error = 0;
	} else {
		$writeable['config'] = result(1, 0);
		$write_error = 1;
	}
}

if(!$action) {

	$license = str_replace('  ', '&nbsp; ', $lang['license']);

?>
<tr>
	<td>
		<b>
			<?php echo $lang['current_process']?>
		</b>
		<font color="#0000EE">
			<?php echo $lang['show_license']?>
		</font>
	</td>
</tr>
<tr>
	<td>
		<hr noshade align="center" width="100%" size="1">
	</td>
</tr>
<tr>
	<td>
		<br>
		<table width="90%" cellspacing="1" bgcolor="#000000" border="0" align="center">
			<tr>
				<td class="altbg1">
					<table width="99%" cellspacing="1" border="0" align="center">
						<tr>
							<td>
								<?php echo $license?>
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	</td>
</tr>
<tr>
	<td align="center">
		<br>
		<form method="post" action="<?php echo $installfile?>">
			<input type="hidden" name="action" value="config">
			<input type="submit" name="submit" value="<?php echo $lang['agreement_yes']?>" style="height: 25">&nbsp;
			<input type="button" name="exit" value="<?php echo $lang['agreement_no']?>" style="height: 25" onclick="javascript: window.close();">
		</form>
	</td>
</tr>
<?php
} elseif($action == 'config') {
?>
<tr>
	<td>
		<b>
			<?php echo $lang['current_process']?>
		</b>
		<font color="#0000EE">
			<?php echo $lang['edit_config']?>
		</font>
	</td>
</tr>
<tr>
	<td>
		<hr noshade align="center" width="100%" size="1">
	</td>
</tr>
<tr>
	<td>
		<br>
	<?php
    if ($write_error==1){
	   $inputreadonly = $write_error ? 'readonly' : '';
	   $msg = '<li>'.$lang['config_unwriteable'].'</li>';
	}else{
	   $msg = '<li>'.$lang['config_comment'].'</li>';
    }
	if(@$_POST['saveconfig']) {
		$msg = '';
		$dbhost = setconfig($_POST['dbhost']);
		$dbuser = setconfig($_POST['dbuser']);
		$dbpw = setconfig($_POST['dbpw']);
		$dbname = setconfig($_POST['dbname']);
		$domain = setconfig($_POST['domain']);
		$tablepre = setconfig($_POST['tablepre']);
		
	    $checkdirarray = array('common' => './common','cache' => './cache');
	    foreach($checkdirarray as $key => $dir) {
		       if(dir_writeable($dir)) {
			     //$writeable[$key] = result(1, 0);
		       } else {
			     //$writeable[$key] = result(0, 0);
				 $langkey = $key.'_unwriteable';
			     $msg .= "<li>$lang[$langkey]</li>";
			     $quit = TRUE;
		      }
	    }
		//检测根目录下的写入权限
		if($rot = @fopen("./test.txt", 'w')) {
			@fclose($rot);
			@unlink("./test.txt");
		}else{
			$msg = '<li>'.$lang['siteroot'].'</li>';
			$quit = TRUE;
		}
		
		if(empty($dbname)) {
			 $msg .= '<li>'.$lang['dbname_invalid'].'</li>';
			 $quit = TRUE;
		   } else {

             $db_config=array(
			  'dsn'=>'mysql:host='.$dbhost.';dbname='.$dbname.'',
			  'username'=>$dbuser,
			  'password'=>$dbpw
			 );
			 $db=new PDO_DB($db_config);
			 $result = $db->exec("CREATE DATABASE IF NOT EXISTS `$dbname` DEFAULT CHARACTER SET $dbcharset",true);
			 if(!$result) {
				 $errormsg = 'database_errno_1044';
				 $msg .= "'<li>$errormsg ".$lang[$errormsg].'</li>';
				 $quit = TRUE;
			 }

		}

		if(!$quit){
			if(!$write_error) {
				$fp = fopen('./common/config.php', 'r');
				$configfile = fread($fp, filesize('./common/config.php'));
				fclose($fp);

				$configfile = preg_replace("/[$]dbhost\s*\=\s*[\"'].*?[\"'];/is", "\$dbhost = '$dbhost';", $configfile);
				$configfile = preg_replace("/[$]dbuser\s*\=\s*[\"'].*?[\"'];/is", "\$dbuser = '$dbuser';", $configfile);
				$configfile = preg_replace("/[$]dbpw\s*\=\s*[\"'].*?[\"'];/is", "\$dbpw = '$dbpw';", $configfile);
				$configfile = preg_replace("/[$]dbname\s*\=\s*[\"'].*?[\"'];/is", "\$dbname = '$dbname';", $configfile);
				$configfile = preg_replace("/[$]cookiedomain\s*\=\s*[\"'].*?[\"'];/is", "\$cookiedomain = '$domain';", $configfile);
				$configfile = preg_replace("/[$]tablepre\s*\=\s*[\"'].*?[\"'];/is", "\$tablepre = '$tablepre';", $configfile);

				$fp = fopen('./common/config.php', 'w');
				fwrite($fp, trim($configfile));
				fclose($fp);
			}
			redirect("$installfile?action=admin");
		}
	}

?>
<tr>
	<td align="center" valign="top">
		<table width="80%" cellspacing="1" bgcolor="#000000" border="0" align="center">
			<tr bgcolor="#085878">
				<td style="color: #FFFFFF; padding-left: 10px" width="32%">
					<?php echo $lang['tips_message']?>
				</td>
			</tr>
			<tr>
				<td class="message">
					<?php echo $msg?>
				</td>
			</tr>
		</table>
		<br>
		<form method="post" action="<?php echo $installfile?>">
			<table width="80%" cellspacing="1" bgcolor="#000000" border="0" align="center">
				<tr class="header">
					<td width="20%">
						<?php echo $lang['variable']?>
					</td>
					<td width="30%">
						<?php echo $lang['value']?>
					</td>
					<td width="50%">
						<?php echo $lang['comment']?>
					</td>
				</tr>
				<tr>
					<td class="altbg1">&nbsp;<span class="redfont">
							<?php echo $lang['dbhost']?>
						</span>
					</td>
					<td class="altbg2">
						<input type="text" name="dbhost" value="<?php echo $dbhost?>" <?php echo $inputreadonly?> size="30"></td>
					<td class="altbg1">&nbsp;
						<?php echo $lang['dbhost_comment']?>
					</td>
				</tr>
				<tr>
					<td class="altbg1">&nbsp;
						<?php echo $lang['dbuser']?>
					</td>
					<td class="altbg2">
						<input type="text" name="dbuser" value="<?php echo $dbuser?>" <?php echo $inputreadonly?> size="30"></td>
					<td class="altbg1">&nbsp;
						<?php echo $lang['dbuser_comment']?>
					</td>
				</tr>
				<tr>
					<td class="altbg1">&nbsp;
						<?php echo $lang['dbpw']?>
					</td>
					<td class="altbg2">
						<input type="password" name="dbpw" value="<?php echo $dbpw?>" <?php echo $inputreadonly?> size="30"></td>
					<td class="altbg1">&nbsp;
						<?php echo $lang['dbpw_comment']?>
					</td>
				</tr>
				<tr>
					<td class="altbg1">&nbsp;
						<?php echo $lang['dbname']?>
					</td>
					<td class="altbg2">
						<input type="test" name="dbname" value="<?php echo $dbname?>" <?php echo $inputreadonly?> size="30"></td>
					<td class="altbg1">&nbsp;
						<?php echo $lang['dbname_comment']?>
					</td>
				</tr>
				<tr>
					<td class="altbg1">&nbsp;
						<?php echo $lang['tablepre']?>
					</td>
					<td class="altbg2">
						<input name="tablepre" type="text" id="tablepre" value="<?php echo $tablepre?>" size="30" <?php echo $inputreadonly?> ></td>
					<td class="altbg1">&nbsp;
						<?php echo $lang['tablepre_comment']?>
					</td>
				</tr>
				<tr>
					<td class="altbg1">&nbsp;
						<?php echo $lang['domain']?>
					</td>
					<td class="altbg2">
						<input type="text" name="domain" value="<?php echo $domain?>" size="30" <?php echo $inputreadonly?> ></td>
					<td class="altbg1">&nbsp;
						<?php echo $lang['domain_comment']?>
					</td>
				</tr>
				<!--<tr>
<td class="altbg1">&nbsp;<span class="redfont"><?php echo $lang['tablepre']?></span></td>
<td class="altbg2"><input type="text" name="tablepre" value="<?php echo $ft?>" <?php echo $inputreadonly?> size="30"></td>
<td class="altbg1">&nbsp;<?php echo $lang['tablepre_comment']?></td>
</tr>-->
			</table>
			<br>
			<input type="hidden" name="action" value="config">
			<input type="hidden" name="saveconfig" value="1">
			<input type="button" name="submit" value=" <?php echo $lang['old_step']?> " style="height: 25" onclick="window.location='install.php'">&nbsp;
			<input type="submit" name="submit" value=" <?php echo $lang['new_step']?> " style="height: 25">
		</form>
	</td>
</tr>
<?php
instfooter();
} elseif($action == 'admin') {

?>
<tr>
	<td>
		<b>
			<?php echo $lang['current_process']?>
		</b>
		<font color="#0000EE">
			<?php echo $lang['add_admin']?>
		</font>
	</td>
</tr>
<tr>
	<td>
		<hr noshade align="center" width="100%" size="1">
	</td>
</tr>
<tr>
	<td>
		<br>
		<?php

	$msg = '<li>'.$lang['add_admin'].'</li>';

		/*
		$curr_mysql_version = mysqli_get_server_info();
		if($curr_mysql_version < '3.23') {
			$msg .= '<li>'.$lang['mysql_version_323'].'</li>';
			$quit = TRUE;
		}
        */
		$db_config=array(
		  'dsn'=>'mysql:host='.$dbhost.';dbname='.$dbname.'',
		  'username'=>$dbuser,
		  'password'=>$dbpw
		);
		$db=new PDO_DB($db_config);

		$sqlarray = array(
				'createtable' => 'CREATE TABLE mydb_test (test TINYINT (3) UNSIGNED)',
				'insert' => 'INSERT INTO mydb_test (test) VALUES (1)',
				'select' => 'SELECT * FROM mydb_test',
				'update' => 'UPDATE mydb_test SET test=\'2\' WHERE test=\'1\'',
				'delete' => 'DELETE FROM mydb_test WHERE test=\'2\'',
				'droptable' => 'DROP TABLE mydb_test'
		);

		foreach($sqlarray as $key => $sql) {
			$db->exec($sql);
		}


	if($_POST['submit']) {

		$username = $_POST['username'];
		$password1 = $_POST['password1'];
		$password2 = $_POST['password2'];

		if($username && $password1 && $password2) {
			if($password1 != $password2) {
				$msg .= '<li><font color="#FF0000">'.$lang['admin_password_invalid'].'</font></li>';
				$quit = TRUE;
			} elseif(strlen($username) > 15 || preg_match("/^$|^c:\\con\\con$|　|[,\"\s\t\<\>&]|^游客|^Guest/is", $username)) {
				$msg = $lang['admin_username_invalid'];
			}
		} else {
			$msg .= '<li><font color="#FF0000">'.$lang['admin_invalid'].'</font></li>';
			$quit = TRUE;
		}

		if(!$quit){
			redirect("$installfile?action=install&username=".rawurlencode($username)."&password=".md5($password1));
		}

	}

?>
<tr>
	<td align="center" valign="top">
		<table width="80%" cellspacing="1" bgcolor="#000000" border="0" align="center">
			<tr bgcolor="#085878">
				<td style="color: #FFFFFF; padding-left: 10px" width="32%">
					<?php echo $lang['tips_message']?>
				</td>
			</tr>
			<tr>
				<td class="message">
					<?php echo $msg?>
				</td>
			</tr>
		</table>
		<br>
		<form method="post" action="<?php echo $installfile?>" <?php echo $alert?>>
			<table width="80%" cellspacing="1" bgcolor="#000000" border="0" align="center">
				<tr bgcolor="#085878">
					<td style="color: #FFFFFF; padding-left: 10px" colspan="2">
						<?php echo $lang['add_admin']?>
					</td>
				</tr>
				<tr>
					<td class="altbg1" width="20%">&nbsp;
						<?php echo $lang['username']?>
					</td>
					<td class="altbg2" width="80%">&nbsp;
						<input type="text" name="username" value="admin" size="30">
					</td>
				</tr>
				<tr>
					<td class="altbg1">&nbsp;
						<?php echo $lang['password']?>
					</td>
					<td class="altbg2">&nbsp;
						<input type="password" name="password1" size="30">
					</td>
				</tr>
				<tr>
					<td class="altbg1">&nbsp;
						<?php echo $lang['repeat_password']?>
					</td>
					<td class="altbg2">&nbsp;
						<input type="password" name="password2" size="30">
					</td>
				</tr>
			</table>
			<br>
			<input type="hidden" name="action" value="admin">
			<input type="button" name="submit" value=" <?php echo $lang['old_step']?> " style="height: 25" onclick="window.location='?action=config'">&nbsp;
			<input type="submit" name="submit" value=" <?php echo $lang['new_step']?> " style="height: 25">
		</form>
	</td>
</tr>
<?php
instfooter();
} elseif($action == 'install') {
    
	$username = htmlspecialchars($_GET['username']);
	$password = htmlspecialchars($_GET['password']);

	$db_config=array(
      'dsn'=>'mysql:host='.$dbhost.';dbname='.$dbname.'',
      'username'=>$dbuser,
      'password'=>$dbpw
    );
    $db=new PDO_DB($db_config);

?>
<tr>
	<td>
		<b>
			<?php echo $lang['current_process']?>
		</b>
		<font color="#0000EE">
			<?php echo $lang['start_install']?>
		</font>
	</td>
</tr>
<tr>
	<td>
		<hr noshade align="center" width="100%" size="1">
	</td>
</tr>
<tr>
	<td align="center">
		<br>
		<script type="text/javascript">
			function showmessage(message) {
					document.getElementById('notice').value += message +"\r\n";
				}
		</script>
		<textarea name="notice" style="width: 80%; height: 590px;font-size:12px;" readonly id="notice"></textarea>

		<br>
		<br>
		<input type="button" name="submit" value=" <?php echo $lang['install_in_processed']?> " disabled style="height: 25" onclick="window.location='index.php'" id="laststep">
		<br>
		<br>
		<br>
	</td>
</tr>
<?php
	instfooter();
	
	$fp = fopen($sqlfile, 'rb');
	$sql = fread($fp, filesize($sqlfile));
	//echo $sql;exit;
	
	fclose($fp);
	runquery($sql);
	$db->exec("UPDATE {$tablepre}scuser SET UserName='$username',Password='$password' WHERE id=1;");
	@touch($lockfile);

	echo '<script type="text/javascript">document.getElementById("laststep").disabled = false; </script>'."\r\n";
	echo '<script type="text/javascript">document.getElementById("laststep").value = \''.$lang['install_succeed'].'\'; </script>'."\r\n";
	echo '<iframe width="0" height="0" src="http://www.ftsucai.com"></iframe>';
}
?>