You are here

function download_count_file_download in Download Count 6

Same name and namespace in other branches
  1. 5 download_count.module \download_count_file_download()
  2. 6.2 download_count.module \download_count_file_download()
  3. 7.2 download_count.module \download_count_file_download()

Implementation of file_download()

File

./download_count.module, line 156
Download counter

Code

function download_count_file_download($filename, $checkonly = FALSE) {

  // Special use of hook_file_download() - 2nd argument added to indicate that it is not a download, but only an access check.
  if ($checkonly) {
    return;
  }
  $extensions = explode(' ', drupal_strtolower(trim(variable_get('download_count_excluded_file_extensions', 'jpg jpeg gif png'))));
  if (count($extensions)) {
    $pathinfo = pathinfo($filename);
    if (in_array(drupal_strtolower($pathinfo['extension']), $extensions)) {
      return;
    }
  }
  $filepath = file_create_path($filename);
  $result = db_query("SELECT u.nid, f.filepath FROM {upload} u JOIN {files} f ON f.fid = u.fid WHERE f.filepath = '%s'", $filepath);
  if ($file = db_fetch_object($result)) {
    if (user_access('view uploaded files') && node_access('view', node_load($file->nid))) {
      watchdog('download', '%file was downloaded', array(
        '%file' => $filename,
      ), WATCHDOG_NOTICE);

      // If the file is already added, just increment the count,
      // otherwise add the file with count 1
      if (db_result(db_query("SELECT filename FROM {file_downloads} WHERE filename = '%s'", $filename))) {
        db_query("UPDATE {file_downloads} SET count = count+1, timestamp = %d  WHERE filename = '%s'", time(), $filename);
      }
      else {
        db_query("INSERT INTO {file_downloads} (filename, count, timestamp) VALUES ('%s', 1,%d)", $filename, time());
      }
    }
    else {
      watchdog('download', 'Failed to download %file', array(
        '%file' => $filename,
      ), WATCHDOG_WARNING);
    }
  }
}