You are here

function advagg_install_get_first_advagg_file in Advanced CSS/JS Aggregation 7.2

Given a advagg path this will return the first aggregate it can find.

Parameters

string $directory: Path to the advagg css/js dir.

string $type: String: css or js.

Return value

string Returns aggregate filename or an empty string on failure.

2 calls to advagg_install_get_first_advagg_file()
advagg_admin_info_form in ./advagg.admin.inc
Form builder; Show info about advagg and advagg settings.
advagg_install_check_via_http in ./advagg.install
Make sure http requests to css/js files work correctly.

File

./advagg.install, line 2412
Handles Advanced Aggregation installation and upgrade tasks.

Code

function advagg_install_get_first_advagg_file($directory, $type) {
  module_load_include('inc', 'advagg', 'advagg.missing');
  $filename = '';

  // Get contents of the advagg directory.
  $scanned_directory = @scandir($directory);

  // Bailout here if the advagg directory is empty.
  if (empty($scanned_directory)) {

    // Get a file that will generate from the database.
    return advagg_generate_advagg_filename_from_db($type);
  }

  // Filter list.
  $blacklist = array(
    '..',
    '.',
    '.htaccess',
    'parts',
  );
  $scanned_directory = array_diff($scanned_directory, $blacklist);

  // Make the last file empty.
  $scanned_directory[] = '';
  foreach ($scanned_directory as $key => $filename) {

    // Skip if filename is not long enough.
    $len = strlen($filename);
    if ($len < 91 + strlen(ADVAGG_SPACE) * 3) {
      continue;
    }

    // See if this uri contains .gz near the end of it.
    $pos = strripos($filename, '.gz', 91 + strlen(ADVAGG_SPACE) * 3);
    if (!empty($pos)) {

      // If this is a .gz file skip.
      if ($pos == $len - 3) {
        continue;
      }
    }
    $gzip_filename = $scanned_directory[$key + 1];
    $br_filename = $scanned_directory[$key + 1];
    if (function_exists('brotli_compress') && defined('BROTLI_TEXT') && variable_get('advagg_brotli', ADVAGG_BROTLI)) {
      $gzip_filename = $scanned_directory[$key + 2];

      // Skip if the next file does not have a .br extension.
      // This can occur if:
      // - File is not .br compressed, or,
      // - Using s3fs module and only .br compression is set. In
      //   this case, the advagg_advadgg_save_aggregate_alter()
      //   function will not add a file extension.
      if (strcmp($filename . '.br', $br_filename) !== 0 && (!module_exists('s3fs') || module_exists('s3fs') && variable_get('advagg_gzip', ADVAGG_GZIP))) {
        continue;
      }
    }
    else {

      // Skip if the next file is a .br file.
      if (strcmp($filename . '.br', $br_filename) === 0) {
        continue;
      }
    }
    if (variable_get('advagg_gzip', ADVAGG_GZIP)) {

      // Skip if the next file does not have a .gz extension.
      // This can occur if:
      // - File is not .gz compressed, or,
      // - Using s3fs module and either:
      //   - Only .gz compression option is set or,
      //   - Both .gz and .br compression options are set. In
      //     this case, the advagg_advagg_save_aggregate_alter()
      //     function creates a .gz file by default.
      if (strcmp($filename . '.gz', $gzip_filename) !== 0 && !module_exists('s3fs')) {
        continue;
      }
    }
    else {

      // Skip if the next file is a .gz file.
      if (strcmp($filename . '.gz', $gzip_filename) === 0) {
        continue;
      }
    }
    $data = advagg_get_hashes_from_filename(basename($filename));
    if (is_array($data)) {
      list($type, $aggregate_filenames_hash, $aggregate_contents_hash) = $data;

      // Get a list of files.
      $files = advagg_get_files_from_hashes($type, $aggregate_filenames_hash, $aggregate_contents_hash);
      if (!empty($files)) {

        // All checked passed above, break out of loop.
        break;
      }
    }
  }
  if (empty($filename)) {
    return advagg_generate_advagg_filename_from_db($type);
  }
  return $filename;
}