dolgeoip.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /* Copyright (C) 2009-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. * or see http://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/class/dolgeoip.class.php
  20. * \ingroup geoip
  21. * \brief File of class to manage module geoip
  22. */
  23. /**
  24. * \class DolGeoIP
  25. * \brief Classe to manage GeoIP
  26. * Usage:
  27. * $geoip=new GeoIP('country',$datfile);
  28. * $geoip->getCountryCodeFromIP($ip);
  29. * $geoip->close();
  30. */
  31. class DolGeoIP
  32. {
  33. var $gi;
  34. /**
  35. * Constructor
  36. *
  37. * @param string $type 'country' or 'city'
  38. * @param string $datfile Data file
  39. */
  40. function __construct($type,$datfile)
  41. {
  42. if ($type == 'country')
  43. {
  44. // geoip may have been already included with PEAR
  45. if (! function_exists('geoip_country_code_by_name')) $res=include_once GEOIP_PATH.'geoip.inc';
  46. }
  47. else if ($type == 'city')
  48. {
  49. // geoip may have been already included with PEAR
  50. if (! function_exists('geoip_country_code_by_name')) $res=include_once GEOIP_PATH.'geoipcity.inc';
  51. }
  52. else { print 'ErrorBadParameterInConstructor'; return 0; }
  53. // Here, function exists (embedded into PHP or exists because we made include)
  54. if (empty($type) || empty($datfile))
  55. {
  56. $this->errorlabel='Constructor was called with no datafile parameter';
  57. dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
  58. return 0;
  59. }
  60. if (! file_exists($datfile) || ! is_readable($datfile))
  61. {
  62. $this->error='ErrorGeoIPClassNotInitialized';
  63. $this->errorlabel="Datafile ".$datfile." not found";
  64. dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
  65. return 0;
  66. }
  67. if (function_exists('geoip_open'))
  68. {
  69. $this->gi = geoip_open($datfile,GEOIP_STANDARD);
  70. }
  71. else
  72. {
  73. $this->gi = 'NOGI'; // We are using embedded php geoip functions
  74. //print 'function_exists(geoip_country_code_by_name))='.function_exists('geoip_country_code_by_name');
  75. //print geoip_database_info();
  76. }
  77. }
  78. /**
  79. * Return in lower case the country code from an ip
  80. *
  81. * @param string $ip IP to scan
  82. * @return string Country code (two letters)
  83. */
  84. function getCountryCodeFromIP($ip)
  85. {
  86. if (empty($this->gi))
  87. {
  88. return '';
  89. }
  90. if ($this->gi == 'NOGI')
  91. {
  92. // geoip_country_code_by_addr does not exists
  93. return strtolower(geoip_country_code_by_name($ip));
  94. }
  95. else
  96. {
  97. if (! function_exists('geoip_country_code_by_addr')) return strtolower(geoip_country_code_by_name($this->gi, $ip));
  98. return strtolower(geoip_country_code_by_addr($this->gi, $ip));
  99. }
  100. }
  101. /**
  102. * Return in lower case the country code from a host name
  103. *
  104. * @param string $name FQN of host (example: myserver.xyz.com)
  105. * @return string Country code (two letters)
  106. */
  107. function getCountryCodeFromName($name)
  108. {
  109. if (empty($this->gi))
  110. {
  111. return '';
  112. }
  113. return geoip_country_code_by_name($this->gi, $name);
  114. }
  115. /**
  116. * Return verion of data file
  117. *
  118. * @return string Version of datafile
  119. */
  120. function getVersion()
  121. {
  122. if ($this->gi == 'NOGI') return geoip_database_info();
  123. return 'Not available (not using PHP internal geo functions)';
  124. }
  125. /**
  126. * Close geoip object
  127. *
  128. * @return void
  129. */
  130. function close()
  131. {
  132. if (function_exists('geoip_close')) // With some geoip with PEAR, geoip_close function may not exists
  133. {
  134. geoip_close($this->gi);
  135. }
  136. }
  137. }