-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathOembedService.php
More file actions
executable file
·653 lines (554 loc) · 19.2 KB
/
Copy pathOembedService.php
File metadata and controls
executable file
·653 lines (554 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
<?php
/**
* oEmbed plugin for Craft CMS 3.x
*
* A simple plugin to extract media information from websites, like youtube videos, twitter statuses or blog articles.
*
* @link https://github.com/wrav
* @copyright Copyright (c) 2017 reganlawton
*/
namespace wrav\oembed\services;
use craft;
use craft\base\Component;
use craft\helpers\Template;
use DOMDocument;
use Embed\Embed;
use Exception;
use wrav\oembed\adapters\EmbedAdapter;
use wrav\oembed\adapters\FallbackAdapter;
use wrav\oembed\events\BrokenUrlEvent;
use wrav\oembed\Oembed;
/**
* OembedService Service
*
* @author reganlawton
* @package Oembed
* @since 1.0.0
*/
class OembedService extends Component
{
private const YOUTUBE_PATTERN = '/(?:http|https)*?:*\/\/(?:www\.|)(?:youtube\.com|m\.youtube\.com|youtu\.be|youtube-nocookie\.com)/i';
private const VIMEO_PATTERN = '/vimeo\.com/i';
private const DEFAULT_CACHE_KEYS = [
'title', 'description', 'url', 'type', 'tags', 'images', 'image',
'imageWidth', 'imageHeight', 'code', 'width', 'height', 'aspectRatio',
'authorName', 'authorUrl', 'providerName', 'providerUrl', 'providerIcons',
'providerIcon', 'publishedDate', 'license', 'linkedData', 'feeds',
];
private const SUCCESSFUL_CACHE_DURATION = 3600; // 1 hour
private const FAILED_CACHE_DURATION = 900; // 15 minutes
// Dependencies - can be injected for testing
private $cacheService;
private $pluginService;
private $settings;
private $eventDispatcher;
/**
* Constructor allows dependency injection for testing
*/
public function __construct($config = [])
{
parent::__construct($config);
// Initialize dependencies - can be overridden for testing
$this->cacheService = $this->cacheService ?? Craft::$app->cache;
$this->pluginService = $this->pluginService ?? Craft::$app->plugins;
$this->settings = $this->settings ?? Oembed::getInstance()->getSettings();
$this->eventDispatcher = $this->eventDispatcher ?? Oembed::getInstance();
}
/**
* Set cache service (for testing)
*/
public function setCacheService($cacheService): void
{
$this->cacheService = $cacheService;
}
/**
* Set plugin service (for testing)
*/
public function setPluginService($pluginService): void
{
$this->pluginService = $pluginService;
}
/**
* Set settings (for testing)
*/
public function setSettings($settings): void
{
$this->settings = $settings;
}
/**
* Set event dispatcher (for testing)
*/
public function setEventDispatcher($eventDispatcher): void
{
$this->eventDispatcher = $eventDispatcher;
}
/**
* @param string $url
* @param array $options
* @param array $cacheProps
* @return string|null
*/
public function render($url, array $options = [], array $cacheProps = [])
{
$media = $this->embed($url, $options, $cacheProps);
if (empty($media)) {
return null;
}
$code = $media->code ?? $media->html ?? '';
return Template::raw($code);
}
/**
* Normalize a URL by adding https:// when no scheme is present.
*/
private function normalizeUrl(string $url): string
{
$url = trim($url);
if ($url === '') {
return $url;
}
if (preg_match('#^https?://#i', $url)) {
return $url;
}
if (str_starts_with($url, '//')) {
return 'https:' . $url;
}
return 'https://' . $url;
}
/**
* @param string $input
* @param array $options
* @return string
*/
public function parseTags(string $input, array $options = [], array $cacheProps = [])
{
if (empty($input)) {
return '';
}
$output = preg_replace_callback('/\<oembed\s(?:.*?)url="(.*?)"(?:.*?)>(?:.*?)<\/oembed>/i', function($matches) use ($options, $cacheProps) {
$url = $matches[1];
return $this->render($url, $options, $cacheProps);
}, $input);
return $output;
}
/**
* Generate cache key for the embed request
*/
protected function generateCacheKey(string $url, array $options, array $cacheProps): string
{
// Additional safety check (should not be needed after embed() method fix)
$url = $url ?: '';
try {
$hash = md5(json_encode($options)) . md5(json_encode($cacheProps));
} catch (\Exception $exception) {
$hash = '';
}
$plugin = $this->pluginService->getPlugin('oembed');
return $url . '_' . $plugin->getVersion() . '_' . $hash;
}
/**
* Get embed data from cache if available
*/
private function getCachedEmbed(string $cacheKey)
{
if ($this->settings->enableCache && $this->cacheService->exists($cacheKey)) {
return $this->cacheService->get($cacheKey);
}
return null;
}
/**
* Set up options with Facebook API key if available
*/
protected function prepareOptions(array $options): array
{
if ($this->settings->facebookKey) {
$options['facebook']['key'] = $this->settings->facebookKey;
}
return $options;
}
/**
* Create embed using the Embed library
*/
protected function createEmbed(string $url, array $options, array $factories): ?EmbedAdapter
{
try {
array_multisort($options);
// Create a custom crawler with cookie settings
$curlClient = new \Embed\Http\CurlClient();
$cookieSettings = $this->getCookieSettings();
$curlClient->setSettings($cookieSettings);
$crawler = new \Embed\Http\Crawler($curlClient);
$embed = new Embed($crawler);
// Add custom factories
if (count($factories) > 0) {
foreach ($factories as $factory) {
$embed->getExtractorFactory()->addAdapter($factory['domain'], $factory['extractor']::class);
}
}
$infos = $embed->get($url ?: "");
$infos->setSettings($options);
$data = $infos->getOEmbed()->all();
return new EmbedAdapter($data, $infos);
} catch (Exception $e) {
$this->logError($e->getMessage());
$this->triggerBrokenUrlEvent($url);
return null;
}
}
/**
* Create fallback adapter when embed fails
*/
private function createFallbackAdapter(string $url): FallbackAdapter
{
return new FallbackAdapter([
'html' => $url ? '<iframe src="' . $url . '"></iframe>' : null
]);
}
/**
* Log error message
*/
private function logError(string $message): void
{
Craft::info($message, 'oembed');
}
/**
* Trigger broken URL event if notifications are enabled
*/
private function triggerBrokenUrlEvent(string $url): void
{
if (!$this->settings->enableNotifications) {
return;
}
// Validate URL before triggering event
if (!$url || trim($url) === '') {
Craft::warning('triggerBrokenUrlEvent: Cannot trigger event for empty URL', 'oembed');
return;
}
Craft::info('triggerBrokenUrlEvent: Triggering broken URL event for: ' . $url, 'oembed');
$this->eventDispatcher->trigger(Oembed::EVENT_BROKEN_URL_DETECTED, new BrokenUrlEvent([
'url' => trim($url),
]));
}
/**
* Get cookie settings for the embed library
*/
private function getCookieSettings(): array
{
$settings = [];
// Set custom cookies path if configured
if (!empty($this->settings->cookiesPath)) {
$cookiesDir = rtrim($this->settings->cookiesPath, '/');
if (!is_dir($cookiesDir)) {
@mkdir($cookiesDir, 0755, true);
}
$settings['cookies_path'] = $cookiesDir . '/embed-cookies.txt';
} else {
// Use plugin-specific temp directory
$tempDir = Craft::$app->getPath()->getTempPath() . '/oembed-cookies';
if (!is_dir($tempDir)) {
@mkdir($tempDir, 0755, true);
}
$settings['cookies_path'] = $tempDir . '/embed-cookies-' . uniqid() . '.txt';
}
return $settings;
}
/**
* Clean up old cookie files
*/
public function cleanupCookieFiles(): int
{
if (!$this->settings->enableCookieCleanup) {
return 0;
}
$deletedCount = 0;
$maxAge = $this->settings->cookieMaxAge;
$cutoffTime = time() - $maxAge;
// Define directories to clean
$dirsToClean = [
Craft::$app->getPath()->getTempPath() . '/oembed-cookies',
sys_get_temp_dir()
];
// Add custom cookies path if set
if (!empty($this->settings->cookiesPath)) {
$dirsToClean[] = rtrim($this->settings->cookiesPath, '/');
}
foreach ($dirsToClean as $dir) {
if (!is_dir($dir)) {
continue;
}
try {
$files = glob($dir . '/embed-cookie*.txt');
if ($files) {
foreach ($files as $file) {
if (is_file($file) && filemtime($file) < $cutoffTime) {
if (@unlink($file)) {
$deletedCount++;
Craft::info("Cleaned up cookie file: {$file}", 'oembed');
}
}
}
}
} catch (\Exception $e) {
Craft::warning("Failed to cleanup cookies in {$dir}: " . $e->getMessage(), 'oembed');
}
}
if ($deletedCount > 0) {
Craft::info("Cookie cleanup completed: {$deletedCount} files removed", 'oembed');
}
return $deletedCount;
}
/**
* Process DOM and apply options to iframe
*/
private function processDomAndApplyOptions($media, array $data, string $url, array $options)
{
try {
$dom = new DOMDocument;
$html = $this->prepareHtml($media, $data, $url);
// Skip DOM processing if HTML is empty
if (empty($html)) {
return $media;
}
$dom->loadHTML($html);
$iframe = $dom->getElementsByTagName('iframe')->item(0);
if (!$iframe) {
return $media;
}
$src = $iframe->getAttribute('src');
$src = $this->processIframeSrc($src, $options, $url);
$iframe->setAttribute('src', $src);
$this->applyGdprAttributes($iframe, $src);
$this->applyIframeAttributes($iframe, $options, $media);
$mainElement = $this->getMainElement($dom);
$code = $dom->saveXML($mainElement, LIBXML_NOEMPTYTAG);
$media->code = $code;
$media->url = $media->url ?: $url;
return $media;
} catch (\Exception $exception) {
$this->logError($exception->getMessage());
return $media;
}
}
/**
* Prepare HTML for DOM processing
*/
private function prepareHtml($media, array $data, string $url): string
{
$html = $media->getCode() ?: null;
if (empty($html) || empty((string)$url)) {
return empty((string)$url) ? '' : '<iframe src="' . $url . '"></iframe>';
}
$html = $data['html'];
$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
return preg_replace('/&(?!#?[a-z0-9]+;)/i', '&', $html);
}
/**
* Process iframe src with options and GDPR
*/
protected function processIframeSrc(string $src, array $options, string $url): string
{
$src = $this->manageGDPR($src);
$src = $this->addQueryParameterBase($src);
$src = $this->applyUrlParameters($src, $options);
$src = $this->handleYouTubeLooping($src, $url);
return $src;
}
/**
* Add base query parameter if none exists
*/
private function addQueryParameterBase(string $src): string
{
if (!preg_match('/\?(.*)$/i', $src)) {
$src .= "?";
}
return $src;
}
/**
* Apply URL parameters from options
*/
private function applyUrlParameters(string $src, array $options): string
{
// Apply params
if (!empty($options['params'])) {
foreach ((array)$options['params'] as $key => $value) {
if (is_array($value)) {
continue;
}
$src = preg_replace('/\?(.*)$/i', '?' . $key . '=' . $value . '&${1}', $src);
}
}
// Apply individual parameters
$parameters = ['autoplay', 'loop', 'autopause', 'rel'];
foreach ($parameters as $param) {
if (!empty($options[$param]) && strpos($src, $param . '=') === false && $src) {
$value = !!$options[$param] ? '1' : '0';
$src = preg_replace('/\?(.*)$/i', '?' . $param . '=' . $value . '&${1}', $src);
}
}
return $src;
}
/**
* Handle YouTube looping special case
*/
private function handleYouTubeLooping(string $src, string $url): string
{
preg_match(self::YOUTUBE_PATTERN, $url, $ytMatch, PREG_OFFSET_CAPTURE);
if (count($ytMatch) && strpos($src, 'loop=1') !== false) {
preg_match('/\/embed\/([^?]+)/', $src, $ytCode);
if (count($ytCode)) {
$src = preg_replace('/\?(.*)$/i', '?playlist=' . $ytCode[1] . '&${1}', $src);
}
}
return $src;
}
/**
* Apply GDPR attributes to iframe
*/
private function applyGdprAttributes($iframe, string $src): void
{
if ($this->settings->enableGdprCookieBot) {
$iframe->setAttribute('data-cookieblock-src', $src);
$iframe->setAttribute('data-cookieconsent', 'marketing');
}
}
/**
* Apply iframe attributes and dimensions
*/
private function applyIframeAttributes($iframe, array $options, $media): void
{
// Width/Height overrides
if (!empty($options['width']) && is_int($options['width'])) {
$iframe->setAttribute('width', $options['width']);
}
if (!empty($options['height']) && is_int($options['height'])) {
$iframe->setAttribute('height', $options['height']);
}
// Apply custom attributes
if (!empty($options['attributes'])) {
foreach ((array)$options['attributes'] as $key => $value) {
$iframe->setAttribute($key, $value);
if (in_array($key, ['width', 'height'])) {
$media->$key = $value;
}
}
}
}
/**
* Get main DOM element for output
*/
private function getMainElement(DOMDocument $dom)
{
$bodyItem = $dom->getElementsByTagName('body')->item(0);
if ($bodyItem === null) {
return $dom->getElementsByTagName('iframe')->item(0);
}
if ($bodyItem->childNodes->count() === 1) {
return $bodyItem->childNodes->item(0);
}
// Multiple children, wrap in div
$mainElement = $dom->createElement('div');
$bodyChildren = [];
foreach ($bodyItem->childNodes as $child) {
$bodyChildren[] = $child;
}
foreach ($bodyChildren as $child) {
$mainElement->appendChild($child);
}
$bodyItem->appendChild($mainElement);
return $mainElement;
}
/**
* Cache the embed result
*/
private function cacheEmbed($media, string $cacheKey, array $cacheProps): void
{
if (!$this->settings->enableCache) {
return;
}
$duration = $media instanceof FallbackAdapter ?
self::FAILED_CACHE_DURATION :
self::SUCCESSFUL_CACHE_DURATION;
$cacheKeys = array_unique(array_merge(self::DEFAULT_CACHE_KEYS, $cacheProps));
// Ensure all keys are set to avoid errors
foreach ($cacheKeys as $key) {
try {
$media->{$key} = $media->{$key};
} catch (\Exception $e) {
$media->{$key} = null;
}
}
$this->cacheService->set($cacheKey, json_decode(json_encode($media)), $duration);
}
/**
* Main embed method - now much cleaner and testable
*
* @param string $url
* @param array $options
* @param array $cacheProps
* @param array $factories
* @return mixed
*/
public function embed($url, array $options = [], array $cacheProps = [], $factories = [])
{
// Normalize null/empty URLs immediately to prevent type errors
$url = $url ?: '';
if (is_string($url)) {
$url = $this->normalizeUrl($url);
}
// Check cache first
$cacheKey = $this->generateCacheKey($url, $options, $cacheProps);
$cachedResult = $this->getCachedEmbed($cacheKey);
if ($cachedResult !== null) {
return $cachedResult;
}
// Prepare options with API keys
$options = $this->prepareOptions($options);
// Try to create embed, fallback if it fails
$media = $this->createEmbed($url, $options, $factories);
if ($media === null) {
$media = $this->createFallbackAdapter($url);
}
// Process DOM and apply options
$data = $media instanceof EmbedAdapter ? ($media->data ?? []) : [];
if (empty($data)) {
$data = ['html' => $url ? '<iframe src="' . $url . '"></iframe>' : null];
}
$media = $this->processDomAndApplyOptions($media, $data, $url, $options);
// Cache the result
$this->cacheEmbed($media, $cacheKey, $cacheProps);
return $media ?? [];
}
/**
* Apply GDPR-compliant URL modifications
*/
protected function manageGDPR(string $url): string
{
if (!$this->settings->enableGdpr) {
return $url;
}
// Handle YouTube URLs - convert to no-cookie domain
if (preg_match(self::YOUTUBE_PATTERN, $url)) {
return preg_replace(self::YOUTUBE_PATTERN, 'https://www.youtube-nocookie.com', $url);
}
// Handle Vimeo URLs - add DNT parameter
if (preg_match(self::VIMEO_PATTERN, $url)) {
return $this->addVimeoDntParameter($url);
}
return $url;
}
/**
* Add DNT (Do Not Track) parameter to Vimeo URLs
*/
private function addVimeoDntParameter(string $url): string
{
// If DNT parameter already exists, ensure it's set to 1
if (strpos($url, 'dnt=') !== false) {
return preg_replace('/(dnt=(1|0))/i', 'dnt=1', $url);
}
// Add DNT parameter
if (strpos($url, '?') !== false) {
return preg_replace('/(\?(.*))$/i', '?dnt=1&${2}', $url);
} else {
return $url . '?dnt=1';
}
}
}