<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
@set_time_limit(1000);
@set_magic_quotes_runtime(0);


include('./include/install_var.php');
include('../common.php');

define('AM_PATH', dirname(__FILE__).'/../');

$checkfiles = array
(
		'appmakerpath' => array('type' => 'dir', 'path' => AM_PATH,'name' => 'appmaker')
);


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=utf8" />
	<title></title>
	<link rel="stylesheet" href="images/css.css" />
</head>
<body style="margin:0px;padding:0px;">
	<div id="check" style="margin-left:auto;margin-right:auto;">
	 
		<div class="check_top">
			<div class="discur_xzaz">
			   <span>APPMAKER APP插件安装</span>
			   <a href=""><img src="images/close.png" alt="" /></a>
		    </div>
			<div class="check_top_jc">
				<div>第一步：检查权限，设置密码</div>
				
				<div>第二步：安装插件</div>
			</div>
			<div class="check_top_img">
				<div class="jiantou"><img src="images/blue_jiantou.png" alt="" /></div>
				
				<div class="wancheng"><img src="images/grey_jiantou.png" alt="" /></div>
			</div>
		</div>
		<?php
			  if(file_exists('install.log'))
			  {
				echo "<script>window.location = 'index.php'</script>";
			  }else{?>
		<div class="check_middle">
			<span class="checkspan">数据库连接检查</span>
			<table class="checktable">
				<tr class="tablehead">
					<td style="width:60%;">目标数据库</td>
					<td style="width:40%;">检查结果</td>
					
				</tr>
				<tr class="tableother">
					<td><?php echo $_TC['sqlname'];?></td>
					
					<?php
						if(file_exists(ROOT_PATH.CONFIG)) {
							include ROOT_PATH.CONFIG;
						} else {
							$_config = $default_config;
						}
						
						$dbname_not_exists = true;
						
						$db = new dbstuff;
						
						$db->connect($_TC['sqlhost'], $_TC['sqluser'], $_TC['sqlpw'], $_TC['sqlname'], $_TC['dbcharset']);
						
						$sql = file_get_contents(AM_PATH.'./install/data/install_data.sql');
						
						$sql = str_replace('{replaceStr}', $_TC['sqlpre'], $sql);
						$sql = str_replace("\r\n", "\n", $sql);
						$result = mysql_query('SHOW TABLES');
						$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', 'MEMORY')) ? $type : 'MYISAM';
						$sql = preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql).
						(mysql_get_server_info() > '4.1' ? " ENGINE=$type DEFAULT CHARSET=".$_TC['dbcharset'] : " TYPE=$type");
					
						
						while($rows = mysql_fetch_array($result))
						{
							$arr[]=$rows[0];
						}
						if(in_array($_TC['sqlpre'].'appmaker_password', $arr))
						{
						
						}
						else {
							$create_table = mysql_query($sql) or die(mysql_errno());
							if($create_table == 1)
							{
								$issubmit = true;
								$istable = true;
 								//echo '数据表创建成功';
								$sqlinsert = "insert into ".tname("appmaker_password")." (`password`) values ('123456')";
								$_TD->FM($sqlinsert); 
								
								
							}
							else {
								$issubmit = false;
								$istable = false;
 								echo '数据表创建失败';
							}
						}
						
						
						
						
						
						
						
						
						

					?>
					
					
					<td class="lj">连接成功</td>
				
				</tr>
			</table>
		</div>
		<div class="check_middle">
		
			<span class="checkspan">文件夹权限检查</span>
			<table class="checktable">
				<tr class="tablehead">
					<td style="width:60%;">目标文件</td>
					<td style="width:40%;">检查结果</td>
				</tr>
				<?php 
					$issubmit = true;
					$checkfiles = dirfile_check($checkfiles);
					foreach ($checkfiles as $key=>$val)
					{
						echo '<tr class="tableother">';
						echo "<td>".$val['name']."</td>";
						
						if(checkFile($val['current'])){
							echo '<td class="lj">可写</td>';
						}else{
							$issubmit = false;
							echo "<td class='lj' style='color:red'>不可写，请务必保证在生成过程中为可写状态，否则生成APP在复制到您的网站时会失败</td>";
						}
				
						echo "</tr>";
					}
				?>

			</table>
		</div>
		
	<form action="step_2.php" class="check_bottom"  name="check_bottom" onsubmit ="return checkpwd()" method ="post">
		<div class="password">
			<span class="setspan">设置APP转换密码</span>
			<div class="import">
				<input type="password" name ='pwd' id ='pwd'/>
				<span class="passwords">（请输入APP转换密码）</span>
			</div>
		</div>
		<input type="reset" value="取消" class="qx" />
			<?php
		if($issubmit =='true')
		{
			echo ' <input type="submit" value="下一步" class="xyb" />';
		}
		else
		{
			echo ' <input type="submit" value="下一步" class="xyb" disabled/>';
		}
		?>

		</form>
		<?php }?>
	</div>
	<script type="text/javascript">
		function checkpwd(){
			var pwd =document.getElementById('pwd').value;
			if(pwd.length < 4){
				alert('密码长度不能小于4位');
				return false;
				}
			
			}
	</script>
</body>
</html>