<?php

$dirs = array('cache', 'config', 'backup');
$tabs = array('article', 'comments', 'val', 'page', 'posts', 'user', 'link');
header('Content-Type: text/html; charset=utf-8');
define('ROOT', './');
define('SYSTIME', time());
if (isset($_GET['post']))
{
    $v = $_POST['v'];
    if (file_exists('./config/config.php'))
    {
        e('如果要重装系统,请先删除./config/config.php');
    }
    PHP_VERSION < '5.0.0' && e('php版本过低');
    while (list($key, $val) = @each($dirs))
    {
        write_file('./' . $val . '/index.html', '0') or e('目录:' . $val . '不可写入');
    }
    @mysql_connect($v[0], $v[2], $v[3]) or e('无法链接数据库');
    mysql_get_server_info() < '5.0' && e('数据库版本过低');
    @mysql_query("CREATE DATABASE `$v[1]` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;");
    @mysql_select_db($v[1]) or e('无法链接数据库');
    mysql_query("SET NAMES 'utf8'");
    $s_pf = 'dodo_';
    foreach ($tabs as $key => $tab)
    {
        $tabs[$key] = $v[4] . $tab;
    }
    if (empty($v[7]))
    {
        $result = mysql_query('show tables');
        while ($row = mysql_fetch_row($result))
        {
            in_array($row[0], $tabs) && e('数据表已存在');
        }
    }
    @mysql_query('DROP TABLE IF EXISTS `' . implode('`,`', $tabs) . '`');
    $sql = explode("\n\n", str_replace($s_pf, $v[4], trim(file_get_contents('includes/mysql_' .
        (empty($v[8]) ? 'empty' : 'data') . '.sql'))));
    foreach ($sql as $query)
    {
        $query = trim($query);
        if (in_array(substr($query, 0, 10), array('CREATE TAB', 'INSERT INT',
            'SET SQL_MO')))
        {
            @mysql_query(trim($query)) or e('创建数据表出错:' . $query);
        }
    }
    $v[6] = md5(md5($v[6]));
    mysql_query("INSERT INTO `" . $v[4] .
        "user` (`id`, `name`,`pass`, `reg_time`,`team`) VALUES
(1, '" . addslashes($v[5]) . "', '" . $v[6] . "','" . SYSTIME . "',9)");
    if (!write_file('./config/config.php', '<?php
if (!defined(\'ABSPATH\'))
    exit(\'Access Denied\');
    //本文件数据由安装程序更新;
return array(
\'dbhost\' => \'' . $v[0] . '\',
\'dbname\' => \'' . $v[1] . '\',
\'dbuser\' => \'' . $v[2] . '\',
\'dbpw\' => \'' . $v[3] . '\',
\'dbprefix\' => \'' . $v[4] . '\',
\'rootpath\'=>\'' . substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'],
        '/') + 1) . '\',
\'timezone\'=>\'Asia/Shanghai\',
);

?>'))
    {
        e('无法生成配置文件');
    }
    ;
    y();
}
function y()
{
    echo '<html><body>';
    echo '<script language="javascript">';
    echo 'parent.pp();';
    echo '</script>';
    echo '</body></html>';
    exit;
}
function e($str)
{
    echo '<html><body>';
    echo '<script language="javascript">';
    echo 'parent.msg("<span style=\\"color:red\\">警告!</span> ' . $str . '");';
    echo '</script>';
    echo '</body></html>';
    exit;
}
function write_file($root, $data, $mode = 'wb')
{
    if (!$fp = @fopen($root, $mode))
    {
        return false;
    }
    flock($fp, LOCK_EX);
    fwrite($fp, $data);
    flock($fp, LOCK_UN);
    fclose($fp);
    return true;
}

?>
<!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=utf-8" />
<title>Ayaya - 安装</title>
<link rel="stylesheet" type="text/css" href="./themes/admin/css/theme1.css" />
<link rel="stylesheet" type="text/css" href="./themes/admin/css/style.css" />
<link rel="stylesheet" type="text/css" href="./themes/admin/css/ayaya.css" />

<!--[if IE]>
<link rel="stylesheet" type="text/css" href="./themes/admin/css/ie-sucks.css" />
<![endif]-->
<script type="text/javascript" src="./includes/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">function parent_location(url){parent.location.href=url;}</script> 
</head>
<body>

<div id="container">
    	<div id="header">
        	<h2>[AYAYA安装] B20100605</h2>
    <div id="topmenu">
            	<ul>
                	<li class="current"><a href="###">安装</a></li>
              </ul>
          </div>
      </div>
        <div id="top-panel">
            <div id="panel">
                <ul>
                   
                </ul>
            </div>
      </div>
        <div id="wrapper">
            <div id="content" style="width:560px; padding-left: 200px; padding-right: 200px;">
                <p>
                
                 <form id="form" action="?post" method="post" enctype="multipart/form-data" target="ajax" >
        
        <h3 class="title">安装设置</h3>
  <div class="in">
    <div class="fieldwrapper">
      <label for="" class="styled">数据库地址</label>
      <div class="thefield">
        <input name="v[0]" type="text" value="localhost" />
      </div>
    </div>
    <div class="fieldwrapper">
      <label for="" class="styled">数据库名称</label>
      <div class="thefield">
        <input name="v[1]" type="text" value="" />
      </div>
    </div>
    <div class="fieldwrapper">
      <label for="" class="styled">数据库用户</label>
      <div class="thefield">
        <input name="v[2]" type="text" value="root" />
      </div>
    </div>
    <div class="fieldwrapper">
      <label for="" class="styled">数据库密码</label>
      <div class="thefield">
        <input name="v[3]" type="text" value="" />
      </div>
    </div>
    <div class="fieldwrapper">
      <label for="" class="styled">数据表前缀</label>
      <div class="thefield">
        <input name="v[4]" type="text" value="dodo_" />
      </div>
    </div>
    <div class="fieldwrapper">
      <label for="" class="styled">创始人</label>
      <div class="thefield">
        <input name="v[5]" type="text" value="admin" />
      </div>
    </div>
    <div class="fieldwrapper">
      <label for="" class="styled">创始人密码</label>
      <div class="thefield">
        <input name="v[6]" type="password" value=""/><br />* 5-20位
      </div>
    </div>
    <div class="fieldwrapper">
      <label for="" class="styled">允许覆盖数据表</label>
      <div class="thefield">
        <input name="v[7]" value="1" class="checkbox" type="checkbox"/>
      </div>
    </div>
    <div class="fieldwrapper">
      <label for="" class="styled">添加测试数据</label>
      <div class="thefield">
        <input name="v[8]" value="1" class="checkbox" type="checkbox"/>
      </div>
    </div>
    
    <div class="buttonsdiv">
      <input value="提交" type="submit" class="button" tabindex="4" onclick="msg('请等待');">
      <input value="重置" type="reset" class="button" tabindex="5">
    </div>
    <p><span id="msg"></span></p>
    <div class="notice" style="margin-left:0px"><strong>使用协议</strong>: 
<a href="http://www.creativecommons.cn/licenses/by-nd-nc/1.0/">署名-非派生作品-非商业用途</a>, 详情<a href="http://www.creativecommons.cn/licenses/by-nd-nc/1.0/">[...]</a>
</div>
  </div>
</form>

  <script type="text/javascript">
function pp(){
	document.getElementById("msg").innerHTML='安装结束,准备跳转...';
	setTimeout("location.href='./?mod=admin'",200);
	
}
function msg(str){
	document.getElementById("msg").innerHTML=str+'...';	
}
</script>               
                </p>
                <p></p>
            </div>
            
  </div>
        <div id="footer">
        <div id="credits">
   		<a href="http://www.ayaya.tw">Ayaya.tw</a> 
        </div>
        <br />

        </div>
</div>
<iframe id="ajax" name="ajax" style="display:none;"></iframe>
</body>
</html>