]> git.mxchange.org Git - friendica.git/blobdiff - include/bbcode.php
Adding cache mechanism for bbcode. Improving cache to use apc.
[friendica.git] / include / bbcode.php
index 96ca39462d6237c3c2e10608322740fed5fff0fb..7f70563c7ae330bf20df95828d6764356e23bc2d 100644 (file)
@@ -534,77 +534,85 @@ function GetProfileUsername($profile, $username) {
 }
 
 function bb_RemovePictureLinks($match) {
-       $ch = @curl_init($match[1]);
-       @curl_setopt($ch, CURLOPT_NOBODY, true);
-       @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-       @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
-       @curl_exec($ch);
-       $curl_info = @curl_getinfo($ch);
-
-       if (substr($curl_info["content_type"], 0, 6) == "image/")
-               $text = "[url=".$match[1]."]".$match[1]."[/url]";
-       else {
-               $text = "[url=".$match[2]."]".$match[2]."[/url]";
-
-               // if its not a picture then look if its a page that contains a picture link
-               require_once("include/network.php");
-
-               $body = fetch_url($match[1]);
-
-               $doc = new DOMDocument();
-               @$doc->loadHTML($body);
-               $xpath = new DomXPath($doc);
-               $list = $xpath->query("//meta[@name]");
-               foreach ($list as $node) {
-                       $attr = array();
-
-                       if ($node->attributes->length)
-                               foreach ($node->attributes as $attribute)
-                                       $attr[$attribute->name] = $attribute->value;
-
-                       if (strtolower($attr["name"]) == "twitter:image")
-                               $text = "[url=".$attr["content"]."]".$attr["content"]."[/url]";
+       $text = Cache::get($match[1]);
+
+       if(is_null($text)){
+               $ch = @curl_init($match[1]);
+               @curl_setopt($ch, CURLOPT_NOBODY, true);
+               @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+               @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
+               @curl_exec($ch);
+               $curl_info = @curl_getinfo($ch);
+
+               if (substr($curl_info["content_type"], 0, 6) == "image/")
+                       $text = "[url=".$match[1]."]".$match[1]."[/url]";
+               else {
+                       $text = "[url=".$match[2]."]".$match[2]."[/url]";
+
+                       // if its not a picture then look if its a page that contains a picture link
+                       require_once("include/network.php");
+
+                       $body = fetch_url($match[1]);
+
+                       $doc = new DOMDocument();
+                       @$doc->loadHTML($body);
+                       $xpath = new DomXPath($doc);
+                       $list = $xpath->query("//meta[@name]");
+                       foreach ($list as $node) {
+                               $attr = array();
+
+                               if ($node->attributes->length)
+                                       foreach ($node->attributes as $attribute)
+                                               $attr[$attribute->name] = $attribute->value;
+
+                               if (strtolower($attr["name"]) == "twitter:image")
+                                       $text = "[url=".$attr["content"]."]".$attr["content"]."[/url]";
+                       }
                }
+               Cache::set($match[1],$text);
        }
-
        return($text);
 }
 
 function bb_CleanPictureLinksSub($match) {
-       $ch = @curl_init($match[1]);
-       @curl_setopt($ch, CURLOPT_NOBODY, true);
-       @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-       @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
-       @curl_exec($ch);
-       $curl_info = @curl_getinfo($ch);
-
-       // if its a link to a picture then embed this picture
-       if (substr($curl_info["content_type"], 0, 6) == "image/")
-               $text = "[img]".$match[1]."[/img]";
-       else {
-               $text = "[img]".$match[2]."[/img]";
-
-               // if its not a picture then look if its a page that contains a picture link
-               require_once("include/network.php");
-
-               $body = fetch_url($match[1]);
-
-               $doc = new DOMDocument();
-               @$doc->loadHTML($body);
-               $xpath = new DomXPath($doc);
-               $list = $xpath->query("//meta[@name]");
-               foreach ($list as $node) {
-                       $attr = array();
-
-                       if ($node->attributes->length)
-                               foreach ($node->attributes as $attribute)
-                                       $attr[$attribute->name] = $attribute->value;
-
-                       if (strtolower($attr["name"]) == "twitter:image")
-                               $text = "[img]".$attr["content"]."[/img]";
+       $text = Cache::get($match[1]);
+
+       if(is_null($text)){
+               $ch = @curl_init($match[1]);
+               @curl_setopt($ch, CURLOPT_NOBODY, true);
+               @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+               @curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Friendica)");
+               @curl_exec($ch);
+               $curl_info = @curl_getinfo($ch);
+
+               // if its a link to a picture then embed this picture
+               if (substr($curl_info["content_type"], 0, 6) == "image/")
+                       $text = "[img]".$match[1]."[/img]";
+               else {
+                       $text = "[img]".$match[2]."[/img]";
+
+                       // if its not a picture then look if its a page that contains a picture link
+                       require_once("include/network.php");
+
+                       $body = fetch_url($match[1]);
+
+                       $doc = new DOMDocument();
+                       @$doc->loadHTML($body);
+                       $xpath = new DomXPath($doc);
+                       $list = $xpath->query("//meta[@name]");
+                       foreach ($list as $node) {
+                               $attr = array();
+
+                               if ($node->attributes->length)
+                                       foreach ($node->attributes as $attribute)
+                                               $attr[$attribute->name] = $attribute->value;
+
+                               if (strtolower($attr["name"]) == "twitter:image")
+                                       $text = "[img]".$attr["content"]."[/img]";
+                       }
                }
+               Cache::set($match[1],$text);
        }
-
        return($text);
 }