function nopremium_file_download in Node Option Premium 7
Implementation of hook_file_download().
@todo use hook_file_download_access_alter() instead once file_file_download() is fixed. See http://drupal.org/node/1245220.
File
- ./
nopremium.module, line 255 - Written by Henri MEDOT <henri.medot[AT]absyx[DOT]fr> http://www.absyx.fr
Code
function nopremium_file_download($uri, $field_type = 'file') {
global $user;
// Get the file record based on the URI. If not in the database just return.
$files = file_load_multiple(array(), array(
'uri' => $uri,
));
if (count($files)) {
foreach ($files as $item) {
// Since some database servers sometimes use a case-insensitive comparison
// by default, double check that the filename is an exact match.
if ($item->uri === $uri) {
$file = $item;
break;
}
}
}
if (!isset($file)) {
return;
}
// Find out which (if any) fields of this type contain the file.
$references = file_get_file_references($file, NULL, FIELD_LOAD_CURRENT, $field_type);
// Stop processing if there are no references in order to avoid returning
// headers for files controlled by other modules. Make an exception for
// temporary files where the host entity has not yet been saved (for example,
// an image preview on a node/add form) in which case, allow download by the
// file's owner.
if (empty($references) && ($file->status == FILE_STATUS_PERMANENT || $file->uid != $user->uid)) {
return;
}
// Loop through all references of this file. If a reference explicitly denies
// access to the field to which this file belongs, according to
// nopremium_access_full_content, no further checks are done and download
// access is denied.
foreach ($references as $field_name => $field_references) {
if (!empty($field_references['node'])) {
foreach ($field_references['node'] as $id => $reference) {
// Try to load $entity and $field.
$entity = entity_load('node', array(
$id,
));
$entity = reset($entity);
$field = field_info_field($field_name);
// Load the field item that references the file if $entity is premium.
if ($entity && $entity->premium) {
// Load all field items for that entity.
$field_items = field_get_items('node', $entity, $field_name);
// Find the field item with the matching URI.
foreach ($field_items as $item) {
if ($item['uri'] == $uri) {
if (!nopremium_access_full_content($entity)) {
return -1;
}
break;
}
}
}
}
}
}
}