Skip to content
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
26 changes: 21 additions & 5 deletions Classes/Utility/SchemaUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function buildPersonData(Author $author): array
$url = '';
$sameAs = [];
if ($author->getProfile() !== '') {
$url = GeneralUtility::locationHeaderUrl($author->getProfile());
$url = self::getAbsoluteUrl($author->getProfile());
}

$data = [
Expand Down Expand Up @@ -65,20 +65,20 @@ public static function buildPersonData(Author $author): array
}

if ($author->getWebsite() !== '') {
$sameAs[] = GeneralUtility::locationHeaderUrl($author->getWebsite());
$sameAs[] = self::getAbsoluteUrl($author->getWebsite());
}
$twitterUrl = TwitterXUtility::getXUrl($author->getTwitter());
if ($twitterUrl !== '') {
$sameAs[] = $twitterUrl;
}
if ($author->getLinkedin() !== '') {
$sameAs[] = GeneralUtility::locationHeaderUrl($author->getLinkedin());
$sameAs[] = self::getAbsoluteUrl($author->getLinkedin());
}
if ($author->getXing() !== '') {
$sameAs[] = GeneralUtility::locationHeaderUrl($author->getXing());
$sameAs[] = self::getAbsoluteUrl($author->getXing());
}
if ($author->getInstagram() !== '') {
$sameAs[] = GeneralUtility::locationHeaderUrl($author->getInstagram());
$sameAs[] = self::getAbsoluteUrl($author->getInstagram());
}
$mastodonUrl = MastodonUtility::getMastodonUrl($author->getMastodon());
if ($mastodonUrl !== '') {
Expand All @@ -92,6 +92,22 @@ public static function buildPersonData(Author $author): array
return $data;
}

/**
* Author fields are plain input, and the website field even validates as a
* domain name, so "www.example.com" is a common value. Resolving it against
* the current page would point sameAs to the own site, so a missing scheme
* becomes https instead, as f:uri.external does in the templates.
*/
private static function getAbsoluteUrl(string $url): string
{
$url = trim($url);
if ($url !== '' && !str_starts_with($url, '/') && parse_url($url, PHP_URL_SCHEME) === null) {
return 'https://' . $url;
}

return GeneralUtility::locationHeaderUrl($url);
}

private static function getImageUrl(Author $author): string
{
$image = $author->getImage();
Expand Down
8 changes: 4 additions & 4 deletions Resources/Private/Partials/Post/Author.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<f:if condition="{author.website} || {author.twitter} || {author.instagram} || {author.linkedin} || {author.xing} || {author.mastodon}">
<div class="postauthor__social">
<f:if condition="{author.website}">
<a href="{author.website}" class="postauthor__sociallink postauthor__sociallink--website" target="_blank" itemprop="sameAs">
<a href="{f:uri.external(uri: author.website)}" class="postauthor__sociallink postauthor__sociallink--website" target="_blank" itemprop="sameAs">
<span class="postauthor__sociallinkicon postauthor__sociallinkicon--website">
<span class="blogicon"><f:render partial="General/SocialIcons" section="Website" optional="true" /></span>
</span>
Expand All @@ -37,23 +37,23 @@
</a>
</f:if>
<f:if condition="{author.instagram}">
<a href="{author.instagram}" class="postauthor__sociallink postauthor__sociallink--instagram" target="_blank" itemprop="sameAs">
<a href="{f:uri.external(uri: author.instagram)}" class="postauthor__sociallink postauthor__sociallink--instagram" target="_blank" itemprop="sameAs">
<span class="postauthor__sociallinkicon postauthor__sociallinkicon--instagram">
<span class="blogicon"><f:render partial="General/SocialIcons" section="Instagram" optional="true" /></span>
</span>
<span class="postauthor__sociallinklabel"><f:translate key="author.social.instagram" /></span>
</a>
</f:if>
<f:if condition="{author.linkedin}">
<a href="{author.linkedin}" class="postauthor__sociallink postauthor__sociallink--linkedin" target="_blank" itemprop="sameAs">
<a href="{f:uri.external(uri: author.linkedin)}" class="postauthor__sociallink postauthor__sociallink--linkedin" target="_blank" itemprop="sameAs">
<span class="postauthor__sociallinkicon postauthor__sociallinkicon--linkedin">
<span class="blogicon"><f:render partial="General/SocialIcons" section="LinkedIn" optional="true" /></span>
</span>
<span class="postauthor__sociallinklabel"><f:translate key="author.social.linkedin" /></span>
</a>
</f:if>
<f:if condition="{author.xing}">
<a href="{author.xing}" class="postauthor__sociallink postauthor__sociallink--xing" target="_blank" itemprop="sameAs">
<a href="{f:uri.external(uri: author.xing)}" class="postauthor__sociallink postauthor__sociallink--xing" target="_blank" itemprop="sameAs">
<span class="postauthor__sociallinkicon postauthor__sociallinkicon--xing">
<span class="blogicon"><f:render partial="General/SocialIcons" section="Xing" optional="true" /></span>
</span>
Expand Down
35 changes: 35 additions & 0 deletions Tests/Functional/ViewHelpers/Schema/PersonViewHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,41 @@ public function render(string $template, string $expected): void
);
}

#[Test]
public function urlsWithoutSchemeBecomeHttpsUrls(): void
{
$this->createTestSite();

(new ConnectionPool())->getConnectionForTable('tx_blog_domain_model_author')->insert(
'tx_blog_domain_model_author',
[
'uid' => 100,
'pid' => self::STORAGE_UID,
'name' => 'TYPO3 Inc Team',
'slug' => 'typo3-inc-team',
'posts' => 1,
'profile' => 'my.typo3.org/u/typo3-inc-team',
'website' => 'www.typo3.com',
'linkedin' => 'www.linkedin.com/company/typo3',
'xing' => 'https://www.xing.com/pages/typo3',
]
);

self::assertSame(
'{"@context":"https://schema.org","@type":"Person","name":"TYPO3 Inc Team","@id":"https://my.typo3.org/u/typo3-inc-team/#Person","url":"https://my.typo3.org/u/typo3-inc-team","sameAs":["https://my.typo3.org/u/typo3-inc-team","https://www.typo3.com","https://www.linkedin.com/company/typo3","https://www.xing.com/pages/typo3"]}',
$this->renderFluidTemplateInTestSite(
'<blogvh:schema.person author="{test.author}" />',
[
[
'type' => 'author',
'uid' => 100,
'as' => 'author',
]
]
)
);
}

#[Test]
public function markupInAuthorDataCannotEscapeTheScriptElement(): void
{
Expand Down
Loading