<?
class mysql_dump
{
	var $INSERT_SPLIT 	= 100;
	var $SQL_SEP 			= '/* -----SEP----- */';

	function mysql_dump() {}

	function _mysqldump($file, $sql_drop_table=true, $sql_create_table=true, $sql_table_data=true, $sql_include_system=true) {
		$this->sql_drop_table		= $sql_drop_table;
		$this->sql_create_table		= $sql_create_table;
		$this->sql_table_data		= $sql_table_data;
		$this->sql_include_system	= $sql_include_system;

		$result=mysql_query(" SHOW TABLES ");
		if($result) {
			while($row=mysql_fetch_row($result)) {
				if(substr($row[0],0,1)=='_' && $this->sql_include_system!=true) continue; //systemowe
				$this->_mysqldump_table_structure($row[0]);
				if($this->sql_table_data==1) $this->_mysqldump_table_data($row[0]);
			}
		} else {
			return false;
		}
		mysql_free_result($result);
		file_put_contents($file, $this->SQL_DUMP);
		return true;
	}

	function _mysqldump_table_structure($table) {
//		$this->SQL_DUMP.= "/* TABLE STRUCTURE FOR TABLE `$table` */\n";
		if($this->sql_drop_table==1) {
			$this->SQL_DUMP.= "DROP TABLE IF EXISTS `$table`;\n";
			$this->SQL_DUMP.= "\n".$this->SQL_SEP."\n\n";
		}
		if($this->sql_create_table) {
			$sql="show create table `$table`; ";
			$result=mysql_query($sql);
			if($result) {
				if($row= mysql_fetch_assoc($result)) {
					$this->SQL_DUMP.= $row['Create Table'].";\n\n";
					$this->SQL_DUMP.= "\n\n".$this->SQL_SEP."\n\n";
				}
			}
			mysql_free_result($result);
		}
//		$this->SQL_DUMP.= $this->SQL_SEP."\n\n";
	}

	function _mysqldump_table_data($table) {
		$sql="SELECT * FROM `$table`;";
		$result=mysql_query($sql);
		if($result) {
			$num_rows=mysql_num_rows($result);
			$num_fields=mysql_num_fields($result);
			
			if($num_rows>0) {
//				$this->SQL_DUMP.= "/* DUMPING DATA FOR TABLE `$table` */\n";
				
				$field_type=array();
				$i=0;
				while($i < $num_fields) {
					$meta=mysql_fetch_field($result, $i);
					array_push($field_type, $meta->type);
					$i++;
					$cols_array[] = $meta->name;
				}

				$cols_list=@implode(',', $cols_array);

				$index=0;
				$insert_col_header = "INSERT INTO `$table` ($cols_list) VALUES\n";
				while($row=mysql_fetch_row($result)) {
					if($index%$this->INSERT_SPLIT==0) $this->SQL_DUMP.=$insert_col_header;

					$this->SQL_DUMP.= "(";
					for($i=0; $i < $num_fields; $i++) {
						if(is_null( $row[$i])) {
							$this->SQL_DUMP.= "null";
						} else {
							switch($field_type[$i]){
								case 'int':
									$this->SQL_DUMP.= $row[$i];
								break;
								case 'string':
								case 'blob' :
								default:
									$this->SQL_DUMP.= "'".mysql_real_escape_string($row[$i])."'";
								break;
							}
						}
						if($i < $num_fields-1) $this->SQL_DUMP.= ",";
					}
					$this->SQL_DUMP.= ")";
					
					++$index;
					if($index < $num_rows && $index%$this->INSERT_SPLIT!=0) {
						$this->SQL_DUMP.= ",";
					} else {
						$this->SQL_DUMP.= ";";
					}
					$this->SQL_DUMP.= "\n";
					if($index%$this->INSERT_SPLIT==0) $this->SQL_DUMP.= "\n".$this->SQL_SEP."\n\n";
				}
				$this->SQL_DUMP.= "\n";
				$this->SQL_DUMP.= $this->SQL_SEP."\n\n";
			}
		}
		mysql_free_result($result);
	}


	function _mysqlrestore($dump) {
		global $X;

		if(!file_exists($dump) || @filesize($dump)==0) return false;
		$X->DB->disconnect();
		$X->DB->connect();

		$file=fopen($dump, "r");
		while(!feof($file)) {
			$line=fgets($file, 16384);

			if(trim($line) == trim($this->SQL_SEP)) {
				$X->DB->q($sql_string);
				$sql_string='';
			} else {
				$sql_string.=$line;
			}
		}
		fclose($file);

		$X->DB->disconnect();
		$X->DB->connect();
		return true;
	}

}
$MYSQL_DUMP=new mysql_dump();
?>