From: Jeff Veit Date: Fri, 23 Jun 2017 00:40:13 +0000 (+0100) Subject: Pathologic was missing because of a .git folder inside. X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=commitdiff_plain;h=2257eb96fa3afedcfba62207e838f49ee9c757e2 Pathologic was missing because of a .git folder inside. --- diff --git a/web/modules/contrib/pathologic b/web/modules/contrib/pathologic deleted file mode 160000 index 4f9f3fdcf..000000000 --- a/web/modules/contrib/pathologic +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 4f9f3fdcf1e0b224c4d8650e383a769f40abf9bf diff --git a/web/modules/contrib/pathologic/README.txt b/web/modules/contrib/pathologic/README.txt new file mode 100644 index 000000000..3b2a5ba4c --- /dev/null +++ b/web/modules/contrib/pathologic/README.txt @@ -0,0 +1,21 @@ +Pathologic +---------- + +Project Page: +http://drupal.org/project/pathologic + +By Garrett Albright +http://drupal.org/user/191212 + +Originally sponsored by Precision Intermedia +http://www.precisionintermedia.com/ + +Thanks to all who have used this module over the years and provided bug reports +and suggestions via email and the issue queue! I love you all. + +Installation & Configuration +---------------------------- + +For full installation and configuration instructions, please see this page in +the Drupal online manual: +http://drupal.org/node/257026 diff --git a/web/modules/contrib/pathologic/config/install/pathologic.settings.yml b/web/modules/contrib/pathologic/config/install/pathologic.settings.yml new file mode 100644 index 000000000..b3869f0f7 --- /dev/null +++ b/web/modules/contrib/pathologic/config/install/pathologic.settings.yml @@ -0,0 +1,3 @@ +scheme_whitelist: ['http', 'https', 'files', 'internal'] +protocol_style: 'full' +local_paths: '' diff --git a/web/modules/contrib/pathologic/config/schema/pathologic.schema.yml b/web/modules/contrib/pathologic/config/schema/pathologic.schema.yml new file mode 100644 index 000000000..f62b3fd15 --- /dev/null +++ b/web/modules/contrib/pathologic/config/schema/pathologic.schema.yml @@ -0,0 +1,33 @@ +pathologic.settings: + type: config_object + mapping: + scheme_whitelist: + type: sequence + label: 'Scheme whitelist' + sequence: + - type: string + label: 'Scheme' + protocol_style: + type: string + label: 'Protocol style' + local_paths: + type: string + label: 'Also considered local' + +filter_settings.filter_pathologic: + type: filter + label: 'Pathologic' + mapping: + settings_source: + type: string + label: 'Settings source' + local_settings: + type: mapping + label: 'Local settings' + mapping: + protocol_style: + type: string + label: 'Protocol style' + local_paths: + type: string + label: 'Also considered local' diff --git a/web/modules/contrib/pathologic/pathologic.api.php b/web/modules/contrib/pathologic/pathologic.api.php new file mode 100644 index 000000000..f75dd523c --- /dev/null +++ b/web/modules/contrib/pathologic/pathologic.api.php @@ -0,0 +1,83 @@ +fields('dup') + ->condition('module', 'pathologic') + ->execute(); + while ($instance = $rez->fetchObject()) { + // Load the format + if ($format = filter_format_load($instance->format)) { + // Load filters. + $format->filters = []; + // Add the filters + foreach (filter_list_format($instance->format) as $filter_name => $filter) { + $format->filters[$filter_name] = (array)$filter; + } + // Add Pathologic + $format->filters['pathologic'] = [ + 'weight' => $instance->weight, + 'status' => 1, + 'settings' => [ + 'absolute' => variable_get('filter_pathologic_absolute_' . $instance->format, TRUE), + 'local_paths' => variable_get('filter_pathologic_local_paths_' . $instance->format, ''), + ], + ]; + // Save the format + filter_format_save($format); + // Unset old variables + variable_del('filter_pathologic_absolute_' . $instance->format); + variable_del('filter_pathologic_local_paths_' . $instance->format); + } + } + // Delete Pathologic data from {d6_upgrade_filter}…? + // No, maybe we don't want to actually do that…? + } +} + +/** + * Convert obsolete "absolute" setting to modern "protocol_style" setting for + * each filter instance. + */ +function pathologic_update_7200(&$sandbox) { + foreach (filter_formats() as $format) { + // @see http://drupal.org/node/1304930 + if (empty($format->filters)) { + $format->filters = []; + // Add the filters + foreach (filter_list_format($format->format) as $filter_name => $filter) { + $format->filters[$filter_name] = (array)$filter; + } + } + if (isset($format->filters['pathologic'])) { + $format->filters['pathologic']['settings']['protocol_style'] = $format->filters['pathologic']['settings']['absolute'] ? 'full' : 'path'; + unset($format->filters['pathologic']['settings']['absolute']); + filter_format_save($format); + } + } +} diff --git a/web/modules/contrib/pathologic/pathologic.links.menu.yml b/web/modules/contrib/pathologic/pathologic.links.menu.yml new file mode 100644 index 000000000..17e8a00ed --- /dev/null +++ b/web/modules/contrib/pathologic/pathologic.links.menu.yml @@ -0,0 +1,5 @@ +pathologic.config_form: + title: 'Pathologic configuration' + parent: system.admin_config_content + description: 'Configure how Pathologic adjusts link and image paths in content.' + route_name: pathologic.config_form diff --git a/web/modules/contrib/pathologic/pathologic.module b/web/modules/contrib/pathologic/pathologic.module new file mode 100644 index 000000000..8f77b03b8 --- /dev/null +++ b/web/modules/contrib/pathologic/pathologic.module @@ -0,0 +1,404 @@ + $base_url_parts['path']]; + $settings['local_paths_exploded'][] = ['path' => $base_url_parts['path'], 'host' => $base_url_parts['host']]; + // We'll also just store the host part separately for easy access. + $settings['base_url_host'] = $base_url_parts['host']; + + $cached_settings[$hash] = $settings; + } + // Take note of which settings in the settings array should apply. + $cached_settings['current_settings'] = &$cached_settings[$hash]; + + // Now that we have all of our settings prepared, attempt to process all + // paths in href, src, action or longdesc HTML attributes. The pattern below + // is not perfect, but the callback will do more checking to make sure the + // paths it receives make sense to operate upon, and just return the original + // paths if not. + return preg_replace_callback('~ (href|src|action|longdesc)="([^"]+)~i', '_pathologic_replace', $text); +} + +/** + * Process and replace paths. preg_replace_callback() callback. + */ +function _pathologic_replace($matches) { + // Get the base path. + global $base_path; + + // Get the settings for the filter. Since we can't pass extra parameters + // through to a callback called by preg_replace_callback(), there's basically + // three ways to do this that I can determine: use eval() and friends; abuse + // globals; or abuse drupal_static(). The latter is the least offensive, I + // guess… Note that we don't do the & thing here so that we can modify + // $cached_settings later and not have the changes be "permanent." + $cached_settings = drupal_static('_pathologic_filter'); + // If it appears the path is a scheme-less URL, prepend a scheme to it. + // parse_url() cannot properly parse scheme-less URLs. Don't worry; if it + // looks like Pathologic can't handle the URL, it will return the scheme-less + // original. + // @see https://drupal.org/node/1617944 + // @see https://drupal.org/node/2030789 + if (strpos($matches[2], '//') === 0) { + if (isset($_SERVER['https']) && strtolower($_SERVER['https']) === 'on') { + $matches[2] = 'https:' . $matches[2]; + } + else { + $matches[2] = 'http:' . $matches[2]; + } + } + // Now parse the URL after reverting HTML character encoding. + // @see http://drupal.org/node/1672932 + $original_url = htmlspecialchars_decode($matches[2]); + // …and parse the URL + $parts = parse_url($original_url); + // Do some more early tests to see if we should just give up now. + if ( + // If parse_url() failed, $parts = FALSE. If the href was just "#", $parts + // is an empty array. Give up in both cases. + empty($parts) + || ( + // If there's a scheme part and it doesn't look useful, bail out. + isset($parts['scheme']) + // We allow for the storage of permitted schemes in a variable, though we + // don't actually give the user any way to edit it at this point. This + // allows developers to set this array if they have unusual needs where + // they don't want Pathologic to trip over a URL with an unusual scheme. + // @see http://drupal.org/node/1834308 + // Default value is ['http', 'https', 'files', 'internal'] + // "files" and "internal" are for Path Filter compatibility. + && !in_array($parts['scheme'], \Drupal::config('pathologic.settings')->get('scheme_whitelist')) + ) + // Bail out if it looks like there's only a fragment part. + || (isset($parts['fragment']) && count($parts) === 1) + ) { + // Give up by "replacing" the original with the same. + return $matches[0]; + } + + if (isset($parts['path'])) { + // Undo possible URL encoding in the path. + // @see http://drupal.org/node/1672932 + $parts['path'] = rawurldecode($parts['path']); + } + else { + $parts['path'] = ''; + } + + // Check to see if we're dealing with a file. + // @todo Should we still try to do path correction on these files too? + if (isset($parts['scheme']) && $parts['scheme'] === 'files') { + // Path Filter "files:" support. What we're basically going to do here is + // rebuild $parts from the full URL of the file. + $new_parts = parse_url(file_create_url(file_default_scheme() . '://' . $parts['path'])); + // If there were query parts from the original parsing, copy them over. + if (!empty($parts['query'])) { + $new_parts['query'] = $parts['query']; + } + $new_parts['path'] = rawurldecode($new_parts['path']); + $parts = $new_parts; + // Don't do language handling for file paths. + $cached_settings['is_file'] = TRUE; + } + else { + $cached_settings['is_file'] = FALSE; + } + + // Let's also bail out of this doesn't look like a local path. + $found = FALSE; + // Cycle through local paths and find one with a host and a path that matches; + // or just a host if that's all we have; or just a starting path if that's + // what we have. + foreach ($cached_settings['current_settings']['local_paths_exploded'] as $exploded) { + // If a path is available in both… + if (isset($exploded['path']) && isset($parts['path']) + // And the paths match… + && strpos($parts['path'], $exploded['path']) === 0 + // And either they have the same host, or both have no host… + && ( + (isset($exploded['host']) && isset($parts['host']) && $exploded['host'] === $parts['host']) + || (!isset($exploded['host']) && !isset($parts['host'])) + ) + ) { + // Remove the shared path from the path. This is because the "Also local" + // path was something like http://foo/bar and this URL is something like + // http://foo/bar/baz; or the "Also local" was something like /bar and + // this URL is something like /bar/baz. And we only care about the /baz + // part. + $parts['path'] = Unicode::substr($parts['path'], Unicode::strlen($exploded['path'])); + $found = TRUE; + // Break out of the foreach loop + break; + } + // Okay, we didn't match on path alone, or host and path together. Can we + // match on just host? Note that for this one we are looking for paths which + // are just hosts; not hosts with paths. + elseif ((isset($parts['host']) && !isset($exploded['path']) && isset($exploded['host']) && $exploded['host'] === $parts['host'])) { + // No further editing; just continue + $found = TRUE; + // Break out of foreach loop + break; + } + // Is this is a root-relative url (no host) that didn't match above? + // Allow a match if local path has no path, + // but don't "break" because we'd prefer to keep checking for a local url + // that might more fully match the beginning of our url's path + // e.g.: if our url is /foo/bar we'll mark this as a match for + // http://example.com but want to keep searching and would prefer a match + // to http://example.com/foo if that's configured as a local path + elseif (!isset($parts['host']) && (!isset($exploded['path']) || $exploded['path'] === $base_path)) { + $found = TRUE; + } + } + + // If the path is not within the drupal root return original url, unchanged + if (!$found) { + return $matches[0]; + } + + // Okay, format the URL. + // If there's still a slash lingering at the start of the path, chop it off. + $parts['path'] = ltrim($parts['path'], '/'); + + // 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; + } + elseif ($key === 'q') { + $parts['path'] = $value; + unset($parts['qparts']['q']); + } + } + } + else { + $parts['qparts'] = NULL; + } + + // If we don't have a path yet, bail out. + if (!isset($parts['path'])) { + return $matches[0]; + } + + // If this looks like a D8-style unclean URL, crop off the "index.php/" from + // the beginning. + if (strpos($parts['path'], 'index.php/') === 0) { + $parts['path'] = substr($parts['path'], 10); + } + + // If we didn't previously identify this as a file, check to see if the file + // exists now that we have the correct path relative to DRUPAL_ROOT + if (!$cached_settings['is_file']) { + $cached_settings['is_file'] = !empty($parts['path']) && is_file(DRUPAL_ROOT . '/' . $parts['path']); + } + + // Okay, deal with language stuff. + // Let's see if we can split off a language prefix from the path. + if (\Drupal::moduleHandler()->moduleExists('language')) { + // This logic is based on + // \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl::getLangcode(). + $languages = \Drupal::languageManager()->getLanguages(); + $config = \Drupal::config('language.negotiation')->get('url'); + + $request_path = urldecode(trim($parts['path'], '/')); + $path_args = explode('/', $request_path); + $prefix = array_shift($path_args); + + // Search for prefix within added languages. + foreach ($languages as $language) { + if (isset($config['prefixes'][$language->getId()]) && $config['prefixes'][$language->getId()] == $prefix) { + $parts['path'] = implode('/', $path_args); + $parts['language_obj'] = $language; + break; + } + } + } + + // If we get to this point and $parts['path'] is now an empty string (which + // will be the case if the path was originally just "/"), then we + // want to link to . + if ($parts['path'] === '') { + $parts['path'] = ''; + } + // Build the parameters we will send to url() + $url_params = [ + 'path' => $parts['path'], + 'options' => [ + 'query' => $parts['qparts'], + 'fragment' => isset($parts['fragment']) ? $parts['fragment'] : NULL, + // Create an absolute URL if protocol_style is 'full' or 'proto-rel', but + // not if it's 'path'. + 'absolute' => $cached_settings['current_settings']['protocol_style'] !== 'path', + // If we seem to have found a language for the path, pass it along to + // url(). Otherwise, ignore the 'language' parameter. + 'language' => isset($parts['language_obj']) ? $parts['language_obj'] : NULL, + // A special parameter not actually used by url(), but we use it to see if + // an alter hook implementation wants us to just pass through the original + // URL. + 'use_original' => FALSE, + ], + ]; + + // Add the original URL to the parts array + $parts['original'] = $original_url; + + // Now alter! + // @see http://drupal.org/node/1762022 + \Drupal::moduleHandler()->alter('pathologic', $url_params, $parts, $cached_settings); + + // If any of the alter hooks asked us to just pass along the original URL, + // then do so. + if ($url_params['options']['use_original']) { + return $matches[0]; + } + + // Now to build the URL. Drumroll, please… + if ($parts['path'] == '') { + $url = Url::fromRoute('', [], $url_params['options'])->toString(); + } + else { + try { + $url = Url::fromUri('base://' . $url_params['path'], $url_params['options']) + ->toString(); + } + catch (\Exception $e) { + // In case of an error, e.g. completely invalid URL, return it unchanged. + return $matches[0]; + } + } + + // If we need to create a protocol-relative URL, then convert the absolute + // URL we have now. + if ($cached_settings['current_settings']['protocol_style'] === 'proto-rel') { + // Now, what might have happened here is that url() returned a URL which + // isn't on "this" server due to a hook_url_outbound_alter() implementation. + // We don't want to convert the URL in that case. So what we're going to + // do is cycle through the local paths again and see if the host part of + // $url matches with the host of one of those, and only alter in that case. + $url_parts = parse_url($url); + if (!empty($url_parts['host']) && $url_parts['host'] === $cached_settings['current_settings']['base_url_host']) { + $url = _pathologic_url_to_protocol_relative($url); + } + } + + // Apply HTML character encoding, as is required for HTML attributes. + // @see http://drupal.org/node/1672932 + $url = Html::escape($url); + // $matches[1] will be the tag attribute; src, href, etc. + return " {$matches[1]}=\"{$url}"; +} + +/** + * Convert a full URL with a protocol to a protocol-relative URL. + * + * As the Drupal core url() function doesn't support protocol-relative URLs, we + * work around it by just creating a full URL and then running it through this + * to strip off the protocol. + * + * Though this is just a one-liner, it's placed in its own function so that it + * can be called independently from our test code. + */ +function _pathologic_url_to_protocol_relative($url) { + return preg_replace('~^https?://~', '//', $url); +} diff --git a/web/modules/contrib/pathologic/pathologic.module.orig b/web/modules/contrib/pathologic/pathologic.module.orig new file mode 100644 index 000000000..968e1b1de --- /dev/null +++ b/web/modules/contrib/pathologic/pathologic.module.orig @@ -0,0 +1,404 @@ + $base_url_parts['path']]; + $settings['local_paths_exploded'][] = ['path' => $base_url_parts['path'], 'host' => $base_url_parts['host']]; + // We'll also just store the host part separately for easy access. + $settings['base_url_host'] = $base_url_parts['host']; + + $cached_settings[$hash] = $settings; + } + // Take note of which settings in the settings array should apply. + $cached_settings['current_settings'] = &$cached_settings[$hash]; + + // Now that we have all of our settings prepared, attempt to process all + // paths in href, src, action or longdesc HTML attributes. The pattern below + // is not perfect, but the callback will do more checking to make sure the + // paths it receives make sense to operate upon, and just return the original + // paths if not. + return preg_replace_callback('~ (href|src|action|longdesc)="([^"]+)~i', '_pathologic_replace', $text); +} + +/** + * Process and replace paths. preg_replace_callback() callback. + */ +function _pathologic_replace($matches) { + // Get the base path. + global $base_path; + + // Get the settings for the filter. Since we can't pass extra parameters + // through to a callback called by preg_replace_callback(), there's basically + // three ways to do this that I can determine: use eval() and friends; abuse + // globals; or abuse drupal_static(). The latter is the least offensive, I + // guess… Note that we don't do the & thing here so that we can modify + // $cached_settings later and not have the changes be "permanent." + $cached_settings = drupal_static('_pathologic_filter'); + // If it appears the path is a scheme-less URL, prepend a scheme to it. + // parse_url() cannot properly parse scheme-less URLs. Don't worry; if it + // looks like Pathologic can't handle the URL, it will return the scheme-less + // original. + // @see https://drupal.org/node/1617944 + // @see https://drupal.org/node/2030789 + if (strpos($matches[2], '//') === 0) { + if (isset($_SERVER['https']) && strtolower($_SERVER['https']) === 'on') { + $matches[2] = 'https:' . $matches[2]; + } + else { + $matches[2] = 'http:' . $matches[2]; + } + } + // Now parse the URL after reverting HTML character encoding. + // @see http://drupal.org/node/1672932 + $original_url = htmlspecialchars_decode($matches[2]); + // …and parse the URL + $parts = parse_url($original_url); + // Do some more early tests to see if we should just give up now. + if ( + // If parse_url() failed, $parts = FALSE. If the href was just "#", $parts + // is an empty array. Give up in both cases. + empty($parts) + || ( + // If there's a scheme part and it doesn't look useful, bail out. + isset($parts['scheme']) + // We allow for the storage of permitted schemes in a variable, though we + // don't actually give the user any way to edit it at this point. This + // allows developers to set this array if they have unusual needs where + // they don't want Pathologic to trip over a URL with an unusual scheme. + // @see http://drupal.org/node/1834308 + // Default value is ['http', 'https', 'files', 'internal'] + // "files" and "internal" are for Path Filter compatibility. + && !in_array($parts['scheme'], \Drupal::config('pathologic.settings')->get('scheme_whitelist')) + ) + // Bail out if it looks like there's only a fragment part. + || (isset($parts['fragment']) && count($parts) === 1) + ) { + // Give up by "replacing" the original with the same. + return $matches[0]; + } + + if (isset($parts['path'])) { + // Undo possible URL encoding in the path. + // @see http://drupal.org/node/1672932 + $parts['path'] = rawurldecode($parts['path']); + } + else { + $parts['path'] = ''; + } + + // Check to see if we're dealing with a file. + // @todo Should we still try to do path correction on these files too? + if (isset($parts['scheme']) && $parts['scheme'] === 'files') { + // Path Filter "files:" support. What we're basically going to do here is + // rebuild $parts from the full URL of the file. + $new_parts = parse_url(file_create_url(file_default_scheme() . '://' . $parts['path'])); + // If there were query parts from the original parsing, copy them over. + if (!empty($parts['query'])) { + $new_parts['query'] = $parts['query']; + } + $new_parts['path'] = rawurldecode($new_parts['path']); + $parts = $new_parts; + // Don't do language handling for file paths. + $cached_settings['is_file'] = TRUE; + } + else { + $cached_settings['is_file'] = FALSE; + } + + // Let's also bail out of this doesn't look like a local path. + $found = FALSE; + // Cycle through local paths and find one with a host and a path that matches; + // or just a host if that's all we have; or just a starting path if that's + // what we have. + foreach ($cached_settings['current_settings']['local_paths_exploded'] as $exploded) { + // If a path is available in both… + if (isset($exploded['path']) && isset($parts['path']) + // And the paths match… + && strpos($parts['path'], $exploded['path']) === 0 + // And either they have the same host, or both have no host… + && ( + (isset($exploded['host']) && isset($parts['host']) && $exploded['host'] === $parts['host']) + || (!isset($exploded['host']) && !isset($parts['host'])) + ) + ) { + // Remove the shared path from the path. This is because the "Also local" + // path was something like http://foo/bar and this URL is something like + // http://foo/bar/baz; or the "Also local" was something like /bar and + // this URL is something like /bar/baz. And we only care about the /baz + // part. + $parts['path'] = Unicode::substr($parts['path'], Unicode::strlen($exploded['path'])); + $found = TRUE; + // Break out of the foreach loop + break; + } + // Okay, we didn't match on path alone, or host and path together. Can we + // match on just host? Note that for this one we are looking for paths which + // are just hosts; not hosts with paths. + elseif ((isset($parts['host']) && !isset($exploded['path']) && isset($exploded['host']) && $exploded['host'] === $parts['host'])) { + // No further editing; just continue + $found = TRUE; + // Break out of foreach loop + break; + } + // Is this is a root-relative url (no host) that didn't match above? + // Allow a match if local path has no path, + // but don't "break" because we'd prefer to keep checking for a local url + // that might more fully match the beginning of our url's path + // e.g.: if our url is /foo/bar we'll mark this as a match for + // http://example.com but want to keep searching and would prefer a match + // to http://example.com/foo if that's configured as a local path + elseif (!isset($parts['host']) && (!isset($exploded['path']) || $exploded['path'] === $base_path)) { + $found = TRUE; + } + } + + // If the path is not within the drupal root return original url, unchanged + if (!$found) { + return $matches[0]; + } + + // Okay, format the URL. + // If there's still a slash lingering at the start of the path, chop it off. + $parts['path'] = ltrim($parts['path'], '/'); + + // 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; + } + elseif ($key === 'q') { + $parts['path'] = $value; + unset($parts['qparts']['q']); + } + } + } + else { + $parts['qparts'] = NULL; + } + + // If we don't have a path yet, bail out. + if (!isset($parts['path'])) { + return $matches[0]; + } + + // If this looks like a D8-style unclean URL, crop off the "index.php/" from + // the beginning. + if (strpos($parts['path'], 'index.php/') === 0) { + $parts['path'] = substr($parts['path'], 10); + } + + // If we didn't previously identify this as a file, check to see if the file + // exists now that we have the correct path relative to DRUPAL_ROOT + if (!$cached_settings['is_file']) { + $cached_settings['is_file'] = !empty($parts['path']) && is_file(DRUPAL_ROOT . '/' . $parts['path']); + } + + // Okay, deal with language stuff. + // Let's see if we can split off a language prefix from the path. + if (\Drupal::moduleHandler()->moduleExists('language')) { + // This logic is based on + // \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl::getLangcode(). + $languages = \Drupal::languageManager()->getLanguages(); + $config = \Drupal::config('language.negotiation')->get('url'); + + $request_path = urldecode(trim($parts['path'], '/')); + $path_args = explode('/', $request_path); + $prefix = array_shift($path_args); + + // Search for prefix within added languages. + foreach ($languages as $language) { + if (isset($config['prefixes'][$language->getId()]) && $config['prefixes'][$language->getId()] == $prefix) { + $parts['path'] = implode('/', $path_args); + $parts['language_obj'] = $language; + break; + } + } + } + + // If we get to this point and $parts['path'] is now an empty string (which + // will be the case if the path was originally just "/"), then we + // want to link to . + if ($parts['path'] === '') { + $parts['path'] = ''; + } + // Build the parameters we will send to url() + $url_params = [ + 'path' => $parts['path'], + 'options' => [ + 'query' => $parts['qparts'], + 'fragment' => isset($parts['fragment']) ? $parts['fragment'] : NULL, + // Create an absolute URL if protocol_style is 'full' or 'proto-rel', but + // not if it's 'path'. + 'absolute' => $cached_settings['current_settings']['protocol_style'] !== 'path', + // If we seem to have found a language for the path, pass it along to + // url(). Otherwise, ignore the 'language' parameter. + 'language' => isset($parts['language_obj']) ? $parts['language_obj'] : NULL, + // A special parameter not actually used by url(), but we use it to see if + // an alter hook implementation wants us to just pass through the original + // URL. + 'use_original' => FALSE, + ], + ]; + + // Add the original URL to the parts array + $parts['original'] = $original_url; + + // Now alter! + // @see http://drupal.org/node/1762022 + \Drupal::moduleHandler()->alter('pathologic', $url_params, $parts, $cached_settings); + + // If any of the alter hooks asked us to just pass along the original URL, + // then do so. + if ($url_params['options']['use_original']) { + return $matches[0]; + } + + // Now to build the URL. Drumroll, please… + if ($parts['path'] == '') { + $url = Url::fromRoute('', [], $url_params['options'])->toString(); + } + else { + try { + $url = Url::fromUri('base://' . $url_params['path'], $url_params['options']) + ->toString(); + } + catch (\Exception $e) { + // In case of an error, e.g. completely invalid URL, return it unchanged. + return $matches[0]; + } + } + + // If we need to create a protocol-relative URL, then convert the absolute + // URL we have now. + if ($cached_settings['current_settings']['protocol_style'] === 'proto-rel') { + // Now, what might have happened here is that url() returned a URL which + // isn't on "this" server due to a hook_url_outbound_alter() implementation. + // We don't want to convert the URL in that case. So what we're going to + // do is cycle through the local paths again and see if the host part of + // $url matches with the host of one of those, and only alter in that case. + $url_parts = parse_url($url); + if (!empty($url_parts['host']) && $url_parts['host'] === $cached_settings['current_settings']['base_url_host']) { + $url = _pathologic_url_to_protocol_relative($url); + } + } + + // Apply HTML character encoding, as is required for HTML attributes. + // @see http://drupal.org/node/1672932 + $url = SafeMarkup::checkPlain($url); + // $matches[1] will be the tag attribute; src, href, etc. + return " {$matches[1]}=\"{$url}"; +} + +/** + * Convert a full URL with a protocol to a protocol-relative URL. + * + * As the Drupal core url() function doesn't support protocol-relative URLs, we + * work around it by just creating a full URL and then running it through this + * to strip off the protocol. + * + * Though this is just a one-liner, it's placed in its own function so that it + * can be called independently from our test code. + */ +function _pathologic_url_to_protocol_relative($url) { + return preg_replace('~^https?://~', '//', $url); +} diff --git a/web/modules/contrib/pathologic/pathologic.routing.yml b/web/modules/contrib/pathologic/pathologic.routing.yml new file mode 100644 index 000000000..7339165a3 --- /dev/null +++ b/web/modules/contrib/pathologic/pathologic.routing.yml @@ -0,0 +1,7 @@ +pathologic.config_form: + path: 'admin/config/content/pathologic' + defaults: + _form: '\Drupal\pathologic\PathologicSettingsForm' + _title: 'Pathologic configuration' + requirements: + _permission: 'administer filters' diff --git a/web/modules/contrib/pathologic/pathologic_test/pathologic_test.info.yml b/web/modules/contrib/pathologic/pathologic_test/pathologic_test.info.yml new file mode 100755 index 000000000..11ef1a09d --- /dev/null +++ b/web/modules/contrib/pathologic/pathologic_test/pathologic_test.info.yml @@ -0,0 +1,9 @@ +name: Pathologic Test +description: Module which implements hook_pathologic_alter() for testing that functionality. +core: 8.x +package: Testing +dependencies: +- filter +- pathologic +type: module +hidden: true diff --git a/web/modules/contrib/pathologic/pathologic_test/pathologic_test.module b/web/modules/contrib/pathologic/pathologic_test/pathologic_test.module new file mode 100644 index 000000000..32b539726 --- /dev/null +++ b/web/modules/contrib/pathologic/pathologic_test/pathologic_test.module @@ -0,0 +1,19 @@ + [ + '#type' => 'radios', + '#title' => $this->t('Processed URL format'), + '#default_value' => $defaults['protocol_style'], + '#options' => [ + 'full' => $this->t('Full URL (http://example.com/foo/bar)'), + 'proto-rel' => $this->t('Protocol relative URL (//example.com/foo/bar)'), + 'path' => $this->t('Path relative to server root (/foo/bar)'), + ], + '#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.'), + '#weight' => 10, + ], + 'local_paths' => [ + '#type' => 'textarea', + '#title' => $this->t('All base paths for this site'), + '#default_value' => $defaults['local_paths'], + '#description' => $this->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.', [':docs' => 'https://www.drupal.org/node/257026']), + '#weight' => 20, + ], + ]; + } + +} diff --git a/web/modules/contrib/pathologic/src/PathologicSettingsForm.php b/web/modules/contrib/pathologic/src/PathologicSettingsForm.php new file mode 100644 index 000000000..a8fb169fc --- /dev/null +++ b/web/modules/contrib/pathologic/src/PathologicSettingsForm.php @@ -0,0 +1,61 @@ +config('pathologic.settings'); + + $form['reminder'] = [ + '#type' => 'markup', + '#markup' => '

' . $this->t('Reminder: The settings on this form only affect text formats for which Pathologic is configured to use the global Pathologic settings; if it’s configured to use per-format settings, these settings will have no effect.') . '

', + '#weight' => 0, + ]; + $defaults = [ + 'protocol_style' => $config->get('protocol_style'), + 'local_paths' => $config->get('local_paths'), + ]; + + $common = new PathologicSettingsCommon(); + $form += $common->commonSettingsForm($defaults); + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->config('pathologic.settings') + ->set('protocol_style', $form_state->getValue('protocol_style')) + ->set('local_paths', $form_state->getValue('local_paths')) + ->save(); + + parent::submitForm($form, $form_state); + } + + /** + * @inheritdoc + */ + protected function getEditableConfigNames() { + return [ + 'pathologic.settings', + ]; + } + +} diff --git a/web/modules/contrib/pathologic/src/Plugin/Filter/FilterPathologic.php b/web/modules/contrib/pathologic/src/Plugin/Filter/FilterPathologic.php new file mode 100644 index 000000000..50b716262 --- /dev/null +++ b/web/modules/contrib/pathologic/src/Plugin/Filter/FilterPathologic.php @@ -0,0 +1,96 @@ + 'markup', + '#markup' => $this->t('In most cases, Pathologic should be the last filter in the “Filter processing order” list.'), + '#weight' => 0, + ]; + $form['settings_source'] = [ + '#type' => 'radios', + '#title' => $this->t('Settings source'), + '#description' => $this->t('Select whether Pathologic should use the global Pathologic settings or custom “local” settings when filtering text in this text format.', [':config' => Url::fromRoute('pathologic.config_form')->toString()]), + '#weight' => 10, + '#default_value' => $this->settings['settings_source'], + '#options' => [ + 'global' => $this->t('Use global Pathologic settings'), + 'local' => $this->t('Use custom settings for this text format'), + ], + ]; + // Fields in fieldsets are… awkward to implement. + // @see https://www.drupal.org/node/2378437 + $form['local_settings'] = [ + '#type' => 'fieldset', + '#title' => $this->t('Custom settings for this text format'), + '#weight' => 20, + '#collapsible' => FALSE, + '#collapsed' => FALSE, + '#description' => $this->t('These settings are ignored if “Use global Pathologic settings” is selected above.'), + // @todo Fix the #states magic (or see if it's a core D8 bug) + '#states' => [ + 'visible' => [ + ':input[name="filters[filter_pathologic][settings][settings_source]"]' => ['value' => 'local'], + ], + ], + ]; + + $common = new PathologicSettingsCommon(); + $form['local_settings'] += $common->commonSettingsForm($this->settings['local_settings']); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function process($text, $langcode) { + $settings = $this->settings; + if ($settings['settings_source'] === 'global') { + $config = \Drupal::config('pathologic.settings'); + $settings['protocol_style'] = $config->get('protocol_style'); + $settings['local_paths'] = $config->get('local_paths'); + } + else { + $settings = $settings['local_settings']; + } + // @todo Move code from .module file to inside here. + return new FilterProcessResult(_pathologic_filter($text, $settings, Crypt::hashBase64(serialize($settings)))); + } + +} diff --git a/web/modules/contrib/pathologic/src/Tests/PathologicTest.php b/web/modules/contrib/pathologic/src/Tests/PathologicTest.php new file mode 100644 index 000000000..519def8e0 --- /dev/null +++ b/web/modules/contrib/pathologic/src/Tests/PathologicTest.php @@ -0,0 +1,307 @@ +assertEqual( + _pathologic_url_to_protocol_relative('http://example.com/foo/bar'), + '//example.com/foo/bar', + t('Protocol-relative URL creation with http:// URL') + ); + $this->assertEqual( + _pathologic_url_to_protocol_relative('https://example.org/baz'), + '//example.org/baz', + t('Protocol-relative URL creation with https:// URL') + ); + + // Build some paths to check against + $test_paths =[ + 'foo' => [ + 'path' => 'foo', + 'opts' => [] + ], + 'foo/bar' => [ + 'path' => 'foo/bar', + 'opts' => [] + ], + 'foo/bar?baz' => [ + 'path' => 'foo/bar', + 'opts' => ['query' => ['baz' => NULL]] + ], + 'foo/bar?baz=qux' => [ + 'path' => 'foo/bar', + 'opts' => ['query' => ['baz' => 'qux']] + ], + 'foo/bar#baz' => [ + 'path' => 'foo/bar', + 'opts' => ['fragment' => 'baz'], + ], + 'foo/bar?baz=qux&quux=quuux#quuuux' => [ + 'path' => 'foo/bar', + 'opts' => [ + 'query' => ['baz' => 'qux', 'quux' => 'quuux'], + 'fragment' => 'quuuux', + ], + ], + 'foo%20bar?baz=qux%26quux' => [ + 'path' => 'foo bar', + 'opts' => [ + 'query' => ['baz' => 'qux&quux'], + ], + ], + '/' => [ + 'path' => '', + 'opts' => [], + ], + ]; + + foreach (['full', 'proto-rel', 'path'] as $protocol_style) { + $format_id = _pathologic_build_format(['settings_source' => 'local', 'local_settings' => ['protocol_style' => $protocol_style]]); + $paths = []; + foreach ($test_paths as $path => $args) { + $args['opts']['absolute'] = $protocol_style !== 'path'; + $paths[$path] = _pathologic_content_url($args['path'], $args['opts']); + if ($protocol_style === 'proto-rel') { + $paths[$path] = _pathologic_url_to_protocol_relative($paths[$path]); + } + } + $t10ns = [ + '@clean' => empty($script_path) ? t('Yes') : t('No'), + '@ps' => $protocol_style, + ]; + + $this->assertEqual( + check_markup('', $format_id), + '', + t('Simple paths. Clean URLs: @clean; protocol style: @ps.', $t10ns) + ); + $this->assertEqual( + check_markup('', $format_id), + '', + t('D7 and earlier-style non-clean URLs. Clean URLs: @clean; protocol style: @ps.', $t10ns) + ); + $this->assertEqual( + check_markup('', $format_id), + '', + t('D8-style non-clean URLs. Clean URLs: @clean; protocol style: @ps.', $t10ns) + ); + $this->assertEqual( + check_markup('
', $format_id), + '', + t('Paths with query string. Clean URLs: @clean; protocol style: @ps.', $t10ns) + ); + $this->assertEqual( + check_markup('', $format_id), + '', + t('Path with fragment. Clean URLs: @clean; protocol style: @ps.', $t10ns) + ); + $this->assertEqual( + check_markup('', $format_id), + '', + t('Fragment-only href. Clean URLs: @clean; protocol style: @ps.', $t10ns) + ); + // @see https://drupal.org/node/2208223 + $this->assertEqual( + check_markup('', $format_id), + '', + t('Hash-only href. Clean URLs: @clean; protocol style: @ps.', $t10ns) + ); + $this->assertEqual( + check_markup('', $format_id), + '', + t('Path with query string and fragment. Clean URLs: @clean; protocol style: @ps.', $t10ns) + ); + $this->assertEqual( + check_markup('', $format_id), + '', + t('Path with URL encoded parts. Clean URLs: @clean; protocol style: @ps.', $t10ns) + ); + $this->assertEqual( + check_markup('', $format_id), + '', + t('Path with just slash. Clean URLs: @clean; protocol style: @ps', $t10ns) + ); + } + + global $base_path; + $this->assertEqual( + check_markup('bar', $format_id), + 'bar', + t('Paths beginning with $base_path (like WYSIWYG editors like to make)') + ); + global $base_url; + $this->assertEqual( + check_markup('bar', $format_id), + 'bar', + t('Paths beginning with $base_url') + ); + + // @see http://drupal.org/node/1617944 + $this->assertEqual( + check_markup('bar', $format_id), + 'bar', + t('Off-site schemeless URLs (//example.com/foo) ignored') + ); + + // Test internal: and all base paths + $format_id = _pathologic_build_format([ + 'settings_source' => 'local', + 'local_settings' => [ + 'local_paths' => "http://example.com/qux\nhttp://example.org\n/bananas", + 'protocol_style' => 'full', + ], + ]); + + // @see https://drupal.org/node/2030789 + $this->assertEqual( + check_markup('bar', $format_id), + 'bar', + t('On-site schemeless URLs processed') + ); + $this->assertEqual( + check_markup('', $format_id), + '', + t('Path Filter compatibility (internal:)') + ); + $this->assertEqual( + check_markup('look', $format_id), + 'look', + t('Path Filter compatibility (files:)') + ); + $this->assertEqual( + check_markup('', $format_id), + '', + t('"All base paths for this site" functionality') + ); + $this->assertEqual( + check_markup('bar', $format_id), + 'bar', + t('URLs with likely protocols are ignored') + ); + // Test hook_pathologic_alter() implementation. + $this->assertEqual( + check_markup('', $format_id), + '', + t('hook_pathologic_alter(): Alter $url_params') + ); + $this->assertEqual( + check_markup('', $format_id), + '', + t('hook_pathologic_alter(): Passthrough with use_original option') + ); + + // Test paths to existing files when clean URLs are disabled. + // @see http://drupal.org/node/1672430 + $script_path = ''; + $filtered_tag = check_markup('', $format_id); + $this->assertTrue( + strpos($filtered_tag, 'q=') === FALSE, + t('Paths to files don\'t have ?q= when clean URLs are off') + ); + + $format_id = _pathologic_build_format([ + 'settings_source' => 'global', + 'local_settings' => [ + 'protocol_style' => 'rel', + ], + ]); + $this->config('pathologic.settings') + ->set('protocol_style', 'proto-rel') + ->set('local_paths', 'http://example.com/') + ->save(); + $this->assertEqual( + check_markup('', $format_id), + '', + t('Use global settings when so configured on the format') + ); + + // Test really broken URLs. + // @see https://www.drupal.org/node/2602312 + $original = 'foo'; + $message = t('Fails sensibly when \Drupal\Core\Url::fromUri() throws exception'); + try { + $filtered = check_markup($original, $format_id); + $this->assertEqual( + $original, + $filtered, + $message + ); + } + catch (\Exception $e) { + $this->fail($message); + } + + } + +} + +/** + * Wrapper around url() which does HTML entity decoding and encoding. + * + * Since Pathologic works with paths in content, it needs to decode paths which + * have been HTML-encoded, and re-encode them when done. This is a wrapper + * around url() which does the same thing so that we can expect the results + * from it and from Pathologic to still match in our tests. + * + * @see url() + * @see http://drupal.org/node/1672932 + * @see http://www.w3.org/TR/xhtml1/guidelines.html#C_12 + */ +function _pathologic_content_url($path, $options) { + // If we should pretend this is a path to a file, make url() behave like clean + // URLs are enabled. + // @see _pathologic_replace() + // @see http://drupal.org/node/1672430 + if (!empty($options['is_file'])) { + $options['script_path'] = ''; + } + + if (parse_url($path, PHP_URL_SCHEME) === NULL) { + if ($path == '') { + return SafeMarkup::checkPlain(Url::fromRoute('', [], $options)->toString()); + } + $path = 'base://' . $path; + } + return SafeMarkup::checkPlain(Url::fromUri(htmlspecialchars_decode($path), $options)->toString()); +} + + +/** + * Build a text format with Pathologic configured a certain way. + * + * @param $settings + * An array of settings for the Pathologic instance on the format. + * @return + * A format machine name (consisting of random characters) for the format. + */ +function _pathologic_build_format($settings) { + $format_id = user_password(); + $format = entity_create('filter_format', [ + 'format' => $format_id, + 'name' => $format_id, + ]); + $format->setFilterConfig('filter_pathologic', [ + 'status' => 1, + 'settings' => $settings, + ]); + $format->save(); + return $format_id; +} diff --git a/web/modules/contrib/pathologic/src/Tests/PathologicUITest.php b/web/modules/contrib/pathologic/src/Tests/PathologicUITest.php new file mode 100644 index 000000000..bacabd58f --- /dev/null +++ b/web/modules/contrib/pathologic/src/Tests/PathologicUITest.php @@ -0,0 +1,103 @@ +drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); + $this->drupalLogin($this->drupalCreateUser(['administer filters', 'create page content'])); + } + + /** + * Tests for the Pathologic UI. + */ + public function testPathologicUi() { + $this->doTestSettingsForm(); + $this->doTestFormatsOptions(); + $this->doTestFixUrl(); + } + + /** + * Test settings form. + */ + public function doTestSettingsForm() { + $this->drupalGet('admin/config/content/pathologic'); + $this->assertText('Pathologic configuration'); + + // Test submit form. + $this->assertNoFieldChecked('edit-protocol-style-proto-rel'); + $edit = [ + 'protocol_style' => 'proto-rel', + 'local_paths' => 'http://example.com/', + ]; + $this->drupalPostForm(NULL, $edit, t('Save configuration')); + $this->assertText('The configuration options have been saved.'); + $this->assertFieldChecked('edit-protocol-style-proto-rel'); + $this->assertText('http://example.com/'); + $this->clickLink('Pathologic’s documentation'); + $this->assertResponse(200); + } + + /** + * Test text formats and editors options with pathologic. + */ + public function doTestFormatsOptions() { + + // Test plain text with pathologic configuration. + $this->drupalGet('/admin/config/content/formats/manage/plain_text'); + + // Select pathologic option. + $this->assertText('Correct URLs with Pathologic'); + $this->assertNoFieldChecked('edit-filters-filter-pathologic-status'); + $this->drupalPostForm(NULL, [ + 'filters[filter_html_escape][status]' => FALSE, + 'filters[filter_pathologic][status]' => '1', + ], t('Save configuration')); + + $this->drupalGet('/admin/config/content/formats/manage/plain_text'); + $this->assertRaw('In most cases, Pathologic should be the last filter in the “Filter processing order” list.'); + $this->assertText('Select whether Pathologic should use the global Pathologic settings'); + $this->assertFieldChecked('edit-filters-filter-pathologic-status'); + $this->drupalPostForm(NULL, [ + 'filters[filter_pathologic][settings][settings_source]' => 'local', + 'filters[filter_pathologic][settings][local_settings][protocol_style]' => 'full', + ], t('Save configuration')); + + $this->drupalGet('/admin/config/content/formats/manage/plain_text'); + $this->assertFieldChecked('edit-filters-filter-pathologic-settings-settings-source-local'); + $this->assertFieldChecked('edit-filters-filter-pathologic-settings-local-settings-protocol-style-full'); + $this->assertText('Custom settings for this text format'); + } + + /** + * Test that a url is fixed with pathologic. + */ + public function doTestFixUrl() { + $this->drupalGet('node/add/page'); + $edit = [ + 'title[0][value]' => 'Test pathologic', + 'body[0][value]' => 'Test link', + ]; + $this->drupalPostForm('node/add/page', $edit, t('Save')); + + // Assert that the link is processed with Pathologic. + $this->clickLink('Test link'); + $this->assertTitle('Test pathologic | Drupal'); + } + +}