View source
<?php
define('BACKUP_MIGRATE_FILENAME_MAXLENGTH', 255);
function backup_migrate_temp_files_add($filepath = NULL) {
$files =& drupal_static('backup_migrate_temp_files_add', array());
if (!$filepath) {
return $files;
}
else {
$files[] = $filepath;
}
}
function _backup_migrate_temp_files_delete() {
if (variable_get('backup_migrate_cleanup_temp_files', BACKUP_MIGRATE_CLEANUP_TEMP_FILES)) {
foreach (backup_migrate_temp_files_add() as $file) {
if (file_exists($file) && is_writable($file)) {
_backup_migrate_temp_files_delete_file($file);
}
}
$dir = file_directory_temp();
$expire = time() - variable_get('backup_migrate_cleanup_time', BACKUP_MIGRATE_CLEANUP_TIME);
if (file_exists($dir) && is_dir($dir) && is_readable($dir) && ($handle = opendir($dir))) {
while (FALSE !== ($file = @readdir($handle))) {
if (strpos($file, 'backup_migrate_') === 0 && is_writable("{$dir}/{$file}") && @filectime("{$dir}/{$file}") < $expire) {
_backup_migrate_temp_files_delete_file("{$dir}/{$file}");
}
}
closedir($handle);
}
}
}
function _backup_migrate_temp_files_delete_file($file) {
if (file_exists($file) && (is_writable($file) || is_link($file))) {
if (!is_link($file) && is_dir($file) && is_readable($file) && ($handle = opendir($file))) {
$dir = $file;
while (FALSE !== ($file = @readdir($handle))) {
if ($file != '..' && $file != '.') {
_backup_migrate_temp_files_delete_file("{$dir}/{$file}");
}
}
closedir($handle);
rmdir($dir);
}
else {
unlink($file);
}
}
}
function _backup_migrate_move_files($from, $to) {
if (is_readable($from)) {
if (is_dir($from) && is_readable($from) && ($handle = opendir($from))) {
if (!file_exists($to)) {
mkdir($to);
}
while (FALSE !== ($file = @readdir($handle))) {
if ($file != '..' && $file != '.') {
_backup_migrate_move_files("{$from}/{$file}", "{$to}/{$file}");
}
}
closedir($handle);
}
else {
rename($from, $to);
}
}
return FALSE;
}
function backup_migrate_temp_directory() {
$tmp = realpath(file_directory_temp());
if (!is_writable(realpath(file_directory_temp()))) {
_backup_migrate_message('Your temporary directory %tmp is not writable. Backup and migrate needs to be able to create temporary files.', array(
'%tmp' => $tmp,
), 'error');
}
$file = $tmp . '/' . uniqid('backup_migrate_');
mkdir($file);
backup_migrate_temp_files_add($file);
return $file;
}
function _backup_migrate_filetypes() {
require_once dirname(__FILE__) . '/filters.inc';
$out = backup_migrate_filters_file_types();
foreach ($out as $key => $info) {
$out[$key]['id'] = empty($info['id']) ? $key : $info['id'];
}
return $out;
}
function _backup_migrate_filename_append_prepare($filename, $append_str) {
$max_name_len = BACKUP_MIGRATE_FILENAME_MAXLENGTH - drupal_strlen($append_str);
if (drupal_strlen($filename) > $max_name_len) {
$filename = drupal_substr($filename, 0, $max_name_len);
}
return $filename;
}
function _backup_migrate_construct_filename($settings) {
$filename = $settings->filename;
if (module_exists('token') && function_exists('token_replace')) {
$filename = token_replace($filename);
}
$filename = _backup_migrate_clean_filename($filename);
$timestamp = '';
if ($settings->append_timestamp == 1 && $settings->timestamp_format) {
$timestamp = format_date(time(), 'custom', $settings->timestamp_format);
}
$filename = _backup_migrate_filename_append_prepare($filename, $timestamp);
$filename .= '-' . $timestamp;
$filename = trim($filename, '-');
if (drupal_strlen($filename) == 0) {
$filename = 'untitled';
}
return $filename;
}
function _backup_migrate_default_filename() {
if (module_exists('token')) {
return '[site:name]';
}
else {
return _backup_migrate_clean_filename(variable_get('site_name', "backup_migrate"));
}
}
function _backup_migrate_clean_filename($filename) {
$filename = preg_replace("/[^a-zA-Z0-9\\.\\-_]/", "", $filename);
return $filename;
}
function _backup_migrate_file_dispose_buffer($buffer) {
return "";
}
class backup_file {
public $file_info = array();
public $type = array();
public $ext = array();
public $path = "";
public $name = "";
public $handle = NULL;
public function __construct($params = array()) {
if (isset($params['filepath']) && file_exists($params['filepath'])) {
$this
->set_filepath($params['filepath']);
}
else {
$this
->set_file_info($params);
$this
->temporary_file();
}
}
public function file_id() {
return isset($this->file_info['file_id']) ? $this->file_info['file_id'] : $this
->filename();
}
public function filepath() {
if ($filepath = drupal_realpath($this->path)) {
return $filepath;
}
return $this->path;
}
public function filename($name = NULL) {
if ($name) {
$this->name = $name;
}
$extension_str = '.' . $this
->extension();
$this->name = _backup_migrate_filename_append_prepare($this->name, $extension_str);
return $this->name . $extension_str;
}
public function set_filepath($path) {
$this->path = $path;
$params = array(
'filename' => basename($path),
'file_id' => basename($path),
);
if (file_exists($path)) {
$params['filesize'] = filesize($path);
$params['filetime'] = filectime($path);
}
$this
->set_file_info($params);
}
public function info($key = NULL) {
if ($key) {
return @$this->file_info[$key];
}
return $this->file_info;
}
public function info_set($key, $value) {
$this->file_info[$key] = $value;
}
public function extension() {
return implode(".", $this->ext);
}
public function type() {
return $this->type;
}
public function mimetype() {
return @$this->type['filemime'] ? $this->type['filemime'] : 'application/octet-stream';
}
public function type_id() {
return @$this->type['id'];
}
public function filesize() {
if (empty($this->file_info['filesize'])) {
$this
->calculate_filesize();
}
return $this->file_info['filesize'];
}
public function calculate_filesize() {
$this->file_info['filesize'] = '';
if (!empty($this->path) && file_exists($this->path)) {
$this->file_info['filesize'] = filesize($this->path);
}
}
public function can_backup() {
return @$this->type['backup'];
}
public function can_restore() {
return @$this->type['restore'];
}
public function is_recognized_type() {
return @$this->type['restore'] || @$this->type['backup'];
}
public function open($write = FALSE, $binary = FALSE) {
if (!$this->handle) {
$path = $this
->filepath();
if ($write && (file_exists($path) && !is_writable($path) || !is_writable(dirname($path)))) {
_backup_migrate_message('The file %path cannot be written to.', array(
'%path' => $path,
), 'error');
return FALSE;
}
if (!$write && !is_readable($path)) {
_backup_migrate_message('The file %path cannot be read.', array(
'%path' => $path,
), 'error');
return FALSE;
}
$mode = ($write ? "w" : "r") . ($binary ? "b" : "");
$this->handle = fopen($path, $mode);
return $this->handle;
}
return NULL;
}
public function close() {
fclose($this->handle);
$this->handle = NULL;
}
public function write($data) {
if (!$this->handle) {
$this->handle = $this
->open(TRUE);
}
if ($this->handle) {
fwrite($this->handle, $data);
}
}
public function read($size = NULL) {
if (!$this->handle) {
$this->handle = $this
->open();
}
if ($this->handle && !feof($this->handle)) {
return $size ? fread($this->handle, $size) : fgets($this->handle);
}
return NULL;
}
public function put_contents($data) {
file_put_contents($this
->filepath(), $data);
}
public function get_contents() {
return file_get_contents($this
->filepath());
}
public function transfer() {
$headers = array(
array(
'key' => 'Content-Type',
'value' => $this
->mimetype(),
),
array(
'key' => 'Content-Disposition',
'value' => 'attachment; filename="' . $this
->filename() . '"',
),
);
if ($this
->mimetype() == 'application/x-gzip') {
$headers[] = array(
'key' => 'Content-Encoding',
'value' => 'gzip',
);
}
if ($size = $this
->info('filesize')) {
$headers[] = array(
'key' => 'Content-Length',
'value' => $size,
);
}
@ob_end_clean();
if ($this
->open(FALSE, TRUE)) {
foreach ($headers as $header) {
$header['value'] = preg_replace('/\\r?\\n(?!\\t| )/', '', $header['value']);
drupal_add_http_header($header['key'], $header['value']);
}
while ($data = $this
->read(1024)) {
print $data;
}
$this
->close();
$GLOBALS['devel_shutdown'] = FALSE;
}
else {
drupal_not_found();
}
ob_start('_backup_migrate_file_dispose_buffer');
backup_migrate_cleanup();
module_invoke_all('exit');
exit;
}
public function push_type($extension) {
$types = _backup_migrate_filetypes();
if ($type = @$types[$extension]) {
$this
->push_filetype($type);
}
$out = $this
->filepath();
$this
->temporary_file();
return $out;
}
public function pop_type() {
$out = new backup_file(array(
'filepath' => $this
->filepath(),
));
$this
->pop_filetype();
$this
->temporary_file();
return $out;
}
public function set_filetype($type) {
$this->type = $type;
$this->ext = array(
$type['extension'],
);
}
public function push_filetype($type) {
$this->ext[] = $type['extension'];
$this->type = $type;
}
public function pop_filetype() {
array_pop($this->ext);
$this
->detect_filetype_from_extension();
}
public function set_file_info($file_info) {
$this->file_info = $file_info;
$this->ext = explode('.', @$this->file_info['filename']);
foreach ($this->ext as $key => $val) {
$this->ext[$key] = trim($val, '_');
}
$this
->filename(array_shift($this->ext));
$this
->detect_filetype_from_extension();
}
public function detect_filetype_from_extension() {
$ext = end($this->ext);
$this->type = array();
$types = _backup_migrate_filetypes();
foreach ($types as $key => $type) {
if (trim($ext, "_0123456789") === $type['extension']) {
$this->type = $type;
$this->type['id'] = $key;
}
}
}
public function temporary_file() {
$file = drupal_tempnam('temporary://', 'backup_migrate_');
backup_migrate_temp_files_add($file);
if ($this
->extension()) {
$file .= '.' . $this
->extension();
backup_migrate_temp_files_add($file);
}
$this->path = $file;
}
}