Merge branch 'MDL-78525/402' of https://github.com/skodak/moodle into MOODLE_402_STABLE
[moodle.git] / media / classes / player_native.php
blob869c448a5f6bec921e7f993208fbfdbe1f6c03ca
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.
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/>.
17 /**
18 * Base class for players which return native HTML5 <video> or <audio> tags
20 * @package core_media
21 * @copyright 2016 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Base class for players which return native HTML5 <video> or <audio> tags
30 * @package core_media
31 * @copyright 2016 Marina Glancy
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 abstract class core_media_player_native extends core_media_player {
35 /**
36 * Extracts a value for an attribute
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
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);
50 return null;
53 /**
54 * Removes an attribute from the media tags
56 * @param string $tag html tag which properties are extracted, for example "<video ...>....</video>"
57 * @param string|array $attrname
58 * @return string
60 public static function remove_attributes($tag, $attrname) {
61 if (is_array($attrname)) {
62 $attrname = join('|', $attrname);
64 while (preg_match('/^(<[^>]*\b)(' . $attrname . ')=".*?"(.*)$/is', $tag, $matches)) {
65 $tag = $matches[1] . $matches[3];
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];
71 return $tag;
74 /**
75 * Adds attributes to the media tags
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
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;
86 $rv = $matches[1];
87 foreach ($attributes as $name => $value) {
88 $rv .= " $name=\"".s($value).'"';
90 $rv .= $matches[2];
91 return $rv;
94 /**
95 * Replaces all embedded <source> tags and src attribute
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
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];
107 return $tag;
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']);
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;
130 return $result;