You are here

function download_count_file_access in Open Social 8.2

Same name and namespace in other branches
  1. 8.9 modules/custom/download_count/download_count.module \download_count_file_access()
  2. 8 modules/custom/download_count/download_count.module \download_count_file_access()
  3. 8.3 modules/custom/download_count/download_count.module \download_count_file_access()
  4. 8.4 modules/custom/download_count/download_count.module \download_count_file_access()
  5. 8.5 modules/custom/download_count/download_count.module \download_count_file_access()
  6. 8.6 modules/custom/download_count/download_count.module \download_count_file_access()
  7. 8.7 modules/custom/download_count/download_count.module \download_count_file_access()
  8. 8.8 modules/custom/download_count/download_count.module \download_count_file_access()
  9. 10.3.x modules/custom/download_count/download_count.module \download_count_file_access()
  10. 10.0.x modules/custom/download_count/download_count.module \download_count_file_access()
  11. 10.1.x modules/custom/download_count/download_count.module \download_count_file_access()
  12. 10.2.x modules/custom/download_count/download_count.module \download_count_file_access()

Implements hook_file_access().

File

modules/custom/download_count/download_count.module, line 30
Tracks file downloads for files stored in the drupal files table.

Code

function download_count_file_access($entity, $operation, $account) {
  if ($operation == 'download') {
    $flood = \Drupal::flood();
    $config = \Drupal::config('download_count.settings');
    $flood_window = $config
      ->get('download_count_flood_window');
    $time = \Drupal::time()
      ->getRequestTime();

    // Invalidate file field formatter.
    $cache_tag = 'file:' . $entity
      ->id();
    Cache::invalidateTags([
      $cache_tag,
    ]);
    if ($account
      ->hasPermission('skip download counts')) {
      return;
    }

    // Check flood control.
    $flood_limit = $config
      ->get('download_count_flood_limit');
    if ($flood_limit > 0) {
      if (!$flood
        ->isAllowed('download_count-fid_' . $entity
        ->id(), $flood_limit, $flood_window)) {
        return;
      }
    }

    // Validate file has extension that should be counted, if not return.
    $extensions = explode(' ', Unicode::strtolower(trim($config
      ->get('download_count_excluded_file_extensions'))));
    if (count($extensions)) {
      $extension = Unicode::strtolower(pathinfo($entity
        ->getFilename(), PATHINFO_EXTENSION));
      if (in_array($extension, $extensions)) {
        return;
      }
    }

    // Count the download.
    $ip = Drupal::request()
      ->getClientIp();
    $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : t('Direct download');
    $references = file_get_file_references($entity, NULL, EntityStorageInterface::FIELD_LOAD_REVISION, NULL);
    foreach ($references as $entity_map) {
      foreach ($entity_map as $referencing_entities) {
        foreach ($referencing_entities as $referencing_entity) {
          $entity_type = $referencing_entity
            ->getEntityTypeId();
          $entity_id = $referencing_entity
            ->id();
        }
      }
    }
    if (!empty($entity_type) && !empty($entity_id)) {
      $connection = Database::getConnection();
      $connection
        ->insert('download_count')
        ->fields([
        'fid' => $entity
          ->id(),
        'uid' => $account
          ->id(),
        'type' => $entity_type,
        'id' => $entity_id,
        'ip_address' => $ip,
        'referrer' => $referrer,
        'timestamp' => $time,
      ])
        ->execute();
      $flood
        ->register('download_count-fid_' . $entity
        ->id(), $flood_window);
    }
  }
}