function _cdn_html_alter_file_url in CDN 7.2
Same name and namespace in other branches
- 6.2 cdn.fallback.inc \_cdn_html_alter_file_url()
Alter the file URLs in a piece of HTML given a regexp pattern and some additional parameters.
Parameters
&$html: The HTML in which file URLs will be altered.
$pattern: A regular expression pattern to apply to the subject.
$search_index: The index of the search string in the array of regexp matches.
$path_index: The index of the file path in the array of regexp matches.
$querystring_index: The index of (an optional) query string in the array of regexp matches.
$prefix: $search_index will be replaced by $prefix, plus the altered file URL, plus the @suffix. If numeric, then it is assumed to be the index of the prefix in the array of regexp matches.
$suffix: See $prefix.
$comparison_index: The index of a comparison path whose file extension should match the file extension of the path located at $path_index.
$require_extension_match: Whether the extension must match a specific extension mapped to a CDN server. This is used when it is undesirable for a path matching only the '*' matcher in the CDN mapping, such as when converting miscellaneous anchor links.
2 calls to _cdn_html_alter_file_url()
- cdn_html_alter_anchor_urls in ./
cdn.fallback.inc - Alter file download URLs.
- cdn_html_alter_image_urls in ./
cdn.fallback.inc - Alter image file URLs in a piece of HTML.
File
- ./
cdn.fallback.inc, line 109 - Fallback when hook_file_url_alter() is not available (i.e. when the core patch is not installed or when not using Pressflow): use the Parallel module's logic (with some adaptations to be able to use the CDN module's logic (in particular:…
Code
function _cdn_html_alter_file_url(&$html, $pattern, $search_index, $path_index, $querystring_index, $prefix, $suffix, $comparison_index = FALSE, $require_extension_match = FALSE) {
$mapping =& drupal_static(__FUNCTION__ . '$mapping');
// Find a match against the given pattern.
preg_match_all($pattern, $html, $matches);
// Generate replacements to alter file URLs.
$searches = array();
$replacements = array();
for ($i = 0; $i < count($matches[0]); $i++) {
$search = $matches[$search_index][$i];
$path = $matches[$path_index][$i];
$prefix_string = is_numeric($prefix) ? $matches[$prefix][$i] : $prefix;
$suffix_string = is_numeric($suffix) ? $matches[$suffix][$i] : $suffix;
// Compare the filename in the path with that of another index. The file
// URL only is rewritten if it matches.
if ($comparison_index) {
$comparison = $matches[$comparison_index][$i];
// Calculate length of extension.
$path_ext_pos = strrpos($path, '.');
$ext_length = -1 * (strlen($path) - $path_ext_pos);
if (substr($path, $ext_length) !== substr($comparison, $ext_length)) {
continue;
}
}
if ($require_extension_match) {
if (!isset($mapping)) {
cdn_load_include('basic');
$mapping = _cdn_basic_parse_raw_mapping(cdn_basic_get_mapping());
}
$file_extension = drupal_strtolower(pathinfo($path, PATHINFO_EXTENSION));
if (!array_key_exists($file_extension, $mapping)) {
continue;
}
}
// Store the current path as the old path, then let cdn_file_url_alter()
// do its magic by invoking all file_url_alter hooks. When the path hasn't
// changed and is not already root-relative or protocol-relative, then
// generate a file URL as Drupal core would: prepend the base path.
$old_path = $path;
drupal_alter('file_url', $path);
if ($path == $old_path && drupal_substr($path, 0, 1) != '/' && drupal_substr($path, 0, 2) != '//') {
$path = base_path() . $path;
}
$searches[] = $search;
$replacements[] = $prefix_string . $path . $matches[$querystring_index][$i] . $suffix_string;
}
// Apply the generated replacements ton the subject.
$html = str_replace($searches, $replacements, $html);
}