Fix missing wrapping span on bbcode to html, split embed data fetch and rappresentation in two functions. Prettier video embed.

pull/1/head
fabrixxm 2011-05-22 23:03:34 +02:00 zatwierdzone przez Fabio Comuni
rodzic 866940ab5d
commit 072e414636
2 zmienionych plików z 88 dodań i 51 usunięć

Wyświetl plik

@ -47,7 +47,7 @@ function oembed_init(&$a) {
if ($a->argv[1]=='b2h'){ if ($a->argv[1]=='b2h'){
$url = array( "", trim(hex2bin($_GET['url']))); $url = array( "", trim(hex2bin($_GET['url'])));
echo "<span class='oembed'>".oembed_replacecb($url)."</span>"; echo oembed_replacecb($url);
} }
if ($a->argv[1]=='h2b'){ if ($a->argv[1]=='h2b'){

Wyświetl plik

@ -1,55 +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 ".$j->type."'>"; $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;*/
$tw=150; $th=120; $xattr = oe_build_xpath("class","oembed");
$ret = "<a href='".$embedurl."' onclick='this.innerHTML=unescape(\"".urlencode($j->html)."\").replace(/\+/g,\" \"); return false;' style='float:left; margin: 1em; '>"; $entries = $xpath->query("//link[@type='application/json+oembed']");
$ret.= "<img width='$tw' height='$th' src='".$j->thumbnail_url."'>"; foreach($entries as $e){
$ret.= "</a>"; $href = $e->getAttributeNode("href")->nodeValue;
} else { $txt = fetch_url($href);
$ret=$j->html; }
} }
$ret.="<br>";
}; break; if ($txt==false || $txt==""){
case "photo": { // try oohembed service
$ret = "<img width='".$j->width."' height='".$j->height."' src='".$j->url."'>"; $ourl = "http://oohembed.com/oohembed/?url=".urlencode($embedurl);
$ret.="<br>"; $txt = fetch_url($ourl);
}; break; }
case "link": {
//$ret = "<a href='".$embedurl."'>".$j->title."</a>"; $txt=trim($txt);
}; break; if ($txt[0]!="{") $txt='{"type":"error"}';
case "rich": {
// not so safe.. //save in cache
$ret = "<blockquote>".$j->html."</blockquote>"; /*q("INSERT INTO `cache` VALUES ('%s','%s','%s')",
}; break; dbesc($embedurl),
} dbesc($txt),
dbesc(datetime_convert()));*/
$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; $j = json_decode($txt);
if (isset($j->provider_name)) $ret.=" on ".$j->provider_name; $j->embedurl = $embedurl;
$ret.="<br style='clear:left'></span>"; return $j;
return $ret; }
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){