<?php
/**
 * @version        Somao_Search X1.0 9:23 2012-1-1 Somao $
 * @package        Somao_Search.Site
 * @copyright      Copyright (c) 2009 - 2012, Somao, Inc.
 * @license        http://www.somao123.com/
 * @link           http://www.somao.cc/
 */
@set_time_limit(0);
//error_reporting(E_ALL);
error_reporting(E_ALL || ~E_NOTICE);
$insLockfile = dirname(__FILE__).'/install_lock.txt';
define('DEDEROOT',preg_replace("#[\\\\\/]install#", '', dirname(__FILE__)));
require_once(DEDEROOT.'/install/install.inc.php');
foreach(Array('_GET','_POST','_COOKIE') as $_request)
{
    foreach($$_request as $_k => $_v) ${$_k} = RunMagicQuotes($_v);
}
if(file_exists($insLockfile))
{
    exit(" 程序已运行安装，如果你确定要重新安装，请先从FTP中删除 install/install_lock.txt！");
}

if(empty($step))
{
    $step = 1;
}
/*------------------------
使用协议书
function _1_Agreement()
------------------------*/
if($step==1)
{
    include('./templates/step-1.html');
    exit();
}
/*------------------------
环境测试
function _2_TestEnv()
------------------------*/
else if($step==2)
{
    $phpv = phpversion();
    $sp_os = PHP_OS;
    $sp_gd = gdversion();
    $sp_server = $_SERVER['SERVER_SOFTWARE'];
    $sp_host = (empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_HOST'] : $_SERVER['REMOTE_ADDR']);
    $sp_name = $_SERVER['SERVER_NAME'];
    $sp_max_execution_time = ini_get('max_execution_time');
    $sp_allow_reference = (ini_get('allow_call_time_pass_reference') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
    $sp_allow_url_fopen = (ini_get('allow_url_fopen') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
    $sp_safe_mode = (ini_get('safe_mode') ? '<font color=red>[×]On</font>' : '<font color=green>[√]Off</font>');
    $sp_gd = ($sp_gd>0 ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');
    $sp_mysql = (function_exists('mysql_connect') ? '<font color=green>[√]On</font>' : '<font color=red>[×]Off</font>');

    if($sp_mysql=='<font color=red>[×]Off</font>')
    $sp_mysql_err = TRUE;
    else
    $sp_mysql_err = FALSE;

    $sp_testdirs = array(
        '/',
        '/data/*',
        '/a/*',
        '/install'
    );
    include('./templates/step-2.html');
    exit();
}
/*------------------------
设置参数
function _3_WriteSeting()
------------------------*/
else if($step==3)
{
    if(!empty($_SERVER['REQUEST_URI']))
    $scriptName = $_SERVER['REQUEST_URI'];
    else
    $scriptName = $_SERVER['PHP_SELF'];

    $basepath = preg_replace("#\/install(.*)$#i", '', $scriptName);

    if(!empty($_SERVER['HTTP_HOST']))
        $baseurl = 'http://'.$_SERVER['HTTP_HOST'];
    else
        $baseurl = "http://".$_SERVER['SERVER_NAME'];
    
    $rnd_cookieEncode = chr(mt_rand(ord('A'),ord('Z'))).chr(mt_rand(ord('a'),ord('z'))).chr(mt_rand(ord('A'),ord('Z'))).chr(mt_rand(ord('A'),ord('Z'))).chr(mt_rand(ord('a'),ord('z'))).mt_rand(1000,9999).chr(mt_rand(ord('A'),ord('Z')));

    if(file_get_contents($install_demo_name)) $isdemosign = 1;
    include('./templates/step-3.html');
    exit();
}
/*------------------------
普通安装
function _4_Setup()
------------------------*/
else if($step==4)
{
	$file_name="soso.sql"; //要导入的SQL文件名
	$fp = @fopen($file_name, "r") or die("不能打开SQL文件 $file_name");//打开文件
    $conn = mysql_connect($dbhost,$dbuser,$dbpwd) or die("<script>alert('数据库服务器或登录密码无效，\\n\\n无法连接数据库，请重新设定！');history.go(-1);</script>");
	mysql_query("CREATE DATABASE IF NOT EXISTS `".$dbname."`;",$conn);
    mysql_select_db($dbname) or die("<script>alert('选择数据库失败，可能是你没权限，请预先创建一个数据库！');history.go(-1);</script>");
    mysql_query("set names 'gbk'");
    while($SQL=GetNextSQL()){
     if (mysql_query($SQL)){
     }
    }
    fclose($fp) or die("Can't close file $file_name");//关闭文件
    mysql_close(); 
    $content.="<?php".chr(13).chr(10);
    $content.="\$dbhost = '$dbhost';".chr(13).chr(10);
    $content.="\$dbuser = '$dbuser';".chr(13).chr(10);
    $content.="\$dbpw = '$dbpwd';".chr(13).chr(10);
    $content.="\$dbname = '$dbname';".chr(13).chr(10);
    $content.="\$dbcharset = 'gbk';".chr(13).chr(10);
    $content.="\$dbpconnect = '0';".chr(13).chr(10);
    $content.="?>";
    $fp=@fopen("../data/db_config.php","w") or die("写方式打开文件失败，请检查程序目录是否为可写");//配置conn.php文件
    @fputs($fp,$content) or die("文件写入失败,请检查程序目录是否为可写"); 
    @fclose($fp);
    //end
     //锁定安装程序
     $fp = fopen($insLockfile,'w');
     fwrite($fp,'ok');
     fclose($fp);
     include('./templates/step-5.html');
     exit();
}
