Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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 handle external links
  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 handle external links (YouTube etc).
  29   *
  30   * As opposed to media files.
  31   *
  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
  36   */
  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
  43       */
  44      protected $matches;
  45  
  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
  54       */
  55      const END_LINK_REGEX_PART = '[^#]*(#d=([\d]{1,4})x([\d]{1,4}))?~si';
  56  
  57      public function embed($urls, $name, $width, $height, $options) {
  58          return $this->embed_external(reset($urls), $name, $width, $height, $options);
  59      }
  60  
  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
  69       */
  70      protected abstract function embed_external(moodle_url $url, $name, $width, $height, $options);
  71  
  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();
  76          }
  77          $url = reset($urls);
  78  
  79          // Check against regex.
  80          if (preg_match($this->get_regex(), $url->out(false), $this->matches)) {
  81              return array($url);
  82          }
  83  
  84          return array();
  85      }
  86  
  87      /**
  88       * Returns regular expression used to match URLs that this player handles
  89       * @return string PHP regular expression e.g. '~^https?://example.org/~'
  90       */
  91      protected function get_regex() {
  92          return '~^unsupported~';
  93      }
  94  
  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)
 101       */
 102      protected static function fix_match_count(&$matches, $count) {
 103          for ($i = count($matches); $i <= $count; $i++) {
 104              $matches[$i] = false;
 105          }
 106      }
 107  }