You are here

function tft_download_file in Taxonomy File Tree 7.2

Page callback: check file access and start transfer.

1 string reference to 'tft_download_file'
tft_menu in ./tft.module
Implementation of hook_menu().

File

includes/tft.pages.inc, line 11
Defines all page callbacks for TFT.

Code

function tft_download_file($nid = NULL) {
  $node = node_load($nid);
  if (empty($node->field_file[LANGUAGE_NONE][0]['fid'])) {
    drupal_not_found();
  }
  elseif (node_access('view', $node)) {
    $file = file_load($node->field_file[LANGUAGE_NONE][0]['fid']);

    // If using private file system check hook_file_download
    $schema = file_uri_scheme($file->uri);
    if ($schema == 'private') {
      $headers = module_invoke_all('file_download', $file->uri);

      // If there is no module granting access or one denying access do not grant access
      if (empty($headers) || in_array(-1, $headers)) {
        drupal_access_denied();
        drupal_exit();
      }
    }
    $http_headers = array(
      'Content-Type' => $file->filemime,
      'Content-Disposition' => 'attachment; filename="' . $file->filename . '"',
    );
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
      $http_headers['Cache-Control'] = 'must-revalidate, post-check=0, pre-check=0';
      $http_headers['Pragma'] = 'public';
    }
    else {
      $http_headers['Pragma'] = 'no-cache';
    }
    file_transfer($file->uri, $http_headers);
  }
  else {
    drupal_access_denied();
  }
  return '';
}