示例#1
0
 function mediaplugin_filter($hotpot, $text, $options = array())
 {
     global $CFG, $PAGE;
     static $eolas_fix_applied = 0;
     // insert media players using Moodle's standard mediaplugin filter
     $filter = new filter_mediaplugin($hotpot->context, array());
     $newtext = $filter->filter($text);
     if ($newtext == $text) {
         // do nothing
     } else {
         if ($eolas_fix_applied == $hotpot->id) {
             // eolas_fix.js and ufo.js have already been added for this quiz
         } else {
             if ($eolas_fix_applied == 0) {
                 // 1st quiz - eolas_fix.js was added by filter/mediaplugin/filter.php
             } else {
                 // 2nd (or later) quiz - e.g. we are being called by hotpot_cron()
                 $PAGE->requires->js('/mod/hotpot/mediafilter/eolas_fix.js');
                 //$newtext .= '<script defer="defer" src="'.$CFG->wwwroot.'/mod/hotpot/mediafilter/eolas_fix.js" type="text/javascript"></script>';
             }
             $PAGE->requires->js('/mod/hotpot/mediafilter/ufo.js', true);
             //$newtext .= '<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/hotpot/mediafilter/ufo.js"></script>';
             $eolas_fix_applied = $hotpot->id;
         }
     }
     return $newtext;
 }
