]> git.mxchange.org Git - friendica.git/blob - include/oembed.php
Merge branch 'pull'
[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.= "<blockquote>".$j->html."</blockquote>";
85                 }; break;
86         }
87
88         $embedlink = (isset($j->title))?$j->title:$embedurl;
89         $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
90         if (isset($j->author_name)) $ret.=" by ".$j->author_name;
91         if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
92         $ret.="<br style='clear:left'></span>";
93         return $ret;
94 }
95
96 function oembed_bbcode2html($text){
97         $stopoembed = get_config("system","no_oembed");
98         if ($stopoembed == true){
99                 return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
100         }
101         return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
102 }
103
104
105 function oe_build_xpath($attr, $value){
106         // http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html
107         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'";
108 }
109
110 function oe_get_inner_html( $node ) {
111     $innerHTML= '';
112     $children = $node->childNodes;
113     foreach ($children as $child) {
114         $innerHTML .= $child->ownerDocument->saveXML( $child );
115     }
116     return $innerHTML;
117
118
119 /**
120  * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
121  * and replace it with [embed]url[/embed]
122  */
123 function oembed_html2bbcode($text) {
124         // start parser only if 'oembed' is in text
125         if (strpos($text, "oembed")){
126                 
127                 // convert non ascii chars to html entities
128                 $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
129                 
130                 // If it doesn't parse at all, just return the text.
131                 $dom = @DOMDocument::loadHTML($html_text);
132                 if(! $dom)
133                         return $text;
134                 $xpath = new DOMXPath($dom);
135                 $attr = "oembed";
136                 
137                 $xattr = oe_build_xpath("class","oembed");
138                 $entries = $xpath->query("//span[$xattr]");
139                 
140                 $xattr = oe_build_xpath("rel","oembed");
141                 foreach($entries as $e) {
142                         $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
143                         if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[/embed]"), $e);
144                 }
145                 return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );
146         } else {
147                 return $text;
148         } 
149 }
150
151 ?>