<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mysql_manage extends Controller
{
	var $backup_path;
	
	function Mysql_manage()
	{
		parent::Controller();
		check_is_login();
		$this->load->database();
		$this->load->library('form_validation');
		$this->backup_path="backup/";
	}
	function index()
	{
		$this->load->helper("file");
		$this->load->helper("number");
		$file_arr=get_dir_file_info($this->backup_path);
		$data=array(
			"file_list"=>$file_arr,
			"folder_size"=>byte_format(getBytes($this->backup_path))
		);	
		$this->load->view(TPL_FOLDER."mysql_manage_list",$data);
	}
	function database_backup()
	{
		$this->load->dbutil();
		$backup=& $this->dbutil->backup();
		$this->load->helper("file");
		if(write_file($this->backup_path.date("YmdHis").".gz",$backup))
		{
			echo_msg('<li>数据库备份成功</li>','','yes');
		}
		else
		{
			echo_msg('<li>数据库备份失败</li>','','no');
		}
	}
	
	function download_database($f_name)
	{
		if($f_name=='')
		{
			return '';
		}
		$this->load->helper("download");
		$data=file_get_contents($this->backup_path.$f_name);
		force_download($f_name,$data);
	}
	
	function delete_backup()
	{
		$f_list=$this->input->post("rd_id");
		if(empty($f_list))
		{
			echo_msg('<li>提交的数据有误</li>','','no');
		}
		foreach($f_list as $f_name)
		{
			@unlink($this->backup_path.$f_name);
		}
		echo_msg('<li>文件删除成功</li>','','yes');
	}
	
	/*function manage_database_view()
	{
		$this->load->view(TPL_FOLDER."manage_database");
	}
	
	function output_to_csv()
	{
		$this->form_validation->set_rules("table_name","表名","trim|required");
		$tx_msg="";
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg.=validation_errors();
		}
		if($tx_msg!="")
		{
			echo_msg($tx_msg);
		}
		$table_name=$this->input->post("table_name");
		$this->load->dbutil();
		$query = $this->db->get($table_name);
		$data=$this->dbutil->csv_from_result($query); 
		$this->load->helper("download");
		force_download($table_name.".csv",$data);
	}
	
	function manage_database()
	{
		$this->form_validation->set_rules("op_type","操作类型","trim|required");
		$this->form_validation->set_rules("table_name","表名","trim|required");
		$tx_msg="";
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg.=validation_errors();
		}
		if($tx_msg!="")
		{
			echo_msg($tx_msg);
		}
		$this->load->dbutil();
		if($this->input->post("op_type")=='optimize')
		{
			if ($this->dbutil->optimize_table($this->input->post("table_name")))
			{
				echo_msg('<li>表优化成功</li>','','yes');
			}
			else
			{
				echo_msg('<li>表优化失败，可能是该数据库平台不支持表优化</li>');
			}
		}
		else
		{
			if ($this->dbutil->repair_table($this->input->post("table_name")))
			{
				echo_msg('<li>表修复成功</li>','','yes');
			} 
			else
			{
				echo_msg('<li>表修复失败，可能是该数据库平台不支持表修复</li>');
			}
		}
	}
	
	function do_query()
	{
		$this->form_validation->set_rules("sql_text","Sql语句","trim|required");
		$tx_msg="";
		if($this->form_validation->run()==FALSE)
		{
			$tx_msg.=validation_errors();
		}
		if($tx_msg!="")
		{
			echo_msg($tx_msg);
		}
		$files = preg_replace( "/(#.+[\r|\n]*)/" , '' , $this->input->post("sql_text"));
		$sqls = $this->split_sql_file( $files );
		
		foreach( $sqls as $sql )
		{
			$this->db->query(trim($sql));
		}
		echo_msg('<li>Sql语句执行成功</li>','','yes');
	}
	
	function split_sql_file($sql, $delimiter = ';') 
	{
		$sql               = trim($sql);
		$char              = '';
		$last_char         = '';
		$ret               = array();
		$string_start      = '';
		$in_string         = FALSE;
		$escaped_backslash = FALSE;
	
		for ($i = 0; $i < strlen($sql); ++$i) {
			$char = $sql[$i];
	
			// if delimiter found, add the parsed part to the returned array
			if ($char == $delimiter && !$in_string) {
				$ret[]     = substr($sql, 0, $i);
				$sql       = substr($sql, $i + 1);
				$i         = 0;
				$last_char = '';
			}
	
			if ($in_string) {
				// We are in a string, first check for escaped backslashes
				if ($char == '\\') {
					if ($last_char != '\\') {
						$escaped_backslash = FALSE;
					} else {
						$escaped_backslash = !$escaped_backslash;
					}
				}
				// then check for not escaped end of strings except for
				// backquotes than cannot be escaped
				if (($char == $string_start)
					&& ($char == '`' || !(($last_char == '\\') && !$escaped_backslash))) {
					$in_string    = FALSE;
					$string_start = '';
				}
			} else {
				// we are not in a string, check for start of strings
				if (($char == '"') || ($char == '\'') || ($char == '`')) {
					$in_string    = TRUE;
					$string_start = $char;
				}
			}
			$last_char = $char;
		} // end for
	
		// add any rest to the returned array
		if (!empty($sql)) {
			$ret[] = $sql;
		}
		return $ret;
	}*/
}
?>