You are here

function _cdn_basic_farfuture_generate_file in CDN 7.2

Same name and namespace in other branches
  1. 6.2 cdn.basic.farfuture.inc \_cdn_basic_farfuture_generate_file()

Perform a nested HTTP request to generate a file.

Parameters

$path: A path relative to the Drupal root (not urlencoded!) to the file that should be generated.

$original_uri: The original url if available.

Return value

Whether the file was generated or not.

1 call to _cdn_basic_farfuture_generate_file()
cdn_file_url_alter in ./cdn.module
Implements hook_file_url_alter().

File

./cdn.basic.farfuture.inc, line 460
Far Future expiration setting for basic mode.

Code

function _cdn_basic_farfuture_generate_file($path, $original_uri = NULL) {

  // Check if there's a file to generate in the first place!
  if (!menu_get_item($path)) {
    return FALSE;
  }

  // While it should already be impossible to enter recursion because of the
  // above menu system check, we still want to detect this just to be safe.
  // This really can only happen if a file is missing and we try to generate
  // it and the request to generate the file itself triggers a 404, which
  // again references the file that is missing, and would thus again trigger a
  // 404, etc.
  // @see http://drupal.org/node/1417616#comment-5694960
  if (request_uri() == base_path() . $path) {
    watchdog('cdn', 'Recursion detected for %file!', array(
      '%file' => $path,
    ), WATCHDOG_ALERT);
    header('HTTP/1.1 404 Not Found');
    exit;
  }
  $url = $GLOBALS['base_url'] . '/' . drupal_encode_path($path);

  // If this is an image style url ensure the image style token is set. Since we
  // have just limited information to work with the evaluation is quite complex.
  // The token query is added even if the 'image_allow_insecure_derivatives'
  // variable is TRUE, so that the emitted links remain valid if it is changed
  // back to the default FALSE.
  if (($scheme = file_uri_scheme($original_uri)) && file_stream_wrapper_valid_scheme($scheme) && stripos($original_uri, $scheme . '://styles/') === 0) {
    $parts = explode('/', $original_uri, 6);
    $orinal_image_path = $scheme . '://' . $parts[5];
    $token_query = array(
      IMAGE_DERIVATIVE_TOKEN => image_style_path_token($parts[3], $orinal_image_path),
    );
    $url .= (strpos($path, '?') !== FALSE ? '&' : '?') . drupal_http_build_query($token_query);
  }
  $headers = array(
    // Make sure we hit the server and do not end up with a stale
    // cached version.
    'Cache-Control' => 'no-cache',
    'Pragma' => 'no-cache',
  );
  drupal_http_request($url, array(
    'headers' => $headers,
  ));
  $exists = file_exists($path);
  watchdog('cdn', 'Nested HTTP request to generate %file: %result (URL: %url, time: !time).', array(
    '!time' => (int) $_SERVER['REQUEST_TIME'],
    '%file' => $path,
    '%url' => $url,
    '%result' => $exists ? 'success' : 'failure',
  ), $exists ? WATCHDOG_NOTICE : WATCHDOG_CRITICAL);
  return $exists;
}