File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/api/class.backup.php
<?php
/**
* MySQL IWP Database Backup Script
*
* Place in admin/temp/ and run twice.
* The first time it will generate backup.sql.
* The second time it will gzip backup.sql if possible.
*
* @author Michael Knight <michael.knight@interspire.com>
* @package IWP
* @subpackage Backup
*/
/**
* MySQL Database Backup Class
*
* It uses mysqldump if possible, then falls back to a manual method.
*
* This class is generic and can be used to backup any MySQL database.
*
* @package IWP
* @subpackage Backup
*/
class iwp_backup extends iwp_base
{
/**
* @var string The name of the backup file to write to. This should include the full path to the file.
*/
public $backupFile = '';
/**
* @var integer The current progress of the database export'
*/
public $progress = 0;
/**
* This is the JS function to call in the parent document to update the progress percentage
*
* @var string
*/
public $jsProgressFunction = "Upgrade.UpdateProgress";
/**
* Instance
* This static variable holds the current instance of this object being loaded.
* So using the getInstance function anywhere will return the very same instance.
*
* @var object Instance
*/
public static $Instance;
/**
* getInstance
* This is a static function that sets up the class instance and stores it to the static variable. It will then return that instantiation in the future.
*
* @return iwp_admin_home Returns the instantiated object
**/
public static function getInstance(){
if(!isset(self::$Instance)){
self::$Instance = new self();
}
return self::$Instance;
}
/**
* This function writes data to the currently defined backup file. It will only ever append to the current file.
*
* @param mixed $data The data to write to the file.
*
* @return void
* @see $backupFile
*/
private function DumpSQL($data){
file_put_contents($this->backupFile, $data, FILE_APPEND);
}
/**
* GetFields
*
* Return all the field names of the table as an array
*
* @param mixed $table
* @return void
*/
public function GetFields($table) {
$query = "SHOW COLUMNS FROM " . $table;
$result = $this->db->Query($query);
$fields = array();
while ($row = $this->db->Fetch($result)) {
$fields[] = $row['Field'];
}
return $fields;
}
/**
* This function determines whether or not the current value need to be escaped or not.
*
* @param mixed $value The value to test and be escaped
*
* @return mixed Either the numeric value or an escaped string
*/
public function EscapeValue($value){
if(is_numeric($value)){
return $value;
}
return "'".$this->db->Quote($value)."'";
}
/**
* Returns the create table definition for a table
*
* @param mixed $table
* @return string
*/
function GetCreateTable($table) {
$query = "SHOW CREATE TABLE ". $table;
$result = $this->db->Query($query);
$row = $this->db->Fetch($result);
return $row['Create Table'] . ";\n";
}
/**
* This function makes a backup file of all the database tables with the application prefix
*
* @param boolean $outputMode Whether or not to output the progress of the export. This is used only for the upgrade wizard.
*
* @return string
*/
public function DumpMySQLDatabase($outputMode=false, $tables=null){
$start_time = microtime_float();
$encoded = $output = '';
if(is_null($tables)) {
$queryTables = $this->db->Query("show tables like '" . iwp_config::Get('tablePrefix') . "%'");
$tables = array();
while ($tableRow = $this->db->Fetch($queryTables)) {
$tables[] = array_pop($tableRow);
}
}
$count = $this->db->CountResult($queryTables);
$percentIncrement = (100 / (max($count, 0.1)));
foreach($tables as $table) {
if (function_exists('gzencode')){
$this->DumpSQL(gzencode($this->GetCreateTable($table)));
}else{
$this->DumpSQL($this->GetCreateTable($table));
}
$fields = $this->GetFields($table);
// Fetch all the data
$query = "SELECT * FROM " .$table;
$result = mysql_unbuffered_query($query);
while (($row = $this->db->Fetch($result))) {
$line = "INSERT INTO " . $table . " (`";
$line .= implode("`, `", $fields);
$line .= "`) VALUES (";
$line .= implode(", ", array_map(array($this, 'escapevalue'), $row));
$line .= ")";
$line .= ";\n";
$output .= $line;
if (strlen($output) > 2097152) { // greater than 2MB? Lets dump it to the file
if (function_exists('gzencode')){
$encoded = gzencode($output);
}else{
$encoded = $output;
}
$this->DumpSQL($encoded);
unset($encoded);
unset($output);
$encoded = $output = '';
}
}
if($outputMode){
$this->progress = $this->progress + $percentIncrement;
echo "<script> self.parent." . $this->jsProgressFunction . "('".(int)$this->progress."'); </script>\n\n\n";
@ob_flush();
@flush();
}
if (function_exists('gzencode')){
$encoded = gzencode($output);
}else{
$encoded = $output;
}
$this->DumpSQL($encoded);
unset($encoded);
unset($output);
$encoded = $output = '';
}
}
}