PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/medialib.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 1209 lines | 617 code | 147 blank | 445 comment | 58 complexity | df2b2d6c466ee3afe3b302dc25856ba6 MD5 | raw file
  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. * Classes for handling embedded media (mainly audio and video).
  18. *
  19. * These are used only from within the core media renderer.
  20. *
  21. * To embed media from Moodle code, do something like the following:
  22. *
  23. * $mediarenderer = $PAGE->get_renderer('core', 'media');
  24. * echo $mediarenderer->embed_url(new moodle_url('http://example.org/a.mp3'));
  25. *
  26. * You do not need to require this library file manually. Getting the renderer
  27. * (the first line above) requires this library file automatically.
  28. *
  29. * @package core_media
  30. * @copyright 2012 The Open University
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. defined('MOODLE_INTERNAL') || die();
  34. if (!defined('CORE_MEDIA_VIDEO_WIDTH')) {
  35. // Default video width if no width is specified; some players may do something
  36. // more intelligent such as use real video width.
  37. // May be defined in config.php if required.
  38. define('CORE_MEDIA_VIDEO_WIDTH', 400);
  39. }
  40. if (!defined('CORE_MEDIA_VIDEO_HEIGHT')) {
  41. // Default video height. May be defined in config.php if required.
  42. define('CORE_MEDIA_VIDEO_HEIGHT', 300);
  43. }
  44. if (!defined('CORE_MEDIA_AUDIO_WIDTH')) {
  45. // Default audio width if no width is specified.
  46. // May be defined in config.php if required.
  47. define('CORE_MEDIA_AUDIO_WIDTH', 300);
  48. }
  49. /**
  50. * Constants and static utility functions for use with core_media_renderer.
  51. *
  52. * @copyright 2011 The Open University
  53. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  54. */
  55. abstract class core_media {
  56. /**
  57. * Option: Disable text link fallback.
  58. *
  59. * Use this option if you are going to print a visible link anyway so it is
  60. * pointless to have one as fallback.
  61. *
  62. * To enable, set value to true.
  63. */
  64. const OPTION_NO_LINK = 'nolink';
  65. /**
  66. * Option: When embedding, if there is no matching embed, do not use the
  67. * default link fallback player; instead return blank.
  68. *
  69. * This is different from OPTION_NO_LINK because this option still uses the
  70. * fallback link if there is some kind of embedding. Use this option if you
  71. * are going to check if the return value is blank and handle it specially.
  72. *
  73. * To enable, set value to true.
  74. */
  75. const OPTION_FALLBACK_TO_BLANK = 'embedorblank';
  76. /**
  77. * Option: Enable players which are only suitable for use when we trust the
  78. * user who embedded the content.
  79. *
  80. * At present, this option enables the SWF player.
  81. *
  82. * To enable, set value to true.
  83. */
  84. const OPTION_TRUSTED = 'trusted';
  85. /**
  86. * Option: Put a div around the output (if not blank) so that it displays
  87. * as a block using the 'resourcecontent' CSS class.
  88. *
  89. * To enable, set value to true.
  90. */
  91. const OPTION_BLOCK = 'block';
  92. /**
  93. * Given a string containing multiple URLs separated by #, this will split
  94. * it into an array of moodle_url objects suitable for using when calling
  95. * embed_alternatives.
  96. *
  97. * Note that the input string should NOT be html-escaped (i.e. if it comes
  98. * from html, call html_entity_decode first).
  99. *
  100. * @param string $combinedurl String of 1 or more alternatives separated by #
  101. * @param int $width Output variable: width (will be set to 0 if not specified)
  102. * @param int $height Output variable: height (0 if not specified)
  103. * @return array Array of 1 or more moodle_url objects
  104. */
  105. public static function split_alternatives($combinedurl, &$width, &$height) {
  106. $urls = explode('#', $combinedurl);
  107. $width = 0;
  108. $height = 0;
  109. $returnurls = array();
  110. foreach ($urls as $url) {
  111. $matches = null;
  112. // You can specify the size as a separate part of the array like
  113. // #d=640x480 without actually including a url in it.
  114. if (preg_match('/^d=([\d]{1,4})x([\d]{1,4})$/i', $url, $matches)) {
  115. $width = $matches[1];
  116. $height = $matches[2];
  117. continue;
  118. }
  119. // Can also include the ?d= as part of one of the URLs (if you use
  120. // more than one they will be ignored except the last).
  121. if (preg_match('/\?d=([\d]{1,4})x([\d]{1,4})$/i', $url, $matches)) {
  122. $width = $matches[1];
  123. $height = $matches[2];
  124. // Trim from URL.
  125. $url = str_replace($matches[0], '', $url);
  126. }
  127. // Clean up url.
  128. $url = clean_param($url, PARAM_URL);
  129. if (empty($url)) {
  130. continue;
  131. }
  132. // Turn it into moodle_url object.
  133. $returnurls[] = new moodle_url($url);
  134. }
  135. return $returnurls;
  136. }
  137. /**
  138. * Returns the file extension for a URL.
  139. * @param moodle_url $url URL
  140. */
  141. public static function get_extension(moodle_url $url) {
  142. // Note: Does not use textlib (. is UTF8-safe).
  143. $filename = self::get_filename($url);
  144. $dot = strrpos($filename, '.');
  145. if ($dot === false) {
  146. return '';
  147. } else {
  148. return strtolower(substr($filename, $dot + 1));
  149. }
  150. }
  151. /**
  152. * Obtains the filename from the moodle_url.
  153. * @param moodle_url $url URL
  154. * @return string Filename only (not escaped)
  155. */
  156. public static function get_filename(moodle_url $url) {
  157. global $CFG;
  158. // Use the 'file' parameter if provided (for links created when
  159. // slasharguments was off). If not present, just use URL path.
  160. $path = $url->get_param('file');
  161. if (!$path) {
  162. $path = $url->get_path();
  163. }
  164. // Remove everything before last / if present. Does not use textlib as / is UTF8-safe.
  165. $slash = strrpos($path, '/');
  166. if ($slash !== false) {
  167. $path = substr($path, $slash + 1);
  168. }
  169. return $path;
  170. }
  171. /**
  172. * Guesses MIME type for a moodle_url based on file extension.
  173. * @param moodle_url $url URL
  174. * @return string MIME type
  175. */
  176. public static function get_mimetype(moodle_url $url) {
  177. return mimeinfo('type', self::get_filename($url));
  178. }
  179. }
  180. /**
  181. * Base class for media players.
  182. *
  183. * Media players return embed HTML for a particular way of playing back audio
  184. * or video (or another file type).
  185. *
  186. * In order to make the code more lightweight, this is not a plugin type
  187. * (players cannot have their own settings, database tables, capabilities, etc).
  188. * These classes are used only by core_media_renderer in outputrenderers.php.
  189. * If you add a new class here (in core code) you must modify the
  190. * get_players_raw function in that file to include it.
  191. *
  192. * If a Moodle installation wishes to add extra player objects they can do so
  193. * by overriding that renderer in theme, and overriding the get_players_raw
  194. * function. The new player class should then of course be defined within the
  195. * custom theme or other suitable location, not in this file.
  196. *
  197. * @copyright 2011 The Open University
  198. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  199. */
  200. abstract class core_media_player {
  201. /**
  202. * Placeholder text used to indicate where the fallback content is placed
  203. * within a result.
  204. */
  205. const PLACEHOLDER = '<!--FALLBACK-->';
  206. /**
  207. * Generates code required to embed the player.
  208. *
  209. * The returned code contains a placeholder comment '<!--FALLBACK-->'
  210. * (constant core_media_player::PLACEHOLDER) which indicates the location
  211. * where fallback content should be placed in the event that this type of
  212. * player is not supported by user browser.
  213. *
  214. * The $urls parameter includes one or more alternative media formats that
  215. * are supported by this player. It does not include formats that aren't
  216. * supported (see list_supported_urls).
  217. *
  218. * The $options array contains key-value pairs. See OPTION_xx constants
  219. * for documentation of standard option(s).
  220. *
  221. * @param array $urls URLs of media files
  222. * @param string $name Display name; '' to use default
  223. * @param int $width Optional width; 0 to use default
  224. * @param int $height Optional height; 0 to use default
  225. * @param array $options Options array
  226. * @return string HTML code for embed
  227. */
  228. public abstract function embed($urls, $name, $width, $height, $options);
  229. /**
  230. * Gets the list of file extensions supported by this media player.
  231. *
  232. * Note: This is only required for the default implementation of
  233. * list_supported_urls. If you override that function to determine
  234. * supported URLs in some way other than by extension, then this function
  235. * is not necessary.
  236. *
  237. * @return array Array of strings (extension not including dot e.g. 'mp3')
  238. */
  239. public function get_supported_extensions() {
  240. return array();
  241. }
  242. /**
  243. * Lists keywords that must be included in a url that can be embedded with
  244. * this player. Any such keywords should be added to the array.
  245. *
  246. * For example if this player supports FLV and F4V files then it should add
  247. * '.flv' and '.f4v' to the array. (The check is not case-sensitive.)
  248. *
  249. * Default handling calls the get_supported_extensions function and adds
  250. * a dot to each of those values, so players only need to override this
  251. * if they don't implement get_supported_extensions.
  252. *
  253. * This is used to improve performance when matching links in the media filter.
  254. *
  255. * @return array Array of keywords to add to the embeddable markers list
  256. */
  257. public function get_embeddable_markers() {
  258. $markers = array();
  259. foreach ($this->get_supported_extensions() as $extension) {
  260. $markers[] = '.' . $extension;
  261. }
  262. return $markers;
  263. }
  264. /**
  265. * Gets the ranking of this player. This is an integer used to decide which
  266. * player to use (after applying other considerations such as which ones
  267. * the user has disabled).
  268. *
  269. * Rank must be unique (no two players should have the same rank).
  270. *
  271. * Rank zero has a special meaning, indicating that this 'player' does not
  272. * really embed the video.
  273. *
  274. * Rank is not a user-configurable value because it needs to be defined
  275. * carefully in order to ensure that the embedding fallbacks actually work.
  276. * It might be possible to have some user options which affect rank, but
  277. * these would be best defined as e.g. checkboxes in settings that have
  278. * a particular effect on the rank of a couple of plugins, rather than
  279. * letting users generally alter rank.
  280. *
  281. * Note: Within medialib.php, players are listed in rank order (highest
  282. * rank first).
  283. *
  284. * @return int Rank (higher is better)
  285. */
  286. public abstract function get_rank();
  287. /**
  288. * @return bool True if player is enabled
  289. */
  290. public function is_enabled() {
  291. global $CFG;
  292. // With the class core_media_player_html5video it is enabled
  293. // based on $CFG->core_media_enable_html5video.
  294. $setting = str_replace('_player_', '_enable_', get_class($this));
  295. return !empty($CFG->{$setting});
  296. }
  297. /**
  298. * Given a list of URLs, returns a reduced array containing only those URLs
  299. * which are supported by this player. (Empty if none.)
  300. * @param array $urls Array of moodle_url
  301. * @param array $options Options (same as will be passed to embed)
  302. * @return array Array of supported moodle_url
  303. */
  304. public function list_supported_urls(array $urls, array $options = array()) {
  305. $extensions = $this->get_supported_extensions();
  306. $result = array();
  307. foreach ($urls as $url) {
  308. if (in_array(core_media::get_extension($url), $extensions)) {
  309. $result[] = $url;
  310. }
  311. }
  312. return $result;
  313. }
  314. /**
  315. * Obtains suitable name for media. Uses specified name if there is one,
  316. * otherwise makes one up.
  317. * @param string $name User-specified name ('' if none)
  318. * @param array $urls Array of moodle_url used to make up name
  319. * @return string Name
  320. */
  321. protected function get_name($name, $urls) {
  322. // If there is a specified name, use that.
  323. if ($name) {
  324. return $name;
  325. }
  326. // Get filename of first URL.
  327. $url = reset($urls);
  328. $name = core_media::get_filename($url);
  329. // If there is more than one url, strip the extension as we could be
  330. // referring to a different one or several at once.
  331. if (count($urls) > 1) {
  332. $name = preg_replace('~\.[^.]*$~', '', $name);
  333. }
  334. return $name;
  335. }
  336. /**
  337. * Compares by rank order, highest first. Used for sort functions.
  338. * @param core_media_player $a Player A
  339. * @param core_media_player $b Player B
  340. * @return int Negative if A should go before B, positive for vice versa
  341. */
  342. public static function compare_by_rank(core_media_player $a, core_media_player $b) {
  343. return $b->get_rank() - $a->get_rank();
  344. }
  345. /**
  346. * Utility function that sets width and height to defaults if not specified
  347. * as a parameter to the function (will be specified either if, (a) the calling
  348. * code passed it, or (b) the URL included it).
  349. * @param int $width Width passed to function (updated with final value)
  350. * @param int $height Height passed to function (updated with final value)
  351. */
  352. protected static function pick_video_size(&$width, &$height) {
  353. if (!$width) {
  354. $width = CORE_MEDIA_VIDEO_WIDTH;
  355. $height = CORE_MEDIA_VIDEO_HEIGHT;
  356. }
  357. }
  358. }
  359. /**
  360. * Base class for players which handle external links (YouTube etc).
  361. *
  362. * As opposed to media files.
  363. *
  364. * @copyright 2011 The Open University
  365. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  366. */
  367. abstract class core_media_player_external extends core_media_player {
  368. /**
  369. * Array of matches from regular expression - subclass can assume these
  370. * will be valid when the embed function is called, to save it rerunning
  371. * the regex.
  372. * @var array
  373. */
  374. protected $matches;
  375. /**
  376. * Part of a regular expression, including ending ~ symbol (note: these
  377. * regexes use ~ instead of / because URLs and HTML code typically include
  378. * / symbol and makes harder to read if you have to escape it).
  379. * Matches the end part of a link after you have read the 'important' data
  380. * including optional #d=400x300 at end of url, plus content of <a> tag,
  381. * up to </a>.
  382. * @var string
  383. */
  384. const END_LINK_REGEX_PART = '[^#]*(#d=([\d]{1,4})x([\d]{1,4}))?~si';
  385. public function embed($urls, $name, $width, $height, $options) {
  386. return $this->embed_external(reset($urls), $name, $width, $height, $options);
  387. }
  388. /**
  389. * Obtains HTML code to embed the link.
  390. * @param moodle_url $url Single URL to embed
  391. * @param string $name Display name; '' to use default
  392. * @param int $width Optional width; 0 to use default
  393. * @param int $height Optional height; 0 to use default
  394. * @param array $options Options array
  395. * @return string HTML code for embed
  396. */
  397. protected abstract function embed_external(moodle_url $url, $name, $width, $height, $options);
  398. public function list_supported_urls(array $urls, array $options = array()) {
  399. // These only work with a SINGLE url (there is no fallback).
  400. if (count($urls) != 1) {
  401. return array();
  402. }
  403. $url = reset($urls);
  404. // Check against regex.
  405. if (preg_match($this->get_regex(), $url->out(false), $this->matches)) {
  406. return array($url);
  407. }
  408. return array();
  409. }
  410. /**
  411. * Returns regular expression used to match URLs that this player handles
  412. * @return string PHP regular expression e.g. '~^https?://example.org/~'
  413. */
  414. protected function get_regex() {
  415. return '~^unsupported~';
  416. }
  417. /**
  418. * Annoyingly, preg_match $matches result does not always have the same
  419. * number of parameters - it leaves out optional ones at the end. WHAT.
  420. * Anyway, this function can be used to fix it.
  421. * @param array $matches Array that should be adjusted
  422. * @param int $count Number of capturing groups (=6 to make $matches[6] work)
  423. */
  424. protected static function fix_match_count(&$matches, $count) {
  425. for ($i = count($matches); $i <= $count; $i++) {
  426. $matches[$i] = false;
  427. }
  428. }
  429. }
  430. /**
  431. * Player that embeds Vimeo links.
  432. *
  433. * @copyright 2011 The Open University
  434. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  435. */
  436. class core_media_player_vimeo extends core_media_player_external {
  437. protected function embed_external(moodle_url $url, $name, $width, $height, $options) {
  438. $videoid = $this->matches[1];
  439. $info = s($name);
  440. // Note: resizing via url is not supported, user can click the fullscreen
  441. // button instead. iframe embedding is not xhtml strict but it is the only
  442. // option that seems to work on most devices.
  443. self::pick_video_size($width, $height);
  444. $output = <<<OET
  445. <span class="mediaplugin mediaplugin_vimeo">
  446. <iframe title="$info" src="https://player.vimeo.com/video/$videoid"
  447. width="$width" height="$height" frameborder="0"></iframe>
  448. </span>
  449. OET;
  450. return $output;
  451. }
  452. protected function get_regex() {
  453. // Initial part of link.
  454. $start = '~^https?://vimeo\.com/';
  455. // Middle bit: either watch?v= or v/.
  456. $middle = '([0-9]+)';
  457. return $start . $middle . core_media_player_external::END_LINK_REGEX_PART;
  458. }
  459. public function get_rank() {
  460. return 1010;
  461. }
  462. public function get_embeddable_markers() {
  463. return array('vimeo.com/');
  464. }
  465. }
  466. /**
  467. * Player that creates YouTube embedding.
  468. *
  469. * @copyright 2011 The Open University
  470. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  471. */
  472. class core_media_player_youtube extends core_media_player_external {
  473. protected function embed_external(moodle_url $url, $name, $width, $height, $options) {
  474. $videoid = end($this->matches);
  475. $info = trim($name);
  476. if (empty($info) or strpos($info, 'http') === 0) {
  477. $info = get_string('siteyoutube', 'core_media');
  478. }
  479. $info = s($info);
  480. self::pick_video_size($width, $height);
  481. return <<<OET
  482. <span class="mediaplugin mediaplugin_youtube">
  483. <iframe title="$info" width="$width" height="$height"
  484. src="https://www.youtube.com/embed/$videoid?rel=0&wmode=transparent" frameborder="0" allowfullscreen="1"></iframe>
  485. </span>
  486. OET;
  487. }
  488. protected function get_regex() {
  489. // Regex for standard youtube link
  490. $link = '(youtube(-nocookie)?\.com/(?:watch\?v=|v/))';
  491. // Regex for shortened youtube link
  492. $shortlink = '((youtu|y2u)\.be/)';
  493. // Initial part of link.
  494. $start = '~^https?://(www\.)?(' . $link . '|' . $shortlink . ')';
  495. // Middle bit: Video key value
  496. $middle = '([a-z0-9\-_]+)';
  497. return $start . $middle . core_media_player_external::END_LINK_REGEX_PART;
  498. }
  499. public function get_rank() {
  500. // I decided to make the link-embedding ones (that don't handle file
  501. // formats) have ranking in the 1000 range.
  502. return 1001;
  503. }
  504. public function get_embeddable_markers() {
  505. return array('youtube.com', 'youtube-nocookie.com', 'youtu.be', 'y2u.be');
  506. }
  507. }
  508. /**
  509. * Player that creates YouTube playlist embedding.
  510. *
  511. * @copyright 2011 The Open University
  512. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  513. */
  514. class core_media_player_youtube_playlist extends core_media_player_external {
  515. public function is_enabled() {
  516. global $CFG;
  517. // Use the youtube on/off flag.
  518. return $CFG->core_media_enable_youtube;
  519. }
  520. protected function embed_external(moodle_url $url, $name, $width, $height, $options) {
  521. $site = $this->matches[1];
  522. $playlist = $this->matches[3];
  523. $info = trim($name);
  524. if (empty($info) or strpos($info, 'http') === 0) {
  525. $info = get_string('siteyoutube', 'core_media');
  526. }
  527. $info = s($info);
  528. self::pick_video_size($width, $height);
  529. return <<<OET
  530. <span class="mediaplugin mediaplugin_youtube">
  531. <iframe width="$width" height="$height" src="https://$site/embed/videoseries?list=$playlist" frameborder="0" allowfullscreen="1"></iframe>
  532. </span>
  533. OET;
  534. }
  535. protected function get_regex() {
  536. // Initial part of link.
  537. $start = '~^https?://(www\.youtube(-nocookie)?\.com)/';
  538. // Middle bit: either view_play_list?p= or p/ (doesn't work on youtube) or playlist?list=.
  539. $middle = '(?:view_play_list\?p=|p/|playlist\?list=)([a-z0-9\-_]+)';
  540. return $start . $middle . core_media_player_external::END_LINK_REGEX_PART;
  541. }
  542. public function get_rank() {
  543. // I decided to make the link-embedding ones (that don't handle file
  544. // formats) have ranking in the 1000 range.
  545. return 1000;
  546. }
  547. public function get_embeddable_markers() {
  548. return array('youtube');
  549. }
  550. }
  551. /**
  552. * MP3 player inserted using JavaScript.
  553. *
  554. * @copyright 2011 The Open University
  555. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  556. */
  557. class core_media_player_mp3 extends core_media_player {
  558. public function embed($urls, $name, $width, $height, $options) {
  559. // Use first url (there can actually be only one unless some idiot
  560. // enters two mp3 files as alternatives).
  561. $url = reset($urls);
  562. // Unique id even across different http requests made at the same time
  563. // (for AJAX, iframes).
  564. $id = 'core_media_mp3_' . md5(time() . '_' . rand());
  565. // When Flash or JavaScript are not available only the fallback is displayed,
  566. // using span not div because players are inline elements.
  567. $spanparams = array('id' => $id, 'class' => 'mediaplugin mediaplugin_mp3');
  568. if ($width) {
  569. $spanparams['style'] = 'width: ' . $width . 'px';
  570. }
  571. $output = html_writer::tag('span', core_media_player::PLACEHOLDER, $spanparams);
  572. // We can not use standard JS init because this may be cached
  573. // note: use 'small' size unless embedding in block mode.
  574. $output .= html_writer::script(js_writer::function_call(
  575. 'M.util.add_audio_player', array($id, $url->out(false),
  576. empty($options[core_media::OPTION_BLOCK]))));
  577. return $output;
  578. }
  579. public function get_supported_extensions() {
  580. return array('mp3');
  581. }
  582. public function get_rank() {
  583. return 80;
  584. }
  585. }
  586. /**
  587. * Flash video player inserted using JavaScript.
  588. *
  589. * @copyright 2011 The Open University
  590. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  591. */
  592. class core_media_player_flv extends core_media_player {
  593. public function embed($urls, $name, $width, $height, $options) {
  594. // Use first url (there can actually be only one unless some idiot
  595. // enters two mp3 files as alternatives).
  596. $url = reset($urls);
  597. // Unique id even across different http requests made at the same time
  598. // (for AJAX, iframes).
  599. $id = 'core_media_flv_' . md5(time() . '_' . rand());
  600. // Compute width and height.
  601. $autosize = false;
  602. if (!$width && !$height) {
  603. $width = CORE_MEDIA_VIDEO_WIDTH;
  604. $height = CORE_MEDIA_VIDEO_HEIGHT;
  605. $autosize = true;
  606. }
  607. // Fallback span (will normally contain link).
  608. $output = html_writer::tag('span', core_media_player::PLACEHOLDER,
  609. array('id'=>$id, 'class'=>'mediaplugin mediaplugin_flv'));
  610. // We can not use standard JS init because this may be cached.
  611. $output .= html_writer::script(js_writer::function_call(
  612. 'M.util.add_video_player', array($id, addslashes_js($url->out(false)),
  613. $width, $height, $autosize)));
  614. return $output;
  615. }
  616. public function get_supported_extensions() {
  617. return array('flv', 'f4v');
  618. }
  619. public function get_rank() {
  620. return 70;
  621. }
  622. }
  623. /**
  624. * Embeds Windows Media Player using object tag.
  625. *
  626. * @copyright 2011 The Open University
  627. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  628. */
  629. class core_media_player_wmp extends core_media_player {
  630. public function embed($urls, $name, $width, $height, $options) {
  631. // Get URL (we just use first, probably there is only one).
  632. $firsturl = reset($urls);
  633. $url = $firsturl->out(false);
  634. // Work out width.
  635. if (!$width || !$height) {
  636. // Object tag has default size.
  637. $mpsize = '';
  638. $size = 'width="' . CORE_MEDIA_VIDEO_WIDTH .
  639. '" height="' . (CORE_MEDIA_VIDEO_HEIGHT+64) . '"';
  640. $autosize = 'true';
  641. } else {
  642. $size = 'width="' . $width . '" height="' . ($height + 15) . '"';
  643. $mpsize = 'width="' . $width . '" height="' . ($height + 64) . '"';
  644. $autosize = 'false';
  645. }
  646. // MIME type for object tag.
  647. $mimetype = core_media::get_mimetype($firsturl);
  648. $fallback = core_media_player::PLACEHOLDER;
  649. // Embed code.
  650. return <<<OET
  651. <span class="mediaplugin mediaplugin_wmp">
  652. <object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" $mpsize
  653. standby="Loading Microsoft(R) Windows(R) Media Player components..."
  654. type="application/x-oleobject">
  655. <param name="Filename" value="$url" />
  656. <param name="src" value="$url" />
  657. <param name="url" value="$url" />
  658. <param name="ShowControls" value="true" />
  659. <param name="AutoRewind" value="true" />
  660. <param name="AutoStart" value="false" />
  661. <param name="Autosize" value="$autosize" />
  662. <param name="EnableContextMenu" value="true" />
  663. <param name="TransparentAtStart" value="false" />
  664. <param name="AnimationAtStart" value="false" />
  665. <param name="ShowGotoBar" value="false" />
  666. <param name="EnableFullScreenControls" value="true" />
  667. <param name="uimode" value="full" />
  668. <!--[if !IE]>-->
  669. <object data="$url" type="$mimetype" $size>
  670. <param name="src" value="$url" />
  671. <param name="controller" value="true" />
  672. <param name="autoplay" value="false" />
  673. <param name="autostart" value="false" />
  674. <param name="resize" value="scale" />
  675. <!--<![endif]-->
  676. $fallback
  677. <!--[if !IE]>-->
  678. </object>
  679. <!--<![endif]-->
  680. </object>
  681. </span>
  682. OET;
  683. }
  684. public function get_supported_extensions() {
  685. return array('wmv', 'avi');
  686. }
  687. public function get_rank() {
  688. return 60;
  689. }
  690. }
  691. /**
  692. * Media player using object tag and QuickTime player.
  693. *
  694. * @copyright 2011 The Open University
  695. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  696. */
  697. class core_media_player_qt extends core_media_player {
  698. public function embed($urls, $name, $width, $height, $options) {
  699. // Show first URL.
  700. $firsturl = reset($urls);
  701. $url = $firsturl->out(true);
  702. // Work out size.
  703. if (!$width || !$height) {
  704. $size = 'width="' . CORE_MEDIA_VIDEO_WIDTH .
  705. '" height="' . (CORE_MEDIA_VIDEO_HEIGHT + 15) . '"';
  706. } else {
  707. $size = 'width="' . $width . '" height="' . ($height + 15) . '"';
  708. }
  709. // MIME type for object tag.
  710. $mimetype = core_media::get_mimetype($firsturl);
  711. $fallback = core_media_player::PLACEHOLDER;
  712. // Embed code.
  713. return <<<OET
  714. <span class="mediaplugin mediaplugin_qt">
  715. <object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
  716. codebase="http://www.apple.com/qtactivex/qtplugin.cab" $size>
  717. <param name="pluginspage" value="http://www.apple.com/quicktime/download/" />
  718. <param name="src" value="$url" />
  719. <param name="controller" value="true" />
  720. <param name="loop" value="true" />
  721. <param name="autoplay" value="false" />
  722. <param name="autostart" value="false" />
  723. <param name="scale" value="aspect" />
  724. <!--[if !IE]>-->
  725. <object data="$url" type="$mimetype" $size>
  726. <param name="src" value="$url" />
  727. <param name="pluginurl" value="http://www.apple.com/quicktime/download/" />
  728. <param name="controller" value="true" />
  729. <param name="loop" value="true" />
  730. <param name="autoplay" value="false" />
  731. <param name="autostart" value="false" />
  732. <param name="scale" value="aspect" />
  733. <!--<![endif]-->
  734. $fallback
  735. <!--[if !IE]>-->
  736. </object>
  737. <!--<![endif]-->
  738. </object>
  739. </span>
  740. OET;
  741. }
  742. public function get_supported_extensions() {
  743. return array('mpg', 'mpeg', 'mov', 'mp4', 'm4v', 'm4a');
  744. }
  745. public function get_rank() {
  746. return 50;
  747. }
  748. }
  749. /**
  750. * Media player using object tag and RealPlayer.
  751. *
  752. * Hopefully nobody is using this obsolete format any more!
  753. *
  754. * @copyright 2011 The Open University
  755. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  756. */
  757. class core_media_player_rm extends core_media_player {
  758. public function embed($urls, $name, $width, $height, $options) {
  759. // Show first URL.
  760. $firsturl = reset($urls);
  761. $url = $firsturl->out(true);
  762. // Get name to use as title.
  763. $info = s($this->get_name($name, $urls));
  764. // The previous version of this code has the following comment, which
  765. // I don't understand, but trust it is correct:
  766. // Note: the size is hardcoded intentionally because this does not work anyway!
  767. $width = CORE_MEDIA_VIDEO_WIDTH;
  768. $height = CORE_MEDIA_VIDEO_HEIGHT;
  769. $fallback = core_media_player::PLACEHOLDER;
  770. return <<<OET
  771. <span class="mediaplugin mediaplugin_real">
  772. <object title="$info" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA"
  773. data="$url" width="$width" height="$height"">
  774. <param name="src" value="$url" />
  775. <param name="controls" value="All" />
  776. <!--[if !IE]>-->
  777. <object title="$info" type="audio/x-pn-realaudio-plugin"
  778. data="$url" width="$width" height="$height">
  779. <param name="src" value="$url" />
  780. <param name="controls" value="All" />
  781. <!--<![endif]-->
  782. $fallback
  783. <!--[if !IE]>-->
  784. </object>
  785. <!--<![endif]-->
  786. </object>
  787. </span>
  788. OET;
  789. }
  790. public function get_supported_extensions() {
  791. return array('ra', 'ram', 'rm', 'rv');
  792. }
  793. public function get_rank() {
  794. return 40;
  795. }
  796. }
  797. /**
  798. * Media player for Flash SWF files.
  799. *
  800. * This player contains additional security restriction: it will only be used
  801. * if you add option core_media_player_swf::ALLOW = true.
  802. *
  803. * Code should only set this option if it has verified that the data was
  804. * embedded by a trusted user (e.g. in trust text).
  805. *
  806. * @copyright 2011 The Open University
  807. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  808. */
  809. class core_media_player_swf extends core_media_player {
  810. public function embed($urls, $name, $width, $height, $options) {
  811. self::pick_video_size($width, $height);
  812. $firsturl = reset($urls);
  813. $url = $firsturl->out(true);
  814. $fallback = core_media_player::PLACEHOLDER;
  815. $output = <<<OET
  816. <span class="mediaplugin mediaplugin_swf">
  817. <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="$width" height="$height">
  818. <param name="movie" value="$url" />
  819. <param name="autoplay" value="true" />
  820. <param name="loop" value="true" />
  821. <param name="controller" value="true" />
  822. <param name="scale" value="aspect" />
  823. <param name="base" value="." />
  824. <param name="allowscriptaccess" value="never" />
  825. <!--[if !IE]>-->
  826. <object type="application/x-shockwave-flash" data="$url" width="$width" height="$height">
  827. <param name="controller" value="true" />
  828. <param name="autoplay" value="true" />
  829. <param name="loop" value="true" />
  830. <param name="scale" value="aspect" />
  831. <param name="base" value="." />
  832. <param name="allowscriptaccess" value="never" />
  833. <!--<![endif]-->
  834. $fallback
  835. <!--[if !IE]>-->
  836. </object>
  837. <!--<![endif]-->
  838. </object>
  839. </span>
  840. OET;
  841. return $output;
  842. }
  843. public function get_supported_extensions() {
  844. return array('swf');
  845. }
  846. public function list_supported_urls(array $urls, array $options = array()) {
  847. // Not supported unless the creator is trusted.
  848. if (empty($options[core_media::OPTION_TRUSTED])) {
  849. return array();
  850. }
  851. return parent::list_supported_urls($urls, $options);
  852. }
  853. public function get_rank() {
  854. return 30;
  855. }
  856. }
  857. /**
  858. * Player that creates HTML5 <video> tag.
  859. *
  860. * @copyright 2011 The Open University
  861. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  862. */
  863. class core_media_player_html5video extends core_media_player {
  864. public function embed($urls, $name, $width, $height, $options) {
  865. // Special handling to make videos play on Android devices pre 2.3.
  866. // Note: I tested and 2.3.3 (in emulator) works without, is 533.1 webkit.
  867. $oldandroid = check_browser_version('WebKit Android') &&
  868. !check_browser_version('WebKit Android', '533.1');
  869. // Build array of source tags.
  870. $sources = array();
  871. foreach ($urls as $url) {
  872. $mimetype = core_media::get_mimetype($url);
  873. $source = html_writer::tag('source', '', array('src' => $url, 'type' => $mimetype));
  874. if ($mimetype === 'video/mp4') {
  875. if ($oldandroid) {
  876. // Old Android fails if you specify the type param.
  877. $source = html_writer::tag('source', '', array('src' => $url));
  878. }
  879. // Better add m4v as first source, it might be a bit more
  880. // compatible with problematic browsers.
  881. array_unshift($sources, $source);
  882. } else {
  883. $sources[] = $source;
  884. }
  885. }
  886. $sources = implode("\n", $sources);
  887. $title = s($this->get_name($name, $urls));
  888. if (!$width) {
  889. // No width specified, use system default.
  890. $width = CORE_MEDIA_VIDEO_WIDTH;
  891. }
  892. if (!$height) {
  893. // Let browser choose height automatically.
  894. $size = "width=\"$width\"";
  895. } else {
  896. $size = "width=\"$width\" height=\"$height\"";
  897. }
  898. $sillyscript = '';
  899. $idtag = '';
  900. if ($oldandroid) {
  901. // Old Android does not support 'controls' option.
  902. $id = 'core_media_html5v_' . md5(time() . '_' . rand());
  903. $idtag = 'id="' . $id . '"';
  904. $sillyscript = <<<OET
  905. <script type="text/javascript">
  906. document.getElementById('$id').addEventListener('click', function() {
  907. this.play();
  908. }, false);
  909. </script>
  910. OET;
  911. }
  912. $fallback = core_media_player::PLACEHOLDER;
  913. return <<<OET
  914. <span class="mediaplugin mediaplugin_html5video">
  915. <video $idtag controls="true" $size preload="metadata" title="$title">
  916. $sources
  917. $fallback
  918. </video>
  919. $sillyscript
  920. </span>
  921. OET;
  922. }
  923. public function get_supported_extensions() {
  924. return array('m4v', 'webm', 'ogv', 'mp4');
  925. }
  926. public function list_supported_urls(array $urls, array $options = array()) {
  927. $extensions = $this->get_supported_extensions();
  928. $result = array();
  929. foreach ($urls as $url) {
  930. $ext = core_media::get_extension($url);
  931. if (in_array($ext, $extensions)) {
  932. // Unfortunately html5 video does not handle fallback properly.
  933. // https://www.w3.org/Bugs/Public/show_bug.cgi?id=10975
  934. // That means we need to do browser detect and not use html5 on
  935. // browsers which do not support the given type, otherwise users
  936. // will not even see the fallback link.
  937. // Based on http://en.wikipedia.org/wiki/HTML5_video#Table - this
  938. // is a simplified version, does not take into account old browser
  939. // versions or manual plugins.
  940. if ($ext === 'ogv' || $ext === 'webm') {
  941. // Formats .ogv and .webm are not supported in IE or Safari.
  942. if (check_browser_version('MSIE') || check_browser_version('Safari')) {
  943. continue;
  944. }
  945. } else {
  946. // Formats .m4v and .mp4 are not supported in Firefox or Opera.
  947. if (check_browser_version('Firefox') || check_browser_version('Opera')) {
  948. continue;
  949. }
  950. }
  951. $result[] = $url;
  952. }
  953. }
  954. return $result;
  955. }
  956. public function get_rank() {
  957. return 20;
  958. }
  959. }
  960. /**
  961. * Player that creates HTML5 <audio> tag.
  962. *
  963. * @copyright 2011 The Open University
  964. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  965. */
  966. class core_media_player_html5audio extends core_media_player {
  967. public function embed($urls, $name, $width, $height, $options) {
  968. // Build array of source tags.
  969. $sources = array();
  970. foreach ($urls as $url) {
  971. $mimetype = core_media::get_mimetype($url);
  972. $sources[] = html_writer::tag('source', '', array('src' => $url, 'type' => $mimetype));
  973. }
  974. $sources = implode("\n", $sources);
  975. $title = s($this->get_name($name, $urls));
  976. // Default to not specify size (so it can be changed in css).
  977. $size = '';
  978. if ($width) {
  979. $size = 'width="' . $width . '"';
  980. }
  981. $fallback = core_media_player::PLACEHOLDER;
  982. return <<<OET
  983. <audio controls="true" $size class="mediaplugin mediaplugin_html5audio" preload="no" title="$title">
  984. $sources
  985. $fallback
  986. </audio>
  987. OET;
  988. }
  989. public function get_supported_extensions() {
  990. return array('ogg', 'oga', 'aac', 'm4a', 'mp3');
  991. }
  992. public function list_supported_urls(array $urls, array $options = array()) {
  993. $extensions = $this->get_supported_extensions();
  994. $result = array();
  995. foreach ($urls as $url) {
  996. $ext = core_media::get_extension($url);
  997. if (in_array($ext, $extensions)) {
  998. if ($ext === 'ogg' || $ext === 'oga') {
  999. // Formats .ogg and .oga are not supported in IE or Safari.
  1000. if (check_browser_version('MSIE') || check_browser_version('Safari')) {
  1001. continue;
  1002. }
  1003. } else {
  1004. // Formats .aac, .mp3, and .m4a are not supported in Firefox or Opera.
  1005. if (check_browser_version('Firefox') || check_browser_version('Opera')) {
  1006. continue;
  1007. }
  1008. }
  1009. // Old Android versions (pre 2.3.3) 'support' audio tag but no codecs.
  1010. if (check_browser_version('WebKit Android') &&
  1011. !check_browser_version('WebKit Android', '533.1')) {
  1012. continue;
  1013. }
  1014. $result[] = $url;
  1015. }
  1016. }
  1017. return $result;
  1018. }
  1019. public function get_rank() {
  1020. return 10;
  1021. }
  1022. }
  1023. /**
  1024. * Special media player class that just puts a link.
  1025. *
  1026. * Always enabled, used as the last fallback.
  1027. *
  1028. * @copyright 2011 The Open University
  1029. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  1030. */
  1031. class core_media_player_link extends core_media_player {
  1032. public function embed($urls, $name, $width, $height, $options) {
  1033. // If link is turned off, return empty.
  1034. if (!empty($options[core_media::OPTION_NO_LINK])) {
  1035. return '';
  1036. }
  1037. // Build up link content.
  1038. $output = '';
  1039. foreach ($urls as $url) {
  1040. $title = core_media::get_filename($url);
  1041. $printlink = html_writer::link($url, $title, array('class' => 'mediafallbacklink'));
  1042. if ($output) {
  1043. // Where there are multiple available formats, there are fallback links
  1044. // for all formats, separated by /.
  1045. $output .= ' / ';
  1046. }
  1047. $output .= $printlink;
  1048. }
  1049. return $output;
  1050. }
  1051. public function list_supported_urls(array $urls, array $options = array()) {
  1052. // Supports all URLs.
  1053. return $urls;
  1054. }
  1055. public function is_enabled() {
  1056. // Cannot be disabled.
  1057. return true;
  1058. }
  1059. public function get_rank() {
  1060. return 0;
  1061. }
  1062. }