Codice PHP:
class backup {
var $newline;
var $struct_only = false;
var $output = true;
var $compress = true;
var $filename = "";
function backup($struct_only = false, $output = true, $compress = true, $filename = "" )
{
$this->output = $output;
$this->struct_only = $struct_only;
$this->compress = $compress;
$this->filename = $filename;
$this->newline = $this->_define_newline();
}
function _backup()
{
global $dbcore ;
$now = gmdate( 'D, d M Y H:i:s' ) . ' GMT';
$newfile .= "#########################################" . $this->newline;
$newfile .= "# Database Backup Class by Iván Melgrati" . $this->newline;
$newfile .= "# Database:". DATABASE_NAME . $this->newline;
$newfile .= "# Date: $now" . $this->newline;
$newfile .= "#########################################" . $this->newline . $this->newline;
$result = $dbcore->query( "SHOW TABLES FROM `".$dbcore->escape(DATABASE_NAME)."`" );
while ( list($table) = $dbcore->fetch_row($result))
{
$newfile .= $this->_get_def($table);
$newfile .= "$this->newline";
if ( !$struct_only )
$newfile .= $this->_get_content( $table );
$newfile .= "$this->newline";
$i++;
}
$this->_out($newfile);
}
function _out($dump)
{
if ( $this->filename ){
$fptr = fopen($this->filename, "wb" );
if ($fptr){
if($this->compress)
{
$gzbackupData = "\x1f\x8b\x08\x00\x00\x00\x00\x00" . substr(gzcompress($dump, 9), 0, -4 ) . pack( 'V', crc32($dump) ) . pack( 'V', strlen($dump));
fwrite($fptr, $gzbackupData);
}else{
fwrite($fptr, $dump);
}
fclose($fptr);
}
}
else
{
if(($this->compress) && ($this->output))
{
$gzbackupData = "\x1f\x8b\x08\x00\x00\x00\x00\x00" . substr( gzcompress($dump, 9), 0, -4 ) . pack( 'V', crc32($dump) ) . pack( 'V', strlen($dump) );
echo $gzbackupData;
}else{
echo $dump;
}
}
}
function _get_def($tablename)
{
global $dbcore ;
$def = "";
$def .= "#------------------------------------------" . $this->newline;
$def .= "# Table definition for $tablename" . $this->newline;
$def .= "#------------------------------------------" . $this->newline;
$def .= "DROP TABLE IF EXISTS $tablename;" . $this->newline . $this->newline;
$def .= "CREATE TABLE $tablename (" . $this->newline;
$result = $dbcore->query( "SHOW FIELDS FROM `".$dbcore->escape($tablename)."`" ) or die( "Table $tablename not existing in database" );
while ( $row = $dbcore->fetch_array($result) )
{
$def .= " ".$row["Field"]."`".$row["Type"];
if($row["Null"] != "YES"){
$def .= " NOT NULL";
}
if($row["Default"] != ""){
if ( $row["Default"] == "CURRENT_TIMESTAMP" ){
$def .= " default CURRENT_TIMESTAMP";
}else{
$def .= " default $row[Default]";
}
}
if($row['Extra'] != "" ){
$def .= " $row[Extra]";
}
$def .= ",$this->newline";
}
$def = ereg_replace( ",$this->newline$", "", $def);
$result = $dbcore->query("SHOW KEYS FROM `".$dbcore->escape($tablename)."`" );
while ($row = $dbcore->fetch_array($result))
{
$kname = $row[Key_name];
if ( ($kname != "PRIMARY") && ($row[Non_unique] == 0) ) $kname = "UNIQUE|$kname";
if ( !isset($index[$kname]) ) $index[$kname] = array();
$index[$kname][] = $row[Column_name];
}
while (list($x, $columns) = @each($index) )
{
$def .= ",$this->newline";
if ( $x == "PRIMARY" ) $def .= " PRIMARY KEY (" . implode( $columns, ", " ) . ")";
else if ( substr($x, 0, 6) == "UNIQUE" ) $def .= " UNIQUE " . substr( $x, 7 ) . " (" . implode( $columns, ", " ) . ")";
else $def .= " KEY $x (" . implode( $columns, ", " ) . ")";
}
$def .= "$this->newline);";
return ( stripslashes($def) );
}
function _get_content( $tablename )
{
global $dbcore ;
$content = "";
$result = $dbcore->query("SELECT * FROM `".$dbcore->escape($tablename)."`" );
if ($dbcore->num_rows($result) > 0)
{
$content .= "#------------------------------------------" . $this->newline;
$content .= "# Data inserts for $tablename" . $this->newline;
$content .= "#------------------------------------------" . $this->newline;
}
while ($row = $dbcore->fetch_row($result))
{
$insert = "INSERT INTO `".$dbcore->escape($tablename)."` VALUES (";
for ( $j = 0; $j < $dbcore->num_fields($result); $j++ )
{
if ( !isset($row[$j]) ) $insert .= "NULL,";
elseif ( $row[$j] != "" ) $insert .= "'" . addslashes( $row[$j] ) . "',";
else $insert .= "'',";
}
$insert = ereg_replace( ",$", "", $insert );
$insert .= ");$this->newline";
$content .= $insert;
}
return $content . $this->newline;
}
function _define_newline()
{
$unewline = "\r\n";
if ( strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), 'win') )
{
$unewline = "\r\n";
}else if ( strstr(strtolower($_SERVER["HTTP_USER_AGENT"]), 'mac') )
{
$unewline = "\r";
}else{
$unewline = "\n";
}
return $unewline;
}
function _get_browser_type()
{
$USER_BROWSER_AGENT = "";
if ( ereg('OPERA(/| )([0-9].[0-9]{1,2})', strtoupper($_SERVER["HTTP_USER_AGENT"]), $log_version) )
{
$USER_BROWSER_AGENT = 'OPERA';
}
else if ( ereg('MSIE ([0-9].[0-9]{1,2})', strtoupper($_SERVER["HTTP_USER_AGENT"]), $log_version) )
{
$USER_BROWSER_AGENT = 'IE';
}
else if ( ereg('OMNIWEB/([0-9].[0-9]{1,2})', strtoupper($_SERVER["HTTP_USER_AGENT"]), $log_version) )
{
$USER_BROWSER_AGENT = 'OMNIWEB';
}
else if ( ereg('MOZILLA/([0-9].[0-9]{1,2})', strtoupper($_SERVER["HTTP_USER_AGENT"]), $log_version) )
{
$USER_BROWSER_AGENT = 'MOZILLA';
}
else if ( ereg('KONQUEROR/([0-9].[0-9]{1,2})', strtoupper($_SERVER["HTTP_USER_AGENT"]), $log_version) )
{
$USER_BROWSER_AGENT = 'KONQUEROR';
}
else
{
$USER_BROWSER_AGENT = 'OTHER';
}
return $USER_BROWSER_AGENT;
}
function _get_mime_type()
{
$USER_BROWSER_AGENT = $this->_get_browser_type();
$mime_type = ( $USER_BROWSER_AGENT == 'IE' || $USER_BROWSER_AGENT == 'OPERA' ) ? 'application/octetstream' : 'application/octet-stream';
return $mime_type;
}
function perform_backup()
{
$now = gmdate( 'D, d M Y H:i:s' ) . 'GMT';
if ( $this->compress )
{
$filename = $this->dbname . ".sql";
$ext = "gz";
}else{
$filename = $this->dbname;
$ext = "sql";
}
$USER_BROWSER_AGENT = $this->_get_browser_type();
if ( $this->filename )
{
$this->_backup();
}
else if ( $this->output == true )
{
header( 'Content-Type: ' . $this->_get_mime_type() );
header( 'Expires: ' . $now );
if ( $USER_BROWSER_AGENT == 'IE' )
{
header( 'Content-Disposition: inline; filename="' . $filename . '.' . $ext . '"' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Pragma: public' );
}
else
{
header( 'Content-Disposition: attachment; filename="' . $filename . '.' . $ext . '"' );
header( 'Pragma: no-cache' );
}
$this->_backup();
}
else
{
echo "<html><body><pre>";
echo htmlspecialchars( $this->_backup() );
echo "</PRE></BODY></HTML>";
}
}
}