<?php 
class Database_Controller extends Base_Controller {
	function __construct(){
		parent::__construct();
		$this->dbmanager	=	new Dbmanager();
	}
	function export($sqlcompat=false,$sqlcharset=false,$sizelimit=false,$fileid=false,$random=false,$tableid=false,$startfrom=false,$tabletype=false){
		if($this->input->post()){
			$tables	=	$this->input->post('select');
			$export	=	$this->input->post('export');
			$this->dbmanager->export($tables,$export['sqlcompat'],$export['sqlcharset'],$export['sizelimit']);
			$this->main->message		=	"网站数据备份成功";
			$this->main->message_type	=	"success";
			$this->import();
		}
		else{
			if($random){
				$this->dbmanager->export(false,$sqlcompat,$sqlcharset,$sizelimit,$fileid,$random,$tableid,$startfrom,$tabletype);
				$this->main->message		=	"网站数据备份成功";
				$this->main->message_type	=	"success";
				$this->import();
			}
			else{
				$this->view	=	new View("export");
				$tables		=	$this->dbmanager->status();
				foreach($tables['lmpictables'] as $table){
					if(!strpos($table['name'],"session")){
						$newtables[]	=	$table;	
					}
				}
				$this->view->tables	=	$newtables;
				$this->main->formAction	=	"database/export";
				$this->display();
			}
		}
	}
	function import($filename=false,$fileid=1){
		if($filename){
			if($filename&&$fileid==1){
				$filename	=	base64_decode($filename);
				preg_match("#([0-9_]+)_(\d+)\.sql#i",$filename,$fname1);
				if(count($fname1)>0){
					$sqlfiles	=	file::scan_dir(DATADIR."bakup",false,array("sql","zip"));
					foreach ($sqlfiles as $tmpf){
						if(strpos($tmpf['path'],$fname1[1])>0){
							preg_match("#([0-9_]+)_(\d+)\.sql#i",$tmpf['path'],$fname2);
							$files[$fname2[2]]	=	$tmpf['path'];
						}
						else{
							continue;
						}
					}
				}
				else{
					$files[1]	=	$filename;
				}
				file_put_contents(DATADIR."cache/bakup_importfiles.php","<?php \r\n return ".var_export($files,true)."; \r\n ?>");
				$countfiles	=	count($files);
			}
			if($fileid>1){
				$files		=	include(DATADIR."cache/bakup_importfiles.php");
				$countfiles	=	count($files);
			}

			for($i=1;$i<=$countfiles;$i++){
				if($fileid==$i)	{
					if($this->dbmanager->import($files[$i])){
						
						if($i+1<=$countfiles){
							echo "成功导入数据--".basename($files[$i]);
							die( "<script>window.location='".BASEURI."database/import/".base64_encode($files[$i])."/".($i+1)."';</script>" );							
						}
						else{
							$this->main->message	=	"导入数据成功";
							$this->main->message_type=	"success";
						}
					}
					else{
						$this->main->message	=	"导入数据失败";
						$this->main->message_type=	"error";
					}
				}
			}
		}
		$this->view	=	new View("import");
		$this->main->formAction="database/import";
		$this->view->files	=	file::scan_dir(DATADIR."bakup",false,array("sql","zip"));
		$this->display();
	}
	function delete($file=false){
		if(@$this->input->post()){
			$files	=	$this->input->post('select');
			if(!is_array($files)){
				$this->main->message		=	"你没有指定需要删除的文件";
				$this->main->message_type	=	"notice";
			}
		}
		else{
			if($file){
				$files[]	=	$file;
			}
			else{
				$this->main->message		=	"你没有指定需要删除的文件";
				$this->main->message_type	=	"notice";
			}
		}
		if(is_array($files)){
			foreach ($files as $tmp){
				@unlink(base64_decode($tmp));
			}
			$this->main->message		=	"删除已成功";
			$this->main->message_type	=	"success";
		}
		unset($_POST);
		$this->import();
	}
}