]> git.mxchange.org Git - friendica.git/blobdiff - mod/proxy.php
new version of the ShaShape iconset
[friendica.git] / mod / proxy.php
index 77515bb8881030e2526824b6d7abdae6fa54d4e6..d26967dddf2f1e6e21d1749728f213236cbab419 100644 (file)
@@ -3,6 +3,12 @@
 
 define("PROXY_DEFAULT_TIME", 86400); // 1 Day
 
+define("PROXY_SIZE_MICRO", "micro");
+define("PROXY_SIZE_THUMB", "thumb");
+define("PROXY_SIZE_SMALL", "small");
+define("PROXY_SIZE_MEDIUM", "medium");
+define("PROXY_SIZE_LARGE", "large");
+
 require_once('include/security.php');
 require_once("include/Photo.php");
 
@@ -37,6 +43,7 @@ function proxy_init() {
 
        $thumb = false;
        $size = 1024;
+       $sizetype = "";
 
        // If the cache path isn't there, try to create it
        if (!is_dir($_SERVER["DOCUMENT_ROOT"]."/proxy"))
@@ -47,7 +54,7 @@ function proxy_init() {
        $direct_cache = (is_dir($_SERVER["DOCUMENT_ROOT"]."/proxy") AND is_writable($_SERVER["DOCUMENT_ROOT"]."/proxy"));
 
        // Look for filename in the arguments
-       if (isset($a->argv[1]) OR isset($a->argv[2]) OR isset($a->argv[3])) {
+       if ((isset($a->argv[1]) OR isset($a->argv[2]) OR isset($a->argv[3])) AND !isset($_REQUEST["url"])) {
                if (isset($a->argv[3]))
                        $url = $a->argv[3];
                elseif (isset($a->argv[2]))
@@ -59,14 +66,27 @@ function proxy_init() {
                        $size = 200;
 
                // thumb, small, medium and large.
-               if (substr($url, -6) == ":thumb")
-                       $size = 150;
-               if (substr($url, -6) == ":small")
-                       $size = 340;
-               if (substr($url, -7) == ":medium")
+               if (substr($url, -6) == ":micro") {
+                       $size = 48;
+                       $sizetype = ":micro";
+                       $url = substr($url, 0, -6);
+               } elseif (substr($url, -6) == ":thumb") {
+                       $size = 80;
+                       $sizetype = ":thumb";
+                       $url = substr($url, 0, -6);
+               } elseif (substr($url, -6) == ":small") {
+                       $size = 175;
+                       $url = substr($url, 0, -6);
+                       $sizetype = ":small";
+               } elseif (substr($url, -7) == ":medium") {
                        $size = 600;
-               if (substr($url, -6) == ":large")
+                       $url = substr($url, 0, -7);
+                       $sizetype = ":medium";
+               } elseif (substr($url, -6) == ":large") {
                        $size = 1024;
+                       $url = substr($url, 0, -6);
+                       $sizetype = ":large";
+               }
 
                $pos = strrpos($url, "=.");
                if ($pos)
@@ -78,7 +98,8 @@ function proxy_init() {
 
                if ($url)
                        $_REQUEST['url'] = $url;
-       }
+       } else
+               $direct_cache = false;
 
        if (!$direct_cache) {
                $urlhash = 'pic:' . sha1($_REQUEST['url']);
@@ -175,6 +196,8 @@ function proxy_init() {
                }
        }
 
+       $img_str_orig = $img_str;
+
        // reduce quality - if it isn't a GIF
        if ($mime != "image/gif") {
                $img = new Photo($img_str, $mime);
@@ -187,10 +210,12 @@ function proxy_init() {
        // If there is a real existing directory then put the cache file there
        // advantage: real file access is really fast
        // Otherwise write in cachefile
-       if ($valid AND $direct_cache)
-               file_put_contents($_SERVER["DOCUMENT_ROOT"]."/proxy/".proxy_url($_REQUEST['url'], true), $img_str);
-       elseif ($cachefile != '')
-               file_put_contents($cachefile, $img_str);
+       if ($valid AND $direct_cache) {
+               file_put_contents($_SERVER["DOCUMENT_ROOT"]."/proxy/".proxy_url($_REQUEST['url'], true), $img_str_orig);
+               if ($sizetype <> '')
+                       file_put_contents($_SERVER["DOCUMENT_ROOT"]."/proxy/".proxy_url($_REQUEST['url'], true).$sizetype, $img_str);
+       } elseif ($cachefile != '')
+               file_put_contents($cachefile, $img_str_orig);
 
        header("Content-type: $mime");
 
@@ -207,14 +232,19 @@ function proxy_init() {
        killme();
 }
 
-function proxy_url($url, $writemode = false) {
+function proxy_url($url, $writemode = false, $size = "") {
        global $_SERVER;
 
+       $a = get_app();
+
        // Only continue if it isn't a local image and the isn't deactivated
-       if (get_config("system", "proxy_disabled") OR proxy_is_local_image($url))
+       if (proxy_is_local_image($url)) {
+               $url = str_replace(normalise_link($a->get_baseurl())."/", $a->get_baseurl()."/", $url);
                return($url);
+       }
 
-       $a = get_app();
+       if (get_config("system", "proxy_disabled"))
+               return($url);
 
        // Creating a sub directory to reduce the amount of files in the cache directory
        $basepath = $_SERVER["DOCUMENT_ROOT"]."/proxy";
@@ -245,13 +275,19 @@ function proxy_url($url, $writemode = false) {
 
        $proxypath = $a->get_baseurl()."/proxy/".$path;
 
+       if ($size != "")
+               $size = ":".$size;
+
        // Too long files aren't supported by Apache
-       if (strlen($proxypath) > 250)
-               return ($url);
+       // Writemode in combination with long files shouldn't be possible
+       if ((strlen($proxypath) > 250) AND $writemode)
+               return (hash("md5", $url));
+       elseif (strlen($proxypath) > 250)
+               return ($a->get_baseurl()."/proxy/".hash("md5", $url)."?url=".urlencode($url));
        elseif ($writemode)
                return ($path);
        else
-               return ($proxypath);
+               return ($proxypath.$size);
 }
 
 /**
@@ -303,5 +339,8 @@ function proxy_img_cb($matches) {
 }
 
 function proxy_parse_html($html) {
+       $a = get_app();
+       $html = str_replace(normalise_link($a->get_baseurl())."/", $a->get_baseurl()."/", $html);
+
        return preg_replace_callback("/(<img [^>]*src *= *[\"'])([^\"']+)([\"'][^>]*>)/siU", "proxy_img_cb", $html);
 }