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(){ function oembed(){
$("#oembed").toggleClass('hide'); var reply = prompt("$oembed_message:");
} if(reply && reply.length) {
tinyMCE.execCommand('mceInsertRawHTML',false, "[embed]"+reply+"[/embed]" );
function oembed_do(){ }
embed = "[embed]"+$('#oembed_url').attr('value')+"[/embed]";
tinyMCE.execCommand('mceInsertRawHTML',false,embed);
oembed();
} }

View file

@ -6,56 +6,89 @@
* http://www.oembed.com/ * http://www.oembed.com/
* *
*/ */
require_once('include/oembed.php');
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('plugin_settings', 'addon/oembed/oembed.php', 'oembed_settings');
register_hook('plugin_settings_post', 'addon/oembed/oembed.php', 'oembed_settings_post');
} }
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');
} }
function oembed_settings_post(){
if(! local_user())
return;
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);
}
}
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){ function oembed_hook_page_header($a, &$b){
$a->page['htmlhead'] .= sprintf('<script src="%s/oembed/oembed.js"></script>', $a->get_baseurl());
if(($a->module !== 'network') && ($a->module !== 'profile'))
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;
}
#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>
</div>
';
} }
function oembed_hook_jot_tool($a, &$b) { function oembed_hook_jot_tool($a, &$b) {
$b .= ' $b .= '
<div class="tool-wrapper" style="display: $visitor;" > <div class="tool-wrapper" style="display: $visitor;" >
<img class="tool-link" src="addon/oembed/oembed.png" alt="Embed" title="Embed" onclick="oembed();" /> <img class="tool-link" src="addon/oembed/oembed.png" alt="Embed" title="Embed" onclick="oembed();" />
</div> </div>
'; ';
} }
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,17 +92,21 @@ function bbcode($Text,$preserve_nl = false) {
// [img=widthxheight]image source[/img] // [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); $Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.+?)\[\/img\]/", '<img src="$3" style="height:{$2}px; width:{$1}px;" >', $Text);
// Youtube extensions 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?:\/\/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\]https?:\/\/youtu.be\/(.+?)\[\/youtube\]/",'[youtube]$1[/youtube]',$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\]/", '<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\]/", '<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);
// $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);
// oembed tag // oembed tag
$Text = oembed_bbcode2html($Text); $Text = oembed_bbcode2html($Text);
call_hooks('bbcode',$Text); call_hooks('bbcode',$Text);
return $Text; return $Text;

View file

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

View file

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

View file

@ -47,6 +47,27 @@
function rep(re, str) { function rep(re, str) {
s = s.replace(re, str); 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] // example: <strong> to [b]
rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url=$1]$2[/url]"); rep(/<a.*?href=\"(.*?)\".*?>(.*?)<\/a>/gi,"[url=$1]$2[/url]");
@ -55,8 +76,8 @@
rep(/<font>(.*?)<\/font>/gi,"$1"); rep(/<font>(.*?)<\/font>/gi,"$1");
rep(/<img.*?width=\"(.*?)\".*?height=\"(.*?)\".*?src=\"(.*?)\".*?\/>/gi,"[img=$1x$2]$3[/img]"); rep(/<img.*?width=\"(.*?)\".*?height=\"(.*?)\".*?src=\"(.*?)\".*?\/>/gi,"[img=$1x$2]$3[/img]");
rep(/<img.*?height=\"(.*?)\".*?width=\"(.*?)\".*?src=\"(.*?)\".*?\/>/gi,"[img=$2x$1]$3[/img]"); rep(/<img.*?height=\"(.*?)\".*?width=\"(.*?)\".*?src=\"(.*?)\".*?\/>/gi,"[img=$2x$1]$3[/img]");
rep(/<img.*?src=\"(.*?)\".*?height=\"(.*?)\".*?width=\"(.*?)\".*?\/>/gi,"[img=$3x$2]$1[/img]"); rep(/<img.*?src=\"(.*?)\".*?height=\"(.*?)\".*?width=\"(.*?)\".*?\/>/gi,"[img=$3x$2]$1[/img]");
rep(/<img.*?src=\"(.*?)\".*?width=\"(.*?)\".*?height=\"(.*?)\".*?\/>/gi,"[img=$2x$3]$1[/img]"); rep(/<img.*?src=\"(.*?)\".*?width=\"(.*?)\".*?height=\"(.*?)\".*?\/>/gi,"[img=$2x$3]$1[/img]");
rep(/<img.*?src=\"(.*?)\".*?\/>/gi,"[img]$1[/img]"); rep(/<img.*?src=\"(.*?)\".*?\/>/gi,"[img]$1[/img]");
rep(/<code>(.*?)<\/code>/gi,"[code]$1[/code]"); rep(/<code>(.*?)<\/code>/gi,"[code]$1[/code]");
rep(/<\/(strong|b)>/gi,"[/b]"); rep(/<\/(strong|b)>/gi,"[/b]");
@ -106,6 +127,24 @@
rep(/\[size=(.*?)\](.*?)\[\/size\]/gi,"<span style=\"font-size: $1;\">$2</span>"); rep(/\[size=(.*?)\](.*?)\[\/size\]/gi,"<span style=\"font-size: $1;\">$2</span>");
rep(/\[code\](.*?)\[\/code\]/gi,"<code>$1</code>"); rep(/\[code\](.*?)\[\/code\]/gi,"<code>$1</code>");
rep(/\[quote.*?\](.*?)\[\/quote\]/gi,"<blockquote>$1</blockquote>"); 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; return s;
} }

View file

@ -119,7 +119,7 @@ $a->strings["Administrator"] = "Amministratore";
$a->strings["Friend/Connection Request"] = "Richieste di Amicizia/Connessione"; $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["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["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["Yes"] = "Si";
$a->strings["No"] = "No"; $a->strings["No"] = "No";
$a->strings["Add a personal note:"] = "Aggiungi una nota personale:"; $a->strings["Add a personal note:"] = "Aggiungi una nota personale:";