-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.php
More file actions
134 lines (114 loc) · 4.81 KB
/
Copy pathfilter.php
File metadata and controls
134 lines (114 loc) · 4.81 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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 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. If not, see <http://www.gnu.org/licenses/>.
/**
* html5embed Media plugin filtering
*
* This filter will replace any links to a video file with
* a media plugin that plays that media inline
*
* @package filter
* @subpackage html5embed
* @copyright 2004 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class filter_html5embed extends moodle_text_filter {
/** @var bool True if currently filtering trusted text */
private $trusted;
/** @var html5embed_media_renderer Media renderer */
private $mediarenderer;
/** @var string Partial regex pattern indicating possible embeddable content */
private $embedmarkers;
public function setup($page, $context) {
global $CFG;
// This only requires execution once per request.
static $jsinitialised = false;
if (empty($jsinitialised)) {
$page->requires->jquery();
$page->requires->js('/filter/html5embed/mediaelement/mediaelement-and-player.min.js');
$page->requires->js('/filter/html5embed/addmediaelement.js');
$page->requires->css('/filter/html5embed/mediaelement/mediaelementplayer.min.css');
$jsinitialised = true;
}
}
public function filter($text, array $options = array()) {
global $CFG, $PAGE;
if (!is_string($text) or empty($text)) {
// non string data can not be filtered anyway
return $text;
}
if (stripos($text, '</a>') === false) {
// Performance shortcut - if not </a> tag, nothing can match.
return $text;
}
if (!$this->mediarenderer) {
$this->mediarenderer = $PAGE->get_renderer('filter_html5embed', 'media');
$this->embedmarkers = $this->mediarenderer->get_embeddable_markers();
}
// Check SWF permissions.
$this->trusted = !empty($options['noclean']) or !empty($CFG->allowobjectembed);
// Handle all links that contain any 'embeddable' marker text (it could
// do all links, but the embeddable markers thing should make it faster
// by meaning for most links it doesn't drop into PHP code).
$newtext = preg_replace_callback($re = '~<a\s[^>]*href="([^"]*(?:' .
$this->embedmarkers . ')[^"]*)"[^>]*>([^>]*)</a>~is',
array($this, 'callback'), $text);
if (empty($newtext) or $newtext === $text) {
// error or not filtered
return $text;
}
return $newtext;
}
/**
* Replace link with embedded content, if supported.
*
* @param array $matches
* @return string
*/
private function callback(array $matches) {
global $CFG, $PAGE;
// Check if we ignore it.
if (preg_match('/class="[^"]*nomediaplugin/i', $matches[0])) {
return $matches[0];
}
// Get name.
$name = trim($matches[2]);
if (empty($name) or strpos($name, 'http') === 0) {
$name = ''; // Use default name.
}
// Split provided URL into alternatives.
$urls = html5embed_media::split_alternatives($matches[1], $width, $height);
$options = array();
// Allow SWF (or not).
if ($this->trusted) {
$options[html5embed_media::OPTION_TRUSTED] = true;
}
// We could test whether embed is possible using can_embed, but to save
// time, let's just embed it with the 'fallback to blank' option which
// does most of the same stuff anyhow.
$options[html5embed_media::OPTION_FALLBACK_TO_BLANK] = true;
// NOTE: Options are not passed through from filter because the 'embed'
// code does not recognise filter options (it's a different kind of
// option-space) as it can be used in non-filter situations.
$result = $this->mediarenderer->embed_alternatives($urls, $name, $width, $height, $options);
// If something was embedded, return it, otherwise return original.
if ($result !== '') {
return $result;
} else {
return $matches[0];
}
}
}