You are here

function _uc_file_download_validate in Ubercart 6.2

Same name and namespace in other branches
  1. 7.3 uc_file/uc_file.pages.inc \_uc_file_download_validate()

Performs 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 _uc_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 call to _uc_file_download_validate()
_uc_file_download in uc_file/uc_file.pages.inc
Handles file downloading and error states.

File

uc_file/uc_file.pages.inc, line 199
File menu items.

Code

function _uc_file_download_validate($file_download, &$user, $ip) {
  $request_cache = cache_get('uc_file_' . $ip);
  $requests = $request_cache ? $request_cache->data + 1 : 1;
  $message_user = $user->uid ? t('The user %username ', array(
    '%username' => $user->name,
  )) : t('The IP address %ip ', array(
    '%ip' => $ip,
  ));
  if ($requests > UC_FILE_REQUEST_LIMIT) {
    return UC_FILE_ERROR_TOO_MANY_BOGUS_REQUESTS;
  }

  // Must be a valid file.
  if (!$file_download || !is_readable($file_download->full_path)) {
    cache_set('uc_file_' . $ip, $requests, 'cache', time() + 86400);
    if ($requests == UC_FILE_REQUEST_LIMIT) {
      watchdog('uc_file', '%username has been temporarily banned from file downloads.', array(
        '%username' => $message_user,
      ), WATCHDOG_WARNING);
    }
    return UC_FILE_ERROR_INVALID_DOWNLOAD;
  }
  $addresses = $file_download->addresses;

  // Check the number of locations.
  if (!empty($file_download->address_limit) && !in_array($ip, $addresses) && count($addresses) >= $file_download->address_limit) {
    watchdog('uc_file', '%username has been denied a file download by downloading it from too many IP addresses.', array(
      '%username' => $message_user,
    ), WATCHDOG_WARNING);
    return UC_FILE_ERROR_TOO_MANY_LOCATIONS;
  }

  // Check the downloads so far.
  if (!empty($file_download->download_limit) && $file_download->accessed >= $file_download->download_limit) {
    watchdog('uc_file', '%username has been denied a file download by downloading it too many times.', array(
      '%username' => $message_user,
    ), WATCHDOG_WARNING);
    return UC_FILE_ERROR_TOO_MANY_DOWNLOADS;
  }

  // Check if it's expired.
  if ($file_download->expiration && time() >= $file_download->expiration) {
    watchdog('uc_file', '%username has been denied an expired file download.', array(
      '%username' => $message_user,
    ), WATCHDOG_WARNING);
    return UC_FILE_ERROR_EXPIRED;
  }

  // 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) {
      return UC_FILE_ERROR_HOOK_ERROR;
    }
  }

  // Everything's ok!
  watchdog('uc_file', '%username has started download of the file %filename.', array(
    '%username' => $message_user,
    '%filename' => basename($file_download->filename),
  ), WATCHDOG_NOTICE);
}