1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Base class for players which return native HTML5 <video> or <audio> tags
|
|
|
19 |
*
|
|
|
20 |
* @package core_media
|
|
|
21 |
* @copyright 2016 Marina Glancy
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
defined('MOODLE_INTERNAL') || die();
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Base class for players which return native HTML5 <video> or <audio> tags
|
|
|
29 |
*
|
|
|
30 |
* @package core_media
|
|
|
31 |
* @copyright 2016 Marina Glancy
|
|
|
32 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
33 |
*/
|
|
|
34 |
abstract class core_media_player_native extends core_media_player {
|
|
|
35 |
/**
|
|
|
36 |
* Extracts a value for an attribute
|
|
|
37 |
*
|
|
|
38 |
* @param string $tag html tag which properties are extracted, for example "<video ...>....</video>"
|
|
|
39 |
* @param string $attrname name of the attribute we are looking for
|
|
|
40 |
* @param string $type one of PARAM_* constants to clean the attribute value
|
|
|
41 |
* @return string|null
|
|
|
42 |
*/
|
|
|
43 |
public static function get_attribute($tag, $attrname, $type = PARAM_RAW) {
|
|
|
44 |
if (preg_match('/^<[^>]*\b' . $attrname . '="(.*?)"/is', $tag, $matches)) {
|
|
|
45 |
return clean_param(htmlspecialchars_decode($matches[1], ENT_COMPAT), $type);
|
|
|
46 |
} else if (preg_match('~^<[^>]*\b' . $attrname . '[ />]"~is', $tag, $matches)) {
|
|
|
47 |
// Some attributes may not have value, for example this is valid: <video controls>.
|
|
|
48 |
return clean_param("true", $type);
|
|
|
49 |
}
|
|
|
50 |
return null;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Removes an attribute from the media tags
|
|
|
55 |
*
|
|
|
56 |
* @param string $tag html tag which properties are extracted, for example "<video ...>....</video>"
|
|
|
57 |
* @param string|array $attrname
|
|
|
58 |
* @return string
|
|
|
59 |
*/
|
|
|
60 |
public static function remove_attributes($tag, $attrname) {
|
|
|
61 |
if (is_array($attrname)) {
|
|
|
62 |
$attrname = join('|', $attrname);
|
|
|
63 |
}
|
|
|
64 |
while (preg_match('/^(<[^>]*\b)(' . $attrname . ')=".*?"(.*)$/is', $tag, $matches)) {
|
|
|
65 |
$tag = $matches[1] . $matches[3];
|
|
|
66 |
}
|
|
|
67 |
while (preg_match('~^(<[^>]*\b)(' . $attrname . ')([ />].*)$~is', $tag, $matches)) {
|
|
|
68 |
// Some attributes may not have value, for example: <video controls>.
|
|
|
69 |
$tag = $matches[1] . $matches[3];
|
|
|
70 |
}
|
|
|
71 |
return $tag;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Adds attributes to the media tags
|
|
|
76 |
*
|
|
|
77 |
* @param string $tag html tag which properties are extracted, for example "<video ...>....</video>"
|
|
|
78 |
* @param array $attributes key-value pairs of attributes to be added
|
|
|
79 |
* @return string
|
|
|
80 |
*/
|
|
|
81 |
public static function add_attributes($tag, $attributes) {
|
|
|
82 |
$tag = self::remove_attributes($tag, array_keys($attributes));
|
|
|
83 |
if (!preg_match('/^(<.*?)(>.*)$/s', $tag, $matches)) {
|
|
|
84 |
return $tag;
|
|
|
85 |
}
|
|
|
86 |
$rv = $matches[1];
|
|
|
87 |
foreach ($attributes as $name => $value) {
|
|
|
88 |
$rv .= " $name=\"".s($value).'"';
|
|
|
89 |
}
|
|
|
90 |
$rv .= $matches[2];
|
|
|
91 |
return $rv;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Replaces all embedded <source> tags and src attribute
|
|
|
96 |
*
|
|
|
97 |
* @param string $tag html tag which properties are extracted, for example "<video ...>....</video>"
|
|
|
98 |
* @param string $sources replacement string (expected to contain <source> tags)
|
|
|
99 |
* @return string
|
|
|
100 |
*/
|
|
|
101 |
public static function replace_sources($tag, $sources) {
|
|
|
102 |
$tag = self::remove_attributes($tag, 'src');
|
|
|
103 |
$tag = preg_replace(['~</?source\b[^>]*>~i'], '', $tag);
|
|
|
104 |
if (preg_match('/^(<.*?>)([^\0]*)$/ms', $tag, $matches)) {
|
|
|
105 |
$tag = $matches[1].$sources.$matches[2];
|
|
|
106 |
}
|
|
|
107 |
return $tag;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
public function get_supported_extensions() {
|
|
|
111 |
global $CFG;
|
|
|
112 |
require_once($CFG->libdir . '/filelib.php');
|
|
|
113 |
return file_get_typegroup('extension', ['html_video', 'html_audio']);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
public function list_supported_urls(array $urls, array $options = array()) {
|
|
|
117 |
$extensions = $this->get_supported_extensions();
|
|
|
118 |
$result = array();
|
|
|
119 |
foreach ($urls as $url) {
|
|
|
120 |
$ext = core_media_manager::instance()->get_extension($url);
|
|
|
121 |
if (in_array('.' . $ext, $extensions) && core_useragent::supports_html5($ext)) {
|
|
|
122 |
// Unfortunately html5 video does not handle fallback properly.
|
|
|
123 |
// https://www.w3.org/Bugs/Public/show_bug.cgi?id=10975
|
|
|
124 |
// That means we need to do browser detect and not use html5 on
|
|
|
125 |
// browsers which do not support the given type, otherwise users
|
|
|
126 |
// will not even see the fallback link.
|
|
|
127 |
$result[] = $url;
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
return $result;
|
|
|
131 |
}
|
|
|
132 |
}
|