You are here

function filebrowser_page_download in Filebrowser 6.2

Same name and namespace in other branches
  1. 8 filebrowser.pages.inc \filebrowser_page_download()
  2. 7.4 filebrowser.pages.inc \filebrowser_page_download()
  3. 7.2 filebrowser.pages.inc \filebrowser_page_download()
  4. 7.3 filebrowser.pages.inc \filebrowser_page_download()

Callback for filebrowser_download/%node menu.

1 call to filebrowser_page_download()
filebrowser_filebrowser_action_process in ./filebrowser.module
hook_filebrowser_action_process implementation
1 string reference to 'filebrowser_page_download'
filebrowser_menu in ./filebrowser.module
Implementation of hook_menu().

File

includes/downloads.inc, line 28

Code

function filebrowser_page_download($fid) {
  $cleanup = NULL;
  $files_fid = NULL;
  if (strpos($fid, ':')) {
    list($fid, $files_fid) = explode(':', $fid);
    $files_fid = explode(',', $files_fid);
  }
  $content = _filebrowser_node_content_load($fid);
  $node = NULL;
  $target = NULL;
  if ($content) {
    $node = node_load($content['nid']);
    $target = _filebrowser_convert_to_fs_encoding($node, _filebrowser_get_path($node) . $content['path']);
  }
  if (!$content || !$node || !_filebrowser_can_download_file($node) || !$target) {
    drupal_access_denied();
    exit;
  }
  if (is_dir($target)) {
    if (!function_exists("zip_open")) {
      drupal_set_message(t("No ZIP support found in PHP installation, please contact your administrator"));
      return;
    }
    $zip = new ZipArchive();
    $target = file_directory_temp() . "/filebrowser_" . _filebrowser_safe_basename($target) . ".zip";
    $cleanup = $target;
    if (!file_exists(_filebrowser_safe_dirname($target))) {
      mkdir(_filebrowser_safe_dirname($target), 0777, TRUE);
    }
    if (file_exists($target)) {
      unlink($target);
    }
    _filebrowser_load_files($node, $fid);
    if ($zip
      ->open($target, ZIPARCHIVE::CREATE) === TRUE) {
      foreach ($node->file_listing as $file_name => $file_data) {
        if ($file_data['kind'] === 0 && (!$files_fid || in_array($file_data['fid'], $files_fid))) {
          $fs_filename = realpath(_filebrowser_convert_to_fs_encoding($node, _filebrowser_get_path($node) . "/" . $file_data['relative-path']));
          $zip
            ->addFile($fs_filename, $file_name);
        }
      }
      $zip
        ->close();
    }
    else {
      return t("Unable to create temporary zip file '@file'", array(
        file => $target,
      ));
    }
  }
  $decoded_file = _filebrowser_convert_from_fs_encoding($node, $target);
  $result = _filebrowser_file_download($target, $decoded_file);
  if ($result) {
    if ($cleanup && file_exists($cleanup)) {
      unlink($cleanup);
    }
  }
  else {
    drupal_access_denied();
  }
  exit;
}