]> git.mxchange.org Git - friendica.git/blob - include/oembed.php
oembed: revert changes from 48f5bd492d
[friendica.git] / include / oembed.php
1 <?php
2 function oembed_replacecb($matches){
3         logger('oembedcb');
4         $embedurl=$matches[1];
5         $j = oembed_fetch_url($embedurl);
6         $s =  oembed_format_object($j);
7         return $s;//oembed_iframe($s,$j->width,$j->height);
8
9
10 }
11
12
13 function oembed_fetch_url($embedurl){
14
15         $txt = Cache::get($embedurl);
16
17         $noexts = array("mp3","mp4","ogg","ogv","oga","ogm","webm");
18         $ext = pathinfo(strtolower($embedurl),PATHINFO_EXTENSION);
19         
20                                 
21         if(is_null($txt)){
22                 $txt = "";
23                 
24                 if (!in_array($ext, $noexts)){
25                         // try oembed autodiscovery
26                         $redirects = 0;
27                         $html_text = fetch_url($embedurl, false, $redirects, 15, "text/*");
28                         if($html_text){
29                                 $dom = @DOMDocument::loadHTML($html_text);
30                                 if ($dom){
31                                         $xpath = new DOMXPath($dom);
32                                         $attr = "oembed";
33                                 
34                                         $xattr = oe_build_xpath("class","oembed");
35                                         $entries = $xpath->query("//link[@type='application/json+oembed']");
36                                         foreach($entries as $e){
37                                                 $href = $e->getAttributeNode("href")->nodeValue;
38                                                 $txt = fetch_url($href);
39                                         }
40                                 }
41                         }
42                 }
43                 
44                 if ($txt==false || $txt==""){
45                         // try oohembed service
46                         $ourl = "http://oohembed.com/oohembed/?url=".urlencode($embedurl);  
47                         $txt = fetch_url($ourl);
48                 }
49                 
50                 $txt=trim($txt);
51                 if ($txt[0]!="{") $txt='{"type":"error"}';
52         
53                 //save in cache
54                 Cache::set($embedurl,$txt);
55
56         }
57         
58         $j = json_decode($txt);
59         $j->embedurl = $embedurl;
60         return $j;
61 }
62         
63 function oembed_format_object($j){
64         $embedurl = $j->embedurl;
65         $jhtml = oembed_iframe($j->embedurl,$j->width,$j->height );
66         $ret="<span class='oembed ".$j->type."'>";
67         switch ($j->type) {
68                 case "video": {
69                         if (isset($j->thumbnail_url)) {
70                                 $tw = (isset($j->thumbnail_width)) ? $j->thumbnail_width:200;
71                                 $th = (isset($j->thumbnail_height)) ? $j->thumbnail_height:180;
72                                 $tr = $tw/$th;
73                                 
74                                 $th=120; $tw = $th*$tr;
75                                 $tpl=get_markup_template('oembed_video.tpl');
76                                 $ret.=replace_macros($tpl, array(
77                                         '$embedurl'=>$embedurl,
78                                         '$escapedhtml'=>base64_encode($jhtml),
79                                         '$tw'=>$tw,
80                                         '$th'=>$th,
81                                         '$turl'=>$j->thumbnail_url,
82                                 ));
83                                 
84                         } else {
85                                 $ret=$jhtml;
86                         }
87                         $ret.="<br>";
88                 }; break;
89                 case "photo": {
90                         $ret.= "<img width='".$j->width."' height='".$j->height."' src='".$j->url."'>";
91                         $ret.="<br>";
92                 }; break;  
93                 case "link": {
94                         //$ret = "<a href='".$embedurl."'>".$j->title."</a>";
95                 }; break;  
96                 case "rich": {
97                         // not so safe.. 
98                         $ret.= $jhtml;
99                 }; break;
100         }
101
102         // add link to source if not present in "rich" type
103         if (  $j->type!='rich' || !strpos($j->html,$embedurl) ){
104                 $embedlink = (isset($j->title))?$j->title:$embedurl;
105                 $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
106                 if (isset($j->author_name)) $ret.=" by ".$j->author_name;
107                 if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
108         } else {
109                 // add <a> for html2bbcode conversion
110                 $ret .= "<a href='$embedurl' rel='oembed'/>";
111         }
112         $ret.="<br style='clear:left'></span>";
113         return  mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));
114 }
115
116 function oembed_iframe($src,$width,$height) {
117         if(! $width || strstr($width,'%'))
118                 $width = '640';
119         if(! $height || strstr($height,'%'))
120                 $height = '300';
121         // try and leave some room for the description line. 
122         $height = intval($height) + 80;
123         $width  = intval($width) + 40;
124
125         $a = get_app();
126
127         $s = $a->get_baseurl()."/oembed/".base64url_encode($src);
128         return '<iframe height="' . $height . '" width="' . $width . '" src="' . $s . '" frameborder="no" >' . t('Embedded content') . '</iframe>'; 
129
130 }
131
132
133
134 function oembed_bbcode2html($text){
135         $stopoembed = get_config("system","no_oembed");
136         if ($stopoembed == true){
137                 return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
138         }
139         return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
140 }
141
142
143 function oe_build_xpath($attr, $value){
144         // http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html
145         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'";
146 }
147
148 function oe_get_inner_html( $node ) {
149     $innerHTML= '';
150     $children = $node->childNodes;
151     foreach ($children as $child) {
152         $innerHTML .= $child->ownerDocument->saveXML( $child );
153     }
154     return $innerHTML;
155
156
157 /**
158  * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
159  * and replace it with [embed]url[/embed]
160  */
161 function oembed_html2bbcode($text) {
162         // start parser only if 'oembed' is in text
163         if (strpos($text, "oembed")){
164                 
165                 // convert non ascii chars to html entities
166                 $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
167                 
168                 // If it doesn't parse at all, just return the text.
169                 $dom = @DOMDocument::loadHTML($html_text);
170                 if(! $dom)
171                         return $text;
172                 $xpath = new DOMXPath($dom);
173                 $attr = "oembed";
174                 
175                 $xattr = oe_build_xpath("class","oembed");
176                 $entries = $xpath->query("//span[$xattr]");
177
178                 $xattr = "@rel='oembed'";//oe_build_xpath("rel","oembed");
179                 foreach($entries as $e) {
180                         $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
181                         if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[/embed]"), $e);
182                 }
183                 return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );
184         } else {
185                 return $text;
186         } 
187 }
188
189
190