You are here

function _uc_file_download_validate in Ubercart 7.3

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

Performs first-pass authorization. Calls authorization hooks afterwards.

Called when a user requests a file download, function checks download limits then checks for any implementation of hook_uc_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 250
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' => format_username($user),
  )) : 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', REQUEST_TIME + 86400);
    if ($requests == UC_FILE_REQUEST_LIMIT) {

      // $message_user has already been passed through check_plain()
      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) {

    // $message_user has already been passed through check_plain()
    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) {

    // $message_user has already been passed through check_plain()
    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 && REQUEST_TIME >= $file_download->expiration) {

    // $message_user has already been passed through check_plain()
    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_uc_download_authorize() calls deny the download
  foreach (module_implements('uc_download_authorize') as $module) {
    $name = $module . '_uc_download_authorize';
    $result = $name($user, $file_download);
    if (!$result) {
      return UC_FILE_ERROR_HOOK_ERROR;
    }
  }

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