<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
if (!defined('ADMIN_ALLOW') && !defined('ADMIN_MAX')) {
	die ("Access Denied");
}
if ($acces == 1) {
if($admintest && $adminsecu)  {
    global $dbhost, $dbuname, $dbpass, $dbname;
ignore_user_abort();
error_reporting(0);
set_time_limit(0);
	//Start MySQL Dump
$backupdir = "cache/Backups/Database";
	$dbcon = @mysql_connect($dbhost, $dbuname, $dbpass);
	$server_version = mysql_get_server_info();
	$datetime = date("m-d-y@h-ia");
	$file_name = "$backupdir/$dbname-$datetime.sql";
	$db_name = "-- Advanced Maximus Backup MySQL dump 1.0\n";
	$db_name.= "-- \n";
	$db_name.= "-- Host: $dbhost    Database: $dbname\n";
	$db_name.= "-- ------------------------------------------------------\n";
	$db_name.= "-- Server Version        $server_version\n\n";

	$fd = fopen($file_name, "a+");
	fwrite($fd, $db_name);
	fclose($fd);

	$tbl_array = array();
	$c = 0;
	$result2 = mysql_list_tables($dbname);
	for($x = 0; $x < mysql_num_rows($result2); $x++){
		$table = mysql_tablename($result2,$x);
		if($table <>""){
			$tbl_array[$c] = mysql_tablename($result2, $x);
			$c++;
		}
	}

	for($y = 0; $y < $c; $y++){
		$table = $tbl_array[$y];
		$def = "DROP TABLE IF EXISTS $table; \n";
		$def .= "CREATE TABLE $table (\n";
		$result3 = mysql_query("SHOW FIELDS FROM $table");
		while($row3 = mysql_fetch_array($result3)){
			$def .= "    $row3[Field] $row3[Type]";
			if ($row3['Default'] != "") $def .= " DEFAULT '$row3[Default]'";
			if ($row3['Null'] != "YES") $def .= " NOT NULL";
			if ($row3['Extra'] != "") $def .= " $row3[Extra]";
			$def .= ",\n";
		}

		$def = ereg_replace(",\n$", "", $def);
		$result4 = mysql_query("SHOW KEYS FROM $table");
		while($row4 = mysql_fetch_array($result4)) {
			$kname = $row4['Key_name'];
			if(($kname != "PRIMARY") && ($row4['Non_unique'] == 0)) $kname = "UNIQUE|$kname";
			if(!isset($index[$kname])) $index[$kname] = array();
			$index[$kname][] = $row['Column_name'];

usleep("100");
		}
		while(list($xy, $columns) = @each($index)){
			$def .= ",\n";
			if($xy == "PRIMARY") $def .= "   PRIMARY KEY (" . implode($columns, ", ") . ")";
			else if(substr($xy,0,6) == "UNIQUE") $def .= "   UNIQUE ".substr($xy,7)." (" . implode($columns, ", ") . ")";
				  else $def .= "   KEY $xy (" . implode($columns, ", ") . ")";
		}
		$result5 = mysql_query('SHOW TABLE STATUS LIKE "'.mysql_escape_string($table).'"');
		$row5 = mysql_fetch_array($result5);
		$type = $row5['Type'];
		$def .= "\n) TYPE=$type; \n\n";
		$def .= "-- \n-- Dumping data for table '$table' \n--\n\n\n";

		$result = array();
		$tbl_name = "-- \n-- Table structure for table '$table' \n--\n\n";
		$fd = fopen($file_name, "a+");
		fwrite($fd, $tbl_name.$def);
usleep("50");
		fclose($fd);
		unset($data);
		if($table > ""){
			$result = mysql_query("SELECT * FROM $table");
			$number = mysql_num_rows($result);
			$snum = mysql_num_fields($result);
				for($i = 0; $i < $number; $i++){
					$line = mysql_fetch_array($result);
					$data .= "INSERT INTO $table (";
					for($split = 0; $split < $snum; $split++){
						$fieldname = mysql_field_name($result, $split);
						if($split == ($snum - 1)){
							$data .= $fieldname;
						}else{
							$data .= $fieldname.",";
						}
					}
					$data .= ") VALUES (";
					for($k = 0; $k < $snum; $k++){
						if($k == ($snum - 1)){
							$data .= "'".addslashes($line[$k])."'";
						}else{
							$data .= "'".addslashes($line[$k])."',";
						}
					}
					$data .= ");\n";
				}
				$data .= "\n";
		}else{
			$result[] = "No table available";
		}
		$fd = fopen($file_name, "a+");
		for($i3 = 0; $i3 < count($result); $i3++){
usleep("10");
			fwrite($fd, $data);
			fclose($fd);
		}
	}
	mysql_close($dbcon);
	//End MySQL Dump

	//if($use_gzip){
		$filename2 = "$backupdir/$dbname-$datetime.sql.gz";
		$fp = fopen($file_name, "r");
		$data = fread ($fp, filesize($file_name));
		fclose($fp);
		$zp = gzopen($filename2, "w9");
		gzwrite($zp, $data);
		gzclose($zp);
		unlink($file_name);


	if($use_gzip){
		$filename2 = "$backupdir/$datetime.sql.gz";
		$fp = fopen($filename, "r");
		$data = fread ($fp, filesize($filename));
		fclose($fp);
		$zp = gzopen($filename2, "w9");
		gzwrite($zp, $data);
		gzclose($zp);
		unlink($filename);}
//
redirect_url("".$admin_file.".php?op=database");
}
}else {
	echo "Access Denied";
}