You are here

function backup_file::transfer in Backup and Migrate 8.2

Same name and namespace in other branches
  1. 8.3 includes/files.inc \backup_file::transfer()
  2. 6.3 includes/files.inc \backup_file::transfer()
  3. 6.2 includes/files.inc \backup_file::transfer()
  4. 7.3 includes/files.inc \backup_file::transfer()
  5. 7.2 includes/files.inc \backup_file::transfer()

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.

File

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

Class

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

Code

function transfer() {
  $headers = array(
    array(
      'key' => 'Content-Type',
      'value' => $this
        ->mimetype(),
    ),
    array(
      'key' => 'Content-Disposition',
      'value' => 'attachment; filename="' . $this
        ->filename() . '"',
    ),
  );
  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']);
      $response = new \Symfony\Component\HttpFoundation\Response();
      $response->headers
        ->set($header['key'], $header['value']);
      $response
        ->send();
    }

    // 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();
  Drupal::moduleHandler()
    ->invokeAll('exit');
  exit;
}