9a17a3386c541fe4483729a57352c2a1a622f585
[surrogator.git] / www / avatar.php
1 <?php
2 namespace surrogator;
3
4 $cfgFile = __DIR__ . '/../data/surrogator.config.php';
5 if (!file_exists($cfgFile)) {
6     $cfgFile = '/etc/surrogator.config.php';
7     if (!file_exists($cfgFile)) {
8         err(
9             500,
10             "Configuration file does not exist.\n"
11             . "Copy data/surrogator.config.php.dist to data/surrogator.config.php"
12         );
13         exit(2);
14     }
15 }
16 require $cfgFile;
17
18 function err($statusCode, $msg)
19 {
20     header('HTTP/1.0 ' . $statusCode . ' ' . $msg);
21     header('Content-Type: text/plain');
22     echo $msg . "\n";
23     exit(1);
24 }
25
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');
30 }
31 $reqHash = $uriParts[2];
32 if (strpos($reqHash, '?') !== false) {
33     $reqHash = substr($reqHash, 0, strpos($reqHash, '?'));
34 }
35 if (strlen($reqHash) !== 32 && strlen($reqHash) !== 64) {
36     err(400, 'Hash has to be 32 or 64 characters long');
37 }
38
39 $reqSize = 80;//default
40 if (isset($_GET['s'])) {
41     $_GET['size'] = $_GET['s'];
42 }
43 if (isset($_GET['size'])) {
44     if ($_GET['size'] != intval($_GET['size'])) {
45         err(400, 'size parameter is not an integer');
46     }
47     if ($_GET['size'] < 1) {
48         err(400, 'size parameter has to be larger than 0');
49     }
50     $reqSize = intval($_GET['size']);
51 }
52
53 $default     = 'default.png';
54 $defaultMode = 'local';
55 if (isset($_GET['d'])) {
56     $_GET['default'] = $_GET['d'];
57 }
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') {
64             $defaultMode = '404';
65             $default     = '404';
66         } else {
67             //FIXME: support mm
68             //local default image
69             $defaultMode = 'local';
70             $default     = 'default.png';
71         }
72     } else {
73         //url
74         $defaultMode = 'redirect';
75         $default     = $_GET['default'];
76         //FIXME: validate?
77     }
78 }
79
80
81 $targetSize = 512;
82 foreach ($sizes as $size) {
83     if ($reqSize <= $size) {
84         $targetSize = $size;
85         break;
86     }
87 }
88
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);
95         exit();
96     } else if ($defaultMode == 'local') {
97         $imgFile = $varDir . $targetSize . '/' . $default;
98         if (!file_exists($imgFile)) {
99             err(500, 'Default file is missing');
100         }
101     } else {
102         err(500, 'Invalid defaultMode');
103     }
104 }
105
106 $stat = stat($imgFile);
107 $etag = sprintf('%x-%x-%x', $stat['ino'], $stat['size'], $stat['mtime'] * 1000000);
108
109 if (isset($_SERVER['HTTP_IF_NONE_MATCH'])
110     && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag
111 ) {
112     header('Etag: "' . $etag . '"');
113     header('HTTP/1.0 304 Not Modified');
114     exit();
115 } else if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
116     && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $stat['mtime']
117 ) {
118     header('Last-Modified: ' . date('r', $stat['mtime']));
119     header('HTTP/1.0 304 Not Modified');
120     exit();
121 }
122
123 header('Last-Modified: ' . date('r', $stat['mtime']));
124 header('Etag: "' . $etag . '"');
125 header('Content-Type: image/png');
126 header('Content-Length:' . $stat['size']);
127
128 readfile($imgFile);
129 ?>