You are here

download_file.module in DownloadFile 6

Module to direct download files or images.

This module is for Drupal 6.x only.

Module by: Matthieu Moratille Mailto: mmoratille@yahoo.fr

File

download_file.module
View source
<?php

/**
 * @file
 * Module to direct download files or images.
 *
 * This module is for Drupal 6.x only.
 *
 * Module by: Matthieu Moratille
 * Mailto: mmoratille@yahoo.fr
 */

/**
 * Implementation of hook_help().
 */
function download_file_help($path, $arg) {
  switch ($path) {
    case 'admin/help#download_file':
      $output = '';
      $output .= '<p>' . t('The download file module to direct download files or images.') . '</p>';
      $output .= '<p>' . t('The download file module <a href="@setting">setting page</a> allows you to modify the format of the link accessible.', array(
        '@setting' => url('admin/settings/download-file'),
      )) . '</p>';
      return $output;
    case 'admin/settings/download-file':
      return '<p>' . t('The fownload file module to direct download files or images. Use the setting below to customize the format of the link accessible.') . '</p>';
  }
}

/**
 * Implementation of hook_perm().
 */
function download_file_perm() {
  return array(
    'access direct download file',
    'administer download file',
  );
}

/**
 * Implementation of hook_menu().
 */
function download_file_menu() {
  $items = array();
  $items['download/file/fid/%'] = array(
    'title' => 'Download file',
    'page callback' => 'download_file_direct_download',
    'page arguments' => array(
      3,
    ),
    'access arguments' => array(
      'access direct download file',
    ),
  );
  $items['admin/settings/download-file'] = array(
    'title' => 'Download file',
    'description' => 'Configure the format of the link accessible.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'download_file_settings',
    ),
    'access arguments' => array(
      'administer download file',
    ),
    'file' => 'download_file.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_init().
 */
function download_file_init() {

  // Insert support
  if (module_exists('insert')) {
    module_load_include('inc', 'download_file', 'download_file');
  }
}

/**
 * Implementation of hook_theme().
 */
function download_file_theme() {
  $theme = array(
    'download_file_formatter_direct_download' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'download_file.formatter.inc',
    ),
    'download_file_formatter_direct_download_icon' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'download_file.formatter.inc',
    ),
    'download_file_formatter_direct_download_accessible' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'download_file.formatter.inc',
    ),
    'download_file_formatter_direct_download_accessible_icon' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'download_file.formatter.inc',
    ),
    'download_file_direct_download_item' => array(
      'arguments' => array(
        'file' => NULL,
      ),
      'file' => 'download_file.formatter.inc',
    ),
    'download_file_direct_download_icon_item' => array(
      'arguments' => array(
        'file' => NULL,
      ),
      'file' => 'download_file.formatter.inc',
    ),
    'download_file_direct_download_item_accessible' => array(
      'arguments' => array(
        'file' => NULL,
      ),
      'file' => 'download_file.formatter.inc',
    ),
    'download_file_direct_download_icon_item_accessible' => array(
      'arguments' => array(
        'file' => NULL,
      ),
      'file' => 'download_file.formatter.inc',
    ),
    'download_file_detail_accessible' => array(
      'arguments' => array(
        'file' => NULL,
      ),
      'file' => 'download_file.formatter.inc',
    ),
    'download_file_extension_accessible' => array(
      'variables' => array(
        'extension' => NULL,
      ),
      'file' => 'download_file.formatter.inc',
    ),
    'download_file_bytes_accessible' => array(
      'arguments' => array(
        'filesize' => NULL,
      ),
      'file' => 'download_file.formatter.inc',
    ),
  );

  // ImageCache support
  if (module_exists('imagecache') && module_exists('imagefield')) {
    foreach (imagecache_presets() as $preset) {
      $theme['download_file_formatter_' . $preset['presetname'] . '_imagelink_direct_download'] = array(
        'arguments' => array(
          'element' => NULL,
        ),
        'file' => 'download_file.formatter.inc',
        'function' => 'theme_download_file_formatter_imagecache_imagelink_direct_download',
      );
      $theme['download_file_formatter_' . $preset['presetname'] . '_accessiblelink_direct_download'] = array(
        'arguments' => array(
          'element' => NULL,
        ),
        'file' => 'download_file.formatter.inc',
        'function' => 'theme_download_file_formatter_imagecache_accessiblelink_direct_download',
      );
    }
    $theme['download_file_imagecache_imagelink_direct_download'] = array(
      'arguments' => array(
        'file' => NULL,
        'formatter' => NULL,
      ),
      'file' => 'download_file.formatter.inc',
    );
    $theme['download_file_imagecache_accessiblelink_direct_download'] = array(
      'arguments' => array(
        'file' => NULL,
        'formatter' => NULL,
      ),
      'file' => 'download_file.formatter.inc',
    );
  }

  // Insert support
  if (module_exists('insert')) {
    $theme['download_file_insert_direct_download_item'] = array(
      'arguments' => array(
        'file' => NULL,
        'widget' => NULL,
      ),
      'file' => 'download_file.inc.inc',
    );
    $theme['download_file_insert_direct_download_icon_item'] = array(
      'arguments' => array(
        'file' => NULL,
        'widget' => NULL,
      ),
      'file' => 'download_file.inc.inc',
    );
    $theme['download_file_insert_direct_download_item_accessible'] = array(
      'arguments' => array(
        'file' => NULL,
        'widget' => NULL,
      ),
      'file' => 'download_file.inc.inc',
    );
    $theme['download_file_insert_direct_download_icon_item_accessible'] = array(
      'arguments' => array(
        'file' => NULL,
        'widget' => NULL,
      ),
      'file' => 'download_file.inc.inc',
    );
    $theme['download_file_insert_link'] = array(
      'arguments' => array(
        'item' => NULL,
        'widget' => NULL,
      ),
      'template' => 'download-file-insert-link',
      'file' => 'download_file.inc',
    );
  }
  return $theme;
}

