]*\s+)src\s*=\s*[\"|\'])($url_prefix_regex)([^\"|^\']*)([\"|\'])#i"; _cdn_html_alter_file_url($html, $pattern, 0, 4, 5, 1, ''); } /** * Generate the URL prefix regular expression, that supports all possible * types of file URLs: root-relative, protocol-relative and absolute URLs. */ function _cdn_generate_url_prefix_regex() { global $base_url; static $url_prefix_regex; if (!isset($url_prefix_regex)) { $url_prefix_regex = implode('|', array_map('preg_quote', array( base_path(), // Root-relative URL. '//' . $_SERVER['HTTP_HOST'] . base_path(), // Protocol-relative URL. $base_url . '/', // Absolute URL. ))); } return $url_prefix_regex; } /** * Alter the file URLs in a piece of HTML given a regexp pattern and some * additional parameters. * * @param &$html * The HTML in which file URLs will be altered. * @param $pattern * A regular expression pattern to apply to the subject. * @param $search_index * The index of the search string in the array of regexp matches. * @param $path_index * The index of the file path in the array of regexp matches. * @param $querystring_index * The index of (an optional) query string in the array of regexp matches. * @param $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. * @param $suffix * See $prefix. */ function _cdn_html_alter_file_url(&$html, $pattern, $search_index, $path_index, $querystring_index, $prefix, $suffix) { // 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; // 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); }