Skip to content

Commit

Permalink
MDL-33041 cleanup moodlemedia plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Aug 11, 2012
1 parent f33693a commit 1edb798
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 97 deletions.
Expand Up @@ -22,4 +22,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['nopreview'] = 'Can not preview media.';
$string['pluginname'] = 'Insert media';

/* All lang strings used from TinyMCE JavaScript code must be named 'pluginname:stringname', no need to create langs/en_dlg.js */
$string['moodlemedia:desc'] = 'Insert Moodle media';
5 changes: 2 additions & 3 deletions lib/editor/tinymce/plugins/moodlemedia/lib.php
Expand Up @@ -19,14 +19,13 @@
/**
* Plugin for Moodle media (audio/video) insertion dialog.
*
* @package tinymce_moodlemedia
* @package tinymce_moodlemedia
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tinymce_moodlemedia extends editor_tinymce_plugin {
protected function update_init_params(array &$params, context $context,
array $options = null) {
global $OUTPUT;

// Add button after emoticon button in advancedbuttons3.
$added = $this->add_button_after($params, 3, 'moodlemedia', 'moodleemoticon', false);
Expand Down
34 changes: 12 additions & 22 deletions lib/editor/tinymce/plugins/moodlemedia/preview.php
Expand Up @@ -18,45 +18,35 @@
* 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
* @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
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require(dirname(__FILE__) . '/../../../../../config.php');
require_once($CFG->libdir . '/filelib.php');
require_once($CFG->libdir . '/editorlib.php');
require_once($CFG->libdir . '/editor/tinymce/lib.php');

// Must be logged in
require_login();

// Require path to draftfile.php file
$path = required_param('path', PARAM_PATH);

$editor = new tinymce_texteditor();
// 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',
array('path' => $path)));
$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 = $PAGE->get_renderer('core', 'media');

$path = '/'.trim($path, '/');

if (empty($CFG->slasharguments)) {
$url = new moodle_url('/draftfile.php', array('file'=>$path));
} else {
$url = new moodle_url('/draftfile.php');
$url->set_slashargument($path);
}
if ($mediarenderer->can_embed_url($url)) {
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();
7 changes: 5 additions & 2 deletions lib/editor/tinymce/plugins/moodlemedia/tinymce/css/media.css
Expand Up @@ -14,8 +14,11 @@
#width, #height { width: 40px }
#src, #media_type { width: 250px }
#class { width: 120px }
#prev {margin: 0; border: 1px solid black; width: 380px; height: 230px; overflow: hidden }
.panel_wrapper div.current { height: 390px; overflow: auto }
#prev {margin: 0; border: 1px solid black; width: 430px; height: 330px; overflow: hidden }
.panel_wrapper div.current { height: 410px}
.panel_wrapper {border-top-style: solid}
.panel_wrapper {border-top-width: 1px}
.panel_wrapper {border-top-color: rgb(145, 155, 156)}
#flash_options, #shockwave_options, #qt_options, #wmp_options, #rmp_options { display: none }
.mceAddSelectValue { background-color: #DDDDDD }
#qt_starttime, #qt_endtime, #qt_fov, #qt_href, #qt_moveid, #qt_moviename, #qt_node, #qt_pan, #qt_qtsrc, #qt_qtsrcchokespeed, #qt_target, #qt_tilt, #qt_urlsubstituten, #qt_volume { width: 70px }
Expand Down
12 changes: 6 additions & 6 deletions lib/editor/tinymce/plugins/moodlemedia/tinymce/editor_plugin.js
Expand Up @@ -14,21 +14,21 @@
t.editor = ed;
t.url = url;

// Register commands
// Register commands.
ed.addCommand('mceMoodleMedia', function() {
ed.windowManager.open({
file : url + '/moodlemedia.htm',
width : 430 + parseInt(ed.getLang('media.delta_width', 0)),
height : 470 + parseInt(ed.getLang('media.delta_height', 0)),
width : 480 + parseInt(ed.getLang('media.delta_width', 0)),
height : 480 + parseInt(ed.getLang('media.delta_height', 0)),
inline : 1
}, {
plugin_url : url
});
});

// Register buttons
// Register buttons.
ed.addButton('moodlemedia', {
title : 'Moodle Media',
title : 'moodlemedia.desc',
image : url + '/img/icon.gif',
cmd : 'mceMoodleMedia'});

Expand All @@ -48,6 +48,6 @@

});

// Register plugin
// Register plugin.
tinymce.PluginManager.add('moodlemedia', tinymce.plugins.MoodlemediaPlugin);
})();
123 changes: 66 additions & 57 deletions lib/editor/tinymce/plugins/moodlemedia/tinymce/js/media.js
@@ -1,9 +1,10 @@
/**
* @author Dongsheng Cai <dongsheng@moodle.com>
*/
tinyMCEPopup.requireLangPack();

var oldWidth, oldHeight, ed, url;
// No need to require lang packs in moodle plugins.

var ed, url;

