Merge branch 'MDL-49059-split' of https://github.com/pauln/moodle

This commit is contained in:
Eloy Lafuente (stronk7)
2015-02-17 20:56:45 +01:00
2 changed files with 84 additions and 1 deletions
+55 -1
View File
@@ -536,15 +536,69 @@ class core_media_player_youtube extends core_media_player_external {
self::pick_video_size($width, $height);
$params = '';
$start = self::get_start_time($url);
if ($start > 0) {
$params .= "start=$start&";
}
$listid = $url->param('list');
// Check for non-empty but valid playlist ID.
if (!empty($listid) && !preg_match('/[^a-zA-Z0-9\-_]/', $listid)) {
// This video is part of a playlist, and we want to embed it as such.
$params .= "list=$listid&";
}
return <<<OET
<span class="mediaplugin mediaplugin_youtube">
<iframe title="$info" width="$width" height="$height"
src="https://www.youtube.com/embed/$videoid?rel=0&wmode=transparent" frameborder="0" allowfullscreen="1"></iframe>
src="https://www.youtube.com/embed/$videoid?{$params}rel=0&wmode=transparent" frameborder="0" allowfullscreen="1"></iframe>
</span>
OET;
}
/**
* Check for start time parameter. Note that it's in hours/mins/secs in the URL,
* but the embedded player takes only a number of seconds as the "start" parameter.
* @param moodle_url $url URL of video to be embedded.
* @return int Number of seconds video should start at.
*/
protected static function get_start_time($url) {
$matches = array();
$seconds = 0;
$rawtime = $url->param('t');
if (empty($rawtime)) {
$rawtime = $url->param('start');
}
if (is_numeric($rawtime)) {
// Start time already specified as a number of seconds; ensure it's an integer.
$seconds = $rawtime;
} else if (preg_match('/(\d+?h)?(\d+?m)?(\d+?s)?/i', $rawtime, $matches)) {
// Convert into a raw number of seconds, as that's all embedded players accept.
for ($i = 1; $i < count($matches); $i++) {
if (empty($matches[$i])) {
continue;
}
$part = str_split($matches[$i], strlen($matches[$i]) - 1);
switch ($part[1]) {
case 'h':
$seconds += 3600 * $part[0];
break;
case 'm':
$seconds += 60 * $part[0];
break;
default:
$seconds += $part[0];
}
}
}
return intval($seconds);
}
protected function get_regex() {
// Regex for standard youtube link
$link = '(youtube(-nocookie)?\.com/(?:watch\?v=|v/))';
+29
View File
@@ -347,6 +347,35 @@ class core_medialib_testcase extends advanced_testcase {
$t = $renderer->embed_url($url);
$this->assertContains('</iframe>', $t);
// Format: youtube video within playlist.
$url = new moodle_url('https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0');
$t = $renderer->embed_url($url);
$this->assertContains('</iframe>', $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 = $renderer->embed_url($url);
$this->assertContains('</iframe>', $t);
$this->assertContains('start=3611', $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 = $renderer->embed_url($url);
$this->assertContains('</iframe>', $t);
$this->assertContains('list=PLxcO_MFWQBDcyn9xpbmx601YSDlDcTcr0', $t);
$this->assertContains('start=65', $t);
// Format: youtube video with invalid parameter values (injection attempts).
$url = new moodle_url('https://www.youtube.com/watch?v=dv2f_xfmbD8&index=4&list=PLxcO_">');
$t = $renderer->embed_url($url);
$this->assertContains('</iframe>', $t);
$this->assertNotContains('list=PLxcO_', $t); // We shouldn't get a list param as input was invalid.
$url = new moodle_url('https://www.youtube.com/watch?v=JNJMF1l3udM&t=">');
$t = $renderer->embed_url($url);
$this->assertContains('</iframe>', $t);
$this->assertNotContains('start=', $t); // We shouldn't get a start param as input was invalid.
// Format: youtube playlist.
$url = new moodle_url('http://www.youtube.com/view_play_list?p=PL6E18E2927047B662');
$t = $renderer->embed_url($url);