add license file, docblocks and update readme
[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     http://git.cweiske.de/?p=surrogator.git
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 {
87             //FIXME: support mm
88             //local default image
89             $defaultMode = 'local';
90             $default     = 'default.png';
91         }
92     } else {
93         //url
94         $defaultMode = 'redirect';
95         $default     = $_GET['default'];
96         //FIXME: validate?
97     }
98 }
99
100
101 $targetSize = 512;
102 foreach ($sizes as $size) {
103     if ($reqSize <= $size) {
104         $targetSize = $size;
105         break;
106     }
107 }
108
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);
115         exit();
116     } else if ($defaultMode == 'local') {
117         $imgFile = $varDir . $targetSize . '/' . $default;
118         if (!file_exists($imgFile)) {
119             err(500, 'Default file is missing');
120         }
121     } else {
122         err(500, 'Invalid defaultMode');
123     }
124 }
125
126 $stat = stat($imgFile);
127 $etag = sprintf('%x-%x-%x', $stat['ino'], $stat['size'], $stat['mtime'] * 1000000);
128
129 if (isset($_SERVER['HTTP_IF_NONE_MATCH'])
130     && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag
131 ) {
132     header('Etag: "' . $etag . '"');
133     header('HTTP/1.0 304 Not Modified');
134     exit();
135 } else if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
136     && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $stat['mtime']
137 ) {
138     header('Last-Modified: ' . date('r', $stat['mtime']));
139     header('HTTP/1.0 304 Not Modified');
140     exit();
141 }
142
143 header('Last-Modified: ' . date('r', $stat['mtime']));
144 header('Etag: "' . $etag . '"');
145 header('Content-Type: image/png');
146 header('Content-Length:' . $stat['size']);
147
148 readfile($imgFile);
149 ?>