array( "extension" => ".sql", "filemime" => "text/x-sql", ), "gzip" => array( "extension" => ".gz", "filemime" => "application/x-gzip", ), "bzip" => array( "extension" => ".bz", "filemime" => "application/x-bzip", ), "zip" => array( "extension" => ".zip", "filemime" => "application/zip", ), ); } /** * Get the filetype info of the given file, or false if the file is not a valid type. */ function _backup_migrate_file_get_filetype($filename) { $types = _backup_migrate_filetypes(); foreach ($types as $key => $type) { $extlen = drupal_strlen($type['extension']); if (drupal_substr($filename, -$extlen, $extlen) === $type['extension']) { return $type + array('type' => $key); } } return FALSE; } /** * Delete all temporary files. */ function _backup_migrate_temp_files_delete($files) { foreach ($files as $file) { if (file_exists($file)) { @unlink($file); } } } /** * Get the compression options as an options array for a form item. */ function _backup_migrate_get_compression_form_item_options() { $compression_options = array("none" => "None"); if (@function_exists("gzencode")) { $compression_options['gzip'] = "GZip"; } if (@function_exists("bzcompress")) { $compression_options['bzip'] = "BZip"; } if (class_exists('ZipArchive')) { $compression_options['zip'] = "Zip"; } return $compression_options; } /** * Gzip encode a file. */ function _backup_migrate_gzip_encode($source, $dest, $level = 9) { $success = FALSE; if (@function_exists("gzopen")) { if (($fp_out = gzopen($dest, 'wb'. $level)) && ($fp_in = fopen($source, 'rb'))) { while (!feof($fp_in)) { gzwrite($fp_out, fread($fp_in, 1024 * 512)); } $success = TRUE; } @fclose($fp_in); @gzclose($fp_out); } return $success; } /** * Gzip decode a file. */ function _backup_migrate_gzip_decode($source, $dest) { $success = FALSE; if (@function_exists("gzopen")) { if (($fp_out = fopen($dest, 'wb')) && ($fp_in = gzopen($source, 'rb'))) { while (!feof($fp_in)) { fwrite($fp_out, gzread($fp_in, 1024 * 512)); } $success = TRUE; } @gzclose($fp_in); @fclose($fp_out); } return $success; } /** * Bzip encode a file. */ function _backup_migrate_bzip_encode($source, $dest) { $success = FALSE; if (@function_exists("bzopen")) { if (($fp_out = bzopen($dest, 'w')) && ($fp_in = fopen($source, 'rb'))) { while (!feof($fp_in)) { bzwrite($fp_out, fread($fp_in, 1024 * 512)); } $success = TRUE; } else { $error = TRUE; } @fclose($fp_in); @bzclose($fp_out); } return $success; } /** * Bzip decode a file. */ function _backup_migrate_bzip_decode($source, $dest) { $success = FALSE; if (@function_exists("bzopen")) { if (($fp_out = fopen($dest, 'w')) && ($fp_in = bzopen($source, 'rb'))) { while (!feof($fp_in)) { fwrite($fp_out, gzread($fp_in, 1024 * 512)); } $success = TRUE; } else { $error = TRUE; } @bzclose($fp_in); @fclose($fp_out); } return $success; } /** * Zip encode a file. */ function _backup_migrate_zip_encode($source, $dest, $filename) { $success = FALSE; if (class_exists('ZipArchive')) { $zip = new ZipArchive; $res = $zip->open($dest, constant("ZipArchive::CREATE")); if ($res === TRUE) { $zip->addFile($source, $filename); $success = $zip->close(); } } return $success; } /** * Zip decode a file. */ function _backup_migrate_zip_decode($source, $dest, $filename) { $success = FALSE; $zip = new ZipArchive; if (class_exists('ZipArchive')) { if (($fp_out = fopen($dest, "w")) && ($zip->open($source)) && ($fp_in = $zip->getStream($filename))) { while (!feof($fp_in)) { fwrite($fp_out, fread($fp_in, 1024 * 512)); } $success = TRUE; } else { $error = TRUE; } @fclose($fp_in); @fclose($fp_out); } return $success; } /** * Construct a filename using token and some cleaning. */ function _backup_migrate_clean_filename($filename) { if (module_exists('token') && function_exists('token_replace')) { $filename = token_replace($filename, 'global'); } $filename = preg_replace("/[^a-zA-Z0-9\.\-_]/", "", $filename); if (drupal_strlen($filename) > 50) { $filename = drupal_substr($filename, 0, 50); } if (drupal_strlen($filename) == 0) { $filename = 'untitled'; } return $filename; } /** * Construct a default filename using the site's name. */ function _backup_migrate_default_filename() { if (module_exists('token')) { return '[site-name]'; } else { return _backup_migrate_clean_filename(variable_get(site_name, "backup_migrate")); } }