-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
173 lines (166 loc) · 4.58 KB
/
index.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
if (getenv("PROTOCOL") && getenv("PROTOCOL") != "") {
$proto = getenv("PROTOCOL");
}
else {
$proto = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http");
}
$host = $proto."://$_SERVER[HTTP_HOST]/";
$uri = $_SERVER['REQUEST_URI'];
$base = explode('/', trim($uri, " /"))[0];
# If the animal folder doesn't exist, return a nice 404 page
if (!(file_exists("./animals/$base") && is_dir("./animals/$base")))
{
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404);
include('404.php');
die();
}
# Root url
if ($uri == "/"){
$dog_count = count(glob("./animals/dogs/*"));
$cat_count = count(glob("./animals/cats/*"));
$wolf_count = count(glob("./animals/wolves/*"));
$possum_count = count(glob("./animals/possums/*"));
$raccoon_count = count(glob("./animals/raccoons/*"));
$bird_count = count(glob("./animals/birds/*"));
$baloo_count = count(glob("./animals/baloo/*"));
$total_count = $dog_count + $cat_count + $wolf_count + $possum_count + $raccoon_count + $bird_count + $baloo_count;
echo <<<EOT
<html lang='en'>
<head>
<title>Random Animals!</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Galen Guyer">
<meta name="description" content="Get a bunch of random animal pics!" />
<style>
body {
font-family: Arial, Helvetica, sans-serif;
text-align: center;
}
img {
max-width: 90vw;
max-height: 70vh;
}
</style>
</head>
<body>
<header>
<h1>Hey look! It's a bunch of random animals!</h1>
</header>
<h3>Currently serving:</h3>
<p><a href="/baloo/">$baloo_count pics of my dog Baloo!</a></p>
<p><a href="/dogs/">$dog_count dogs!</a></p>
<p><a href="/cats/">$cat_count cats!</a></p>
<p><a href="/wolves/">$wolf_count wolves!</a></p>
<p><a href="/possums/">$possum_count possums!</a></p>
<p><a href="/raccoons/">$raccoon_count raccoons!</a></p>
<p><a href="/birds/">$bird_count birds!</a></p>
<p>$total_count total pictures!</p>
<script data-goatcounter="https://randomanimals.goatcounter.com/count"
async src="//gc.zgo.at/count.js"></script>
</body>
</html>
EOT;
die();
}
# Raw endpoint
else if (trim($uri, "/") == ($base . "/raw")) {
$files = glob("./animals/$base/*");
$file = str_replace(" ", "%20", substr($files[array_rand($files)], 2));
header("Content-Type: text/plain");
echo $host.$file;
die();
}
# JSON endpoint
else if (trim($uri, "/") == ($base . "/json")) {
$files = glob("./animals/$base/*");
$file = str_replace(" ", "%20", substr($files[array_rand($files)], 2));
header("Content-Type: application/json");
echo "{\"link\": \"$host$file\"}";
die();
}
# Pretty endpoint
else if (trim($uri, "/") == ($base)) {
$files = glob("./animals/$base/*");
$file = str_replace(" ", "%20", substr($files[array_rand($files)], 2));
$singulars = [
"dogs" => "dog",
"cats" => "cat",
"possums" => "possum",
"raccoons" => "raccoon",
"birds" => "bird",
"wolves" => "wolf",
"baloo" => "baloo"
];
$singular = $singulars[$base];
$usingular = ucwords($singular);
header("Content-Type: text/html");
echo <<< EOT
<html lang='en'>
<head>
<title>Random $usingular!</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Galen Guyer">
<meta name="description" content="Get a random $singular!" />
<style>
html, body {
margin: 0px;
padding: 0px;
height: 100%;
}
h1 {
margin-top: 0px;
padding-top: 12px;
}
#container {
font-family: Arial, Helvetica, sans-serif;
text-align: center;
min-height: 100%;
position: relative;
}
#main {
padding-bottom: 32px;
}
img {
max-width: 90vw;
max-height: 70vh;
}
#footer {
position: absolute;
bottom: 0;
width: 100%;
height: 32px;
text-align: center;
color: #bebebe;
}
#footer a {
color: #bebebe;
text-decoration: none;
}
</style>
</head>
<body>
<div id="container">
<div id="header">
<h1>Hey look! It's a random $singular!</h1>
</div>
<div id="main">
<img src="$host$file"></img>
<p>Permalink: <a href="$host$file">$host$file</a></p>
</div>
<div id="footer">
<a href="/$base/raw">/raw</a> | <a href="/$base/json">/json</a> | <a href="https://github.com/galenguyer/UniversalAnimalApi">source</a>
</div>
<script data-goatcounter="https://randomanimals.goatcounter.com/count"
async src="//gc.zgo.at/count.js"></script>
</body>
</html>
EOT;
die();
}
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404);
include('404.php');
die();
?>