[0001]
[0002]
[0003]
[0004]
[0005]
[0006]
[0007]
[0008]
[0009]
[0010]
[0011]
[0012]
[0013]
[0014]
[0015]
[0016]
[0017]
[0018]
[0019]
[0020]
[0021]
[0022]
[0023]
[0024]
[0025]
[0026]
[0027]
[0028]
[0029]
[0030]
[0031]
[0032]
[0033]
[0034]
[0035]
[0036]
[0037]
[0038]
[0039]
[0040]
[0041]
[0042]
[0043]
[0044]
[0045]
[0046]
[0047]
[0048]
[0049]
[0050]
[0051]
[0052]
[0053]
[0054]
[0055]
[0056]
[0057]
[0058]
[0059]
[0060]
[0061]
[0062]
[0063]
[0064]
[0065]
[0066]
[0067]
[0068]
[0069]
[0070]
[0071]
[0072]
[0073]
[0074]
[0075]
[0076]
[0077]
[0078]
[0079]
[0080]
[0081]
[0082]
[0083]
[0084]
[0085]
[0086]
[0087]
[0088]
[0089]
[0090]
[0091]
[0092]
[0093]
[0094]
[0095]
[0096]
[0097]
[0098]
[0099]
[0100]
[0101]
[0102]
[0103]
[0104]
[0105]
[0106]
[0107]
[0108]
[0109]
[0110]
[0111]
[0112]
[0113]
[0114]
[0115]
[0116]
[0117]
[0118]
[0119]
[0120]
[0121]
[0122]
[0123]
[0124]
[0125]
[0126]
[0127]
[0128]
[0129]
[0130]
[0131]
[0132]
[0133]
[0134]
[0135]
[0136]
[0137]
[0138]
[0139]
///////////////////////////////////////////////////////////////////////////////
/*
                       qdlogstats_geolocate_geoipdb.js

https://geolocation-db.com/

This is a free service and does not require registration.

This module caches geo data and so only accesses the service once for each
unique host begin resolved.  This service only accepts IP adresses.


COPYRIGHT
---------
Copyright (C) 2018-2021 Mark G.Daniel
This program, comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it under the
conditions of the GNU GENERAL PUBLIC LICENSE, version 3, or any later version.
http://www.gnu.org/licenses/gpl.txt


VERSION
-------
05-MAY-2021  MGD  moves https://geoip-db.com/ to https://geolocation-db.com/
                    (leave as the original JavaScript file name though)
                  make xhr.open() asynchronous
06-NOV-2018  MGD  initial
*/
///////////////////////////////////////////////////////////////////////////////

var geoLocateAddr = [];
var geoLocateData = [];
var geoLocateWait = [];
var geoLocateURL = 'https://geolocation-db.com/jsonp/';

///////////////////////////////////////////////////////////////////////////////

function geoLocate (id, addr)
{
   var idx;

   for (idx = 0; idx < geoLocateAddr.length; idx++)
      if (geoLocateAddr[idx] == addr) break;

   if (!geoLocateData[idx])
   {
      geoLocateAddr[idx] = addr;
      geoLocateData[idx] = '';
      if (!geoLocateWait[idx])
      {
         geoLocateGeoIpDb(id,idx,addr);
         geoLocateWait[idx] = '';
      }
      else
         geoLocateWait[idx] += '|';
      geoLocateWait[idx] += id;
      return;
   }

   document.getElementById(id).innerHTML = geoLocateData[idx];
}

///////////////////////////////////////////////////////////////////////////////

function geoLocateGeoIpDb(id,idx,addr)
{
   var url = geoLocateURL + addr;

   var xhr = new XMLHttpRequest();

   // add the address as a private attribute
   xhr._addr = addr;

   xhr.open('GET', url, true);

   xhr.onreadystatechange = function()
   {
      if (this.readyState == 4)
      {
         var gld;
         if (this.status == 200)
         {
            var jsnp = this.responseText;
            var jsn = JSON.parse(jsnp.substr(9,jsnp.length-10));
            var country = jsn['country_name'];
            var state = jsn['state'];
            var city = jsn['city'];

            gld = '{';

            if (country != null)
            {
               if (country != "")
               {
                  gld += country;

                  if (state && state != "" && state != country)
                     gld += ', ' + jsn['state'];

                  if (city && city != "" && city != state)
                     gld += ', ' + jsn['city'];
               }
               else
                  gld += '?';
            }
            else
               gld += 'bugcheck';

            gld += '}';
         }
         else
         if (this.status)
         {
            gld = '{HTTP ' + this.status + ' error}';
         }
         else
         {
            console.log(this);
            gld = '{see console}';
         }

         // populate those waiting for asynchronous completion
         for (var idx1 = 0; idx1 < geoLocateAddr.length; idx1++)
         {
            if (!geoLocateWait[idx1]) continue;
            if (this._addr != geoLocateAddr[idx1]) continue;
            geoLocateData[idx1] = gld;
            var wait = geoLocateWait[idx1].split('|');
            for (var idx2 = 0; idx2 < wait.length; idx2++)
               document.getElementById(wait[idx2]).innerHTML = gld;
            geoLocateWait[idx1] = null;
         }
      }
   };

   xhr.send(); 
}

///////////////////////////////////////////////////////////////////////////////