function download_count_file_download in Download Count 7.2
Same name and namespace in other branches
- 5 download_count.module \download_count_file_download()
- 6.2 download_count.module \download_count_file_download()
- 6 download_count.module \download_count_file_download()
Implements hook_file_download().
File
- ./
download_count.module, line 159 - 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) {
global $user;
$extensions = explode(' ', strtolower(trim(variable_get('download_count_excluded_file_extensions', 'jpg jpeg gif png'))));
if (count($extensions)) {
$pathinfo = pathinfo($filename);
if (in_array(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_query("SELECT fid FROM {files} WHERE filepath = :filepath", array(
':filepath' => $filepath,
))
->fetchField();
$node = array_pop($fileinfo);
$nid = $node->nid;
$vid = $node->vid;
}
// TODO Please review the conversion of this statement to the D7 database API syntax.
/* 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_address(), isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : NULL, REQUEST_TIME) */
$id = db_insert('download_count')
->fields(array(
'fid' => $fid,
'nid' => $nid,
'uid' => $user->uid,
'vid' => $vid,
'ip_address' => ip_address(),
'referrer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : NULL,
'timestamp' => REQUEST_TIME,
))
->execute();
watchdog('download_count', '%file was downloaded', array(
'%file' => $filename,
), WATCHDOG_NOTICE);
}