Merge branch 'MDL-79705-402' of https://github.com/paulholden/moodle into MOODLE_402_...
[moodle.git] / media / classes / player_external.php
bloba377de65f6bfbe3fc201c76e73ef05125dcfd03f
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 handle external links
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 handle external links (YouTube etc).
30 * As opposed to media files.
32 * @package core_media
33 * @copyright 2016 Marina Glancy
34 * @author 2011 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 abstract class core_media_player_external extends core_media_player {
38 /**
39 * Array of matches from regular expression - subclass can assume these
40 * will be valid when the embed function is called, to save it rerunning
41 * the regex.
42 * @var array
44 protected $matches;
46 /**
47 * Part of a regular expression, including ending ~ symbol (note: these
48 * regexes use ~ instead of / because URLs and HTML code typically include
49 * / symbol and makes harder to read if you have to escape it).
50 * Matches the end part of a link after you have read the 'important' data
51 * including optional #d=400x300 at end of url, plus content of <a> tag,
52 * up to </a>.
53 * @var string
55 const END_LINK_REGEX_PART = '[^#]*(#d=([\d]{1,4})x([\d]{1,4}))?~si';
57 public function embed($urls, $name, $width, $height, $options) {
58 return $this->embed_external(reset($urls), $name, $width, $height, $options);
61 /**
62 * Obtains HTML code to embed the link.
63 * @param moodle_url $url Single URL to embed
64 * @param string $name Display name; '' to use default
65 * @param int $width Optional width; 0 to use default
66 * @param int $height Optional height; 0 to use default
67 * @param array $options Options array
68 * @return string HTML code for embed
70 protected abstract function embed_external(moodle_url $url, $name, $width, $height, $options);
72 public function list_supported_urls(array $urls, array $options = array()) {
73 // These only work with a SINGLE url (there is no fallback).
74 if (count($urls) != 1) {
75 return array();
77 $url = reset($urls);
79 // Check against regex.
80 if (preg_match($this->get_regex(), $url->out(false), $this->matches)) {
81 return array($url);
84 return array();
87 /**
88 * Returns regular expression used to match URLs that this player handles
89 * @return string PHP regular expression e.g. '~^https?://example.org/~'
91 protected function get_regex() {
92 return '~^unsupported~';
95 /**
96 * Annoyingly, preg_match $matches result does not always have the same
97 * number of parameters - it leaves out optional ones at the end. WHAT.
98 * Anyway, this function can be used to fix it.
99 * @param array $matches Array that should be adjusted
100 * @param int $count Number of capturing groups (=6 to make $matches[6] work)
102 protected static function fix_match_count(&$matches, $count) {
103 for ($i = count($matches); $i <= $count; $i++) {
104 $matches[$i] = false;