/**
 * Page who forces the download of the file by changing the header.
 *
 * @param $fid
 *   The filefield identifier.
 */
function download_file_direct_download($fid) {
  $fid = intval($fid);
  $file = field_file_load($fid);
  if (empty($fid) || empty($file) || !$file['status']) {
    return drupal_access_denied();
  }
  $file_name = $file['filename'];
  $headers = array(
    'Content-Type: force-download',
    'Content-Disposition: attachment; filename="' . $file_name . '"',
    'Content-Length: ' . $file['filesize'],
    'Content-Transfer-Encoding: binary',
    'Pragma: no-cache',
    'Cache-Control: must-revalidate, post-check=0, pre-check=0',
    'Expires: 0',
    'Accept-Ranges: bytes',
  );
  if (in_array(-1, $headers)) {
    return drupal_access_denied();
  }
  if (count($headers)) {
    file_transfer($file['filepath'], $headers);
  }
}

/**
 * Get the URL to download file.
 *
 * @param $fid
 *   The filefield identifier.
 * @return
 *   An string containing the URL to direct download file.
 */
function download_file_url($fid) {
  return url('download/file/fid/' . $fid);
}

/**
 * Get the path to download file.
 *
 * @param $fid
 *   The filefield identifier.
 * @return
 *   An string containing the path to direct download file.
 */
function download_file_path($fid) {
  return 'download/file/fid/' . $fid;
}

/**
 * Implementation of hook_field_formatter_info().
 */
