Advertisement
Whitetigerswt

GeoIP v2.0

Nov 10th, 2011
3,610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 11.55 KB | None | 0 0
  1. /*
  2.  
  3.     GeoIP Include by Whitetiger
  4.     -> SQLite GeoIP Include
  5.    
  6.     Credits: Whitetiger, RaekwonDaChef, Y_Less, Andreas Gohr.
  7.    
  8.     MaxMind, GeoIP and related marks are registered trademarks of MaxMind, Inc.
  9.    
  10.     ---------------------------------------------------------------------------------------
  11.    
  12. */
  13.  
  14. #if defined _geolocation_included
  15.   #endinput
  16. #endif
  17.  
  18.  
  19. #define _geolocation_included
  20.  
  21. #include <a_samp>
  22.  
  23. /*
  24.     ---------------------------------------------------------------------------------------
  25.     NATIVES
  26.     ---------------------------------------------------------------------------------------
  27. */
  28.  
  29. /*
  30.     native GetPlayerCountry(playerid, string[], const len = sizeof(string));
  31.     native GetPlayerISP(playerid, string[], const len = sizeof(string));
  32.     native GetPlayerCity(playerid, string[], const len = sizeof(string));
  33.     native GetPlayerGMT(playerid);
  34.     native GetPlayerProxy(playerid);
  35. */
  36.  
  37. /*
  38.     ---------------------------------------------------------------------------------------
  39.     Variables
  40.     ---------------------------------------------------------------------------------------
  41. */
  42.  
  43.  
  44. #if !defined GeoIP_MainFile
  45.     #define GeoIP_MainFile      "geoip.db"
  46. #endif
  47.  
  48. #if !defined GeoIP_CityFile
  49.     #define GeoIP_CityFile      "geoip_city.db"
  50. #endif
  51.  
  52. new DB:geoip_db;
  53. new DB:geoip_city;
  54.  
  55.  
  56. new DBResult:_result;
  57.  
  58. stock GetPlayerCountry(playerid, string[], const len = sizeof(string)) {
  59.     new ip[24];
  60.     GetPlayerIp(playerid, ip, sizeof(ip));
  61.  
  62.     GetIPCountry(ip, string, len);
  63.  
  64.     return true;
  65. }
  66.  
  67. stock GetPlayerISP(playerid, string[], const len = sizeof(string)) {
  68.     new ip[24];
  69.     GetPlayerIp(playerid, ip, sizeof(ip));
  70.  
  71.     GetIPISP(ip, string, len);
  72.     new placeholder[1];
  73.     sscanf_(string, "ss", placeholder, string);
  74.  
  75.     return true;
  76. }
  77.  
  78. stock GetPlayerCity(playerid, string[], const len = sizeof(string)) {
  79.     new ip[24];
  80.     GetPlayerIp(playerid, ip, sizeof(ip));
  81.  
  82.     GetIPCity(ip, string, len);
  83.  
  84.     return true;
  85. }
  86.  
  87. stock GetPlayerGMT(playerid) {
  88.     new ip[24];
  89.     GetPlayerIp(playerid, ip, sizeof(ip));
  90.  
  91.     return GetIPGMT(ip);
  92. }
  93.  
  94. stock GetPlayerProxy(playerid) {
  95.     new ip[24];
  96.     GetPlayerIp(playerid, ip, sizeof(ip));
  97.    
  98.     return GetIPProxy(ip, playerid);
  99. }
  100.  
  101. /*
  102.  
  103.     ---------------------------------------------------------------------------------------
  104.     INTERNAL FUNCTIONS
  105.     ---------------------------------------------------------------------------------------
  106.  
  107.     - stock GetIPCountry(ip[], dest[], len = sizeof(dest))
  108.     - stock GetIPISP(ip[], dest[], len = sizeof(dest))
  109.     - stock GetIPCity(ip[], dest[], len = sizeof(dest))
  110.     - stock GetIPGMT(ip[])
  111.     - stock GetIPProxy(ip[])
  112.     - stock ip2long(ip[])
  113.  
  114. */
  115.  
  116. stock GetIPCountry(ip[], dest[], const len = sizeof(dest)) {
  117.     new tmp[90];
  118.     tmp = ip2long(ip);
  119.    
  120.     new string[500];
  121.     format(string, sizeof(string), "SELECT loc.*\n FROM loc_country loc,\n blocks_country blk\n WHERE blk.idx = (%s-(%s %% 65536))\n AND blk.startIpNum < %s\n AND blk.endIpNum > %s\n AND loc.locId = blk.locId LIMIT 1;", tmp, tmp, tmp, tmp);
  122.    
  123.     geoip_db = db_open(GeoIP_MainFile);
  124.     _result = db_query(geoip_db, string);
  125.     if(db_num_rows(_result) >= 1)
  126.     {
  127.         db_get_field_assoc(_result,"cn",dest,len);
  128.     }
  129.     db_free_result(_result);
  130.     db_close(geoip_db);
  131.    
  132.     if(!strlen(dest)) format(dest, len, "Unknown");
  133.    
  134.     return true;
  135. }
  136.  
  137. stock GetIPISP(ip[], dest[], const len = sizeof(dest)) {
  138.  
  139.     new tmp[90];
  140.     tmp = ip2long(ip);
  141.    
  142.     new string[500];
  143.     format(string, sizeof(string), "SELECT internet_service_provider FROM geo_isp WHERE idx >= (%s-(%s %% 65536)) AND ip_to >= %s AND  ip_from < %s LIMIT 1", tmp, tmp, tmp, tmp);
  144.  
  145.     geoip_db = db_open(GeoIP_MainFile);
  146.     _result = db_query(geoip_db, string);
  147.     if(db_num_rows(_result) >= 1)
  148.     {
  149.         db_get_field_assoc(_result,"internet_service_provider",dest,len);
  150.     }
  151.     db_free_result(_result);
  152.     db_close(geoip_db);
  153.    
  154.     if(!strlen(dest)) format(dest, len, "Unknown");
  155.  
  156.     return true;
  157. }
  158.  
  159. stock GetIPCity(ip[], dest[], const len = sizeof(dest)) {
  160.     new tmp[90];
  161.     tmp = ip2long(ip);
  162.    
  163.     new string[500];
  164.     format(string, sizeof(string), "SELECT loc.*\n FROM geolocation loc,\n geoblocks blk\n WHERE blk.idx = (%s-(%s %% 65536))\n AND blk.startIpNum < %s\n AND blk.endIpNum > %s\n AND loc.locId = blk.locId LIMIT 1;", tmp, tmp, tmp, tmp);
  165.  
  166.     geoip_city = db_open(GeoIP_CityFile);
  167.     _result = db_query(geoip_city, string);
  168.     if(db_num_rows(_result) >= 1)
  169.     {
  170.         db_get_field_assoc(_result,"city",dest,len);
  171.     }
  172.     db_free_result(_result);
  173.     db_close(geoip_city);
  174.    
  175.     if(!strlen(dest)) format(dest, len, "Unknown");
  176.    
  177.     return true;
  178. }
  179.  
  180. stock GetIPGMT(ip[]) {
  181.     new tmp[90];
  182.     tmp = ip2long(ip);
  183.    
  184.     new dest[50];
  185.    
  186.     new string[500];
  187.     format(string, sizeof(string), "SELECT loc.*\n FROM geolocation loc,\n geoblocks blk\n WHERE blk.idx = (%s-(%s %% 65536))\n AND blk.startIpNum < %s\n AND blk.endIpNum > %s\n AND loc.locId = blk.locId LIMIT 1;", tmp, tmp, tmp, tmp);
  188.  
  189.     geoip_city = db_open(GeoIP_CityFile);
  190.     _result = db_query(geoip_city, string);
  191.     if(db_num_rows(_result) >= 1)
  192.     {
  193.         db_get_field_assoc(_result, "longitude", dest, sizeof(dest));
  194.     }
  195.     db_free_result(_result);
  196.     db_close(geoip_city);
  197.    
  198.     if(!strlen(dest)) format(dest, sizeof(dest), "Unknown");
  199.    
  200.     return floatround( strval( dest ) / 15 );
  201. }
  202.  
  203. stock GetIPProxy(ip[], playerid) {
  204.  
  205.     if(IsPlayerConnected(playerid) && strlen(ip) > 0) {
  206.    
  207.         new string[500];
  208.         format(string, sizeof(string), "SELECT blk.*\n FROM geolocation loc,\n geoblocks blk\n WHERE blk.idx = (%s-(%s %% 65536))\n AND blk.startIpNum < %s\n AND blk.endIpNum > %s\n AND loc.locId = blk.locId LIMIT 1;", tmp, tmp, tmp, tmp);
  209.  
  210.         geoip_city = db_open(GeoIP_CityFile);
  211.         _result = db_query(geoip_city, string);
  212.         if(db_num_rows(_result) >= 1)
  213.         {
  214.             db_get_field_assoc(_result, "is_anonymous_proxy", dest, sizeof(dest));
  215.         }
  216.         db_free_result(_result);
  217.         db_close(geoip_city);
  218.        
  219.         return strval(dest);
  220.     }
  221.    
  222.    
  223.     return false;
  224. }
  225.  
  226. /*forward OnProxyRecieved(index, response_code, data[]);
  227. public OnProxyRecieved(index, response_code, data[]) {
  228.  
  229.     if(IsPlayerConnected(index)) {
  230.    
  231.         new ip[MAX_PLAYER_NAME];
  232.         GetPlayerIp(index, ip, sizeof(ip));
  233.        
  234.         if(strlen(IPInfo[index]) > 0 && strlen(ip) > 0 && !strcmp(IPInfo[index], ip, true)) {
  235.             CallLocalFunction("OnProxyResponse", "ii",  index, strval(data));
  236.         }
  237.     }
  238.     IPInfo[index] = "";
  239. }*/
  240.  
  241. stock ip2long(ip[]) {
  242.     new ips[4];
  243.     sscanf_(ip, "p.dddd", ips[0], ips[1], ips[2], ips[3]);
  244.     new tmp[90];
  245.     format(tmp, sizeof(tmp), "((16777216 * %d) + (65536 * %d) + (256 * %d) + %d)", ips[0], ips[1], ips[2], ips[3]);
  246.     // we use a string here so it will not pass the 32-bit integer limits, the math is done later in the sql query
  247.     return tmp;
  248. }
  249.  
  250. /*
  251.  
  252.     ---------------------------------------------------------------------------------------
  253.     SECONDARY FUNCTIONS
  254.     ---------------------------------------------------------------------------------------
  255.    
  256.     - stock sscanf_(string[], format[], {Float,_}:...)
  257.  
  258. */
  259.  
  260. stock sscanf_(string[], format[], {Float,_}:...) // by Y_Less
  261. {
  262.     #if defined isnull
  263.         if (isnull(string))
  264.     #else
  265.         if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  266.     #endif
  267.         {
  268.             return format[0];
  269.         }
  270.     #pragma tabsize 4
  271.     new
  272.         formatPos = 0,
  273.         stringPos = 0,
  274.         paramPos = 2,
  275.         paramCount = numargs(),
  276.         delim = ' ';
  277.     while (string[stringPos] && string[stringPos] <= ' ')
  278.     {
  279.         stringPos++;
  280.     }
  281.     while (paramPos < paramCount && string[stringPos])
  282.     {
  283.         switch (format[formatPos++])
  284.         {
  285.             case '\0':
  286.             {
  287.                 return 0;
  288.             }
  289.             case 'i', 'd':
  290.             {
  291.                 new
  292.                     neg = 1,
  293.                     num = 0,
  294.                     ch = string[stringPos];
  295.                 if (ch == '-')
  296.                 {
  297.                     neg = -1;
  298.                     ch = string[++stringPos];
  299.                 }
  300.                 do
  301.                 {
  302.                     stringPos++;
  303.                     if ('0' <= ch <= '9')
  304.                     {
  305.                         num = (num * 10) + (ch - '0');
  306.                     }
  307.                     else
  308.                     {
  309.                         return -1;
  310.                     }
  311.                 }
  312.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  313.                 setarg(paramPos, 0, num * neg);
  314.             }
  315.             case 'h', 'x':
  316.             {
  317.                 new
  318.                     num = 0,
  319.                     ch = string[stringPos];
  320.                 do
  321.                 {
  322.                     stringPos++;
  323.                     switch (ch)
  324.                     {
  325.                         case 'x', 'X':
  326.                         {
  327.                             num = 0;
  328.                             continue;
  329.                         }
  330.                         case '0' .. '9':
  331.                         {
  332.                             num = (num << 4) | (ch - '0');
  333.                         }
  334.                         case 'a' .. 'f':
  335.                         {
  336.                             num = (num << 4) | (ch - ('a' - 10));
  337.                         }
  338.                         case 'A' .. 'F':
  339.                         {
  340.                             num = (num << 4) | (ch - ('A' - 10));
  341.                         }
  342.                         default:
  343.                         {
  344.                             return -1;
  345.                         }
  346.                     }
  347.                 }
  348.                 while ((ch = string[stringPos]) > ' ' && ch != delim);
  349.                 setarg(paramPos, 0, num);
  350.             }
  351.             case 'c':
  352.             {
  353.                 setarg(paramPos, 0, string[stringPos++]);
  354.             }
  355.             case 'f':
  356.             {
  357.  
  358.                 new changestr[16], changepos = 0, strpos = stringPos;
  359.                 while(changepos < 16 && string[strpos] && string[strpos] != delim)
  360.                 {
  361.                     changestr[changepos++] = string[strpos++];
  362.                 }
  363.                 changestr[changepos] = '\0';
  364.                 setarg(paramPos,0,_:floatstr(changestr));
  365.             }
  366.             case 'p':
  367.             {
  368.                 delim = format[formatPos++];
  369.                 continue;
  370.             }
  371.             case '\'':
  372.             {
  373.                 new
  374.                     end = formatPos - 1,
  375.                     ch;
  376.                 while ((ch = format[++end]) && ch != '\'') {}
  377.                 if (!ch)
  378.                 {
  379.                     return -1;
  380.                 }
  381.                 format[end] = '\0';
  382.                 if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  383.                 {
  384.                     if (format[end + 1])
  385.                     {
  386.                         return -1;
  387.                     }
  388.                     return 0;
  389.                 }
  390.                 format[end] = '\'';
  391.                 stringPos = ch + (end - formatPos);
  392.                 formatPos = end + 1;
  393.             }
  394.             case 'u':
  395.             {
  396.                 new
  397.                     end = stringPos - 1,
  398.                     id = 0,
  399.                     bool:num = true,
  400.                     ch;
  401.                 while ((ch = string[++end]) && ch != delim)
  402.                 {
  403.                     if (num)
  404.                     {
  405.                         if ('0' <= ch <= '9')
  406.                         {
  407.                             id = (id * 10) + (ch - '0');
  408.                         }
  409.                         else
  410.                         {
  411.                             num = false;
  412.                         }
  413.                     }
  414.                 }
  415.                 if (num && IsPlayerConnected(id))
  416.                 {
  417.                     setarg(paramPos, 0, id);
  418.                 }
  419.                 else
  420.                 {
  421.                     #if !defined foreach
  422.                         #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  423.                         #define __SSCANF_FOREACH__
  424.                     #endif
  425.                     string[end] = '\0';
  426.                     num = false;
  427.                     new
  428.                         name[MAX_PLAYER_NAME];
  429.                     id = end - stringPos;
  430.                     foreach (Player, playerid)
  431.                     {
  432.                         GetPlayerName(playerid, name, sizeof (name));
  433.                         if (!strcmp(name, string[stringPos], true, id))
  434.                         {
  435.                             setarg(paramPos, 0, playerid);
  436.                             num = true;
  437.                             break;
  438.                         }
  439.                     }
  440.                     if (!num)
  441.                     {
  442.                         setarg(paramPos, 0, INVALID_PLAYER_ID);
  443.                     }
  444.                     string[end] = ch;
  445.                     #if defined __SSCANF_FOREACH__
  446.                         #undef foreach
  447.                         #undef __SSCANF_FOREACH__
  448.                     #endif
  449.                 }
  450.                 stringPos = end;
  451.             }
  452.             case 's', 'z':
  453.             {
  454.                 new
  455.                     i = 0,
  456.                     ch;
  457.                 if (format[formatPos])
  458.                 {
  459.                     while ((ch = string[stringPos++]) && ch != delim)
  460.                     {
  461.                         setarg(paramPos, i++, ch);
  462.                     }
  463.                     if (!i)
  464.                     {
  465.                         return -1;
  466.                     }
  467.                 }
  468.                 else
  469.                 {
  470.                     while ((ch = string[stringPos++]))
  471.                     {
  472.                         setarg(paramPos, i++, ch);
  473.                     }
  474.                 }
  475.                 stringPos--;
  476.                 setarg(paramPos, i, '\0');
  477.             }
  478.             default:
  479.             {
  480.                 continue;
  481.             }
  482.         }
  483.         while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  484.         {
  485.             stringPos++;
  486.         }
  487.         while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  488.         {
  489.             stringPos++;
  490.         }
  491.         paramPos++;
  492.     }
  493.     do
  494.     {
  495.         if ((delim = format[formatPos++]) > ' ')
  496.         {
  497.             if (delim == '\'')
  498.             {
  499.                 while ((delim = format[formatPos++]) && delim != '\'') {}
  500.             }
  501.             else if (delim != 'z')
  502.             {
  503.                 return delim;
  504.             }
  505.         }
  506.     }
  507.     while (delim > ' ');
  508.     return 0;
  509. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement