]> git.mxchange.org Git - friendica.git/blobdiff - include/oembed.php
multi-user, do not cache open mbox
[friendica.git] / include / oembed.php
index f9441b370944fb54627e4767f2acf70075911c93..4d2b7185e9a43d2f516a46e62461b605f0150d97 100644 (file)
@@ -1,8 +1,20 @@
 <?php
 function oembed_replacecb($matches){
   $embedurl=$matches[1];
-  $ourl = "http://oohembed.com/oohembed/?url=".urlencode($embedurl);  
-  $txt = fetch_url($ourl);
+  
+  $r = q("SELECT v FROM `cache` WHERE k='%s'",
+               dbesc($embedurl));
+  if(count($r)){
+       $txt = $r[0]['v'];
+  } else {
+         $ourl = "http://oohembed.com/oohembed/?url=".urlencode($embedurl);  
+         $txt = fetch_url($ourl);
+         //save in cache
+         q("INSERT INTO `cache` VALUES ('%s','%s','%s')",
+               dbesc($embedurl),
+               dbesc($txt),
+               dbesc(datetime_convert()));
+  }
   $j = json_decode($txt);
   $ret="<span class='oembed'>";
   switch ($j->type) {
@@ -44,7 +56,7 @@ function oembed_bbcode2html($text){
        if ($stopoembed == true){
                return preg_replace("/\[embed\](.+?)\[\/embed\]/is", "<!-- oembed $1 --><i>". t('Embedding disabled') ." : $1</i><!-- /oembed $1 -->" ,$text);
        }
-       return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", oembed_replacecb ,$text);
+       return preg_replace_callback("/\[embed\](.+?)\[\/embed\]/is", 'oembed_replacecb' ,$text);
 }
 
 
@@ -67,19 +79,31 @@ function oe_get_inner_html( $node ) {
  * and replace it with [embed]url[/embed]
  */
 function oembed_html2bbcode($text) {
-       $dom = DOMDocument::loadHTML($text);
-       $xpath = new DOMXPath($dom);
-       $attr = "oembed";
-       
-       $xattr = oe_build_xpath("class","oembed");
-       $entries = $xpath->query("//span[$xattr]");
-       
-       $xattr = oe_build_xpath("rel","oembed");
-       foreach($entries as $e) {
-               $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
-               if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[embed]"), $e);
-       }
-       return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) ); 
+       // start parser only if 'oembed' is in text
+       if (strpos($text, "oembed")){
+               
+               // convert non ascii chars to html entities
+               $html_text = mb_convert_encoding($text, 'HTML-ENTITIES', mb_detect_encoding($text));
+               
+               // If it doesn't parse at all, just return the text.
+               $dom = @DOMDocument::loadHTML($html_text);
+               if(! $dom)
+                       return $text;
+               $xpath = new DOMXPath($dom);
+               $attr = "oembed";
+               
+               $xattr = oe_build_xpath("class","oembed");
+               $entries = $xpath->query("//span[$xattr]");
+               
+               $xattr = oe_build_xpath("rel","oembed");
+               foreach($entries as $e) {
+                       $href = $xpath->evaluate("a[$xattr]/@href", $e)->item(0)->nodeValue;
+                       if(!is_null($href)) $e->parentNode->replaceChild(new DOMText("[embed]".$href."[embed]"), $e);
+               }
+               return oe_get_inner_html( $dom->getElementsByTagName("body")->item(0) );
+       } else {
+               return $text;
+       } 
 }
 
 ?>
\ No newline at end of file