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