You are here

function advagg_missing_send_saved_file in Advanced CSS/JS Aggregation 7.2

Send the css/js file to the client.

Parameters

array $files_to_save: Array of filenames and data.

string $uri: Requested filename.

bool $created: If file was created in a different thread.

string $filename: Just the filename no path information.

string $type: String: css or js.

array $aggregate_settings: Array of settings. Used to generate the 307 redirect location.

1 call to advagg_missing_send_saved_file()
advagg_missing_generate in ./advagg.missing.inc
Generates a missing CSS/JS file and send it to client.

File

./advagg.missing.inc, line 229
Advanced CSS/JS aggregation module.

Code

function advagg_missing_send_saved_file(array $files_to_save, $uri, $created, $filename, $type, $redirect_counter, array $aggregate_settings = array()) {

  // Send a 304 if this is a repeat request.
  if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= REQUEST_TIME) {
    header("HTTP/1.1 304 Not Modified");
    exit;
  }
  $return_compressed_br = FALSE;
  $return_compressed_gz = FALSE;

  // Negotiate whether to use gzip compression.
  if (!empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
    if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'br') !== FALSE) {
      $return_compressed_br = TRUE;
    }
    if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) {
      $return_compressed_gz = TRUE;
    }
  }
  header('Vary: Accept-Encoding', FALSE);
  if (!empty($created)) {
    if ($return_compressed_br && file_exists($uri . '.br') && filesize($uri . '.br') > 0) {
      $uri .= '.br';
    }
    elseif ($return_compressed_gz && file_exists($uri . '.gz') && filesize($uri . '.gz') > 0) {
      $uri .= '.gz';
    }
    if (!isset($files_to_save[$uri]) && file_exists($uri) && filesize($uri)) {

      // Do not use advagg_file_get_contents here.
      $files_to_save[$uri] = (string) @file_get_contents($uri);
    }
  }

  // Make sure zlib.output_compression does not compress the output.
  ini_set('zlib.output_compression', '0');
  header('Vary: Accept-Encoding', FALSE);

  // Clear the output buffer.
  if (ob_get_level()) {
    ob_end_clean();
  }

  // Set generic far future headers.
  if (variable_get('advagg_farfuture_php', ADVAGG_FARFUTURE_PHP)) {
    advagg_missing_set_farfuture_headers();
  }

  // Return compressed content if we can.
  if ($return_compressed_br || $return_compressed_gz) {
    foreach ($files_to_save as $uri => $data) {

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

          // .br file exists, send it out.
          header('Content-Encoding: br');
          break;
        }
      }

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

          // .gz file exists, send it out.
          header('Content-Encoding: gzip');
          break;
        }
      }
    }
  }
  else {
    $data = trim(reset($files_to_save));
  }

  // Output file and exit.
  if (!empty($data)) {
    $strlen = strlen($data);

    // Send a 304 if this is a repeat request.
    if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
      $etags = explode(' ', $_SERVER['HTTP_IF_NONE_MATCH']);
      if ($etags[0] < REQUEST_TIME + 31 * 24 * 60 * 60 && isset($etags[1]) && $etags[1] == $strlen) {
        header("HTTP/1.1 304 Not Modified");
        exit;
      }
    }

    // Send out a 200 OK status.
    $default = ADVAGG_HTTP_200_CODE;
    if (module_exists('httprl') && variable_get('advagg_use_httprl', ADVAGG_USE_HTTPRL) && (is_callable('httprl_is_background_callback_capable') && httprl_is_background_callback_capable() || !is_callable('httprl_is_background_callback_capable'))) {

      // Use 203 instead of 200 if HTTPRL is being used.
      $default = 203;
    }
    $number = variable_get('advagg_http_200_code', $default);
    header("{$_SERVER['SERVER_PROTOCOL']} {$number} OK");

    // Insure the Last-Modified header is set so 304's work correctly.
    if (file_exists($uri) && ($filemtime = @filemtime($uri))) {
      header('Last-Modified: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', $filemtime));

      // Etags generation in php is broken due to millisecond precision for the
      // files mtime; apache has it, php does not.
    }
    else {
      header('Last-Modified: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', REQUEST_TIME));
    }

    // Set the Expires date 1 month into the future.
    if (variable_get('advagg_farfuture_php', ADVAGG_FARFUTURE_PHP)) {
      header('Expires: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', REQUEST_TIME + 31 * 24 * 60 * 60));
    }

    // Also send an etag out.
    header('Etag: ' . REQUEST_TIME . ' ' . $strlen);
    if ($type === 'css') {
      header("Content-Type: text/css");
    }
    elseif ($type === 'js') {
      header("Content-Type: application/javascript; charset=UTF-8");
    }
    header('X-AdvAgg: Generated file at ' . REQUEST_TIME);
    print $data;
    exit;
  }
  else {

    // Redirect and try again on failure.
    $uri = advagg_generate_location_uri($filename, $type, $aggregate_settings);
    ++$redirect_counter;
    $uri .= '?redirect_counter=' . $redirect_counter;
    header('Location: ' . $uri, TRUE, 307);
    exit;
  }
}