danielebarchiesi@4: array(
danielebarchiesi@4: 'title' => t('Correct URLs with Pathologic'),
danielebarchiesi@4: 'process callback' => '_pathologic_filter',
danielebarchiesi@4: 'settings callback' => '_pathologic_settings',
danielebarchiesi@4: 'default settings' => array(
danielebarchiesi@4: 'local_paths' => '',
danielebarchiesi@4: 'protocol_style' => 'full',
danielebarchiesi@4: ),
danielebarchiesi@4: // Set weight to 50 so that it will hopefully appear at the bottom of
danielebarchiesi@4: // filter lists by default. 50 is the maximum value of the weight menu
danielebarchiesi@4: // for each row in the filter table (the menu is hidden by JavaScript to
danielebarchiesi@4: // use table row dragging instead when JS is enabled).
danielebarchiesi@4: 'weight' => 50,
danielebarchiesi@4: )
danielebarchiesi@4: );
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: /**
danielebarchiesi@4: * Settings callback for Pathologic.
danielebarchiesi@4: */
danielebarchiesi@4: function _pathologic_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
danielebarchiesi@4: return array(
danielebarchiesi@4: 'reminder' => array(
danielebarchiesi@4: '#type' => 'item',
danielebarchiesi@4: '#title' => t('In most cases, Pathologic should be the last filter in the “Filter processing order” list.'),
danielebarchiesi@4: '#weight' => -10,
danielebarchiesi@4: ),
danielebarchiesi@4: 'protocol_style' => array(
danielebarchiesi@4: '#type' => 'radios',
danielebarchiesi@4: '#title' => t('Processed URL format'),
danielebarchiesi@4: '#default_value' => isset($filter->settings['protocol_style']) ? $filter->settings['protocol_style'] : $defaults['protocol_style'],
danielebarchiesi@4: '#options' => array(
danielebarchiesi@4: 'full' => t('Full URL (http://example.com/foo/bar
)'),
danielebarchiesi@4: 'proto-rel' => t('Protocol relative URL (//example.com/foo/bar
)'),
danielebarchiesi@4: 'path' => t('Path relative to server root (/foo/bar
)'),
danielebarchiesi@4: ),
danielebarchiesi@4: '#description' => t('The Full URL option is best for stopping broken images and links in syndicated content (such as in RSS feeds), but will likely lead to problems if your site is accessible by both HTTP and HTTPS. Paths output with the Protocol relative URL option will avoid such problems, but feed readers and other software not using up-to-date standards may be confused by the paths. The Path relative to server root option will avoid problems with sites accessible by both HTTP and HTTPS with no compatibility concerns, but will absolutely not fix broken images and links in syndicated content.'),
danielebarchiesi@4: '#weight' => 10,
danielebarchiesi@4: ),
danielebarchiesi@4: 'local_paths' => array(
danielebarchiesi@4: '#type' => 'textarea',
danielebarchiesi@4: '#title' => t('All base paths for this site'),
danielebarchiesi@4: '#default_value' => isset($filter->settings['local_paths']) ? $filter->settings['local_paths'] : $defaults['local_paths'],
danielebarchiesi@4: '#description' => t('If this site is or was available at more than one base path or URL, enter them here, separated by line breaks. For example, if this site is live at http://example.com/
but has a staging version at http://dev.example.org/staging/
, you would enter both those URLs here. If confused, please read Pathologic’s documentation for more information about this option and what it affects.', array('!docs' => 'http://drupal.org/node/257026')),
danielebarchiesi@4: '#weight' => 20,
danielebarchiesi@4: ),
danielebarchiesi@4: );
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: /**
danielebarchiesi@4: * Pathologic filter callback.
danielebarchiesi@4: *
danielebarchiesi@4: * Previous versions of this module worked (or, rather, failed) under the
danielebarchiesi@4: * assumption that $langcode contained the language code of the node. Sadly,
danielebarchiesi@4: * this isn't the case.
danielebarchiesi@4: * @see http://drupal.org/node/1812264
danielebarchiesi@4: * However, it turns out that the language of the current node isn't as
danielebarchiesi@4: * important as the language of the node we're linking to, and even then only
danielebarchiesi@4: * if language path prefixing (eg /ja/node/123) is in use. REMEMBER THIS IN THE
danielebarchiesi@4: * FUTURE, ALBRIGHT.
danielebarchiesi@4: *
danielebarchiesi@4: * @todo Can we do the parsing of the local path settings somehow when the
danielebarchiesi@4: * settings form is submitted instead of doing it here?
danielebarchiesi@4: */
danielebarchiesi@4: function _pathologic_filter($text, $filter, $format, $langcode, $cache, $cache_id) {
danielebarchiesi@4: // Get the base URL and explode it into component parts. We add these parts
danielebarchiesi@4: // to the exploded local paths settings later.
danielebarchiesi@4: global $base_url;
danielebarchiesi@4: $base_url_parts = parse_url($base_url . '/');
danielebarchiesi@4: // Since we have to do some gnarly processing even before we do the *really*
danielebarchiesi@4: // gnarly processing, let's static save the settings - it'll speed things up
danielebarchiesi@4: // if, for example, we're importing many nodes, and not slow things down too
danielebarchiesi@4: // much if it's just a one-off. But since different input formats will have
danielebarchiesi@4: // different settings, we build an array of settings, keyed by format ID.
danielebarchiesi@4: $settings = &drupal_static(__FUNCTION__, array());
danielebarchiesi@4: if (!isset($settings[$filter->format])) {
danielebarchiesi@4: $filter->settings['local_paths_exploded'] = array();
danielebarchiesi@4: if ($filter->settings['local_paths'] !== '') {
danielebarchiesi@4: // Build an array of the exploded local paths for this format's settings.
danielebarchiesi@4: // array_filter() below is filtering out items from the array which equal
danielebarchiesi@4: // FALSE - so empty strings (which were causing problems.
danielebarchiesi@4: // @see http://drupal.org/node/1727492
danielebarchiesi@4: $local_paths = array_filter(array_map('trim', explode("\n", $filter->settings['local_paths'])));
danielebarchiesi@4: foreach ($local_paths as $local) {
danielebarchiesi@4: $parts = parse_url($local);
danielebarchiesi@4: // Okay, what the hellish "if" statement is doing below is checking to
danielebarchiesi@4: // make sure we aren't about to add a path to our array of exploded
danielebarchiesi@4: // local paths which matches the current "local" path. We consider it
danielebarchiesi@4: // not a match, if…
danielebarchiesi@4: // @todo: This is pretty horrible. Can this be simplified?
danielebarchiesi@4: if (
danielebarchiesi@4: (
danielebarchiesi@4: // If this URI has a host, and…
danielebarchiesi@4: isset($parts['host']) &&
danielebarchiesi@4: (
danielebarchiesi@4: // Either the host is different from the current host…
danielebarchiesi@4: $parts['host'] !== $base_url_parts['host']
danielebarchiesi@4: // Or, if the hosts are the same, but the paths are different…
danielebarchiesi@4: // @see http://drupal.org/node/1875406
danielebarchiesi@4: || (
danielebarchiesi@4: // Noobs (like me): "xor" means "true if one or the other are
danielebarchiesi@4: // true, but not both."
danielebarchiesi@4: (isset($parts['path']) xor isset($base_url_parts['path']))
danielebarchiesi@4: || (isset($parts['path']) && isset($base_url_parts['path']) && $parts['path'] !== $base_url_parts['path'])
danielebarchiesi@4: )
danielebarchiesi@4: )
danielebarchiesi@4: ) ||
danielebarchiesi@4: // Or…
danielebarchiesi@4: (
danielebarchiesi@4: // The URI doesn't have a host…
danielebarchiesi@4: !isset($parts['host'])
danielebarchiesi@4: ) &&
danielebarchiesi@4: // And the path parts don't match (if either doesn't have a path
danielebarchiesi@4: // part, they can't match)…
danielebarchiesi@4: (
danielebarchiesi@4: !isset($parts['path']) ||
danielebarchiesi@4: !isset($base_url_parts['path']) ||
danielebarchiesi@4: $parts['path'] !== $base_url_parts['path']
danielebarchiesi@4: )
danielebarchiesi@4: ) {
danielebarchiesi@4: // Add it to the list.
danielebarchiesi@4: $filter->settings['local_paths_exploded'][] = $parts;
danielebarchiesi@4: }
danielebarchiesi@4: }
danielebarchiesi@4: }
danielebarchiesi@4: // Now add local paths based on "this" server URL.
danielebarchiesi@4: $filter->settings['local_paths_exploded'][] = array('path' => $base_url_parts['path']);
danielebarchiesi@4: $filter->settings['local_paths_exploded'][] = array('path' => $base_url_parts['path'], 'host' => $base_url_parts['host']);
danielebarchiesi@4: // We'll also just store the host part separately for easy access.
danielebarchiesi@4: $filter->settings['base_url_host'] = $base_url_parts['host'];
danielebarchiesi@4:
danielebarchiesi@4: $settings[$filter->format] = $filter->settings;
danielebarchiesi@4: }
danielebarchiesi@4: // Get the language code for the text we're about to process.
danielebarchiesi@4: $settings['langcode'] = $langcode;
danielebarchiesi@4: // And also take note of which settings in the settings array should apply.
danielebarchiesi@4: $settings['current_settings'] = &$settings[$filter->format];
danielebarchiesi@4:
danielebarchiesi@4: // Now that we have all of our settings prepared, attempt to process all
danielebarchiesi@4: // paths in href, src, action or longdesc HTML attributes. The pattern below
danielebarchiesi@4: // is not perfect, but the callback will do more checking to make sure the
danielebarchiesi@4: // paths it receives make sense to operate upon, and just return the original
danielebarchiesi@4: // paths if not.
danielebarchiesi@4: return preg_replace_callback('~(href|src|action|longdesc)="([^"]+)~i', '_pathologic_replace', $text);
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: /**
danielebarchiesi@4: * Process and replace paths. preg_replace_callback() callback.
danielebarchiesi@4: */
danielebarchiesi@4: function _pathologic_replace($matches) {
danielebarchiesi@4: // Get the settings for the filter. Since we can't pass extra parameters
danielebarchiesi@4: // through to a callback called by preg_replace_callback(), there's basically
danielebarchiesi@4: // three ways to do this that I can determine: use eval() and friends; abuse
danielebarchiesi@4: // globals; or abuse drupal_static(). The latter is the least offensive, I
danielebarchiesi@4: // guess… Note that we don't do the & thing here so that we can modify
danielebarchiesi@4: // $settings later and not have the changes be "permanent."
danielebarchiesi@4: $settings = drupal_static('_pathologic_filter');
danielebarchiesi@4: // If it appears the path is a scheme-less URL, prepend a scheme to it.
danielebarchiesi@4: // parse_url() cannot properly parse scheme-less URLs. Don't worry; if it
danielebarchiesi@4: // looks like Pathologic can't handle the URL, it will return the scheme-less
danielebarchiesi@4: // original.
danielebarchiesi@4:
danielebarchiesi@4: // @see https://drupal.org/node/1617944
danielebarchiesi@4: // @see https://drupal.org/node/2030789
danielebarchiesi@4: if (strpos($matches[2], '//') === 0) {
danielebarchiesi@4: if (isset($_SERVER['https']) && strtolower($_SERVER['https']) === 'on') {
danielebarchiesi@4: $matches[2] = 'https:' . $matches[2];
danielebarchiesi@4: }
danielebarchiesi@4: else {
danielebarchiesi@4: $matches[2] = 'http:' . $matches[2];
danielebarchiesi@4: }
danielebarchiesi@4: }
danielebarchiesi@4: // Now parse the URL after reverting HTML character encoding.
danielebarchiesi@4: // @see http://drupal.org/node/1672932
danielebarchiesi@4: $original_url = htmlspecialchars_decode($matches[2]);
danielebarchiesi@4: // …and parse the URL
danielebarchiesi@4: $parts = parse_url($original_url);
danielebarchiesi@4: // Do some more early tests to see if we should just give up now.
danielebarchiesi@4: if (
danielebarchiesi@4: // If parse_url() failed, give up.
danielebarchiesi@4: $parts === FALSE
danielebarchiesi@4: || (
danielebarchiesi@4: // If there's a scheme part and it doesn't look useful, bail out.
danielebarchiesi@4: isset($parts['scheme'])
danielebarchiesi@4: // We allow for the storage of permitted schemes in a variable, though we
danielebarchiesi@4: // don't actually give the user any way to edit it at this point. This
danielebarchiesi@4: // allows developers to set this array if they have unusual needs where
danielebarchiesi@4: // they don't want Pathologic to trip over a URL with an unusual scheme.
danielebarchiesi@4: // @see http://drupal.org/node/1834308
danielebarchiesi@4: // "files" and "internal" are for Path Filter compatibility.
danielebarchiesi@4: && !in_array($parts['scheme'], variable_get('pathologic_scheme_whitelist', array('http', 'https', 'files', 'internal')))
danielebarchiesi@4: )
danielebarchiesi@4: // Bail out if it looks like there's only a fragment part.
danielebarchiesi@4: || (isset($parts['fragment']) && count($parts) === 1)
danielebarchiesi@4: ) {
danielebarchiesi@4: // Give up by "replacing" the original with the same.
danielebarchiesi@4: return $matches[0];
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: if (isset($parts['path'])) {
danielebarchiesi@4: // Undo possible URL encoding in the path.
danielebarchiesi@4: // @see http://drupal.org/node/1672932
danielebarchiesi@4: $parts['path'] = rawurldecode($parts['path']);
danielebarchiesi@4: }
danielebarchiesi@4: else {
danielebarchiesi@4: $parts['path'] = '';
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: // Check to see if we're dealing with a file.
danielebarchiesi@4: // @todo Should we still try to do path correction on these files too?
danielebarchiesi@4: if (isset($parts['scheme']) && $parts['scheme'] === 'files') {
danielebarchiesi@4: // Path Filter "files:" support. What we're basically going to do here is
danielebarchiesi@4: // rebuild $parts from the full URL of the file.
danielebarchiesi@4: $new_parts = parse_url(file_create_url(file_default_scheme() . '://' . $parts['path']));
danielebarchiesi@4: // If there were query parts from the original parsing, copy them over.
danielebarchiesi@4: if (!empty($parts['query'])) {
danielebarchiesi@4: $new_parts['query'] = $parts['query'];
danielebarchiesi@4: }
danielebarchiesi@4: $new_parts['path'] = rawurldecode($new_parts['path']);
danielebarchiesi@4: $parts = $new_parts;
danielebarchiesi@4: // Don't do language handling for file paths.
danielebarchiesi@4: $settings['is_file'] = TRUE;
danielebarchiesi@4: }
danielebarchiesi@4: else {
danielebarchiesi@4: $settings['is_file'] = FALSE;
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: // Let's also bail out of this doesn't look like a local path.
danielebarchiesi@4: $found = FALSE;
danielebarchiesi@4: // Cycle through local paths and find one with a host and a path that matches;
danielebarchiesi@4: // or just a host if that's all we have; or just a starting path if that's
danielebarchiesi@4: // what we have.
danielebarchiesi@4: foreach ($settings['current_settings']['local_paths_exploded'] as $exploded) {
danielebarchiesi@4: // If a path is available in both…
danielebarchiesi@4: if (isset($exploded['path']) && isset($parts['path'])
danielebarchiesi@4: // And the paths match…
danielebarchiesi@4: && strpos($parts['path'], $exploded['path']) === 0
danielebarchiesi@4: // And either they have the same host, or both have no host…
danielebarchiesi@4: && (
danielebarchiesi@4: (isset($exploded['host']) && isset($parts['host']) && $exploded['host'] === $parts['host'])
danielebarchiesi@4: || (!isset($exploded['host']) && !isset($parts['host']))
danielebarchiesi@4: )
danielebarchiesi@4: ) {
danielebarchiesi@4: // Remove the shared path from the path. This is because the "Also local"
danielebarchiesi@4: // path was something like http://foo/bar and this URL is something like
danielebarchiesi@4: // http://foo/bar/baz; or the "Also local" was something like /bar and
danielebarchiesi@4: // this URL is something like /bar/baz. And we only care about the /baz
danielebarchiesi@4: // part.
danielebarchiesi@4: $parts['path'] = drupal_substr($parts['path'], drupal_strlen($exploded['path']));
danielebarchiesi@4: $found = TRUE;
danielebarchiesi@4: // Break out of the foreach loop
danielebarchiesi@4: break;
danielebarchiesi@4: }
danielebarchiesi@4: // Okay, we didn't match on path alone, or host and path together. Can we
danielebarchiesi@4: // match on just host? Note that for this one we are looking for paths which
danielebarchiesi@4: // are just hosts; not hosts with paths.
danielebarchiesi@4: elseif ((isset($parts['host']) && !isset($exploded['path']) && isset($exploded['host']) && $exploded['host'] === $parts['host'])) {
danielebarchiesi@4: // No further editing; just continue
danielebarchiesi@4: $found = TRUE;
danielebarchiesi@4: // Break out of foreach loop
danielebarchiesi@4: break;
danielebarchiesi@4: }
danielebarchiesi@4: // Is this is a root-relative url (no host) that didn't match above?
danielebarchiesi@4: // Allow a match if local path has no path,
danielebarchiesi@4: // but don't "break" because we'd prefer to keep checking for a local url
danielebarchiesi@4: // that might more fully match the beginning of our url's path
danielebarchiesi@4: // e.g.: if our url is /foo/bar we'll mark this as a match for
danielebarchiesi@4: // http://example.com but want to keep searching and would prefer a match
danielebarchiesi@4: // to http://example.com/foo if that's configured as a local path
danielebarchiesi@4: elseif (!isset($parts['host']) && (!isset($exploded['path']) || $exploded['path'] == '/')) {
danielebarchiesi@4: $found = TRUE;
danielebarchiesi@4: }
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: // If the path is not within the drupal root return original url, unchanged
danielebarchiesi@4: if (!$found) {
danielebarchiesi@4: return $matches[0];
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: // Okay, format the URL.
danielebarchiesi@4: // If there's still a slash lingering at the start of the path, chop it off.
danielebarchiesi@4: $parts['path'] = ltrim($parts['path'],'/');
danielebarchiesi@4:
danielebarchiesi@4: // Examine the query part of the URL. Break it up and look through it; if it
danielebarchiesi@4: // has a value for "q", we want to use that as our trimmed path, and remove it
danielebarchiesi@4: // from the array. If any of its values are empty strings (that will be the
danielebarchiesi@4: // case for "bar" if a string like "foo=3&bar&baz=4" is passed through
danielebarchiesi@4: // parse_str()), replace them with NULL so that url() (or, more
danielebarchiesi@4: // specifically, drupal_http_build_query()) can still handle it.
danielebarchiesi@4: if (isset($parts['query'])) {
danielebarchiesi@4: parse_str($parts['query'], $parts['qparts']);
danielebarchiesi@4: foreach ($parts['qparts'] as $key => $value) {
danielebarchiesi@4: if ($value === '') {
danielebarchiesi@4: $parts['qparts'][$key] = NULL;
danielebarchiesi@4: }
danielebarchiesi@4: elseif ($key === 'q') {
danielebarchiesi@4: $parts['path'] = $value;
danielebarchiesi@4: unset($parts['qparts']['q']);
danielebarchiesi@4: }
danielebarchiesi@4: }
danielebarchiesi@4: }
danielebarchiesi@4: else {
danielebarchiesi@4: $parts['qparts'] = NULL;
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: // If we don't have a path yet, bail out.
danielebarchiesi@4: if (!isset($parts['path'])) {
danielebarchiesi@4: return $matches[0];
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: // If we didn't previously identify this as a file, check to see if the file
danielebarchiesi@4: // exists now that we have the correct path relative to DRUPAL_ROOT
danielebarchiesi@4: if (!$settings['is_file']){
danielebarchiesi@4: $settings['is_file'] = !empty($parts['path']) && is_file(DRUPAL_ROOT . '/'. $parts['path']);
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: // Okay, deal with language stuff.
danielebarchiesi@4: if ($settings['is_file']) {
danielebarchiesi@4: // If we're linking to a file, use a fake LANGUAGE_NONE language object.
danielebarchiesi@4: // Otherwise, the path may get prefixed with the "current" language prefix
danielebarchiesi@4: // (eg, /ja/misc/message-24-ok.png)
danielebarchiesi@4: $parts['language_obj'] = (object) array('language' => LANGUAGE_NONE, 'prefix' => '');
danielebarchiesi@4: }
danielebarchiesi@4: else {
danielebarchiesi@4: // Let's see if we can split off a language prefix from the path.
danielebarchiesi@4: if (module_exists('locale')) {
danielebarchiesi@4: // Sometimes this file will be require_once-d by the locale module before
danielebarchiesi@4: // this point, and sometimes not. We require_once it ourselves to be sure.
danielebarchiesi@4: require_once DRUPAL_ROOT . '/includes/language.inc';
danielebarchiesi@4: list($language_obj, $path) = language_url_split_prefix($parts['path'], language_list());
danielebarchiesi@4: if ($language_obj) {
danielebarchiesi@4: $parts['path'] = $path;
danielebarchiesi@4: $parts['language_obj'] = $language_obj;
danielebarchiesi@4: }
danielebarchiesi@4: }
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: // If we get to this point and $parts['path'] is now an empty string (which
danielebarchiesi@4: // will be the case if the path was originally just "/"), then we
danielebarchiesi@4: // want to link to .
danielebarchiesi@4: if ($parts['path'] === '') {
danielebarchiesi@4: $parts['path'] = '';
danielebarchiesi@4: }
danielebarchiesi@4: // Build the parameters we will send to url()
danielebarchiesi@4: $url_params = array(
danielebarchiesi@4: 'path' => $parts['path'],
danielebarchiesi@4: 'options' => array(
danielebarchiesi@4: 'query' => $parts['qparts'],
danielebarchiesi@4: 'fragment' => isset($parts['fragment']) ? $parts['fragment'] : NULL,
danielebarchiesi@4: // Create an absolute URL if protocol_style is 'full' or 'proto-rel', but
danielebarchiesi@4: // not if it's 'path'.
danielebarchiesi@4: 'absolute' => $settings['current_settings']['protocol_style'] !== 'path',
danielebarchiesi@4: // If we seem to have found a language for the path, pass it along to
danielebarchiesi@4: // url(). Otherwise, ignore the 'language' parameter.
danielebarchiesi@4: 'language' => isset($parts['language_obj']) ? $parts['language_obj'] : NULL,
danielebarchiesi@4: // A special parameter not actually used by url(), but we use it to see if
danielebarchiesi@4: // an alter hook implementation wants us to just pass through the original
danielebarchiesi@4: // URL.
danielebarchiesi@4: 'use_original' => FALSE,
danielebarchiesi@4: ),
danielebarchiesi@4: );
danielebarchiesi@4:
danielebarchiesi@4: // Add the original URL to the parts array
danielebarchiesi@4: $parts['original'] = $original_url;
danielebarchiesi@4:
danielebarchiesi@4: // Now alter!
danielebarchiesi@4: // @see http://drupal.org/node/1762022
danielebarchiesi@4: drupal_alter('pathologic', $url_params, $parts, $settings);
danielebarchiesi@4:
danielebarchiesi@4: // If any of the alter hooks asked us to just pass along the original URL,
danielebarchiesi@4: // then do so.
danielebarchiesi@4: if ($url_params['options']['use_original']) {
danielebarchiesi@4: return $matches[0];
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: // If the path is for a file and clean URLs are disabled, then the path that
danielebarchiesi@4: // url() will create will have a q= query fragment, which won't work for
danielebarchiesi@4: // files. To avoid that, we use this trick to temporarily turn clean URLs on.
danielebarchiesi@4: // This is horrible, but it seems to be the sanest way to do this.
danielebarchiesi@4: // @see http://drupal.org/node/1672430
danielebarchiesi@4: // @todo Submit core patch allowing clean URLs to be toggled by option sent
danielebarchiesi@4: // to url()?
danielebarchiesi@4: if (!empty($settings['is_file'])) {
danielebarchiesi@4: $settings['orig_clean_url'] = !empty($GLOBALS['conf']['clean_url']);
danielebarchiesi@4: if (!$settings['orig_clean_url']) {
danielebarchiesi@4: $GLOBALS['conf']['clean_url'] = TRUE;
danielebarchiesi@4: }
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: // Now for the url() call. Drumroll, please…
danielebarchiesi@4: $url = url($url_params['path'], $url_params['options']);
danielebarchiesi@4:
danielebarchiesi@4: // If we turned clean URLs on before to create a path to a file, turn them
danielebarchiesi@4: // back off.
danielebarchiesi@4: if ($settings['is_file'] && !$settings['orig_clean_url']) {
danielebarchiesi@4: $GLOBALS['conf']['clean_url'] = FALSE;
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: // If we need to create a protocol-relative URL, then convert the absolute
danielebarchiesi@4: // URL we have now.
danielebarchiesi@4: if ($settings['current_settings']['protocol_style'] === 'proto-rel') {
danielebarchiesi@4: // Now, what might have happened here is that url() returned a URL which
danielebarchiesi@4: // isn't on "this" server due to a hook_url_outbound_alter() implementation.
danielebarchiesi@4: // We don't want to convert the URL in that case. So what we're going to
danielebarchiesi@4: // do is cycle through the local paths again and see if the host part of
danielebarchiesi@4: // $url matches with the host of one of those, and only alter in that case.
danielebarchiesi@4: $url_parts = parse_url($url);
danielebarchiesi@4: if (!empty($url_parts['host']) && $url_parts['host'] === $settings['current_settings']['base_url_host']) {
danielebarchiesi@4: $url = _pathologic_url_to_protocol_relative($url);
danielebarchiesi@4: }
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: // Apply HTML character encoding, as is required for HTML attributes.
danielebarchiesi@4: // @see http://drupal.org/node/1672932
danielebarchiesi@4: $url = check_plain($url);
danielebarchiesi@4: // $matches[1] will be the tag attribute; src, href, etc.
danielebarchiesi@4: return "{$matches[1]}=\"{$url}";
danielebarchiesi@4: }
danielebarchiesi@4:
danielebarchiesi@4: /**
danielebarchiesi@4: * Convert a full URL with a protocol to a protocol-relative URL.
danielebarchiesi@4: *
danielebarchiesi@4: * As the Drupal core url() function doesn't support protocol-relative URLs, we
danielebarchiesi@4: * work around it by just creating a full URL and then running it through this
danielebarchiesi@4: * to strip off the protocol.
danielebarchiesi@4: *
danielebarchiesi@4: * Though this is just a one-liner, it's placed in its own function so that it
danielebarchiesi@4: * can be called independently from our test code.
danielebarchiesi@4: */
danielebarchiesi@4: function _pathologic_url_to_protocol_relative($url) {
danielebarchiesi@4: return preg_replace('~^https?://~', '//', $url);
danielebarchiesi@4: }