[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/filter/mediaplugin/ -> filter.php (source)

   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   *  Media plugin filtering
  19   *
  20   *  This filter will replace any links to a media file with
  21   *  a media plugin that plays that media inline
  22   *
  23   * @package    filter
  24   * @subpackage mediaplugin
  25   * @copyright  2004 onwards Martin Dougiamas  {@link http://moodle.com}
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * Automatic media embedding filter class.
  33   *
  34   * It is highly recommended to configure servers to be compatible with our slasharguments,
  35   * otherwise the "?d=600x400" may not work.
  36   *
  37   * @package    filter
  38   * @subpackage mediaplugin
  39   * @copyright  2004 onwards Martin Dougiamas  {@link http://moodle.com}
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class filter_mediaplugin extends moodle_text_filter {
  43      /** @var bool True if currently filtering trusted text */
  44      private $trusted;
  45      /** @var core_media_renderer Media renderer */
  46      private $mediarenderer;
  47      /** @var string Partial regex pattern indicating possible embeddable content */
  48      private $embedmarkers;
  49  
  50      public function filter($text, array $options = array()) {
  51          global $CFG, $PAGE;
  52  
  53          if (!is_string($text) or empty($text)) {
  54              // non string data can not be filtered anyway
  55              return $text;
  56          }
  57  
  58          if (stripos($text, '</a>') === false) {
  59              // Performance shortcut - if not </a> tag, nothing can match.
  60              return $text;
  61          }
  62  
  63          if (!$this->mediarenderer) {
  64              $this->mediarenderer = $PAGE->get_renderer('core', 'media');
  65              $this->embedmarkers = $this->mediarenderer->get_embeddable_markers();
  66          }
  67  
  68          // Check SWF permissions.
  69          $this->trusted = !empty($options['noclean']) or !empty($CFG->allowobjectembed);
  70  
  71          // Handle all links that contain any 'embeddable' marker text (it could
  72          // do all links, but the embeddable markers thing should make it faster
  73          // by meaning for most links it doesn't drop into PHP code).
  74          $newtext = preg_replace_callback($re = '~<a\s[^>]*href="([^"]*(?:' .
  75                  $this->embedmarkers . ')[^"]*)"[^>]*>([^>]*)</a>~is',
  76                  array($this, 'callback'), $text);
  77  
  78          if (empty($newtext) or $newtext === $text) {
  79              // error or not filtered
  80              return $text;
  81          }
  82  
  83          return $newtext;
  84      }
  85  
  86      /**
  87       * Replace link with embedded content, if supported.
  88       *
  89       * @param array $matches
  90       * @return string
  91       */
  92      private function callback(array $matches) {
  93          global $CFG, $PAGE;
  94          // Check if we ignore it.
  95          if (preg_match('/class="[^"]*nomediaplugin/i', $matches[0])) {
  96              return $matches[0];
  97          }
  98  
  99          // Get name.
 100          $name = trim($matches[2]);
 101          if (empty($name) or strpos($name, 'http') === 0) {
 102              $name = ''; // Use default name.
 103          }
 104  
 105          // Split provided URL into alternatives.
 106          $urls = core_media::split_alternatives($matches[1], $width, $height);
 107  
 108          $options = array();
 109  
 110          // Allow SWF (or not).
 111          if ($this->trusted) {
 112              $options[core_media::OPTION_TRUSTED] = true;
 113          }
 114  
 115          // We could test whether embed is possible using can_embed, but to save
 116          // time, let's just embed it with the 'fallback to blank' option which
 117          // does most of the same stuff anyhow.
 118          $options[core_media::OPTION_FALLBACK_TO_BLANK] = true;
 119  
 120          // NOTE: Options are not passed through from filter because the 'embed'
 121          // code does not recognise filter options (it's a different kind of
 122          // option-space) as it can be used in non-filter situations.
 123          $result = $this->mediarenderer->embed_alternatives($urls, $name, $width, $height, $options);
 124  
 125          // If something was embedded, return it, otherwise return original.
 126          if ($result !== '') {
 127              return $result;
 128          } else {
 129              return $matches[0];
 130          }
 131      }
 132  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1