function download_count_file_download in Download Count 6.2
Same name and namespace in other branches
- 5 download_count.module \download_count_file_download()
- 6 download_count.module \download_count_file_download()
- 7.2 download_count.module \download_count_file_download()
Implementation of hook_file_download().
File
- ./
download_count.module, line 156 - Tracks file downloads for files stored in the drupal files table using the private files setting or custom private filefield.
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;
}
global $user;
$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);
// NULL: not known
// FALSE: not accessible
// TRUE: accessible
$accessible_file = NULL;
// check if the file is known by Upload
$accessible_file = _download_count_is_accessible_by_upload($filepath);
if ($accessible_file === NULL) {
// check if the file is known by CCK FileField
$accessible_file = _download_count_is_accessible_by_filefield($filepath);
}
if ($accessible_file === NULL) {
// not known by any hooks, so we don't care about this file
return;
}
// inaccessible file
if ($accessible_file === FALSE) {
watchdog('download_count', 'Failed to download %file', array(
'%file' => $filename,
), WATCHDOG_ERROR);
return;
}
// accessible file
if ($fileinfo = _download_count_get_file_by_upload($filepath)) {
// core upload file
$fid = $fileinfo->fid;
$nid = $fileinfo->nid;
$vid = $fileinfo->vid;
}
elseif ($fileinfo = _download_count_get_nodes_by_filefield($filepath)) {
//cck filefield
$fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", $filepath));
$node = array_pop($fileinfo);
$nid = $node->nid;
$vid = $node->vid;
}
$ip = ip_address();
$referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : NULL;
$time = time();
db_query("INSERT INTO {download_count} (fid, nid, uid, vid, ip_address, referrer, timestamp) VALUES (%d, %d, %d, %d, '%s', '%s', %d)", $fid, $nid, $user->uid, $vid, $ip, $referrer, $time);
watchdog('download_count', '%file was downloaded', array(
'%file' => $filename,
), WATCHDOG_NOTICE);
if (module_exists('rules')) {
rules_invoke_event('download_count_file_download', $pathinfo['basename'], $user, $nid, $ip, $referrer, $time);
}
}