This repository has been archived by the owner on Oct 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsteckbrief-random.php
89 lines (65 loc) · 1.75 KB
/
steckbrief-random.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
$imgDir = "./sync/steckbriefe";
$allowedIpRanges = array('10.26.180.0/24', '128.90.0.0/16', '195.145.75.0/24');
$defaultImg = "default.jpg";
/****************************/
$imgFormat = $_GET['format'];
if(!$imgFormat) $imgFormat = 'jpg';
$images = array();
$dir = opendir($imgDir);
$excluded = array(".", "..", $defaultImg);
while($img = readdir($dir)){
if(in_array($img, $excluded)) continue;
array_push($images, $img);
}
closedir($dir);
// default image when there are no other images
if(!count($images)) {
sendImage($defaultImg, $imgFormat, $imgDir);
exit();
}
// default image if IP is not in allowed ranges
if(!isInAnyRange($_SERVER["HTTP_X_FORWARDED_FOR"], $allowedIpRanges)) {
sendImage($defaultImg, $imgFormat, $imgDir);
exit();
}
// randomly select one image
$randomImage = $images[rand(0, count($images) - 1)];
sendImage($randomImage, $imgFormat, $imgDir);
exit();
function sendImage($imgFilename, $imgFormat, $imgDir) {
$image = imagecreatefromstring(file_get_contents($imgDir."/".$imgFilename));
// output image
switch($imgFormat) {
case 'jpg':
case 'jpeg':
header('Content-Type: image/jpeg');
imagejpeg($image);
return;
case 'png':
header('Content-Type: image/png');
imagepng($image);
return;
default:
return;
}
}
function isInRange($checkip, $range) {
@list($ip, $len) = explode('/', $range);
if (($min = ip2long($ip)) !== false && !is_null($len)) {
$clong = ip2long($checkip);
$max = ($min | (1<<(32-$len))-1);
if ($clong > $min && $clong < $max) {
return true;
} else {
return false;
}
}
}
function isInAnyRange($checkip, $ranges) {
foreach($ranges as $range) {
if(isInRange($checkip, $range)) return true;
}
return false;
}
?>