MDL-12789 - dont unnecessarily iterate over all groups
[moodle.git] / iplookup / ipatlas / plot.inc
CommitLineData
f4fb66d7 1<?php
2
3$version = "1.0";
4
5// check for bad agents immidietly
6$blockbadagents=1;
7if($blockbadagents == 1) {
8 // those metaquery assholes at t-dialin and others can't
9 // get another dumber using the default user-agent, can they?
10
11 $agent = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
12
13 if(
14 strstr("libwww-perl", $agent) ||
15 strstr("lwp-trivial", $agent) ||
16 strstr("LWP::Simple", $agent) ||
17 strstr("PHP/", $agent)) {
18
19 // goodbye
20 exit;
21
22 }
23
24}
25
26function s10_rfc_1918_ip($in) {
27
28 if (ereg('^(127\.)', $in, $part)) {
29 return TRUE;
30 }
31
32 if (ereg('^(10\.)', $in, $part)) {
33 return TRUE;
34 }
35
36 if (ereg('^(192\.168\.)', $in, $part)) {
37 return TRUE;
38 }
39
40 if (ereg('^(172\.)' . '([0-9]{1,3})', $in, $part)) {
41 if(($part[2] > 15) and ($part[2] < 32)) {
42 return TRUE;
43 }
44 }
45
46 return FALSE;
47
48}
49
50function stuffanalyze($stuff) {
51
52$stuff2[0] = array_values(preg_grep ("/CITY:/", $stuff)) or die("Sorry, but the lookup for this IP address failed! (CITY)");
53$stuff2[1] = array_values(preg_grep ("/STATE:/", $stuff)) or die("Sorry, but the lookup for this IP address failed! (STATE)");
54$stuff2[2] = array_values(preg_grep ("/COUNTRY:/", $stuff)) or die("Sorry, but the lookup for this IP address failed! (COUNTRY)");
55$stuff2[3] = array_values(preg_grep ("/LAT:/", $stuff)) or die("Sorry, but the lookup for this IP address failed! (LAT)");
56$stuff2[4] = array_values(preg_grep ("/LONG:/", $stuff)) or die("Sorry, but the lookup for this IP address failed! (LONG)");
57
58// all the stuff2 values are actually arrays, making the following code look like crap
59
60// the power of $count
61
62 if(isset($stuff2[0][0])) {
63
64for ($count = 0; $count < count($stuff2); $count++) {
65 $stuff2[$count] = $stuff2[$count][0];
66 $stuff2[$count] = ucwords(strtolower(trim(substr(substr($stuff2[$count], 15), 0, -5))));
67}
68
69 } else {
70
71 // if no data was found
72 $stuff2 = array("bad", "bad", "bad", "bad", "bad");
73
74 }
75
76$count = 0;
77
78 return $stuff2;
79
80}
81
82function getlatdata($ip) {
83
84global $firewall_host;
85global $firewall_port;
86global $use_firewall;
87
88
89 $data = array("");
90
91if ($use_firewall) {
92
93 $fp = fsockopen ($firewall_host, $firewall_port, $errno, $errstr, 30) or die("Could not open socket to proxy");
94 if (!$fp) {
71502268 95 echo "$errstr ($errno)<br/>\n";
f4fb66d7 96 } else {
97 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");
98 while (!feof($fp)) {
99 $packet = fgets ($fp,128) or die("Could not read from socket");
100 array_push($data, $packet) or die("Could not push data into array");
101 }
102 fclose ($fp);
103 }
104
105} else {
106
107$fp = fsockopen ("netgeo.caida.org", 80, $errno, $errstr, 30) or die("Could not open socket to caida.org");
108if (!$fp) {
71502268 109 echo "$errstr ($errno)<br/>\n";
f4fb66d7 110} else {
111 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");
112 while (!feof($fp)) {
113 $packet = fgets ($fp,128) or die("Could not read from socket");
114 array_push($data, $packet) or die("Could not push socket data into array");
115 }
116 fclose ($fp);
117}
118
119}
120
121 // make it an array
122
123return $data;
124
125}
126
127function finddot($name, $cssdots, $defaultdot) {
128 foreach($cssdots as $x) {
129 list($filename, , $width, $height) = split(":", $x);
130 if($filename == $name) { $found = 1; $return = array($name, $width, $height); }
131 }
132if(isset($found)) {
133return $return;
134} else {
135$dott = $cssdots[$defaultdot];
136list($dott, , $width, $height) = split(":", $dott);
137return array($dott, $width, $height);
138}
139}
140
141function getlocationcoords($lat, $lon, $width, $height) {
142 // some cartographers weren't mathematicians, up is apparently negative to them
143 $lat = ($lat * -1);
144
145 $lat = ($lat + 90);
146 $lon = ($lon + 180);
147 $x = ($lon * ($width / 360));
148 $y = ($lat * ($height / 180));
149
150 $x = round($x);
151 $y = round($y);
152 return array($x, $y);
153}
154
155function getimagecoords($earthimages, $image) {
156 foreach($earthimages as $x) {
157 list($file, , $width, $height) = split(":", $x);
158 if($file == $image) {
159 $coords = array("$width", "$height");
160 return $coords;
161 }
162 }
163}
164
165function validcookie($cookie) {
166 if(preg_match("/.*.:.*.:.*.:.*.:.*./", $cookie)) {
167 return TRUE;
168 } else {
169 return FALSE;
170 }
171}
172
173function isvalidimage($cookie, $earthimages, $defaultimage) {
174# list(, , , $setearthimage) = split(":", $cookie);
175 $setearthimage = $cookie;
176 if(isset($setearthimage)) {
177 // check if the image is one actually defined, not a cookie edit
178 foreach($earthimages as $image) {
179 list($testearthimage, , , ) = split(":", $image);
180 if($testearthimage == $setearthimage) {
181 $found = 1;
182 }
183 }
184 if(isset($found)) {
185 return $setearthimage;
186 } else {
187 return $earthimages[$defaultimage];
188 }
189 } else {
190 return $earthimages[$defaultimage];
191 }
192}
193
194
195
196function istheregd() {
197 global $trygd;
198 if($trygd == 1) {
199 if (@ImageTypes() & IMG_PNG) {
200 return TRUE;
201 } else {
202 return FALSE;
203 }
204 } else {
205 return FALSE;
206 }
207}
208
209function checkbrowser($agent) {
210 if (strstr($agent, "Mozilla/4.7") || strstr($agent, "Opera 6") || strstr($agent, "Opera/6")) {
211 return FALSE;
212 } else {
213 return TRUE;
214 }
215}
216
217function shouldrun($agent) {
218
219 // could cause probs... dunno
220 global $HTTP_COOKIE_VARS;
221
222 // check for new format of cookie with 6 parameters
223 if(@preg_match("/.*.:.*.:.*.:.*.:.*.:.*./", $HTTP_COOKIE_VARS["atlasprefs"])) {
224 list( , , , , , $drawmode) = split(":", $HTTP_COOKIE_VARS["atlasprefs"]);
225 } else {
226 $drawmode = "";
227 }
228
229 // don't try to understand below, it figures out whether
230 // to run it in css or gd based of prefs, server ability, and user agent.
231
232 if(istheregd() && ($drawmode == "1")) {
233 return TRUE;
234 } elseif($drawmode == "0") {
235 return FALSE;
236 } elseif(checkbrowser($agent) && istheregd()) {
237 return TRUE;
238 } elseif(!checkbrowser($agent)) {
239 return FALSE;
240 } elseif (!istheregd()) {
241 return FALSE;
242 }
243
244
245}
246
247function s10_validate_ip($in) {
248 if (is_string($in) && ereg('^([0-9]{1,3})\.([0-9]{1,3})\.' .
249 '([0-9]{1,3})\.([0-9]{1,3})$' ,
250 $in, $part)) {
251 if ($part[1] <= 255 && $part[2] <= 255 &&
252 $part[3] <= 255 && $part[4] <= 255)
253 return TRUE;
254 }
255 return FALSE;
256 }
257
258function getstuff($address, $local) {
259
260 $address = trim($address);
261
262# some people still think that urls are hostnames
263$address = str_replace("http://", "", $address);
264$address = preg_replace("/\/.*$/", "", $address);
265
266// Security checks
267$address = escapeshellcmd($address);
268
269$values = array();
270
271$values["address"] = $address;
272
273if(eregi("[a-z]", $address)){
274$ipaddress = gethostbyname($address);
275
276 if($ipaddress == $address) {
277
278$values["validity"] = "no";
279
280 }
281
282$values["hostname"] = $address;
283
284$values["ishost"] = "yes";
285
286} else {
287
288if(s10_validate_ip($address)) {
289
290$ipaddress = $address;
291 $values["hostname"] = "";
292} else {
293$values["validity"] = "no";
294}
295
296}
297
298 if(!isset($values["validity"])) {
299 if(s10_rfc_1918_ip($ipaddress)) {
300 $private = "yes";
301 } else {
302 $private = "no";
303 }
304 } else {
305 $private = "no";
306 }
307
308 if(!isset($ipaddress)) { $ipaddress = ""; }
309
310$values["ipaddress"] = $ipaddress;
311
312if(!isset($values["validity"]) && ($private == "no")) {
313
314$stuff = getlatdata($ipaddress);
315
316list(
317
318 $values["city"],
319 $values["state"],
320 $values["country"],
321 $values["lat"],
322 $values["lon"]
323
324) = stuffanalyze($stuff);
325
326$desc = "";
327
328// check if it is the user's ip address
329if($local == 1) { $desc .= t("You at"); }
330
331// add the ip address and hostname
332$desc .= "<b>$values[hostname]</b> (<b>$values[ipaddress]</b>) ";
333
334// use "are" if it is the user's ip address
335if($local == 1) { $desc .= t("are"); } else { $desc .= t("is"); }
336
337$desc .= " ".t("located in")." ";
338
339// add the city if it's there
340if($values["city"]) {
341$desc .= "$values[city], ";
342}
343
344// add the state if its there
345if($values["state"]) {
346$desc .= "$values[state], ";
347}
348
349if($values["country"]) {
350// make the country code capital so its ready for lookup
351$values["country"] = strtoupper($values["country"]);
352// convert the country code to a country name
353$countries = file("countries.txt") or die("Could not open countries file");
354$precountry = array_values(preg_grep("/$values[country] /", $countries));
355$values["country"] = trim(substr($precountry[0], 4));
356$desc .= "$values[country].";
357}
358
359 $desc .= " <font color=\"#aaaaaa\">($values[lat], $values[lon])</font>";
360
361 $state = $values["state"];
362 $city = $values["city"];
363
364// decide if address can't resolve, be located, or if it's fine
365if($values["lat"] == "0.00" && $values["lon"] == "0.00" && (@($state != "bad" && $city != "bad"))) {
366 $values["desc"] = "<b>$values[address]</b> ".t("cannot be located.");
367$values["lat"] = "";
368$values["lon"] = "";
369} elseif($values["lat"] == "" && $values["lon"] == "") {
370 $values["desc"] = "<b>$values[address]</b> ".t("cannot be located.");
371} else {
372$values["desc"] = $desc;
373}
374
375} else {
376
377// check if it was a host before and decide on an error message
378
379if($private == "yes") {
380 $values["desc"] = "<b>$address</b> ".t("is a host in the private IP address range.");
381} elseif(@($values["state"] == "bad" && $values["city"] == "bad")) {
382$values["desc"] = t("Temporary lookup failure.");
383} elseif(isset($values["ishost"])) {
384$values["desc"] = "<b>$address</b> ".t("does not resolve.");
385} else {
386$values["desc"] = "<b>$address</b> ".t("is not a valid IP address.");
387}
388
389// some blank lat/lon for the image script or it will plot us in the center
390$values["lon"] = "";
391$values["lat"] = "";
392
393}
394
395return($values);
396}
397
398?>