You are here

function _file_download in Ubercart 5

Perform first-pass authorization. Call authorization hooks afterwards.

Called when a user requests a file download, function checks download limits then checks for any implementation of hook_download_authorize. Passing that, the function _file_download_transfer is called.

Parameters

$fid: The fid of the file specified to download.

$key: The hash key of a user's download

1 string reference to '_file_download'
uc_file_menu in uc_file/uc_file.module
Implementation of hook_menu().

File

uc_file/uc_file.module, line 1023
Allows products to be associated with downloadable files.

Code

function _file_download($fid, $key) {
  global $user;

  // In case the $user doesn't exist (downloading with a Download Manager, or Anonymous) find the $user based on the URL given
  if (!$user->uid) {
    $user_id = db_result(db_query("SELECT uid FROM {uc_file_users} WHERE fid = %d and file_key = '%s'", $fid, $key));
    $user = user_load(array(
      'uid' => $user_id,
    ));
  }
  $ip = _uc_file_ip_address();
  $message_admin = t('Please contact the site administrator if this message has been received in error.');
  $message_user = $user->uid ? t('The user %username ', array(
    '%username' => $user->name,
  )) : t('The IP address %ip ', array(
    '%ip' => $ip,
  ));
  $file_download = db_fetch_object(db_query("SELECT * FROM {uc_file_users} WHERE fid = %d AND `file_key` = '%s'", $fid, $key));
  $request_cache = cache_get('uc_file_' . $ip);
  $requests = $request_cache ? $request_cache->data + 1 : 1;
  if ($requests > UC_FILE_REQUEST_LIMIT) {
    _file_download_deny($user->uid, t('You have attempted to download an incorrect file URL too many times. ') . $message_admin);
  }
  if (!$file_download) {
    cache_set('uc_file_' . $ip, 'cache', $requests, time() + 86400);
    if ($requests == UC_FILE_REQUEST_LIMIT) {
      watchdog('uc_file', t('%username has been temporarily banned from file downloads.', array(
        '%username' => $message_user,
      )), WATCHDOG_WARNING);
    }
    _file_download_deny($user->uid, t("The following URL is not a valid download link. ") . $message_admin);
  }
  else {
    $ip_limit = variable_get('uc_file_download_limit_addresses', NULL);
    $addresses = unserialize($file_download->addresses);
    if (!empty($ip_limit) && !in_array($ip, $addresses) && count($addresses) >= $ip_limit) {
      watchdog('uc_file', t('%username has been denied a file download by downloading it from too many IP addresses.', array(
        '%username' => $message_user,
      )), WATCHDOG_WARNING);
      _file_download_deny($user->uid, t('You have downloaded this file from too many different locations. ') . $message_admin);
    }
    else {
      $download_limit = variable_get('uc_file_download_limit_number', NULL);
      if (!empty($download_limit) && $file_download->accessed >= $download_limit) {
        watchdog('uc_file', t('%username has been denied a file download by downloading it too many times.', array(
          '%username' => $message_user,
        )), WATCHDOG_WARNING);
        _file_download_deny($user->uid, t('You have downloaded this file too many times. ') . $message_admin);
      }
      else {
        $duration_limit = _file_expiration_date($file_download->granted);
        if ($duration_limit !== FALSE && time() >= $duration_limit) {
          watchdog('uc_file', t('%username has been denied an expired file download.', array(
            '%username' => $message_user,
          )), WATCHDOG_WARNING);
          _file_download_deny($user->uid, t("This file download has expired. ") . $message_admin);
        }
        else {

          //Check any if any hook_download_authorize calls deny the download
          foreach (module_implements('download_authorize') as $module) {
            $name = $module . '_download_authorize';
            $result = $name($user, $file_download);
            if (!$result) {
              _file_download_deny($user->uid);
            }
          }
          $filename = db_result(db_query("SELECT filename FROM {uc_files} WHERE fid = %d", $fid));
          watchdog('uc_file', t('%username has started download of the file %filename.', array(
            '%username' => $message_user,
            '%filename' => basename($filename),
          )), WATCHDOG_NOTICE);
          _file_download_transfer($file_download, $ip, $file_download->fid);
        }
      }
    }
  }
}