function download_file_field_formatter_info() {
  $formatters = array(
    'direct_download' => array(
      'label' => t('Direct download file'),
      'field types' => array(
        'filefield',
      ),
      'multiple values' => CONTENT_HANDLE_MODULE,
      'description' => t('Displays a link to direct download the file.'),
    ),
    'direct_download_icon' => array(
      'label' => t('Direct download file with the type icon'),
      'field types' => array(
        'filefield',
      ),
      'multiple values' => CONTENT_HANDLE_MODULE,
      'description' => t('Displays a link to direct download the file with the type icon.'),
    ),
    'direct_download_accessible' => array(
      'label' => t('Direct download file accessible'),
      'field types' => array(
        'filefield',
      ),
      'multiple values' => CONTENT_HANDLE_MODULE,
      'description' => t('Displays a link accessible to direct download the file.'),
    ),
    'direct_download_accessible_icon' => array(
      'label' => t('Direct download file accessible with the type icon'),
      'field types' => array(
        'filefield',
      ),
      'multiple values' => CONTENT_HANDLE_MODULE,
      'description' => t('Displays a link accessible to direct download the file with the type icon.'),
    ),
  );

  // ImageCache support
  if (module_exists('imagecache') && module_exists('imagefield')) {
    foreach (imagecache_presets() as $preset) {
      $formatters[$preset['presetname'] . '_imagelink_direct_download'] = array(
        'label' => t('Direct download file with thumbnail @preset', array(
          '@preset' => $preset['presetname'],
        )),
        'field types' => array(
          'image',
          'filefield',
        ),
        'multiple values' => CONTENT_HANDLE_MODULE,
        'description' => t('Displays a link with a thumbnail to direct download the image.'),
      );
      $formatters[$preset['presetname'] . '_accessiblelink_direct_download'] = array(
        'label' => t('Direct download file accessible with thumbnail @preset', array(
          '@preset' => $preset['presetname'],
        )),
        'field types' => array(
          'image',
          'filefield',
        ),
        'multiple values' => CONTENT_HANDLE_MODULE,
        'description' => t('Displays a link accessible with a thumbnail to direct download the image.'),
      );
    }
  }
  return $formatters;
}

/**
 * Converts human readable file size (e.g. 10 MB, 200.20 GB) into bytes.
 *
 * @param $bytes
 *   File size in bytes.
 * @param $precision
 *   The precision after the decimal.
 * @return
 *   An array containing the formatted size and the unit.
 */
function download_file_format_bytes($bytes, $precision) {
  $units = array(
    t('B'),
    t('KB'),
    t('MB'),
    t('GB'),
    t('TB'),
    t('PB'),
  );
  $pow = floor(log($bytes) / log(1024));
  $output = sprintf('%.' . $precision . 'f ', $bytes / pow(1024, floor($pow)));
  return array(
    $output,
    $units[$pow],
  );
}

/**
 * Get the acronym of file extension.
 *
 * @param $file
 *   The file.
 * @return
 *   An array containing the formatted acronym of file extension.
 */