if (url = tinyMCEPopup.getParam("media_external_list_url")) {
document.write('<script language="javascript" type="text/javascript" src="' + tinyMCEPopup.editor.documentBaseURI.toAbsolute(url) + '"></script>');
Expand All @@ -23,51 +24,16 @@ function insertMedia() {
tinyMCEPopup.close();
}

function getType(v) {
var fo, i, c, el, x, f = document.forms[0];

fo = ed.getParam("media_types", "flash=swf;flv=flv;shockwave=dcr;qt=mov,qt,mpg,mp3,mp4,mpeg;shockwave=dcr;wmp=avi,wmv,wm,asf,asx,wmx,wvx;rmp=rm,ra,ram").split(';');

// YouTube
if (v.match(/watch\?v=(.+)(.*)/)) {
f.src.value = 'http://www.youtube.com/v/' + v.match(/v=(.*)(.*)/)[0].split('=')[1];
return 'flash';
} else if (v.match(/v\/(.+)(.*)/)) {
return 'flash';
}

// Google video
if (v.indexOf('http://video.google.com/videoplay?docid=') == 0) {
f.src.value = 'http://video.google.com/googleplayer.swf?docId=' + v.substring('http://video.google.com/videoplay?docid='.length) + '&hl=en';
return 'flash';
}

for (i=0; i<fo.length; i++) {
c = fo[i].split('=');

el = c[1].split(',');
for (x=0; x<el.length; x++)
if (v.indexOf('.' + el[x]) != -1)
return c[0];
}

return null;
}


function serializeParameters() {
var d = document, f = d.forms[0], s = '';
var d = document, s = '';
s += getStr(null, 'src');
s += 'width:300,';
s += 'height:225,';

// delete the tail comma
s = s.length > 0 ? s.substring(0, s.length - 1) : s;

return s;
}


function getStr(p, n, d) {
var e = document.forms[0].elements[(p != null ? p + "_" : "") + n];
var v = e.type == "hidden" ? e.value : e.options[e.selectedIndex].value;
Expand All @@ -87,10 +53,9 @@ function jsEncode(s) {
}

function generatePreview(c) {
var f = document.forms[0], p = document.getElementById('prev'), h = '', cls, pl, n, type, codebase, wp, hp, nw, nh;
var f = document.forms[0], p = document.getElementById('prev');

p.innerHTML = '<!-- x --->';
var type = getType(f.src.value);
var re = new RegExp("(.+)\#(.+)", "i");
var result = f.src.value.match(re);
if (result) {
Expand All @@ -102,12 +67,11 @@ function generatePreview(c) {
}

// After constrain
pl = serializeParameters();
var pl = serializeParameters();
if (pl == '') {
p.innerHTML = '';
return;
}

pl = tinyMCEPopup.editor.plugins.moodlemedia._parse(pl);

if (!pl.src) {
Expand All @@ -116,22 +80,67 @@ function generatePreview(c) {
}

pl.src = tinyMCEPopup.editor.documentBaseURI.toAbsolute(pl.src);
pl.width = !pl.width ? 100 : pl.width;
pl.height = !pl.height ? 100 : pl.height;
pl.id = !pl.id ? 'moodlemediaid' : pl.id;
pl.name = !pl.name ? 'moodlemedianame' : pl.name;
pl.align = !pl.align ? '' : pl.align;

// Avoid annoying warning about insecure items
if (!tinymce.isIE || document.location.protocol != 'https:') {
// Include all the draftfile params after the ?
var draftparams = pl.src.toString().replace(/^.*\/draftfile.php\//, '');
h = '<iframe src="../../..//moodlemedia/preview.php?path=' +
draftparams + '" width="100%" height="100%"></iframe>';
}

// I don't know why the HTML comment is there, but leaving it just in case
p.innerHTML = "<!-- x --->" + h;
// NOTE: Do not try to prevent https security popups here - users would get them later on real page anyway!

// We can not include URL directly in parameters because some security filters might block it.
p.innerHTML = '<iframe src="' + tinyMCEPopup.editor.getParam("moodle_plugin_base") + 'moodlemedia/preview.php'
+ '?media=' + encodeURIComponent(encode64(pl.src.toString()))
+ '&sesskey=' + encodeURIComponent(parent.M.cfg.sesskey)
+ '" width="100%" height="100%"></iframe>';
}

function encode64(input) {
/*
CryptoMX Tools
Copyright (C) 2004 - 2006 Derek Buitenhuis
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;

do {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);

enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;

if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}

output = output +
keyStr.charAt(enc1) +
keyStr.charAt(enc2) +
keyStr.charAt(enc3) +
keyStr.charAt(enc4);
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);

return output;
}

tinyMCEPopup.onInit.add(init);
@@ -1,7 +1,8 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!DOCTYPE html>
<html>
<head>
<title>{#media_dlg.title}</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>{#moodlemedia.desc}</title>
<script type="text/javascript">
// Because there is no relative path to TinyMCE, we have to use JavaScript
// to work out correct path from the .js files from TinyMCE. Only files
Expand All @@ -11,7 +12,6 @@
parent.tinyMCE.baseURL + '/' + path + '"></' + 'script>');
};
editor_tinymce_include('tiny_mce_popup.js');
editor_tinymce_include('utils/mctabs.js');
editor_tinymce_include('utils/validate.js');
editor_tinymce_include('utils/form_utils.js');
editor_tinymce_include('utils/editable_selects.js');
Expand All @@ -36,7 +36,6 @@
<link href="css/media.css" rel="stylesheet" type="text/css" />
</head>
<body style="display: none">
<div class="tabs"></div>
<form onsubmit="insertMedia();return false;" action="#">
<div class="panel_wrapper">
<div id="general_panel" class="panel current">
Expand Down
4 changes: 2 additions & 2 deletions lib/editor/tinymce/plugins/moodlemedia/version.php
Expand Up @@ -17,9 +17,9 @@
/**
* TinyMCE media insert plugin version details.
*
* @package tinymce_moodlemedia
* @package tinymce_moodlemedia
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();
Expand Down

0 comments on commit 1edb798

Please sign in to comment.