--- dba.module.orig 2007-03-31 01:49:42.663774800 +0200 +++ dba.module 2007-03-31 02:05:27.146503400 +0200 @@ -309,17 +309,23 @@ function dba_cron() { } } -function dba_auto_backup() { - $path = variable_get('dba_auto_backup_path', file_directory_temp()); +function dba_auto_backup($path = NULL, $filename = NULL, $exclude_tables = NULL, $mail = TRUE, $compression_allowed = TRUE) { + if (!isset($path)) { + $path = variable_get('dba_auto_backup_path', file_directory_temp()); + } // See what tables (if any) the admin wants us to only backup // the schema, not the actual data. we need it as an array, so we // lookup the setting as a string, then split() it into an array. - $exclude_tables_str = variable_get('dba_auto_backup_exclude_tables', DBA_BACKUP_EXCLUDE); - $exclude_tables = split('[ ,]', $exclude_tables_str); + if (!isset($exclude_tables)) { + $exclude_tables_str = variable_get('dba_auto_backup_exclude_tables', DBA_BACKUP_EXCLUDE); + $exclude_tables = split('[ ,]', $exclude_tables_str); + } // Make sure we have permission to save our backup file if (file_check_directory($path)) { $database = dba_get_database(); - $filename = format_date(time(), 'custom', 'Y-md-Hi_') . variable_get('dba_default_filename', 'backup.sql'); + if (!isset($filename)) { + $filename = format_date(time(), 'custom', 'Y-md-Hi_') . variable_get('dba_default_filename', 'backup.sql'); + } $backup = "-- Drupal dba.module database dump\n"; $backup .= "--\n"; @@ -330,20 +336,22 @@ function dba_auto_backup() { $backup .= dba_backup_table($table, TRUE, FALSE, in_array($table, $exclude_tables) ? FALSE : TRUE); } - // Optionally bzip2 compress auto-backup file - if (variable_get('dba_auto_backup_bzip2', 0)) { - $backup = bzcompress($backup, 9); - $filename = $filename .'.bz2'; - } - // Otherwise, optionally gzip compress auto-backup file - else if (variable_get('dba_auto_backup_gzip', 0)) { - if (version_compare(phpversion(), '4.2', '>=')) { - $backup = gzencode($backup, 9, FORCE_GZIP); - } - else { - $backup = gzencode($backup, FORCE_GZIP); + if ($compression_allowed) { + // Optionally bzip2 compress auto-backup file + if (variable_get('dba_auto_backup_bzip2', 0)) { + $backup = bzcompress($backup, 9); + $filename = $filename .'.bz2'; + } + // Otherwise, optionally gzip compress auto-backup file + else if (variable_get('dba_auto_backup_gzip', 0)) { + if (version_compare(phpversion(), '4.2', '>=')) { + $backup = gzencode($backup, 9, FORCE_GZIP); + } + else { + $backup = gzencode($backup, FORCE_GZIP); + } + $filename = $filename .'.gz'; } - $filename = $filename .'.gz'; } if ($fp = fopen($path ."/$filename", 'wb')) { @@ -1218,13 +1226,18 @@ function dba_get_database() { /** * Return all fields in specified table as array. */ -function dba_get_fields($table) { +function dba_get_fields($table, $describe = FALSE) { $fields = array(); if (_is_mysql()) { $result = db_query("DESCRIBE $table"); while ($row = db_fetch_object($result)) { - $fields[] = $row->Field; + if (!$describe) { + $fields[] = $row->Field; + } + else { + $fields[] = $row; + } } } else { @@ -1313,17 +1326,24 @@ function dba_backup_table($table, $add_d $result = db_query("select * from $table"); $numrow = db_num_rows($result); - $fields = dba_get_fields($table); + $fields = dba_get_fields($table, TRUE); $num_fields = sizeof($fields); while ($row = db_fetch_array($result)) { $line = "INSERT INTO $table VALUES("; $i = 0; foreach ($row as $value) { - $value = addslashes($value); - $value = ereg_replace("\n", "\\n", $value); - $line .= (isset($value)) ? "\"$value\"" : "\"\""; - $line .= (++$i < $num_fields) ? ',' : ");\n"; + if (!empty($value) && ($fields[$i]->Type == 'longblob' || $fields[$i]->Type == 'blob')) { + $line .= '0x'. bin2hex($value); + } + else if (is_numeric($value)) { + $line .= (!empty($value) ? $value : 0); + } + else { + $value = mysql_real_escape_string($value); + $line .= (!empty($value)) ? "'$value'" : "''"; + } + $line .= (++$i < $num_fields) ? ', ' : ");\n"; } $output .= $line; if ($live) { @@ -1363,9 +1383,11 @@ function dba_delete_table($table) { /** * Drop table and all contents from current database. */ -function dba_drop_table($table) { +function dba_drop_table($table, $verbose = TRUE) { $query = "DROP TABLE $table;"; - drupal_set_message($query); + if ($verbose) { + drupal_set_message($query); + } return db_query($query); }