<?php
if(!defined('IN_MICECMS')){header("HTTP/1.0 404 Not Found");die();}
class restoreDB {
	public $results;
	public $ExeResults;

	function restoreDB($filename) {
			$dropfilter = false;
			$this->results='';

			if('gz'==FileExtName($filename)){
				$f=fopen($filename, 'rb');
				fseek($f, -4, SEEK_END);
				$len = fread($f, 4);
				fclose($f);
				$len = unpack("V", $len);
				$len = end($len);

				$f = gzopen($filename, "rb");
				$query = gzread($f, $len);
				gzclose($f);
			}else{
				$query = file_get_contents($filename);
				if('php'==FileExtName($filename)){
					 $query = StrReplaceOnce("<?php die;?>", '', $query);
				}
			}
						

			// PostgreSQL fix
			if (strstr(MICE_DB,'postgre')){
				$query = preg_replace(array('/id int(?:eger) AUTO_INCREMENT/i',
										'/\\s+LONGTEXT,/'), array('id SERIAL',
										' TEXT,'), $query);
			}elseif (strstr(MICE_DB,'mssql')){
				$query = preg_replace(array('/id int(?:eger) AUTO_INCREMENT/i',
										'/\\s+LONGTEXT,/'), array('id INTEGER IDENTITY (1, 1) NOT NULL',
										' TEXT,'), $query);
			}elseif (strstr(MICE_DB,'sybase')){
				$query = preg_replace(array('/id int(?:eger) AUTO_INCREMENT/i',
										'/\\s+LONGTEXT,/'), array('id NUMERIC(10,0) IDENTITY',
										' TEXT,'), $query);
			}
			
			if (strstr(MICE_DB,'mysql')){
				$query =str_replace('tableset','DEFAULT CHARSET=utf8',$query);
			}else{
				$query =str_replace('tableset','',$query);
			}

			$query_arr = SplitSQL($query, true);
			$err = 0;
			if(MICE_DB=='txt'){
				$db_type='gladius';
			}else{
				$db_type=MICE_DB;
			}
			$conn2 = new DBBase($db_type);
			$tablename='';
			foreach($query_arr as $query) {
				$this->results.=$query."&#13;&#10;";
				if (strpos($query, 'DROP TABLE #__')===0  AND strstr(MICE_DB,'mssql')){
					if($tablename){
						$conn2->Close();
						$conn2 = new DBBase($db_type);
						@$conn2->Execute('SET IDENTITY_INSERT '.$tablename.' OFF;');
					}
					$tablename =str_replace('DROP TABLE','',$query);
					$tablename =str_replace(';','',$tablename);
				}

				if (strpos($query, "SELECT setval('#__")===0  AND !strstr(MICE_DB,'postgre')){
					continue;
				}

				if (strpos($query, 'DROP TABLE #__')===0){
					@$conn2->Execute($query);
				}elseif(!$dropfilter){
					if ($conn2->Execute($query)===false) {
						$err++;
						$this->results.=$query.'&#13;&#10;错误: '.$conn2->ErrorMsg().'&#13;&#10;';
						flush();
						continue;
					}
					if($conn2->ErrorMsg()){
						$this->results.='&#13;&#10;警告: '.$conn2->ErrorMsg().'&#13;&#10;';
					}
				}
				if($tablename AND strpos($query, 'CREATE TABLE #__')===0 AND strstr(MICE_DB,'mssql')){
					$conn2->Close();
					$conn2 = new DBBase($db_type);
					@$conn2->Execute('SET IDENTITY_INSERT '.$tablename.' ON;');
				}
			}
			if($tablename AND strstr(MICE_DB,'mssql')){
				//当数据库为mssql时,设置最后一个表
				$conn2->Close();
				$conn2 = new DBBase($db_type);
				@$conn2->Execute('SET IDENTITY_INSERT '.$tablename.' OFF;');
			}

			$sqlcount = count($query_arr);
			
			$this->ExeResults='共执行了'.$sqlcount.'条SQL语句,执行成功了'.($sqlcount-$err).'条SQL语句';
			$this->results.='&#13;&#10;&#13;&#10;'.$this->ExeResults;
	}
}
?>