-
Notifications
You must be signed in to change notification settings - Fork 34
/
Service.php
749 lines (652 loc) · 27.4 KB
/
Service.php
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
<?php
namespace spicyweb\embeddedassets;
use Craft;
use craft\elements\Asset;
use craft\helpers\Assets;
use craft\helpers\FileHelper;
use craft\helpers\Html as HtmlHelper;
use craft\helpers\Json;
use craft\helpers\StringHelper;
use craft\helpers\Template;
use craft\helpers\UrlHelper;
use craft\models\VolumeFolder;
use DateTimeImmutable;
use DateTimeInterface;
use DOMDocument;
use Embed\Embed;
use Embed\Extractor;
use Embed\Http\Crawler;
use Embed\Http\CurlClient;
use Embed\Http\Url;
use spicyweb\embeddedassets\adapters\akamai\Extractor as AkamaiExtractor;
use spicyweb\embeddedassets\adapters\bluesky\Extractor as BlueskyExtractor;
use spicyweb\embeddedassets\adapters\default\Extractor as DefaultExtractor;
use spicyweb\embeddedassets\adapters\default\detectors\Type as TypeDetector;
use spicyweb\embeddedassets\adapters\googlemaps\Extractor as GoogleMapsExtractor;
use spicyweb\embeddedassets\adapters\openstreetmap\Extractor as OpenStreetMapExtractor;
use spicyweb\embeddedassets\adapters\pbs\Extractor as PbsExtractor;
use spicyweb\embeddedassets\adapters\sharepoint\Extractor as SharepointExtractor;
use spicyweb\embeddedassets\errors\NotWhitelistedException;
use spicyweb\embeddedassets\errors\RefreshException;
use spicyweb\embeddedassets\events\BeforeRequestEvent;
use spicyweb\embeddedassets\jobs\InstagramRefreshCheck;
use spicyweb\embeddedassets\models\EmbeddedAsset;
use spicyweb\embeddedassets\Plugin as EmbeddedAssets;
use Twig\Markup as TwigMarkup;
use yii\base\Component;
use yii\base\ErrorException;
use yii\base\Exception;
use yii\base\InvalidArgumentException;
/**
* Class Service
*
* @package spicyweb\embeddedassets
* @author Spicy Web <plugins@spicyweb.com.au>
* @author Benjamin Fleming
* @since 1.0.0
*/
class Service extends Component
{
/**
* @event BeforeRequestEvent The event that is triggered before a request is made.
* @since 4.0.0
*/
public const EVENT_BEFORE_REQUEST = 'beforeRequest';
private array $embeddedAssetData = [];
/**
* Requests embed data from a URL.
*
* @param string $url
* @param bool $checkCache Whether to check for data associated with the URL that's been stored in Craft's cache
* @return EmbeddedAsset
* @throws NotWhitelistedException if the `preventNonWhitelistedUploads` setting is enabled and the embedded asset
* provider is non-whitelisted.
*/
public function requestUrl(string $url, bool $checkCache = true): EmbeddedAsset
{
$pluginSettings = EmbeddedAssets::$plugin->getSettings();
$cacheService = Craft::$app->getCache();
$cacheKey = 'embeddedasset:' . $url;
$embeddedAsset = $checkCache ? $cacheService->get($cacheKey) : null;
if (!$embeddedAsset) {
$pluginSettings = EmbeddedAssets::$plugin->getSettings();
$array = $this->_getDataFromEmbed($url);
$embeddedAsset = $this->createEmbeddedAsset($array);
$cacheService->set($cacheKey, $embeddedAsset, $pluginSettings->cacheDuration);
}
if ($pluginSettings->preventNonWhitelistedUploads && !$embeddedAsset->getIsSafe()) {
throw new NotWhitelistedException(
Craft::t('embeddedassets', 'Tried to upload embedded asset with non-whitelisted provider'),
$embeddedAsset,
);
}
return $embeddedAsset;
}
private function _getDataFromEmbed(string $url): array
{
$pluginSettings = EmbeddedAssets::$plugin->getSettings();
$embedSettings = [
'oembed:query_parameters' => [],
];
if (!empty($pluginSettings->parameters)) {
foreach ($pluginSettings->parameters as $parameter) {
$param = $parameter['param'];
$value = $parameter['value'];
$embedSettings['oembed:query_parameters'][$param] = $value;
}
}
if ($pluginSettings->googleKey) {
$embedSettings['google:key'] = Craft::parseEnv($pluginSettings->googleKey);
}
if ($pluginSettings->facebookKey) {
$embedSettings['facebook:token'] = $embedSettings['instagram:token'] = Craft::parseEnv($pluginSettings->facebookKey);
}
$clientSettings = [];
$adapters = [
'akamaized.net' => AkamaiExtractor::class,
'bsky.app' => BlueskyExtractor::class,
'bsky.social' => BlueskyExtractor::class,
'pbs.org' => PbsExtractor::class,
'nhpbs.org' => PbsExtractor::class,
'openstreetmap.org' => OpenStreetMapExtractor::class,
'sharepoint.com' => SharepointExtractor::class,
];
if (
preg_match('/:\/\/maps.google.([a-z\.]+)\/?/', $url, $matches) ||
preg_match('/:\/\/www.google.([a-z\.]+)\/maps\/?/', $url, $matches)
) {
$adapters['google.' . $matches[1]] = GoogleMapsExtractor::class;
}
$headers = array_filter([
'Referer' => $pluginSettings->referer ? Craft::parseEnv($pluginSettings->referer) : null,
]);
// Allow other plugins/modules to modify the settings
if ($this->hasEventHandlers(self::EVENT_BEFORE_REQUEST)) {
$event = new BeforeRequestEvent(compact(
'adapters',
'clientSettings',
'embedSettings',
'headers',
'url',
));
$this->trigger(self::EVENT_BEFORE_REQUEST, $event);
$adapters = $event->adapters;
$clientSettings = $event->clientSettings;
$embedSettings = $event->embedSettings;
$headers = $event->headers;
}
// Create the Embed object, set the adapters and settings
$client = new CurlClient();
$client->setSettings($clientSettings);
$crawler = new Crawler($client);
$crawler->addDefaultHeaders($headers);
$embed = new Embed($crawler);
$factory = $embed->getExtractorFactory();
foreach ($adapters as $host => $class) {
$factory->addAdapter($host, $class);
}
$factory->setDefault(DefaultExtractor::class);
$factory->addDetector('type', TypeDetector::class);
$embed->setSettings($embedSettings);
// Now get the embed data
$embedData = $embed->get($url);
return $this->_convertFromExtractor($embedData);
}
/**
* Checks a URL against the whitelist set on the plugin settings model.
*
* @param string $url
* @return bool Whether the URL is in the whitelist.
*/
public function checkWhitelist(string $url): bool
{
$pluginSettings = EmbeddedAssets::$plugin->getSettings();
$whitelist = array_merge($pluginSettings->whitelist, $pluginSettings->extraWhitelist);
foreach ($whitelist as $whitelistUrl) {
if ($whitelistUrl) {
$pattern = explode('*', $whitelistUrl);
$pattern = array_map('preg_quote', $pattern);
$pattern = implode('[a-z][a-z0-9]*', $pattern);
$pattern = "%^(https?:)?//([a-z0-9\-]+\\.)?$pattern([:/].*)?$%";
if (preg_match($pattern, $url)) {
return true;
}
}
}
return false;
}
/**
* Retrieves an embedded asset model from an asset element, if one exists.
*
* @param Asset $asset
* @return EmbeddedAsset|null
* @throws \yii\base\InvalidConfigException
* @throws \craft\errors\AssetException
*/
public function getEmbeddedAsset(Asset $asset): ?EmbeddedAsset
{
// Embedded assets are just JSON files, so clearly if this isn't a JSON file it can't be an embedded asset
if ($asset->kind !== Asset::KIND_JSON) {
return null;
}
// If the embedded asset data has already been loaded this request, there's no need to reload it
if (isset($this->embeddedAssetData[$asset->uid])) {
return $this->embeddedAssetData[$asset->uid];
}
$pluginSettings = EmbeddedAssets::$plugin->getSettings();
$embeddedAsset = null;
try {
$decodedJson = $this->_getAssetContents($asset);
// Automatic refreshing of Instagram embedded assets every seven days, see issue #114 for why
if (
$pluginSettings->enableAutoRefresh
&& strtolower($decodedJson['providerName']) === 'instagram'
&& $this->_hasBeenWeekSince($asset->dateModified)
) {
Craft::$app->queue->push(new InstagramRefreshCheck([
'asset' => $asset,
'embeddedAssetData' => $decodedJson,
]));
}
// Make YouTube iframes use the nocookie embed URL if the relevant setting is enabled
if ($decodedJson['providerName'] === 'YouTube' && $pluginSettings->useYouTubeNoCookie) {
$decodedJson['code'] = preg_replace(
'/src="https?:\/\/www.youtube.com\/embed\/(.+)\?/',
'src="https://www.youtube-nocookie.com/embed/$1?',
$decodedJson['code']
);
}
// Make Vimeo iframes use the nocookie embed URL if the relevant setting is enabled
if ($decodedJson['providerName'] === 'Vimeo' && $pluginSettings->disableVimeoTracking) {
$oldSrc = HtmlHelper::parseTagAttributes($decodedJson['code'])['src'];
$newSrc = HtmlHelper::decode(UrlHelper::urlWithParams($oldSrc, ['dnt' => '1']));
$decodedJson['code'] = HtmlHelper::modifyTagAttributes($decodedJson['code'], ['src' => $newSrc]);
}
if (is_array($decodedJson)) {
$embeddedAsset = $this->createEmbeddedAsset($decodedJson);
$this->embeddedAssetData[$asset->uid] = $embeddedAsset;
}
} catch (\Throwable $e) {
// Ignore errors and assume it's not an embedded asset
$embeddedAsset = null;
}
return $embeddedAsset;
}
/**
* Returns whether the given array represents valid data for creating an EmbeddedAsset.
*
* @since 2.3.0
* @param array $array
* @return bool
*/
public function isValidEmbeddedAssetData(array $array): bool
{
return $this->createEmbeddedAsset($array) !== null;
}
/**
* Creates an embedded asset model from an array of property/value pairs.
* Returns the model unless the array included any properties that aren't defined on the model.
*
* @param array $array
* @return EmbeddedAsset|null
*/
public function createEmbeddedAsset(array $array): ?EmbeddedAsset
{
$isLegacy = isset($array['__embeddedasset__']);
if ($isLegacy) {
$array = $this->_convertFromLegacy($array);
}
foreach (array_keys($array) as $key) {
if (
!property_exists(EmbeddedAsset::class, $key) &&
!in_array($key, EmbeddedAsset::deprecatedProperties())
) {
return null;
}
}
if (isset($array['code'])) {
$code = $array['code'] instanceof TwigMarkup ? (string)$array['code'] : (is_string($array['code']) ? $array['code'] : '');
$array['code'] = empty($code) ? null : Template::raw($code);
}
// Attempts to extract missing dimensional properties from the embed code
$dimensions = $this->_getDimensions($array);
$array['width'] = $dimensions[0] ?: null;
$array['height'] = $dimensions[1] ?: null;
// Sets aspect ratio if it's missing
if (!isset($array['aspectRatio']) && isset($array['width']) && isset($array['height'])) {
$array['aspectRatio'] = $array['height'] / $array['width'] * 100;
}
// Correct invalid types to similar, valid types
if (isset($array['type']) && $array['type'] === 'photo') {
$array['type'] = 'image';
}
$embeddedAsset = new EmbeddedAsset($array);
return $embeddedAsset->validate() ? $embeddedAsset : null;
}
/**
* Refreshes an embedded asset with the current data from the embedded asset's URL.
*
* @param Asset $asset
* @param EmbeddedAsset|null $embeddedAsset
* @return EmbeddedAsset the refreshed embedded asset
* @throws RefreshException if the embedded asset could not be refreshed
* @since 3.0.2
*/
public function refreshEmbeddedAsset(Asset $asset, ?EmbeddedAsset $embeddedAsset = null): EmbeddedAsset
{
if ($embeddedAsset === null) {
$embeddedAsset = $this->getEmbeddedAsset($asset) ?? throw new RefreshException('Asset is not an embedded asset');
}
$assetsService = Craft::$app->getAssets();
$elementsService = Craft::$app->getElements();
$folder = $asset->getFolder();
$newEmbeddedAsset = $this->requestUrl($embeddedAsset->url, false);
// Retain deprecated property values not supported by Embed 4
foreach (['imageHeight', 'imageWidth', 'images', 'providerIcons', 'tags', 'type'] as $prop) {
$newEmbeddedAsset->{$prop} = $embeddedAsset->{$prop};
}
$newAsset = $this->createAsset($newEmbeddedAsset, $folder);
$result = $elementsService->saveElement($newAsset);
if (!$result) {
throw new RefreshException(implode('; ', $newAsset->getFirstErrors()));
}
$tempPath = $newAsset->getCopyOfFile();
$assetsService->replaceAssetFile($asset, $tempPath, $asset->filename);
$elementsService->deleteElement($newAsset);
// Replace the old cached data for the embedded asset
Craft::$app->getCache()->set(
$this->getCachedAssetKey($asset),
Json::encode($newEmbeddedAsset->jsonSerialize(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
);
return $newEmbeddedAsset;
}
/**
* Creates an asset element ready to be saved from an embedded asset model.
*
* @param EmbeddedAsset $embeddedAsset
* @param VolumeFolder $folder The folder to save the asset to.
* @return Asset
* @throws \craft\errors\AssetLogicException
* @throws \yii\base\ErrorException
* @throws \yii\base\Exception
*/
public function createAsset(EmbeddedAsset $embeddedAsset, VolumeFolder $folder): Asset
{
$assetsService = Craft::$app->getAssets();
$pluginSettings = EmbeddedAssets::$plugin->getSettings();
$tempFilePath = Assets::tempFilePath();
$fileContents = Json::encode($embeddedAsset,
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
FileHelper::writeToFile($tempFilePath, $fileContents);
// Ensure the title contains no emoji
$assetTitle = StringHelper::replaceMb4($embeddedAsset->title ?: $embeddedAsset->url, '');
$fileName = Assets::prepareAssetName($assetTitle, false);
$fileName = str_replace('.', '', $fileName);
$fileName = $fileName ?: 'embedded-asset';
$fileName = StringHelper::safeTruncate($fileName, $pluginSettings->maxFileNameLength) . '.json';
$fileName = $assetsService->getNameReplacementInFolder($fileName, $folder->id);
$asset = new Asset();
$asset->title = StringHelper::safeTruncate($assetTitle, $pluginSettings->maxAssetNameLength);
$asset->tempFilePath = $tempFilePath;
$asset->filename = $fileName;
$asset->newFolderId = $folder->id;
$asset->volumeId = $folder->volumeId;
$asset->uploaderId = Craft::$app->getUser()->getId();
$asset->avoidFilenameConflicts = true;
$asset->setScenario(Asset::SCENARIO_CREATE);
return $asset;
}
/**
* Checks an embedded asset embed code for URL's that are safe (or in other words, are whitelisted).
*
* @param EmbeddedAsset $embeddedAsset
* @return bool
*/
public function isEmbedSafe(EmbeddedAsset $embeddedAsset): bool
{
$isSafe = $this->checkWhitelist($embeddedAsset->url);
if ($isSafe) {
$dom = $this->getEmbedCode($embeddedAsset);
if ($dom) {
foreach ($dom->getElementsByTagName('iframe') as $iframeElement) {
$href = $iframeElement->getAttribute('href');
$isSafe = $isSafe && (!$href || $this->checkWhitelist($href));
}
foreach ($dom->getElementsByTagName('script') as $scriptElement) {
$src = $scriptElement->getAttribute('src');
$content = $scriptElement->textContent;
// Inline scripts are impossible to analyse for safety, so just assume they're all evil
$isSafe = $isSafe && !$content && (!$src || $this->checkWhitelist($src));
}
}
}
return $isSafe;
}
/**
* Gets the embed code as DOM, if one exists and is valid.
*
* @param EmbeddedAsset $embeddedAsset
* @return DOMDocument|null
*/
public function getEmbedCode(EmbeddedAsset $embeddedAsset): ?DOMDocument
{
$dom = null;
if ($embeddedAsset->code) {
$errors = libxml_use_internal_errors(true);
$entities = libxml_disable_entity_loader(true);
try {
$dom = new DOMDocument();
$code = "<div>$embeddedAsset->code</div>";
$isHtml = $dom->loadHTML($code, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
if (!$isHtml) {
throw new ErrorException();
}
} catch (ErrorException $e) {
// Corrupted code property, likely due to invalid HTML.
$dom = null;
} finally {
libxml_use_internal_errors($errors);
libxml_disable_entity_loader($entities);
}
}
return $dom;
}
/**
* Gets the HTML for the embedded asset.
* This method automatically checks if the embed code is safe to use. If it is, then the embed code is returned.
* Otherwise, if the embedded asset is not a "link" type and it has an image, an <img> tag is returned. Otherwise,
* an <a> link tag is returned.
*
* @param EmbeddedAsset $embeddedAsset
* @return TwigMarkup
*/
public function getEmbedHtml(EmbeddedAsset $embeddedAsset): TwigMarkup
{
if ($embeddedAsset->code && $embeddedAsset->getIsSafe()) {
$html = $embeddedAsset->code;
} elseif ($embeddedAsset->type !== 'link' && $embeddedAsset->image) {
$html = Template::raw("<img src=\"$embeddedAsset->image\" alt=\"$embeddedAsset->title\" width=\"$embeddedAsset->imageWidth\" height=\"$embeddedAsset->imageHeight\">");
} else {
$html = Template::raw("<a href=\"$embeddedAsset->url\" target=\"_blank\" rel=\"noopener\">$embeddedAsset->title</a>");
}
return $html;
}
/**
* Returns the image from an embedded asset closest to some size.
* It favours images that most minimally exceed the supplied size.
*
* @param EmbeddedAsset $embeddedAsset
* @param int $size
* @return array|null
*/
public function getImageToSize(EmbeddedAsset $embeddedAsset, int $size): ?array
{
return is_array($embeddedAsset->images) ?
$this->_getImageToSize($embeddedAsset->images, $size) : null;
}
/**
* Returns the provider icon from an embedded asset closest to some size.
* It favours icons that most minimally exceed the supplied size.
*
* @param EmbeddedAsset $embeddedAsset
* @param int $size
* @return array|null
*/
public function getProviderIconToSize(EmbeddedAsset $embeddedAsset, int $size): ?array
{
return is_array($embeddedAsset->providerIcons) ?
$this->_getImageToSize($embeddedAsset->providerIcons, $size) : null;
}
/**
* Gets the width/height of an embedded asset.
* Attempts to extract missing dimensional properties from the embed code.
*
* @param array $array
* @return array
*/
private function _getDimensions(array $array): array
{
$embeddedAsset = new EmbeddedAsset($array);
$width = $embeddedAsset->width;
$height = $embeddedAsset->height;
if (!$width || !$height) {
$dom = $this->getEmbedCode($embeddedAsset);
if ($dom) {
$iframeElement = $dom->getElementsByTagName('iframe')->item(0);
if ($iframeElement) {
$width = $iframeElement->getAttribute('width');
$height = $iframeElement->getAttribute('height');
$style = $iframeElement->getAttribute('style');
$matches = [];
$matchCount = preg_match_all('/(width|height):\s*([0-9]+(\.[0-9]+)?)px/i', $style, $matches);
for ($i = 0; $i < $matchCount; $i++) {
$styleProperty = strtolower($matches[1][$i]);
$styleValue = $matches[2][$i];
switch ($styleProperty) {
case 'width':
$width = $styleValue;
break;
case 'height':
$height = $styleValue;
break;
}
}
$width = $width ? floatval($width) : null;
$height = $height ? floatval($height) : null;
}
}
}
// TikTok sets the width and height as '100%', which Embedded Assets doesn't really like. Just remove them.
if ($width === '100%' && $height === '100%') {
$width = null;
$height = null;
}
return [$width, $height];
}
/**
* Helper method for retrieving an image closest to some size.
*
* @param array $images
* @param int $size
* @return array|null
*/
private function _getImageToSize(array $images, int $size): ?array
{
$selectedImage = null;
$selectedSize = 0;
foreach ($images as $image) {
if (is_array($image)) {
$imageWidth = isset($image['width']) && is_numeric($image['width']) ? $image['width'] : 0;
$imageHeight = isset($image['height']) && is_numeric($image['height']) ? $image['height'] : 0;
$imageSize = max($imageWidth, $imageHeight);
if (
!$selectedImage ||
($selectedSize < $size && $imageSize > $selectedSize) ||
($selectedSize > $imageSize && $imageSize > $size)
) {
$selectedImage = $image;
$selectedSize = $imageSize;
}
}
}
return $selectedImage;
}
/**
* Creates an "embedded asset ready" array from an Extractor.
*
* @param Extractor $extractor
* @return array
*/
private function _convertFromExtractor(Extractor $extractor): array
{
return [
'title' => $extractor->title,
'description' => $extractor->description,
'url' => (string)$extractor->url,
'type' => $extractor->type,
'image' => (string)$extractor->image,
'code' => Template::raw($extractor->code?->html ?: ''),
'width' => $extractor->code?->width,
'height' => $extractor->code?->height,
'aspectRatio' => $extractor->code?->ratio,
'authorName' => $extractor->authorName,
'authorUrl' => (string)$extractor->authorUrl,
'providerName' => $extractor->providerName,
'providerUrl' => (string)$extractor->providerUrl,
'providerIcon' => (string)$extractor->icon,
'publishedTime' => $extractor->publishedTime?->format('Y-m-d'),
'license' => $extractor->license,
'feeds' => array_map(function ($feed) { return (string)$feed; }, $extractor->feeds),
// New properties in Embed 4
'cms' => $extractor->cms,
'favicon' => (string)$extractor->favicon,
'keywords' => $extractor->keywords,
'language' => $extractor->language,
'languages' => array_map(function ($language) { return (string)$language; }, $extractor->languages),
'redirect' => (string)$extractor->redirect,
];
}
/**
* Creates an "embedded asset ready" array from an array matching the Craft 2 version of the plugin.
*
* @param array $legacy
* @return array
*/
private function _convertFromLegacy(array $legacy): array
{
$width = intval($legacy['width'] ?? 0);
$height = intval($legacy['height'] ?? 0);
$imageUrl = $legacy['thumbnailUrl'] ?? null;
$imageWidth = intval($legacy['thumbnailWidth'] ?? 0);
$imageHeight = intval($legacy['thumbnailHeight'] ?? 0);
return [
'title' => $legacy['title'] ?? null,
'description' => $legacy['description'] ?? null,
'url' => $legacy['url'] ?? null,
'type' => $legacy['type'] ?? null,
'images' => $imageUrl ? [
[
'url' => $imageUrl,
'width' => $imageWidth,
'height' => $imageHeight,
'size' => $imageWidth * $imageHeight,
'mime' => null,
],
] : [],
'image' => $imageUrl,
'imageWidth' => $imageWidth,
'imageHeight' => $imageHeight,
'code' => Template::raw($legacy['html'] ?? $legacy['safeHtml'] ?? ''),
'width' => $width,
'height' => $height,
'aspectRatio' => $width > 0 ? $height / $width * 100 : 0,
'authorName' => $legacy['authorName'] ?? null,
'authorUrl' => $legacy['authorUrl'] ?? null,
'providerName' => $legacy['providerName'] ?? null,
'providerUrl' => $legacy['providerUrl'] ?? null,
];
}
/**
* Gets the contents of the given asset.
*
* If a cached copy of the embedded asset exists, it will be loaded. If a cached copy does not exist, one will be
* created after loading the embedded asset from its original location.
*
* @param Asset $asset
* @return array of embedded asset data
*/
private function _getAssetContents(Asset $asset): array
{
$contents = Craft::$app->getCache()->getOrSet(
$this->getCachedAssetKey($asset),
static function() use ($asset) {
return $asset->getContents();
},
0);
try {
$contents = Json::decode($contents);
} catch (\Throwable $e) {
throw new Exception('Tried to get the contents of a non-embedded asset');
}
return $contents;
}
/**
* Gets the cached key for the given asset.
*
* @since 2.5.0
* @param Asset $asset
* @return string the embedded asset's cached key
* @throws InvalidArgumentException if $asset is an unsaved Asset
*/
public function getCachedAssetKey(Asset $asset): string
{
if ($asset->uid === null) {
throw new InvalidArgumentException('Tried to get the cached key of an unsaved embedded asset');
}
return 'embeddedassets:' . $asset->uid;
}
private function _hasBeenWeekSince(DateTimeInterface $dateModified): bool
{
return $dateModified->diff(new DateTimeImmutable())->d >= 7;
}
}