<?php

/**
 *      【橙创家校通v4.0】 (C)2010-2012 Tuntron Inc.
 *      
 *    	 http://www.tuntron.com   QQ：1121251729
 *
 *      &数据库备份还原类文件&
 */

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

class tj_databaseOP extends tj_mysql{
	
	var $dbinfo = '';
	var $backup_file;
	var $dbname = '';
	
	function __construct(){
		global $_G;
		parent::__construct();
		$this->dbinfo = $_G['dbinfo'];
		$this->dbname = $_G['dbinfo']['dbname'];
		$this->connect();
		$this->select_db($_G['dbinfo']['dbname']);
	}
	
	function restore($filename){
		$backup_file = TUNTRON_DAT . '/backup/' . $filename;
		if(file_exists($backup_file)){
			$sql = file_get_contents($backup_file);
			$sql = str_replace("", "\n", $sql);
			$sqlArr = explode(';', $sql);
			$i=1;
			foreach($sqlArr as $key=>$value){
				if(mysql_query($value)){
					$i++;
				}
			}
			if($i == count($sqlArr)){
				return '1';
			}else{
				return '0';
			}
		}else{
			return '0';
		}
	}
	
	function backup(){
		$sql_list = mysql_list_tables($this->dbname);
		$filename = $this->generate_password('15');
		$this->backup_file = TUNTRON_DAT . '/backup/' . $filename . '.sql';
		chmod($this->backup_file, 0777);
		
		$fow = fopen($this->backup_file, 'w+');
		$foa = fopen($this->backup_file, 'a+');
		$time = date("Y-m-d H:i:s", time());
		$result = fwrite($fow, "\n");
		if(!$result){
			return '0';
		}
		
		while ($row_data = mysql_fetch_row($sql_list)){
			$tablename = $row_data[0];
			$this->get_table_fd($tablename, $foa);
			$this->get_table_value($tablename, $foa);
		}
		
		fclose($fow);
		fclose($foa);
		return '1';
	}
	
	private function get_table_fd($tablename, $resource){
		$field = "DROP TABLE IF EXISTS `$tablename`;\n";
		$createtable = mysql_query("SHOW CREATE TABLE `$tablename`");
		$create = mysql_fetch_assoc($createtable);
		$field .= $create['Create Table'] . ";\n";
		fwrite($resource, $field);
	}
	
	private function get_table_value($tablename, $resource){
		$result1 = mysql_query("SELECT * FROM $tablename");
		while($meta = mysql_fetch_field($result1)){
			$field[] = '`' . $meta->name . '`';
		}
		
		$result2 = mysql_query("SELECT * FROM $tablename");
		while($meta2 = mysql_fetch_assoc($result2)){
			$varr = array();
			foreach($meta2 as $key=>$value){
				$varr[] = "'$value'";
			}
			$vstr = implode(',', $varr);
			$string = "INSERT INTO `$tablename`(" . implode(',', $field) . ")VALUES($vstr);\n";
			fwrite($resource, $string);
		}
		fwrite($resource, "\n");		
	}
	
	function generate_password($length='20'){
    	// 密码字符集，可任意添加你需要的字符
    	//$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
    	$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
 		$string = '';
    	for($i = 0; $i < $length; $i++ ){
        	// 这里提供两种字符获取方式
        	// 第一种是使用 substr 截取$chars中的任意一位字符；
        	// 第二种是取字符数组 $chars 的任意元素
        	// $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        	$string .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    	}
 		return $string;
	}	
	
}