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){
17 $txt = Cache::get($a->videowidth . $embedurl);
19 // These media files should now be caught in bbcode.php
20 // left here as a fallback in case this is called from another source
22 $noexts = array("mp3","mp4","ogg","ogv","oga","ogm","webm");
23 $ext = pathinfo(strtolower($embedurl),PATHINFO_EXTENSION);
29 if (!in_array($ext, $noexts)){
30 // try oembed autodiscovery
32 $html_text = fetch_url($embedurl, false, $redirects, 15, "text/*"); /**/
34 $dom = @DOMDocument::loadHTML($html_text);
36 $xpath = new DOMXPath($dom);
39 $xattr = oe_build_xpath("class","oembed");
40 $entries = $xpath->query("//link[@type='application/json+oembed']");
41 foreach($entries as $e){
42 $href = $e->getAttributeNode("href")->nodeValue;
43 $txt = fetch_url($href . '&maxwidth=' . $a->videowidth);
50 if ($txt==false || $txt==""){
51 // try oohembed service
52 $ourl = "http://oohembed.com/oohembed/?url=".urlencode($embedurl).'&maxwidth=' . $a->videowidth;
53 $txt = fetch_url($ourl);
57 if ($txt[0]!="{") $txt='{"type":"error"}';
60 Cache::set($a->videowidth . $embedurl,$txt);
64 $j = json_decode($txt);
65 $j->embedurl = $embedurl;
69 function oembed_format_object($j){
71 $embedurl = $j->embedurl;
72 $jhtml = oembed_iframe($j->embedurl,(isset($j->width) ? $j->width : null), (isset($j->height) ? $j->height : null) );
73 $ret="<span class='oembed ".$j->type."'>";
76 if (isset($j->thumbnail_url)) {
77 $tw = (isset($j->thumbnail_width) && intval($j->thumbnail_width)) ? $j->thumbnail_width:200;
78 $th = (isset($j->thumbnail_height) && intval($j->thumbnail_height)) ? $j->thumbnail_height:180;
79 // make sure we don't attempt divide by zero, fallback is a 1:1 ratio
80 $tr = (($th) ? $tw/$th : 1);
82 $th=120; $tw = $th*$tr;
83 $tpl=get_markup_template('oembed_video.tpl');
84 $ret.=replace_macros($tpl, array(
85 '$baseurl' => $a->get_baseurl(),
86 '$embedurl'=>$embedurl,
87 '$escapedhtml'=>base64_encode($jhtml),
90 '$turl'=>$j->thumbnail_url,
99 $ret.= "<img width='".$j->width."' src='".$j->url."'>";
100 //$ret.= "<img width='".$j->width."' height='".$j->height."' src='".$j->url."'>";
104 //$ret = "<a href='".$embedurl."'>".$j->title."</a>";
112 // add link to source if not present in "rich" type
113 if ($j->type!='rich' || !strpos($j->html,$embedurl) ){
114 if (isset($j->provider_name)) $ret .= $j->provider_name.": ";
115 $embedlink = (isset($j->title))?$j->title:$embedurl;
116 $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
117 if (isset($j->author_name)) $ret.=" (".$j->author_name.")";
118 //if (isset($j->author_name)) $ret.=" by ".$j->author_name;
119 //if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
121 // add <a> for html2bbcode conversion
122 $ret .= "<a href='$embedurl' rel='oembed'></a>";
124 $ret.="<br style='clear:left'></span>";
125 return mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));
128 function oembed_iframe($src,$width,$height) {
129 if(! $width || strstr($width,'%'))
131 if(! $height || strstr($height,'%'))
133 // try and leave some room for the description line.
134 $height = intval($height) + 80;
135 $width = intval($width) + 40;
139 $s = $a->get_baseurl()."/oembed/".base64url_encode($src);
140 return '<iframe height="' . $height . '" width="' . $width . '" src="' . $s . '" frameborder="no" >' . t('Embedded content') . '</iframe>';
146 function oembed_bbcode2html($text){
147 $stopoembed = get_config("system","no_oembed");
148 if ($stopoembed == true){
149 return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
151 return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
155 function oe_build_xpath($attr, $value){
156 // http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html
157 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'";
160 function oe_get_inner_html( $node ) {
162 $children = $node->childNodes;
163 foreach ($children as $child) {
164 $innerHTML .= $child->ownerDocument->saveXML( $child );
170 * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
171 * and replace it with [embed]url[/embed]
173 function oembed_html2bbcode($text) {
174 // start parser only if 'oembed' is in text
175 if (strpos($text, "oembed")){
177 // convert non ascii chars to html entities
178 $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
180 // If it doesn't parse at all, just return the text.
181 $dom = @DOMDocument::loadHTML($html_text);
184 $xpath = new DOMXPath($dom);
187 $xattr = oe_build_xpath("class","oembed");
188 $entries = $xpath->query("//span[$xattr]");
190 $xattr = "@rel='oembed'";//oe_build_xpath("rel","oembed");
191 foreach($entries as $e) {
192 $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
193 if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[/embed]"), $e);
195 return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );