]> git.mxchange.org Git - friendica.git/blob - include/oembed.php
regex stuffed up
[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'>";
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          $ret = "<a href='".$embedurl."' onclick='this.innerHTML=unescape(\"".urlencode($j->html)."\").replace(/\+/g,\" \"); return false;' >";
26          $ret.= "<img width='$tw' height='$th' src='".$j->thumbnail_url."'>";
27          $ret.= "</a>";
28        } else {
29          $ret=$j->html;
30        }
31        $ret.="<br>";
32     }; break;
33     case "photo": {
34       $ret = "<img width='".$j->width."' height='".$j->height."' src='".$j->url."'>";
35       $ret.="<br>";
36     }; break;  
37     case "link": {
38       //$ret = "<a href='".$embedurl."'>".$j->title."</a>";
39     }; break;  
40     case "rich": {
41       // not so safe.. 
42       $ret = "<blockquote>".$j->html."</blockquote>";
43     }; break;
44   }
45   
46   $embedlink = (isset($j->title))?$j->title:$embedurl;
47   $ret .= "<a href='$embedurl' rel='oembed'>$embedlink</a>";
48   if (isset($j->author_name)) $ret.=" by ".$j->author_name;
49   if (isset($j->provider_name)) $ret.=" on ".$j->provider_name;
50   $ret.="</span>";
51   return $ret;
52 }
53
54 function oembed_bbcode2html($text){
55         $stopoembed = get_config("system","no_oembed");
56         if ($stopoembed == true){
57                 return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
58         }
59         return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
60 }
61
62
63 function oe_build_xpath($attr, $value){
64         // http://westhoffswelt.de/blog/0036_xpath_to_select_html_by_class.html
65         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'";
66 }
67
68 function oe_get_inner_html( $node ) {
69     $innerHTML= '';
70     $children = $node->childNodes;
71     foreach ($children as $child) {
72         $innerHTML .= $child->ownerDocument->saveXML( $child );
73     }
74     return $innerHTML;
75
76
77 /**
78  * Find <span class='oembed'>..<a href='url' rel='oembed'>..</a></span>
79  * and replace it with [embed]url[/embed]
80  */
81 function oembed_html2bbcode($text) {
82         // start parser only if 'oembed' is in text
83         if (strpos($text, "oembed")){
84                 
85                 // convert non ascii chars to html entities
86                 $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
87                 
88                 // If it doesn't parse at all, just return the text.
89                 $dom = @DOMDocument::loadHTML($html_text);
90                 if(! $dom)
91                         return $text;
92                 $xpath = new DOMXPath($dom);
93                 $attr = "oembed";
94                 
95                 $xattr = oe_build_xpath("class","oembed");
96                 $entries = $xpath->query("//span[$xattr]");
97                 
98                 $xattr = oe_build_xpath("rel","oembed");
99                 foreach($entries as $e) {
100                         $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
101                         if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[embed]"), $e);
102                 }
103                 return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );
104         } else {
105                 return $text;
106         } 
107 }
108
109 ?>