2 function oembed_replacecb($matches){
5 $j = oembed_fetch_url($embedurl);
6 $s = oembed_format_object($j);
7 return $s;//oembed_iframe($s,$j->width,$j->height);
13 function oembed_fetch_url($embedurl){
15 $txt = Cache::get($embedurl);
20 // try oembed autodiscovery
22 $html_text = fetch_url($embedurl, false, $redirects, 15);
24 $dom = @DOMDocument::loadHTML($html_text);
26 $xpath = new DOMXPath($dom);
29 $xattr = oe_build_xpath("class","oembed");
30 $entries = $xpath->query("//link[@type='application/json+oembed']");
31 foreach($entries as $e){
32 $href = $e->getAttributeNode("href")->nodeValue;
33 $txt = fetch_url($href);
38 if ($txt==false || $txt==""){
39 // try oohembed service
40 $ourl = "http://oohembed.com/oohembed/?url=".urlencode($embedurl);
41 $txt = fetch_url($ourl);
45 if ($txt[0]!="{") $txt='{"type":"error"}';
48 Cache::set($embedurl,$txt);
52 $j = json_decode($txt);
53 $j->embedurl = $embedurl;
57 function oembed_format_object($j){
58 $embedurl = $j->embedurl;
59 $jhtml = oembed_iframe($j->html,$j->width,$j->height );
60 $ret="<span class='oembed ".$j->type."'>";
63 if (isset($j->thumbnail_url)) {
64 $tw = (isset($j->thumbnail_width)) ? $j->thumbnail_width:200;
65 $th = (isset($j->thumbnail_height)) ? $j->thumbnail_height:180;
68 $th=120; $tw = $th*$tr;
69 $tpl=get_markup_template('oembed_video.tpl');
70 $ret.=replace_macros($tpl, array(
71 '$embedurl'=>$embedurl,
72 '$escapedhtml'=>base64_encode($jhtml),
75 '$turl'=>$j->thumbnail_url,
84 $ret.= "<img width='".$j->width."' height='".$j->height."' src='".$j->url."'>";
88 //$ret = "<a href='".$embedurl."'>".$j->title."</a>";
96 // add link to source if not present in "rich" type
97 if ( $j->type!='rich' || !strpos($j->html,$embedurl) ){
98 $embedlink = (isset($j->title))?$j->title:$embedurl;
99 $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
100 if (isset($j->author_name)) $ret.=" by ".$j->author_name;
101 if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
103 // add <a> for html2bbcode conversion
104 $ret .= "<a href='$embedurl' rel='oembed'/>";
106 $ret.="<br style='clear:left'></span>";
107 return mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));
110 function oembed_iframe($src,$width,$height) {
111 if(! $width || strstr($width,'%'))
113 if(! $height || strstr($height,'%'))
115 // try and leave some room for the description line.
116 $height = intval($height) + 80;
117 $width = intval($width) + 40;
119 $s = 'data:text/html;base64,' . base64_encode('<html><body>' . $src . '</body></html>');
120 return '<iframe height="' . $height . '" width="' . $width . '" src="' . $s . '" frameborder="no" >' . t('Embedded content') . '</iframe>';
126 function oembed_bbcode2html($text){
127 $stopoembed = get_config("system","no_oembed");
128 if ($stopoembed == true){
129 return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
131 return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
135 function oe_build_xpath($attr, $value){
136 // http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html
137 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'";
140 function oe_get_inner_html( $node ) {
142 $children = $node->childNodes;
143 foreach ($children as $child) {
144 $innerHTML .= $child->ownerDocument->saveXML( $child );
150 * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
151 * and replace it with [embed]url[/embed]
153 function oembed_html2bbcode($text) {
154 // start parser only if 'oembed' is in text
155 if (strpos($text, "oembed")){
157 // convert non ascii chars to html entities
158 $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
160 // If it doesn't parse at all, just return the text.
161 $dom = @DOMDocument::loadHTML($html_text);
164 $xpath = new DOMXPath($dom);
167 $xattr = oe_build_xpath("class","oembed");
168 $entries = $xpath->query("//span[$xattr]");
170 $xattr = "@rel='oembed'";//oe_build_xpath("rel","oembed");
171 foreach($entries as $e) {
172 $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
173 if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[/embed]"), $e);
175 return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );