]> git.mxchange.org Git - friendica.git/blob - include/oembed.php
d40ceb3fd58389f9e1999bd93318441804eb7ddd
[friendica.git] / include / oembed.php
1 <?php
2 function oembed_replacecb($matches){
3         $embedurl=$matches[1];
4         $j = oembed_fetch_url($embedurl);
5         return oembed_format_object($j);
6 }
7
8
9 function oembed_fetch_url($embedurl){
10
11         $txt = Cache::get($embedurl);
12                                 
13         if(is_null($txt)){
14                 $txt = "";
15                 
16                 // try oembed autodiscovery
17                 $redirects = 0;
18                 $html_text = fetch_url($embedurl, false, $redirects, 15);
19                 if(! $html_text)
20                         return;
21                 $dom = @DOMDocument::loadHTML($html_text);
22                 if ($dom){
23                         $xpath = new DOMXPath($dom);
24                         $attr = "oembed";
25                 
26                         $xattr = oe_build_xpath("class","oembed");
27                         $entries = $xpath->query("//link[@type='application/json+oembed']");
28                         foreach($entries as $e){
29                                 $href = $e->getAttributeNode("href")->nodeValue;
30                                 $txt = fetch_url($href);
31                         }
32                 }
33                 
34                 if ($txt==false || $txt==""){
35                         // try oohembed service
36                         $ourl = "http://oohembed.com/oohembed/?url=".urlencode($embedurl);  
37                         $txt = fetch_url($ourl);
38                 }
39                 
40                 $txt=trim($txt);
41                 if ($txt[0]!="{") $txt='{"type":"error"}';
42         
43                 //save in cache
44                 Cache::set($embedurl,$txt);
45
46         }
47         
48         $j = json_decode($txt);
49         $j->embedurl = $embedurl;
50         return $j;
51 }
52         
53 function oembed_format_object($j){
54         $embedurl = $j->embedurl;
55         $ret="<span class='oembed ".$j->type."'>";
56         switch ($j->type) {
57                 case "video": {
58                         if (isset($j->thumbnail_url)) {
59                                 /*$tw = (isset($j->thumbnail_width)) ? $j->thumbnail_width:200;
60                                 $th = (isset($j->thumbnail_height)) ? $j->thumbnail_height:180;*/
61                                 $tw=150; $th=120; 
62                                 $ret.= "<a href='".$embedurl."' onclick='this.innerHTML=unescape(\"".urlencode($j->html)."\").replace(/\+/g,\" \"); return false;' style='float:left; margin: 1em; '>";
63                                 $ret.= "<img width='$tw' height='$th' src='".$j->thumbnail_url."'>";
64                                 $ret.= "</a>";
65                         } else {
66                                 $ret=$j->html;
67                         }
68                         $ret.="<br>";
69                 }; break;
70                 case "photo": {
71                         $ret.= "<img width='".$j->width."' height='".$j->height."' src='".$j->url."'>";
72                         $ret.="<br>";
73                 }; break;  
74                 case "link": {
75                         //$ret = "<a href='".$embedurl."'>".$j->title."</a>";
76                 }; break;  
77                 case "rich": {
78                         // not so safe.. 
79                         $ret.= $j->html;
80                 }; break;
81         }
82
83         // add link to source if not present in "rich" type
84         if (  $j->type!='rich' || !strpos($ret,$embedurl) ){
85                 $embedlink = (isset($j->title))?$j->title:$embedurl;
86                 $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
87                 if (isset($j->author_name)) $ret.=" by ".$j->author_name;
88                 if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
89         } else {
90                 // add <a> for html2bbcode conversion
91                 $ret .= "<a href='$embedurl' rel='oembed'/>";
92         }
93         $ret.="<br style='clear:left'></span>";
94         return  mb_convert_encoding($ret, 'HTML-ENTITIES', mb_detect_encoding($ret));
95 }
96
97 function oembed_bbcode2html($text){
98         $stopoembed = get_config("system","no_oembed");
99         if ($stopoembed == true){
100                 return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
101         }
102         return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
103 }
104
105
106 function oe_build_xpath($attr, $value){
107         // http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html
108         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'";
109 }
110
111 function oe_get_inner_html( $node ) {
112     $innerHTML= '';
113     $children = $node->childNodes;
114     foreach ($children as $child) {
115         $innerHTML .= $child->ownerDocument->saveXML( $child );
116     }
117     return $innerHTML;
118
119
120 /**
121  * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
122  * and replace it with [embed]url[/embed]
123  */
124 function oembed_html2bbcode($text) {
125         // start parser only if 'oembed' is in text
126         if (strpos($text, "oembed")){
127                 
128                 // convert non ascii chars to html entities
129                 $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
130                 
131                 // If it doesn't parse at all, just return the text.
132                 $dom = @DOMDocument::loadHTML($html_text);
133                 if(! $dom)
134                         return $text;
135                 $xpath = new DOMXPath($dom);
136                 $attr = "oembed";
137                 
138                 $xattr = oe_build_xpath("class","oembed");
139                 $entries = $xpath->query("//span[$xattr]");
140
141                 $xattr = "@rel='oembed'";//oe_build_xpath("rel","oembed");
142                 foreach($entries as $e) {
143                         $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
144                         if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[/embed]"), $e);
145                 }
146                 return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );
147         } else {
148                 return $text;
149         } 
150 }
151
152
153