<?php 
include_once 'publicfunc/setstepfun.inc.php';
?>
<?php
header("Content-Type: text/html; charset=utf-8");                      //处理中文乱码
error_reporting(E_ERROR | E_WARNING | E_PARSE);                        //设置系统错误显示类型
set_time_limit(0);                                                     //页面执行时间无限制
set_magic_quotes_runtime(0);                                           //禁止服务器自动转义 
$urlPath = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];       //本页面的URL地址
if(PHP_VERSION < '4.1.0')                                              //如果PHP版本低于4.1,接受参数用以下方式
{
  $_GET = &$HTTP_GET_VARS;
  $_POST = &$HTTP_POST_VARS;
  $_COOKIE = &$HTTP_COOKIE_VARS;
  $_SERVER = &$HTTP_SERVER_VARS;
  $_ENV = &$HTTP_ENV_VARS;
  $_FILES = &$HTTP_POST_FILES;
}
foreach (array('_GET', '_POST', '_COOKIE') as $GPC)                      //接受参数并将值赋值给相应的参数
{
    foreach ($$GPC as $key => $value)
    {
        if(!set_magic_quotes_runtime(0))                                  //返回值0表示关闭，1表示开启                     
        {
            $$key = addslashes($value);
        }
        else
        {
            set_magic_quotes_runtime(0);                                 //禁止服务器自动转义 
            $$key = addslashes($value);                                  //调用函数addslashes转义
        }
    }
}

if(file_exists('../inc/install.lock'))                             //根据install.lock是否存在，标记是否该系统已经安装
{
  echo "<SCRIPT language=JavaScript>alert('File is lock, setup fail! 1 second back.');</SCRIPT>";
  $urlPathJump = substr($urlPath, 0, -18);
  echo "<META HTTP-EQUIV='refresh' content=\"1; url=$urlPathJump\"/>";
  exit;
}
else
{
    switch ($gpc)
    {
        case 'checkinfo': 
        {
            $systemCheckMsg = array();                   //系统信息检测数组
            $systemCheckFlag = array();                  //数组标记系统信息检测是否通过，默认通过（1：通过，0：不通过）
            $dirCheckMeg = array();                      //目录是否存在，是否可写状态检测后相关信息数组
            $dirPassFlag = array();                      //数组标记目录检测后是否通过，默认通过（1：通过，0：不通过）
            $checkPass = 1;                              //标记所用检测是否通过，默认不通过（1：通过，0：不通过）    
            if(function_exists(mysql_connect))
            {
                $systemCheckMsg['mysqlsupport'] = '支持';
            }
            else
            {
                $systemCheckMsg['mysqlsupport'] = '不支持MySQL数据库连接，请检测是否安装MySQL数据库';
                $systemCheckFlag['mysqlsupport'] = 0;
            }
            if('4.1.0' > PHP_VERSION)
            {
                $systemCheckMsg['phpVersion'] = '版本太低， 建议更新';
            }
            $dirCheck = array(
            '../admin',
            '../admin/inc',
            '../config',
            '../inc',
            '../upload'
            );
            $dirCheckCount = count($dirCheck);
            for($i = 0; $i < $dirCheckCount; $i++)
            {
                if(file_exists($dirCheck[$i]))
                {
                    if(is_writable($dirCheck[$i]))
                    {
                        $dirCheckMeg[$i] = '通过';
                        $dirPassFlag[$i] = 1;
                    }
                    else
                    {
                        $dirCheckMeg[$i] = '文件夹或者文件不可写';
                        $dirPassFlag[$i] = 0;
                    }
                }
                else
                {
                    $dirCheckMeg[$i] = '<font color="red">文件夹或者文件不存在，请安装文件中相关内容检测！</font>';
                    $dirPassFlag[$i] = 0;
                }
            }
            //循环遍历数组$systemCheckFlag，$dirPassFlag判断检测是否都通过
            foreach ($systemCheckFlag as $value)
            {
                if(0 == $value)
                {
                    $checkPass = 0;
                    break;
                }
            }
            if(1 == $checkPass)
            {
                foreach ($dirPassFlag as $value)
                {
                    if(0 == $value)
                    {
                        $checkPass = 0;
                        break;
                    }
                }
            }
            require_once 'templates/checkinfo.php';
            exit;
        } 
        case 'db':
        {
            if('dbFillIn' == $setUp)
            {
                $dbConfig = "<?php
                \$configTableHead = '$dbPrefix';
                \$configDBHost = '$dbHost';
                \$configDBUser = '$dbUserName';
                \$configDBPassword = '$dbPassWord';
                \$configDBName = '$dbName';
                \$configDefaultDBLang = 'utf8';
                \$configDefaultLang = 'utf-8';
                ?>";
                //echo $dbHost, $dbUserName, $dbPassWord;die();
                $dbConn = mysql_connect($dbHost, $dbUserName, $dbPassWord) or die('数据库连接失败'.mysql_error());   //连接数据库
                if(!mysql_select_db($dbName, $dbConn))                                                          //选择数据库
                {
                    $sql = "create database $dbName default character set utf8";
                    mysql_query($sql, $dbConn) or die("数据库创建失败".mysql_error());
                    mysql_select_db($dbName, $dbConn); 
                }
                $fileContent = InstallTool::getFileContent('kuwebs.sql');
                $installInfo = InstallTool::createTable($fileContent, $dbPrefix);
                if(is_writable('../config/config.db.php'))                 //判断文件是否是可写状态
                {
                    $file = fopen('../config/config.db.php', 'w+');            //打开文件流
                    fputs($file, $dbConfig);                                  //写入  
                    fclose($file);                                            //关闭文件流
                }
                require_once 'templates/dbsetup.php';
                exit;
            }
            else
            {
                require_once 'templates/dbfillin.php';
                exit;
            }
        } 
        case 'adminsetup':
        {
          if('adminSetUpFillIn' == $setUp)
          {
            require_once "../config/config.db.php";
            require_once "../inc/commonfunc.inc.php";
            $adminName = trim($adminName);
            $adminPassword = CommTool::encryptMd5(trim($adminPassword), 2);
            $dbConn = mysql_connect($configDBHost, $configDBUser, $configDBPassword) or die("数据库连接失败！".mysql_error());
            mysql_select_db($configDBName, $dbConn) or die("数据库选择失败！".mysql_error());
            $sql = "insert into {$configTableHead}admin (adminuser, adminpassword, adminsex)values ('{$adminName}', '{$adminPassword}', 1);";
            mysql_query($sql, $dbConn) or die("数据库数据插入失败！".mysql_error());
            require_once 'templates/finished.php';
            if(!file_exists('../inc/install.lock'))
          {
            $fp= fopen('../inc/install.lock', "w");
            fclose($fp);
          }
            exit; 
          }
          else
          {
            require_once 'templates/adminsetup.php';
            exit; 
          }
              
        } 
        default:
        {
            require_once 'templates/index.php';
            exit;   
        }                 
    }  
}
?>