-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed.php
More file actions
386 lines (317 loc) · 9.89 KB
/
embed.php
File metadata and controls
386 lines (317 loc) · 9.89 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
<?php
namespace Plugins\embed;
use \Typemill\Plugin;
class embed extends Plugin
{
protected $pluginData;
protected $services = [
"youtube" => [
'url' => "https://www.youtube.com/watch",
'privacy' => "https://policies.google.com/privacy",
'csp' => ["https://www.youtube-nocookie.com/", "https://www.youtube.com"]
],
"gmaps" => [
'url' => "https://www.google.com/maps/place",
'privacy' => "https://policies.google.com/privacy",
'csp' => ["https://maps.google.com/", "https://www.google.com/maps/"]
],
"vimeo" => [
'url' => "https://vimeo.com/",
'privacy' => "https://vimeo.com/privacy",
'csp' => ["https://player.vimeo.com/"]
],
"soundcloud" => [
'url' => "https://soundcloud.com/",
'privacy' => "https://soundcloud.com/pages/privacy",
'csp' => ["https://w.soundcloud.com/"]
],
];
protected $error;
protected $embedFound = false;
# you can add a licence check here
public static function setPremiumLicence()
{
return 'MAKER';
}
public static function getSubscribedEvents()
{
return [
'onCspLoaded' => 'onCspLoaded',
'onShortcodeFound' => 'onShortcodeFound',
'onHtmlLoaded' => 'onHtmlLoaded',
];
}
public function onCspLoaded($csp)
{
$data = $csp->getData();
$pluginSettings = $this->getPluginSettings();
$activeServices = $pluginSettings['services'] ?? false;
if($activeServices)
{
foreach($this->services as $serviceName => $serviceData)
{
if(in_array($serviceName, $activeServices))
{
foreach($serviceData['csp'] as $domain)
{
$data[] = $domain;
}
}
}
}
$csp->setData($data);
}
public function onShortcodeFound($shortcode)
{
$shortcodeArray = $shortcode->getData();
# register the shortcode
if(is_array($shortcodeArray) && $shortcodeArray['name'] == 'registershortcode')
{
$shortcodeArray['data']['embed'] = [ 'url' => '', 'parameter' => 'example=600;useforheight=350'];
$shortcode->setData($shortcodeArray);
}
if (is_array($shortcodeArray) && $shortcodeArray['name'] == 'embed')
{
$shortcode->stopPropagation();
$url = $shortcodeArray['params']['url'] ?? false;
if(!$url OR $url == '')
{
$html = "Please add a valid embed url.";
}
else
{
$pluginSettings = $this->getPluginSettings();
$serviceName = $this->getServiceFromUrl($url);
if(!$serviceName)
{
$html = "The url seems to be wrong and we did not find a way to embed. Please always use the url from the browser bar if you want to embed something. See supported services and example urls here:";
}
elseif(!isset($pluginSettings['services']))
{
$html = "No services are activated in the plugin settings.";
}
elseif(!in_array($serviceName, $pluginSettings['services']))
{
$html = "Please check if the service " . $serviceName . " is activated in the plugin settings.";
}
else
{
$methodName = $serviceName . "Iframe";
if(method_exists($this, $methodName))
{
$parameter = $shortcodeArray['params']['parameter'] ?? false;
$parameter = $this->sanitizeParameter($parameter);
$iframe = $this->$methodName($url, $parameter);
if($iframe)
{
$html = '<div
class="embedWrapper"
data-service="' . $serviceName . '"
data-privacylink="' . $this->services[$serviceName]['privacy'] . '"
style="margin:auto">' . $iframe .
'</div>';
$this->embedFound = true;
}
else
{
$html = $this->error;
}
}
else
{
$html = "Method $methodName does not exist.";
}
}
}
$shortcode->setData($html);
}
}
public function onHtmlLoaded($html)
{
$htmlString = $html->getData();
$pluginSettings = $this->getPluginSettings();
$privacytext = $pluginSettings['privacytext'] ?? "If you want to load content from this service, use the button below and read the ";
$privacylabel = $pluginSettings['privacylabel'] ?? "privacy policy";
$buttonlabel = $pluginSettings['buttonlabel'] ?? "load content from this service";
if($this->embedFound && isset($pluginSettings['privacybox']) && $pluginSettings['privacybox'])
{
$this->addJS('/embed/js/script.js');
$this->addCSS('/embed/css/style.css');
$htmlString .= '<div
id = "embedPrivacyConsent"
style = "display:none"
data-privacytext = "' . $privacytext . '"
data-privacylabel = "' . $privacylabel . '"
data-buttonlabel = "' . $buttonlabel . '"
>';
}
$html->setData($htmlString);
}
protected function getServiceFromUrl($url)
{
# Cleanup google international
if (strpos($url, 'google') !== false)
{
$url = preg_replace('/\.google\.\w{2,3}(\/|$)/', '.google.com$1', $url);
}
foreach($this->services as $serviceName => $serviceData)
{
if(strpos($url, $serviceData['url']) === 0)
{
return $serviceName;
}
}
return false;
}
protected function youtubeIframe(string $url, array $params)
{
preg_match('/(?:\?|&)v=([a-zA-Z0-9_-]+)/', $url, $matches);
if (isset($matches[1]))
{
$id = $matches[1];
}
else
{
$this->error = 'We could not find a valid id in the url';
return false;
}
$src = 'https://www.youtube-nocookie.com/embed/' . $id;
$width = $params['width'] ?? '560';
$height = $params['height'] ?? '315';
$html = '<iframe
width = "' . $width . '"
height = "' . $height . '"
src = "' . $src . '"
title = "YouTube video player"
frameborder = "0"
allow = "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
referrerpolicy = "strict-origin-when-cross-origin"
allowfullscreen>
</iframe>';
return $html;
}
protected function gmapsIframe(string $url, array $params)
{
$urlParts = parse_url($url);
if (!isset($urlParts['path']))
{
$this->error = 'We could not find a valid path in the url';
return false;
}
preg_match('/@([-0-9.]+),([-0-9.]+),/', $urlParts['path'], $matches);
if (!isset($matches[1]) OR !isset($matches[1]) )
{
$this->error = 'We could not find valid coordinates (latitude or longitude) in the url';
return false;
}
$latitude = $matches[1];
$longitude = $matches[2];
preg_match('/\/place\/([^\/?]+)/', $urlParts['path'], $addressMatches);
$address = urldecode($addressMatches[1]);
if (!isset($addressMatches[1]))
{
$this->error = 'We could not find a valid address in the url, this plugin only supports map-urls for places with a valid address';
}
$width = $params['width'] ?? '100%';
$height = $params['height'] ?? '450';
$zoom = $params['zoom'] ?? '17';
$src = 'https://maps.google.com/maps?'
. 'width=' . $width
. '&height=' . $height
. '&hl=en'
. '&coord=' . $latitude . ',' . $longitude
. '&q=' . urlencode($address)
. '&z=' . $zoom
. '&iwloc=B&output=embed';
$html = '<iframe
width = "' . $width . '"
height = "' . $height . '"
src = "' . $src . '"
frameborder = "0"
scrolling = "no"
marginheight = "0"
marginwidth = "0">
</iframe>';
return $html;
}
protected function vimeoIframe(string $url, array $params)
{
preg_match('/vimeo\.com\/(\d+)(?:\/([\w\d]+))?/', $url, $matches);
if (!isset($matches[1])) {
$this->error = 'We could not find a valid ID in the Vimeo URL';
return false;
}
$id = $matches[1];
$hash = $matches[2] ?? ''; // Captures the privacy hash if present
$width = $params['width'] ?? '640';
$height = $params['height'] ?? '360';
$responsive = $params['responsive'] ?? 'false';
// Try oEmbed first
$oembedUrl = "https://vimeo.com/api/oembed.json?url=https://vimeo.com/{$id}"
. ($hash ? "/$hash" : '')
. '&width=' . $width
. '&height=' . $height
. '&responsive=' . $responsive;
$response = @file_get_contents($oembedUrl);
if ($response) {
$data = json_decode($response, true);
if (isset($data['html'])) {
return str_replace(['width="', 'height="'], ["width=\"$width\" ", "height=\"$height\" "], $data['html']);
}
}
// Fallback: Construct the correct embed URL
$src = "https://player.vimeo.com/video/{$id}" . ($hash ? "?h={$hash}" : "?dnt=1&title=0&byline=0&portrait=0");
return '<iframe
src="' . $src . '"
width="' . $width . '"
height="' . $height . '"
frameborder="0"
allow="autoplay; fullscreen; picture-in-picture"
allowfullscreen>
</iframe>';
}
protected function soundcloudIframe(string $url, array $params)
{
$oembedUrl = 'https://soundcloud.com/oembed?url=' . urlencode($url) . '&format=json';
$response = file_get_contents($oembedUrl);
if($response === false)
{
if (!empty($http_response_header))
{
list($version, $status_code, $msg) = explode(' ', $http_response_header[0], 3);
$this->error = 'We got an error from soundcloud: ' . $status_code . ' ' . $msg;
}
else
{
$this->error = 'We did not get any response from soundcloud, the request might be blocked by your server.';
}
return false;
}
$oembedData = json_decode($response, true);
if(isset($oembedData['html']))
{
return $oembedData['html'];
}
$this->error = 'We could not find anything for the soundcloud url.';
return false;
}
private function sanitizeParameter(string $parameter)
{
if($parameter)
{
$sanitizedString = preg_replace('/[^a-zA-Z0-9=;]/', '', $parameter);
$paramsArray = explode(';', $sanitizedString);
$params = [];
foreach ($paramsArray as $param)
{
if (strpos($param, '=') !== false)
{
list($key, $value) = explode('=', $param, 2);
$params[$key] = $value;
}
}
return $params;
}
return [];
}
}