Skip to content

Commit 6a5d169

Browse files
committed
Prevent EmbedParser to follow infinite redirect for Twitter links
fix according to php-embed/Embed#520 (comment) remp/helpdesk#2594
1 parent ede45c6 commit 6a5d169

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
2828
- Fixed issue with oversize images in MS Outlook. remp/remp#1330
2929
- Fixed issue with persistent embed cookies stored in system `tmp` folder, which were shared across releases. remp/helpdesk#2587
3030
- Each release now stores embed cookies in its own temp folder.
31+
- Added ability to set custom CURL settings for `EmbedParser`. remp/helpdesk#2594
3132

3233
## Archive
3334

Mailer/app/Models/Generators/EmbedParser.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public function setTwitterLinkText(string $twitterLinkText = null): void
1212
$this->twitterLinkText = $twitterLinkText;
1313
}
1414

15+
private function isTwitterLink($link): bool
16+
{
17+
return str_contains($link, 'twitt');
18+
}
19+
1520
public function createEmbedMarkup(string $link, ?string $title = null, ?string $image = null, bool $isVideo = false): string
1621
{
1722
$html = "<br>";
@@ -20,7 +25,7 @@ public function createEmbedMarkup(string $link, ?string $title = null, ?string $
2025

2126
if (!is_null($image) && !is_null($title)) {
2227
$html .= "<img src='{$image}' alt='{$title}' style='outline:none;text-decoration:none;-ms-interpolation-mode:bicubic;max-width:100%;clear:both;display:inline;width:100%;height:auto;'>";
23-
} elseif (preg_match('/twitt/', $link)) {
28+
} elseif ($this->isTwitterLink($link)) {
2429
return "<br><a style=\"display: block;margin: 0 0 20px;padding: 7px 10px;text-decoration: none;text-align: center;font-weight: bold;font-family:'Helvetica Neue', Helvetica, Arial;color: #249fdc; background: #ffffff; border: 3px solid #249fdc;margin: 16px 0 16px 0\" href=\"{$link}\" target=\"_blank\">{$this->twitterLinkText}</a>";
2530
} else {
2631
$html .= "<span style='text-decoration: underline; color: #1F3F83;'>" . $link . "</span>";

Mailer/extensions/mailer-module/src/Models/Generators/EmbedParser.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,31 @@ class EmbedParser
1212
{
1313
protected ?string $videoLinkText;
1414

15-
public function __construct(private CurlClient $curlClient)
16-
{
17-
}
15+
protected array $curlSettings = [];
1816

1917
public function setVideoLinkText(?string $videoLinkText = null): void
2018
{
2119
$this->videoLinkText = $videoLinkText;
2220
}
2321

22+
public function setCurlSettings(array $settings)
23+
{
24+
$this->curlSettings = $settings;
25+
}
26+
2427
private function fetch(string $url): ?array
2528
{
26-
$embed = new Embed(new Crawler($this->curlClient));
29+
$curlSettings = [
30+
// Twitter may generate infinite redirect (2024/03),
31+
// fix according to https://github.com/oscarotero/Embed/issues/520#issue-1782756560
32+
'follow_location' => !$this->isTwitterLink($url),
33+
...$this->curlSettings,
34+
];
35+
36+
$curlClient = new CurlClient();
37+
$curlClient->setSettings($curlSettings);
38+
39+
$embed = new Embed(new Crawler($curlClient));
2740
$embed = $embed->get($url);
2841

2942
$oEmbed = $embed->getOEmbed();
@@ -38,17 +51,22 @@ private function fetch(string $url): ?array
3851
'link' => $embed->url->__toString(),
3952
'title' => $embed->title ?? '',
4053
'image' => $image,
41-
'isVideo' => $type === 'video'
54+
'isVideo' => $type === 'video',
4255
];
4356
}
4457

58+
private function isTwitterLink($link)
59+
{
60+
return str_contains($link, 'twitt');
61+
}
62+
4563
public function parse(string $link): ?string
4664
{
4765
$link = trim($link);
4866

4967
if (preg_match('/^(?:(?:https?:)?\/\/)?(?:www\.)?facebook\.com\/[a-zA-Z0-9.]+\/videos\/(?:[a-zA-Z0-9.]+\/)?([0-9]+)/', $link)
5068
|| str_contains($link, 'youtu')
51-
|| str_contains($link, 'twitt')
69+
|| $this->isTwitterLink($link)
5270
) {
5371
try {
5472
if ($data = $this->fetch($link)) {

Mailer/extensions/mailer-module/src/config/config.neon

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,12 @@ services:
333333
- Remp\MailerModule\Models\Generators\WordpressHelpers
334334
embedParser:
335335
factory: Remp\MailerModule\Models\Generators\EmbedParser
336+
setup:
337+
- setCurlSettings([cookies_path: %tempDir%/embed-cookies.txt])
336338
articleLocker:
337339
factory: Remp\MailerModule\Models\Generators\ArticleLocker
338340

339341
# healtCheck - inject temp for storage check
340342
- Remp\MailerModule\Presenters\HealthPresenter(%tempDir%)
341343

342344
- Remp\MailerModule\Components\MailLinkStats\MailLinkStats
343-
344-
embedCurlClient:
345-
factory: Embed\Http\CurlClient
346-
setup:
347-
- setSettings([cookies_path: %tempDir%/embed-cookies.txt])

0 commit comments

Comments
 (0)