]> git.mxchange.org Git - friendica.git/blob - include/oembed.php
Merge pull request #907 from annando/master
[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         $a = get_app();
16
17         $txt = Cache::get($a->videowidth . $embedurl);
18
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
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 . '&maxwidth=' . $a->videowidth);
44                                                 break;
45                                         }
46                                 }
47                         }
48                 }
49
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);
54                 }
55
56                 $txt=trim($txt);
57                 if ($txt[0]!="{") $txt='{"type":"error"}';
58
59                 //save in cache
60                 Cache::set($a->videowidth . $embedurl,$txt);
61
62         }
63
64         $j = json_decode($txt);
65
66         if (!is_object($j))
67                 return false;
68
69         $j->embedurl = $embedurl;
70         return $j;
71 }
72
73 function oembed_format_object($j){
74         $a = get_app();
75         $embedurl = $j->embedurl;
76         $jhtml = oembed_iframe($j->embedurl,(isset($j->width) ? $j->width : null), (isset($j->height) ? $j->height : null) );
77         $ret="<span class='oembed ".$j->type."'>";
78         switch ($j->type) {
79                 case "video": {
80                         if (isset($j->thumbnail_url)) {
81                                 $tw = (isset($j->thumbnail_width) && intval($j->thumbnail_width)) ? $j->thumbnail_width:200;
82                                 $th = (isset($j->thumbnail_height) && intval($j->thumbnail_height)) ? $j->thumbnail_height:180;
83                                 // make sure we don't attempt divide by zero, fallback is a 1:1 ratio
84                                 $tr = (($th) ? $tw/$th : 1);
85
86                                 $th=120; $tw = $th*$tr;
87                                 $tpl=get_markup_template('oembed_video.tpl');
88                                 $ret.=replace_macros($tpl, array(
89                                 '$baseurl' => $a->get_baseurl(),
90                                         '$embedurl'=>$embedurl,
91                                         '$escapedhtml'=>base64_encode($jhtml),
92                                         '$tw'=>$tw,
93                                         '$th'=>$th,
94                                         '$turl'=>$j->thumbnail_url,
95                                 ));
96
97                         } else {
98                                 $ret=$jhtml;
99                         }
100                         $ret.="<br>";
101                 }; break;
102                 case "photo": {
103                         $ret.= "<img width='".$j->width."' src='".$j->url."'>";
104                         //$ret.= "<img width='".$j->width."' height='".$j->height."' src='".$j->url."'>";
105                         $ret.="<br>";
106                 }; break;
107                 case "link": {
108                         //$ret = "<a href='".$embedurl."'>".$j->title."</a>";
109                 }; break;
110                 case "rich": {
111                         // not so safe..
112                         if (!get_config("system","no_oembed_rich_content"))
113                                 $ret.= $jhtml;
114                 }; break;
115         }
116
117         // add link to source if not present in "rich" type
118         if ($j->type!='rich' || !strpos($j->html,$embedurl) ){
119                 if (isset($j->title)) {
120                         if (isset($j->provider_name))
121                                 $ret .= $j->provider_name.": ";
122
123                         $embedlink = (isset($j->title))?$j->title:$embedurl;
124                         $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
125                         if (isset($j->author_name))
126                                 $ret.=" (".$j->author_name.")";
127                 } elseif (isset($j->provider_name) OR isset($j->author_name)) {
128                         $embedlink = "";
129                         if (isset($j->provider_name))
130                                 $embedlink .= $j->provider_name;
131
132                         if (isset($j->author_name)) {
133                                 if ($embedlink != "")
134                                         $embedlink .= ": ";
135
136                                 $embedlink .= $j->author_name;
137                         }
138                         $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
139                 }
140                 //if (isset($j->author_name)) $ret.=" by ".$j->author_name;
141                 //if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
142         } else {
143                 // add <a> for html2bbcode conversion
144                 $ret .= "<a href='$embedurl' rel='oembed'></a>";
145         }
146         $ret.="<br style='clear:left'></span>";
147         return  mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));
148 }
149
150 function oembed_iframe($src,$width,$height) {
151         if(! $width || strstr($width,'%'))
152                 $width = '640';
153         if(! $height || strstr($height,'%'))
154                 $height = '300';
155         // try and leave some room for the description line.
156         $height = intval($height) + 80;
157         $width  = intval($width) + 40;
158
159         $a = get_app();
160
161         $s = $a->get_baseurl()."/oembed/".base64url_encode($src);
162         return '<iframe height="' . $height . '" width="' . $width . '" src="' . $s . '" frameborder="no" >' . t('Embedded content') . '</iframe>';
163
164 }
165
166
167
168 function oembed_bbcode2html($text){
169         $stopoembed = get_config("system","no_oembed");
170         if ($stopoembed == true){
171                 return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
172         }
173         return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
174 }
175
176
177 function oe_build_xpath($attr, $value){
178         // http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html
179         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'";
180 }
181
182 function oe_get_inner_html( $node ) {
183     $innerHTML= '';
184     $children = $node->childNodes;
185     foreach ($children as $child) {
186         $innerHTML .= $child->ownerDocument->saveXML( $child );
187     }
188     return $innerHTML;
189 }
190
191 /**
192  * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
193  * and replace it with [embed]url[/embed]
194  */
195 function oembed_html2bbcode($text) {
196         // start parser only if 'oembed' is in text
197         if (strpos($text, "oembed")){
198
199                 // convert non ascii chars to html entities
200                 $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
201
202                 // If it doesn't parse at all, just return the text.
203                 $dom = @DOMDocument::loadHTML($html_text);
204                 if(! $dom)
205                         return $text;
206                 $xpath = new DOMXPath($dom);
207                 $attr = "oembed";
208
209                 $xattr = oe_build_xpath("class","oembed");
210                 $entries = $xpath->query("//span[$xattr]");
211
212                 $xattr = "@rel='oembed'";//oe_build_xpath("rel","oembed");
213                 foreach($entries as $e) {
214                         $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
215                         if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[/embed]"), $e);
216                 }
217                 return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );
218         } else {
219                 return $text;
220         }
221 }