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