n class="time-since" title="Sun, 18 Jul 2021 17:39:38 UTC">5 anos atrás 3394_fsm a4345e5759 first commit 5 anos atrás 3394_vm a4345e5759 first commit 5 anos atrás 3395 a4345e5759 first commit 5 anos atrás 3429 a4345e5759 first commit 5 anos atrás 3430 a4345e5759 first commit 5 anos atrás 3431 a4345e5759 first commit 5 anos atrás 3433 a4345e5759 first commit 5 anos atrás 3439 a4345e5759 first commit 5 anos atrás 3440 a4345e5759 first commit 5 anos atrás 3455 a4345e5759 first commit 5 anos atrás 3456 a4345e5759 first commit 5 anos atrás 3456_fsm a4345e5759 first commit 5 anos atrás 3456_vm a4345e5759 first commit 5 anos atrás 3466 a4345e5759 first commit 5 anos atrás 3467 a4345e5759 first commit 5 anos atrás 3468 a4345e5759 first commit 5 anos atrás 3501 a4345e5759 first commit 5 anos atrás 3502 a4345e5759 first commit 5 anos atrás 3503 a4345e5759 first commit 5 anos atrás 3534 a4345e5759 first commit 5 anos atrás 3541 a4345e5759 first commit 5 anos atrás 3541_fsm a4345e5759 first commit 5 anos atrás 3541_vm a4345e5759 first commit 5 anos atrás 3542 a4345e5759 first commit 5 anos atrás 3574 a4345e5759 first commit 5 anos atrás 3575 a4345e5759 first commit 5 anos atrás 3576 a4345e5759 first commit 5 anos atrás 3596 a4345e5759 first commit 5 anos atrás 3597 a4345e5759 first commit 5 anos atrás 3598 a4345e5759 first commit 5 anos atrás 3599 a4345e5759 first commit 5 anos atrás 3600 a4345e5759 first commit 5 anos atrás 3600_fsm a4345e5759 first commit 5 anos atrás 3600_vm a4345e5759 first commit 5 anos atrás 3601 a4345e5759 first commit 5 anos atrás 3601_fsm a4345e5759 first commit 5 anos atrás 3601_vm a4345e5759 first commit 5 anos atrás 3602 a4345e5759 first commit 5 anos atrás 3602_fsm a4345e5759 first commit 5 anos atrás 3602_vm a4345e5759 first commit 5 anos atrás 3603 a4345e5759 first commit 5 anos atrás 3603_fsm a4345e5759 first commit 5 anos atrás 3603_vm a4345e5759 first commit 5 anos atrás 3604 a4345e5759 first commit 5 anos atrás 3605 a4345e5759 first commit 5 anos atrás 3606 a4345e5759 first commit 5 anos atrás 3607 a4345e5759 first commit 5 anos atrás 3608 a4345e5759 first commit 5 anos atrás 3609 a4345e5759 first commit 5 anos atrás 3712 a4345e5759 first commit 5 anos atrás 3764 a4345e5759 first commit 5 anos atrás 3764_fsm a4345e5759 first commit 5 anos atrás 3764_vm a4345e5759 first commit 5 anos atrás 3766 a4345e5759 first commit 5 anos atrás 3767 a4345e5759 first commit 5 anos atrás 3997 a4345e5759 first commit 5 anos atrás 4143 a4345e5759 first commit 5 anos atrás 4144 a4345e5759 first commit 5 anos atrás 4145 a4345e5759 first commit 5 anos atrás 4146 a4345e5759 first commit 5 anos atrás 4147 a4345e5759 first commit 5 anos atrás 4148 a4345e5759 first commit 5 anos atrás 4149 a4345e5759 first commit 5 anos atrás 4150 a4345e5759 first commit 5 anos atrás 4151 a4345e5759 first commit 5 anos atrás 4152 a4345e5759 first commit 5 anos atrás 4153 a4345e5759 first commit 5 anos atrás 4154 a4345e5759 first commit 5 anos atrás 4155 a4345e5759 first commit 5 anos atrás 4156 a4345e5759 first commit 5 anos atrás 4157 a4345e5759 first commit 5 anos atrás 4158 a4345e5759 first commit 5 anos atrás 4159 a4345e5759 first commit 5 anos atrás 4160 a4345e5759 first commit 5 anos atrás 4161 a4345e5759 first commit 5 anos atrás 4162 a4345e5759 first commit 5 anos atrás 4163 a4345e5759 first commit 5 anos atrás 4164 a4345e5759 first commit 5 anos atrás 4165 a4345e5759 first commit 5 anos atrás 4166 a4345e5759 first commit 5 anos atrás 4167 a4345e5759 first commit 5 anos atrás 4168 a4345e5759 first commit 5 anos atrás 4169 a4345e5759 first commit 5 anos atrás 4170 a4345e5759 first commit 5 anos atrás 4171 a4345e5759 first commit 5 anos atrás 4172 a4345e5759 first commit 5 anos atrás 4173 a4345e5759 first commit 5 anos atrás 4174 a4345e5759 first commit 5 anos atrás 5002 a4345e5759 first commit 5 anos atrás 548 a4345e5759 first commit 5 anos atrás 549 a4345e5759 first commit 5 anos atrás 6102 a4345e5759 first commit 5 anos atrás 6104 a4345e5759 first commit 5 anos atrás 6106 a4345e5759 first commit 5 anos atrás 6110 a4345e5759 first commit 5 anos atrás 6111 a4345e5759 first commit 5 anos atrás 6112 a4345e5759 first commit 5 anos atrás 6113 a4345e5759 first commit 5 anos atrás 6117 a4345e5759 first commit 5 anos atrás 826 a4345e5759 first commit 5 anos atrás 827 a4345e5759 first commit 5 anos atrás 828 a4345e5759 first commit 5 anos atrás PG_VERSION a4345e5759 first commit 5 anos atrás pg_filenode.map a4345e5759 first commit 5 anos atrás tum/whitesports - Gogs: Simplico Git Service

Aucune description

class-wc-geo-ip.php 30KB

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