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