You are here

class backup_file in Backup and Migrate 7.3

Same name and namespace in other branches
  1. 8.2 includes/files.inc \backup_file
  2. 8.3 includes/files.inc \backup_file
  3. 6.3 includes/files.inc \backup_file
  4. 6.2 includes/files.inc \backup_file
  5. 7.2 includes/files.inc \backup_file

A backup file which allows for saving to and reading from the server.

Hierarchy

Expanded class hierarchy of backup_file

File

includes/files.inc, line 205
General file handling code for Backup and Migrate.

View source
class backup_file {
  public $file_info = array();
  public $type = array();
  public $ext = array();
  public $path = "";
  public $name = "";
  public $handle = NULL;

  /**
   * Construct a file object given a file path, or create a temp file for writing.
   */
  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();
    }
  }

  /**
   * Get the file_id if the file has been saved to a destination.
   */
  public function file_id() {

    // The default file_id is the filename. Destinations can override the file_id if needed.
    return isset($this->file_info['file_id']) ? $this->file_info['file_id'] : $this
      ->filename();
  }

  /**
   * Get the current filepath.
   */
  public function filepath() {
    if ($filepath = drupal_realpath($this->path)) {
      return $filepath;
    }
    return $this->path;
  }

  /**
   * Get the final filename.
   */
  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;
  }

  /**
   * Set the current filepath.
   */
  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);
  }

  /**
   * Get one or all pieces of info for the file.
   */
  public function info($key = NULL) {
    if ($key) {
      return @$this->file_info[$key];
    }
    return $this->file_info;
  }

  /**
   * Get one or all pieces of info for the file.
   */
  public function info_set($key, $value) {
    $this->file_info[$key] = $value;
  }

  /**
   * Get the file extension.
   */
  public function extension() {
    return implode(".", $this->ext);
  }

  /**
   * Get the file type.
   */
  public function type() {
    return $this->type;
  }

  /**
   * Get the file mimetype.
   */
  public function mimetype() {
    return @$this->type['filemime'] ? $this->type['filemime'] : 'application/octet-stream';
  }

  /**
   * Get the file mimetype.
   */
  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);
    }
  }

  /**
   * Can this file be used to backup to.
   */
  public function can_backup() {
    return @$this->type['backup'];
  }

  /**
   * Can this file be used to restore to.
   */
  public function can_restore() {
    return @$this->type['restore'];
  }

  /**
   * Can this file be used to restore to.
   */
  public function is_recognized_type() {
    return @$this->type['restore'] || @$this->type['backup'];
  }

  /**
   * Open a file for reading or writing.
   */
  public function open($write = FALSE, $binary = FALSE) {
    if (!$this->handle) {
      $path = $this
        ->filepath();

      // Check if the file can be read/written.
      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;
      }

      // Open the file.
      $mode = ($write ? "w" : "r") . ($binary ? "b" : "");
      $this->handle = fopen($path, $mode);
      return $this->handle;
    }
    return NULL;
  }

  /**
   * Close a file when we're done reading/writing.
   */
  public function close() {
    fclose($this->handle);
    $this->handle = NULL;
  }

  /**
   * Write a line to the file.
   */
  public function write($data) {
    if (!$this->handle) {
      $this->handle = $this
        ->open(TRUE);
    }
    if ($this->handle) {
      fwrite($this->handle, $data);
    }
  }

  /**
   * Read a line from the file.
   */
  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;
  }

  /**
   * Write data to the file.
   */
  public function put_contents($data) {
    file_put_contents($this
      ->filepath(), $data);
  }

  /**
   * Read data from the file.
   */
  public function get_contents() {
    return file_get_contents($this
      ->filepath());
  }

  /**
   * Transfer file using http to client. Similar to the built in file_transfer,
   *  but it calls module_invoke_all('exit') so that temp files can be deleted.
   */
  public function transfer() {
    $headers = array(
      array(
        'key' => 'Content-Type',
        'value' => $this
          ->mimetype(),
      ),
      array(
        'key' => 'Content-Disposition',
        'value' => 'attachment; filename="' . $this
          ->filename() . '"',
      ),
    );

    // In some circumstances, web-servers will double compress gzipped files.
    // This may help aleviate that issue by disabling mod-deflate.
    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,
      );
    }

    // Suppress the warning you get when the buffer is empty.
    @ob_end_clean();
    if ($this
      ->open(FALSE, TRUE)) {
      foreach ($headers as $header) {

        // To prevent HTTP header injection, we delete new lines that are
        // not followed by a space or a tab.
        // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
        $header['value'] = preg_replace('/\\r?\\n(?!\\t| )/', '', $header['value']);
        drupal_add_http_header($header['key'], $header['value']);
      }

      // Transfer file in 1024 byte chunks to save memory usage.
      while ($data = $this
        ->read(1024)) {
        print $data;
      }
      $this
        ->close();

      // Ask devel.module not to print it's footer.
      $GLOBALS['devel_shutdown'] = FALSE;
    }
    else {
      drupal_not_found();
    }

    // Start buffering and throw away the results so that errors don't get appended to the file.
    ob_start('_backup_migrate_file_dispose_buffer');
    backup_migrate_cleanup();
    module_invoke_all('exit');
    exit;
  }

  /**
   * Push a file extension onto the file and return the previous file path.
   */
  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;
  }

  /**
   * Push a file extension onto the file and return the previous file path.
   */
  public function pop_type() {
    $out = new backup_file(array(
      'filepath' => $this
        ->filepath(),
    ));
    $this
      ->pop_filetype();
    $this
      ->temporary_file();
    return $out;
  }

  /**
   * Set the current file type.
   */
  public function set_filetype($type) {
    $this->type = $type;
    $this->ext = array(
      $type['extension'],
    );
  }

  /**
   * Set the current file type.
   */
  public function push_filetype($type) {
    $this->ext[] = $type['extension'];
    $this->type = $type;
  }

  /**
   * Pop the current file type.
   */
  public function pop_filetype() {
    array_pop($this->ext);
    $this
      ->detect_filetype_from_extension();
  }

  /**
   * Set the file info.
   */
  public function set_file_info($file_info) {
    $this->file_info = $file_info;
    $this->ext = explode('.', @$this->file_info['filename']);

    // Remove the underscores added to file extensions by Drupal's upload security.
    foreach ($this->ext as $key => $val) {
      $this->ext[$key] = trim($val, '_');
    }
    $this
      ->filename(array_shift($this->ext));
    $this
      ->detect_filetype_from_extension();
  }

  /**
   * Get the filetype info of the given file, or false if the file is not a valid type.
   */
  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;
      }
    }
  }

  /**
   * Get a temporary file name with path.
   */
  public function temporary_file() {
    $file = drupal_tempnam('temporary://', 'backup_migrate_');

    // Add the version without the extension. The tempnam function creates this for us.
    backup_migrate_temp_files_add($file);
    if ($this
      ->extension()) {
      $file .= '.' . $this
        ->extension();

      // Add the version with the extension. This is the one we will actually use.
      backup_migrate_temp_files_add($file);
    }
    $this->path = $file;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
backup_file::$ext public property
backup_file::$file_info public property
backup_file::$handle public property
backup_file::$name public property
backup_file::$path public property
backup_file::$type public property
backup_file::calculate_filesize public function
backup_file::can_backup public function Can this file be used to backup to.
backup_file::can_restore public function Can this file be used to restore to.
backup_file::close public function Close a file when we're done reading/writing.
backup_file::detect_filetype_from_extension public function Get the filetype info of the given file, or false if the file is not a valid type.
backup_file::extension public function Get the file extension.
backup_file::filename public function Get the final filename.
backup_file::filepath public function Get the current filepath.
backup_file::filesize public function
backup_file::file_id public function Get the file_id if the file has been saved to a destination.
backup_file::get_contents public function Read data from the file.
backup_file::info public function Get one or all pieces of info for the file.
backup_file::info_set public function Get one or all pieces of info for the file.
backup_file::is_recognized_type public function Can this file be used to restore to.
backup_file::mimetype public function Get the file mimetype.
backup_file::open public function Open a file for reading or writing.
backup_file::pop_filetype public function Pop the current file type.
backup_file::pop_type public function Push a file extension onto the file and return the previous file path.
backup_file::push_filetype public function Set the current file type.
backup_file::push_type public function Push a file extension onto the file and return the previous file path.
backup_file::put_contents public function Write data to the file.
backup_file::read public function Read a line from the file.
backup_file::set_filepath public function Set the current filepath.
backup_file::set_filetype public function Set the current file type.
backup_file::set_file_info public function Set the file info.
backup_file::temporary_file public function Get a temporary file name with path.
backup_file::transfer public function Transfer file using http to client. Similar to the built in file_transfer, but it calls module_invoke_all('exit') so that temp files can be deleted.
backup_file::type public function Get the file type.
backup_file::type_id public function Get the file mimetype.
backup_file::write public function Write a line to the file.
backup_file::__construct public function Construct a file object given a file path, or create a temp file for writing.