2 function oembed_replacecb($matches){
5 $r = q("SELECT v FROM `cache` WHERE k='%s'",
10 $ourl = "http://oohembed.com/oohembed/?url=".urlencode($embedurl);
11 $txt = fetch_url($ourl);
13 q("INSERT INTO `cache` VALUES ('%s','%s','%s')",
16 dbesc(datetime_convert()));
18 $j = json_decode($txt);
19 $ret="<span class='oembed'>";
22 if (isset($j->thumbnail_url)) {
23 $tw = (isset($j->thumbnail_width)) ? $j->thumbnail_width:200;
24 $th = (isset($j->thumbnail_height)) ? $j->thumbnail_height:180;
25 $ret = "<a href='".$embedurl."' onclick='this.innerHTML=unescape(\"".urlencode($j->html)."\").replace(/\+/g,\" \"); return false;' >";
26 $ret.= "<img width='$tw' height='$th' src='".$j->thumbnail_url."'>";
34 $ret = "<img width='".$j->width."' height='".$j->height."' src='".$j->url."'>";
38 //$ret = "<a href='".$embedurl."'>".$j->title."</a>";
42 $ret = "<blockquote>".$j->html."</blockquote>";
46 $embedlink = (isset($j->title))?$j->title:$embedurl;
47 $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
48 if (isset($j->author_name)) $ret.=" by ".$j->author_name;
49 if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
54 function oembed_bbcode2html($text){
55 $stopoembed = get_config("system","no_oembed");
56 if ($stopoembed == true){
57 return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
59 return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
63 function oe_build_xpath($attr, $value){
64 // http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html
65 return "contains( normalize-space( @$attr ), ' $value ' ) or substring( normalize-space( @$attr ), 1, string-length( '$value' ) + 1 ) = '$value ' or substring( normalize-space( @$attr ), string-length( @$attr ) - string-length( '$value' ) ) = ' $value' or @$attr = '$value'";
68 function oe_get_inner_html( $node ) {
70 $children = $node->childNodes;
71 foreach ($children as $child) {
72 $innerHTML .= $child->ownerDocument->saveXML( $child );
78 * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
79 * and replace it with [embed]url[/embed]
81 function oembed_html2bbcode($text) {
82 // start parser only if 'oembed' is in text
83 if (strpos($text, "oembed")){
85 // convert non ascii chars to html entities
86 $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
88 // If it doesn't parse at all, just return the text.
89 $dom = @DOMDocument::loadHTML($html_text);
92 $xpath = new DOMXPath($dom);
95 $xattr = oe_build_xpath("class","oembed");
96 $entries = $xpath->query("//span[$xattr]");
98 $xattr = oe_build_xpath("rel","oembed");
99 foreach($entries as $e) {
100 $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
101 if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[embed]"), $e);
103 return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );