Example #1
0
 /**
  * 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);
 }
Example #2
0
 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;
 }
Example #3
0
 /**
  * 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 &amp; 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);
 }
Example #4
0
 /**
  * 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 &amp; 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);
 }
Example #5
0
 /**
  * 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);
 }
Example #6
0
/**
 * 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;
}
Example #7
0
// 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();
Example #8
0
 /**
  * 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;
 }
Example #9
0
 /**
  * Reset contents of all database tables to initial values, reset caches, etc.
  *
  * Note: this is relatively slow (cca 2 seconds for pg and 7 for mysql) - please use with care!
  *
  * @static
  * @param bool $detectchanges
  *      true  - changes in global state and database are reported as errors
  *      false - no errors reported
  *      null  - only critical problems are reported as errors
  * @return void
  */
 public static function reset_all_data($detectchanges = false)
 {
     global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION, $FULLME;
     // Stop any message redirection.
     self::stop_message_redirection();
     // Stop any message redirection.
     self::stop_event_redirection();
     // Start a new email redirection.
     // This will clear any existing phpmailer redirection.
     // We redirect all phpmailer output to this message sink which is
     // called instead of phpmailer actually sending the message.
     self::start_phpmailer_redirection();
     // We used to call gc_collect_cycles here to ensure desctructors were called between tests.
     // This accounted for 25% of the total time running phpunit - so we removed it.
     // Show any unhandled debugging messages, the runbare() could already reset it.
     self::display_debugging_messages();
     self::reset_debugging();
     // reset global $DB in case somebody mocked it
     $DB = self::get_global_backup('DB');
     if ($DB->is_transaction_started()) {
         // we can not reset inside transaction
         $DB->force_transaction_rollback();
     }
     $resetdb = self::reset_database();
     $localename = self::get_locale_name();
     $warnings = array();
     if ($detectchanges === true) {
         if ($resetdb) {
             $warnings[] = 'Warning: unexpected database modification, resetting DB state';
         }
         $oldcfg = self::get_global_backup('CFG');
         $oldsite = self::get_global_backup('SITE');
         foreach ($CFG as $k => $v) {
             if (!property_exists($oldcfg, $k)) {
                 $warnings[] = 'Warning: unexpected new $CFG->' . $k . ' value';
             } else {
                 if ($oldcfg->{$k} !== $CFG->{$k}) {
                     $warnings[] = 'Warning: unexpected change of $CFG->' . $k . ' value';
                 }
             }
             unset($oldcfg->{$k});
         }
         if ($oldcfg) {
             foreach ($oldcfg as $k => $v) {
                 $warnings[] = 'Warning: unexpected removal of $CFG->' . $k;
             }
         }
         if ($USER->id != 0) {
             $warnings[] = 'Warning: unexpected change of $USER';
         }
         if ($COURSE->id != $oldsite->id) {
             $warnings[] = 'Warning: unexpected change of $COURSE';
         }
         if ($FULLME !== self::get_global_backup('FULLME')) {
             $warnings[] = 'Warning: unexpected change of $FULLME';
         }
         if (setlocale(LC_TIME, 0) !== $localename) {
             $warnings[] = 'Warning: unexpected change of locale';
         }
     }
     if (ini_get('max_execution_time') != 0) {
         // This is special warning for all resets because we do not want any
         // libraries to mess with timeouts unintentionally.
         // Our PHPUnit integration is not supposed to change it either.
         if ($detectchanges !== false) {
             $warnings[] = 'Warning: max_execution_time was changed to ' . ini_get('max_execution_time');
         }
         set_time_limit(0);
     }
     // restore original globals
     $_SERVER = self::get_global_backup('_SERVER');
     $CFG = self::get_global_backup('CFG');
     $SITE = self::get_global_backup('SITE');
     $FULLME = self::get_global_backup('FULLME');
     $_GET = array();
     $_POST = array();
     $_FILES = array();
     $_REQUEST = array();
     $COURSE = $SITE;
     // reinitialise following globals
     $OUTPUT = new bootstrap_renderer();
     $PAGE = new moodle_page();
     $FULLME = null;
     $ME = null;
     $SCRIPT = null;
     // Empty sessison and set fresh new not-logged-in user.
     \core\session\manager::init_empty_session();
     // reset all static caches
     \core\event\manager::phpunit_reset();
     accesslib_clear_all_caches(true);
     get_string_manager()->reset_caches(true);
     reset_text_filters_cache(true);
     events_get_handlers('reset');
     core_text::reset_caches();
     get_message_processors(false, true, true);
     filter_manager::reset_caches();
     core_filetypes::reset_caches();
     \core_search\manager::clear_static();
     core_user::reset_caches();
     if (class_exists('core_media_manager', false)) {
         core_media_manager::reset_caches();
     }
     // Reset static unit test options.
     if (class_exists('\\availability_date\\condition', false)) {
         \availability_date\condition::set_current_time_for_test(0);
     }
     // Reset internal users.
     core_user::reset_internal_users();
     //TODO MDL-25290: add more resets here and probably refactor them to new core function
     // Reset course and module caches.
     if (class_exists('format_base')) {
         // If file containing class is not loaded, there is no cache there anyway.
         format_base::reset_course_cache(0);
     }
     get_fast_modinfo(0, 0, true);
     // Reset other singletons.
     if (class_exists('core_plugin_manager')) {
         core_plugin_manager::reset_caches(true);
     }
     if (class_exists('\\core\\update\\checker')) {
         \core\update\checker::reset_caches(true);
     }
     // Clear static cache within restore.
     if (class_exists('restore_section_structure_step')) {
         restore_section_structure_step::reset_caches();
     }
     // purge dataroot directory
     self::reset_dataroot();
     // restore original config once more in case resetting of caches changed CFG
     $CFG = self::get_global_backup('CFG');
     // inform data generator
     self::get_data_generator()->reset();
     // fix PHP settings
     error_reporting($CFG->debug);
     // Reset the date/time class.
     core_date::phpunit_reset();
     // Make sure the time locale is consistent - that is Australian English.
     setlocale(LC_TIME, $localename);
     // Reset the log manager cache.
     get_log_manager(true);
     // verify db writes just in case something goes wrong in reset
     if (self::$lastdbwrites != $DB->perf_get_writes()) {
         error_log('Unexpected DB writes in phpunit_util::reset_all_data()');
         self::$lastdbwrites = $DB->perf_get_writes();
     }
     if ($warnings) {
         $warnings = implode("\n", $warnings);
         trigger_error($warnings, E_USER_WARNING);
     }
 }
Example #10
0
 /**
  * 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);
 }
Example #11
0
 /**
  * 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];
     }
 }
Example #12
0
 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;
 }
Example #13
0
 /**
  * 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);
 }
Example #14
0
/**
 * 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;
}
Example #15
0
/**
 * 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('&amp;', '&', $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;
}
Example #16
0
 /**
  * Resets cached singleton instance. To be used after $CFG->media_plugins_sortorder is modified
  */
 public static function reset_caches()
 {
     self::$instance = null;
 }
Example #17
0
 /**
  * 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();
 }