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, $no_rich_type = false){
14 $embedurl = trim($embedurl, "'");
15 $embedurl = trim($embedurl, '"');
19 $txt = Cache::get($a->videowidth . $embedurl);
21 // These media files should now be caught in bbcode.php
22 // left here as a fallback in case this is called from another source
24 $noexts = array("mp3","mp4","ogg","ogv","oga","ogm","webm");
25 $ext = pathinfo(strtolower($embedurl),PATHINFO_EXTENSION);
31 if (!in_array($ext, $noexts)){
32 // try oembed autodiscovery
34 $html_text = fetch_url($embedurl, false, $redirects, 15, "text/*"); /**/
36 $dom = @DOMDocument::loadHTML($html_text);
38 $xpath = new DOMXPath($dom);
40 $xattr = oe_build_xpath("class","oembed");
41 $entries = $xpath->query("//link[@type='application/json+oembed']");
42 foreach($entries as $e){
43 $href = $e->getAttributeNode("href")->nodeValue;
44 $txt = fetch_url($href . '&maxwidth=' . $a->videowidth);
47 $entries = $xpath->query("//link[@type='text/json+oembed']");
48 foreach($entries as $e){
49 $href = $e->getAttributeNode("href")->nodeValue;
50 $txt = fetch_url($href . '&maxwidth=' . $a->videowidth);
57 if ($txt==false || $txt==""){
58 $embedly = get_config("system", "embedly");
60 // try embedly service
61 $ourl = "https://api.embed.ly/1/oembed?key=".$embedly."&url=".urlencode($embedurl);
62 $txt = fetch_url($ourl);
64 logger("oembed_fetch_url: ".$txt, LOGGER_DEBUG);
70 if ($txt==false || $txt=="")
74 $txt='{"type":"error"}';
76 Cache::set($a->videowidth . $embedurl,$txt, CACHE_DAY);
79 $j = json_decode($txt);
84 // Always embed the SSL version
86 $j->html = str_replace(array("http://www.youtube.com/", "http://player.vimeo.com/"),
87 array("https://www.youtube.com/", "https://player.vimeo.com/"), $j->html);
89 $j->embedurl = $embedurl;
91 // If fetching information doesn't work, then improve via internal functions
92 if (($j->type == "error") OR ($no_rich_type AND ($j->type == "rich"))) {
93 require_once("mod/parse_url.php");
94 $data = parseurl_getsiteinfo($embedurl, true, false);
95 $j->type = $data["type"];
97 if ($j->type == "photo") {
98 $j->url = $data["url"];
99 //$j->width = $data["images"][0]["width"];
100 //$j->height = $data["images"][0]["height"];
103 if (isset($data["title"]))
104 $j->title = $data["title"];
106 if (isset($data["text"]))
107 $j->description = $data["text"];
109 if (is_array($data["images"])) {
110 $j->thumbnail_url = $data["images"][0]["src"];
111 $j->thumbnail_width = $data["images"][0]["width"];
112 $j->thumbnail_height = $data["images"][0]["height"];
116 call_hooks('oembed_fetch_url', $embedurl, $j);
121 function oembed_format_object($j){
122 require_once("mod/proxy.php");
125 $embedurl = $j->embedurl;
126 $jhtml = oembed_iframe($j->embedurl,(isset($j->width) ? $j->width : null), (isset($j->height) ? $j->height : null) );
127 $ret="<span class='oembed ".$j->type."'>";
130 if (isset($j->thumbnail_url)) {
131 $tw = (isset($j->thumbnail_width) && intval($j->thumbnail_width)) ? $j->thumbnail_width:200;
132 $th = (isset($j->thumbnail_height) && intval($j->thumbnail_height)) ? $j->thumbnail_height:180;
133 // make sure we don't attempt divide by zero, fallback is a 1:1 ratio
134 $tr = (($th) ? $tw/$th : 1);
136 $th=120; $tw = $th*$tr;
137 $tpl=get_markup_template('oembed_video.tpl');
138 $ret.=replace_macros($tpl, array(
139 '$baseurl' => $a->get_baseurl(),
140 '$embedurl'=>$embedurl,
141 '$escapedhtml'=>base64_encode($jhtml),
144 '$turl'=>$j->thumbnail_url,
153 $ret.= "<img width='".$j->width."' src='".proxy_url($j->url)."'>";
154 //$ret.= "<img width='".$j->width."' height='".$j->height."' src='".proxy_url($j->url)."'>";
158 //$ret = "<a href='".$embedurl."'>".$j->title."</a>";
162 if (!get_config("system","no_oembed_rich_content"))
167 // add link to source if not present in "rich" type
168 if ($j->type!='rich' || !strpos($j->html,$embedurl) ){
170 if (isset($j->title)) {
171 if (isset($j->provider_name))
172 $ret .= $j->provider_name.": ";
174 $embedlink = (isset($j->title))?$j->title:$embedurl;
175 $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
176 if (isset($j->author_name))
177 $ret.=" (".$j->author_name.")";
178 } elseif (isset($j->provider_name) OR isset($j->author_name)) {
180 if (isset($j->provider_name))
181 $embedlink .= $j->provider_name;
183 if (isset($j->author_name)) {
184 if ($embedlink != "")
187 $embedlink .= $j->author_name;
189 if (trim($embedlink) == "")
190 $embedlink = $embedurl;
192 $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
194 //if (isset($j->author_name)) $ret.=" by ".$j->author_name;
195 //if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
198 // add <a> for html2bbcode conversion
199 $ret .= "<a href='$embedurl' rel='oembed'>$embedurl</a>";
200 $ret .= "<br style='clear:left'>";
203 return mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));
206 function oembed_iframe($src,$width,$height) {
208 if(! $width || strstr($width,'%'))
210 if(! $height || strstr($height,'%')) {
212 $resize = 'onload="resizeIframe(this);"';
216 // try and leave some room for the description line.
217 $height = intval($height) + 80;
218 $width = intval($width) + 40;
222 $s = $a->get_baseurl()."/oembed/".base64url_encode($src);
223 return '<iframe '.$resize.' class="embed_rich" height="'.$height.'" width="'.$width.'" src="'.$s.'" frameborder="no">'.t('Embedded content').'</iframe>';
229 function oembed_bbcode2html($text){
230 $stopoembed = get_config("system","no_oembed");
231 if ($stopoembed == true){
232 return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
234 return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
238 function oe_build_xpath($attr, $value){
239 // http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html
240 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'";
243 function oe_get_inner_html( $node ) {
245 $children = $node->childNodes;
246 foreach ($children as $child) {
247 $innerHTML .= $child->ownerDocument->saveXML( $child );
253 * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
254 * and replace it with [embed]url[/embed]
256 function oembed_html2bbcode($text) {
257 // start parser only if 'oembed' is in text
258 if (strpos($text, "oembed")){
260 // convert non ascii chars to html entities
261 $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
263 // If it doesn't parse at all, just return the text.
264 $dom = @DOMDocument::loadHTML($html_text);
267 $xpath = new DOMXPath($dom);
270 $xattr = oe_build_xpath("class","oembed");
271 $entries = $xpath->query("//span[$xattr]");
273 $xattr = "@rel='oembed'";//oe_build_xpath("rel","oembed");
274 foreach($entries as $e) {
275 $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
276 if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[/embed]"), $e);
278 return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );