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