You are here

downloads.inc in Filebrowser 6.2

File

includes/downloads.inc
View source
<?php

/* This file is part of "filebrowser".
 *    Copyright 2009-2011, arNuméral
 *    Author : Yoran Brault
 *    eMail  : yoran.brault@bad_arnumeral.fr (remove bad_ before sending an email)
 *    Site   : http://www.arnumeral.fr
 *
 * "filebrowser" is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * "filebrowser" is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with "filebrowser"; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

/**
 * Callback for filebrowser_download/%node menu.
 */
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;
}

/**
 * hook_filebrowser_download_manager_process implementation.
 */
function _filebrowser_file_download($file, $filename) {
  header('Content-Description: File Transfer');
  header("Cache-Control: public, must-revalidate, max-age=0");

  // HTTP/1.1
  header("Pragma: public");
  header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");

  // Date in the past
  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  header("Content-Type: " . file_get_mimetype($filename));
  header("Content-Transfer-Encoding: binary");
  header("Content-Length: " . filesize($file));
  if (eregi("MSIE", getenv("HTTP_USER_AGENT")) || eregi("Internet Explorer", getenv("HTTP_USER_AGENT"))) {
    header('Content-Disposition: inline; filename="' . mb_convert_encoding(_filebrowser_safe_basename($filename), "ISO-8859-2", "UTF-8") . '";');
  }
  else {
    header('Content-Disposition: inline; filename="' . _filebrowser_safe_basename($filename) . '";');
  }
  $block_size = 4096;
  $buffer = '';
  $handle = fopen($file, 'rb');
  set_time_limit(0);
  if ($handle !== FALSE) {
    while (!feof($handle)) {
      $buffer = fgets($handle, $block_size);
      echo $buffer;
      ob_flush();
      flush();
    }
    fclose($handle);
  }
  return TRUE;
}
function _filebrowser_file_url($node, $record) {
  static $cache = NULL;
  if (is_null($cache)) {
    $cache = module_invoke_all('filebrowser_download_manager_info');
  }
  $mode = $node->folder_rights->download_manager;
  $mode = isset($cache[$mode]) ? $mode : 'private';
  $callback = $cache[$mode]['url_callback'];
  return $callback($node, $record);
}
function filebrowser_private_download_url($node, $record) {

  // added language '' issue 2312731
  return url('filebrowser/download/' . $record['fid'], array(
    'absolute' => TRUE,
    'language' => '',
  ));
}
function filebrowser_public_download_url($node, $record) {
  $file = _filebrowser_convert_to_fs_encoding($node, _filebrowser_get_path($node) . $record['path']);
  $web_root = getcwd();
  if (strpos($web_root, $file) === 0) {
    $target = substr($file, strlen($web_root));
  }
  else {
    $target = $file;
  }

  // added language '' issue 2312731
  return url($target, array(
    'absolute' => TRUE,
    'language' => '',
  ));
}

Functions

Namesort descending Description
filebrowser_page_download Callback for filebrowser_download/%node menu.
filebrowser_private_download_url
filebrowser_public_download_url
_filebrowser_file_download hook_filebrowser_download_manager_process implementation.
_filebrowser_file_url