3 * Script that handles avatar image requests.
5 * Part of Surrogator - a simple libravatar avatar image server.
11 * @author Christian Weiske <cweiske@cweiske.de>
12 * @license http://www.gnu.org/licenses/agpl.html AGPLv3 or later
13 * @link http://git.cweiske.de/?p=surrogator.git
16 $cfgFile = __DIR__ . '/../data/surrogator.config.php';
17 if (!file_exists($cfgFile)) {
18 $cfgFile = '/etc/surrogator.config.php';
19 if (!file_exists($cfgFile)) {
22 "Configuration file does not exist.\n"
23 . "Copy data/surrogator.config.php.dist to data/surrogator.config.php"
31 * Send an error message out.
33 * @param integer $statusCode HTTP status code
34 * @param string $msg Error message
38 function err($statusCode, $msg)
40 header('HTTP/1.0 ' . $statusCode . ' ' . $msg);
41 header('Content-Type: text/plain');
46 $uri = $_SERVER['REQUEST_URI'];
47 $uriParts = explode('/', $uri, 3);
48 if (count($uriParts) != 3 || $uriParts[1] != 'avatar') {
49 err(400, 'URI is wrong, should be avatar/$hash');
51 $reqHash = $uriParts[2];
52 if (strpos($reqHash, '?') !== false) {
53 $reqHash = substr($reqHash, 0, strpos($reqHash, '?'));
55 if (strlen($reqHash) !== 32 && strlen($reqHash) !== 64) {
56 err(400, 'Hash has to be 32 or 64 characters long');
59 $reqSize = 80;//default
60 if (isset($_GET['s'])) {
61 $_GET['size'] = $_GET['s'];
63 if (isset($_GET['size'])) {
64 if ($_GET['size'] != intval($_GET['size'])) {
65 err(400, 'size parameter is not an integer');
67 if ($_GET['size'] < 1) {
68 err(400, 'size parameter has to be larger than 0');
70 $reqSize = intval($_GET['size']);
73 $default = 'default.png';
74 $defaultMode = 'local';
75 if (isset($_GET['d'])) {
76 $_GET['default'] = $_GET['d'];
78 if (isset($_GET['default'])) {
79 if ($_GET['default'] == '') {
80 err(400, 'default parameter is empty');
81 } else if (preg_match('#^[a-z0-9]+$#', $_GET['default'])) {
82 //special default mode, we support none of them except 404
83 if ($_GET['default'] == '404') {
89 $defaultMode = 'local';
90 $default = 'default.png';
94 $defaultMode = 'redirect';
95 $default = $_GET['default'];
102 foreach ($sizes as $size) {
103 if ($reqSize <= $size) {
109 $imgFile = $varDir . $targetSize . '/' . $reqHash . '.png';
110 if (!file_exists($imgFile)) {
111 if ($defaultMode == '404') {
112 err(404, 'File does not exist');
113 } else if ($defaultMode == 'redirect') {
114 header('Location: ' . $default);
116 } else if ($defaultMode == 'local') {
117 $imgFile = $varDir . $targetSize . '/' . $default;
118 if (!file_exists($imgFile)) {
119 err(500, 'Default file is missing');
122 err(500, 'Invalid defaultMode');
126 $stat = stat($imgFile);
127 $etag = sprintf('%x-%x-%x', $stat['ino'], $stat['size'], $stat['mtime'] * 1000000);
129 if (isset($_SERVER['HTTP_IF_NONE_MATCH'])
130 && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag
132 header('Etag: "' . $etag . '"');
133 header('HTTP/1.0 304 Not Modified');
135 } else if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
136 && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $stat['mtime']
138 header('Last-Modified: ' . date('r', $stat['mtime']));
139 header('HTTP/1.0 304 Not Modified');
143 header('Last-Modified: ' . date('r', $stat['mtime']));
144 header('Etag: "' . $etag . '"');
145 header('Content-Type: image/png');
146 header('Content-Length:' . $stat['size']);