/** * Test embedding without media filter (for example for displaying URL resorce). */ public function test_embed_url() { global $CFG; $url = new moodle_url('http://vimeo.com/1176321'); $manager = core_media_manager::instance(); $embedoptions = array(core_media_manager::OPTION_TRUSTED => true, core_media_manager::OPTION_BLOCK => true); $this->assertTrue($manager->can_embed_url($url, $embedoptions)); $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions); $this->assertRegExp('~mediaplugin_vimeo~', $content); $this->assertRegExp('~</iframe>~', $content); $this->assertRegExp('~width="' . $CFG->media_default_width . '" height="' . $CFG->media_default_height . '"~', $content); // Repeat sending the specific size to the manager. $content = $manager->embed_url($url, 'New file', 123, 50, $embedoptions); $this->assertRegExp('~width="123" height="50"~', $content); }
public function list_supported_urls(array $urls, array $options = array()) { $extensions = $this->get_supported_extensions(); $result = array(); foreach ($urls as $url) { $ext = core_media_manager::instance()->get_extension($url); if (in_array('.' . $ext, $extensions) && core_useragent::supports_html5($ext)) { // Unfortunately html5 video does not handle fallback properly. // https://www.w3.org/Bugs/Public/show_bug.cgi?id=10975 // That means we need to do browser detect and not use html5 on // browsers which do not support the given type, otherwise users // will not even see the fallback link. $result[] = $url; } } return $result; }
/** * Test embedding without media filter (for example for displaying file resorce). */ public function test_embed_url() { global $CFG; $url = new moodle_url('http://example.org/1.webm'); $manager = core_media_manager::instance(); $embedoptions = array(core_media_manager::OPTION_TRUSTED => true, core_media_manager::OPTION_BLOCK => true); $this->assertTrue($manager->can_embed_url($url, $embedoptions)); $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions); $this->assertRegExp('~mediaplugin_html5video~', $content); $this->assertRegExp('~</video>~', $content); $this->assertRegExp('~title="Test & file"~', $content); $this->assertRegExp('~width="' . $CFG->media_default_width . '"~', $content); $this->assertNotRegExp('~height=~', $content); // Allow to set automatic height. // Repeat sending the specific size to the manager. $content = $manager->embed_url($url, 'New file', 123, 50, $embedoptions); $this->assertRegExp('~width="123" height="50"~', $content); }
/** * Test embedding without media filter (for example for displaying file resorce). */ public function test_embed_url() { global $CFG; $url = new moodle_url('http://example.org/1.wav'); $manager = core_media_manager::instance(); $embedoptions = array(core_media_manager::OPTION_TRUSTED => true, core_media_manager::OPTION_BLOCK => true); $this->assertTrue($manager->can_embed_url($url, $embedoptions)); $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions); $this->assertRegExp('~mediaplugin_html5audio~', $content); $this->assertRegExp('~</audio>~', $content); $this->assertRegExp('~title="Test & file"~', $content); // Do not set default width/height (it's an audio after all). $this->assertNotRegExp('~width=~', $content); $this->assertNotRegExp('~height=~', $content); // This plugin ignores size settings. $this->assertNotRegExp('~width=~', $content); $this->assertNotRegExp('~height=~', $content); }
/** * Test embedding without media filter (for example for displaying file resorce). */ public function test_embed_url() { global $CFG; $url = new moodle_url('http://example.org/1.swf'); $manager = core_media_manager::instance(); $embedoptions = array(core_media_manager::OPTION_TRUSTED => true, core_media_manager::OPTION_BLOCK => true); $this->assertTrue($manager->can_embed_url($url, $embedoptions)); $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions); $this->assertRegExp('~mediaplugin_swf~', $content); $this->assertRegExp('~</object>~', $content); $this->assertRegExp('~width="' . $CFG->media_default_width . '" height="' . $CFG->media_default_height . '"~', $content); // Repeat sending the specific size to the manager. $content = $manager->embed_url($url, 'New file', 123, 50, $embedoptions); $this->assertRegExp('~width="123" height="50"~', $content); // Not working without trust! $embedoptions = array(core_media_manager::OPTION_BLOCK => true); $this->assertFalse($manager->can_embed_url($url, $embedoptions)); $content = $manager->embed_url($url, 'Test & file', 0, 0, $embedoptions); $this->assertNotRegExp('~mediaplugin_swf~', $content); }
/** * Display embedded resource file. * @param object $resource * @param object $cm * @param object $course * @param stored_file $file main file * @return does not return */ function resource_display_embed($resource, $cm, $course, $file) { global $CFG, $PAGE, $OUTPUT; $clicktoopen = resource_get_clicktoopen($file, $resource->revision); $context = context_module::instance($cm->id); $path = '/' . $context->id . '/mod_resource/content/' . $resource->revision . $file->get_filepath() . $file->get_filename(); $fullurl = file_encode_url($CFG->wwwroot . '/pluginfile.php', $path, false); $moodleurl = new moodle_url('/pluginfile.php' . $path); $mimetype = $file->get_mimetype(); $title = $resource->name; $extension = resourcelib_get_extension($file->get_filename()); $mediamanager = core_media_manager::instance(); $embedoptions = array(core_media_manager::OPTION_TRUSTED => true, core_media_manager::OPTION_BLOCK => true); if (file_mimetype_in_typegroup($mimetype, 'web_image')) { // It's an image $code = resourcelib_embed_image($fullurl, $title); } else { if ($mimetype === 'application/pdf') { // PDF document $code = resourcelib_embed_pdf($fullurl, $title, $clicktoopen); } else { if ($mediamanager->can_embed_url($moodleurl, $embedoptions)) { // Media (audio/video) file. $code = $mediamanager->embed_url($moodleurl, $title, 0, 0, $embedoptions); } else { // anything else - just try object tag enlarged as much as possible $code = resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype); } } } resource_print_header($resource, $cm, $course); resource_print_heading($resource, $cm, $course); echo $code; resource_print_intro($resource, $cm, $course); echo $OUTPUT->footer(); die; }
/** * Resets cached singleton instance. To be used after $CFG->media_plugins_sortorder is modified */ public static function reset_caches() { self::$instance = null; }
/** * Display embedded url file. * @param object $url * @param object $cm * @param object $course * @return does not return */ function url_display_embed($url, $cm, $course) { global $CFG, $PAGE, $OUTPUT; $mimetype = resourcelib_guess_url_mimetype($url->externalurl); $fullurl = url_get_full_url($url, $cm, $course); $title = $url->name; $link = html_writer::tag('a', $fullurl, array('href' => str_replace('&', '&', $fullurl))); $clicktoopen = get_string('clicktoopen', 'url', $link); $moodleurl = new moodle_url($fullurl); $extension = resourcelib_get_extension($url->externalurl); $mediamanager = core_media_manager::instance(); $embedoptions = array(core_media_manager::OPTION_TRUSTED => true, core_media_manager::OPTION_BLOCK => true); if (in_array($mimetype, array('image/gif', 'image/jpeg', 'image/png'))) { // It's an image $code = resourcelib_embed_image($fullurl, $title); } else { if ($mediamanager->can_embed_url($moodleurl, $embedoptions)) { // Media (audio/video) file. $code = $mediamanager->embed_url($moodleurl, $title, 0, 0, $embedoptions); } else { // anything else - just try object tag enlarged as much as possible $code = resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype); } } url_print_header($url, $cm, $course); url_print_heading($url, $cm, $course); echo $code; url_print_intro($url, $cm, $course); echo $OUTPUT->footer(); die; }
// You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>. /** * Provides A/V preview features for the TinyMCE editor Moodle Media plugin. * The preview is included in an iframe within the popup dialog. * * @package tinymce_moodlemedia * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ require __DIR__ . '/../../../../../config.php'; require_once $CFG->libdir . '/filelib.php'; // Decode the url - it can not be passed around unencoded because security filters might block it. $media = required_param('media', PARAM_RAW); $media = base64_decode($media); $url = clean_param($media, PARAM_URL); $url = new moodle_url($url); // Now output this file which is super-simple $PAGE->set_pagelayout('embedded'); $PAGE->set_url(new moodle_url('/lib/editor/tinymce/plugins/moodlemedia/preview.php')); $PAGE->set_context(context_system::instance()); $PAGE->add_body_class('core_media_preview'); echo $OUTPUT->header(); $mediarenderer = core_media_manager::instance(); if (isloggedin() and !isguestuser() and $mediarenderer->can_embed_url($url)) { require_sesskey(); echo $mediarenderer->embed_url($url); } else { print_string('nopreview', 'tinymce_moodlemedia'); } echo $OUTPUT->footer();
/** * Obtains suitable name for media. Uses specified name if there is one, * otherwise makes one up. * @param string $name User-specified name ('' if none) * @param array $urls Array of moodle_url used to make up name * @return string Name */ protected function get_name($name, $urls) { // If there is a specified name, use that. if ($name) { return $name; } // Get filename of first URL. $url = reset($urls); $name = core_media_manager::instance()->get_filename($url); // If there is more than one url, strip the extension as we could be // referring to a different one or several at once. if (count($urls) > 1) { $name = preg_replace('~\\.[^.]*$~', '', $name); } return $name; }
/** * Test that VideoJS can embed youtube videos. */ public function test_youtube() { set_config('youtube', 1, 'media_videojs'); set_config('useflash', 0, 'media_videojs'); $manager = core_media_manager::instance(); // Format: youtube. $url = new moodle_url('http://www.youtube.com/watch?v=vyrwMmsufJc'); $t = $manager->embed_url($url); $this->youtube_plugin_engaged($t); $url = new moodle_url('http://www.youtube.com/v/vyrwMmsufJc'); $t = $manager->embed_url($url); $this->youtube_plugin_engaged($t); // Format: youtube video within playlist - this will be played by video.js but without tracks selection. $url = new moodle_url('https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0'); $t = $manager->embed_url($url); $this->youtube_plugin_engaged($t); $this->assertContains('list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0', $t); // Format: youtube video with start time. $url = new moodle_url('https://www.youtube.com/watch?v=JNJMF1l3udM&t=1h11s'); $t = $manager->embed_url($url); $this->youtube_plugin_engaged($t); $this->assertContains('t=1h11s', $t); // Format: youtube video within playlist with start time. $url = new moodle_url('https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0&t=1m5s'); $t = $manager->embed_url($url); $this->youtube_plugin_engaged($t); $this->assertContains('list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0', $t); $this->assertContains('t=1m5s', $t); // Format: youtube playlist - not supported. $url = new moodle_url('http://www.youtube.com/view_play_list?p=PL6E18E2927047B662'); $t = $manager->embed_url($url); $this->assertNotContains('mediaplugin_videojs', $t); $url = new moodle_url('http://www.youtube.com/playlist?list=PL6E18E2927047B662'); $t = $manager->embed_url($url); $this->assertNotContains('mediaplugin_videojs', $t); $url = new moodle_url('http://www.youtube.com/p/PL6E18E2927047B662'); $t = $manager->embed_url($url); $this->assertNotContains('mediaplugin_videojs', $t); }
/** * Renders media files (audio or video) using suitable embedded player. * * Wrapper for {@link core_media_manager::embed_alternatives()} * * @param array $urls Array of moodle_url to media files * @param string $name Optional user-readable name to display in download link * @param int $width Width in pixels (optional) * @param int $height Height in pixels (optional) * @param array $options Array of key/value pairs * @return string HTML content of embed */ protected function embed_alternatives($urls, $name, $width, $height, $options) { // Allow SWF (or not). if ($this->trusted) { $options[core_media_manager::OPTION_TRUSTED] = true; } // We could test whether embed is possible using can_embed, but to save // time, let's just embed it with the 'fallback to blank' option which // does most of the same stuff anyhow. $options[core_media_manager::OPTION_FALLBACK_TO_BLANK] = true; // NOTE: Options are not passed through from filter because the 'embed' // code does not recognise filter options (it's a different kind of // option-space) as it can be used in non-filter situations. $result = core_media_manager::instance()->embed_alternatives($urls, $name, $width, $height, $options); // If something was embedded, return it, otherwise return original. if ($result !== '') { return $result; } else { return $options[core_media_manager::OPTION_ORIGINAL_TEXT]; } }
/** * Obtains a list of markers that can be used in a regular expression when * searching for URLs that can be embedded by any player type. * * This string is used to improve peformance of regex matching by ensuring * that the (presumably C) regex code can do a quick keyword check on the * URL part of a link to see if it matches one of these, rather than having * to go into PHP code for every single link to see if it can be embedded. * * @return string String suitable for use in regex such as '(\.mp4|\.flv)' */ public function get_embeddable_markers() { return core_media_manager::instance()->get_embeddable_markers(); }
public function list_supported_urls(array $urls, array $options = array()) { $result = []; // Youtube. $this->youtube = false; if (count($urls) == 1 && get_config('media_videojs', 'youtube')) { $url = reset($urls); // Check against regex. if (preg_match($this->get_regex_youtube(), $url->out(false), $this->matches)) { $this->youtube = true; return array($url); } } if (!get_config('media_videojs', 'useflash')) { return parent::list_supported_urls($urls, $options); } // If Flash fallback is enabled we can not check if/when browser supports flash. $extensions = $this->get_supported_extensions(); foreach ($urls as $url) { $ext = core_media_manager::instance()->get_extension($url); if (in_array('.' . $ext, $extensions)) { $result[] = $url; } } return $result; }
/** * Guesses MIME type for a moodle_url based on file extension. * @param moodle_url $url URL * @return string MIME type */ public static function get_mimetype(moodle_url $url) { return core_media_manager::instance()->get_mimetype($url); }
/** * This is a function used to detect media types and generate html code. * * @global object $CFG * @global object $PAGE * @param object $lesson * @param object $context * @return string $code the html code of media */ function lesson_get_media_html($lesson, $context) { global $CFG, $PAGE, $OUTPUT; require_once "{$CFG->libdir}/resourcelib.php"; // get the media file link if (strpos($lesson->mediafile, '://') !== false) { $url = new moodle_url($lesson->mediafile); } else { // the timemodified is used to prevent caching problems, instead of '/' we should better read from files table and use sortorder $url = moodle_url::make_pluginfile_url($context->id, 'mod_lesson', 'mediafile', $lesson->timemodified, '/', ltrim($lesson->mediafile, '/')); } $title = $lesson->mediafile; $clicktoopen = html_writer::link($url, get_string('download')); $mimetype = resourcelib_guess_url_mimetype($url); $extension = resourcelib_get_extension($url->out(false)); $mediamanager = core_media_manager::instance(); $embedoptions = array(core_media_manager::OPTION_TRUSTED => true, core_media_manager::OPTION_BLOCK => true); // find the correct type and print it out if (in_array($mimetype, array('image/gif', 'image/jpeg', 'image/png'))) { // It's an image $code = resourcelib_embed_image($url, $title); } else { if ($mediamanager->can_embed_url($url, $embedoptions)) { // Media (audio/video) file. $code = $mediamanager->embed_url($url, $title, 0, 0, $embedoptions); } else { // anything else - just try object tag enlarged as much as possible $code = resourcelib_embed_general($url, $title, $clicktoopen, $mimetype); } } return $code; }