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");
$thumb = false;
$size = 1024;
+ $sizetype = "";
+ $basepath = $a->get_basepath();
// If the cache path isn't there, try to create it
- if (!is_dir($_SERVER["DOCUMENT_ROOT"]."/proxy"))
- if (is_writable($_SERVER["DOCUMENT_ROOT"]))
- mkdir($_SERVER["DOCUMENT_ROOT"]."/proxy");
+ if (!is_dir($basepath."/proxy"))
+ if (is_writable($basepath))
+ mkdir($basepath."/proxy");
// Checking if caching into a folder in the webroot is activated and working
- $direct_cache = (is_dir($_SERVER["DOCUMENT_ROOT"]."/proxy") AND is_writable($_SERVER["DOCUMENT_ROOT"]."/proxy"));
+ $direct_cache = (is_dir($basepath."/proxy") AND is_writable($basepath."/proxy"));
// Look for filename in the arguments
if ((isset($a->argv[1]) OR isset($a->argv[2]) OR isset($a->argv[3])) AND !isset($_REQUEST["url"])) {
$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)
$valid = true;
if (!$direct_cache AND ($cachefile == "")) {
- $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' LIMIT 1", $urlhash);
- if (count($r)) {
+ $r = qu("SELECT * FROM `photo` WHERE `resource-id` = '%s' LIMIT 1", $urlhash);
+ if (dbm::is_result($r)) {
$img_str = $r[0]['data'];
$mime = $r[0]["desc"];
if ($mime == "") $mime = "image/jpeg";
} else
$r = array();
- if (!count($r)) {
+ if (!dbm::is_result($r)) {
// It shouldn't happen but it does - spaces in URL
$_REQUEST['url'] = str_replace(" ", "+", $_REQUEST['url']);
$redirects = 0;
}
}
+ $img_str_orig = $img_str;
+
// reduce quality - if it isn't a GIF
if ($mime != "image/gif") {
$img = new Photo($img_str, $mime);
// 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($basepath."/proxy/".proxy_url($_REQUEST['url'], true), $img_str_orig);
+ if ($sizetype <> '')
+ file_put_contents($basepath."/proxy/".proxy_url($_REQUEST['url'], true).$sizetype, $img_str);
+ } elseif ($cachefile != '')
+ file_put_contents($cachefile, $img_str_orig);
header("Content-type: $mime");
killme();
}
-function proxy_url($url, $writemode = false) {
- global $_SERVER;
-
+/**
+ * @brief Transform a remote URL into a local one
+ *
+ * This function only performs the URL replacement on http URL and if the
+ * provided URL isn't local, "the isn't deactivated" (sic) and if the config
+ * system.proxy_disabled is set to false.
+ *
+ * @param string $url The URL to proxyfy
+ * @param bool $writemode Returns a local path the remote URL should be saved to
+ * @param string $size One of the PROXY_SIZE_* constants
+ *
+ * @return string The proxyfied URL or relative path
+ */
+function proxy_url($url, $writemode = false, $size = '') {
$a = get_app();
+ if (substr($url, 0, strlen('http')) !== 'http') {
+ return $url;
+ }
+
// Only continue if it isn't a local image and the isn't deactivated
if (proxy_is_local_image($url)) {
- $url = str_replace(normalise_link($a->get_baseurl())."/", $a->get_baseurl()."/", $url);
- return($url);
+ $url = str_replace(normalise_link(App::get_baseurl()) . '/', App::get_baseurl() . '/', $url);
+ return $url;
}
- if (get_config("system", "proxy_disabled"))
- return($url);
+ if (get_config('system', 'proxy_disabled')) {
+ return $url;
+ }
+
+ // Image URL may have encoded ampersands for display which aren't desirable for proxy
+ $url = html_entity_decode($url, ENT_NOQUOTES, 'utf-8');
// Creating a sub directory to reduce the amount of files in the cache directory
- $basepath = $_SERVER["DOCUMENT_ROOT"]."/proxy";
+ $basepath = $a->get_basepath() . '/proxy';
- $path = substr(hash("md5", $url), 0, 2);
+ $shortpath = hash('md5', $url);
+ $longpath = substr($shortpath, 0, 2);
- if (is_dir($basepath) and $writemode)
- if (!is_dir($basepath."/".$path)) {
- mkdir($basepath."/".$path);
- chmod($basepath."/".$path, 0777);
+ if (is_dir($basepath) and $writemode) {
+ if (!is_dir($basepath . '/' . $longpath)) {
+ mkdir($basepath . '/' . $longpath);
+ chmod($basepath . '/' . $longpath, 0777);
}
+ }
- $path .= "/".strtr(base64_encode($url), '+/', '-_');
+ $longpath .= '/' . strtr(base64_encode($url), '+/', '-_');
// Checking for valid extensions. Only add them if they are safe
- $pos = strrpos($url, ".");
+ $pos = strrpos($url, '.');
if ($pos) {
- $extension = strtolower(substr($url, $pos+1));
- $pos = strpos($extension, "?");
- if ($pos)
+ $extension = strtolower(substr($url, $pos + 1));
+ $pos = strpos($extension, '?');
+ if ($pos) {
$extension = substr($extension, 0, $pos);
+ }
}
- $extensions = array("jpg", "jpeg", "gif", "png");
+ $extensions = array('jpg', 'jpeg', 'gif', 'png');
+ if (in_array($extension, $extensions)) {
+ $shortpath .= '.' . $extension;
+ $longpath .= '.' . $extension;
+ }
- if (in_array($extension, $extensions))
- $path .= ".".$extension;
+ $proxypath = App::get_baseurl() . '/proxy/' . $longpath;
- $proxypath = $a->get_baseurl()."/proxy/".$path;
+ if ($size != '') {
+ $size = ':' . $size;
+ }
// Too long files aren't supported by Apache
// 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);
+ if ((strlen($proxypath) > 250) AND $writemode) {
+ return $shortpath;
+ } elseif (strlen($proxypath) > 250) {
+ return App::get_baseurl() . '/proxy/' . $shortpath . '?url=' . urlencode($url);
+ } elseif ($writemode) {
+ return $longpath;
+ } else {
+ return $proxypath . $size;
+ }
}
/**
function proxy_parse_html($html) {
$a = get_app();
- $html = str_replace(normalise_link($a->get_baseurl())."/", $a->get_baseurl()."/", $html);
+ $html = str_replace(normalise_link(App::get_baseurl())."/", App::get_baseurl()."/", $html);
return preg_replace_callback("/(<img [^>]*src *= *[\"'])([^\"']+)([\"'][^>]*>)/siU", "proxy_img_cb", $html);
}