annotate sites/all/modules/pathologic/pathologic.module @ 4:ce11bbd8f642

added modules
author danieleb <danielebarchiesi@me.com>
date Thu, 19 Sep 2013 10:38:44 +0100
parents
children
rev   line source
danielebarchiesi@4 1 <?php
danielebarchiesi@4 2
danielebarchiesi@4 3 /**
danielebarchiesi@4 4 * @file
danielebarchiesi@4 5 * Pathologic text filter for Drupal.
danielebarchiesi@4 6 *
danielebarchiesi@4 7 * This input filter attempts to make sure that link and image paths will
danielebarchiesi@4 8 * always be correct, even when domain names change, content is moved from one
danielebarchiesi@4 9 * server to another, the Clean URLs feature is toggled, etc.
danielebarchiesi@4 10 */
danielebarchiesi@4 11
danielebarchiesi@4 12 /**
danielebarchiesi@4 13 * Implements hook_filter_info().
danielebarchiesi@4 14 */
danielebarchiesi@4 15 function pathologic_filter_info() {
danielebarchiesi@4 16 return array(
danielebarchiesi@4 17 'pathologic' => array(
danielebarchiesi@4 18 'title' => t('Correct URLs with Pathologic'),
danielebarchiesi@4 19 'process callback' => '_pathologic_filter',
danielebarchiesi@4 20 'settings callback' => '_pathologic_settings',
danielebarchiesi@4 21 'default settings' => array(
danielebarchiesi@4 22 'local_paths' => '',
danielebarchiesi@4 23 'protocol_style' => 'full',
danielebarchiesi@4 24 ),
danielebarchiesi@4 25 // Set weight to 50 so that it will hopefully appear at the bottom of
danielebarchiesi@4 26 // filter lists by default. 50 is the maximum value of the weight menu
danielebarchiesi@4 27 // for each row in the filter table (the menu is hidden by JavaScript to
danielebarchiesi@4 28 // use table row dragging instead when JS is enabled).
danielebarchiesi@4 29 'weight' => 50,
danielebarchiesi@4 30 )
danielebarchiesi@4 31 );
danielebarchiesi@4 32 }
danielebarchiesi@4 33
danielebarchiesi@4 34 /**
danielebarchiesi@4 35 * Settings callback for Pathologic.
danielebarchiesi@4 36 */
danielebarchiesi@4 37 function _pathologic_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
danielebarchiesi@4 38 return array(
danielebarchiesi@4 39 'reminder' => array(
danielebarchiesi@4 40 '#type' => 'item',
danielebarchiesi@4 41 '#title' => t('In most cases, Pathologic should be the <em>last</em> filter in the &ldquo;Filter processing order&rdquo; list.'),
danielebarchiesi@4 42 '#weight' => -10,
danielebarchiesi@4 43 ),
danielebarchiesi@4 44 'protocol_style' => array(
danielebarchiesi@4 45 '#type' => 'radios',
danielebarchiesi@4 46 '#title' => t('Processed URL format'),
danielebarchiesi@4 47 '#default_value' => isset($filter->settings['protocol_style']) ? $filter->settings['protocol_style'] : $defaults['protocol_style'],
danielebarchiesi@4 48 '#options' => array(
danielebarchiesi@4 49 'full' => t('Full URL (<code>http://example.com/foo/bar</code>)'),
danielebarchiesi@4 50 'proto-rel' => t('Protocol relative URL (<code>//example.com/foo/bar</code>)'),
danielebarchiesi@4 51 'path' => t('Path relative to server root (<code>/foo/bar</code>)'),
danielebarchiesi@4 52 ),
danielebarchiesi@4 53 '#description' => t('The <em>Full URL</em> 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 <em>Protocol relative URL</em> option will avoid such problems, but feed readers and other software not using up-to-date standards may be confused by the paths. The <em>Path relative to server root</em> 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 54 '#weight' => 10,
danielebarchiesi@4 55 ),
danielebarchiesi@4 56 'local_paths' => array(
danielebarchiesi@4 57 '#type' => 'textarea',
danielebarchiesi@4 58 '#title' => t('All base paths for this site'),
danielebarchiesi@4 59 '#default_value' => isset($filter->settings['local_paths']) ? $filter->settings['local_paths'] : $defaults['local_paths'],
danielebarchiesi@4 60 '#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 <code>http://example.com/</code> but has a staging version at <code>http://dev.example.org/staging/</code>, you would enter both those URLs here. If confused, please read <a href="!docs">Pathologic&rsquo;s documentation</a> for more information about this option and what it affects.', array('!docs' => 'http://drupal.org/node/257026')),
danielebarchiesi@4 61 '#weight' => 20,
danielebarchiesi@4 62 ),
danielebarchiesi@4 63 );
danielebarchiesi@4 64 }
danielebarchiesi@4 65
danielebarchiesi@4 66 /**
danielebarchiesi@4 67 * Pathologic filter callback.
danielebarchiesi@4 68 *
danielebarchiesi@4 69 * Previous versions of this module worked (or, rather, failed) under the
danielebarchiesi@4 70 * assumption that $langcode contained the language code of the node. Sadly,
danielebarchiesi@4 71 * this isn't the case.
danielebarchiesi@4 72 * @see http://drupal.org/node/1812264
danielebarchiesi@4 73 * However, it turns out that the language of the current node isn't as
danielebarchiesi@4 74 * important as the language of the node we're linking to, and even then only
danielebarchiesi@4 75 * if language path prefixing (eg /ja/node/123) is in use. REMEMBER THIS IN THE
danielebarchiesi@4 76 * FUTURE, ALBRIGHT.
danielebarchiesi@4 77 *
danielebarchiesi@4 78 * @todo Can we do the parsing of the local path settings somehow when the
danielebarchiesi@4 79 * settings form is submitted instead of doing it here?
danielebarchiesi@4 80 */
danielebarchiesi@4 81 function _pathologic_filter($text, $filter, $format, $langcode, $cache, $cache_id) {
danielebarchiesi@4 82 // Get the base URL and explode it into component parts. We add these parts
danielebarchiesi@4 83 // to the exploded local paths settings later.
danielebarchiesi@4 84 global $base_url;
danielebarchiesi@4 85 $base_url_parts = parse_url($base_url . '/');
danielebarchiesi@4 86 // Since we have to do some gnarly processing even before we do the *really*
danielebarchiesi@4 87 // gnarly processing, let's static save the settings - it'll speed things up
danielebarchiesi@4 88 // if, for example, we're importing many nodes, and not slow things down too
danielebarchiesi@4 89 // much if it's just a one-off. But since different input formats will have
danielebarchiesi@4 90 // different settings, we build an array of settings, keyed by format ID.
danielebarchiesi@4 91 $settings = &drupal_static(__FUNCTION__, array());
danielebarchiesi@4 92 if (!isset($settings[$filter->format])) {
danielebarchiesi@4 93 $filter->settings['local_paths_exploded'] = array();
danielebarchiesi@4 94 if ($filter->settings['local_paths'] !== '') {
danielebarchiesi@4 95 // Build an array of the exploded local paths for this format's settings.
danielebarchiesi@4 96 // array_filter() below is filtering out items from the array which equal
danielebarchiesi@4 97 // FALSE - so empty strings (which were causing problems.
danielebarchiesi@4 98 // @see http://drupal.org/node/1727492
danielebarchiesi@4 99 $local_paths = array_filter(array_map('trim', explode("\n", $filter->settings['local_paths'])));
danielebarchiesi@4 100 foreach ($local_paths as $local) {
danielebarchiesi@4 101 $parts = parse_url($local);
danielebarchiesi@4 102 // Okay, what the hellish "if" statement is doing below is checking to
danielebarchiesi@4 103 // make sure we aren't about to add a path to our array of exploded
danielebarchiesi@4 104 // local paths which matches the current "local" path. We consider it
danielebarchiesi@4 105 // not a match, if…
danielebarchiesi@4 106 // @todo: This is pretty horrible. Can this be simplified?
danielebarchiesi@4 107 if (
danielebarchiesi@4 108 (
danielebarchiesi@4 109 // If this URI has a host, and…
danielebarchiesi@4 110 isset($parts['host']) &&
danielebarchiesi@4 111 (
danielebarchiesi@4 112 // Either the host is different from the current host…
danielebarchiesi@4 113 $parts['host'] !== $base_url_parts['host']
danielebarchiesi@4 114 // Or, if the hosts are the same, but the paths are different…
danielebarchiesi@4 115 // @see http://drupal.org/node/1875406
danielebarchiesi@4 116 || (
danielebarchiesi@4 117 // Noobs (like me): "xor" means "true if one or the other are
danielebarchiesi@4 118 // true, but not both."
danielebarchiesi@4 119 (isset($parts['path']) xor isset($base_url_parts['path']))
danielebarchiesi@4 120 || (isset($parts['path']) && isset($base_url_parts['path']) && $parts['path'] !== $base_url_parts['path'])
danielebarchiesi@4 121 )
danielebarchiesi@4 122 )
danielebarchiesi@4 123 ) ||
danielebarchiesi@4 124 // Or…
danielebarchiesi@4 125 (
danielebarchiesi@4 126 // The URI doesn't have a host…
danielebarchiesi@4 127 !isset($parts['host'])
danielebarchiesi@4 128 ) &&
danielebarchiesi@4 129 // And the path parts don't match (if either doesn't have a path
danielebarchiesi@4 130 // part, they can't match)…
danielebarchiesi@4 131 (
danielebarchiesi@4 132 !isset($parts['path']) ||
danielebarchiesi@4 133 !isset($base_url_parts['path']) ||
danielebarchiesi@4 134 $parts['path'] !== $base_url_parts['path']
danielebarchiesi@4 135 )
danielebarchiesi@4 136 ) {
danielebarchiesi@4 137 // Add it to the list.
danielebarchiesi@4 138 $filter->settings['local_paths_exploded'][] = $parts;
danielebarchiesi@4 139 }
danielebarchiesi@4 140 }
danielebarchiesi@4 141 }
danielebarchiesi@4 142 // Now add local paths based on "this" server URL.
danielebarchiesi@4 143 $filter->settings['local_paths_exploded'][] = array('path' => $base_url_parts['path']);
danielebarchiesi@4 144 $filter->settings['local_paths_exploded'][] = array('path' => $base_url_parts['path'], 'host' => $base_url_parts['host']);
danielebarchiesi@4 145 // We'll also just store the host part separately for easy access.
danielebarchiesi@4 146 $filter->settings['base_url_host'] = $base_url_parts['host'];
danielebarchiesi@4 147
danielebarchiesi@4 148 $settings[$filter->format] = $filter->settings;
danielebarchiesi@4 149 }
danielebarchiesi@4 150 // Get the language code for the text we're about to process.
danielebarchiesi@4 151 $settings['langcode'] = $langcode;
danielebarchiesi@4 152 // And also take note of which settings in the settings array should apply.
danielebarchiesi@4 153 $settings['current_settings'] = &$settings[$filter->format];
danielebarchiesi@4 154
danielebarchiesi@4 155 // Now that we have all of our settings prepared, attempt to process all
danielebarchiesi@4 156 // paths in href, src, action or longdesc HTML attributes. The pattern below
danielebarchiesi@4 157 // is not perfect, but the callback will do more checking to make sure the
danielebarchiesi@4 158 // paths it receives make sense to operate upon, and just return the original
danielebarchiesi@4 159 // paths if not.
danielebarchiesi@4 160 return preg_replace_callback('~(href|src|action|longdesc)="([^"]+)~i', '_pathologic_replace', $text);
danielebarchiesi@4 161 }
danielebarchiesi@4 162
danielebarchiesi@4 163 /**
danielebarchiesi@4 164 * Process and replace paths. preg_replace_callback() callback.
danielebarchiesi@4 165 */
danielebarchiesi@4 166 function _pathologic_replace($matches) {
danielebarchiesi@4 167 // Get the settings for the filter. Since we can't pass extra parameters
danielebarchiesi@4 168 // through to a callback called by preg_replace_callback(), there's basically
danielebarchiesi@4 169 // three ways to do this that I can determine: use eval() and friends; abuse
danielebarchiesi@4 170 // globals; or abuse drupal_static(). The latter is the least offensive, I
danielebarchiesi@4 171 // guess… Note that we don't do the & thing here so that we can modify
danielebarchiesi@4 172 // $settings later and not have the changes be "permanent."
danielebarchiesi@4 173 $settings = drupal_static('_pathologic_filter');
danielebarchiesi@4 174 // If it appears the path is a scheme-less URL, prepend a scheme to it.
danielebarchiesi@4 175 // parse_url() cannot properly parse scheme-less URLs. Don't worry; if it
danielebarchiesi@4 176 // looks like Pathologic can't handle the URL, it will return the scheme-less
danielebarchiesi@4 177 // original.
danielebarchiesi@4 178
danielebarchiesi@4 179 // @see https://drupal.org/node/1617944
danielebarchiesi@4 180 // @see https://drupal.org/node/2030789
danielebarchiesi@4 181 if (strpos($matches[2], '//') === 0) {
danielebarchiesi@4 182 if (isset($_SERVER['https']) && strtolower($_SERVER['https']) === 'on') {
danielebarchiesi@4 183 $matches[2] = 'https:' . $matches[2];
danielebarchiesi@4 184 }
danielebarchiesi@4 185 else {
danielebarchiesi@4 186 $matches[2] = 'http:' . $matches[2];
danielebarchiesi@4 187 }
danielebarchiesi@4 188 }
danielebarchiesi@4 189 // Now parse the URL after reverting HTML character encoding.
danielebarchiesi@4 190 // @see http://drupal.org/node/1672932
danielebarchiesi@4 191 $original_url = htmlspecialchars_decode($matches[2]);
danielebarchiesi@4 192 // …and parse the URL
danielebarchiesi@4 193 $parts = parse_url($original_url);
danielebarchiesi@4 194 // Do some more early tests to see if we should just give up now.
danielebarchiesi@4 195 if (
danielebarchiesi@4 196 // If parse_url() failed, give up.
danielebarchiesi@4 197 $parts === FALSE
danielebarchiesi@4 198 || (
danielebarchiesi@4 199 // If there's a scheme part and it doesn't look useful, bail out.
danielebarchiesi@4 200 isset($parts['scheme'])
danielebarchiesi@4 201 // We allow for the storage of permitted schemes in a variable, though we
danielebarchiesi@4 202 // don't actually give the user any way to edit it at this point. This
danielebarchiesi@4 203 // allows developers to set this array if they have unusual needs where
danielebarchiesi@4 204 // they don't want Pathologic to trip over a URL with an unusual scheme.
danielebarchiesi@4 205 // @see http://drupal.org/node/1834308
danielebarchiesi@4 206 // "files" and "internal" are for Path Filter compatibility.
danielebarchiesi@4 207 && !in_array($parts['scheme'], variable_get('pathologic_scheme_whitelist', array('http', 'https', 'files', 'internal')))
danielebarchiesi@4 208 )
danielebarchiesi@4 209 // Bail out if it looks like there's only a fragment part.
danielebarchiesi@4 210 || (isset($parts['fragment']) && count($parts) === 1)
danielebarchiesi@4 211 ) {
danielebarchiesi@4 212 // Give up by "replacing" the original with the same.
danielebarchiesi@4 213 return $matches[0];
danielebarchiesi@4 214 }
danielebarchiesi@4 215
danielebarchiesi@4 216 if (isset($parts['path'])) {
danielebarchiesi@4 217 // Undo possible URL encoding in the path.
danielebarchiesi@4 218 // @see http://drupal.org/node/1672932
danielebarchiesi@4 219 $parts['path'] = rawurldecode($parts['path']);
danielebarchiesi@4 220 }
danielebarchiesi@4 221 else {
danielebarchiesi@4 222 $parts['path'] = '';
danielebarchiesi@4 223 }
danielebarchiesi@4 224
danielebarchiesi@4 225 // Check to see if we're dealing with a file.
danielebarchiesi@4 226 // @todo Should we still try to do path correction on these files too?
danielebarchiesi@4 227 if (isset($parts['scheme']) && $parts['scheme'] === 'files') {
danielebarchiesi@4 228 // Path Filter "files:" support. What we're basically going to do here is
danielebarchiesi@4 229 // rebuild $parts from the full URL of the file.
danielebarchiesi@4 230 $new_parts = parse_url(file_create_url(file_default_scheme() . '://' . $parts['path']));
danielebarchiesi@4 231 // If there were query parts from the original parsing, copy them over.
danielebarchiesi@4 232 if (!empty($parts['query'])) {
danielebarchiesi@4 233 $new_parts['query'] = $parts['query'];
danielebarchiesi@4 234 }
danielebarchiesi@4 235 $new_parts['path'] = rawurldecode($new_parts['path']);
danielebarchiesi@4 236 $parts = $new_parts;
danielebarchiesi@4 237 // Don't do language handling for file paths.
danielebarchiesi@4 238 $settings['is_file'] = TRUE;
danielebarchiesi@4 239 }
danielebarchiesi@4 240 else {
danielebarchiesi@4 241 $settings['is_file'] = FALSE;
danielebarchiesi@4 242 }
danielebarchiesi@4 243
danielebarchiesi@4 244 // Let's also bail out of this doesn't look like a local path.
danielebarchiesi@4 245 $found = FALSE;
danielebarchiesi@4 246 // Cycle through local paths and find one with a host and a path that matches;
danielebarchiesi@4 247 // or just a host if that's all we have; or just a starting path if that's
danielebarchiesi@4 248 // what we have.
danielebarchiesi@4 249 foreach ($settings['current_settings']['local_paths_exploded'] as $exploded) {
danielebarchiesi@4 250 // If a path is available in both…
danielebarchiesi@4 251 if (isset($exploded['path']) && isset($parts['path'])
danielebarchiesi@4 252 // And the paths match…
danielebarchiesi@4 253 && strpos($parts['path'], $exploded['path']) === 0
danielebarchiesi@4 254 // And either they have the same host, or both have no host…
danielebarchiesi@4 255 && (
danielebarchiesi@4 256 (isset($exploded['host']) && isset($parts['host']) && $exploded['host'] === $parts['host'])
danielebarchiesi@4 257 || (!isset($exploded['host']) && !isset($parts['host']))
danielebarchiesi@4 258 )
danielebarchiesi@4 259 ) {
danielebarchiesi@4 260 // Remove the shared path from the path. This is because the "Also local"
danielebarchiesi@4 261 // path was something like http://foo/bar and this URL is something like
danielebarchiesi@4 262 // http://foo/bar/baz; or the "Also local" was something like /bar and
danielebarchiesi@4 263 // this URL is something like /bar/baz. And we only care about the /baz
danielebarchiesi@4 264 // part.
danielebarchiesi@4 265 $parts['path'] = drupal_substr($parts['path'], drupal_strlen($exploded['path']));
danielebarchiesi@4 266 $found = TRUE;
danielebarchiesi@4 267 // Break out of the foreach loop
danielebarchiesi@4 268 break;
danielebarchiesi@4 269 }
danielebarchiesi@4 270 // Okay, we didn't match on path alone, or host and path together. Can we
danielebarchiesi@4 271 // match on just host? Note that for this one we are looking for paths which
danielebarchiesi@4 272 // are just hosts; not hosts with paths.
danielebarchiesi@4 273 elseif ((isset($parts['host']) && !isset($exploded['path']) && isset($exploded['host']) && $exploded['host'] === $parts['host'])) {
danielebarchiesi@4 274 // No further editing; just continue
danielebarchiesi@4 275 $found = TRUE;
danielebarchiesi@4 276 // Break out of foreach loop
danielebarchiesi@4 277 break;
danielebarchiesi@4 278 }
danielebarchiesi@4 279 // Is this is a root-relative url (no host) that didn't match above?
danielebarchiesi@4 280 // Allow a match if local path has no path,
danielebarchiesi@4 281 // but don't "break" because we'd prefer to keep checking for a local url
danielebarchiesi@4 282 // that might more fully match the beginning of our url's path
danielebarchiesi@4 283 // e.g.: if our url is /foo/bar we'll mark this as a match for
danielebarchiesi@4 284 // http://example.com but want to keep searching and would prefer a match
danielebarchiesi@4 285 // to http://example.com/foo if that's configured as a local path
danielebarchiesi@4 286 elseif (!isset($parts['host']) && (!isset($exploded['path']) || $exploded['path'] == '/')) {
danielebarchiesi@4 287 $found = TRUE;
danielebarchiesi@4 288 }
danielebarchiesi@4 289 }
danielebarchiesi@4 290
danielebarchiesi@4 291 // If the path is not within the drupal root return original url, unchanged
danielebarchiesi@4 292 if (!$found) {
danielebarchiesi@4 293 return $matches[0];
danielebarchiesi@4 294 }
danielebarchiesi@4 295
danielebarchiesi@4 296 // Okay, format the URL.
danielebarchiesi@4 297 // If there's still a slash lingering at the start of the path, chop it off.
danielebarchiesi@4 298 $parts['path'] = ltrim($parts['path'],'/');
danielebarchiesi@4 299
danielebarchiesi@4 300 // Examine the query part of the URL. Break it up and look through it; if it
danielebarchiesi@4 301 // has a value for "q", we want to use that as our trimmed path, and remove it
danielebarchiesi@4 302 // from the array. If any of its values are empty strings (that will be the
danielebarchiesi@4 303 // case for "bar" if a string like "foo=3&bar&baz=4" is passed through
danielebarchiesi@4 304 // parse_str()), replace them with NULL so that url() (or, more
danielebarchiesi@4 305 // specifically, drupal_http_build_query()) can still handle it.
danielebarchiesi@4 306 if (isset($parts['query'])) {
danielebarchiesi@4 307 parse_str($parts['query'], $parts['qparts']);
danielebarchiesi@4 308 foreach ($parts['qparts'] as $key => $value) {
danielebarchiesi@4 309 if ($value === '') {
danielebarchiesi@4 310 $parts['qparts'][$key] = NULL;
danielebarchiesi@4 311 }
danielebarchiesi@4 312 elseif ($key === 'q') {
danielebarchiesi@4 313 $parts['path'] = $value;
danielebarchiesi@4 314 unset($parts['qparts']['q']);
danielebarchiesi@4 315 }
danielebarchiesi@4 316 }
danielebarchiesi@4 317 }
danielebarchiesi@4 318 else {
danielebarchiesi@4 319 $parts['qparts'] = NULL;
danielebarchiesi@4 320 }
danielebarchiesi@4 321
danielebarchiesi@4 322 // If we don't have a path yet, bail out.
danielebarchiesi@4 323 if (!isset($parts['path'])) {
danielebarchiesi@4 324 return $matches[0];
danielebarchiesi@4 325 }
danielebarchiesi@4 326
danielebarchiesi@4 327 // If we didn't previously identify this as a file, check to see if the file
danielebarchiesi@4 328 // exists now that we have the correct path relative to DRUPAL_ROOT
danielebarchiesi@4 329 if (!$settings['is_file']){
danielebarchiesi@4 330 $settings['is_file'] = !empty($parts['path']) && is_file(DRUPAL_ROOT . '/'. $parts['path']);
danielebarchiesi@4 331 }
danielebarchiesi@4 332
danielebarchiesi@4 333 // Okay, deal with language stuff.
danielebarchiesi@4 334 if ($settings['is_file']) {
danielebarchiesi@4 335 // If we're linking to a file, use a fake LANGUAGE_NONE language object.
danielebarchiesi@4 336 // Otherwise, the path may get prefixed with the "current" language prefix
danielebarchiesi@4 337 // (eg, /ja/misc/message-24-ok.png)
danielebarchiesi@4 338 $parts['language_obj'] = (object) array('language' => LANGUAGE_NONE, 'prefix' => '');
danielebarchiesi@4 339 }
danielebarchiesi@4 340 else {
danielebarchiesi@4 341 // Let's see if we can split off a language prefix from the path.
danielebarchiesi@4 342 if (module_exists('locale')) {
danielebarchiesi@4 343 // Sometimes this file will be require_once-d by the locale module before
danielebarchiesi@4 344 // this point, and sometimes not. We require_once it ourselves to be sure.
danielebarchiesi@4 345 require_once DRUPAL_ROOT . '/includes/language.inc';
danielebarchiesi@4 346 list($language_obj, $path) = language_url_split_prefix($parts['path'], language_list());
danielebarchiesi@4 347 if ($language_obj) {
danielebarchiesi@4 348 $parts['path'] = $path;
danielebarchiesi@4 349 $parts['language_obj'] = $language_obj;
danielebarchiesi@4 350 }
danielebarchiesi@4 351 }
danielebarchiesi@4 352 }
danielebarchiesi@4 353
danielebarchiesi@4 354 // If we get to this point and $parts['path'] is now an empty string (which
danielebarchiesi@4 355 // will be the case if the path was originally just "/"), then we
danielebarchiesi@4 356 // want to link to <front>.
danielebarchiesi@4 357 if ($parts['path'] === '') {
danielebarchiesi@4 358 $parts['path'] = '<front>';
danielebarchiesi@4 359 }
danielebarchiesi@4 360 // Build the parameters we will send to url()
danielebarchiesi@4 361 $url_params = array(
danielebarchiesi@4 362 'path' => $parts['path'],
danielebarchiesi@4 363 'options' => array(
danielebarchiesi@4 364 'query' => $parts['qparts'],
danielebarchiesi@4 365 'fragment' => isset($parts['fragment']) ? $parts['fragment'] : NULL,
danielebarchiesi@4 366 // Create an absolute URL if protocol_style is 'full' or 'proto-rel', but
danielebarchiesi@4 367 // not if it's 'path'.
danielebarchiesi@4 368 'absolute' => $settings['current_settings']['protocol_style'] !== 'path',
danielebarchiesi@4 369 // If we seem to have found a language for the path, pass it along to
danielebarchiesi@4 370 // url(). Otherwise, ignore the 'language' parameter.
danielebarchiesi@4 371 'language' => isset($parts['language_obj']) ? $parts['language_obj'] : NULL,
danielebarchiesi@4 372 // A special parameter not actually used by url(), but we use it to see if
danielebarchiesi@4 373 // an alter hook implementation wants us to just pass through the original
danielebarchiesi@4 374 // URL.
danielebarchiesi@4 375 'use_original' => FALSE,
danielebarchiesi@4 376 ),
danielebarchiesi@4 377 );
danielebarchiesi@4 378
danielebarchiesi@4 379 // Add the original URL to the parts array
danielebarchiesi@4 380 $parts['original'] = $original_url;
danielebarchiesi@4 381
danielebarchiesi@4 382 // Now alter!
danielebarchiesi@4 383 // @see http://drupal.org/node/1762022
danielebarchiesi@4 384 drupal_alter('pathologic', $url_params, $parts, $settings);
danielebarchiesi@4 385
danielebarchiesi@4 386 // If any of the alter hooks asked us to just pass along the original URL,
danielebarchiesi@4 387 // then do so.
danielebarchiesi@4 388 if ($url_params['options']['use_original']) {
danielebarchiesi@4 389 return $matches[0];
danielebarchiesi@4 390 }
danielebarchiesi@4 391
danielebarchiesi@4 392 // If the path is for a file and clean URLs are disabled, then the path that
danielebarchiesi@4 393 // url() will create will have a q= query fragment, which won't work for
danielebarchiesi@4 394 // files. To avoid that, we use this trick to temporarily turn clean URLs on.
danielebarchiesi@4 395 // This is horrible, but it seems to be the sanest way to do this.
danielebarchiesi@4 396 // @see http://drupal.org/node/1672430
danielebarchiesi@4 397 // @todo Submit core patch allowing clean URLs to be toggled by option sent
danielebarchiesi@4 398 // to url()?
danielebarchiesi@4 399 if (!empty($settings['is_file'])) {
danielebarchiesi@4 400 $settings['orig_clean_url'] = !empty($GLOBALS['conf']['clean_url']);
danielebarchiesi@4 401 if (!$settings['orig_clean_url']) {
danielebarchiesi@4 402 $GLOBALS['conf']['clean_url'] = TRUE;
danielebarchiesi@4 403 }
danielebarchiesi@4 404 }
danielebarchiesi@4 405
danielebarchiesi@4 406 // Now for the url() call. Drumroll, please…
danielebarchiesi@4 407 $url = url($url_params['path'], $url_params['options']);
danielebarchiesi@4 408
danielebarchiesi@4 409 // If we turned clean URLs on before to create a path to a file, turn them
danielebarchiesi@4 410 // back off.
danielebarchiesi@4 411 if ($settings['is_file'] && !$settings['orig_clean_url']) {
danielebarchiesi@4 412 $GLOBALS['conf']['clean_url'] = FALSE;
danielebarchiesi@4 413 }
danielebarchiesi@4 414
danielebarchiesi@4 415 // If we need to create a protocol-relative URL, then convert the absolute
danielebarchiesi@4 416 // URL we have now.
danielebarchiesi@4 417 if ($settings['current_settings']['protocol_style'] === 'proto-rel') {
danielebarchiesi@4 418 // Now, what might have happened here is that url() returned a URL which
danielebarchiesi@4 419 // isn't on "this" server due to a hook_url_outbound_alter() implementation.
danielebarchiesi@4 420 // We don't want to convert the URL in that case. So what we're going to
danielebarchiesi@4 421 // do is cycle through the local paths again and see if the host part of
danielebarchiesi@4 422 // $url matches with the host of one of those, and only alter in that case.
danielebarchiesi@4 423 $url_parts = parse_url($url);
danielebarchiesi@4 424 if (!empty($url_parts['host']) && $url_parts['host'] === $settings['current_settings']['base_url_host']) {
danielebarchiesi@4 425 $url = _pathologic_url_to_protocol_relative($url);
danielebarchiesi@4 426 }
danielebarchiesi@4 427 }
danielebarchiesi@4 428
danielebarchiesi@4 429 // Apply HTML character encoding, as is required for HTML attributes.
danielebarchiesi@4 430 // @see http://drupal.org/node/1672932
danielebarchiesi@4 431 $url = check_plain($url);
danielebarchiesi@4 432 // $matches[1] will be the tag attribute; src, href, etc.
danielebarchiesi@4 433 return "{$matches[1]}=\"{$url}";
danielebarchiesi@4 434 }
danielebarchiesi@4 435
danielebarchiesi@4 436 /**
danielebarchiesi@4 437 * Convert a full URL with a protocol to a protocol-relative URL.
danielebarchiesi@4 438 *
danielebarchiesi@4 439 * As the Drupal core url() function doesn't support protocol-relative URLs, we
danielebarchiesi@4 440 * work around it by just creating a full URL and then running it through this
danielebarchiesi@4 441 * to strip off the protocol.
danielebarchiesi@4 442 *
danielebarchiesi@4 443 * Though this is just a one-liner, it's placed in its own function so that it
danielebarchiesi@4 444 * can be called independently from our test code.
danielebarchiesi@4 445 */
danielebarchiesi@4 446 function _pathologic_url_to_protocol_relative($url) {
danielebarchiesi@4 447 return preg_replace('~^https?://~', '//', $url);
danielebarchiesi@4 448 }