|
@@ -0,0 +1,70 @@
|
|
1
|
+<?php
|
|
2
|
+
|
|
3
|
+// Location where hosted images will be stored
|
|
4
|
+define('IMG_ROOT', '/var/images/');
|
|
5
|
+// Pre-shared key - must match IMAGE_PSK in gazelle config
|
|
6
|
+define('PSK', 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
|
|
7
|
+
|
|
8
|
+ini_set('memory_limit', '256M');
|
|
9
|
+
|
|
10
|
+function image_type($Data) {
|
|
11
|
+ if (!strncmp($Data, pack('H*', '89504E47'), 4)) return 'png';
|
|
12
|
+ if (!strncmp($Data, pack('H*', 'FFD8'), 2)) return 'jpeg';
|
|
13
|
+ if (!strncmp($Data, 'GIF', 3)) return 'gif';
|
|
14
|
+ if (strlen($Data) > 35 && !substr_compare($Data, 'webm', 31, 4)) return 'webm';
|
|
15
|
+ if (!strncmp($Data, 'BM', 2)) return 'bmp';
|
|
16
|
+ if (!strncmp($Data, 'II', 2) || !strncmp($Data, 'MM', 2)) return 'tiff';
|
|
17
|
+ return '';
|
|
18
|
+}
|
|
19
|
+
|
|
20
|
+$ImgURL = $_GET['i'];
|
|
21
|
+$Auth = rawurldecode($_GET['h']);
|
|
22
|
+$ImgURLHash = hash('sha256', $ImgURL);
|
|
23
|
+
|
|
24
|
+// Deletion
|
|
25
|
+if (!empty($_GET['d'])) {
|
|
26
|
+ $ImgURL = $_GET['d'];
|
|
27
|
+ $ImgURLHash = hash('sha256', $ImgURL);
|
|
28
|
+ if (base64_encode(hash_hmac('sha256', $ImgURL, strrev(PSK), true)) != $Auth) {
|
|
29
|
+ echo 'Auth failure';
|
|
30
|
+ die();
|
|
31
|
+ }
|
|
32
|
+ if (file_exists(IMG_ROOT.substr($ImgURLHash,0,2).'/'.$ImgURLHash)) {
|
|
33
|
+ unlink(IMG_ROOT.substr($ImgURLHash,0,2).'/'.$ImgURLHash);
|
|
34
|
+ echo 'Success';
|
|
35
|
+ } else {
|
|
36
|
+ echo 'File does not exist';
|
|
37
|
+ }
|
|
38
|
+ die();
|
|
39
|
+}
|
|
40
|
+
|
|
41
|
+// Normal use
|
|
42
|
+if (base64_encode(hash_hmac('sha256', $ImgURL, PSK, true)) != $Auth) {
|
|
43
|
+ // Authentication is incorrect. Something other than the paired Gazelle instance is attempting to use the host.
|
|
44
|
+ header('Content-type: image/png');
|
|
45
|
+ echo file_get_contents('imgs/unauthorized.png');
|
|
46
|
+ die();
|
|
47
|
+}
|
|
48
|
+if (file_exists(IMG_ROOT.substr($ImgURLHash,0,2).'/'.$ImgURLHash)) {
|
|
49
|
+ // The file is cached. Serve it.
|
|
50
|
+ $Img = file_get_contents(IMG_ROOT.substr($ImgURLHash,0,2).'/'.$ImgURLHash);
|
|
51
|
+ $FileType = image_type($Img);
|
|
52
|
+ header('Content-type: '.(($FileType=='webm')?'video':'image').'/'.$FileType);
|
|
53
|
+ echo $Img;
|
|
54
|
+} else {
|
|
55
|
+ // The file is not cached. Fetch it, cache it, and serve it.
|
|
56
|
+ $Img = @file_get_contents($ImgURL, 0, stream_context_create(['http' => ['user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.2987.133 Safari/537.36'], 'ssl' => ['verify_peer' => false]]), 0, 134217728);
|
|
57
|
+ $FileType = image_type($Img);
|
|
58
|
+ if (!empty($FileType)) {
|
|
59
|
+ if (!file_exists(IMG_ROOT.substr($ImgURLHash,0,2))) {
|
|
60
|
+ mkdir(IMG_ROOT.substr($ImgURLHash,0,2));
|
|
61
|
+ }
|
|
62
|
+ file_put_contents(IMG_ROOT.substr($ImgURLHash,0,2).'/'.$ImgURLHash, $Img);
|
|
63
|
+ header('Content-type: '.(($FileType=='webm')?'video':'image').'/'.$FileType);
|
|
64
|
+ echo $Img;
|
|
65
|
+ } else {
|
|
66
|
+ header('Content-type: image/png');
|
|
67
|
+ echo file_get_contents('imgs/invalid.png');
|
|
68
|
+ }
|
|
69
|
+}
|
|
70
|
+?>
|