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