home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 December / PCWorld_2002-12_cd.bin / Software / Vyzkuste / ipatlas / plot.inc < prev    next >
Text File  |  2002-07-02  |  10KB  |  398 lines

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