2 function oembed_replacecb($matches){
4 $j = oembed_fetch_url($embedurl);
5 return oembed_format_object($j);
9 function oembed_fetch_url($embedurl){
10 $r = q("SELECT v FROM `cache` WHERE k='%s'",
18 // try oembed autodiscovery
19 $html_text = fetch_url($embedurl);
20 $dom = @DOMDocument::loadHTML($html_text);
22 $xpath = new DOMXPath($dom);
25 $xattr = oe_build_xpath("class","oembed");
26 $entries = $xpath->query("//link[@type='application/json+oembed']");
27 foreach($entries as $e){
28 $href = $e->getAttributeNode("href")->nodeValue;
29 $txt = fetch_url($href);
33 if ($txt==false || $txt==""){
34 // try oohembed service
35 $ourl = "http://oohembed.com/oohembed/?url=".urlencode($embedurl);
36 $txt = fetch_url($ourl);
40 if ($txt[0]!="{") $txt='{"type":"error"}';
43 /*q("INSERT INTO `cache` VALUES ('%s','%s','%s')",
46 dbesc(datetime_convert()));*/
49 $j = json_decode($txt);
50 $j->embedurl = $embedurl;
54 function oembed_format_object($j){
55 $embedurl = $j->embedurl;
56 $ret="<span class='oembed ".$j->type."'>";
59 if (isset($j->thumbnail_url)) {
60 /*$tw = (isset($j->thumbnail_width)) ? $j->thumbnail_width:200;
61 $th = (isset($j->thumbnail_height)) ? $j->thumbnail_height:180;*/
63 $ret.= "<a href='".$embedurl."' onclick='this.innerHTML=unescape(\"".urlencode($j->html)."\").replace(/\+/g,\" \"); return false;' style='float:left; margin: 1em; '>";
64 $ret.= "<img width='$tw' height='$th' src='".$j->thumbnail_url."'>";
72 $ret.= "<img width='".$j->width."' height='".$j->height."' src='".$j->url."'>";
76 //$ret = "<a href='".$embedurl."'>".$j->title."</a>";
80 $ret.= "<blockquote>".$j->html."</blockquote>";
84 $embedlink = (isset($j->title))?$j->title:$embedurl;
85 $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
86 if (isset($j->author_name)) $ret.=" by ".$j->author_name;
87 if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
88 $ret.="<br style='clear:left'></span>";
92 function oembed_bbcode2html($text){
93 $stopoembed = get_config("system","no_oembed");
94 if ($stopoembed == true){
95 return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
97 return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
101 function oe_build_xpath($attr, $value){
102 // http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html
103 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'";
106 function oe_get_inner_html( $node ) {
108 $children = $node->childNodes;
109 foreach ($children as $child) {
110 $innerHTML .= $child->ownerDocument->saveXML( $child );
116 * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
117 * and replace it with [embed]url[/embed]
119 function oembed_html2bbcode($text) {
120 // start parser only if 'oembed' is in text
121 if (strpos($text, "oembed")){
123 // convert non ascii chars to html entities
124 $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
126 // If it doesn't parse at all, just return the text.
127 $dom = @DOMDocument::loadHTML($html_text);
130 $xpath = new DOMXPath($dom);
133 $xattr = oe_build_xpath("class","oembed");
134 $entries = $xpath->query("//span[$xattr]");
136 $xattr = oe_build_xpath("rel","oembed");
137 foreach($entries as $e) {
138 $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
139 if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[/embed]"), $e);
141 return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );