Skip to content

Commit

Permalink
Rewrite iframe URLs so they always work.
Browse files Browse the repository at this point in the history
  • Loading branch information
kepol committed Mar 5, 2024
1 parent 7d7c95c commit bf7c025
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
9 changes: 7 additions & 2 deletions modules/quant_api/src/EventSubscriber/QuantApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,13 @@ public function onOutput(QuantEvent $event) {
/** @var \DOMElement $node */
foreach ($xpath->query('//iframe[contains(@src, "/media/oembed")]') as $node) {
$oembed_url = $new_href = $node->getAttribute('src');
$oembed_item = new RouteItem(['route' => $oembed_url]);
$oembed_item->send();
// Only add if it's a relative URL to handle the case where the URL
// contains `/media/oembed` but is from another website.
if (str_starts_with($oembed_url, '/')) {
$oembed_item = new RouteItem(['route' => $oembed_url]);
$oembed_item->send();
$this->logger->notice("[route_item] $oembed_url");
}
}

// @todo Report on forms that need proxying (attachments.forms).
Expand Down
30 changes: 25 additions & 5 deletions src/Seed.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,17 +639,37 @@ public static function rewriteRelative($markup) {
// Do not strip host domain unless configured.
$strip = $config->get('host_domain_strip') ?: FALSE;
if (!$strip) {
// Handle iframes even if `host_domain_strip` is disabled, so iframe
// renders correctly.
$markup = self::rewriteRelativeIframe($markup);

return $markup;
}

// Strip the host domain from everywhere in the content including header
// metadata such as canonical links.
$hostname = $config->get('host_domain') ?: $_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT'];
$markup = preg_replace("/(https?:\/\/)?{$hostname}(\:{$port})?/i", '', $markup);
$markup = Utility::stripLocalHost($markup);

// Edge case: Replace http://default when run via drush without base_url.
$markup = preg_replace("/http:\/\/default/i", '', $markup);
return $markup;
}

/**
* Replaces absolute iframe URLs with relative in markup.
*
* @param string $markup
* The markup to search and rewire relative iframe paths for.
*
* @return string
* Sanitized markup string.
*/
public static function rewriteRelativeIframe($markup) {
$pattern = '/<iframe src="([^"]+)"/i';
if (preg_match_all($pattern, $markup, $matches)) {
foreach ($matches[1] as $url) {
$updated_url = Utility::stripLocalHost($url);
$markup = str_replace($url, $updated_url, $markup);
}
}

return $markup;
}
Expand Down
24 changes: 23 additions & 1 deletion src/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static function getPathPrefix(string $langcode = NULL) : string {
* @return string
* The relative canonical URL.
*/
public static function getCanonicalUrl($type, $id, $langcode) {
public static function getCanonicalUrl(string $type, int $id, string $langcode) : string {
$options = ['absolute' => FALSE];
if (!empty($langcode)) {
$language = \Drupal::languageManager()->getLanguage($langcode);
Expand All @@ -119,6 +119,28 @@ public static function getCanonicalUrl($type, $id, $langcode) {
return Url::fromRoute('entity.' . $type . '.canonical', [$type => $id], $options)->toString();
}

/**
* Strip local host from text.
*
* @param string $text
* The text.
*
* @return string
* The text without local host.
*/
public static function stripLocalHost(string $text) : string {

$config = \Drupal::config('quant.settings');
$hostname = $config->get('host_domain') ?: $_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT'];
$text = preg_replace("/(https?:\/\/)?{$hostname}(\:{$port})?/i", '', $text);

// Edge case: Replace http://default when run via drush without base_url.
$text = preg_replace("/http:\/\/default/i", '', $text);

return $text;
}

/**
* Checks if it's an external URL.
*
Expand Down

0 comments on commit bf7c025

Please sign in to comment.