This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
forked from brum1975/moodle-filter_oembed
-
Notifications
You must be signed in to change notification settings - Fork 13
/
filter.php
464 lines (424 loc) · 18 KB
/
filter.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
<?php
// This file is part of Moodle-oembed-Filter
//
// Moodle-oembed-Filter is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle-oembed-Filter is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle-oembed-Filter. If not, see <http://www.gnu.org/licenses/>.
/**
* Filter for component 'filter_oembed'
*
* @package filter_oembed
* @copyright 2012 Matthew Cannings; modified 2015 by Microsoft, Inc.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* code based on the following filters...
* Screencast (Mark Schall)
* Soundcloud (Troy Williams)
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/filelib.php');
/**
* Filter for processing HTML content containing links to media from services that support the OEmbed protocol.
* The filter replaces the links with the embeddable content returned from the service via the Oembed protocol.
*
* @package filter_oembed
*/
class filter_oembed extends moodle_text_filter {
/**
* Set up the filter using settings provided in the admin settings page.
*
* @param $page
* @param $context
*/
public function setup($page, $context) {
// This only requires execution once per request.
static $jsinitialised = false;
if (get_config('filter_oembed', 'lazyload')) {
if (empty($jsinitialised)) {
$page->requires->yui_module(
'moodle-filter_oembed-lazyload',
'M.filter_oembed.init_filter_lazyload',
array(array('courseid' => 0)));
$jsinitialised = true;
}
}
if (get_config('filter_oembed', 'provider_powerbi_enabled')) {
global $PAGE;
$PAGE->requires->yui_module('moodle-filter_oembed-powerbiloader', 'M.filter_oembed.init_powerbiloader');
}
}
/**
* Filters the given HTML text, looking for links pointing to media from services that support the Oembed
* protocol and replacing them with the embeddable content returned from the protocol.
*
* @param $text HTML to be processed.
* @param $options
* @return string String containing processed HTML.
*/
public function filter($text, array $options = array()) {
global $CFG;
if (!is_string($text) or empty($text)) {
// Non string data can not be filtered anyway.
return $text;
}
// if (get_user_device_type() !== 'default'){
// no lazy video on mobile
// return $text;
// }
if (stripos($text, '</a>') === false) {
// Performance shortcut - all regexes below end with the </a> tag.
// If not present nothing can match.
return $text;
}
$newtext = $text; // We need to return the original value if regex fails!
if (get_config('filter_oembed', 'youtube')) {
$search = '/<a\s[^>]*href="((https?:\/\/(www\.)?)(youtube\.com|youtu\.be|youtube\.googleapis.com)\/(?:embed\/|v\/|watch\?v=|watch\?.+?&v=|watch\?.+?&v=)?((\w|-){11})(.*?))"(.*?)>(.*?)<\/a>/is';
$newtext = preg_replace_callback($search, 'filter_oembed_youtubecallback', $newtext);
}
if (get_config('filter_oembed', 'vimeo')) {
$search = '/<a\s[^>]*href="(https?:\/\/(www\.)?)(vimeo\.com)\/(\d+)(.*?)"(.*?)>(.*?)<\/a>/is';
$newtext = preg_replace_callback($search, 'filter_oembed_vimeocallback', $newtext);
}
if (get_config('filter_oembed', 'slideshare')) {
$search = '/<a\s[^>]*href="(https?:\/\/(www\.)?)(slideshare\.net)\/(.*?)"(.*?)>(.*?)<\/a>/is';
$newtext = preg_replace_callback($search, 'filter_oembed_slidesharecallback', $newtext);
}
if (get_config('filter_oembed', 'officemix')) {
$search = '/<a\s[^>]*href="(https?:\/\/(www\.)?)(mix\.office\.com)\/(.*?)"(.*?)>(.*?)<\/a>/is';
$newtext = preg_replace_callback($search, 'filter_oembed_officemixcallback', $newtext);
}
if (get_config('filter_oembed', 'issuu')) {
$search = '/<a\s[^>]*href="(https?:\/\/(www\.)?)(issuu\.com)\/(.*?)"(.*?)>(.*?)<\/a>/is';
$newtext = preg_replace_callback($search, 'filter_oembed_issuucallback', $newtext);
}
if (get_config('filter_oembed', 'soundcloud')) {
$search = '/<a\s[^>]*href="(https?:\/\/(www\.)?)(soundcloud\.com)\/(.*?)"(.*?)>(.*?)<\/a>/is';
$newtext = preg_replace_callback($search, 'filter_oembed_soundcloudcallback', $newtext);
}
if (get_config('filter_oembed', 'ted')) {
$search = '/<a\s[^>]*href="(https?:\/\/(www\.)?)(ted\.com)\/talks\/(.*?)"(.*?)>(.*?)<\/a>/is';
$newtext = preg_replace_callback($search, 'filter_oembed_tedcallback', $newtext);
}
if (get_config('filter_oembed', 'pollev')) {
$search = '/<a\s[^>]*href="(https?:\/\/(www\.)?)(polleverywhere\.com)\/(polls|multiple_choice_polls|free_text_polls)\/(.*?)"(.*?)>(.*?)<\/a>/is';
$newtext = preg_replace_callback($search, 'filter_oembed_pollevcallback', $newtext);
}
$odburl = get_config('local_o365', 'odburl');
if (get_config('filter_oembed', 'o365video') && !empty($odburl)) {
$odburl = preg_replace('/^https?:\/\//', '', $odburl);
$odburl = preg_replace('/\/.*/', '', $odburl);
$trimedurl = preg_replace("/-my/", "", $odburl);
$search = '/<a\s[^>]*href="(https?:\/\/)('.$odburl.'|'.$trimedurl.')\/(.*?)"(.*?)>(.*?)<\/a>/is';
$newtext = preg_replace_callback($search, 'filter_oembed_o365videocallback', $newtext);
}
if (get_config('filter_oembed', 'sway')) {
$search = '/<a\s[^>]*href="(https?:\/\/(www\.)?)(sway\.com)\/(.*?)"(.*?)>(.*?)<\/a>/is';
$newtext = preg_replace_callback($search, 'filter_oembed_swaycallback', $newtext);
}
// New method for embed providers.
$providers = static::get_supported_providers();
$filterconfig = get_config('filter_oembed');
foreach ($providers as $provider) {
$enabledkey = 'provider_'.$provider.'_enabled';
if (!empty($filterconfig->$enabledkey)) {
$providerclass = '\filter_oembed\provider\\'.$provider;
if (class_exists($providerclass)) {
$provider = new $providerclass();
$newtext = $provider->filter($newtext);
}
}
}
if (empty($newtext) or $newtext === $text) {
// Error or not filtered.
unset($newtext);
return $text;
}
return $newtext;
}
/**
* Return list of supported providers.
*
* @return array Array of supported providers.
*/
public static function get_supported_providers() {
return [
'docsdotcom', 'powerbi', 'officeforms'
];
}
}
/**
* Looks for links pointing to Youtube content and processes them.
*
* @param $link HTML tag containing a link
* @return string HTML content after processing.
*/
function filter_oembed_youtubecallback($link) {
global $CFG;
$url = "http://www.youtube.com/oembed?url=".urlencode(trim($link[1]))."&format=json";
$jsonret = filter_oembed_curlcall($url);
return filter_oembed_vidembed($jsonret, trim($link[7]));
}
/**
* Looks for links pointing to Vimeo content and processes them.
*
* @param $link HTML tag containing a link
* @return string HTML content after processing.
*/
function filter_oembed_vimeocallback($link) {
global $CFG;
$url = "http://vimeo.com/api/oembed.json?url=".trim($link[1]).trim($link[2]).trim($link[3]).'/'.trim($link[4]).'&maxwidth=480&maxheight=270';
$jsonret = filter_oembed_curlcall($url);
return filter_oembed_vidembed($jsonret);
}
/**
* Looks for links pointing to TED content and processes them.
*
* @param $link HTML tag containing a link
* @return string HTML content after processing.
*/
function filter_oembed_tedcallback($link) {
global $CFG;
$url = "http://www.ted.com/services/v1/oembed.json?url=".trim($link[1]).trim($link[3]).'/talks/'.trim($link[4]).'&maxwidth=480&maxheight=270';
$jsonret = filter_oembed_curlcall($url);
return filter_oembed_vidembed($jsonret);
}
/**
* Looks for links pointing to SlideShare content and processes them.
*
* @param $link HTML tag containing a link
* @return string HTML content after processing.
*/
function filter_oembed_slidesharecallback($link) {
global $CFG;
$url = "http://www.slideshare.net/api/oembed/2?url=".trim($link[1]).trim($link[3]).'/'.trim($link[4])."&format=json&maxwidth=480&maxheight=270";
$json = filter_oembed_curlcall($url);
return $json === null ? '<h3>'. get_string('connection_error', 'filter_oembed') .'</h3>' : $json['html'];
}
/**
* Looks for links pointing to Microsoft Office Mix content and processes them.
*
* @param $link HTML tag containing a link
* @return string HTML content after processing.
*/
function filter_oembed_officemixcallback($link) {
global $CFG;
$url = "https://mix.office.com/oembed/?url=".trim($link[1]).trim($link[2]).trim($link[3]).'/'.trim($link[4]);
$json = filter_oembed_curlcall($url);
if($json === null){
return '<h3>'. get_string('connection_error', 'filter_oembed') .'</h3>';
}
// Increase the height and width of iframe.
$json['html'] = str_replace('width="348"', 'width="480"', $json['html']);
$json['html'] = str_replace('height="245"', 'height="320"', $json['html']);
$json['html'] = str_replace('height="310"', 'height="410"', $json['html']);
$json['html'] = str_replace('height="267"', 'height="350"', $json['html']);
return filter_oembed_vidembed($json);
}
/**
* Looks for links pointing to PollEverywhere content and processes them.
*
* @param $link HTML tag containing a link
* @return string HTML content after processing.
*/
function filter_oembed_pollevcallback($link) {
global $CFG;
$url = "http://www.polleverywhere.com/services/oembed?url=".trim($link[1]).trim($link[3]).'/'.trim($link[4]).'/'.trim($link[5])."&format=json&maxwidth=480&maxheight=270";
$json = filter_oembed_curlcall($url);
return $json === null ? '<h3>'. get_string('connection_error', 'filter_oembed') .'</h3>' : $json['html'];
}
/**
* Looks for links pointing to Issuu content and processes them.
*
* @param $link HTML tag containing a link
* @return string HTML content after processing.
*/
function filter_oembed_issuucallback($link) {
global $CFG;
$url = "http://issuu.com/oembed?url=".trim($link[1]).trim($link[3]).'/'.trim($link[4])."&format=json";
$json = filter_oembed_curlcall($url);
return $json === null ? '<h3>'. get_string('connection_error', 'filter_oembed') .'</h3>' : $json['html'];
}
/**
* Looks for links pointing to SoundCloud content and processes them.
*
* @param $link HTML tag containing a link
* @return string HTML content after processing.
*/
function filter_oembed_soundcloudcallback($link) {
global $CFG;
$url = "http://soundcloud.com/oembed?url=".trim($link[1]).trim($link[3]).'/'.trim($link[4])."&format=json&maxwidth=480&maxheight=270'";
$json = filter_oembed_curlcall($url);
return filter_oembed_vidembed($json);
}
/**
* Looks for links pointing to Office 365 Video content and processes them.
*
* @param $link HTML tag containing a link
* @return string HTML content after processing.
*/
function filter_oembed_o365videocallback($link) {
if (empty($link[3])) {
return $link[0];
}
$link[3] = preg_replace("/&/", "&", $link[3]);
$values = array();
parse_str($link[3], $values);
if (empty($values['chid']) || empty($values['vid'])) {
return $link[0];
}
if (!\local_o365\rest\sharepoint::is_configured()) {
\local_o365\utils::debug('filter_oembed share point is not configured', 'filter_oembed_o365videocallback');
return $link[0];
}
try {
$spresource = \local_o365\rest\sharepoint::get_resource();
if (!empty($spresource)) {
$httpclient = new \local_o365\httpclient();
$clientdata = \local_o365\oauth2\clientdata::instance_from_oidc();
$sptoken = \local_o365\oauth2\systemtoken::instance(null, $spresource, $clientdata, $httpclient);
if (!empty($sptoken)) {
$sharepoint = new \local_o365\rest\sharepoint($sptoken, $httpclient);
// Retrieve api url for video service.
$url = $sharepoint->videoservice_discover();
if (!empty($url)) {
$sharepoint->override_resource($url);
$width = 640;
if (!empty($values['width'])) {
$width = $values['width'];
}
$height = 360;
if (!empty($values['height'])) {
$height = $values['height'];
}
// Retrieve embed code.
return $sharepoint->get_video_embed_code($values['chid'], $values['vid'], $width, $height);
}
}
}
} catch (\Exception $e) {
\local_o365\utils::debug('filter_oembed share point execption: '.$e->getMessage(), 'filter_oembed_o365videocallback', $e);
}
return $link[0];
}
/**
* Looks for links pointing to sway.com content and processes them.
*
* @param $link HTML tag containing a link
* @return string HTML content after processing.
*/
function filter_oembed_swaycallback($link) {
global $CFG;
$width = 500;
$height = 760;
$link[4] = preg_replace("/&/", "&", $link[4]);
$id = preg_replace("/^(.*)(\?(.*)?)/", "$1", $link[4]);
$url = "https://www.sway.com/s/".trim($id)."/embed";
// Check for optional width and height passed as query string.
if (preg_match("/width/", $link[4])) {
$query = array();
parse_str(preg_replace("/^(.*)\?/", "", $link[4]), $query);
if (!empty($query['width'])) {
$width = $query['width'];
}
if (!empty($query['height'])) {
$height = $query['height'];
}
}
$options = array(
'class' => 'oembed_sway',
'width' => $width.'px',
'height' => $height.'px',
'src' => $url,
'frameborder' => '0',
'marginwidth' => '0',
'scrolling' => 'no',
'style' => 'border: none; max-width:100%; max-height:100vh',
'allowfullscreen' => '',
'webkitallowfullscreen' => '',
'msallowfullscreen' => '',
);
return html_writer::tag('iframe', '', $options);
}
/**
* Makes the OEmbed request to the service that supports the protocol.
*
* @param $url URL for the Oembed request
* @return mixed|null|string The HTTP response object from the OEmbed request.
*/
function filter_oembed_curlcall($url) {
static $cache;
if (!isset($cache)) {
$cache = cache::make('filter_oembed', 'embeddata');
}
if ($ret = $cache->get(md5($url))) {
return json_decode($ret, true);
}
$curl = new \curl();
$ret = $curl->get($url);
// Check if curl call fails.
if ($curl->errno != CURLE_OK) {
// Check if error is due to network connection.
if (in_array($curl->errno, [6, 7, 28])) {
// Try curl call up to 3 times.
usleep(50000);
$retryno = (!is_int($retryno)) ? 0 : $retryno+1;
if ($retryno < 3) {
return $this->getoembeddata($url, $retryno);
} else {
return null;
}
} else {
return null;
}
}
$cache->set(md5($url), $ret);
$result = json_decode($ret, true);
return $result;
}
/**
* Return the HTML content to be embedded given the response from the OEmbed request.
* This method returns the thumbnail image if we lazy loading is enabled. Ogtherwise it returns the
* embeddable HTML returned from the OEmbed request. An error message is returned if there was an error during
* the request.
*
* @param array $json Response object returned from the OEmbed request.
* @param string $params Additional parameters to include in the embed URL.
* @return string The HTML content to be embedded in the page.
*/
function filter_oembed_vidembed($json, $params = '') {
if ($json === null) {
return '<h3>'. get_string('connection_error', 'filter_oembed') .'</h3>';
}
$embed = $json['html'];
if ($params != ''){
$embed = str_replace('?feature=oembed', '?feature=oembed'.htmlspecialchars($params), $embed );
}
if (get_config('filter_oembed', 'lazyload')) {
$embed = htmlspecialchars($embed);
$dom = new DOMDocument();
// To surpress the loadHTML Warnings.
libxml_use_internal_errors(true);
$dom->loadHTML($json['html']);
libxml_use_internal_errors(false);
// Get height and width of iframe.
$height = $dom->getElementsByTagName('iframe')->item(0)->getAttribute('height');
$width = $dom->getElementsByTagName('iframe')->item(0)->getAttribute('width');
$embedcode = '<a class="lvoembed lvvideo" data-embed="'.$embed.'"';
$embedcode .= 'href="#" data-height="'. $height .'" data-width="'. $width .'"><div class="filter_oembed_lazyvideo_container">';
$embedcode .= '<img class="filter_oembed_lazyvideo_placeholder" src="'.$json['thumbnail_url'].'" />';
$embedcode .= '<div class="filter_oembed_lazyvideo_title"><div class="filter_oembed_lazyvideo_text">'.$json['title'].'</div></div>';
$embedcode .= '<span class="filter_oembed_lazyvideo_playbutton"></span>';
$embedcode .= '</div></a>';
} else {
$embedcode = $embed;
}
return $embedcode;
}