avatar serving works now
authorChristian Weiske <cweiske@cweiske.de>
Thu, 16 Aug 2012 20:28:53 +0000 (22:28 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 16 Aug 2012 20:28:53 +0000 (22:28 +0200)
.gitignore
data/surrogator.config.php [deleted file]
data/surrogator.config.php.dist
www/.htaccess [new file with mode: 0644]
www/avatar.php [new file with mode: 0644]

index 45645377e4638cd78ff7616ab271b92408bde234..76144a9d0415a195c5bbecea04828cf640efede4 100644 (file)
@@ -1,2 +1,3 @@
 var/*
 raw/*
 var/*
 raw/*
+data/surrogator.config.php
diff --git a/data/surrogator.config.php b/data/surrogator.config.php
deleted file mode 100644 (file)
index 87efce0..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<?php
-$rawDir = __DIR__ . '/../raw/';
-$varDir = __DIR__ . '/../var/';
-$sizes   = array(16, 32, 48, 64, 80, 96, 128, 256, 512);
-$maxSize = 512;
-$logLevel = 1;//0 - nothing, 3 - high
-$forceUpdate = false;
-?>
index 421e057d3c1aaf33d4381801827526330a00805d..87efce0970d192e4f3a298a8212b17f1462702bb 100644 (file)
@@ -3,4 +3,6 @@ $rawDir = __DIR__ . '/../raw/';
 $varDir = __DIR__ . '/../var/';
 $sizes   = array(16, 32, 48, 64, 80, 96, 128, 256, 512);
 $maxSize = 512;
 $varDir = __DIR__ . '/../var/';
 $sizes   = array(16, 32, 48, 64, 80, 96, 128, 256, 512);
 $maxSize = 512;
+$logLevel = 1;//0 - nothing, 3 - high
+$forceUpdate = false;
 ?>
 ?>
diff --git a/www/.htaccess b/www/.htaccess
new file mode 100644 (file)
index 0000000..4b6a45e
--- /dev/null
@@ -0,0 +1,3 @@
+RewriteEngine On
+RewriteBase /
+RewriteRule ^avatar/ avatar.php [L]
diff --git a/www/avatar.php b/www/avatar.php
new file mode 100644 (file)
index 0000000..9a17a33
--- /dev/null
@@ -0,0 +1,129 @@
+<?php
+namespace surrogator;
+
+$cfgFile = __DIR__ . '/../data/surrogator.config.php';
+if (!file_exists($cfgFile)) {
+    $cfgFile = '/etc/surrogator.config.php';
+    if (!file_exists($cfgFile)) {
+        err(
+            500,
+            "Configuration file does not exist.\n"
+            . "Copy data/surrogator.config.php.dist to data/surrogator.config.php"
+        );
+        exit(2);
+    }
+}
+require $cfgFile;
+
+function err($statusCode, $msg)
+{
+    header('HTTP/1.0 ' . $statusCode . ' ' . $msg);
+    header('Content-Type: text/plain');
+    echo $msg . "\n";
+    exit(1);
+}
+
+$uri = $_SERVER['REQUEST_URI'];
+$uriParts = explode('/', $uri, 3);
+if (count($uriParts) != 3 || $uriParts[1] != 'avatar') {
+    err(400, 'URI is wrong, should be avatar/$hash');
+}
+$reqHash = $uriParts[2];
+if (strpos($reqHash, '?') !== false) {
+    $reqHash = substr($reqHash, 0, strpos($reqHash, '?'));
+}
+if (strlen($reqHash) !== 32 && strlen($reqHash) !== 64) {
+    err(400, 'Hash has to be 32 or 64 characters long');
+}
+
+$reqSize = 80;//default
+if (isset($_GET['s'])) {
+    $_GET['size'] = $_GET['s'];
+}
+if (isset($_GET['size'])) {
+    if ($_GET['size'] != intval($_GET['size'])) {
+        err(400, 'size parameter is not an integer');
+    }
+    if ($_GET['size'] < 1) {
+        err(400, 'size parameter has to be larger than 0');
+    }
+    $reqSize = intval($_GET['size']);
+}
+
+$default     = 'default.png';
+$defaultMode = 'local';
+if (isset($_GET['d'])) {
+    $_GET['default'] = $_GET['d'];
+}
+if (isset($_GET['default'])) {
+    if ($_GET['default'] == '') {
+        err(400, 'default parameter is empty');
+    } else if (preg_match('#^[a-z0-9]+$#', $_GET['default'])) {
+        //special default mode, we support none of them except 404
+        if ($_GET['default'] == '404') {
+            $defaultMode = '404';
+            $default     = '404';
+        } else {
+            //FIXME: support mm
+            //local default image
+            $defaultMode = 'local';
+            $default     = 'default.png';
+        }
+    } else {
+        //url
+        $defaultMode = 'redirect';
+        $default     = $_GET['default'];
+        //FIXME: validate?
+    }
+}
+
+
+$targetSize = 512;
+foreach ($sizes as $size) {
+    if ($reqSize <= $size) {
+        $targetSize = $size;
+        break;
+    }
+}
+
+$imgFile = $varDir . $targetSize . '/' . $reqHash . '.png';
+if (!file_exists($imgFile)) {
+    if ($defaultMode == '404') {
+        err(404, 'File does not exist');
+    } else if ($defaultMode == 'redirect') {
+        header('Location: ' . $default);
+        exit();
+    } else if ($defaultMode == 'local') {
+        $imgFile = $varDir . $targetSize . '/' . $default;
+        if (!file_exists($imgFile)) {
+            err(500, 'Default file is missing');
+        }
+    } else {
+        err(500, 'Invalid defaultMode');
+    }
+}
+
+$stat = stat($imgFile);
+$etag = sprintf('%x-%x-%x', $stat['ino'], $stat['size'], $stat['mtime'] * 1000000);
+
+if (isset($_SERVER['HTTP_IF_NONE_MATCH'])
+    && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag
+) {
+    header('Etag: "' . $etag . '"');
+    header('HTTP/1.0 304 Not Modified');
+    exit();
+} else if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
+    && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $stat['mtime']
+) {
+    header('Last-Modified: ' . date('r', $stat['mtime']));
+    header('HTTP/1.0 304 Not Modified');
+    exit();
+}
+
+header('Last-Modified: ' . date('r', $stat['mtime']));
+header('Etag: "' . $etag . '"');
+header('Content-Type: image/png');
+header('Content-Length:' . $stat['size']);
+
+readfile($imgFile);
+?>
\ No newline at end of file