K9283RU18934G3894TIK

geolocation include

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