copy index.surrogator.html to index.html if it does not exist
[surrogator.git] / www / avatar.php
1 <?php
2 /**
3  * Script that handles avatar image requests.
4  *
5  * Part of Surrogator - a simple libravatar avatar image server.
6  *
7  * PHP version 5
8  *
9  * @category Tools
10  * @package  Surrogator
11  * @author   Christian Weiske <cweiske@cweiske.de>
12  * @license  http://www.gnu.org/licenses/agpl.html AGPLv3 or later
13  * @link     https://sourceforge.net/p/surrogator/
14  */
15 namespace surrogator;
16 $cfgFile = __DIR__ . '/../data/surrogator.config.php';
17 if (!file_exists($cfgFile)) {
18     $cfgFile = '/etc/surrogator.config.php';
19     if (!file_exists($cfgFile)) {
20         err(
21             500,
22             "Configuration file does not exist.\n"
23             . "Copy data/surrogator.config.php.dist to data/surrogator.config.php"
24         );
25         exit(2);
26     }
27 }
28 require $cfgFile;
29
30 /**
31  * Send an error message out.
32  *
33  * @param integer $statusCode HTTP status code
34  * @param string  $msg        Error message
35  *
36  * @return void
37  */
38 function err($statusCode, $msg)
39 {
40     header('HTTP/1.0 ' . $statusCode . ' ' . $msg);
41     header('Content-Type: text/plain');
42     echo $msg . "\n";
43     exit(1);
44 }
45
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');
50 }
51 $reqHash = $uriParts[2];
52 if (strpos($reqHash, '?') !== false) {
53     $reqHash = substr($reqHash, 0, strpos($reqHash, '?'));
54 }
55 if (strlen($reqHash) !== 32 && strlen($reqHash) !== 64) {
56     err(400, 'Hash has to be 32 or 64 characters long');
57 }
58
59 $reqSize = 80;//default
60 if (isset($_GET['s'])) {
61     $_GET['size'] = $_GET['s'];
62 }
63 if (isset($_GET['size'])) {
64     if ($_GET['size'] != intval($_GET['size'])) {
65         err(400, 'size parameter is not an integer');
66     }
67     if ($_GET['size'] < 1) {
68         err(400, 'size parameter has to be larger than 0');
69     }
70     $reqSize = intval($_GET['size']);
71 }
72
73 $default     = 'default.png';
74 $defaultMode = 'local';
75 if (isset($_GET['d'])) {
76     $_GET['default'] = $_GET['d'];
77 }
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') {
84             $defaultMode = '404';
85             $default     = '404';
86         } else if ($_GET['default'] == 'mm') {
87             //mystery man fallback image
88             $defaultMode = 'local';
89             $default     = 'mm.png';
90         } else {
91             //local default image
92             $defaultMode = 'local';
93             $default     = 'default.png';
94         }
95     } else {
96         //url
97         $defaultMode = 'redirect';
98         $default     = $_GET['default'];
99         //FIXME: validate?
100     }
101 }
102
103
104 $targetSize = 512;
105 foreach ($sizes as $size) {
106     if ($reqSize <= $size) {
107         $targetSize = $size;
108         break;
109     }
110 }
111
112 $imgFile = $varDir . $targetSize . '/' . $reqHash . '.png';
113 if (!file_exists($imgFile)) {
114     if ($defaultMode == '404') {
115         err(404, 'File does not exist');
116     } else if ($defaultMode == 'redirect') {
117         header('Location: ' . $default);
118         exit();
119     } else if ($defaultMode == 'local') {
120         $imgFile = $varDir . $targetSize . '/' . $default;
121         if (!file_exists($imgFile)) {
122             err(500, 'Default file is missing');
123         }
124     } else {
125         err(500, 'Invalid defaultMode');
126     }
127 }
128
129 $stat = stat($imgFile);
130 $etag = sprintf('%x-%x-%x', $stat['ino'], $stat['size'], $stat['mtime'] * 1000000);
131
132 if (isset($_SERVER['HTTP_IF_NONE_MATCH'])
133     && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag
134 ) {
135     header('Etag: "' . $etag . '"');
136     header('HTTP/1.0 304 Not Modified');
137     exit();
138 } else if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
139     && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $stat['mtime']
140 ) {
141     header('Last-Modified: ' . date('r', $stat['mtime']));
142     header('HTTP/1.0 304 Not Modified');
143     exit();
144 }
145
146 header('Last-Modified: ' . date('r', $stat['mtime']));
147 header('Etag: "' . $etag . '"');
148 header('Content-Type: image/png');
149 header('Content-Length:' . $stat['size']);
150
151 readfile($imgFile);
152 ?>