示例#2
0
    function test_filter_mediaplugin_link()
    {
        global $CFG;
        $this->resetAfterTest(true);
        // we need to enable the plugins somehow
        $CFG->core_media_enable_youtube = 1;
        $CFG->core_media_enable_vimeo = 1;
        $CFG->core_media_enable_mp3 = 1;
        $CFG->core_media_enable_flv = 1;
        $CFG->core_media_enable_swf = 1;
        $CFG->core_media_enable_html5audio = 1;
        $CFG->core_media_enable_html5video = 1;
        $CFG->core_media_enable_qt = 1;
        $CFG->core_media_enable_wmp = 1;
        $CFG->core_media_enable_rm = 1;
        $filterplugin = new filter_mediaplugin(null, array());
        $longurl = '<a href="http://moodle/.mp4">my test file</a>';
        $longhref = '';
        do {
            $longhref .= 'a';
        } while (strlen($longhref) + strlen($longurl) < 4095);
        $longurl = '<a href="http://moodle/' . $longhref . '.mp4">my test file</a>';
        $validtexts = array('<a href="http://moodle.org/testfile/test.mp3">test mp3</a>', '<a href="http://moodle.org/testfile/test.ogg">test ogg</a>', '<a id="movie player" class="center" href="http://moodle.org/testfile/test.mpg">test mpg</a>', '<a href="http://moodle.org/testfile/test.ram">test</a>', '<a href="http://www.youtube.com/watch?v=JghQgA2HMX8" class="href=css">test file</a>', '<a href="http://www.youtube-nocookie.com/watch?v=JghQgA2HMX8" class="href=css">test file</a>', '<a href="http://youtu.be/JghQgA2HMX8" class="href=css">test file</a>', '<a href="http://y2u.be/JghQgA2HMX8" class="href=css">test file</a>', '<a class="youtube" href="http://www.youtube.com/watch?v=JghQgA2HMX8">test file</a>', '<a class="_blanktarget" href="http://moodle.org/testfile/test.flv?d=100x100">test flv</a>', '<a class="hrefcss" href="http://www.youtube.com/watch?v=JghQgA2HMX8">test file</a>', '<a  class="content"     href="http://moodle.org/testfile/test.avi">test mp3</a>', '<a     id="audio"      href="http://moodle.org/testfile/test.mp3">test mp3</a>', '<a  href="http://moodle.org/testfile/test.mp3">test mp3</a>', '<a     href="http://moodle.org/testfile/test.mp3">test mp3</a>', '<a     href="http://www.youtube.com/watch?v=JghQgA2HMX8?d=200x200">youtube\'s</a>', '<a
                            href="http://moodle.org/testfile/test.mp3">
                            test mp3</a>', '<a                         class="content"


                            href="http://moodle.org/testfile/test.avi">test mp3
                                    </a>', '<a             href="http://www.youtube.com/watch?v=JghQgA2HMX8?d=200x200"     >youtube\'s</a>', $longurl);
        //test for valid link
        foreach ($validtexts as $text) {
            $msg = "Testing text: " . $text;
            $filter = $filterplugin->filter($text);
            $this->assertNotEquals($text, $filter, $msg);
        }
        $insertpoint = strrpos($longurl, 'http://');
        $longurl = substr_replace($longurl, 'http://pushover4096chars', $insertpoint, 0);
        $originalurl = '<p>Some text.</p><pre style="color: rgb(0, 0, 0); line-height: normal;">' . '<a href="https://www.youtube.com/watch?v=uUhWl9Lm3OM">Valid link</a></pre><pre style="color: rgb(0, 0, 0); line-height: normal;">';
        $paddedurl = str_pad($originalurl, 6000, 'z');
        $validpaddedurl = '<p>Some text.</p><pre style="color: rgb(0, 0, 0); line-height: normal;"><span class="mediaplugin mediaplugin_youtube">
<iframe title="Valid link" width="400" height="300"
  src="https://www.youtube.com/embed/uUhWl9Lm3OM?rel=0&wmode=transparent" frameborder="0" allowfullscreen="1"></iframe>
</span></pre><pre style="color: rgb(0, 0, 0); line-height: normal;">';
        $validpaddedurl = str_pad($validpaddedurl, 6000 + (strlen($validpaddedurl) - strlen($originalurl)), 'z');
        $invalidtexts = array('<a class="_blanktarget">href="http://moodle.org/testfile/test.mp3"</a>', '<a>test test</a>', '<a >test test</a>', '<a     >test test</a>', '<a >test test</a>', '<ahref="http://moodle.org/testfile/test.mp3">sample</a>', '<a href="" test></a>', '<a href="http://www.moodle.com/path/to?#param=29">test</a>', '<a href="http://moodle.org/testfile/test.mp3">test mp3', '<a href="http://moodle.org/testfile/test.mp3"test</a>', '<a href="http://moodle.org/testfile/">test</a>', '<href="http://moodle.org/testfile/test.avi">test</a>', '<abbr href="http://moodle.org/testfile/test.mp3">test mp3</abbr>', '<ahref="http://moodle.org/testfile/test.mp3">test mp3</a>', '<aclass="content" href="http://moodle.org/testfile/test.mp3">test mp3</a>', $longurl);
        //test for invalid link
        foreach ($invalidtexts as $text) {
            $msg = "Testing text: " . $text;
            $filter = $filterplugin->filter($text);
            $this->assertEquals($text, $filter, $msg);
        }
        // Valid mediaurl followed by a longurl.
        $precededlongurl = '<a href="http://moodle.org/testfile/test.mp3">test.mp3</a>' . $longurl;
        $filter = $filterplugin->filter($precededlongurl);
        $this->assertEquals(1, substr_count($filter, 'M.util.add_audio_player'));
        $this->assertContains($longurl, $filter);
        // Testing for cases where: to be filtered content has 6+ text afterwards.
        $filter = $filterplugin->filter($paddedurl);
        $this->assertEquals($validpaddedurl, $filter, $msg);
    }
    function test_filter_mediaplugin_link()
    {
        global $CFG;
        // we need to enable the plugins somehow
        $oldcfg = clone $CFG;
        // very, very ugly hack
        $CFG->filter_mediaplugin_enable_youtube = 1;
        $CFG->filter_mediaplugin_enable_vimeo = 1;
        $CFG->filter_mediaplugin_enable_mp3 = 1;
        $CFG->filter_mediaplugin_enable_flv = 1;
        $CFG->filter_mediaplugin_enable_swf = 1;
        $CFG->filter_mediaplugin_enable_html5audio = 1;
        $CFG->filter_mediaplugin_enable_html5video = 1;
        $CFG->filter_mediaplugin_enable_qt = 1;
        $CFG->filter_mediaplugin_enable_wmp = 1;
        $CFG->filter_mediaplugin_enable_rm = 1;
        $filterplugin = new filter_mediaplugin(null, array());
        $validtexts = array('<a href="http://moodle.org/testfile/test.mp3">test mp3</a>', '<a href="http://moodle.org/testfile/test.ogg">test ogg</a>', '<a id="movie player" class="center" href="http://moodle.org/testfile/test.mpg">test mpg</a>', '<a href="http://moodle.org/testfile/test.ram">test</a>', '<a href="http://www.youtube.com/watch?v=JghQgA2HMX8" class="href=css">test file</a>', '<a class="youtube" href="http://www.youtube.com/watch?v=JghQgA2HMX8">test file</a>', '<a class="_blanktarget" href="http://moodle.org/testfile/test.flv?d=100x100">test flv</a>', '<a class="hrefcss" href="http://www.youtube.com/watch?v=JghQgA2HMX8">test file</a>', '<a  class="content"     href="http://moodle.org/testfile/test.avi">test mp3</a>', '<a     id="audio"      href="http://moodle.org/testfile/test.mp3">test mp3</a>', '<a  href="http://moodle.org/testfile/test.mp3">test mp3</a>', '<a     href="http://moodle.org/testfile/test.mp3">test mp3</a>', '<a     href="http://www.youtube.com/watch?v=JghQgA2HMX8?d=200x200">youtube\'s</a>', '<a
                            href="http://moodle.org/testfile/test.mp3">
                            test mp3</a>', '<a                         class="content"


                            href="http://moodle.org/testfile/test.avi">test mp3
                                    </a>', '<a             href="http://www.youtube.com/watch?v=JghQgA2HMX8?d=200x200"     >youtube\'s</a>');
        //test for valid link
        foreach ($validtexts as $text) {
            $msg = "Testing text: " . $text;
            $filter = $filterplugin->filter($text);
            $this->assertNotEqual($text, $filter, $msg);
        }
        $invalidtexts = array('<a class="_blanktarget">href="http://moodle.org/testfile/test.mp3"</a>', '<a>test test</a>', '<a >test test</a>', '<a     >test test</a>', '<a >test test</a>', '<ahref="http://moodle.org/testfile/test.mp3">sample</a>', '<a href="" test></a>', '<a class="_blanktarget" href="http://moodle.org/testfile/test.flv?d=100x">test flv</a>', '<a href="http://www.moodle.com/path/to?#param=29">test</a>', '<a href="http://moodle.org/testfile/test.mp3">test mp3', '<a href="http://moodle.org/testfile/test.mp3"test</a>', '<a href="http://moodle.org/testfile/">test</a>', '<href="http://moodle.org/testfile/test.avi">test</a>', '<abbr href="http://moodle.org/testfile/test.mp3">test mp3</abbr>', '<ahref="http://moodle.org/testfile/test.mp3">test mp3</a>', '<aclass="content" href="http://moodle.org/testfile/test.mp3">test mp3</a>');
        //test for invalid link
        foreach ($invalidtexts as $text) {
            $msg = "Testing text: " . $text;
            $filter = $filterplugin->filter($text);
            $this->assertEqual($text, $filter, $msg);
        }
        $CFG = $oldcfg;
        // very, very ugly hack
    }
