function backup_tables($file, $host, $user, $pass, $name, $current_table = '', $current_row = 0) { global $cfg; $iStart = time(); $iMET = 2; #(intval(ini_get('max_execution_time')) - 10); # We need some time for other tasks $db = new DB_Contenido(); # Open the output file $gz = extension_loaded('zlib'); if ($gz) { if (!($handle = gzopen($file . '.gz', 'a'))) { return false; } } else { if (!($handle = fopen($file, 'a'))) { return false; } } if (strlen($current_table) == 0) { # Create the header $return = '-- drugCMS SQL Dump' . "\n"; $return .= '-- drugCMS ' . $cfg['version'] . "\n"; $return .= '-- (c) 2013-' . date('Y') . ' Spider IT Deutschland' . "\n"; $return .= '--' . "\n"; $return .= '-- Host: ' . $host . "\n"; $return .= '-- Backup creation date: ' . date('r') . "\n"; $ver = $db->server_info(); $return .= '-- Server version: ' . $ver['description'] . "\n"; $return .= '-- PHP version: ' . phpversion() . "\n"; $return .= "\n"; $return .= 'SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";' . "\n"; $return .= "\n"; $return .= '--' . "\n"; $return .= '-- Database: `' . $name . '`' . "\n"; $return .= '--'; if ($gz) { gzwrite($handle, $return); } else { fwrite($handle, $return); } $return = ''; } # Get all the tables $tables = array(); $result = $db->query('SHOW TABLES'); while ($db->next_record()) { $tables[] = $db->f(0); } # Loop through the tables $bOK = false; foreach ($tables as $table) { if (strlen($current_table) && !$bOK) { if ($table == $current_table) { $bOK = true; } } else { $bOK = true; } if ($bOK) { if ($current_row == 0) { $return .= "\n"; $return .= "\n"; $return .= '-- --------------------------------------------------------' . "\n"; $return .= "\n"; $return .= '--' . "\n"; $return .= '-- Table structure for table `' . $table . '`' . "\n"; $return .= '--' . "\n"; $return .= "\n"; if (in_array(substr($table, strlen($cfg['sql']['sqlprefix'])), array('_online_user', '_phplib_active_sessions'))) { # Don't drop these tables as the current user would be logged out # while restoring the database, stopping the restore process $db->query('SHOW CREATE TABLE `' . $table . '`'); $db->next_record(); $row2 = $db->toArray(DB_SQL_Abstract::FETCH_NUMERIC); $return .= str_replace('CREATE TABLE `', 'CREATE TABLE IF NOT EXISTS `', $row2[1]) . ";\n"; } else { $return .= 'DROP TABLE IF EXISTS `' . $table . '`;' . "\n"; $db->query('SHOW CREATE TABLE `' . $table . '`'); $db->next_record(); $row2 = $db->toArray(DB_SQL_Abstract::FETCH_NUMERIC); $return .= $row2[1] . ";\n"; } if ($gz) { gzwrite($handle, $return); } else { fwrite($handle, $return); } $return = ''; # Only backup data which is supposed to be permanent if (!in_array(substr($table, strlen($cfg['sql']['sqlprefix'])), array('_code', '_inuse', '_online_user', '_phplib_active_sessions'))) { $return .= "\n"; $return .= '--' . "\n"; $return .= '-- Data for table `' . $table . '`' . "\n"; $return .= '--' . "\n"; } } # Only backup data which is supposed to be permanent if (!in_array(substr($table, strlen($cfg['sql']['sqlprefix'])), array('_code', '_inuse', '_online_user', '_phplib_active_sessions'))) { # Get the key (first) column in the table (we sort it on this to export # each row just once if we split because of the time management) $db->query('SHOW COLUMNS FROM ' . $table); $db->next_record(); $row = $db->toArray(DB_SQL_Abstract::FETCH_NUMERIC); $key_column = $row[0]; # Get the amount of rows in this table $db->query('SELECT COUNT(' . $key_column . ') AS num_rows FROM ' . $table); $db->next_record(); $num_rows = $db->f('num_rows'); # Get the columns $db->query('SELECT * FROM ' . $table . ' ORDER BY ' . $key_column . ' LIMIT 0, 1'); if ($db->next_record()) { $row = $db->toArray(DB_SQL_Abstract::FETCH_BOTH); $return .= "\n"; $return .= 'INSERT INTO `' . $table . '` ('; $keys = array(); foreach ($row as $key => $value) { if (!is_numeric($key)) { $keys[] = '`' . $key . '`'; } } $return .= implode(', ', $keys); $return .= ') VALUES'; } # Query the data $db->query('SELECT * FROM ' . $table . ' ORDER BY ' . $key_column . ' LIMIT ' . $current_row . ', ' . ($num_rows - $current_row)); $num_fields = $db->num_fields(); while ($db->next_record()) { $row = $db->toArray(DB_SQL_Abstract::FETCH_BOTH); $return .= "\n" . '('; for ($i = 0; $i < $num_fields; $i++) { if (!isset($row[$i])) { $return .= 'NULL'; } elseif (is_numeric($row[$i])) { $return .= $row[$i]; } else { $return .= "'" . str_replace(array("'", '\\', "\r", "\n"), array("''", '\\\\', "\\r", "\\n"), $row[$i]) . "'"; } if ($i < $num_fields - 1) { $return .= ', '; } } $return .= ')'; $current_row++; # Time management if (time() - $iStart >= $iMET) { $return .= ';'; if ($gz) { gzwrite($handle, $return); } else { fwrite($handle, $return); } return array('table' => $table, 'row' => $current_row); } elseif (strlen($return) > 30000) { $return .= ';'; if ($gz) { gzwrite($handle, $return); } else { fwrite($handle, $return); } $return = "\n" . 'INSERT INTO `' . $table . '` ('; $return .= implode(', ', $keys); $return .= ') VALUES'; } else { $return .= ','; } } $return = strlen($return) > 1 && substr($return, -1) == ',' ? substr($return, 0, -1) . ';' : ''; if ($gz) { gzwrite($handle, $return); } else { fwrite($handle, $return); } $return = ''; $current_row = 0; # Reset for the next table } } } # Set the code generation flag on restoring $return .= "\n"; $return .= "\n"; $return .= "\n"; $return .= '-- --------------------------------------------------------' . "\n"; $return .= "\n"; $return .= '--' . "\n"; $return .= '-- Set the code generation flag on restoring' . "\n"; $return .= '--' . "\n"; $return .= "\n"; $return .= 'UPDATE `' . $cfg['sql']['sqlprefix'] . '_cat_art` SET `createcode` = 1;'; //save file if ($gz) { gzwrite($handle, $return . "\n"); gzclose($handle); } else { fwrite($handle, $return . "\n"); fclose($handle); } return true; }