mirror of
https://github.com/moodle/moodle.git
synced 2025-08-09 02:46:40 +02:00
MDL-38158 core_media: Convert media players to new plugin type
AMOS BEGIN MOV [siteyoutube,core_media],[pluginname,media_youtube] MOV [siteyoutube_desc,core_media],[pluginname_help,media_youtube] MOV [sitevimeo,core_media],[pluginname,media_vimeo] MOV [sitevimeo_desc,core_media],[pluginname_help,media_vimeo] MOV [html5audio,core_media],[pluginname,media_html5audio] MOV [html5audio_desc,core_media],[pluginname_help,media_html5audio] MOV [html5video,core_media],[pluginname,media_html5video] MOV [html5video_desc,core_media],[pluginname_help,media_html5video] MOV [flashanimation,core_media],[pluginname,media_swf] MOV [flashanimation_desc,core_media],[pluginname_help,media_swf] AMOS END
This commit is contained in:
parent
3c73b26c4b
commit
fab11235d8
76 changed files with 3524 additions and 4406 deletions
466
media/classes/manager.php
Normal file
466
media/classes/manager.php
Normal file
|
@ -0,0 +1,466 @@
|
|||
<?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/>.
|
||||
|
||||
/**
|
||||
* Manager for media files
|
||||
*
|
||||
* @package core_media
|
||||
* @copyright 2016 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Manager for media files.
|
||||
*
|
||||
* Used in file resources, media filter, and any other places that need to
|
||||
* output embedded media.
|
||||
*
|
||||
* Usage:
|
||||
* $manager = core_media_manager::instance();
|
||||
*
|
||||
*
|
||||
* @package core_media
|
||||
* @copyright 2016 Marina Glancy
|
||||
* @author 2011 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class core_media_manager {
|
||||
/**
|
||||
* Option: Disable text link fallback.
|
||||
*
|
||||
* Use this option if you are going to print a visible link anyway so it is
|
||||
* pointless to have one as fallback.
|
||||
*
|
||||
* To enable, set value to true.
|
||||
*/
|
||||
const OPTION_NO_LINK = 'nolink';
|
||||
|
||||
/**
|
||||
* Option: When embedding, if there is no matching embed, do not use the
|
||||
* default link fallback player; instead return blank.
|
||||
*
|
||||
* This is different from OPTION_NO_LINK because this option still uses the
|
||||
* fallback link if there is some kind of embedding. Use this option if you
|
||||
* are going to check if the return value is blank and handle it specially.
|
||||
*
|
||||
* To enable, set value to true.
|
||||
*/
|
||||
const OPTION_FALLBACK_TO_BLANK = 'embedorblank';
|
||||
|
||||
/**
|
||||
* Option: Enable players which are only suitable for use when we trust the
|
||||
* user who embedded the content.
|
||||
*
|
||||
* At present, this option enables the SWF player.
|
||||
*
|
||||
* To enable, set value to true.
|
||||
*/
|
||||
const OPTION_TRUSTED = 'trusted';
|
||||
|
||||
/**
|
||||
* Option: Put a div around the output (if not blank) so that it displays
|
||||
* as a block using the 'resourcecontent' CSS class.
|
||||
*
|
||||
* To enable, set value to true.
|
||||
*/
|
||||
const OPTION_BLOCK = 'block';
|
||||
|
||||
/**
|
||||
* Option: When the request for media players came from a text filter this option will contain the
|
||||
* original HTML snippet, usually one of the tags: <a> or <video> or <audio>
|
||||
*
|
||||
* Players that support other HTML5 features such as tracks may find them in this option.
|
||||
*/
|
||||
const OPTION_ORIGINAL_TEXT = 'originaltext';
|
||||
|
||||
/** @var array Array of available 'player' objects */
|
||||
private $players;
|
||||
|
||||
/** @var string Regex pattern for links which may contain embeddable content */
|
||||
private $embeddablemarkers;
|
||||
|
||||
/** @var core_media_manager caches a singleton instance */
|
||||
static protected $instance;
|
||||
|
||||
/**
|
||||
* Returns a singleton instance of a manager
|
||||
*
|
||||
* @return core_media_manager
|
||||
*/
|
||||
public static function instance() {
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets cached singleton instance. To be used after $CFG->media_plugins_sortorder is modified
|
||||
*/
|
||||
public static function reset_caches() {
|
||||
self::$instance = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains the list of core_media_player objects currently in use to render
|
||||
* items.
|
||||
*
|
||||
* The list is in rank order (highest first) and does not include players
|
||||
* which are disabled.
|
||||
*
|
||||
* @return core_media_player[] Array of core_media_player objects in rank order
|
||||
*/
|
||||
protected function get_players() {
|
||||
// Save time by only building the list once.
|
||||
if (!$this->players) {
|
||||
// Get raw list of players.
|
||||
$allplayers = $this->get_players_raw();
|
||||
$sortorder = \core\plugininfo\media::get_enabled_plugins();
|
||||
|
||||
$this->players = [];
|
||||
foreach ($sortorder as $key) {
|
||||
if (array_key_exists($key, $allplayers)) {
|
||||
$this->players[] = $allplayers[$key];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->players;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains a raw list of player objects that includes objects regardless
|
||||
* of whether they are disabled or not, and without sorting.
|
||||
*
|
||||
* You can override this in a subclass if you need to add additional_
|
||||
* players.
|
||||
*
|
||||
* The return array is be indexed by player name to make it easier to
|
||||
* remove players in a subclass.
|
||||
*
|
||||
* @return array $players Array of core_media_player objects in any order
|
||||
*/
|
||||
protected function get_players_raw() {
|
||||
$plugins = core_plugin_manager::instance()->get_plugins_of_type('media');
|
||||
$rv = [];
|
||||
foreach ($plugins as $name => $dir) {
|
||||
$classname = "media_" . $name . "_plugin";
|
||||
if (class_exists($classname)) {
|
||||
$rv[$name] = new $classname();
|
||||
}
|
||||
}
|
||||
return $rv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a media file (audio or video) using suitable embedded player.
|
||||
*
|
||||
* See embed_alternatives function for full description of parameters.
|
||||
* This function calls through to that one.
|
||||
*
|
||||
* When using this function you can also specify width and height in the
|
||||
* URL by including ?d=100x100 at the end. If specified in the URL, this
|
||||
* will override the $width and $height parameters.
|
||||
*
|
||||
* @param moodle_url $url Full URL of media file
|
||||
* @param string $name Optional user-readable name to display in download link
|
||||
* @param int $width Width in pixels (optional)
|
||||
* @param int $height Height in pixels (optional)
|
||||
* @param array $options Array of key/value pairs
|
||||
* @return string HTML content of embed
|
||||
*/
|
||||
public function embed_url(moodle_url $url, $name = '', $width = 0, $height = 0,
|
||||
$options = array()) {
|
||||
|
||||
// Get width and height from URL if specified (overrides parameters in
|
||||
// function call).
|
||||
$rawurl = $url->out(false);
|
||||
if (preg_match('/[?#]d=([\d]{1,4}%?)x([\d]{1,4}%?)/', $rawurl, $matches)) {
|
||||
$width = $matches[1];
|
||||
$height = $matches[2];
|
||||
$url = new moodle_url(str_replace($matches[0], '', $rawurl));
|
||||
}
|
||||
|
||||
// Defer to array version of function.
|
||||
return $this->embed_alternatives(array($url), $name, $width, $height, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders media files (audio or video) using suitable embedded player.
|
||||
* The list of URLs should be alternative versions of the same content in
|
||||
* multiple formats. If there is only one format it should have a single
|
||||
* entry.
|
||||
*
|
||||
* If the media files are not in a supported format, this will give students
|
||||
* a download link to each format. The download link uses the filename
|
||||
* unless you supply the optional name parameter.
|
||||
*
|
||||
* Width and height are optional. If specified, these are suggested sizes
|
||||
* and should be the exact values supplied by the user, if they come from
|
||||
* user input. These will be treated as relating to the size of the video
|
||||
* content, not including any player control bar.
|
||||
*
|
||||
* For audio files, height will be ignored. For video files, a few formats
|
||||
* work if you specify only width, but in general if you specify width
|
||||
* you must specify height as well.
|
||||
*
|
||||
* The $options array is passed through to the core_media_player classes
|
||||
* that render the object tag. The keys can contain values from
|
||||
* core_media::OPTION_xx.
|
||||
*
|
||||
* @param array $alternatives Array of moodle_url to media files
|
||||
* @param string $name Optional user-readable name to display in download link
|
||||
* @param int $width Width in pixels (optional)
|
||||
* @param int $height Height in pixels (optional)
|
||||
* @param array $options Array of key/value pairs
|
||||
* @return string HTML content of embed
|
||||
*/
|
||||
public function embed_alternatives($alternatives, $name = '', $width = 0, $height = 0,
|
||||
$options = array()) {
|
||||
|
||||
// Get list of player plugins.
|
||||
$players = $this->get_players();
|
||||
|
||||
// Set up initial text which will be replaced by first player that
|
||||
// supports any of the formats.
|
||||
$out = core_media_player::PLACEHOLDER;
|
||||
|
||||
// Loop through all players that support any of these URLs.
|
||||
foreach ($players as $player) {
|
||||
$supported = $player->list_supported_urls($alternatives, $options);
|
||||
if ($supported) {
|
||||
// Embed.
|
||||
$text = $player->embed($supported, $name, $width, $height, $options);
|
||||
|
||||
// Put this in place of the 'fallback' slot in the previous text.
|
||||
$out = str_replace(core_media_player::PLACEHOLDER, $text, $out);
|
||||
|
||||
// Check if we need to continue looking for players.
|
||||
if (strpos($out, core_media_player::PLACEHOLDER) === false) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($options[self::OPTION_FALLBACK_TO_BLANK]) || $out !== core_media_player::PLACEHOLDER) {
|
||||
// Fallback to the link. Exception: in case of OPTION_FALLBACK_TO_BLANK and no other player matched do not fallback.
|
||||
$text = $this->fallback_to_link($alternatives, $name, $options);
|
||||
$out = str_replace(core_media_player::PLACEHOLDER, $text, $out);
|
||||
}
|
||||
|
||||
// Remove 'fallback' slot from final version and return it.
|
||||
$out = str_replace(core_media_player::PLACEHOLDER, '', $out);
|
||||
if (!empty($options[self::OPTION_BLOCK]) && $out !== '') {
|
||||
$out = html_writer::tag('div', $out, array('class' => 'resourcecontent'));
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns links to the specified URLs unless OPTION_NO_LINK is passed.
|
||||
*
|
||||
* @param array $urls URLs of media files
|
||||
* @param string $name Display name; '' to use default
|
||||
* @param array $options Options array
|
||||
* @return string HTML code for embed
|
||||
*/
|
||||
protected function fallback_to_link($urls, $name, $options) {
|
||||
// If link is turned off, return empty.
|
||||
if (!empty($options[core_media_manager::OPTION_NO_LINK])) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// Build up link content.
|
||||
$output = '';
|
||||
foreach ($urls as $url) {
|
||||
if (strval($name) !== '' && $output === '') {
|
||||
$title = $name;
|
||||
} else {
|
||||
$title = core_media_manager::instance()->get_filename($url);
|
||||
}
|
||||
$printlink = html_writer::link($url, $title, array('class' => 'mediafallbacklink'));
|
||||
if ($output) {
|
||||
// Where there are multiple available formats, there are fallback links
|
||||
// for all formats, separated by /.
|
||||
$output .= ' / ';
|
||||
}
|
||||
$output .= $printlink;
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a file can be embedded. If this returns true you will get
|
||||
* an embedded player; if this returns false, you will just get a download
|
||||
* link.
|
||||
*
|
||||
* This is a wrapper for can_embed_urls.
|
||||
*
|
||||
* @param moodle_url $url URL of media file
|
||||
* @param array $options Options (same as when embedding)
|
||||
* @return bool True if file can be embedded
|
||||
*/
|
||||
public function can_embed_url(moodle_url $url, $options = array()) {
|
||||
return $this->can_embed_urls(array($url), $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a file can be embedded. If this returns true you will get
|
||||
* an embedded player; if this returns false, you will just get a download
|
||||
* link.
|
||||
*
|
||||
* @param array $urls URL of media file and any alternatives (moodle_url)
|
||||
* @param array $options Options (same as when embedding)
|
||||
* @return bool True if file can be embedded
|
||||
*/
|
||||
public function can_embed_urls(array $urls, $options = array()) {
|
||||
// Check all players to see if any of them support it.
|
||||
foreach ($this->get_players() as $player) {
|
||||
// First player that supports it, return true.
|
||||
if ($player->list_supported_urls($urls, $options)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains a list of markers that can be used in a regular expression when
|
||||
* searching for URLs that can be embedded by any player type.
|
||||
*
|
||||
* This string is used to improve peformance of regex matching by ensuring
|
||||
* that the (presumably C) regex code can do a quick keyword check on the
|
||||
* URL part of a link to see if it matches one of these, rather than having
|
||||
* to go into PHP code for every single link to see if it can be embedded.
|
||||
*
|
||||
* @return string String suitable for use in regex such as '(\.mp4|\.flv)'
|
||||
*/
|
||||
public function get_embeddable_markers() {
|
||||
if (empty($this->embeddablemarkers)) {
|
||||
$markers = '';
|
||||
foreach ($this->get_players() as $player) {
|
||||
foreach ($player->get_embeddable_markers() as $marker) {
|
||||
if ($markers !== '') {
|
||||
$markers .= '|';
|
||||
}
|
||||
$markers .= preg_quote($marker);
|
||||
}
|
||||
}
|
||||
$this->embeddablemarkers = $markers;
|
||||
}
|
||||
return $this->embeddablemarkers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a string containing multiple URLs separated by #, this will split
|
||||
* it into an array of moodle_url objects suitable for using when calling
|
||||
* embed_alternatives.
|
||||
*
|
||||
* Note that the input string should NOT be html-escaped (i.e. if it comes
|
||||
* from html, call html_entity_decode first).
|
||||
*
|
||||
* @param string $combinedurl String of 1 or more alternatives separated by #
|
||||
* @param int $width Output variable: width (will be set to 0 if not specified)
|
||||
* @param int $height Output variable: height (0 if not specified)
|
||||
* @return array Array of 1 or more moodle_url objects
|
||||
*/
|
||||
public function split_alternatives($combinedurl, &$width, &$height) {
|
||||
$urls = explode('#', $combinedurl);
|
||||
$width = 0;
|
||||
$height = 0;
|
||||
$returnurls = array();
|
||||
|
||||
foreach ($urls as $url) {
|
||||
$matches = null;
|
||||
|
||||
// You can specify the size as a separate part of the array like
|
||||
// #d=640x480 without actually including a url in it.
|
||||
if (preg_match('/^d=([\d]{1,4})x([\d]{1,4})$/i', $url, $matches)) {
|
||||
$width = $matches[1];
|
||||
$height = $matches[2];
|
||||
continue;
|
||||
}
|
||||
|
||||
// Can also include the ?d= as part of one of the URLs (if you use
|
||||
// more than one they will be ignored except the last).
|
||||
if (preg_match('/\?d=([\d]{1,4})x([\d]{1,4})$/i', $url, $matches)) {
|
||||
$width = $matches[1];
|
||||
$height = $matches[2];
|
||||
|
||||
// Trim from URL.
|
||||
$url = str_replace($matches[0], '', $url);
|
||||
}
|
||||
|
||||
// Clean up url.
|
||||
$url = clean_param($url, PARAM_URL);
|
||||
if (empty($url)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Turn it into moodle_url object.
|
||||
$returnurls[] = new moodle_url($url);
|
||||
}
|
||||
|
||||
return $returnurls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file extension for a URL.
|
||||
* @param moodle_url $url URL
|
||||
*/
|
||||
public function get_extension(moodle_url $url) {
|
||||
// Note: Does not use core_text (. is UTF8-safe).
|
||||
$filename = self::get_filename($url);
|
||||
$dot = strrpos($filename, '.');
|
||||
if ($dot === false) {
|
||||
return '';
|
||||
} else {
|
||||
return strtolower(substr($filename, $dot + 1));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains the filename from the moodle_url.
|
||||
* @param moodle_url $url URL
|
||||
* @return string Filename only (not escaped)
|
||||
*/
|
||||
public function get_filename(moodle_url $url) {
|
||||
// Use the 'file' parameter if provided (for links created when
|
||||
// slasharguments was off). If not present, just use URL path.
|
||||
$path = $url->get_param('file');
|
||||
if (!$path) {
|
||||
$path = $url->get_path();
|
||||
}
|
||||
|
||||
// Remove everything before last / if present. Does not use textlib as / is UTF8-safe.
|
||||
$slash = strrpos($path, '/');
|
||||
if ($slash !== false) {
|
||||
$path = substr($path, $slash + 1);
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Guesses MIME type for a moodle_url based on file extension.
|
||||
* @param moodle_url $url URL
|
||||
* @return string MIME type
|
||||
*/
|
||||
public function get_mimetype(moodle_url $url) {
|
||||
return mimeinfo('type', $this->get_filename($url));
|
||||
}
|
||||
|
||||
}
|
249
media/classes/player.php
Normal file
249
media/classes/player.php
Normal file
|
@ -0,0 +1,249 @@
|
|||
<?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/>.
|
||||
|
||||
/**
|
||||
* Base class for media players
|
||||
*
|
||||
* @package core_media
|
||||
* @copyright 2016 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Base class for media players.
|
||||
*
|
||||
* Media players return embed HTML for a particular way of playing back audio
|
||||
* or video (or another file type).
|
||||
*
|
||||
* In order to make the code more lightweight, this is not a plugin type
|
||||
* (players cannot have their own settings, database tables, capabilities, etc).
|
||||
* These classes are used only by core_media_renderer in outputrenderers.php.
|
||||
* If you add a new class here (in core code) you must modify the
|
||||
* get_players_raw function in that file to include it.
|
||||
*
|
||||
* If a Moodle installation wishes to add extra player objects they can do so
|
||||
* by overriding that renderer in theme, and overriding the get_players_raw
|
||||
* function. The new player class should then of course be defined within the
|
||||
* custom theme or other suitable location, not in this file.
|
||||
*
|
||||
* @package core_media
|
||||
* @copyright 2016 Marina Glancy
|
||||
* @author 2011 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
abstract class core_media_player {
|
||||
/**
|
||||
* Placeholder text used to indicate where the fallback content is placed
|
||||
* within a result.
|
||||
*/
|
||||
const PLACEHOLDER = '<!--FALLBACK-->';
|
||||
|
||||
/**
|
||||
* Generates code required to embed the player.
|
||||
*
|
||||
* The returned code contains a placeholder comment '<!--FALLBACK-->'
|
||||
* (constant core_media_player::PLACEHOLDER) which indicates the location
|
||||
* where fallback content should be placed in the event that this type of
|
||||
* player is not supported by user browser.
|
||||
*
|
||||
* The $urls parameter includes one or more alternative media formats that
|
||||
* are supported by this player. It does not include formats that aren't
|
||||
* supported (see list_supported_urls).
|
||||
*
|
||||
* The $options array contains key-value pairs. See OPTION_xx constants
|
||||
* for documentation of standard option(s).
|
||||
*
|
||||
* @param array $urls URLs of media files
|
||||
* @param string $name Display name; '' to use default
|
||||
* @param int $width Optional width; 0 to use default
|
||||
* @param int $height Optional height; 0 to use default
|
||||
* @param array $options Options array
|
||||
* @return string HTML code for embed
|
||||
*/
|
||||
public abstract function embed($urls, $name, $width, $height, $options);
|
||||
|
||||
/**
|
||||
* Gets the list of file extensions supported by this media player.
|
||||
*
|
||||
* Note: This is only required for the default implementations of
|
||||
* list_supported_urls(), get_embeddable_markers() and supports().
|
||||
* If you override these functions to determine
|
||||
* supported URLs in some way other than by extension, then this function
|
||||
* is not necessary.
|
||||
*
|
||||
* @return array Array of strings (extension not including dot e.g. '.mp3')
|
||||
*/
|
||||
public function get_supported_extensions() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists keywords that must be included in a url that can be embedded with
|
||||
* this player. Any such keywords should be added to the array.
|
||||
*
|
||||
* For example if this player supports FLV and F4V files then it should add
|
||||
* '.flv' and '.f4v' to the array. (The check is not case-sensitive.)
|
||||
*
|
||||
* Default handling calls the get_supported_extensions function, so players
|
||||
* only need to override this if they don't implement get_supported_extensions.
|
||||
*
|
||||
* This is used to improve performance when matching links in the media filter.
|
||||
*
|
||||
* @return array Array of keywords to add to the embeddable markers list
|
||||
*/
|
||||
public function get_embeddable_markers() {
|
||||
return $this->get_supported_extensions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns human-readable string of supported file/link types for the "Manage media players" page
|
||||
* @param array $usedextensions extensions that should NOT be highlighted
|
||||
* @return string
|
||||
*/
|
||||
public function supports($usedextensions = []) {
|
||||
$out = [];
|
||||
if ($extensions = $this->get_supported_extensions()) {
|
||||
$video = $audio = $other = [];
|
||||
foreach ($extensions as $key => $extension) {
|
||||
$displayextension = $extension;
|
||||
if (!in_array($extension, $usedextensions)) {
|
||||
$displayextension = '<strong>'.$extension.'</strong>';
|
||||
}
|
||||
if (file_extension_in_typegroup('file.'.$extension, 'audio')) {
|
||||
$audio[] = $displayextension;
|
||||
} else if (file_extension_in_typegroup('file.'.$extension, 'video')) {
|
||||
$video[] = $displayextension;
|
||||
} else {
|
||||
$other[] = $displayextension;
|
||||
}
|
||||
}
|
||||
if ($video) {
|
||||
$out[] = get_string('videoextensions', 'core_media', join(', ', $video));
|
||||
}
|
||||
if ($audio) {
|
||||
$out[] = get_string('audioextensions', 'core_media', join(', ', $audio));
|
||||
}
|
||||
if ($other) {
|
||||
$out[] = get_string('extensions', 'core_media', join(', ', $other));
|
||||
}
|
||||
}
|
||||
return join('<br>', $out);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the ranking of this player. This is an integer used to decide which
|
||||
* player to use (after applying other considerations such as which ones
|
||||
* the user has disabled).
|
||||
*
|
||||
* This function returns the default rank that can be adjusted by the administrator
|
||||
* on the Manage media players page.
|
||||
*
|
||||
* @return int Rank (higher is better)
|
||||
*/
|
||||
public abstract function get_rank();
|
||||
|
||||
/**
|
||||
* Returns if the current player is enabled.
|
||||
*
|
||||
* @deprecated since Moodle 3.2
|
||||
* @return bool True if player is enabled
|
||||
*/
|
||||
public function is_enabled() {
|
||||
debugging('Function core_media_player::is_enabled() is deprecated without replacement', DEBUG_DEVELOPER);
|
||||
|
||||
$enabled = \core\plugininfo\media::get_enabled_plugins();
|
||||
|
||||
if ($enabled && preg_match('/^media_(.*)_plugin$/', get_class($this), $matches)) {
|
||||
return array_key_exists($matches[1], $enabled);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a list of URLs, returns a reduced array containing only those URLs
|
||||
* which are supported by this player. (Empty if none.)
|
||||
* @param array $urls Array of moodle_url
|
||||
* @param array $options Options (same as will be passed to embed)
|
||||
* @return array Array of supported moodle_url
|
||||
*/
|
||||
public function list_supported_urls(array $urls, array $options = array()) {
|
||||
$extensions = $this->get_supported_extensions();
|
||||
$result = array();
|
||||
foreach ($urls as $url) {
|
||||
$ext = core_media_manager::instance()->get_extension($url);
|
||||
if (in_array('.' . $ext, $extensions) || in_array($ext, $extensions)) {
|
||||
$result[] = $url;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains suitable name for media. Uses specified name if there is one,
|
||||
* otherwise makes one up.
|
||||
* @param string $name User-specified name ('' if none)
|
||||
* @param array $urls Array of moodle_url used to make up name
|
||||
* @return string Name
|
||||
*/
|
||||
protected function get_name($name, $urls) {
|
||||
// If there is a specified name, use that.
|
||||
if ($name) {
|
||||
return $name;
|
||||
}
|
||||
|
||||
// Get filename of first URL.
|
||||
$url = reset($urls);
|
||||
$name = core_media_manager::instance()->get_filename($url);
|
||||
|
||||
// If there is more than one url, strip the extension as we could be
|
||||
// referring to a different one or several at once.
|
||||
if (count($urls) > 1) {
|
||||
$name = preg_replace('~\.[^.]*$~', '', $name);
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares by rank order, highest first. Used for sort functions.
|
||||
* @deprecated since Moodle 3.2
|
||||
* @param core_media_player $a Player A
|
||||
* @param core_media_player $b Player B
|
||||
* @return int Negative if A should go before B, positive for vice versa
|
||||
*/
|
||||
public static function compare_by_rank(core_media_player $a, core_media_player $b) {
|
||||
debugging('Function core_media_player::compare_by_rank() is deprecated without replacement', DEBUG_DEVELOPER);
|
||||
return $b->get_rank() - $a->get_rank();
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility function that sets width and height to defaults if not specified
|
||||
* as a parameter to the function (will be specified either if, (a) the calling
|
||||
* code passed it, or (b) the URL included it).
|
||||
* @param int $width Width passed to function (updated with final value)
|
||||
* @param int $height Height passed to function (updated with final value)
|
||||
*/
|
||||
protected static function pick_video_size(&$width, &$height) {
|
||||
global $CFG;
|
||||
if (!$width) {
|
||||
$width = $CFG->media_default_width;
|
||||
$height = $CFG->media_default_height;
|
||||
}
|
||||
}
|
||||
}
|
107
media/classes/player_external.php
Normal file
107
media/classes/player_external.php
Normal file
|
@ -0,0 +1,107 @@
|
|||
<?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/>.
|
||||
|
||||
/**
|
||||
* Base class for players which handle external links
|
||||
*
|
||||
* @package core_media
|
||||
* @copyright 2016 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Base class for players which handle external links (YouTube etc).
|
||||
*
|
||||
* As opposed to media files.
|
||||
*
|
||||
* @package core_media
|
||||
* @copyright 2016 Marina Glancy
|
||||
* @author 2011 The Open University
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
abstract class core_media_player_external extends core_media_player {
|
||||
/**
|
||||
* Array of matches from regular expression - subclass can assume these
|
||||
* will be valid when the embed function is called, to save it rerunning
|
||||
* the regex.
|
||||
* @var array
|
||||
*/
|
||||
protected $matches;
|
||||
|
||||
/**
|
||||
* Part of a regular expression, including ending ~ symbol (note: these
|
||||
* regexes use ~ instead of / because URLs and HTML code typically include
|
||||
* / symbol and makes harder to read if you have to escape it).
|
||||
* Matches the end part of a link after you have read the 'important' data
|
||||
* including optional #d=400x300 at end of url, plus content of <a> tag,
|
||||
* up to </a>.
|
||||
* @var string
|
||||
*/
|
||||
const END_LINK_REGEX_PART = '[^#]*(#d=([\d]{1,4})x([\d]{1,4}))?~si';
|
||||
|
||||
public function embed($urls, $name, $width, $height, $options) {
|
||||
return $this->embed_external(reset($urls), $name, $width, $height, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains HTML code to embed the link.
|
||||
* @param moodle_url $url Single URL to embed
|
||||
* @param string $name Display name; '' to use default
|
||||
* @param int $width Optional width; 0 to use default
|
||||
* @param int $height Optional height; 0 to use default
|
||||
* @param array $options Options array
|
||||
* @return string HTML code for embed
|
||||
*/
|
||||
protected abstract function embed_external(moodle_url $url, $name, $width, $height, $options);
|
||||
|
||||
public function list_supported_urls(array $urls, array $options = array()) {
|
||||
// These only work with a SINGLE url (there is no fallback).
|
||||
if (count($urls) != 1) {
|
||||
return array();
|
||||
}
|
||||
$url = reset($urls);
|
||||
|
||||
// Check against regex.
|
||||
if (preg_match($this->get_regex(), $url->out(false), $this->matches)) {
|
||||
return array($url);
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns regular expression used to match URLs that this player handles
|
||||
* @return string PHP regular expression e.g. '~^https?://example.org/~'
|
||||
*/
|
||||
protected function get_regex() {
|
||||
return '~^unsupported~';
|
||||
}
|
||||
|
||||
/**
|
||||
* Annoyingly, preg_match $matches result does not always have the same
|
||||
* number of parameters - it leaves out optional ones at the end. WHAT.
|
||||
* Anyway, this function can be used to fix it.
|
||||
* @param array $matches Array that should be adjusted
|
||||
* @param int $count Number of capturing groups (=6 to make $matches[6] work)
|
||||
*/
|
||||
protected static function fix_match_count(&$matches, $count) {
|
||||
for ($i = count($matches); $i <= $count; $i++) {
|
||||
$matches[$i] = false;
|
||||
}
|
||||
}
|
||||
}
|
130
media/classes/player_native.php
Normal file
130
media/classes/player_native.php
Normal file
|
@ -0,0 +1,130 @@
|
|||
<?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/>.
|
||||
|
||||
/**
|
||||
* Base class for players which return native HTML5 <video> or <audio> tags
|
||||
*
|
||||
* @package core_media
|
||||
* @copyright 2016 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Base class for players which return native HTML5 <video> or <audio> tags
|
||||
*
|
||||
* @package core_media
|
||||
* @copyright 2016 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
abstract class core_media_player_native extends core_media_player {
|
||||
/**
|
||||
* Extracts a value for an attribute
|
||||
*
|
||||
* @param string $tag html tag which properties are extracted, for example "<video ...>....</video>"
|
||||
* @param string $attrname name of the attribute we are looking for
|
||||
* @param string $type one of PARAM_* constants to clean the attribute value
|
||||
* @return string|null
|
||||
*/
|
||||
public static function get_attribute($tag, $attrname, $type = PARAM_RAW) {
|
||||
if (preg_match('/^<[^>]*\b' . $attrname . '="(.*?)"/is', $tag, $matches)) {
|
||||
return clean_param(htmlspecialchars_decode($matches[1]), $type);
|
||||
} else if (preg_match('~^<[^>]*\b' . $attrname . '[ />]"~is', $tag, $matches)) {
|
||||
// Some attributes may not have value, for example this is valid: <video controls>.
|
||||
return clean_param("true", $type);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an attribute from the media tags
|
||||
*
|
||||
* @param string $tag html tag which properties are extracted, for example "<video ...>....</video>"
|
||||
* @param string|array $attrname
|
||||
* @return string
|
||||
*/
|
||||
public static function remove_attributes($tag, $attrname) {
|
||||
if (is_array($attrname)) {
|
||||
$attrname = join('|', $attrname);
|
||||
}
|
||||
while (preg_match('/^(<[^>]*\b)(' . $attrname . ')=".*?"(.*)$/is', $tag, $matches)) {
|
||||
$tag = $matches[1] . $matches[3];
|
||||
}
|
||||
while (preg_match('~^(<[^>]*\b)(' . $attrname . ')([ />].*)$~is', $tag, $matches)) {
|
||||
// Some attributes may not have value, for example: <video controls>.
|
||||
$tag = $matches[1] . $matches[3];
|
||||
}
|
||||
return $tag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds attributes to the media tags
|
||||
*
|
||||
* @param string $tag html tag which properties are extracted, for example "<video ...>....</video>"
|
||||
* @param array $attributes key-value pairs of attributes to be added
|
||||
* @return string
|
||||
*/
|
||||
public static function add_attributes($tag, $attributes) {
|
||||
$tag = self::remove_attributes($tag, array_keys($attributes));
|
||||
if (!preg_match('/^(<.*?)(>.*)$/s', $tag, $matches)) {
|
||||
return $tag;
|
||||
}
|
||||
$rv = $matches[1];
|
||||
foreach ($attributes as $name => $value) {
|
||||
$rv .= " $name=\"".s($value).'"';
|
||||
}
|
||||
$rv .= $matches[2];
|
||||
return $rv;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces all embedded <source> tags and src attribute
|
||||
*
|
||||
* @param string $tag html tag which properties are extracted, for example "<video ...>....</video>"
|
||||
* @param string $sources replacement string (expected to contain <source> tags)
|
||||
* @return string
|
||||
*/
|
||||
public static function replace_sources($tag, $sources) {
|
||||
$tag = self::remove_attributes($tag, 'src');
|
||||
$tag = preg_replace(['~</?source\b[^>]*>~i'], '', $tag);
|
||||
if (preg_match('/^(<.*?>)([^\0]*)$/ms', $tag, $matches)) {
|
||||
$tag = $matches[1].$sources.$matches[2];
|
||||
}
|
||||
return $tag;
|
||||
}
|
||||
|
||||
public function get_supported_extensions() {
|
||||
return file_get_typegroup('extension', ['html_video', 'html_audio']);
|
||||
}
|
||||
|
||||
public function list_supported_urls(array $urls, array $options = array()) {
|
||||
$extensions = $this->get_supported_extensions();
|
||||
$result = array();
|
||||
foreach ($urls as $url) {
|
||||
$ext = core_media_manager::instance()->get_extension($url);
|
||||
if (in_array('.' . $ext, $extensions) && core_useragent::supports_html5($ext)) {
|
||||
// Unfortunately html5 video does not handle fallback properly.
|
||||
// https://www.w3.org/Bugs/Public/show_bug.cgi?id=10975
|
||||
// That means we need to do browser detect and not use html5 on
|
||||
// browsers which do not support the given type, otherwise users
|
||||
// will not even see the fallback link.
|
||||
$result[] = $url;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue