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