Codice PHP:
class backup {
var $struct_only = false;
var $save_on_server = true;
var $createtable = true;
var $compress = true;
var $filename = "";
var $ext = "";
var $useragent = "";
var $newline;
function backup($struct_only = false , $save_on_server = true , $createtable = true ,$compress = true, $filename = "" )
{
$this->struct_only = $struct_only;
$this->save_on_server = $save_on_server;
$this->createtable = $createtable;
$this->compress = $compress;
$this->filename = $filename;
$this->useragent = trim(substr($_SERVER['HTTP_USER_AGENT'], 0, 60));
$this->newline = $this->_define_newline();
}
function perform_backup($filename,$ext){
ob_start();
header('Content-Type: ' . $this->_get_mime_type());
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
if( $this->_get_browser_type() == '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' );
}
ob_end_clean();
}
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 `".$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 `".$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 `".$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 _backup()
{
global $dbcore ;
$i = 0;
$now = gmdate( 'D, d M Y H:i:s' ) . ' GMT';
$newfile = "";
$newfile .= "#########################################" . $this->newline;
$newfile .= "# rcSupport Backup System" . $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))
{
if ( $this->createtable ){
$newfile .= $this->_get_def($table);
$newfile .= "$this->newline";
}
if ( !$this->struct_only ){
$newfile .= $this->_get_content($table );
$newfile .= "$this->newline";
}
$i++;
}
$this->_out($newfile);
return true ;
}
function _out( $dump )
{
if(empty($this->filename) ){
$this->filename = DATABASE_NAME .".".date("d")."_".date("M")."_".date("Y")."_".date("H")."_".date("i")."_".date("s");
}else{
$this->filename = $filename .".".date("d")."_".date("M")."_".date("Y")."_".date("H")."_".date("i")."_".date("s");
}
if($this->compress && extension_loaded("zlib")) {
$this->ext = "sql.gz";
}else{
$this->ext = "sql";
}
if($this->save_on_server) {
$dir = "./includes/";
$files = "Backup";
if(!is_dir($dir.$files))
{
mkdir($dir . $files , 0777);
chmod($dir . $files , 0777);
}
$handle = fopen($dir . $files ."/" . $this->filename .'.'. $this->ext, "wb" );
if($handle){
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( $handle, $gzbackupData );
}else{
fwrite($handle, $dump );
}
fclose($handle);
}
}
if($this->compress && extension_loaded("zlib"))
{
set_time_limit(0);
ob_start();
$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 ;
}
$this->perform_backup($this->filename,$this->ext);
return true ;
}
function _define_newline()
{
$unewline = "\r\n";
if ( strstr(strtolower($this->useragent), 'win') )
{
$unewline = "\r\n";
}else if ( strstr(strtolower($this->useragent), '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($this->useragent), $log_version) )
{
$USER_BROWSER_AGENT = 'OPERA';
}
else if ( ereg('MSIE ([0-9].[0-9]{1,2})', strtoupper($this->useragent), $log_version) )
{
$USER_BROWSER_AGENT = 'IE';
}
else if ( ereg('OMNIWEB/([0-9].[0-9]{1,2})', strtoupper($this->useragent), $log_version) )
{
$USER_BROWSER_AGENT = 'OMNIWEB';
}
else if ( ereg('MOZILLA/([0-9].[0-9]{1,2})', strtoupper($this->useragent), $log_version) )
{
$USER_BROWSER_AGENT = 'MOZILLA';
}
else if ( ereg('KONQUEROR/([0-9].[0-9]{1,2})', strtoupper($this->useragent), $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;
}
}