function download_file_format_extension($file) {
  $extension = pathinfo(strtoupper(check_plain($file['filename'])), PATHINFO_EXTENSION);
  switch ($file['filemime']) {

    // Word document types.
    case 'application/msword':
    case 'application/doc':
    case 'application/vnd.ms-word':
    case 'application/winword':
    case 'application/word':
    case 'application/vnd.ms-word.document.macroEnabled.12':
    case 'application/vnd.oasis.opendocument.text':
    case 'application/vnd.oasis.opendocument.text-template':
    case 'application/vnd.oasis.opendocument.text-master':
    case 'application/vnd.oasis.opendocument.text-web':
    case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
    case 'application/vnd.wordperfect':
      return array(
        'definition' => 'DOCument',
        'name' => 'DOC',
      );

    // Spreadsheet document types.
    case 'application/vnd.ms-excel':
    case 'application/xls':
    case 'application/msexcel':
    case 'application/vnd.ms-excel.sheet.macroEnabled.12':
    case 'application/vnd.oasis.opendocument.spreadsheet':
    case 'application/vnd.oasis.opendocument.spreadsheet-template':
    case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
      return array(
        'definition' => 'eXceL Spreadsheet',
        'name' => 'XLS',
      );

    // Presentation document types.
    case 'application/vnd.ms-powerpoint':
    case 'application/powerpoint':
    case 'application/mspowerpoint':
    case 'application/ms-powerpoint':
    case 'application/vnd.ms-powerpoint.presentation.macroEnabled.12':
    case 'application/vnd.oasis.opendocument.presentation':
    case 'application/vnd.oasis.opendocument.presentation-template':
    case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
      return array(
        'definition' => 'PowerPoinT',
        'name' => 'PPT',
      );

    // Portable document types.
    case 'application/pdf':
    case 'application/x-pdf':
    case 'application/x-bzpdf':
    case 'application/x-gzpdf':
    case 'applications/vnd.pdf':
    case 'text/pdf':
    case 'text/x-pdf':
      return array(
        'definition' => 'Portable Document Format',
        'name' => 'PDF',
      );

    // ZIP compressed archive types.
    case 'application/zip':
    case 'application/gzip':
    case 'application/gzip-compressed':
    case 'application/x-zip':
    case 'application/x-zip-compressed':
    case 'application/x-bzip':
    case 'application/x-compress':
    case 'application/x-compressed':
    case 'application/x-gzip':
    case 'application/x-tgz':
      return array(
        'definition' => 'Archive',
        'name' => 'ZIP',
      );

    // RAR compressed archive types.
    case 'application/tar':
    case 'application/x-rar':
    case 'application/x-bzip-compressed-tar':
    case 'application/x-tar':
    case 'application/x-gtar':
    case 'application/x-tarz':
      return array(
        'definition' => 'Roshal ARchive',
        'name' => 'RAR',
      );

    // 7z compressed archive types.
    case 'application/x-7z-compressed':
      return array(
        'definition' => '7-Zip Archive',
        'name' => '7Z',
      );

    // Text file types.
    case 'application/txt':
    case 'text/plain':
      return array(
        'definition' => 'TeXTe',
        'name' => 'TXT',
      );

    // CSV aliases.
    case 'application/csv':
    case 'text/csv':
    case 'text/comma-separated-values':
      return array(
        'definition' => 'Comma-Separated Values',
        'name' => 'CSV',
      );

    // HTML aliases.
    case 'application/xhtml+xml':
    case 'text/html':
      return array(
        'definition' => 'HyperText Markup Language',
        'name' => 'HTML',
      );

    // XML aliases.
    case 'application/xml':
    case 'text/xml':
      return array(
        'definition' => 'eXtensible Markup Language',
        'name' => 'XML',
      );

    // RSS aliases.
    case 'application/rss+xml':
      return array(
        'definition' => 'Really Simple Syndication',
        'name' => 'RSS',
      );

    // JPEG images.
    case 'image/jpeg':
    case 'image/jpg':
      return array(
        'definition' => 'Joint Photographic Experts Group',
        'name' => 'JPEG',
      );

    // PNG images.
    case 'application/png':
    case 'image/png':
      return array(
        'definition' => 'Portable Network Graphics',
        'name' => 'PNG',
      );

    // GIF images.
    case 'image/gif':
      return array(
        'definition' => 'Graphics Interchange Format',
        'name' => 'GIF',
      );

    // Executable types.
    case 'application/exe':
    case 'application/dos-exe':
    case 'application/x-macbinary':
    case 'application/x-ms-dos-executable':
    case 'application/x-pef-executable':
      return array(
        'definition' => 'Executable',
        'name' => 'EXE',
      );

    // Audio Video Interleave types.
    case 'video/vnd.avi':
    case 'video/avi':
    case 'video/msvideo':
    case 'video/x-msvideo':
      return array(
        'definition' => 'Audio Video Interleave',
        'name' => 'AVI',
      );

    // QuickTime movie types.
    case 'video/quicktime':
    case 'video/x-quicktime':
      return array(
        'definition' => 'QuickTime MOVie',
        'name' => 'MOV',
      );

    // Windows audio types.
    case 'audio/vnd.wave':
    case 'audio/wav':
    case 'audio/wave':
    case 'audio/x-wav':
      return array(
        'definition' => 'WAVeform Audio File Format',
        'name' => 'WAV',
      );

    // MPEG audio types.
    case 'audio/mpeg':
    case 'audio/x-mpeg':
    case 'audio/mp3':
    case 'audio/x-mp3':
    case 'audio/mpeg3':
    case 'audio/x-mpeg3':
    case 'audio/MPA':
    case 'audio/mpa-robust':
      return array(
        'definition' => 'MPEG-1 Audio Layer 3',
        'name' => 'MP3',
      );
    default:
      return $extension;
  }
}

Functions

Namesort descending Description
download_file_direct_download Page who forces the download of the file by changing the header.
download_file_field_formatter_info Implementation of hook_field_formatter_info().
download_file_format_bytes Converts human readable file size (e.g. 10 MB, 200.20 GB) into bytes.
download_file_format_extension Get the acronym of file extension.
download_file_help Implementation of hook_help().
download_file_init Implementation of hook_init().
download_file_menu Implementation of hook_menu().
download_file_path Get the path to download file.
download_file_perm Implementation of hook_perm().
download_file_theme Implementation of hook_theme().
download_file_url Get the URL to download file.