示例#4
0
 /**
  *
  */
 public function display_browse($entry, $type = '')
 {
     global $CFG;
     $field = $this->_field;
     $fieldid = $field->id;
     if (isset($entry->{"c{$fieldid}_content"})) {
         $url = $entry->{"c{$fieldid}_content"};
         if (empty($url) or $url == 'http://') {
             return '';
         }
         // Simple url text.
         if (empty($type)) {
             return $url;
         }
         // Param2 forces the text to something.
         if ($field->param2) {
             $alttext = s($field->param2);
         } else {
             $alttext = empty($entry->{"c{$fieldid}_content1"}) ? $url : $entry->{"c{$fieldid}_content1"};
         }
         // Linking.
         if ($type == 'link') {
             return html_writer::link($url, $alttext);
         }
         // Image.
         if ($type == 'image') {
             return html_writer::empty_tag('img', array('src' => $url));
         }
         // Image flexible.
         if ($type == 'imageflex') {
             return html_writer::empty_tag('img', array('src' => $url, 'style' => 'width:100%'));
         }
         // Media.
         if ($type == 'media') {
             require_once "{$CFG->dirroot}/filter/mediaplugin/filter.php";
             $mpfilter = new filter_mediaplugin($field->get_df()->context, array());
             return $mpfilter->filter(html_writer::link($url, ''));
         }
     }
     return '';
 }
