Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URL-encode URLs #348

Open
wants to merge 2 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Sitemap/Url/GoogleImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function toXML(): string
{
$xml = '<image:image>';

$xml .= '<image:loc>' . Utils::encode($this->getLocation()) . '</image:loc>';
$xml .= '<image:loc>' . Utils::encodeUrl($this->getLocation()) . '</image:loc>';

if ($this->getCaption()) {
$xml .= '<image:caption>' . Utils::cdata($this->getCaption()) . '</image:caption>';
Expand Down
2 changes: 1 addition & 1 deletion src/Sitemap/Url/GoogleMultilangUrlDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function generateLinkXml(string $href, string $hreflang, string $rel =

return '<xhtml:link rel="' . $rel
. '" hreflang="' . $hreflang
. '" href="' . Utils::encode($href) . '" />';
. '" href="' . Utils::encodeUrl($href) . '" />';
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Sitemap/Url/GoogleVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ public function toXml(): string

//----------------------
// required fields
$videoXml .= '<video:thumbnail_loc>' . Utils::encode($this->getThumbnailLocation()) . '</video:thumbnail_loc>';
$videoXml .= '<video:thumbnail_loc>' . Utils::encodeUrl($this->getThumbnailLocation()) . '</video:thumbnail_loc>';
$videoXml .= '<video:title>' . Utils::cdata($this->getTitle()) . '</video:title>';
$videoXml .= '<video:description>' . Utils::cdata($this->getDescription()) . '</video:description>';

Expand All @@ -952,7 +952,7 @@ public function toXml(): string
$videoXml .= '<video:category>' . Utils::cdata($category) . '</video:category>';
}
if ($location = $this->getContentLocation()) {
$videoXml .= '<video:content_loc>' . Utils::encode($location) . '</video:content_loc>';
$videoXml .= '<video:content_loc>' . Utils::encodeUrl($location) . '</video:content_loc>';
}
if ($duration = $this->getDuration()) {
$videoXml .= '<video:duration>' . $duration . '</video:duration>';
Expand Down
2 changes: 1 addition & 1 deletion src/Sitemap/Url/UrlConcrete.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function getPriority(): ?float
*/
public function toXml(): string
{
$xml = '<url><loc>' . Utils::encode($this->getLoc()) . '</loc>';
$xml = '<url><loc>' . Utils::encodeUrl($this->getLoc()) . '</loc>';

$lastmod = $this->getLastmod();
if ($lastmod) {
Expand Down
40 changes: 40 additions & 0 deletions src/Sitemap/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,44 @@
{
return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}

/**
* Encode URL
*
* @param string $string
*
* @return string
*/
public static function encodeUrl(string $url): string

Check failure on line 50 in src/Sitemap/Utils.php

View workflow job for this annotation

GitHub Actions / PhpStan (8.3, 7.1.*)

PHPDoc tag @param references unknown parameter: $string
{
$parts = parse_url($url);

// Optional but we only sanitize URLs with scheme and host defined
if ($parts === false || empty($parts['scheme']) || empty($parts['host'])) {
return $url;
}

$sanitizedPath = null;
if (!empty($parts['path'])) {
$pathParts = explode('/', $parts['path']);
foreach ($pathParts as $pathPart) {
if (empty($pathPart)) {
continue;
}
// The Path part might already be urlencoded
$sanitizedPath .= '/'.rawurlencode(rawurldecode($pathPart));
}
}

// Build the url
$targetUrl = $parts['scheme'].'://'.
((!empty($parts['user']) && !empty($parts['pass'])) ? $parts['user'].':'.$parts['pass'].'@' : '').
$parts['host'].
(!empty($parts['port']) ? ':'.$parts['port'] : '').
(!empty($sanitizedPath) ? $sanitizedPath : '').
(!empty($parts['query']) ? '?'.self::encode($parts['query']) : '').
(!empty($parts['fragment']) ? '#'.$parts['fragment'] : '');

return $targetUrl;
}
}
2 changes: 2 additions & 0 deletions tests/Unit/Sitemap/Url/UrlConcreteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public function toXmlProvider(): array
['<url><loc>http://example.com/</loc></url>', 'http://example.com/'],
['<url><loc>http://example.com/abcd</loc></url>', 'http://example.com/abcd'],
['<url><loc>http://example.com/abcd/?a=1&amp;b=cdf</loc></url>', 'http://example.com/abcd/?a=1&b=cdf'],
['<url><loc>http://example.com/%C3%A4</loc></url>', 'http://example.com/ä'],
['<url><loc>http://example.com/folder/%C3%A4</loc></url>', 'http://example.com/folder/ä'],
[
'<url><loc>http://example.com/</loc><lastmod>2012-12-29T10:39:12+00:00</lastmod></url>',
'http://example.com/',
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/Sitemap/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ public function testEncode(): void
$actual = Utils::encode('data & spécial chars>');
self::assertEquals('data &amp; spécial chars&gt;', $actual);
}

public function testEncodeUrl(): void
{
$actual = Utils::encodeUrl('http://example.org/test_ä');
self::assertEquals('http://example.org/test_%C3%A4', $actual);
}
}
Loading