forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.inc
398 lines (299 loc) · 9.54 KB
/
plot.inc
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
$version = "1.0";
// check for bad agents immidietly
$blockbadagents=1;
if($blockbadagents == 1) {
// those metaquery assholes at t-dialin and others can't
// get another dumber using the default user-agent, can they?
$agent = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
if(
strstr("libwww-perl", $agent) ||
strstr("lwp-trivial", $agent) ||
strstr("LWP::Simple", $agent) ||
strstr("PHP/", $agent)) {
// goodbye
exit;
}
}
function s10_rfc_1918_ip($in) {
if (ereg('^(127\.)', $in, $part)) {
return TRUE;
}
if (ereg('^(10\.)', $in, $part)) {
return TRUE;
}
if (ereg('^(192\.168\.)', $in, $part)) {
return TRUE;
}
if (ereg('^(172\.)' . '([0-9]{1,3})', $in, $part)) {
if(($part[2] > 15) and ($part[2] < 32)) {
return TRUE;
}
}
return FALSE;
}
function stuffanalyze($stuff) {
$stuff2[0] = array_values(preg_grep ("/CITY:/", $stuff)) or die("Sorry, but the lookup for this IP address failed! (CITY)");
$stuff2[1] = array_values(preg_grep ("/STATE:/", $stuff)) or die("Sorry, but the lookup for this IP address failed! (STATE)");
$stuff2[2] = array_values(preg_grep ("/COUNTRY:/", $stuff)) or die("Sorry, but the lookup for this IP address failed! (COUNTRY)");
$stuff2[3] = array_values(preg_grep ("/LAT:/", $stuff)) or die("Sorry, but the lookup for this IP address failed! (LAT)");
$stuff2[4] = array_values(preg_grep ("/LONG:/", $stuff)) or die("Sorry, but the lookup for this IP address failed! (LONG)");
// all the stuff2 values are actually arrays, making the following code look like crap
// the power of $count
if(isset($stuff2[0][0])) {
for ($count = 0; $count < count($stuff2); $count++) {
$stuff2[$count] = $stuff2[$count][0];
$stuff2[$count] = ucwords(strtolower(trim(substr(substr($stuff2[$count], 15), 0, -5))));
}
} else {
// if no data was found
$stuff2 = array("bad", "bad", "bad", "bad", "bad");
}
$count = 0;
return $stuff2;
}
function getlatdata($ip) {
global $firewall_host;
global $firewall_port;
global $use_firewall;
$data = array("");
if ($use_firewall) {
$fp = fsockopen ($firewall_host, $firewall_port, $errno, $errstr, 30) or die("Could not open socket to proxy");
if (!$fp) {
echo "$errstr ($errno)<br/>\n";
} else {
fputs ($fp, "GET http://netgeo.caida.org/perl/netgeo.cgi?target=$ip HTTP/1.0\r\nHost: netgeo.caida.org\r\n\r\n") or die("Could not write to socket");
while (!feof($fp)) {
$packet = fgets ($fp,128) or die("Could not read from socket");
array_push($data, $packet) or die("Could not push data into array");
}
fclose ($fp);
}
} else {
$fp = fsockopen ("netgeo.caida.org", 80, $errno, $errstr, 30) or die("Could not open socket to caida.org");
if (!$fp) {
echo "$errstr ($errno)<br/>\n";
} else {
fputs ($fp, "GET /perl/netgeo.cgi?target=$ip HTTP/1.0\r\nHost: netgeo.caida.org\r\n\r\n") or die("Could not write to socket");
while (!feof($fp)) {
$packet = fgets ($fp,128) or die("Could not read from socket");
array_push($data, $packet) or die("Could not push socket data into array");
}
fclose ($fp);
}
}
// make it an array
return $data;
}
function finddot($name, $cssdots, $defaultdot) {
foreach($cssdots as $x) {
list($filename, , $width, $height) = split(":", $x);
if($filename == $name) { $found = 1; $return = array($name, $width, $height); }
}
if(isset($found)) {
return $return;
} else {
$dott = $cssdots[$defaultdot];
list($dott, , $width, $height) = split(":", $dott);
return array($dott, $width, $height);
}
}
function getlocationcoords($lat, $lon, $width, $height) {
// some cartographers weren't mathematicians, up is apparently negative to them
$lat = ($lat * -1);
$lat = ($lat + 90);
$lon = ($lon + 180);
$x = ($lon * ($width / 360));
$y = ($lat * ($height / 180));
$x = round($x);
$y = round($y);
return array($x, $y);
}
function getimagecoords($earthimages, $image) {
foreach($earthimages as $x) {
list($file, , $width, $height) = split(":", $x);
if($file == $image) {
$coords = array("$width", "$height");
return $coords;
}
}
}
function validcookie($cookie) {
if(preg_match("/.*.:.*.:.*.:.*.:.*./", $cookie)) {
return TRUE;
} else {
return FALSE;
}
}
function isvalidimage($cookie, $earthimages, $defaultimage) {
# list(, , , $setearthimage) = split(":", $cookie);
$setearthimage = $cookie;
if(isset($setearthimage)) {
// check if the image is one actually defined, not a cookie edit
foreach($earthimages as $image) {
list($testearthimage, , , ) = split(":", $image);
if($testearthimage == $setearthimage) {
$found = 1;
}
}
if(isset($found)) {
return $setearthimage;
} else {
return $earthimages[$defaultimage];
}
} else {
return $earthimages[$defaultimage];
}
}
function istheregd() {
global $trygd;
if($trygd == 1) {
if (@ImageTypes() & IMG_PNG) {
return TRUE;
} else {
return FALSE;
}
} else {
return FALSE;
}
}
function checkbrowser($agent) {
if (strstr($agent, "Mozilla/4.7") || strstr($agent, "Opera 6") || strstr($agent, "Opera/6")) {
return FALSE;
} else {
return TRUE;
}
}
function shouldrun($agent) {
// could cause probs... dunno
global $HTTP_COOKIE_VARS;
// check for new format of cookie with 6 parameters
if(@preg_match("/.*.:.*.:.*.:.*.:.*.:.*./", $HTTP_COOKIE_VARS["atlasprefs"])) {
list( , , , , , $drawmode) = split(":", $HTTP_COOKIE_VARS["atlasprefs"]);
} else {
$drawmode = "";
}
// don't try to understand below, it figures out whether
// to run it in css or gd based of prefs, server ability, and user agent.
if(istheregd() && ($drawmode == "1")) {
return TRUE;
} elseif($drawmode == "0") {
return FALSE;
} elseif(checkbrowser($agent) && istheregd()) {
return TRUE;
} elseif(!checkbrowser($agent)) {
return FALSE;
} elseif (!istheregd()) {
return FALSE;
}
}
function s10_validate_ip($in) {
if (is_string($in) && ereg('^([0-9]{1,3})\.([0-9]{1,3})\.' .
'([0-9]{1,3})\.([0-9]{1,3})$' ,
$in, $part)) {
if ($part[1] <= 255 && $part[2] <= 255 &&
$part[3] <= 255 && $part[4] <= 255)
return TRUE;
}
return FALSE;
}
function getstuff($address, $local) {
$address = trim($address);
# some people still think that urls are hostnames
$address = str_replace("http://", "", $address);
$address = preg_replace("/\/.*$/", "", $address);
// Security checks
$address = escapeshellcmd($address);
$values = array();
$values["address"] = $address;
if(eregi("[a-z]", $address)){
$ipaddress = gethostbyname($address);
if($ipaddress == $address) {
$values["validity"] = "no";
}
$values["hostname"] = $address;
$values["ishost"] = "yes";
} else {
if(s10_validate_ip($address)) {
$ipaddress = $address;
$values["hostname"] = "";
} else {
$values["validity"] = "no";
}
}
if(!isset($values["validity"])) {
if(s10_rfc_1918_ip($ipaddress)) {
$private = "yes";
} else {
$private = "no";
}
} else {
$private = "no";
}
if(!isset($ipaddress)) { $ipaddress = ""; }
$values["ipaddress"] = $ipaddress;
if(!isset($values["validity"]) && ($private == "no")) {
$stuff = getlatdata($ipaddress);
list(
$values["city"],
$values["state"],
$values["country"],
$values["lat"],
$values["lon"]
) = stuffanalyze($stuff);
$desc = "";
// check if it is the user's ip address
if($local == 1) { $desc .= t("You at"); }
// add the ip address and hostname
$desc .= "<b>$values[hostname]</b> (<b>$values[ipaddress]</b>) ";
// use "are" if it is the user's ip address
if($local == 1) { $desc .= t("are"); } else { $desc .= t("is"); }
$desc .= " ".t("located in")." ";
// add the city if it's there
if($values["city"]) {
$desc .= "$values[city], ";
}
// add the state if its there
if($values["state"]) {
$desc .= "$values[state], ";
}
if($values["country"]) {
// make the country code capital so its ready for lookup
$values["country"] = strtoupper($values["country"]);
// convert the country code to a country name
$countries = file("countries.txt") or die("Could not open countries file");
$precountry = array_values(preg_grep("/$values[country] /", $countries));
$values["country"] = trim(substr($precountry[0], 4));
$desc .= "$values[country].";
}
$desc .= " <font color=\"#aaaaaa\">($values[lat], $values[lon])</font>";
$state = $values["state"];
$city = $values["city"];
// decide if address can't resolve, be located, or if it's fine
if($values["lat"] == "0.00" && $values["lon"] == "0.00" && (@($state != "bad" && $city != "bad"))) {
$values["desc"] = "<b>$values[address]</b> ".t("cannot be located.");
$values["lat"] = "";
$values["lon"] = "";
} elseif($values["lat"] == "" && $values["lon"] == "") {
$values["desc"] = "<b>$values[address]</b> ".t("cannot be located.");
} else {
$values["desc"] = $desc;
}
} else {
// check if it was a host before and decide on an error message
if($private == "yes") {
$values["desc"] = "<b>$address</b> ".t("is a host in the private IP address range.");
} elseif(@($values["state"] == "bad" && $values["city"] == "bad")) {
$values["desc"] = t("Temporary lookup failure.");
} elseif(isset($values["ishost"])) {
$values["desc"] = "<b>$address</b> ".t("does not resolve.");
} else {
$values["desc"] = "<b>$address</b> ".t("is not a valid IP address.");
}
// some blank lat/lon for the image script or it will plot us in the center
$values["lon"] = "";
$values["lat"] = "";
}
return($values);
}
?>