示例#5
0
 /**
  * fix_link
  *
  * @param xxx $match
  * @param xxx $output (passed by reference)
  * @param xxx $options (optional, default=array)
  * @return xxx
  */
 function fix_link($output, $options, $match)
 {
     global $CFG, $PAGE;
     static $load_flowplayer = 0;
     static $eolas_fix_applied = 0;
     if (is_string($match)) {
         $link = $match;
         $unicode = '';
     } else {
         if (is_array($match)) {
             $link = $match[0];
             $unicode = $match[2];
         } else {
             debugging('Oops, $match is not an array or string !');
             $args = func_get_args();
             print_object(count($args));
             die;
         }
     }
     if (array_key_exists('unicode', $options)) {
         $unicode = $options['unicode'];
     }
     // set player default, if necessary
     if (empty($options['player'])) {
         $options['player'] = $this->defaultplayer;
     }
     // hide player if required
     if (array_key_exists('hide', $options)) {
         if ($options['hide']) {
             $options['width'] = 1;
             $options['height'] = 1;
             if ($options['player'] == 'moodle') {
                 $options['autoPlay'] = 'yes';
                 $options['waitForPlay'] = 'no';
             }
         }
         unset($options['hide']);
         unset($options['center']);
     }
     // call filter to add media player
     if (empty($options['movie']) && $options['player'] == 'moodle') {
         $filter = new filter_mediaplugin($output->hotpot->context, array());
         $object = $filter->filter($link);
         if ($object == $link) {
             // do nothing
         } else {
             if ($eolas_fix_applied == $output->hotpot->id) {
                 // eolas_fix.js and ufo.js have already been added for this quiz
             } else {
                 if ($eolas_fix_applied == 0) {
                     // 1st quiz - eolas_fix.js was added by filter/mediaplugin/filter.php
                 } else {
                     // 2nd (or later) quiz - e.g. we are being called by hotpot_cron()
                     $PAGE->requires->js('/mod/hotpot/mediafilter/eolas_fix.js');
                     //$object .= '<script defer="defer" src="'.$CFG->wwwroot.'/mod/hotpot/mediafilter/eolas_fix.js" type="text/javascript"></script>';
                 }
                 $PAGE->requires->js('/mod/hotpot/mediafilter/ufo.js', true);
                 //$object .= '<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/hotpot/mediafilter/ufo.js"></script>';
                 $eolas_fix_applied = $output->hotpot->id;
             }
         }
         $search = '/(flashvars:")([^"]*)(")/';
         $callback = array($this, 'fix_flashvars');
         $callback = partial($callback, $options);
         $object = preg_replace_callback($search, $callback, $object);
         // fix height and width (e.g. height="15", "height": 15)
         foreach (array('width', 'height') as $option) {
             if (array_key_exists($option, $options)) {
                 $search = array('/(?<=' . $option . ':")\\w+(?=")/i', '/(?<="' . $option . '": )\\w+/i');
                 $object = preg_replace($search, $options[$option], $object);
             }
         }
     } else {
         $object = $this->mediaplugin_filter($output->hotpot, $link, $options);
     }
     // center content if required
     if (array_key_exists('center', $options)) {
         if ($options['center']) {
             $object = '<div style="text-align:center;">' . $object . '</div>';
         }
         unset($options['center']);
     }
     // if required, remove the link contained in the object tag
     // Note: strcmp() returns true if strings are different
     $player = $options['player'];
     if ($this->players[$player]->removelink && strcmp($object, $link)) {
         $search = '/<a href="[^"]*"[^>]*>[^<]*<\\/a>\\s*/is';
         $object = preg_replace($search, '', $object);
     }
     // extract the external javascripts
     $search = '/\\s*<script[^>]*src[^>]*>.*?<\\/script>\\s*/is';
     if (preg_match_all($search, $object, $scripts, PREG_OFFSET_CAPTURE)) {
         foreach (array_reverse($scripts[0]) as $script) {
             // $script: [0] = matched string, [1] = offset to start of string
             // remove the javascript from the player
             $object = substr_replace($object, "\n", $script[1], strlen($script[0]));
             // store this javascript so it can be run later
             $this->js_external = trim($script[0]) . "\n" . $this->js_external;
         }
     }
     // extract the inline javascripts
     $search = '/\\s*<script[^>]*>.*?<\\/script>\\s*/is';
     if (preg_match_all($search, $object, $scripts, PREG_OFFSET_CAPTURE)) {
         foreach (array_reverse($scripts[0]) as $script) {
             // $script: [0] = matched string, [1] = offset to start of string
             // remove the script from the player
             $object = substr_replace($object, "\n", $script[1], strlen($script[0]));
             // format the script (helps readability of the html source)
             $script[0] = $this->format_script($script[0]);
             //store this javascript so it can be run later
             $this->js_inline = trim($script[0]) . "\n" . $this->js_inline;
         }
         if ($this->js_inline && $load_flowplayer == 0) {
             $load_flowplayer = 1;
             $this->js_inline .= '' . '<script type="text/javascript">' . "\n" . "//<![CDATA[\n" . "\t" . 'M.util.load_flowplayer();' . "\n" . "//]]>\n" . "</script>\n";
         }
     }
     // remove white space between tags, standardize other white space to a single space
     $object = preg_replace('/(?<=>)\\s+(?=<)/', '', $object);
     $object = preg_replace('/\\s+/', ' ', $object);
     if ($unicode) {
         // encode angle brackets as javascript $unicode
         $object = str_replace('<', '\\u003C', $object);
         $object = str_replace('>', '\\u003E', $object);
         //$object = str_replace('&amp;', '&', $object);
     }
     return $object;
 }
示例#6
0
require_once $CFG->dirroot . '/filter/mediaplugin/filter.php';
// Only available to site admins.
require_login();
if (!is_siteadmin()) {
    print_error('nopermissions', 'error', '', 'perftest');
}
// Set up page.
$PAGE->set_context(context_system::instance());
$PAGE->set_url(new moodle_url('/filter/mediaplugin/dev/perftest.php'));
$PAGE->set_heading($SITE->fullname);
print $OUTPUT->header();
// Enable all players.
$enabledmediaplugins = \core\plugininfo\media::get_enabled_plugins();
\core\plugininfo\media::set_enabled_plugins('vimeo,youtube,videojs,html5audio,html5video,swf');
// Create plugin.
$filterplugin = new filter_mediaplugin(null, array());
// Note: As this is a developer test page, language strings are not used: all
// text is English-only.
/**
 * Starts time counter.
 */
function filter_mediaplugin_perf_start()
{
    global $filter_mediaplugin_starttime;
    $filter_mediaplugin_starttime = microtime(true);
}
/**
 * Ends and displays time counter.
 * @param string $name Counter name to display
 */
function filter_mediaplugin_perf_stop($name)