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);
17 // These media files should now be caught in bbcode.php
18 // left here as a fallback in case this is called from another source
20 $noexts = array("mp3","mp4","ogg","ogv","oga","ogm","webm");
21 $ext = pathinfo(strtolower($embedurl),PATHINFO_EXTENSION);
27 if (!in_array($ext, $noexts)){
28 // try oembed autodiscovery
30 $html_text = fetch_url($embedurl, false, $redirects, 15, "text/*");
32 $dom = @DOMDocument::loadHTML($html_text);
34 $xpath = new DOMXPath($dom);
37 $xattr = oe_build_xpath("class","oembed");
38 $entries = $xpath->query("//link[@type='application/json+oembed']");
39 foreach($entries as $e){
40 $href = $e->getAttributeNode("href")->nodeValue;
41 $txt = fetch_url($href . '&maxwidth=425');
48 if ($txt==false || $txt==""){
49 // try oohembed service
50 $ourl = "http://oohembed.com/oohembed/?url=".urlencode($embedurl).'&maxwidth=425';
51 $txt = fetch_url($ourl);
55 if ($txt[0]!="{") $txt='{"type":"error"}';
58 Cache::set($embedurl,$txt);
62 $j = json_decode($txt);
63 $j->embedurl = $embedurl;
67 function oembed_format_object($j){
69 $embedurl = $j->embedurl;
70 $jhtml = oembed_iframe($j->embedurl,(isset($j->width) ? $j->width : null), (isset($j->height) ? $j->height : null) );
71 $ret="<span class='oembed ".$j->type."'>";
74 if (isset($j->thumbnail_url)) {
75 $tw = (isset($j->thumbnail_width)) ? $j->thumbnail_width:200;
76 $th = (isset($j->thumbnail_height)) ? $j->thumbnail_height:180;
79 $th=120; $tw = $th*$tr;
80 $tpl=get_markup_template('oembed_video.tpl');
81 $ret.=replace_macros($tpl, array(
82 '$baseurl' => $a->get_baseurl(),
83 '$embedurl'=>$embedurl,
84 '$escapedhtml'=>base64_encode($jhtml),
87 '$turl'=>$j->thumbnail_url,
96 $ret.= "<img width='".$j->width."' height='".$j->height."' src='".$j->url."'>";
100 //$ret = "<a href='".$embedurl."'>".$j->title."</a>";
108 // add link to source if not present in "rich" type
109 if ( $j->type!='rich' || !strpos($j->html,$embedurl) ){
110 $embedlink = (isset($j->title))?$j->title:$embedurl;
111 $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
112 if (isset($j->author_name)) $ret.=" by ".$j->author_name;
113 if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
115 // add <a> for html2bbcode conversion
116 $ret .= "<a href='$embedurl' rel='oembed'/>";
118 $ret.="<br style='clear:left'></span>";
119 return mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));
122 function oembed_iframe($src,$width,$height) {
123 if(! $width || strstr($width,'%'))
125 if(! $height || strstr($height,'%'))
127 // try and leave some room for the description line.
128 $height = intval($height) + 80;
129 $width = intval($width) + 40;
133 $s = $a->get_baseurl()."/oembed/".base64url_encode($src);
134 return '<iframe height="' . $height . '" width="' . $width . '" src="' . $s . '" frameborder="no" >' . t('Embedded content') . '</iframe>';
140 function oembed_bbcode2html($text){
141 $stopoembed = get_config("system","no_oembed");
142 if ($stopoembed == true){
143 return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
145 return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
149 function oe_build_xpath($attr, $value){
150 // http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html
151 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'";
154 function oe_get_inner_html( $node ) {
156 $children = $node->childNodes;
157 foreach ($children as $child) {
158 $innerHTML .= $child->ownerDocument->saveXML( $child );
164 * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
165 * and replace it with [embed]url[/embed]
167 function oembed_html2bbcode($text) {
168 // start parser only if 'oembed' is in text
169 if (strpos($text, "oembed")){
171 // convert non ascii chars to html entities
172 $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
174 // If it doesn't parse at all, just return the text.
175 $dom = @DOMDocument::loadHTML($html_text);
178 $xpath = new DOMXPath($dom);
181 $xattr = oe_build_xpath("class","oembed");
182 $entries = $xpath->query("//span[$xattr]");
184 $xattr = "@rel='oembed'";//oe_build_xpath("rel","oembed");
185 foreach($entries as $e) {
186 $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
187 if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[/embed]"), $e);
189 return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );