Merge pull request #107 from fabrixxm/oembed

Oembed plugin
This commit is contained in:
Friendika 2011-05-23 18:12:14 -07:00
commit 9a3f5eaf7d
7 changed files with 217 additions and 108 deletions

View file

@ -1,10 +1,6 @@
function oembed(){
$("#oembed").toggleClass('hide');
}
function oembed_do(){
embed = "[embed]"+$('#oembed_url').attr('value')+"[/embed]";
tinyMCE.execCommand('mceInsertRawHTML',false,embed);
oembed();
var reply = prompt("$oembed_message:");
if(reply && reply.length) {
tinyMCE.execCommand('mceInsertRawHTML',false, "[embed]"+reply+"[/embed]" );
}
}

View file

@ -7,9 +7,13 @@
*
*/
require_once('include/oembed.php');
function oembed_install() {
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('plugin_settings', 'addon/oembed/oembed.php', 'oembed_settings');
register_hook('plugin_settings_post', 'addon/oembed/oembed.php', 'oembed_settings_post');
}
function oembed_uninstall() {
@ -17,33 +21,38 @@ function oembed_uninstall() {
unregister_hook('page_header', 'addon/oembed/oembed.php', 'oembed_hook_page_header');
}
function oembed_hook_page_header($a, &$b){
if(($a->module !== 'network') && ($a->module !== 'profile'))
function oembed_settings_post(){
if(! local_user())
return;
$b .= '<script src="addon/oembed/oembed.js"></script>
<style>#oembed.hide { display: none }
#oembed {
display:block; position: absolute; width: 300px; height:200px;
background-color:#fff; color: #000;
border:2px solid #8888FF; padding: 1em;
top: 200px; left: 400px; z-index:2000;
if (isset($_POST['oembed-submit'])){
set_pconfig(local_user(), 'oembed', 'use_for_youtube', (isset($_POST['oembed_use_for_youtube'])?1:0));
notice( t('OEmbed settings updated') . EOL);
}
#oembed_url { width: 100%; margin-bottom:3px;}
</style>';
}
$b .= '
<div id="oembed" class="hide"><input id="oembed_url">&nbsp;
<input type="button" value="Embed" onclick="oembed_do()" style="float:left;">
<a onclick="oembed(); return false;" style="float:right;"><img onmouseout="imgdull(this);" onmouseover="imgbright(this);" class="wall-item-delete-icon" src="images/b_drophide.gif" style="width: 16px; height: 16px;"></a>
<div style="clear:both">Paste a link from 5min.com, Amazon Product Image, blip.tv, Clikthrough, CollegeHumor Video,
Daily Show with Jon Stewart, Dailymotion, dotSUB.com, Flickr Photos, Funny or Die Video,
Google Video, Hulu, Kinomap, LiveJournal UserPic, Metacafe, National Film Board of Canada,
Phodroid Photos, Photobucket, Qik Video, Revision3, Scribd, SlideShare, TwitPic, Twitter Status,
Viddler Video, Vimeo, Wikipedia, Wordpress.com, XKCD Comic, YFrog, YouTube</div>
function oembed_settings(&$a,&$o) {
if(! local_user())
return;
$uofy = get_pconfig(local_user(), 'oembed', 'use_for_youtube' );
$o .='<h3 class="settings-heading">OEmbed</h3>';
$o.='
<div id="settings-username-wrapper">
<label for="oembed_use_for_youtube">'
.t('Use OEmbed for YouTube videos: ')
.'</label><input type="checkbox" id="oembed_use_for_youtube" name="oembed_use_for_youtube"'
. ($uofy==1?'checked="true"':'')
.'>
</div>
';
<div id="settings-username-end"></div>
<div class="settings-submit-wrapper">
<input type="submit" value="'.t('Submit').'" class="settings-submit" name="oembed-submit">
</div>';
}
function oembed_hook_page_header($a, &$b){
$a->page['htmlhead'] .= sprintf('<script src="%s/oembed/oembed.js"></script>', $a->get_baseurl());
}
@ -56,6 +65,30 @@ function oembed_hook_jot_tool($a, &$b) {
}
function oembed_module() {
return;
}
function oembed_init(&$a) {
if ($a->argv[1]=='oembed.js'){
$tpl = file_get_contents('addon/oembed/oembed.js');
echo replace_macros($tpl, array(
'$oembed_message' => t('URL to embed:'),
));
}
if ($a->argv[1]=='b2h'){
$url = array( "", trim(hex2bin($_GET['url'])));
echo oembed_replacecb($url);
}
if ($a->argv[1]=='h2b'){
$text = trim(hex2bin($_GET['text']));
echo oembed_html2bbcode($text);
}
killme();
}
?>

View file

@ -92,13 +92,17 @@ function bbcode($Text,$preserve_nl = false) {
// [img=widthxheight]image source[/img]
$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.+?)\[\/img\]/", '<img src="$3" style="height:{$2}px; width:{$1}px;" >', $Text);
if (get_pconfig(local_user(), 'oembed', 'use_for_youtube' )==1){
// use oembed for youtube links
$Text = preg_replace("/\[youtube\]/",'[embed]',$Text);
$Text = preg_replace("/\[\/youtube\]/",'[/embed]',$Text);
} else {
// Youtube extensions
$Text = preg_replace("/\[youtube\]https?:\/\/www.youtube.com\/watch\?v\=(.+?)\[\/youtube\]/",'[youtube]$1[/youtube]',$Text);
$Text = preg_replace("/\[youtube\]https?:\/\/youtu.be\/(.+?)\[\/youtube\]/",'[youtube]$1[/youtube]',$Text);
$Text = preg_replace("/\[youtube\](.+?)\[\/youtube\]/", '<br /><iframe style="width:425px;height:349px;padding:10px 0 10px 0;float:left;" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>', $Text);
// $Text = preg_replace("/\[youtube\](.+?)\[\/youtube\]/", '<br /><object style="width:425px;height:350px;" type="application/x-shockwave-flash" data="http://www.youtube.com/v/$1" ><param name="movie" value="http://www.youtube.com/v/$1"></param><!--[if IE]><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" style="width:425px;height:350px;" /><![endif]--></object>', $Text);
$Text = preg_replace("/\[youtube\](.+?)\[\/youtube\]/", '<iframe width="425" height="349" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>', $Text);
}
// $Text = preg_replace("/\[youtube\](.+?)\[\/youtube\]/", '<object width="425" height="350" type="application/x-shockwave-flash" data="http://www.youtube.com/v/$1" ><param name="movie" value="http://www.youtube.com/v/$1"></param><!--[if IE]><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" width="425" height="350" /><![endif]--></object>', $Text);
// oembed tag
$Text = oembed_bbcode2html($Text);

View file

@ -1,28 +1,66 @@
<?php
function oembed_replacecb($matches){
$embedurl=$matches[1];
$j = oembed_fetch_url($embedurl);
return oembed_format_object($j);
}
function oembed_fetch_url($embedurl){
$r = q("SELECT v FROM `cache` WHERE k='%s'",
dbesc($embedurl));
if(count($r)){
$txt = $r[0]['v'];
} else {
$txt = "";
// try oembed autodiscovery
$html_text = fetch_url($embedurl);
$dom = @DOMDocument::loadHTML($html_text);
if ($dom){
$xpath = new DOMXPath($dom);
$attr = "oembed";
$xattr = oe_build_xpath("class","oembed");
$entries = $xpath->query("//link[@type='application/json+oembed']");
foreach($entries as $e){
$href = $e->getAttributeNode("href")->nodeValue;
$txt = fetch_url($href);
}
}
if ($txt==false || $txt==""){
// try oohembed service
$ourl = "http://oohembed.com/oohembed/?url=".urlencode($embedurl);
$txt = fetch_url($ourl);
}
$txt=trim($txt);
if ($txt[0]!="{") $txt='{"type":"error"}';
//save in cache
q("INSERT INTO `cache` VALUES ('%s','%s','%s')",
/*q("INSERT INTO `cache` VALUES ('%s','%s','%s')",
dbesc($embedurl),
dbesc($txt),
dbesc(datetime_convert()));
dbesc(datetime_convert()));*/
}
$j = json_decode($txt);
$ret="<span class='oembed'>";
$j->embedurl = $embedurl;
return $j;
}
function oembed_format_object($j){
$embedurl = $j->embedurl;
$ret="<span class='oembed ".$j->type."'>";
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='".$embedurl."' onclick='this.innerHTML=unescape(\"".urlencode($j->html)."\").replace(/\+/g,\" \"); return false;' >";
/*$tw = (isset($j->thumbnail_width)) ? $j->thumbnail_width:200;
$th = (isset($j->thumbnail_height)) ? $j->thumbnail_height:180;*/
$tw=150; $th=120;
$ret.= "<a href='".$embedurl."' onclick='this.innerHTML=unescape(\"".urlencode($j->html)."\").replace(/\+/g,\" \"); return false;' style='float:left; margin: 1em; '>";
$ret.= "<img width='$tw' height='$th' src='".$j->thumbnail_url."'>";
$ret.= "</a>";
} else {
@ -31,7 +69,7 @@ function oembed_replacecb($matches){
$ret.="<br>";
}; break;
case "photo": {
$ret = "<img width='".$j->width."' height='".$j->height."' src='".$j->url."'>";
$ret.= "<img width='".$j->width."' height='".$j->height."' src='".$j->url."'>";
$ret.="<br>";
}; break;
case "link": {
@ -39,7 +77,7 @@ function oembed_replacecb($matches){
}; break;
case "rich": {
// not so safe..
$ret = "<blockquote>".$j->html."</blockquote>";
$ret.= "<blockquote>".$j->html."</blockquote>";
}; break;
}
@ -47,7 +85,7 @@ function oembed_replacecb($matches){
$ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
if (isset($j->author_name)) $ret.=" by ".$j->author_name;
if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
$ret.="</span>";
$ret.="<br style='clear:left'></span>";
return $ret;
}
@ -98,7 +136,7 @@ function oembed_html2bbcode($text) {
$xattr = oe_build_xpath("rel","oembed");
foreach($entries as $e) {
$href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[embed]"), $e);
if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[/embed]"), $e);
}
return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );
} else {

View file

@ -2,7 +2,6 @@
require_once('library/HTML5/Parser.php');
function parse_url_content(&$a) {
logger('parse_url: ' . $_GET['url']);
@ -25,9 +24,9 @@ function parse_url_content(&$a) {
killme();
}
if($url)
if($url) {
$s = fetch_url($url);
else {
} else {
echo '';
killme();
}

View file

@ -48,6 +48,27 @@
s = s.replace(re, str);
};
/* oembed */
function _h2b_cb(match) {
text = bin2hex(match);
function s_h2b(data) {
match = data;
}
$.ajax({
url: 'oembed/h2b?text=' + text,
async: false,
success: s_h2b,
dataType: 'html'
});
return match;
}
s = s.replace(/<span class=\"oembed(.*?)<\/span>/gi, _h2b_cb);
/* /oembed */
// example: <strong> to [b]
rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url=$1]$2[/url]");
rep(/<span style=\"font-size:(.*?);\">(.*?)<\/span>/gi,"[size=$1]$2[/size]");
@ -107,6 +128,24 @@
rep(/\[code\](.*?)\[\/code\]/gi,"<code>$1</code>");
rep(/\[quote.*?\](.*?)\[\/quote\]/gi,"<blockquote>$1</blockquote>");
/* oembed */
function _b2h_cb(match, url) {
url = bin2hex(url);
function s_b2h(data) {
match = data;
}
$.ajax({
url: 'oembed/b2h?url=' + url,
async: false,
success: s_b2h,
dataType: 'html'
});
return match;
}
s = s.replace(/\[embed\](.*?)\[\/embed\]/gi, _b2h_cb);
/* /oembed */
return s;
}
});

View file

@ -119,7 +119,7 @@ $a->strings["Administrator"] = "Amministratore";
$a->strings["Friend/Connection Request"] = "Richieste di Amicizia/Connessione";
$a->strings["Examples: jojo@demo.friendika.com, http://demo.friendika.com/profile/jojo, testuser@identi.ca"] = "Esempi: jojo@demo.friendika.com, http://demo.friendika.com/profile/jojo, testuser@identi.ca";
$a->strings["Please answer the following:"] = "Rispondi al seguente:";
$a->strings["Does \$name know you?"] = "$name ti conosce?";
$a->strings["Does \$name know you?"] = "\$name ti conosce?";
$a->strings["Yes"] = "Si";
$a->strings["No"] = "No";
$a->strings["Add a personal note:"] = "Aggiungi una nota personale:";