<?php

class process{
    private $confile;
    private $sqlfile;
    private $info;


    public function __construct($confile=null, $sqlfile="zhcms.sql"){
        $this->confile=dirname(dirname(__FILE__))."/zh/init.php";
        $this->sqlfile=$sqlfile;
        $this->info="";
    }

    public function configsys($config){
        $configtext=file_get_contents($this->confile);
        foreach($config as $key => $val){
            $pattern[]='/define\(\"'.$key.'\",\s*.+\);/';
            $repcontent[]='define("'.$key.'", "'.$val.'");';
        }
        $configtext = preg_replace($pattern, $repcontent, $configtext);
        return file_put_contents($this->confile, $configtext);
    }

    public function createdb($user){
        $mysqli=new mysqli(DB_HOST, DB_USER, DB_PWD);
        if(mysqli_connect_errno()){
            $this->info.='<font color="red">链接失败,原因为:'.mysqli_connect_error().'</fonr>';
            $mysqli=false;
            return false;
        }
        if($mysqli->query('CREATE DATABASE IF NOT EXISTS '.DB_NAME)){
            $this->info.='创建数据库<b>'.DB_NAME.'</b>成功</br>';
        }else{
            $this->info.='<font color="red">创建数据库<b>'.DB_NAME.'</b>失败</fonr>';
            $mysqli->close();
            return false;
        }

        if($mysqli->select_db(DB_NAME)){
            $this->info.='数据库<b>'.DB_NAME.'</b>选择成功<br>';
        }else{
            $this->info.='<font color="red">选择数据库<b>'.DB_NAME.'</b>失败</font>';
            $mysqli->close();
            return false;
        }

        $sql=file_get_contents($this->sqlfile);
        $sql=str_replace("\r", "\n", str_replace(' zh_', ' '.TAB_FIX, $sql));
        $num=0;
        $mysqli->set_charset("utf8");
        foreach(explode(";\n", trim($sql)) as $query){   
            $queries = explode("\n", trim($query));
            foreach($queries as $query){
                @$ret[$num] .= $query[0] == '*' || $query[0].$query[1] == '--' ? '' : $query;
            }
            $num++;
        }
        unset($sql);
        foreach($ret as $query){
            $query = trim($query);
            if($query){
                if(substr($query, 0, 12) == 'CREATE TABLE'){
                    $name = preg_replace("/CREATE TABLE ([a-z0-9_]+) .*/is", "\\1", $query);
                    if($mysqli->query($query)){
                        $this->info.='建立数据表'.' <b>'.$name.'</b> ...'.'成功!</br>';
                    }else{
                        $this->info.='<font color="red">建立数据表'.' '.$name.' ... '.'失败!</font>';
                        $mysqli->close();
                        return false;
                    }
                }else{
                    if(!$mysqli->query($query)){
                        $this->info.='<font color="red">查询语句'.$query.'执行失败</font>';
                        $mysqli->close();
                        return false;
                    }
                }
            }
        }
        $insert="INSERT INTO ".TAB_FIX."user(gid, user, passwd, email, regtime) VALUES('1', '".$user['ADMIN_USER']."', '".substr(md5($user['ADMIN_PWD']), 5, 20)."', '".$user['ADMIN_MAIL']."', '".time()."')";
        if($mysqli->query($insert)){
            $this->info.='添加管理员用户<b>'.$user["ADMIN_USER"].'</b>成功</br>';
        }else{
            $this->info.='<font color="red">添加管理员用户<b>'.$user["ADMIN_USER"].'</b>失败</font>';
            $mysqli->close();
            return false;
        }
        $mysqli->close();
        return true;
    }

    public function getinstallinfo(){
        return $this->info;
    }

}

?>
