Rewrite oembed plugin to use new bbcode tag [embed] and make discovery server-side

This commit is contained in:
Fabio Comuni 2011-01-26 17:17:02 +01:00
parent 6ca1e2ddfe
commit 43be72297a
2 changed files with 48 additions and 44 deletions

View file

@ -3,49 +3,8 @@ function oembed(){
} }
function oembed_do(){ function oembed_do(){
embedurl = $('#oembed_url').attr('value'); embed = "[embed]"+$('#oembed_url').attr('value')+"[/embed]";
var url = 'http://oohembed.com/oohembed/?url='+escape( embedurl )+"&callback=?";
$.getJSON(url, function(data) { tinyMCE.execCommand('mceInsertRawHTML',false,embed);
var ret="";
switch(data.type){
case "video": {
if (data.thumbnail_url){
tw = 200; if (data.thumbnail_width) tw=data.thumbnail_width;
th = 180; if (data.thumbnail_height) tw=data.thumbnail_height;
ret = "<a href='"+embedurl+"'>";
// tiny mce bbcode plugin not support image size......
ret += "<img width='"+tw+"' height='"+th+"' src='"+data.thumbnail_url+"'></a>";
} else {
ret = data.html;
}
}; break;
case "photo": {
// tiny mce bbcode plugin not support image size......
ret = "<img width='"+data.width+"' height='"+data.height+"' src='"+data.url+"'>";
}; break;
case "link": {
ret = "<a href='"+embedurl+"'>"+data.title+"</a>";
}; break;
case "rich": {
ret = data.html; // not so safe... http://www.oembed.com/ : "Consumers may wish to load the HTML in an off-domain iframe to avoid XSS"
}; break;
default: {
alert("Error retriving data!");
return;
}
}
var embedlink = embedurl;
if (data.title) embedlink = data.title
ret+="<br><a href='"+embedurl+"'>"+embedlink+"</a>";
if (data.author_name) {
ret+=" by "+data.author_name;
}
if (data.provider_name) {
ret+=" on "+data.provider_name;
}
tinyMCE.execCommand('mceInsertRawHTML',false,ret);
oembed(); oembed();
});
} }

View file

@ -10,11 +10,13 @@
function oembed_install() { function oembed_install() {
register_hook('jot_tool', 'addon/oembed/oembed.php', 'oembed_hook_jot_tool'); register_hook('jot_tool', 'addon/oembed/oembed.php', 'oembed_hook_jot_tool');
register_hook('page_header', 'addon/oembed/oembed.php', 'oembed_hook_page_header'); register_hook('page_header', 'addon/oembed/oembed.php', 'oembed_hook_page_header');
register_hook('bbcode', 'addon/oembed/oembed.php', 'oembed_hook_bbcode');
} }
function oembed_uninstall() { function oembed_uninstall() {
unregister_hook('jot_tool', 'addon/oembed/oembed.php', 'oembed_hook_jot_tool'); unregister_hook('jot_tool', 'addon/oembed/oembed.php', 'oembed_hook_jot_tool');
unregister_hook('page_header', 'addon/oembed/oembed.php', 'oembed_hook_page_header'); unregister_hook('page_header', 'addon/oembed/oembed.php', 'oembed_hook_page_header');
unregister_hook('bbcode', 'addon/oembed/oembed.php', 'oembed_hook_bbcode');
} }
function oembed_hook_page_header($a, &$b){ function oembed_hook_page_header($a, &$b){
@ -51,6 +53,49 @@ function oembed_hook_jot_tool($a, &$b) {
'; ';
} }
function oembed_replacecb($matches){
$embedurl=$matches[1];
$ourl = "http://oohembed.com/oohembed/?url=".urlencode($embedurl);
$txt = fetch_url($ourl);
$j = json_decode($txt);
$ret="<!-- oembed $embedurl -->";
switch ($j->type) {
case "video": {
if (isset($j->thumbnail_url)) {
$tw = (isset($j->thumbnail_width)) ? $j->thumbnail_width:200;
$th = (isset($j->thumbnail_height)) ? $j->thumbnail_height:180;
$ret = "<a href='#' onclick='this.innerHTML=unescape(\"".urlencode($j->html)."\").replace(/\+/g,\" \"); return false;' >";
$ret.= "<img width='$tw' height='$th' src='".$j->thumbnail_url."'>";
$ret.= "</a>";
} else {
$ret=$j->html;
}
$ret.="<br>";
}; break;
case "photo": {
$ret = "<img width='".$j->width."' height='".$j->height."' src='".$j->url."'>";
$ret.="<br>";
}; break;
case "link": {
//$ret = "<a href='".$embedurl."'>".$j->title."</a>";
}; break;
case "rich": {
// not so safe..
$ret = "<blockquote>".$j->html."</blockquote>";
}; break;
}
$embedlink = (isset($j->title))?$j->title:$embedurl;
$ret .= "<a href='$embedurl'>$embedlink</a>";
if (isset($j->author_name)) $ret.=" by ".$j->author_name;
if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
$ret.="<!-- /oembed $embedurl -->";
return $ret;
}
function oembed_hook_bbcode($a, &$text){
$text = preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", oembed_replacecb ,$text);
}
?> ?>