You are here

function advagg_sri_get_filenames_hashes in Advanced CSS/JS Aggregation 7.2

Returns the hashes settings.

Parameters

array $filenames: An array of filenames.

Return value

array The hashes for the given filenames.

1 call to advagg_sri_get_filenames_hashes()
advagg_sri_advagg_build_aggregate_plans_post_alter in advagg_sri/advagg_sri.advagg.inc
Implements hook_advagg_build_aggregate_plans_post_alter().

File

advagg_sri/advagg_sri.advagg.inc, line 177
Advanced aggregation sri module.

Code

function advagg_sri_get_filenames_hashes(array $filenames) {
  $hashes = array();

  // Do not use the DB if in development mode.
  if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 0) {
    $rows = db_select('advagg_sri', 'advagg_sri')
      ->fields('advagg_sri', array(
      'filename',
      'hashes',
    ))
      ->condition('filename', $filenames, 'IN')
      ->execute();
    foreach ($rows as $row) {
      $hashes[$row->filename] = unserialize($row->hashes);
    }
  }

  // If the hash is not in the database, generate it on demand.
  $db_filenames = array_keys($hashes);
  $not_in_db = array_diff($filenames, $db_filenames);
  foreach ($not_in_db as $file) {
    $filepath = array_search($file, $filenames);
    if (is_readable($filepath)) {

      // Do not use advagg_file_get_contents here.
      $contents = (string) @file_get_contents($filepath);
      if (!empty($contents)) {
        $hashes[$file] = advagg_sri_compute_hashes($contents);
        advagg_sri_set_filename_hashes($file, $hashes[$file]);
      }
    }
  }

  // Check to make sure we have all hashes.
  $all_hashes = array_keys($hashes);
  $not_hashed = array_diff($filenames, $all_hashes);
  if (!empty($not_hashed)) {
    drupal_page_is_cacheable(FALSE);

    // Disable saving to the cache if a sri is missing.
    if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) > 1) {
      $GLOBALS['conf']['advagg_cache_level'] = 0;
    }
    if (!module_exists('httprl') || !variable_get('advagg_use_httprl', ADVAGG_USE_HTTPRL)) {
      watchdog('advagg_sri', 'The subresource integrity hashes could not be generated for these files: %files', array(
        '%files' => print_r($not_hashed, TRUE),
      ));
    }
  }
  return $hashes;
}