4 $cfgFile = __DIR__ . '/../data/surrogator.config.php';
5 if (!file_exists($cfgFile)) {
6 $cfgFile = '/etc/surrogator.config.php';
7 if (!file_exists($cfgFile)) {
10 "Configuration file does not exist.\n"
11 . "Copy data/surrogator.config.php.dist to data/surrogator.config.php"
18 function err($statusCode, $msg)
20 header('HTTP/1.0 ' . $statusCode . ' ' . $msg);
21 header('Content-Type: text/plain');
26 $uri = $_SERVER['REQUEST_URI'];
27 $uriParts = explode('/', $uri, 3);
28 if (count($uriParts) != 3 || $uriParts[1] != 'avatar') {
29 err(400, 'URI is wrong, should be avatar/$hash');
31 $reqHash = $uriParts[2];
32 if (strpos($reqHash, '?') !== false) {
33 $reqHash = substr($reqHash, 0, strpos($reqHash, '?'));
35 if (strlen($reqHash) !== 32 && strlen($reqHash) !== 64) {
36 err(400, 'Hash has to be 32 or 64 characters long');
39 $reqSize = 80;//default
40 if (isset($_GET['s'])) {
41 $_GET['size'] = $_GET['s'];
43 if (isset($_GET['size'])) {
44 if ($_GET['size'] != intval($_GET['size'])) {
45 err(400, 'size parameter is not an integer');
47 if ($_GET['size'] < 1) {
48 err(400, 'size parameter has to be larger than 0');
50 $reqSize = intval($_GET['size']);
53 $default = 'default.png';
54 $defaultMode = 'local';
55 if (isset($_GET['d'])) {
56 $_GET['default'] = $_GET['d'];
58 if (isset($_GET['default'])) {
59 if ($_GET['default'] == '') {
60 err(400, 'default parameter is empty');
61 } else if (preg_match('#^[a-z0-9]+$#', $_GET['default'])) {
62 //special default mode, we support none of them except 404
63 if ($_GET['default'] == '404') {
69 $defaultMode = 'local';
70 $default = 'default.png';
74 $defaultMode = 'redirect';
75 $default = $_GET['default'];
82 foreach ($sizes as $size) {
83 if ($reqSize <= $size) {
89 $imgFile = $varDir . $targetSize . '/' . $reqHash . '.png';
90 if (!file_exists($imgFile)) {
91 if ($defaultMode == '404') {
92 err(404, 'File does not exist');
93 } else if ($defaultMode == 'redirect') {
94 header('Location: ' . $default);
96 } else if ($defaultMode == 'local') {
97 $imgFile = $varDir . $targetSize . '/' . $default;
98 if (!file_exists($imgFile)) {
99 err(500, 'Default file is missing');
102 err(500, 'Invalid defaultMode');
106 $stat = stat($imgFile);
107 $etag = sprintf('%x-%x-%x', $stat['ino'], $stat['size'], $stat['mtime'] * 1000000);
109 if (isset($_SERVER['HTTP_IF_NONE_MATCH'])
110 && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag
112 header('Etag: "' . $etag . '"');
113 header('HTTP/1.0 304 Not Modified');
115 } else if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
116 && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $stat['mtime']
118 header('Last-Modified: ' . date('r', $stat['mtime']));
119 header('HTTP/1.0 304 Not Modified');
123 header('Last-Modified: ' . date('r', $stat['mtime']));
124 header('Etag: "' . $etag . '"');
125 header('Content-Type: image/png');
126 header('Content-Length:' . $stat['size']);