/geo/geoip.inc.php

  1. <?php 
  2.  
  3. /** -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ 
  4. /** geoip.inc 
  5. * 
  6. * Copyright (C) 2007 MaxMind LLC 
  7. * 
  8. * This library is free software; you can redistribute it and/or 
  9. * modify it under the terms of the GNU Lesser General Public 
  10. * License as published by the Free Software Foundation; either 
  11. * version 2.1 of the License, or (at your option) any later version. 
  12. * 
  13. * This library is distributed in the hope that it will be useful,  
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of 
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
  16. * Lesser General Public License for more details. 
  17. * 
  18. * You should have received a copy of the GNU Lesser General Public 
  19. * License along with this library; if not, write to the Free Software 
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
  21. */ 
  22.  
  23. define("GEOIP_COUNTRY_BEGIN", 16776960); 
  24. define("GEOIP_STATE_BEGIN_REV0", 16700000); 
  25. define("GEOIP_STATE_BEGIN_REV1", 16000000); 
  26. define("GEOIP_STANDARD", 0); 
  27. define("GEOIP_MEMORY_CACHE", 1); 
  28. define("GEOIP_SHARED_MEMORY", 2); 
  29. define("STRUCTURE_INFO_MAX_SIZE", 20); 
  30. define("DATABASE_INFO_MAX_SIZE", 100); 
  31. define("GEOIP_COUNTRY_EDITION", 1); 
  32. define("GEOIP_PROXY_EDITION", 8); 
  33. define("GEOIP_ASNUM_EDITION", 9); 
  34. define("GEOIP_NETSPEED_EDITION", 10); 
  35. define("GEOIP_REGION_EDITION_REV0", 7); 
  36. define("GEOIP_REGION_EDITION_REV1", 3); 
  37. define("GEOIP_CITY_EDITION_REV0", 6); 
  38. define("GEOIP_CITY_EDITION_REV1", 2); 
  39. define("GEOIP_ORG_EDITION", 5); 
  40. define("GEOIP_ISP_EDITION", 4); 
  41. define("SEGMENT_RECORD_LENGTH", 3); 
  42. define("STANDARD_RECORD_LENGTH", 3); 
  43. define("ORG_RECORD_LENGTH", 4); 
  44. define("MAX_RECORD_LENGTH", 4); 
  45. define("MAX_ORG_RECORD_LENGTH", 300); 
  46. define("GEOIP_SHM_KEY", 0x4f415401); 
  47. define("US_OFFSET", 1); 
  48. define("CANADA_OFFSET", 677); 
  49. define("WORLD_OFFSET", 1353); 
  50. define("FIPS_RANGE", 360); 
  51. define("GEOIP_UNKNOWN_SPEED", 0); 
  52. define("GEOIP_DIALUP_SPEED", 1); 
  53. define("GEOIP_CABLEDSL_SPEED", 2); 
  54. define("GEOIP_CORPORATE_SPEED", 3); 
  55. define("GEOIP_DOMAIN_EDITION", 11); 
  56. define("GEOIP_COUNTRY_EDITION_V6", 12); 
  57. define("GEOIP_LOCATIONA_EDITION", 13); 
  58. define("GEOIP_ACCURACYRADIUS_EDITION", 14); 
  59. define("GEOIP_CITYCOMBINED_EDITION", 15); 
  60. define("GEOIP_CITY_EDITION_REV1_V6", 30); 
  61. define("GEOIP_CITY_EDITION_REV0_V6", 31); 
  62. define("GEOIP_NETSPEED_EDITION_REV1", 32); 
  63. define("GEOIP_NETSPEED_EDITION_REV1_V6", 33); 
  64. define("GEOIP_USERTYPE_EDITION", 28); 
  65. define("GEOIP_USERTYPE_EDITION_V6", 29); 
  66. define("GEOIP_ASNUM_EDITION_V6", 21); 
  67. define("GEOIP_ISP_EDITION_V6", 22); 
  68. define("GEOIP_ORG_EDITION_V6", 23); 
  69. define("GEOIP_DOMAIN_EDITION_V6", 24); 
  70.  
  71. define("CITYCOMBINED_FIXED_RECORD", 7); 
  72.  
  73. class GeoIP 
  74. public $flags; 
  75. public $filehandle; 
  76. public $memory_buffer; 
  77. public $databaseType; 
  78. public $databaseSegments; 
  79. public $record_length; 
  80. public $shmid; 
  81. public $GEOIP_COUNTRY_CODE_TO_NUMBER = array( 
  82. "" => 0,  
  83. "AP" => 1,  
  84. "EU" => 2,  
  85. "AD" => 3,  
  86. "AE" => 4,  
  87. "AF" => 5,  
  88. "AG" => 6,  
  89. "AI" => 7,  
  90. "AL" => 8,  
  91. "AM" => 9,  
  92. "CW" => 10,  
  93. "AO" => 11,  
  94. "AQ" => 12,  
  95. "AR" => 13,  
  96. "AS" => 14,  
  97. "AT" => 15,  
  98. "AU" => 16,  
  99. "AW" => 17,  
  100. "AZ" => 18,  
  101. "BA" => 19,  
  102. "BB" => 20,  
  103. "BD" => 21,  
  104. "BE" => 22,  
  105. "BF" => 23,  
  106. "BG" => 24,  
  107. "BH" => 25,  
  108. "BI" => 26,  
  109. "BJ" => 27,  
  110. "BM" => 28,  
  111. "BN" => 29,  
  112. "BO" => 30,  
  113. "BR" => 31,  
  114. "BS" => 32,  
  115. "BT" => 33,  
  116. "BV" => 34,  
  117. "BW" => 35,  
  118. "BY" => 36,  
  119. "BZ" => 37,  
  120. "CA" => 38,  
  121. "CC" => 39,  
  122. "CD" => 40,  
  123. "CF" => 41,  
  124. "CG" => 42,  
  125. "CH" => 43,  
  126. "CI" => 44,  
  127. "CK" => 45,  
  128. "CL" => 46,  
  129. "CM" => 47,  
  130. "CN" => 48,  
  131. "CO" => 49,  
  132. "CR" => 50,  
  133. "CU" => 51,  
  134. "CV" => 52,  
  135. "CX" => 53,  
  136. "CY" => 54,  
  137. "CZ" => 55,  
  138. "DE" => 56,  
  139. "DJ" => 57,  
  140. "DK" => 58,  
  141. "DM" => 59,  
  142. "DO" => 60,  
  143. "DZ" => 61,  
  144. "EC" => 62,  
  145. "EE" => 63,  
  146. "EG" => 64,  
  147. "EH" => 65,  
  148. "ER" => 66,  
  149. "ES" => 67,  
  150. "ET" => 68,  
  151. "FI" => 69,  
  152. "FJ" => 70,  
  153. "FK" => 71,  
  154. "FM" => 72,  
  155. "FO" => 73,  
  156. "FR" => 74,  
  157. "SX" => 75,  
  158. "GA" => 76,  
  159. "GB" => 77,  
  160. "GD" => 78,  
  161. "GE" => 79,  
  162. "GF" => 80,  
  163. "GH" => 81,  
  164. "GI" => 82,  
  165. "GL" => 83,  
  166. "GM" => 84,  
  167. "GN" => 85,  
  168. "GP" => 86,  
  169. "GQ" => 87,  
  170. "GR" => 88,  
  171. "GS" => 89,  
  172. "GT" => 90,  
  173. "GU" => 91,  
  174. "GW" => 92,  
  175. "GY" => 93,  
  176. "HK" => 94,  
  177. "HM" => 95,  
  178. "HN" => 96,  
  179. "HR" => 97,  
  180. "HT" => 98,  
  181. "HU" => 99,  
  182. "ID" => 100,  
  183. "IE" => 101,  
  184. "IL" => 102,  
  185. "IN" => 103,  
  186. "IO" => 104,  
  187. "IQ" => 105,  
  188. "IR" => 106,  
  189. "IS" => 107,  
  190. "IT" => 108,  
  191. "JM" => 109,  
  192. "JO" => 110,  
  193. "JP" => 111,  
  194. "KE" => 112,  
  195. "KG" => 113,  
  196. "KH" => 114,  
  197. "KI" => 115,  
  198. "KM" => 116,  
  199. "KN" => 117,  
  200. "KP" => 118,  
  201. "KR" => 119,  
  202. "KW" => 120,  
  203. "KY" => 121,  
  204. "KZ" => 122,  
  205. "LA" => 123,  
  206. "LB" => 124,  
  207. "LC" => 125,  
  208. "LI" => 126,  
  209. "LK" => 127,  
  210. "LR" => 128,  
  211. "LS" => 129,  
  212. "LT" => 130,  
  213. "LU" => 131,  
  214. "LV" => 132,  
  215. "LY" => 133,  
  216. "MA" => 134,  
  217. "MC" => 135,  
  218. "MD" => 136,  
  219. "MG" => 137,  
  220. "MH" => 138,  
  221. "MK" => 139,  
  222. "ML" => 140,  
  223. "MM" => 141,  
  224. "MN" => 142,  
  225. "MO" => 143,  
  226. "MP" => 144,  
  227. "MQ" => 145,  
  228. "MR" => 146,  
  229. "MS" => 147,  
  230. "MT" => 148,  
  231. "MU" => 149,  
  232. "MV" => 150,  
  233. "MW" => 151,  
  234. "MX" => 152,  
  235. "MY" => 153,  
  236. "MZ" => 154,  
  237. "NA" => 155,  
  238. "NC" => 156,  
  239. "NE" => 157,  
  240. "NF" => 158,  
  241. "NG" => 159,  
  242. "NI" => 160,  
  243. "NL" => 161,  
  244. "NO" => 162,  
  245. "NP" => 163,  
  246. "NR" => 164,  
  247. "NU" => 165,  
  248. "NZ" => 166,  
  249. "OM" => 167,  
  250. "PA" => 168,  
  251. "PE" => 169,  
  252. "PF" => 170,  
  253. "PG" => 171,  
  254. "PH" => 172,  
  255. "PK" => 173,  
  256. "PL" => 174,  
  257. "PM" => 175,  
  258. "PN" => 176,  
  259. "PR" => 177,  
  260. "PS" => 178,  
  261. "PT" => 179,  
  262. "PW" => 180,  
  263. "PY" => 181,  
  264. "QA" => 182,  
  265. "RE" => 183,  
  266. "RO" => 184,  
  267. "RU" => 185,  
  268. "RW" => 186,  
  269. "SA" => 187,  
  270. "SB" => 188,  
  271. "SC" => 189,  
  272. "SD" => 190,  
  273. "SE" => 191,  
  274. "SG" => 192,  
  275. "SH" => 193,  
  276. "SI" => 194,  
  277. "SJ" => 195,  
  278. "SK" => 196,  
  279. "SL" => 197,  
  280. "SM" => 198,  
  281. "SN" => 199,  
  282. "SO" => 200,  
  283. "SR" => 201,  
  284. "ST" => 202,  
  285. "SV" => 203,  
  286. "SY" => 204,  
  287. "SZ" => 205,  
  288. "TC" => 206,  
  289. "TD" => 207,  
  290. "TF" => 208,  
  291. "TG" => 209,  
  292. "TH" => 210,  
  293. "TJ" => 211,  
  294. "TK" => 212,  
  295. "TM" => 213,  
  296. "TN" => 214,  
  297. "TO" => 215,  
  298. "TL" => 216,  
  299. "TR" => 217,  
  300. "TT" => 218,  
  301. "TV" => 219,  
  302. "TW" => 220,  
  303. "TZ" => 221,  
  304. "UA" => 222,  
  305. "UG" => 223,  
  306. "UM" => 224,  
  307. "US" => 225,  
  308. "UY" => 226,  
  309. "UZ" => 227,  
  310. "VA" => 228,  
  311. "VC" => 229,  
  312. "VE" => 230,  
  313. "VG" => 231,  
  314. "VI" => 232,  
  315. "VN" => 233,  
  316. "VU" => 234,  
  317. "WF" => 235,  
  318. "WS" => 236,  
  319. "YE" => 237,  
  320. "YT" => 238,  
  321. "RS" => 239,  
  322. "ZA" => 240,  
  323. "ZM" => 241,  
  324. "ME" => 242,  
  325. "ZW" => 243,  
  326. "A1" => 244,  
  327. "A2" => 245,  
  328. "O1" => 246,  
  329. "AX" => 247,  
  330. "GG" => 248,  
  331. "IM" => 249,  
  332. "JE" => 250,  
  333. "BL" => 251,  
  334. "MF" => 252,  
  335. "BQ" => 253,  
  336. "SS" => 254 
  337. ); 
  338.  
  339. public $GEOIP_COUNTRY_CODES = array( 
  340. "",  
  341. "AP",  
  342. "EU",  
  343. "AD",  
  344. "AE",  
  345. "AF",  
  346. "AG",  
  347. "AI",  
  348. "AL",  
  349. "AM",  
  350. "CW",  
  351. "AO",  
  352. "AQ",  
  353. "AR",  
  354. "AS",  
  355. "AT",  
  356. "AU",  
  357. "AW",  
  358. "AZ",  
  359. "BA",  
  360. "BB",  
  361. "BD",  
  362. "BE",  
  363. "BF",  
  364. "BG",  
  365. "BH",  
  366. "BI",  
  367. "BJ",  
  368. "BM",  
  369. "BN",  
  370. "BO",  
  371. "BR",  
  372. "BS",  
  373. "BT",  
  374. "BV",  
  375. "BW",  
  376. "BY",  
  377. "BZ",  
  378. "CA",  
  379. "CC",  
  380. "CD",  
  381. "CF",  
  382. "CG",  
  383. "CH",  
  384. "CI",  
  385. "CK",  
  386. "CL",  
  387. "CM",  
  388. "CN",  
  389. "CO",  
  390. "CR",  
  391. "CU",  
  392. "CV",  
  393. "CX",  
  394. "CY",  
  395. "CZ",  
  396. "DE",  
  397. "DJ",  
  398. "DK",  
  399. "DM",  
  400. "DO",  
  401. "DZ",  
  402. "EC",  
  403. "EE",  
  404. "EG",  
  405. "EH",  
  406. "ER",  
  407. "ES",  
  408. "ET",  
  409. "FI",  
  410. "FJ",  
  411. "FK",  
  412. "FM",  
  413. "FO",  
  414. "FR",  
  415. "SX",  
  416. "GA",  
  417. "GB",  
  418. "GD",  
  419. "GE",  
  420. "GF",  
  421. "GH",  
  422. "GI",  
  423. "GL",  
  424. "GM",  
  425. "GN",  
  426. "GP",  
  427. "GQ",  
  428. "GR",  
  429. "GS",  
  430. "GT",  
  431. "GU",  
  432. "GW",  
  433. "GY",  
  434. "HK",  
  435. "HM",  
  436. "HN",  
  437. "HR",  
  438. "HT",  
  439. "HU",  
  440. "ID",  
  441. "IE",  
  442. "IL",  
  443. "IN",  
  444. "IO",  
  445. "IQ",  
  446. "IR",  
  447. "IS",  
  448. "IT",  
  449. "JM",  
  450. "JO",  
  451. "JP",  
  452. "KE",  
  453. "KG",  
  454. "KH",  
  455. "KI",  
  456. "KM",  
  457. "KN",  
  458. "KP",  
  459. "KR",  
  460. "KW",  
  461. "KY",  
  462. "KZ",  
  463. "LA",  
  464. "LB",  
  465. "LC",  
  466. "LI",  
  467. "LK",  
  468. "LR",  
  469. "LS",  
  470. "LT",  
  471. "LU",  
  472. "LV",  
  473. "LY",  
  474. "MA",  
  475. "MC",  
  476. "MD",  
  477. "MG",  
  478. "MH",  
  479. "MK",  
  480. "ML",  
  481. "MM",  
  482. "MN",  
  483. "MO",  
  484. "MP",  
  485. "MQ",  
  486. "MR",  
  487. "MS",  
  488. "MT",  
  489. "MU",  
  490. "MV",  
  491. "MW",  
  492. "MX",  
  493. "MY",  
  494. "MZ",  
  495. "NA",  
  496. "NC",  
  497. "NE",  
  498. "NF",  
  499. "NG",  
  500. "NI",  
  501. "NL",  
  502. "NO",  
  503. "NP",  
  504. "NR",  
  505. "NU",  
  506. "NZ",  
  507. "OM",  
  508. "PA",  
  509. "PE",  
  510. "PF",  
  511. "PG",  
  512. "PH",  
  513. "PK",  
  514. "PL",  
  515. "PM",  
  516. "PN",  
  517. "PR",  
  518. "PS",  
  519. "PT",  
  520. "PW",  
  521. "PY",  
  522. "QA",  
  523. "RE",  
  524. "RO",  
  525. "RU",  
  526. "RW",  
  527. "SA",  
  528. "SB",  
  529. "SC",  
  530. "SD",  
  531. "SE",  
  532. "SG",  
  533. "SH",  
  534. "SI",  
  535. "SJ",  
  536. "SK",  
  537. "SL",  
  538. "SM",  
  539. "SN",  
  540. "SO",  
  541. "SR",  
  542. "ST",  
  543. "SV",  
  544. "SY",  
  545. "SZ",  
  546. "TC",  
  547. "TD",  
  548. "TF",  
  549. "TG",  
  550. "TH",  
  551. "TJ",  
  552. "TK",  
  553. "TM",  
  554. "TN",  
  555. "TO",  
  556. "TL",  
  557. "TR",  
  558. "TT",  
  559. "TV",  
  560. "TW",  
  561. "TZ",  
  562. "UA",  
  563. "UG",  
  564. "UM",  
  565. "US",  
  566. "UY",  
  567. "UZ",  
  568. "VA",  
  569. "VC",  
  570. "VE",  
  571. "VG",  
  572. "VI",  
  573. "VN",  
  574. "VU",  
  575. "WF",  
  576. "WS",  
  577. "YE",  
  578. "YT",  
  579. "RS",  
  580. "ZA",  
  581. "ZM",  
  582. "ME",  
  583. "ZW",  
  584. "A1",  
  585. "A2",  
  586. "O1",  
  587. "AX",  
  588. "GG",  
  589. "IM",  
  590. "JE",  
  591. "BL",  
  592. "MF",  
  593. "BQ",  
  594. "SS",  
  595. "O1" 
  596. ); 
  597.  
  598. public $GEOIP_COUNTRY_CODES3 = array( 
  599. "",  
  600. "AP",  
  601. "EU",  
  602. "AND",  
  603. "ARE",  
  604. "AFG",  
  605. "ATG",  
  606. "AIA",  
  607. "ALB",  
  608. "ARM",  
  609. "CUW",  
  610. "AGO",  
  611. "ATA",  
  612. "ARG",  
  613. "ASM",  
  614. "AUT",  
  615. "AUS",  
  616. "ABW",  
  617. "AZE",  
  618. "BIH",  
  619. "BRB",  
  620. "BGD",  
  621. "BEL",  
  622. "BFA",  
  623. "BGR",  
  624. "BHR",  
  625. "BDI",  
  626. "BEN",  
  627. "BMU",  
  628. "BRN",  
  629. "BOL",  
  630. "BRA",  
  631. "BHS",  
  632. "BTN",  
  633. "BVT",  
  634. "BWA",  
  635. "BLR",  
  636. "BLZ",  
  637. "CAN",  
  638. "CCK",  
  639. "COD",  
  640. "CAF",  
  641. "COG",  
  642. "CHE",  
  643. "CIV",  
  644. "COK",  
  645. "CHL",  
  646. "CMR",  
  647. "CHN",  
  648. "COL",  
  649. "CRI",  
  650. "CUB",  
  651. "CPV",  
  652. "CXR",  
  653. "CYP",  
  654. "CZE",  
  655. "DEU",  
  656. "DJI",  
  657. "DNK",  
  658. "DMA",  
  659. "DOM",  
  660. "DZA",  
  661. "ECU",  
  662. "EST",  
  663. "EGY",  
  664. "ESH",  
  665. "ERI",  
  666. "ESP",  
  667. "ETH",  
  668. "FIN",  
  669. "FJI",  
  670. "FLK",  
  671. "FSM",  
  672. "FRO",  
  673. "FRA",  
  674. "SXM",  
  675. "GAB",  
  676. "GBR",  
  677. "GRD",  
  678. "GEO",  
  679. "GUF",  
  680. "GHA",  
  681. "GIB",  
  682. "GRL",  
  683. "GMB",  
  684. "GIN",  
  685. "GLP",  
  686. "GNQ",  
  687. "GRC",  
  688. "SGS",  
  689. "GTM",  
  690. "GUM",  
  691. "GNB",  
  692. "GUY",  
  693. "HKG",  
  694. "HMD",  
  695. "HND",  
  696. "HRV",  
  697. "HTI",  
  698. "HUN",  
  699. "IDN",  
  700. "IRL",  
  701. "ISR",  
  702. "IND",  
  703. "IOT",  
  704. "IRQ",  
  705. "IRN",  
  706. "ISL",  
  707. "ITA",  
  708. "JAM",  
  709. "JOR",  
  710. "JPN",  
  711. "KEN",  
  712. "KGZ",  
  713. "KHM",  
  714. "KIR",  
  715. "COM",  
  716. "KNA",  
  717. "PRK",  
  718. "KOR",  
  719. "KWT",  
  720. "CYM",  
  721. "KAZ",  
  722. "LAO",  
  723. "LBN",  
  724. "LCA",  
  725. "LIE",  
  726. "LKA",  
  727. "LBR",  
  728. "LSO",  
  729. "LTU",  
  730. "LUX",  
  731. "LVA",  
  732. "LBY",  
  733. "MAR",  
  734. "MCO",  
  735. "MDA",  
  736. "MDG",  
  737. "MHL",  
  738. "MKD",  
  739. "MLI",  
  740. "MMR",  
  741. "MNG",  
  742. "MAC",  
  743. "MNP",  
  744. "MTQ",  
  745. "MRT",  
  746. "MSR",  
  747. "MLT",  
  748. "MUS",  
  749. "MDV",  
  750. "MWI",  
  751. "MEX",  
  752. "MYS",  
  753. "MOZ",  
  754. "NAM",  
  755. "NCL",  
  756. "NER",  
  757. "NFK",  
  758. "NGA",  
  759. "NIC",  
  760. "NLD",  
  761. "NOR",  
  762. "NPL",  
  763. "NRU",  
  764. "NIU",  
  765. "NZL",  
  766. "OMN",  
  767. "PAN",  
  768. "PER",  
  769. "PYF",  
  770. "PNG",  
  771. "PHL",  
  772. "PAK",  
  773. "POL",  
  774. "SPM",  
  775. "PCN",  
  776. "PRI",  
  777. "PSE",  
  778. "PRT",  
  779. "PLW",  
  780. "PRY",  
  781. "QAT",  
  782. "REU",  
  783. "ROU",  
  784. "RUS",  
  785. "RWA",  
  786. "SAU",  
  787. "SLB",  
  788. "SYC",  
  789. "SDN",  
  790. "SWE",  
  791. "SGP",  
  792. "SHN",  
  793. "SVN",  
  794. "SJM",  
  795. "SVK",  
  796. "SLE",  
  797. "SMR",  
  798. "SEN",  
  799. "SOM",  
  800. "SUR",  
  801. "STP",  
  802. "SLV",  
  803. "SYR",  
  804. "SWZ",  
  805. "TCA",  
  806. "TCD",  
  807. "ATF",  
  808. "TGO",  
  809. "THA",  
  810. "TJK",  
  811. "TKL",  
  812. "TKM",  
  813. "TUN",  
  814. "TON",  
  815. "TLS",  
  816. "TUR",  
  817. "TTO",  
  818. "TUV",  
  819. "TWN",  
  820. "TZA",  
  821. "UKR",  
  822. "UGA",  
  823. "UMI",  
  824. "USA",  
  825. "URY",  
  826. "UZB",  
  827. "VAT",  
  828. "VCT",  
  829. "VEN",  
  830. "VGB",  
  831. "VIR",  
  832. "VNM",  
  833. "VUT",  
  834. "WLF",  
  835. "WSM",  
  836. "YEM",  
  837. "MYT",  
  838. "SRB",  
  839. "ZAF",  
  840. "ZMB",  
  841. "MNE",  
  842. "ZWE",  
  843. "A1",  
  844. "A2",  
  845. "O1",  
  846. "ALA",  
  847. "GGY",  
  848. "IMN",  
  849. "JEY",  
  850. "BLM",  
  851. "MAF",  
  852. "BES",  
  853. "SSD",  
  854. "O1" 
  855. ); 
  856.  
  857. public $GEOIP_COUNTRY_NAMES = array( 
  858. "",  
  859. "Asia/Pacific Region",  
  860. "Europe",  
  861. "Andorra",  
  862. "United Arab Emirates",  
  863. "Afghanistan",  
  864. "Antigua and Barbuda",  
  865. "Anguilla",  
  866. "Albania",  
  867. "Armenia",  
  868. "Curacao",  
  869. "Angola",  
  870. "Antarctica",  
  871. "Argentina",  
  872. "American Samoa",  
  873. "Austria",  
  874. "Australia",  
  875. "Aruba",  
  876. "Azerbaijan",  
  877. "Bosnia and Herzegovina",  
  878. "Barbados",  
  879. "Bangladesh",  
  880. "Belgium",  
  881. "Burkina Faso",  
  882. "Bulgaria",  
  883. "Bahrain",  
  884. "Burundi",  
  885. "Benin",  
  886. "Bermuda",  
  887. "Brunei Darussalam",  
  888. "Bolivia",  
  889. "Brazil",  
  890. "Bahamas",  
  891. "Bhutan",  
  892. "Bouvet Island",  
  893. "Botswana",  
  894. "Belarus",  
  895. "Belize",  
  896. "Canada",  
  897. "Cocos (Keeling) Islands",  
  898. "Congo, The Democratic Republic of the",  
  899. "Central African Republic",  
  900. "Congo",  
  901. "Switzerland",  
  902. "Cote D'Ivoire",  
  903. "Cook Islands",  
  904. "Chile",  
  905. "Cameroon",  
  906. "China",  
  907. "Colombia",  
  908. "Costa Rica",  
  909. "Cuba",  
  910. "Cape Verde",  
  911. "Christmas Island",  
  912. "Cyprus",  
  913. "Czech Republic",  
  914. "Germany",  
  915. "Djibouti",  
  916. "Denmark",  
  917. "Dominica",  
  918. "Dominican Republic",  
  919. "Algeria",  
  920. "Ecuador",  
  921. "Estonia",  
  922. "Egypt",  
  923. "Western Sahara",  
  924. "Eritrea",  
  925. "Spain",  
  926. "Ethiopia",  
  927. "Finland",  
  928. "Fiji",  
  929. "Falkland Islands (Malvinas)",  
  930. "Micronesia, Federated States of",  
  931. "Faroe Islands",  
  932. "France",  
  933. "Sint Maarten (Dutch part)",  
  934. "Gabon",  
  935. "United Kingdom",  
  936. "Grenada",  
  937. "Georgia",  
  938. "French Guiana",  
  939. "Ghana",  
  940. "Gibraltar",  
  941. "Greenland",  
  942. "Gambia",  
  943. "Guinea",  
  944. "Guadeloupe",  
  945. "Equatorial Guinea",  
  946. "Greece",  
  947. "South Georgia and the South Sandwich Islands",  
  948. "Guatemala",  
  949. "Guam",  
  950. "Guinea-Bissau",  
  951. "Guyana",  
  952. "Hong Kong",  
  953. "Heard Island and McDonald Islands",  
  954. "Honduras",  
  955. "Croatia",  
  956. "Haiti",  
  957. "Hungary",  
  958. "Indonesia",  
  959. "Ireland",  
  960. "Israel",  
  961. "India",  
  962. "British Indian Ocean Territory",  
  963. "Iraq",  
  964. "Iran, Islamic Republic of",  
  965. "Iceland",  
  966. "Italy",  
  967. "Jamaica",  
  968. "Jordan",  
  969. "Japan",  
  970. "Kenya",  
  971. "Kyrgyzstan",  
  972. "Cambodia",  
  973. "Kiribati",  
  974. "Comoros",  
  975. "Saint Kitts and Nevis",  
  976. "Korea, Democratic People's Republic of",  
  977. "Korea, Republic of",  
  978. "Kuwait",  
  979. "Cayman Islands",  
  980. "Kazakhstan",  
  981. "Lao People's Democratic Republic",  
  982. "Lebanon",  
  983. "Saint Lucia",  
  984. "Liechtenstein",  
  985. "Sri Lanka",  
  986. "Liberia",  
  987. "Lesotho",  
  988. "Lithuania",  
  989. "Luxembourg",  
  990. "Latvia",  
  991. "Libya",  
  992. "Morocco",  
  993. "Monaco",  
  994. "Moldova, Republic of",  
  995. "Madagascar",  
  996. "Marshall Islands",  
  997. "Macedonia",  
  998. "Mali",  
  999. "Myanmar",  
  1000. "Mongolia",  
  1001. "Macau",  
  1002. "Northern Mariana Islands",  
  1003. "Martinique",  
  1004. "Mauritania",  
  1005. "Montserrat",  
  1006. "Malta",  
  1007. "Mauritius",  
  1008. "Maldives",  
  1009. "Malawi",  
  1010. "Mexico",  
  1011. "Malaysia",  
  1012. "Mozambique",  
  1013. "Namibia",  
  1014. "New Caledonia",  
  1015. "Niger",  
  1016. "Norfolk Island",  
  1017. "Nigeria",  
  1018. "Nicaragua",  
  1019. "Netherlands",  
  1020. "Norway",  
  1021. "Nepal",  
  1022. "Nauru",  
  1023. "Niue",  
  1024. "New Zealand",  
  1025. "Oman",  
  1026. "Panama",  
  1027. "Peru",  
  1028. "French Polynesia",  
  1029. "Papua New Guinea",  
  1030. "Philippines",  
  1031. "Pakistan",  
  1032. "Poland",  
  1033. "Saint Pierre and Miquelon",  
  1034. "Pitcairn Islands",  
  1035. "Puerto Rico",  
  1036. "Palestinian Territory",  
  1037. "Portugal",  
  1038. "Palau",  
  1039. "Paraguay",  
  1040. "Qatar",  
  1041. "Reunion",  
  1042. "Romania",  
  1043. "Russian Federation",  
  1044. "Rwanda",  
  1045. "Saudi Arabia",  
  1046. "Solomon Islands",  
  1047. "Seychelles",  
  1048. "Sudan",  
  1049. "Sweden",  
  1050. "Singapore",  
  1051. "Saint Helena",  
  1052. "Slovenia",  
  1053. "Svalbard and Jan Mayen",  
  1054. "Slovakia",  
  1055. "Sierra Leone",  
  1056. "San Marino",  
  1057. "Senegal",  
  1058. "Somalia",  
  1059. "Suriname",  
  1060. "Sao Tome and Principe",  
  1061. "El Salvador",  
  1062. "Syrian Arab Republic",  
  1063. "Swaziland",  
  1064. "Turks and Caicos Islands",  
  1065. "Chad",  
  1066. "French Southern Territories",  
  1067. "Togo",  
  1068. "Thailand",  
  1069. "Tajikistan",  
  1070. "Tokelau",  
  1071. "Turkmenistan",  
  1072. "Tunisia",  
  1073. "Tonga",  
  1074. "Timor-Leste",  
  1075. "Turkey",  
  1076. "Trinidad and Tobago",  
  1077. "Tuvalu",  
  1078. "Taiwan",  
  1079. "Tanzania, United Republic of",  
  1080. "Ukraine",  
  1081. "Uganda",  
  1082. "United States Minor Outlying Islands",  
  1083. "United States",  
  1084. "Uruguay",  
  1085. "Uzbekistan",  
  1086. "Holy See (Vatican City State)",  
  1087. "Saint Vincent and the Grenadines",  
  1088. "Venezuela",  
  1089. "Virgin Islands, British",  
  1090. "Virgin Islands, U.S.",  
  1091. "Vietnam",  
  1092. "Vanuatu",  
  1093. "Wallis and Futuna",  
  1094. "Samoa",  
  1095. "Yemen",  
  1096. "Mayotte",  
  1097. "Serbia",  
  1098. "South Africa",  
  1099. "Zambia",  
  1100. "Montenegro",  
  1101. "Zimbabwe",  
  1102. "Anonymous Proxy",  
  1103. "Satellite Provider",  
  1104. "Other",  
  1105. "Aland Islands",  
  1106. "Guernsey",  
  1107. "Isle of Man",  
  1108. "Jersey",  
  1109. "Saint Barthelemy",  
  1110. "Saint Martin",  
  1111. "Bonaire, Saint Eustatius and Saba",  
  1112. "South Sudan",  
  1113. "Other" 
  1114. ); 
  1115.  
  1116. public $GEOIP_CONTINENT_CODES = array( 
  1117. "--",  
  1118. "AS",  
  1119. "EU",  
  1120. "EU",  
  1121. "AS",  
  1122. "AS",  
  1123. "NA",  
  1124. "NA",  
  1125. "EU",  
  1126. "AS",  
  1127. "NA",  
  1128. "AF",  
  1129. "AN",  
  1130. "SA",  
  1131. "OC",  
  1132. "EU",  
  1133. "OC",  
  1134. "NA",  
  1135. "AS",  
  1136. "EU",  
  1137. "NA",  
  1138. "AS",  
  1139. "EU",  
  1140. "AF",  
  1141. "EU",  
  1142. "AS",  
  1143. "AF",  
  1144. "AF",  
  1145. "NA",  
  1146. "AS",  
  1147. "SA",  
  1148. "SA",  
  1149. "NA",  
  1150. "AS",  
  1151. "AN",  
  1152. "AF",  
  1153. "EU",  
  1154. "NA",  
  1155. "NA",  
  1156. "AS",  
  1157. "AF",  
  1158. "AF",  
  1159. "AF",  
  1160. "EU",  
  1161. "AF",  
  1162. "OC",  
  1163. "SA",  
  1164. "AF",  
  1165. "AS",  
  1166. "SA",  
  1167. "NA",  
  1168. "NA",  
  1169. "AF",  
  1170. "AS",  
  1171. "AS",  
  1172. "EU",  
  1173. "EU",  
  1174. "AF",  
  1175. "EU",  
  1176. "NA",  
  1177. "NA",  
  1178. "AF",  
  1179. "SA",  
  1180. "EU",  
  1181. "AF",  
  1182. "AF",  
  1183. "AF",  
  1184. "EU",  
  1185. "AF",  
  1186. "EU",  
  1187. "OC",  
  1188. "SA",  
  1189. "OC",  
  1190. "EU",  
  1191. "EU",  
  1192. "NA",  
  1193. "AF",  
  1194. "EU",  
  1195. "NA",  
  1196. "AS",  
  1197. "SA",  
  1198. "AF",  
  1199. "EU",  
  1200. "NA",  
  1201. "AF",  
  1202. "AF",  
  1203. "NA",  
  1204. "AF",  
  1205. "EU",  
  1206. "AN",  
  1207. "NA",  
  1208. "OC",  
  1209. "AF",  
  1210. "SA",  
  1211. "AS",  
  1212. "AN",  
  1213. "NA",  
  1214. "EU",  
  1215. "NA",  
  1216. "EU",  
  1217. "AS",  
  1218. "EU",  
  1219. "AS",  
  1220. "AS",  
  1221. "AS",  
  1222. "AS",  
  1223. "AS",  
  1224. "EU",  
  1225. "EU",  
  1226. "NA",  
  1227. "AS",  
  1228. "AS",  
  1229. "AF",  
  1230. "AS",  
  1231. "AS",  
  1232. "OC",  
  1233. "AF",  
  1234. "NA",  
  1235. "AS",  
  1236. "AS",  
  1237. "AS",  
  1238. "NA",  
  1239. "AS",  
  1240. "AS",  
  1241. "AS",  
  1242. "NA",  
  1243. "EU",  
  1244. "AS",  
  1245. "AF",  
  1246. "AF",  
  1247. "EU",  
  1248. "EU",  
  1249. "EU",  
  1250. "AF",  
  1251. "AF",  
  1252. "EU",  
  1253. "EU",  
  1254. "AF",  
  1255. "OC",  
  1256. "EU",  
  1257. "AF",  
  1258. "AS",  
  1259. "AS",  
  1260. "AS",  
  1261. "OC",  
  1262. "NA",  
  1263. "AF",  
  1264. "NA",  
  1265. "EU",  
  1266. "AF",  
  1267. "AS",  
  1268. "AF",  
  1269. "NA",  
  1270. "AS",  
  1271. "AF",  
  1272. "AF",  
  1273. "OC",  
  1274. "AF",  
  1275. "OC",  
  1276. "AF",  
  1277. "NA",  
  1278. "EU",  
  1279. "EU",  
  1280. "AS",  
  1281. "OC",  
  1282. "OC",  
  1283. "OC",  
  1284. "AS",  
  1285. "NA",  
  1286. "SA",  
  1287. "OC",  
  1288. "OC",  
  1289. "AS",  
  1290. "AS",  
  1291. "EU",  
  1292. "NA",  
  1293. "OC",  
  1294. "NA",  
  1295. "AS",  
  1296. "EU",  
  1297. "OC",  
  1298. "SA",  
  1299. "AS",  
  1300. "AF",  
  1301. "EU",  
  1302. "EU",  
  1303. "AF",  
  1304. "AS",  
  1305. "OC",  
  1306. "AF",  
  1307. "AF",  
  1308. "EU",  
  1309. "AS",  
  1310. "AF",  
  1311. "EU",  
  1312. "EU",  
  1313. "EU",  
  1314. "AF",  
  1315. "EU",  
  1316. "AF",  
  1317. "AF",  
  1318. "SA",  
  1319. "AF",  
  1320. "NA",  
  1321. "AS",  
  1322. "AF",  
  1323. "NA",  
  1324. "AF",  
  1325. "AN",  
  1326. "AF",  
  1327. "AS",  
  1328. "AS",  
  1329. "OC",  
  1330. "AS",  
  1331. "AF",  
  1332. "OC",  
  1333. "AS",  
  1334. "EU",  
  1335. "NA",  
  1336. "OC",  
  1337. "AS",  
  1338. "AF",  
  1339. "EU",  
  1340. "AF",  
  1341. "OC",  
  1342. "NA",  
  1343. "SA",  
  1344. "AS",  
  1345. "EU",  
  1346. "NA",  
  1347. "SA",  
  1348. "NA",  
  1349. "NA",  
  1350. "AS",  
  1351. "OC",  
  1352. "OC",  
  1353. "OC",  
  1354. "AS",  
  1355. "AF",  
  1356. "EU",  
  1357. "AF",  
  1358. "AF",  
  1359. "EU",  
  1360. "AF",  
  1361. "--",  
  1362. "--",  
  1363. "--",  
  1364. "EU",  
  1365. "EU",  
  1366. "EU",  
  1367. "EU",  
  1368. "NA",  
  1369. "NA",  
  1370. "NA",  
  1371. "AF",  
  1372. "--" 
  1373. ); 
  1374.  
  1375. function geoip_load_shared_mem($file) 
  1376. $fp = fopen($file, "rb"); 
  1377. if (!$fp) { 
  1378. print "error opening $file: $php_errormsg\n"; 
  1379. exit; 
  1380. $s_array = fstat($fp); 
  1381. $size = $s_array['size']; 
  1382. if (($shmid = @shmop_open(GEOIP_SHM_KEY, "w", 0, 0))) { 
  1383. shmop_delete($shmid); 
  1384. shmop_close($shmid); 
  1385. $shmid = shmop_open(GEOIP_SHM_KEY, "c", 0644, $size); 
  1386. shmop_write($shmid, fread($fp, $size), 0); 
  1387. shmop_close($shmid); 
  1388.  
  1389. function _setup_segments($gi) 
  1390. $gi->databaseType = GEOIP_COUNTRY_EDITION; 
  1391. $gi->record_length = STANDARD_RECORD_LENGTH; 
  1392. if ($gi->flags & GEOIP_SHARED_MEMORY) { 
  1393. $offset = shmop_size($gi->shmid) - 3; 
  1394. for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) { 
  1395. $delim = shmop_read($gi->shmid, $offset, 3); 
  1396. $offset += 3; 
  1397. if ($delim == (chr(255) . chr(255) . chr(255))) { 
  1398. $gi->databaseType = ord(shmop_read($gi->shmid, $offset, 1)); 
  1399. if ($gi->databaseType >= 106) { 
  1400. $gi->databaseType -= 105; 
  1401. $offset++; 
  1402.  
  1403. if ($gi->databaseType == GEOIP_REGION_EDITION_REV0) { 
  1404. $gi->databaseSegments = GEOIP_STATE_BEGIN_REV0; 
  1405. } elseif ($gi->databaseType == GEOIP_REGION_EDITION_REV1) { 
  1406. $gi->databaseSegments = GEOIP_STATE_BEGIN_REV1; 
  1407. } elseif (($gi->databaseType == GEOIP_CITY_EDITION_REV0) 
  1408. || ($gi->databaseType == GEOIP_CITY_EDITION_REV1) 
  1409. || ($gi->databaseType == GEOIP_ORG_EDITION) 
  1410. || ($gi->databaseType == GEOIP_ORG_EDITION_V6) 
  1411. || ($gi->databaseType == GEOIP_DOMAIN_EDITION) 
  1412. || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6) 
  1413. || ($gi->databaseType == GEOIP_ISP_EDITION) 
  1414. || ($gi->databaseType == GEOIP_ISP_EDITION_V6) 
  1415. || ($gi->databaseType == GEOIP_USERTYPE_EDITION) 
  1416. || ($gi->databaseType == GEOIP_USERTYPE_EDITION_V6) 
  1417. || ($gi->databaseType == GEOIP_LOCATIONA_EDITION) 
  1418. || ($gi->databaseType == GEOIP_ACCURACYRADIUS_EDITION) 
  1419. || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6) 
  1420. || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6) 
  1421. || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1) 
  1422. || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1_V6) 
  1423. || ($gi->databaseType == GEOIP_ASNUM_EDITION) 
  1424. || ($gi->databaseType == GEOIP_ASNUM_EDITION_V6) 
  1425. ) { 
  1426. $gi->databaseSegments = 0; 
  1427. $buf = shmop_read($gi->shmid, $offset, SEGMENT_RECORD_LENGTH); 
  1428. for ($j = 0; $j < SEGMENT_RECORD_LENGTH; $j++) { 
  1429. $gi->databaseSegments += (ord($buf[$j]) << ($j * 8)); 
  1430. if (($gi->databaseType == GEOIP_ORG_EDITION) 
  1431. || ($gi->databaseType == GEOIP_ORG_EDITION_V6) 
  1432. || ($gi->databaseType == GEOIP_DOMAIN_EDITION) 
  1433. || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6) 
  1434. || ($gi->databaseType == GEOIP_ISP_EDITION) 
  1435. || ($gi->databaseType == GEOIP_ISP_EDITION_V6) 
  1436. ) { 
  1437. $gi->record_length = ORG_RECORD_LENGTH; 
  1438. break; 
  1439. } else { 
  1440. $offset -= 4; 
  1441. if (($gi->databaseType == GEOIP_COUNTRY_EDITION) || 
  1442. ($gi->databaseType == GEOIP_COUNTRY_EDITION_V6) || 
  1443. ($gi->databaseType == GEOIP_PROXY_EDITION) || 
  1444. ($gi->databaseType == GEOIP_NETSPEED_EDITION) 
  1445. ) { 
  1446. $gi->databaseSegments = GEOIP_COUNTRY_BEGIN; 
  1447. } else { 
  1448. $filepos = ftell($gi->filehandle); 
  1449. fseek($gi->filehandle, -3, SEEK_END); 
  1450. for ($i = 0; $i < STRUCTURE_INFO_MAX_SIZE; $i++) { 
  1451. $delim = fread($gi->filehandle, 3); 
  1452. if ($delim == (chr(255) . chr(255) . chr(255))) { 
  1453. $gi->databaseType = ord(fread($gi->filehandle, 1)); 
  1454. if ($gi->databaseType >= 106) { 
  1455. $gi->databaseType -= 105; 
  1456. if ($gi->databaseType == GEOIP_REGION_EDITION_REV0) { 
  1457. $gi->databaseSegments = GEOIP_STATE_BEGIN_REV0; 
  1458. } elseif ($gi->databaseType == GEOIP_REGION_EDITION_REV1) { 
  1459. $gi->databaseSegments = GEOIP_STATE_BEGIN_REV1; 
  1460. } elseif (($gi->databaseType == GEOIP_CITY_EDITION_REV0) 
  1461. || ($gi->databaseType == GEOIP_CITY_EDITION_REV1) 
  1462. || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6) 
  1463. || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6) 
  1464. || ($gi->databaseType == GEOIP_ORG_EDITION) 
  1465. || ($gi->databaseType == GEOIP_DOMAIN_EDITION) 
  1466. || ($gi->databaseType == GEOIP_ISP_EDITION) 
  1467. || ($gi->databaseType == GEOIP_ORG_EDITION_V6) 
  1468. || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6) 
  1469. || ($gi->databaseType == GEOIP_ISP_EDITION_V6) 
  1470. || ($gi->databaseType == GEOIP_LOCATIONA_EDITION) 
  1471. || ($gi->databaseType == GEOIP_ACCURACYRADIUS_EDITION) 
  1472. || ($gi->databaseType == GEOIP_CITY_EDITION_REV0_V6) 
  1473. || ($gi->databaseType == GEOIP_CITY_EDITION_REV1_V6) 
  1474. || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1) 
  1475. || ($gi->databaseType == GEOIP_NETSPEED_EDITION_REV1_V6) 
  1476. || ($gi->databaseType == GEOIP_USERTYPE_EDITION) 
  1477. || ($gi->databaseType == GEOIP_USERTYPE_EDITION_V6) 
  1478. || ($gi->databaseType == GEOIP_ASNUM_EDITION) 
  1479. || ($gi->databaseType == GEOIP_ASNUM_EDITION_V6) 
  1480. ) { 
  1481. $gi->databaseSegments = 0; 
  1482.  
  1483. $buf = fread($gi->filehandle, SEGMENT_RECORD_LENGTH); 
  1484. for ($j = 0; $j < SEGMENT_RECORD_LENGTH; $j++) { 
  1485. $gi->databaseSegments += (ord($buf[$j]) << ($j * 8)); 
  1486. if (($gi->databaseType == GEOIP_ORG_EDITION) 
  1487. || ($gi->databaseType == GEOIP_DOMAIN_EDITION) 
  1488. || ($gi->databaseType == GEOIP_ISP_EDITION) 
  1489. || ($gi->databaseType == GEOIP_ORG_EDITION_V6) 
  1490. || ($gi->databaseType == GEOIP_DOMAIN_EDITION_V6) 
  1491. || ($gi->databaseType == GEOIP_ISP_EDITION_V6) 
  1492. ) { 
  1493. $gi->record_length = ORG_RECORD_LENGTH; 
  1494. break; 
  1495. } else { 
  1496. fseek($gi->filehandle, -4, SEEK_CUR); 
  1497. if (($gi->databaseType == GEOIP_COUNTRY_EDITION) || 
  1498. ($gi->databaseType == GEOIP_COUNTRY_EDITION_V6) || 
  1499. ($gi->databaseType == GEOIP_PROXY_EDITION) || 
  1500. ($gi->databaseType == GEOIP_NETSPEED_EDITION) 
  1501. ) { 
  1502. $gi->databaseSegments = GEOIP_COUNTRY_BEGIN; 
  1503. fseek($gi->filehandle, $filepos, SEEK_SET); 
  1504. return $gi; 
  1505.  
  1506. # This should be only used for variable-length records where 
  1507. # $start + $maxLength may be greater than the shared mem size 
  1508. function _sharedMemRead($gi, $start, $maxLength) 
  1509. $readLength = min(shmop_size($gi->shmid) - $start, $maxLength); 
  1510. return shmop_read($gi->shmid, $start, $readLength); 
  1511.  
  1512. function geoip_open($filename, $flags) 
  1513. $gi = new GeoIP; 
  1514. $gi->flags = $flags; 
  1515. if ($gi->flags & GEOIP_SHARED_MEMORY) { 
  1516. $gi->shmid = shmop_open(GEOIP_SHM_KEY, "a", 0, 0); 
  1517. } else { 
  1518. $gi->filehandle = fopen($filename, "rb") or trigger_error("GeoIP API: Can not open $filename\n", E_USER_ERROR); 
  1519. if ($gi->flags & GEOIP_MEMORY_CACHE) { 
  1520. $s_array = fstat($gi->filehandle); 
  1521. $gi->memory_buffer = fread($gi->filehandle, $s_array['size']); 
  1522.  
  1523. $gi = _setup_segments($gi); 
  1524. return $gi; 
  1525.  
  1526. function geoip_close($gi) 
  1527. if ($gi->flags & GEOIP_SHARED_MEMORY) { 
  1528. return true; 
  1529.  
  1530. return fclose($gi->filehandle); 
  1531.  
  1532. function geoip_country_id_by_name_v6($gi, $name) 
  1533. $rec = dns_get_record($name, DNS_AAAA); 
  1534. if (!$rec) { 
  1535. return false; 
  1536. $addr = $rec[0]["ipv6"]; 
  1537. if (!$addr || $addr == $name) { 
  1538. return false; 
  1539. return geoip_country_id_by_addr_v6($gi, $addr); 
  1540.  
  1541. function geoip_country_id_by_name($gi, $name) 
  1542. $addr = gethostbyname($name); 
  1543. if (!$addr || $addr == $name) { 
  1544. return false; 
  1545. return geoip_country_id_by_addr($gi, $addr); 
  1546.  
  1547. function geoip_country_code_by_name_v6($gi, $name) 
  1548. $country_id = geoip_country_id_by_name_v6($gi, $name); 
  1549. if ($country_id !== false) { 
  1550. return $gi->GEOIP_COUNTRY_CODES[$country_id]; 
  1551. return false; 
  1552.  
  1553. function geoip_country_code_by_name($gi, $name) 
  1554. $country_id = geoip_country_id_by_name($gi, $name); 
  1555. if ($country_id !== false) { 
  1556. return $gi->GEOIP_COUNTRY_CODES[$country_id]; 
  1557. return false; 
  1558.  
  1559. function geoip_country_name_by_name_v6($gi, $name) 
  1560. $country_id = geoip_country_id_by_name_v6($gi, $name); 
  1561. if ($country_id !== false) { 
  1562. return $gi->GEOIP_COUNTRY_NAMES[$country_id]; 
  1563. return false; 
  1564.  
  1565. function geoip_country_name_by_name($gi, $name) 
  1566. $country_id = geoip_country_id_by_name($gi, $name); 
  1567. if ($country_id !== false) { 
  1568. return $gi->GEOIP_COUNTRY_NAMES[$country_id]; 
  1569. return false; 
  1570.  
  1571. function geoip_country_id_by_addr_v6($gi, $addr) 
  1572. $ipnum = inet_pton($addr); 
  1573. return _geoip_seek_country_v6($gi, $ipnum) - GEOIP_COUNTRY_BEGIN; 
  1574.  
  1575. function geoip_country_id_by_addr($gi, $addr) 
  1576. $ipnum = ip2long($addr); 
  1577. return _geoip_seek_country($gi, $ipnum) - GEOIP_COUNTRY_BEGIN; 
  1578.  
  1579. function geoip_country_code_by_addr_v6($gi, $addr) 
  1580. $country_id = geoip_country_id_by_addr_v6($gi, $addr); 
  1581. if ($country_id !== false) { 
  1582. return $gi->GEOIP_COUNTRY_CODES[$country_id]; 
  1583. return false; 
  1584.  
  1585. function geoip_country_code_by_addr($gi, $addr) 
  1586. if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) { 
  1587. $record = GeoIP_record_by_addr($gi, $addr); 
  1588. if ($record) { 
  1589. return $record->country_code; 
  1590. } else { 
  1591. $country_id = geoip_country_id_by_addr($gi, $addr); 
  1592. if ($country_id !== false) { 
  1593. return $gi->GEOIP_COUNTRY_CODES[$country_id]; 
  1594. return false; 
  1595.  
  1596. function geoip_country_name_by_addr_v6($gi, $addr) 
  1597. $country_id = geoip_country_id_by_addr_v6($gi, $addr); 
  1598. if ($country_id !== false) { 
  1599. return $gi->GEOIP_COUNTRY_NAMES[$country_id]; 
  1600. return false; 
  1601.  
  1602. function geoip_country_name_by_addr($gi, $addr) 
  1603. if ($gi->databaseType == GEOIP_CITY_EDITION_REV1) { 
  1604. $record = GeoIP_record_by_addr($gi, $addr); 
  1605. return $record->country_name; 
  1606. } else { 
  1607. $country_id = geoip_country_id_by_addr($gi, $addr); 
  1608. if ($country_id !== false) { 
  1609. return $gi->GEOIP_COUNTRY_NAMES[$country_id]; 
  1610. return false; 
  1611.  
  1612. function _geoip_seek_country_v6($gi, $ipnum) 
  1613. # arrays from unpack start with offset 1 
  1614. # yet another php mystery. array_merge work around 
  1615. # this broken behaviour 
  1616. $v6vec = array_merge(unpack("C16", $ipnum)); 
  1617.  
  1618. $offset = 0; 
  1619. for ($depth = 127; $depth >= 0; --$depth) { 
  1620. if ($gi->flags & GEOIP_MEMORY_CACHE) { 
  1621. $buf = _safe_substr( 
  1622. $gi->memory_buffer,  
  1623. 2 * $gi->record_length * $offset,  
  1624. 2 * $gi->record_length 
  1625. ); 
  1626. } elseif ($gi->flags & GEOIP_SHARED_MEMORY) { 
  1627. $buf = _sharedMemRead($gi,  
  1628. 2 * $gi->record_length * $offset,  
  1629. 2 * $gi->record_length 
  1630. ); 
  1631. } else { 
  1632. fseek($gi->filehandle, 2 * $gi->record_length * $offset, SEEK_SET) == 0 
  1633. or trigger_error("GeoIP API: fseek failed", E_USER_ERROR); 
  1634. $buf = fread($gi->filehandle, 2 * $gi->record_length); 
  1635. $x = array(0, 0); 
  1636. for ($i = 0; $i < 2; ++$i) { 
  1637. for ($j = 0; $j < $gi->record_length; ++$j) { 
  1638. $x[$i] += ord($buf[$gi->record_length * $i + $j]) << ($j * 8); 
  1639.  
  1640. $bnum = 127 - $depth; 
  1641. $idx = $bnum >> 3; 
  1642. $b_mask = 1 << ($bnum & 7 ^ 7); 
  1643. if (($v6vec[$idx] & $b_mask) > 0) { 
  1644. if ($x[1] >= $gi->databaseSegments) { 
  1645. return $x[1]; 
  1646. $offset = $x[1]; 
  1647. } else { 
  1648. if ($x[0] >= $gi->databaseSegments) { 
  1649. return $x[0]; 
  1650. $offset = $x[0]; 
  1651. trigger_error("GeoIP API: Error traversing database - perhaps it is corrupt?", E_USER_ERROR); 
  1652. return false; 
  1653.  
  1654. function _geoip_seek_country($gi, $ipnum) 
  1655. $offset = 0; 
  1656. for ($depth = 31; $depth >= 0; --$depth) { 
  1657. if ($gi->flags & GEOIP_MEMORY_CACHE) { 
  1658. $buf = _safe_substr( 
  1659. $gi->memory_buffer,  
  1660. 2 * $gi->record_length * $offset,  
  1661. 2 * $gi->record_length 
  1662. ); 
  1663. } elseif ($gi->flags & GEOIP_SHARED_MEMORY) { 
  1664. $buf = _sharedMemRead( 
  1665. $gi,  
  1666. 2 * $gi->record_length * $offset,  
  1667. 2 * $gi->record_length 
  1668. ); 
  1669. } else { 
  1670. fseek($gi->filehandle, 2 * $gi->record_length * $offset, SEEK_SET) == 0 
  1671. or trigger_error("GeoIP API: fseek failed", E_USER_ERROR); 
  1672. $buf = fread($gi->filehandle, 2 * $gi->record_length); 
  1673. $x = array(0, 0); 
  1674. for ($i = 0; $i < 2; ++$i) { 
  1675. for ($j = 0; $j < $gi->record_length; ++$j) { 
  1676. $x[$i] += ord($buf[$gi->record_length * $i + $j]) << ($j * 8); 
  1677. if ($ipnum & (1 << $depth)) { 
  1678. if ($x[1] >= $gi->databaseSegments) { 
  1679. return $x[1]; 
  1680. $offset = $x[1]; 
  1681. } else { 
  1682. if ($x[0] >= $gi->databaseSegments) { 
  1683. return $x[0]; 
  1684. $offset = $x[0]; 
  1685. trigger_error("GeoIP API: Error traversing database - perhaps it is corrupt?", E_USER_ERROR); 
  1686. return false; 
  1687.  
  1688. function _common_get_org($gi, $seek_org) 
  1689. $record_pointer = $seek_org + (2 * $gi->record_length - 1) * $gi->databaseSegments; 
  1690. if ($gi->flags & GEOIP_SHARED_MEMORY) { 
  1691. $org_buf = _sharedMemRead($gi, $record_pointer, MAX_ORG_RECORD_LENGTH); 
  1692. } else { 
  1693. fseek($gi->filehandle, $record_pointer, SEEK_SET); 
  1694. $org_buf = fread($gi->filehandle, MAX_ORG_RECORD_LENGTH); 
  1695. $org_buf = _safe_substr($org_buf, 0, strpos($org_buf, "\0")); 
  1696. return $org_buf; 
  1697.  
  1698. function _get_org_v6($gi, $ipnum) 
  1699. $seek_org = _geoip_seek_country_v6($gi, $ipnum); 
  1700. if ($seek_org == $gi->databaseSegments) { 
  1701. return null; 
  1702. return _common_get_org($gi, $seek_org); 
  1703.  
  1704. function _get_org($gi, $ipnum) 
  1705. $seek_org = _geoip_seek_country($gi, $ipnum); 
  1706. if ($seek_org == $gi->databaseSegments) { 
  1707. return null; 
  1708. return _common_get_org($gi, $seek_org); 
  1709.  
  1710.  
  1711. function geoip_name_by_addr_v6($gi, $addr) 
  1712. if ($addr == null) { 
  1713. return 0; 
  1714. $ipnum = inet_pton($addr); 
  1715. return _get_org_v6($gi, $ipnum); 
  1716.  
  1717. function geoip_name_by_addr($gi, $addr) 
  1718. if ($addr == null) { 
  1719. return 0; 
  1720. $ipnum = ip2long($addr); 
  1721. return _get_org($gi, $ipnum); 
  1722.  
  1723. function geoip_org_by_addr($gi, $addr) 
  1724. return geoip_name_by_addr($gi, $addr); 
  1725.  
  1726. function _get_region($gi, $ipnum) 
  1727. if ($gi->databaseType == GEOIP_REGION_EDITION_REV0) { 
  1728. $seek_region = _geoip_seek_country($gi, $ipnum) - GEOIP_STATE_BEGIN_REV0; 
  1729. if ($seek_region >= 1000) { 
  1730. $country_code = "US"; 
  1731. $region = chr(($seek_region - 1000) / 26 + 65) . chr(($seek_region - 1000) % 26 + 65); 
  1732. } else { 
  1733. $country_code = $gi->GEOIP_COUNTRY_CODES[$seek_region]; 
  1734. $region = ""; 
  1735. return array($country_code, $region); 
  1736. } elseif ($gi->databaseType == GEOIP_REGION_EDITION_REV1) { 
  1737. $seek_region = _geoip_seek_country($gi, $ipnum) - GEOIP_STATE_BEGIN_REV1; 
  1738. if ($seek_region < US_OFFSET) { 
  1739. $country_code = ""; 
  1740. $region = ""; 
  1741. } elseif ($seek_region < CANADA_OFFSET) { 
  1742. $country_code = "US"; 
  1743. $region = chr(($seek_region - US_OFFSET) / 26 + 65) . chr(($seek_region - US_OFFSET) % 26 + 65); 
  1744. } elseif ($seek_region < WORLD_OFFSET) { 
  1745. $country_code = "CA"; 
  1746. $region = chr(($seek_region - CANADA_OFFSET) / 26 + 65) . chr(($seek_region - CANADA_OFFSET) % 26 + 65); 
  1747. } else { 
  1748. $country_code = $gi->GEOIP_COUNTRY_CODES[(int) (($seek_region - WORLD_OFFSET) / FIPS_RANGE)]; 
  1749. $region = ""; 
  1750. return array($country_code, $region); 
  1751. return null; 
  1752.  
  1753. function geoip_region_by_addr($gi, $addr) 
  1754. if ($addr == null) { 
  1755. return 0; 
  1756. $ipnum = ip2long($addr); 
  1757. return _get_region($gi, $ipnum); 
  1758.  
  1759. function _safe_substr($string, $start, $length) 
  1760. // workaround php's broken substr, strpos, etc handling with 
  1761. // mbstring.func_overload and mbstring.internal_encoding 
  1762. $mbExists = extension_loaded('mbstring'); 
  1763.  
  1764. if ($mbExists) { 
  1765. $enc = mb_internal_encoding(); 
  1766. mb_internal_encoding('ISO-8859-1'); 
  1767.  
  1768. $buf = substr($string, $start, $length); 
  1769.  
  1770. if ($mbExists) { 
  1771. mb_internal_encoding($enc); 
  1772.  
  1773. return $buf; 
.