--- pathologic.module Tue Mar 20 04:26:38 2012 +++ pathologic.module Wed Mar 21 09:45:14 2012 @@ -190,69 +190,76 @@ return $matches[0]; } - // First, "files:" support. This is fairly easy. + // We have a local path. We now have to transform this into a valid URL. + // How to do this depends on whether it is a path handled by Drupal (e.g. + // node/21) or a path to an existing resource (e.g. + // sites/default/files/my-image.jpg). Paths with a valid (file) scheme are + // handled first. For now only the files: scheme is handled. + $url = FALSE; if (isset($parts['scheme']) && $parts['scheme'] === 'files') { - // Can we create a file URL for it? - $file_url = file_create_url(file_build_uri($parts['path'])); - if ($file_url) { - return $matches[1] . '="' . $file_url; + // Create the URL for it. + $url = file_create_url(file_build_uri($parts['path'])); + } + else { + // No scheme or a scheme that will be ignored (http, https, internal) was + // used. Check if the path is an existing resource or not? But be aware of + // the query parameter q (probably in combination with index.php as path, + // but Drupal will also work with default.php?q=... if so configured). + if (isset($parts['query'])) { + parse_str($parts['query'], $parts['qparts']); + if (isset($parts['qparts']['q'])) { + $parts['path'] = $parts['qparts']['q']; + unset($parts['qparts']['q']); + } } else { - // That didn't work. Bail out. + $parts['qparts'] = NULL; + } + + // If we don't have a path yet, bail out. + if (!isset($parts['path'])) { return $matches[0]; } - } - elseif (file_exists(realpath($parts['path']))) { - // Okay, they didn't use files:, but does it appear that the path is still - // to a real file on the disk? If so, we don't want to adjust for language. - // Also, skip all other tweaks. - $settings['langcode'] = LANGUAGE_NONE; - } + // Chop off any starting slash (i.e if we had something like ?q=/...). + if (strpos($parts['path'], '/') === 0) { + $parts['path'] = substr($parts['path'], 1); + } - // Examine the query part of the URL. Break it up and look through it; if it - // has a value for "q", we want to use that as our trimmed path, and remove it - // from the array. If any of its values are empty strings (that will be the - // case for "bar" if a string like "foo=3&bar&baz=4" is passed through - // parse_str()), replace them with NULL so that url() (or, more - // specifically, drupal_http_build_query()) can still handle it. - if (isset($parts['query'])) { - parse_str($parts['query'], $parts['qparts']); - foreach ($parts['qparts'] as $key => $value) { - if ($value === '') { - $parts['qparts'][$key] = NULL; + if (file_exists(realpath($parts['path']))) { + // Note: this will loose any query and fragment part. + $url = file_create_url($parts['path']); + // So we are adding that back, if necessary. + if (isset($parts['qparts'])) { + $url .= '?' . drupal_http_build_query($parts['qparts']); } - elseif ($key === 'q') { - $parts['path'] = $value; - unset($parts['qparts']['q']); + if (isset($parts['fragment']) && $parts['fragment'] !== '') { + $url .= '#' . $parts['fragment']; } } + else { + // Assemble the URL options to pass with the path. + // Start with the absolute setting. + $url_options = array( + 'absolute' => $settings['current_settings']['absolute'], + ); + // Add query part if not empty. + if (!empty($parts['qparts'])) { + $url_options['query'] = $parts['qparts']; + } + // Add fragment part if not empty. + if (!empty($parts['fragment'])) { + $url_options['fragment'] = $parts['fragment']; + } + // Add language of current context as Drupal may rewrite URL's based on + // language. + if (!empty($settings['langcode']) && $settings['langcode'] !== LANGUAGE_NONE) { + if (isset($settings['language_list'][$settings['langcode']])) { + $url_options['language'] = isset($settings['language_list'][$settings['langcode']]); + } + } + $url = url($parts['path'], $url_options); + } } - else { - $parts['qparts'] = NULL; - } - - // If we don't have a path yet, bail out. - if (!isset($parts['path'])) { - return $matches[0]; - } - - // Okay, format the URL. - // If there's still a slash lingering at the start of the path, chop it off. - // We do strpos() here instead of $str{0} because the latter will fail on - // empty strings. - if (strpos($parts['path'], '/') === 0) { - $parts['path'] = substr($parts['path'], 1); - } - - $url = url( - $parts['path'] === '' ? '' : $parts['path'], - array( - 'query' => $parts['qparts'], - 'fragment' => isset($parts['fragment']) ? $parts['fragment'] : NULL, - 'absolute' => $settings['current_settings']['absolute'], - 'language' => isset($settings['language_list'][$settings['langcode']]) ? $settings['language_list'][$settings['langcode']] : $settings['language_list'][LANGUAGE_NONE], - ) - ); // $matches[1] will be the tag attribute; src, href, etc. - return "{$matches[1]}=\"{$url}"; + return is_string($url) ? "{$matches[1]}=\"{$url}" : $matches[0]; }