dolgeoip.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <https://www.gnu.org/licenses/>.
  16. * or see https://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. public $gi;
  34. /**
  35. * Constructor
  36. *
  37. * @param string $type 'country' or 'city'
  38. * @param string $datfile Data file
  39. */
  40. public function __construct($type, $datfile)
  41. {
  42. global $conf;
  43. $geoipversion = '2'; // 'php', or '2'
  44. if (!empty($conf->global->GEOIP_VERSION)) {
  45. $geoipversion = $conf->global->GEOIP_VERSION;
  46. }
  47. if ($type == 'country') {
  48. // geoip may have been already included with PEAR
  49. if ($geoipversion == '2' || ($geoipversion != 'php' && !function_exists('geoip_country_code_by_name'))) {
  50. require_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
  51. }
  52. } elseif ($type == 'city') {
  53. // geoip may have been already included with PEAR
  54. if ($geoipversion == '2' || ($geoipversion != 'php' && !function_exists('geoip_country_code_by_name'))) {
  55. require_once DOL_DOCUMENT_ROOT.'/includes/geoip2/geoip2.phar';
  56. }
  57. } else {
  58. print 'ErrorBadParameterInConstructor';
  59. return 0;
  60. }
  61. // Here, function exists (embedded into PHP or exists because we made include)
  62. if (empty($type) || empty($datfile)) {
  63. $this->errorlabel = 'Constructor was called with no datafile parameter';
  64. dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
  65. return 0;
  66. }
  67. if (!file_exists($datfile) || !is_readable($datfile)) {
  68. $this->error = 'ErrorGeoIPClassNotInitialized';
  69. $this->errorlabel = "Datafile ".$datfile." not found";
  70. dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
  71. return 0;
  72. }
  73. if ($geoipversion == '2') {
  74. try {
  75. $this->gi = new GeoIp2\Database\Reader($datfile); // '/usr/local/share/GeoIP/GeoIP2-City.mmdb'
  76. } catch (Exception $e) {
  77. $this->error = $e->getMessage();
  78. dol_syslog('DolGeoIP '.$this->errorlabel, LOG_ERR);
  79. return 0;
  80. }
  81. } elseif (function_exists('geoip_open') && defined('GEOIP_STANDARD')) {
  82. $this->gi = geoip_open($datfile, constant('GEOIP_STANDARD'));
  83. } elseif (function_exists('geoip_country_code_by_name')) {
  84. $this->gi = 'NOGI'; // We are using embedded php geoip functions
  85. //print 'function_exists(geoip_country_code_by_name))='.function_exists('geoip_country_code_by_name');
  86. //print geoip_database_info();
  87. } else {
  88. $this->gi = ''; // For avoid error
  89. }
  90. }
  91. /**
  92. * Return in lower case the country code from an ip
  93. *
  94. * @param string $ip IP to scan
  95. * @return string Country code (two letters)
  96. */
  97. public function getCountryCodeFromIP($ip)
  98. {
  99. global $conf;
  100. $geoipversion = '2'; // 'php', or '2'
  101. if (!empty($conf->global->GEOIP_VERSION)) {
  102. $geoipversion = $conf->global->GEOIP_VERSION;
  103. }
  104. if (empty($this->gi)) {
  105. return '';
  106. }
  107. if ($this->gi == 'NOGI') {
  108. // geoip_country_code_by_addr does not exists
  109. return strtolower(geoip_country_code_by_name($ip));
  110. } else {
  111. if (preg_match('/^[0-9]+.[0-9]+\.[0-9]+\.[0-9]+/', $ip)) {
  112. if ($geoipversion == '2') {
  113. try {
  114. $record = $this->gi->country($ip);
  115. return strtolower($record->country->isoCode);
  116. } catch (Exception $e) {
  117. //return $e->getMessage();
  118. return '';
  119. }
  120. } else {
  121. if (!function_exists('geoip_country_code_by_addr')) {
  122. return strtolower(geoip_country_code_by_name($ip));
  123. }
  124. return strtolower(geoip_country_code_by_addr($this->gi, $ip));
  125. }
  126. } else {
  127. if ($geoipversion == '2') {
  128. try {
  129. $record = $this->gi->country($ip);
  130. return strtolower($record->country->isoCode);
  131. } catch (Exception $e) {
  132. //return $e->getMessage();
  133. return '';
  134. }
  135. } else {
  136. if (!function_exists('geoip_country_code_by_addr_v6')) {
  137. return strtolower(geoip_country_code_by_name_v6($this->gi, $ip));
  138. }
  139. return strtolower(geoip_country_code_by_addr_v6($this->gi, $ip));
  140. }
  141. }
  142. }
  143. }
  144. /**
  145. * Return in lower case the country code from a host name
  146. *
  147. * @param string $name FQN of host (example: myserver.xyz.com)
  148. * @return string Country code (two letters)
  149. */
  150. public function getCountryCodeFromName($name)
  151. {
  152. global $conf;
  153. $geoipversion = '2'; // 'php', or '2'
  154. if (!empty($conf->global->GEOIP_VERSION)) {
  155. $geoipversion = $conf->global->GEOIP_VERSION;
  156. }
  157. if (empty($this->gi)) {
  158. return '';
  159. }
  160. if ($geoipversion == '2') {
  161. try {
  162. $record = $this->gi->country($name);
  163. return $record->country->isoCode;
  164. } catch (Exception $e) {
  165. //return $e->getMessage();
  166. return '';
  167. }
  168. } else {
  169. return geoip_country_code_by_name($this->gi, $name);
  170. }
  171. }
  172. /**
  173. * Return verion of data file
  174. *
  175. * @return string Version of datafile
  176. */
  177. public function getVersion()
  178. {
  179. global $conf;
  180. $geoipversion = '2'; // 'php', or '2'
  181. if (!empty($conf->global->GEOIP_VERSION)) {
  182. $geoipversion = $conf->global->GEOIP_VERSION;
  183. }
  184. if ($geoipversion == 'php') {
  185. if ($this->gi == 'NOGI') {
  186. return geoip_database_info();
  187. } else {
  188. return 'geoip_database_info() function not available';
  189. }
  190. }
  191. return 'Not available (not using PHP internal geo functions - We are using embedded Geoip v'.$geoipversion.')';
  192. }
  193. /**
  194. * Close geoip object
  195. *
  196. * @return void
  197. */
  198. public function close()
  199. {
  200. if (function_exists('geoip_close')) {
  201. // With some geoip with PEAR, geoip_close function may not exists
  202. geoip_close($this->gi);
  203. }
  204. }
  205. }