2 function oembed_replacecb($matches){
4 $j = oembed_fetch_url($embedurl);
5 return oembed_format_object($j);
9 function oembed_fetch_url($embedurl){
11 $txt = Cache::get($embedurl);
16 // try oembed autodiscovery
18 $html_text = fetch_url($embedurl, false, $redirects, 15);
21 $dom = @DOMDocument::loadHTML($html_text);
23 $xpath = new DOMXPath($dom);
26 $xattr = oe_build_xpath("class","oembed");
27 $entries = $xpath->query("//link[@type='application/json+oembed']");
28 foreach($entries as $e){
29 $href = $e->getAttributeNode("href")->nodeValue;
30 $txt = fetch_url($href);
34 if ($txt==false || $txt==""){
35 // try oohembed service
36 $ourl = "http://oohembed.com/oohembed/?url=".urlencode($embedurl);
37 $txt = fetch_url($ourl);
41 if ($txt[0]!="{") $txt='{"type":"error"}';
44 Cache::set($embedurl,$txt);
48 $j = json_decode($txt);
49 $j->embedurl = $embedurl;
53 function oembed_format_object($j){
54 $embedurl = $j->embedurl;
55 $ret="<span class='oembed ".$j->type."'>";
58 if (isset($j->thumbnail_url)) {
59 /*$tw = (isset($j->thumbnail_width)) ? $j->thumbnail_width:200;
60 $th = (isset($j->thumbnail_height)) ? $j->thumbnail_height:180;*/
62 $ret.= "<a href='".$embedurl."' onclick='this.innerHTML=unescape(\"".urlencode($j->html)."\").replace(/\+/g,\" \"); return false;' style='float:left; margin: 1em; '>";
63 $ret.= "<img width='$tw' height='$th' src='".$j->thumbnail_url."'>";
71 $ret.= "<img width='".$j->width."' height='".$j->height."' src='".$j->url."'>";
75 //$ret = "<a href='".$embedurl."'>".$j->title."</a>";
83 // add link to source if not present in "rich" type
84 if ( $j->type!='rich' || !strpos($ret,$embedurl) ){
85 $embedlink = (isset($j->title))?$j->title:$embedurl;
86 $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
87 if (isset($j->author_name)) $ret.=" by ".$j->author_name;
88 if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
90 // add <a> for html2bbcode conversion
91 $ret .= "<a href='$embedurl' rel='oembed'/>";
93 $ret.="<br style='clear:left'></span>";
94 return mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));
97 function oembed_bbcode2html($text){
98 $stopoembed = get_config("system","no_oembed");
99 if ($stopoembed == true){
100 return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
102 return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
106 function oe_build_xpath($attr, $value){
107 // http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html
108 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'";
111 function oe_get_inner_html( $node ) {
113 $children = $node->childNodes;
114 foreach ($children as $child) {
115 $innerHTML .= $child->ownerDocument->saveXML( $child );
121 * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
122 * and replace it with [embed]url[/embed]
124 function oembed_html2bbcode($text) {
125 // start parser only if 'oembed' is in text
126 if (strpos($text, "oembed")){
128 // convert non ascii chars to html entities
129 $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
131 // If it doesn't parse at all, just return the text.
132 $dom = @DOMDocument::loadHTML($html_text);
135 $xpath = new DOMXPath($dom);
138 $xattr = oe_build_xpath("class","oembed");
139 $entries = $xpath->query("//span[$xattr]");
141 $xattr = "@rel='oembed'";//oe_build_xpath("rel","oembed");
142 foreach($entries as $e) {
143 $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
144 if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[/embed]"), $e);
146 return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );