www.gusucode.com > ShopEx481 & PHPWind 整合版码程序 > install/install.core.php

    <?php
define('IN_INSTALLER',true);
require(CORE_DIR.'/kernel.php');
class installCore extends kernel{

	function __construct(){
		if(file_exists('../config/install.lock')){
			$this->responseCode('403');
			echo 'Access denied by install.lock';
			exit();
		}else{

			header('Cache-Control: no-cache');
			header('Expires: Fri, 01 Jan 1999 00:00:00 GMT');

			set_error_handler(array(&$this,"errorHandler"));
			define('PHP_SELF',dirname($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']));
			$this->_halt_err_level = E_ERROR | E_USER_ERROR | E_CORE_ERROR;
			$this->_start = $this->microtime();
			$GLOBALS['system'] = &$this;

			set_include_path(CORE_DIR.'/include'.PATH_SEPARATOR.'.');
			require('defined.php');
			error_reporting(E_ALL);


			$cacheStorage = null;
			require('cachemgr.php');
			$this->cache = new cachemgr($this);
			$this->checkPerm();
			$this->run();
		}
	}

	function run(){

		$_GET['step'] = $_GET['step']?$_GET['step']:1;
		if(is_numeric($_GET['step'])){
			if($this->loadConfig()){
				$this->db = &$this->database();
				if($this->db->rwInstance(true)){
					if(preg_match('/[0-9\.]+/is',mysql_get_server_info(),$match) && version_compare($match[0],'4.1.1','<')){
						if(defined('DB_OLDVERSION') && DB_OLDVERSION){
							header('Location: index.php?step=2'); //正常
						}else{
							header('Location: index.php?step=mysqlver&type=less');
						}
					}else{
						if(defined('DB_OLDVERSION') && DB_OLDVERSION){
							header('Location: index.php?step=mysqlver&type=more');
						}else{
							header('Location: index.php?step=2'); //正常
						}
					}
				}
			}
		}
		$method = 'step_'.$_GET['step'];
		if(method_exists($this,$method)){
			call_user_func(array(&$this,$method));
		}else{
			header("HTTP/1.0 404 Not Found",true,404);
			echo '<h1>404 Not Found</h1><hr />';
		}
	}

	function loadConfig(){
		ob_start();
		if(include('../config/config.php')){
			if(defined('DB_NAME') && defined('DB_USER') && defined('DB_PASSWORD') && defined('DB_HOST')){
				ob_end_clean();
				return true;
			}
		}
		ob_end_clean();
		return false;
	}

	function installCore(){
		$this->__construct();
	}

	function step_checkdb(){
		if(!($link = mysql_connect($_POST['dbhost'], $_POST['uname'], $_POST['pwd']))){
			header("Cache-Control: no-cache, must-revalidate",true,503);
			exit();
		}
		$database = array();
		if($result = mysql_query('show databases')){
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				if($row['Database']!='mysql' && $row['Database']!='information_schema')
					$database[] = $row['Database'];
			}
		}
		mysql_free_result($result);
		mysql_close($link);
		if(count($database)>0){
			$html = '<select id="db_name" name="dbname">';
			foreach($database as $db){
				$html .= '<option value="'.htmlspecialchars($db).'">'.$db.'</option>';
			}
			echo $html .= '</select>';
		}
	}

	function step_1(){
		$this->output('step_1.html');
	}

	function step_2(){
		$this->pagedata['timezone'] = timezone_list();
		$this->pagedata['default_timezone'] = 8;
		$this->output('step_2.html');
	}

	/**
	 * step_mysqlver 提示用户版本过低
	 *
	 * @access public
	 * @return void
	 */
	function step_mysqlver(){

		$this->pagedata['type'] = $_GET['type'];
		$this->output('mysqlver.html');
	}

	function step_setdb(){

		define('DB_NAME', $_POST['dbname']);  // The name of the database
		define('DB_USER', $_POST['uname']);  // Your MySQL username
		define('DB_PASSWORD', $_POST['pwd']); // ...and password
		define('DB_HOST', $_POST['dbhost']);
		define('DB_PREFIX', $_POST['prefix_sdb']);
		define('DB_PREFIX_PW', $_POST['prefix_pw']);
		define('STORE_KEY',md5(print_r(microtime(),true).print_r($_SERVER,true)));


		$this->db = &$this->database();
		if(!$this->db->rwInstance(true)){
			$this->output('wrong_db.html');
		}else{
			if(preg_match('/[0-9\.]+/is',mysql_get_server_info(),$match) && version_compare($match[0],'4.1.1','<')){
				define('DB_OLDVERSION', true);
			}else{
				define('DB_OLDVERSION', false);
			}
			$sample = file_get_contents(BASE_DIR.'/config/config.sample.php');
			//PW
			$sample_pw = file_get_contents(BASE_DIR.'/bbs/data/sql_config.sample.php');

			$s = array('DB_NAME', 'DB_USER', 'DB_PASSWORD', 'DB_HOST','STORE_KEY','DB_PREFIX','DB_PREFIX_PW','DB_OLDVERSION');
			//PW
			$s_pw = array('$dbname' => $_POST['dbname'],
						 '$dbuser' => $_POST['uname'],
						 '$dbpw' => $_POST['pwd'],
						 '$dbhost' => $_POST['dbhost'],
						 '$PW' => $_POST['prefix_pw']);

			foreach($s as $v){
				if(defined($v)){
					$arr['#(define\\(\\s*[\'"]'.$v.'[\'"]\\s*,\\s*)[^;]+;#i'] = '\\1\''.str_replace('\'','\\\'',constant($v)).'\');';
				}else{
					echo 'error: undefined constant: '.$v;
					exit();
				}
			}
			if(RANDOM_HOME) $arr['#(define\\(\\s*[\'"]HOME_DIR[\'"]\\s*,\\s*)[^;]+;#i'] = '\\1dirname(__FILE__).\'/home'.'_'.substr(md5(microtime()),3,6).'\');';

			if(file_put_contents(BASE_DIR.'/config/config.php',preg_replace(array_keys($arr),array_values($arr),$sample))){
				header('Location: index.php?step=s3');
			}else{
				echo 'error: configure file write error! ';
				exit();
			}
			//PW
			foreach($s_pw as $k => $v)
			{

				$sample_pw .= $k . " = '" . $v . "';\r\n";

			}
			file_put_contents(BASE_DIR.'/bbs/data/sql_config.php', $sample_pw);
		}
	}

	function step_s2() {
		$check_dir = array(
			array(
				'name' => BASE_DIR . 'install/templates_c'
			),
			array(
				'name' => BASE_DIR . 'config'
			),
			array(
				'name' => BASE_DIR . 'home'
			),
			array(
				'name' => BASE_DIR . 'home/backup'
			),
			array(
				'name' => BASE_DIR . 'home/cache'
			),
			array(
				'name' => BASE_DIR . 'home/cache/admin_tmpl'
			),
			array(
				'name' => BASE_DIR . 'home/cache/front_tmpl'
			),
			array(
				'name' => BASE_DIR . 'home/download'
			),
			array(
				'name' => BASE_DIR . 'home/logs'
			),
			array(
				'name' => BASE_DIR . 'home/tmp'
			),
			array(
				'name' => BASE_DIR . 'home/upload'
			),
			/** PHPWIND BBS INSTALL **/
			array(
				'name' => BASE_DIR . 'bbs/attachment'
			),
			array(
				'name' => BASE_DIR . 'bbs/attachment/cn_img'
			),
			array(
				'name' => BASE_DIR . 'bbs/attachment/photo'
			),
			array(
				'name' => BASE_DIR . 'bbs/attachment/thumb'
			),
			array(
				'name' => BASE_DIR . 'bbs/attachment/upload'
			),
			array(
				'name' => BASE_DIR . 'bbs/data'
			),
			array(
				'name' => BASE_DIR . 'bbs/data/bbscache'
			),
			array(
				'name' => BASE_DIR . 'bbs/data/groupdb'
			),
			array(
				'name' => BASE_DIR . 'bbs/data/guestcache'
			),
			array(
				'name' => BASE_DIR . 'bbs/data/style'
			),
			array(
				'name' => BASE_DIR . 'bbs/data/tmp'
			),
			array(
				'name' => BASE_DIR . 'bbs/htm_data'
			),
			array(
				'name' => BASE_DIR . 'bbs/template'
			),
			array(
				'name' => BASE_DIR . 'bbs/template/admin'
			),
			array(
				'name' => BASE_DIR . 'bbs/template/wind'
			)
		);
		for($i = 0; $i < count($check_dir); $i++) {
			if(is_readable($check_dir[$i]['name'])) {
				$check_dir[$i]['r'] = 1;
			}
			else {
				$check_dir[$i]['r'] = 0;
			}
			if(is_writable($check_dir[$i]['name'])) {
				$check_dir[$i]['w'] = 1;
			}
			else {
				$check_dir[$i]['w'] = 0;
			}
		}
		$this->pagedata['dir'] = $check_dir;
		$this->output('step_2.html');
	}
	//source step_ready
	function step_s3() {
		if($this->loadConfig()) {
			include('svinfo.php');
			$tester = new mdl_serverinfo();
			$this->pagedata['svinfo'] = $tester->run();
			$this->pagedata['status'] = 0;
			$this->db = &$this->database();
			if(!$this->db->rwInstance(true)){
				echo 'error: can\'t connect MySql Server! '.mysql_error();

				exit();
			}
			if(!defined('DB_PREFIX')){
				define('DB_PREFIX','');
			}
			$this->pagedata['db_pre'] = DB_PREFIX;
			$this->pagedata['db_pre_pw'] = DB_PREFIX_PW;
			$defaultTimezone = 8;

			$timezone = array();
			for($i=-12;$i<13;$i++){
				if($i>0){
					$timezone[$i] = '+'.$i;
				}elseif($i==0){
					$timezone[$i] = 'GMT';
				}else{
					$timezone[$i] = $i;
				}
			}

			$timelist = array();
			$min = date('i');
			$hour = date('H',time()+$defaultTimezone*3600);
			for($i=0;$i<24;$i++){
				$timelist[$i] = str_pad($i,2,'0',STR_PAD_LEFT).':'.$min;
			}

			$this->pagedata['defaultTimezone'] = $defaultTimezone;
			$this->pagedata['defaultHour'] = $hour;
			$this->pagedata['timezone'] = &$timezone;
			$this->pagedata['timelist'] = &$timelist;

			$this->output('step_3.html');
		}else header('Location: index.php?step=1');
	}
	//source step_install
	function step_s4() {
		set_time_limit(0);
		if($this->loadConfig()) {
			$this->db = &$this->database();
			$sqlUtility = $this->loadModel('utility/sql');


			if(!defined('DB_OLDVERSION') || !DB_OLDVERSION){
				$this->db->exec('SET NAMES \''.DB_CHARSET.'\'');
			}
			//get url start
			$uname = $_POST['uname']?$_POST['uname']:'admin';
			$password = $_POST['password'];
			$url_f = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
			$url = dirname($url_f) . '/';
			$url_shopex = $url . 'bbs/';
			$url_arr = explode('/install/', $url_shopex);
			$url_shopex = $url_arr[0] . '/' . $url_arr[1];
			$url_pw = substr($url, 0,-9);
			$in_shopex_arr = array('passport.phpwind.config.URL' => $url_shopex,
								   'passport.phpwind.config.PrivateKey' => 'shopexphpwind',
								   'passport.phpwind.config.conntype' => $_POST['type'],
								   'passport.phpwind.config.encoding' => 'utf8',
								   'passport.config.current_use' => 'phpwind');
			$in_shopex = serialize($in_shopex_arr);
			//PW plugin start
			include_once('dbscripts/pw_plugin.php');
			$plugin = $_POST['plugin'];
			$hackdb = array();
			foreach ($plugin as $a)
			{
				$hackdb[$a] = $db_hackdb[$a];
			}
			$ser_hackdb = serialize($hackdb);
			$str_hackdb = var_export($hackdb, true);
			//PW plugin end
			//PW alimama start
			$ad = $_POST['ad'];
			foreach ($ad as $b)
			{
				${$b} = 1;
			}
			$db_hash = '';
			mt_srand((double)microtime()*1000000);
			$rand = '0123%^&*45ICV%^&*B6789qazw~!@#$sxedcrikolpQWER%^&*TYUNM';
			$randlen = strlen($rand);
			for ($i=0;$i<10;$i++) {
				$db_hash .= $rand[mt_rand(0,$randlen)];
			}
			$db_windmagic = 1;
			mt_srand((double)microtime()*1000000);
			$db_siteid      = $this->generatestr(16);
			$db_siteownerid = $this->generatestr(18);
			$db_sitehash = '10'.$this->SitStrCode(md5($db_siteid.$db_siteownerid),md5($db_siteownerid.$db_siteid));
			$banners = (int)$banners; $atcbottoms = (int)$atcbottoms; $footers = (int)$footers;
			if ($banners || $atcbottoms || $footers) {
				$ad = "<img src=\"http://init.phpwind.com/init_agent.php?sitehash=$db_sitehash&v=$wind_version&c=$ceversion&referer=$_SERVER[HTTP_HOST]&banner=$banners&atcbottom=$atcbottoms&footer=$footers\" width=\"0\" height=\"0\">";
				$this->pagedata['ad'] = $ad;
			}
			else
			{
				$this->pagedata['ad'] = '';
			}
			//PW alimama end


			$in_shopex_sql = "INSERT INTO `sdb_settings` (`s_name`, `s_data`, `s_time`, `disabled`) VALUES ('plugin', '" . $in_shopex . "', 1224125041, 'false');";
			//判断整合方式
			if($_POST['type'] == 'service') {
				$in_pw_sql = "UPDATE " . DB_PREFIX_PW . "config SET db_value='client' WHERE db_name='db_ppttype';";
			} else {
				$in_pw_sql = "UPDATE " . DB_PREFIX_PW . "config SET db_value='server' WHERE db_name='db_ppttype';";
			}

			$in_pw_sql .= "UPDATE " . DB_PREFIX_PW . "config SET db_value='" . $url_pw . "' WHERE db_name='db_pptserverurl';UPDATE " . DB_PREFIX_PW . "config SET db_value='" . $url_arr[0] . "' WHERE db_name='db_ppturls';UPDATE " . DB_PREFIX_PW . "config SET db_value='1' WHERE db_name='db_pptcmode';UPDATE " . DB_PREFIX_PW . "config SET db_value='shopexphpwind' WHERE db_name='db_pptkey';INSERT INTO " . DB_PREFIX_PW . "config (`db_name`, `db_value`, `decrip`) VALUES('db_bbsurl', '" . $url_pw . "/bbs', '');INSERT INTO " . DB_PREFIX_PW . "config (`db_name`, `db_value`, `decrip`) VALUES('db_ceoconnect', '" . $url_pw . "/bbs/sendemail.php?username=" . $uname . "', '');INSERT INTO " . DB_PREFIX_PW . "members (`uid`, `username`, `password`, `safecv`, `email`, `publicmail`, `groupid`, `memberid`, `groups`, `icon`, `gender`, `regdate`, `signature`, `introduce`, `oicq`, `icq`, `msn`, `yahoo`, `site`, `location`, `honor`, `bday`, `receivemail`, `lastaddrst`, `yz`, `timedf`, `style`, `datefm`, `t_num`, `p_num`, `attach`, `hack`, `signchange`, `newpm`, `newrp`, `banpm`, `showsign`, `msggroups`, `medals`, `payemail`) VALUES(1, '" . $uname . "', '" . md5($password) . "', '', 'admin@admin.com', 1, 3, 8, '', '', 0, 1224222025, '', '', '', '', '', '', '', '', '', '0000-00-00', 1, '', 1, '', '', '', 0, 0, '', '0', 0, 0, 0, '', 1, '', '', '');INSERT INTO " . DB_PREFIX_PW . "administrators (`uid`, `username`, `groupid`, `groups`) VALUES(1, '". $uname ."', 3, '');INSERT INTO " . DB_PREFIX_PW . "config (`db_name`, `db_value`, `decrip`) VALUES('db_hackdb', '" . $ser_hackdb . "', '');UPDATE " . DB_PREFIX_PW . "config SET `db_value` = '" . $db_hash . "' WHERE db_name = 'db_hash';UPDATE " . DB_PREFIX_PW . "config SET `db_value` = '" . $db_windmagic . "' WHERE db_name = 'db_windmagic';UPDATE " . DB_PREFIX_PW . "config SET `db_value` = '" . $db_siteid . "' WHERE db_name = 'db_siteid';UPDATE " . DB_PREFIX_PW . "config SET `db_value` = '" . $db_siteownerid . "' WHERE db_name = 'db_siteownerid';UPDATE " . DB_PREFIX_PW . "config SET `db_value` = '" . $db_sitehash . "' WHERE db_name = 'db_sitehash';";
			//get url end

			$sqlContent = file_get_contents('dbscripts/mysql.sql');
			$sqlContent .= file_get_contents('dbscripts/init.sql');

			//PW整合包,插入shopex
			$sqlContent .= $in_shopex_sql;

			if($_POST['use_demo']){
				$sqlContent .= file_get_contents('dbscripts/demo.sql');
			}

			//PW整合包,插入PW数据库
			$sqlContent .= str_replace('pw_', DB_PREFIX_PW, file_get_contents('dbscripts/pw_mysql.sql'));
			$sqlContent .= str_replace('pw_', DB_PREFIX_PW, file_get_contents('dbscripts/pw_init.sql'));
			$sqlContent .= $in_pw_sql;
			//insert pw file
			$in_pw_file = file_get_contents(BASE_DIR.'bbs/data/bbscache/config.php');

			//判断整合方式
			if($_POST['type'] == 'service') {
				$in_pw_file .= '$db_ppttype=' . "'client';\r\n";
			} else {
				$in_pw_file .= '$db_ppttype=' . "'server';\r\n";
			}
			$in_pw_file .= '$db_pptserverurl=' . "'" . $url_pw . "';\r\n";
			$in_pw_file .= '$db_ppturls=' . "'" . $url_arr[0] . "';\r\n";
			$in_pw_file .= '$db_pptcmode=' . "'" . '1' . "';\r\n";
			$in_pw_file .= '$db_pptkey=' . "'" . 'shopexphpwind' . "';\r\n";
			$in_pw_file .= '$db_bbsurl=' . "'" . $url_pw . "/bbs';\r\n";
			$in_pw_file .= '$db_ceoconnect=' . "'" . $url_pw . "/bbs/sendemail.php?username=" . $uname . "';\r\n";
			$in_pw_file .= '$db_hackdb=' . "{$str_hackdb};\r\n";
			file_put_contents(BASE_DIR.'/bbs/data/bbscache/config.php', $in_pw_file);
			//PW END

			foreach($sqlUtility->splitSql($sqlContent) as $sql){
				if(defined('DB_OLDVERSION') && DB_OLDVERSION){
					$sql = str_replace('DEFAULT CHARACTER SET utf8','',$sql);
				}
				if(!$this->db->exec($sql,true)){
					echo '<textarea style="width:500px;height:300px">'.$this->db->_lastInstance->sql.'</textarea>';
					echo $this->db->errorInfo();
					exit();
				}
			}


			$rs = $this->db->exec('select * from sdb_operators where 0=1');
			$sql = $this->db->getInsertSQL($rs,array(
				'username'=>$uname,
				'userpass'=>md5($password),
				'status'=>1,
				'super'=>1
			));

			//PW写入创始人信息start
			$sample_pw = file_get_contents(BASE_DIR.'/bbs/data/sql_config.php');
			$u_pw = $uname;
			$p_pw = md5($password);
			$sample_pw .= '$manager' . " = array('" . $u_pw . "');\r\n";
			$sample_pw .= '$manager_pwd' . " = array('" . $p_pw . "');\r\n";
			file_put_contents(BASE_DIR.'/bbs/data/sql_config.php', $sample_pw);
			//PW写入创始人信息end

			if(!$this->db->exec($sql,true)){
				echo 'error: cannot create administrator account.';
				exit();
			}
			if(RANDOM_HOME){
				if(!is_dir(HOME_DIR)){
					if(!mkdir(HOME_DIR,0777)){
						echo 'error: can\' mk home dir';
						exit();
					}
				}

				if(
					!mkdir_p(HOME_DIR.'/cache/data') ||
					!mkdir_p(HOME_DIR.'/cache/front_tmpl') ||
					!mkdir_p(HOME_DIR.'/cache/admin_tmpl') ||
					!mkdir_p(HOME_DIR.'/download') ||
					!mkdir_p(HOME_DIR.'/fonts') ||
					!mkdir_p(HOME_DIR.'/logs') ||
					!mkdir_p(HOME_DIR.'/template') ||
					!mkdir_p(HOME_DIR.'/upload')
				){
					echo 'error: can\' mk home dir';
					exit();
				}
			}

			$serverTimezone = date('H')+$_POST['timezone']-$_POST['localtime'];
			$this->setConf('system.timezone.server',$serverTimezone);
			$this->setConf('system.timezone.default',$_POST['timezone']);

			$this->init();

			$this->pagedata['password'] = $password;
			$this->pagedata['uname'] = $uname;

			file_put_contents(BASE_DIR.'config/install.lock','If you want to reinstall system, delete this file!');
			$this->output('step_4.html');

			exit();
		}
		header('Location: index.php?step=1');
	}

	function step_ready(){
		if($this->loadConfig()) {

			include('svinfo.php');
			$tester = new mdl_serverinfo();
			$this->pagedata['svinfo'] = $tester->run();
			$this->pagedata['status'] = 0;
			$this->db = &$this->database();
			if(!$this->db->rwInstance(true)){
				echo 'error: can\'t connect MySql Server! '.mysql_error();

				exit();
			}
			if(!defined('DB_PREFIX')){
				define('DB_PREFIX','');
			}
			$this->pagedata['db_pre'] = DB_PREFIX;

			$defaultTimezone = 8;

			$timezone = array();
			for($i=-12;$i<13;$i++){
				if($i>0){
					$timezone[$i] = '+'.$i;
				}elseif($i==0){
					$timezone[$i] = 'GMT';
				}else{
					$timezone[$i] = $i;
				}
			}

			$timelist = array();
			$min = date('i');
			$hour = date('H',time()+$defaultTimezone*3600);
			for($i=0;$i<24;$i++){
				$timelist[$i] = str_pad($i,2,'0',STR_PAD_LEFT).':'.$min;
			}

			$this->pagedata['defaultTimezone'] = $defaultTimezone;
			$this->pagedata['defaultHour'] = $hour;
			$this->pagedata['timezone'] = &$timezone;
			$this->pagedata['timelist'] = &$timelist;

			$this->output('step_ready.html');
		}else header('Location: index.php?step=1');
	}

	function step_cluster(){
		if($this->loadConfig()) {
			$this->output('step_cluster.html');
		}else header('Location: index.php?step=1');
	}

	function step_install(){

		set_time_limit(0);

		if($this->loadConfig()) {
			$this->db = &$this->database();
			$sqlUtility = $this->loadModel('utility/sql');


			if(!defined('DB_OLDVERSION') || !DB_OLDVERSION){
				$this->db->exec('SET NAMES \''.DB_CHARSET.'\'');
			}

			$sqlContent = file_get_contents('dbscripts/mysql.sql');
			$sqlContent .= file_get_contents('dbscripts/init.sql');
			if($_POST['use_demo']){
				$sqlContent .= file_get_contents('dbscripts/demo.sql');
			}

			foreach($sqlUtility->splitSql($sqlContent) as $sql){
				if(defined('DB_OLDVERSION') && DB_OLDVERSION){
					$sql = str_replace('DEFAULT CHARACTER SET utf8','',$sql);
				}
				if(!$this->db->exec($sql,true)){
					echo '<textarea style="width:500px;height:300px">'.$this->db->_lastInstance->sql.'</textarea>';
					echo $this->db->errorInfo();
					exit();
				}
			}

			$password = $_POST['password'];
			$uname = $_POST['uname']?$_POST['uname']:'admin';
			$rs = $this->db->exec('select * from sdb_operators where 0=1');
			$sql = $this->db->getInsertSQL($rs,array(
				'username'=>$uname,
				'userpass'=>md5($password),
				'status'=>1,
				'super'=>1
			));
			if(!$this->db->exec($sql,true)){
				echo 'error: cannot create administrator account.';
				exit();
			}
			if(RANDOM_HOME){
				if(!is_dir(HOME_DIR)){
					if(!mkdir(HOME_DIR,0777)){
						echo 'error: can\' mk home dir';
						exit();
					}
				}

				if(
					!mkdir_p(HOME_DIR.'/cache/data') ||
					!mkdir_p(HOME_DIR.'/cache/front_tmpl') ||
					!mkdir_p(HOME_DIR.'/cache/admin_tmpl') ||
					!mkdir_p(HOME_DIR.'/download') ||
					!mkdir_p(HOME_DIR.'/fonts') ||
					!mkdir_p(HOME_DIR.'/logs') ||
					!mkdir_p(HOME_DIR.'/template') ||
					!mkdir_p(HOME_DIR.'/upload')
				){
					echo 'error: can\' mk home dir';
					exit();
				}
			}

			$serverTimezone = date('H')+$_POST['timezone']-$_POST['localtime'];
			$this->setConf('system.timezone.server',$serverTimezone);
			$this->setConf('system.timezone.default',$_POST['timezone']);

			$this->init();

			$this->pagedata['password'] = $password;
			$this->pagedata['uname'] = $uname;

			file_put_contents(BASE_DIR.'config/install.lock','If you want to reinstall system, delete this file!');
			$this->output('step_install.html');

			exit();
		}
		header('Location: index.php?step=1');
	}

	function init(){
		$ver = $this->version();
		$db = &$this->database();
		$db->exec('drop table if exists sdb_dbver');
		$ver['dbver'] = $ver['dbver']?$ver['dbver']:'dbver';
		$db->exec("create table sdb_dbver(`{$ver['dbver']}` varchar(255)) type = MYISAM");

		$o = &$this->loadModel('content/sitemap');
		$o->update();

		$o = &$this->loadModel('system/template');
		$result=$o->setDefault('zqzy');
		$usedTpl = $o->getDefault();
		$o->getList();
	}

	function step_reinstall(){
		$this->step_install();
	}

	function output($file){
		header('Content-type: text/html;charset=utf-8');
		$this->pagedata['PAGE'] = $file;
		$this->pagedata['version'] = $this->version();
		$smarty = &$this->template();
		$smarty->ctl = &$this;
		$smarty->plugins_dir[] = CORE_DIR.'/admin/smartyplugin';
		$smarty->template_dir = 'view';
		if(defined('HOME_DIR')){
			$smarty->compile_dir = HOME_DIR.'/cache/front_tmpl';
		}
		$smarty->left_delimiter='{';
		$smarty->right_delimiter='}';
		foreach($this->pagedata as $k=>$v){
			$smarty->assign($k,$v);
		}
		$smarty->display('main.html');
		exit();
	}

	function checkPerm(){
		if(!is_writable('templates_c')){
			echo '<strong>Error:</strong> <i>install/templates_c</i> is not writable!';
			exit();
		}
		$arr = array(
			array('path'=>'install/templates_c','is_writable'=>true)
		);

		chdir('../');
		$this->_checkDirPerm('config',$arr);
		$this->_checkDirPerm('home',$arr,true);
		chdir('install');

		foreach($arr as $item){
			if(!$item['is_writable']){
				$this->pagedata['dirs'] = $arr;
				$this->output('permview.html');
				exit();
			}
		}
	}

	function _checkDirPerm($dir,&$arr,$r=false){

		$arr[] = array(
			'path'=>$dir,
			'is_writable'=>is_writable($dir),
		);

		if($r){
			if($handle = opendir($dir)){
				while(false != ($file = readdir($handle))){
					if($file{0}!='.' && is_dir($dir.'/'.$file)){
						$this->_checkDirPerm($dir.'/'.$file,$arr,$r);
					}
				}
				closedir($handle);
			}
		}
	}

	function base_url(){
		return dirname(dirname($_SERVER["PHP_SELF"]));
	}

	//-------------------------PW FUNCTION--------------------------------------------//
	function generatestr($len) {
		mt_srand((double)microtime() * 1000000);
		$keychars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWYXZ";
		$maxlen = strlen($keychars)-1;
		$str = '';
		for ($i=0;$i<$len;$i++){
			$str .= $keychars[mt_rand(0,$maxlen)];
		}
		return substr(md5($str.time().$_SERVER['HTTP_USER_AGENT'].$GLOBALS['db_hash']),0,$len);
	}
	function SitStrCode($string,$key,$action='ENCODE'){
		$string	= $action == 'ENCODE' ? $string : base64_decode($string);
		$len	= strlen($key);
		$code	= '';
		for($i=0; $i<strlen($string); $i++){
			$k		= $i % $len;
			$code  .= $string[$i] ^ $key[$k];
		}
		$code = $action == 'DECODE' ? $code : str_replace('=','',base64_encode($code));
		return $code;
	}
}
?>