function cdn_file_url_alter in CDN 6.2
Same name and namespace in other branches
- 8.3 cdn.module \cdn_file_url_alter()
- 7.2 cdn.module \cdn_file_url_alter()
Implementation of hook_file_url_alter().
File
- ./
cdn.module, line 18
Code
function cdn_file_url_alter(&$path) {
$mode = variable_get(CDN_MODE_VARIABLE, CDN_MODE_BASIC);
$farfuture = variable_get(CDN_BASIC_FARFUTURE_VARIABLE, CDN_BASIC_FARFUTURE_DEFAULT);
$stats = variable_get(CDN_STATS_VARIABLE, FALSE) && user_access(CDN_PERM_ACCESS_STATS);
$https_support = variable_get(CDN_HTTPS_SUPPORT_VARIABLE, FALSE);
$maintenance_mode = variable_get('maintenance_mode', FALSE);
$is_https_page = cdn_request_is_https();
// Don't alter file URLs when running update.php.
if (defined('MAINTENANCE_MODE')) {
return;
}
if (cdn_status_is_enabled()) {
// If the current path is an absolute or protocol-relative URL, return
// immediately.
$fragments = parse_url($path);
if (isset($fragments['host']) || drupal_substr($path, 0, 2) == '//') {
return;
}
if (!cdn_check_protocol()) {
return;
}
if (!cdn_check_drupal_path($_GET['q'])) {
return;
}
if (!cdn_check_file($path)) {
return;
}
if ($stats) {
cdn_load_include('stats');
$start = microtime();
}
// Alter the file path when using Origin Pull mode and using that mode's
// Far Future setting.
if ($mode == CDN_MODE_BASIC && $farfuture && !$maintenance_mode) {
cdn_load_include('basic.farfuture');
// We need the unescaped version of the path to perform file operations.
$path = urldecode($path);
// If the file does not yet exist, perform a normal HTTP request to this
// file, to generate it. (E.g. when ImageCache is used, this will
// generate the derivative file.) When that fails, don't serve it from
// the CDN.
if (!file_exists($path) && !_cdn_basic_farfuture_generate_file($path)) {
$path = drupal_urlencode($path);
return;
}
// Generate a unique file identifier (UFI).
$ufi = cdn_basic_farfuture_get_identifier($path);
// Now that file operations have been performed, re-encode the path.
$path = drupal_urlencode($path);
// Generate the new path.
$path_before_farfuture = $path;
// Generate a unique token to verify that the request was generated by
// CDN. We cannot use drupal_get_token() since it depends on the user
// session.
$path_info = pathinfo(urldecode($path));
$token = _cdn_hmac_base64($ufi . $path_info['filename'], drupal_get_private_key() . _cdn_get_hash_salt());
$path = "cdn/farfuture/{$token}/{$ufi}/{$path}";
}
// Load the include file that contains the logic for the mode that's
// currently enabled.
cdn_load_include($mode == CDN_MODE_BASIC ? 'basic' : 'advanced');
// Depending on the mode, use a different function to get the servers on
// which the file is available.
$servers = $mode == CDN_MODE_BASIC ? cdn_basic_get_servers($path) : cdn_advanced_get_servers($path);
// The file is not available on any server.
if (count($servers) == 0) {
$cdn_url = FALSE;
$server = FALSE;
}
elseif (count($servers) > 1 && function_exists('cdn_pick_server')) {
$picked_server = cdn_pick_server($servers);
$cdn_url = $picked_server['url'];
$server = $picked_server['server'];
}
elseif (count($servers) > 1 && variable_get(CDN_PICK_SERVER_PHP_CODE_VARIABLE, '') !== '') {
$php_code = variable_get(CDN_PICK_SERVER_PHP_CODE_VARIABLE, '');
$picked_server = _cdn_eval_pick_server($php_code, $servers);
$cdn_url = $picked_server['url'];
$server = $picked_server['server'];
}
elseif (count($servers > 1)) {
$filename = basename($servers[0]['url']);
$unique_file_id = hexdec(substr(md5($filename), 0, 5));
$choice = $unique_file_id % count($servers);
$cdn_url = $servers[$choice]['url'];
$server = $servers[$choice]['server'];
}
else {
$cdn_url = $servers[0]['url'];
$server = $servers[0]['server'];
}
// If the current page is being served via HTTPS, and the CDN supports
// HTTPS, then use the HTTPS file URL.
if ($is_https_page && $https_support && !empty($cdn_url)) {
$cdn_url = preg_replace('/^http:/', 'https:', $cdn_url);
}
// If the user can access it, add this to the per-page statistics.
if ($stats) {
$end = microtime();
$source_path = $mode == CDN_MODE_BASIC && $farfuture && !$maintenance_mode ? $path_before_farfuture : $path;
_cdn_devel_page_stats($source_path, $cdn_url, $server, $end - $start);
}
// Override the path with the corresponding CDN URL, *if* the file is
// available on the CDN (it may only be not available in advanced mode).
if ($cdn_url !== FALSE) {
$path = $cdn_url;
}
else {
// If the file is not available on the CDN and Origin Pull mode's Far
// Future setting is enabled, then don't serve it using CDN module's
// non-scalable Far Future functionality, but using Drupal's default.
if ($mode == CDN_MODE_BASIC && $farfuture) {
$path = $path_before_farfuture;
}
}
}
}