<?php

/**
 *      【橙创家校通v4.0】 (C)2010-2012 Tuntron Inc.
 *      
 *    	 http://www.tuntron.com   QQ：1121251729
 *
 *      &程序安装基类文件& BY CC
 */

if (!defined('IS_RUN')) die('Forbidden');

class tj_install extends tj_model {
	
	var $table = 'dbpre_users';
    var $key = 'uid';

    function __construct() {
        parent::__construct();
    }

    function tj_install() {
		$this->__construct();
    }
	
	
   function import($sql,$table,$db_charset)
{
	$fp  = fopen($sql, 'rb');
	$sql = fread($fp, filesize($sql));
	fclose($fp);
	$sql = str_replace("\r", "\n", str_replace(' `tj_', ' `'.$table, $sql));
	$sql = str_replace("\r", "\n", $sql);
	$sql = explode(";\n", trim($sql));
	foreach($sql as $key)
	{
		$key = trim($key);
		if($key)
		{
			if(substr($key, 0, 12) == 'CREATE TABLE')
			{
				$this->db->query($this->part_version($key, $db_charset));
			}
			else
			{
				$this->db->query($key);
			}
		} else {
			return false;
		}
	}
	return true;
	exit;
}
	function part_version($sql,$db_charset) {
		$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')) ? $type : 'MYISAM';
		return preg_replace("/^\s*(CREATE TABLE\s+.+\s+\(.+?\)).*$/isU", "\\1", $sql).
			(mysql_get_server_info() > '4.1' ? ' ENGINE='.$type.' DEFAULT CHARSET='.$db_charset : ' TYPE='.$type);
	}
	
	function create_admin($username,$pwd,$pwd2) {
		$post['username'] = $username;
		$post['realname'] = "系统管理员";
		$post['password'] = $pwd;
		$post['password2'] = $pwd2;
		G_class('user')->save($post,'1');
		
	}
	
	function create_siteinfo($sitename,$now_domain) {
		
		$post['SiteName'] = $sitename;
		$post['SiteUrl']  = $now_domain.'/';
		$post['SafeCode'] = md5(random());
		G_class('system')->update($post);
		
	}
	
	
}
?>