You are here

function filebrowser_page_download in Filebrowser 7.4

Same name and namespace in other branches
  1. 8 filebrowser.pages.inc \filebrowser_page_download()
  2. 6.2 includes/downloads.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.

Parameters

$fid:

Return value

null|string

1 call to filebrowser_page_download()
filebrowser_filebrowser_action_process in ./filebrowser.module
hook_filebrowser_action_process implementation When downloading files $fids will be configured as fid_of_root:fid,fid,fid etc.
1 string reference to 'filebrowser_page_download'
filebrowser_menu in ./filebrowser.module

File

./filebrowser.pages.inc, line 35
Several support functions for filebrowser

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);

  //debug($content, 'content line 42 function filebrowser_page_download');
  $node = NULL;
  $target = NULL;
  if ($content) {
    $node = node_load($content['nid']);

    // check if user has view rights on this node
    if (!node_access('view', $node)) {
      drupal_access_denied();
      exit(t('You have no viewing rights for this node'));
    }
    $target = _filebrowser_encoding_to_fs($node, _filebrowser_get_node_root($node) . $content['path']);
  }
  if (!$content || !$node || !_filebrowser_can_download_file($node) || !$target) {

    //debug($content); debug($node); //debug($target);
    drupal_access_denied();
    exit;
  }

  //  debug($target, '$target @ function filebrowser_page_download');
  if (is_s3_filesystem($target)) {
    $is_s3 = true;
    $client = awssdk_get_client('s3');
    $client
      ->registerStreamWrapper();
  }
  else {
    $is_s3 = false;
  }
  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;
    }
    if ($is_s3) {

      // Creation of archive handled by s3_fb module
      $target = s3_create_archive($target, $node, $fid, $files_fid);
    }
    else {
      $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_encoding_to_fs($node, _filebrowser_get_node_root($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_encoding_from_fs($node, $target);

  //  debug($decoded_file, '$decoded_file line 94');
  $result = module_invoke_all('filebrowser_download_manager_process', 'private', $target, $decoded_file, !empty($node->folder_rights->force_download));
  if ($result) {
    if ($cleanup && file_exists($cleanup)) {
      unlink($cleanup);
    }
  }
  else {
    drupal_access_denied();
  }
  exit;
}