PageRenderTime 157ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/class/dolgeoip.class.php

https://github.com/asterix14/dolibarr
PHP | 125 lines | 56 code | 10 blank | 59 comment | 13 complexity | f9018176b95929016d0054adbbdbd9ba MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2009 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 2 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 Library for managing module geoip
  22. */
  23. /**
  24. * \class DolGeoIP
  25. * \brief Classe to manage GeoIP
  26. * \remarks Usage:
  27. * \remarks $geoip=new GeoIP('country',$datfile);
  28. * \remarks $geoip->getCountryCodeFromIP($ip);
  29. * \remarks $geoip->close();
  30. */
  31. class DolGeoIP
  32. {
  33. var $gi;
  34. /**
  35. * Constructor
  36. *
  37. * @param $type 'country' or 'city'
  38. * @param $datfile Data file
  39. * @return GeoIP
  40. */
  41. function DolGeoIP($type,$datfile)
  42. {
  43. if ($type == 'country')
  44. {
  45. // geoip may have been already included with PEAR
  46. if (! function_exists('geoip_country_code_by_name')) $res=include_once(GEOIP_PATH."geoip.inc");
  47. }
  48. else if ($type == 'city')
  49. {
  50. // geoip may have been already included with PEAR
  51. if (! function_exists('geoip_country_code_by_name')) $res=include_once(GEOIP_PATH."geoipcity.inc");
  52. }
  53. else { print 'ErrorBadParameterInConstructor'; return 0; }
  54. if (empty($type) || empty($datfile))
  55. {
  56. //dol_syslog("DolGeoIP::DolGeoIP parameter datafile not defined", LOG_ERR);
  57. $this->errorlabel='DolGeoIP constructor was called with no datafile parameter';
  58. //dol_print_error('','DolGeoIP constructor was called with no datafile parameter');
  59. print $this->errorlabel;
  60. return 0;
  61. }
  62. if (! file_exists($datfile))
  63. {
  64. //dol_syslog("DolGeoIP::DolGeoIP datafile ".$datfile." can not be read", LOG_ERR);
  65. $this->error='ErrorGeoIPClassNotInitialized';
  66. $this->errorlabel="Datafile ".$datfile." not found";
  67. print $this->errorlabel;
  68. return 0;
  69. }
  70. $this->gi = geoip_open($datfile,GEOIP_STANDARD);
  71. }
  72. /**
  73. * Return in lower case the country code from an ip
  74. *
  75. * @param $ip IP to scan
  76. * @return string Country code (two letters)
  77. */
  78. function getCountryCodeFromIP($ip)
  79. {
  80. if (empty($this->gi))
  81. {
  82. return '';
  83. }
  84. return strtolower(geoip_country_code_by_addr($this->gi, $ip));
  85. }
  86. /**
  87. * Return in lower case the country code from a host name
  88. *
  89. * @param $name FQN of host (example: myserver.xyz.com)
  90. * @return string Country code (two letters)
  91. */
  92. function getCountryCodeFromName($name)
  93. {
  94. if (empty($this->gi))
  95. {
  96. return '';
  97. }
  98. return geoip_country_code_by_name($this->gi, $name);
  99. }
  100. /**
  101. * Return verion of data file
  102. */
  103. function getVersion()
  104. {
  105. return '';
  106. }
  107. /**
  108. * Close geoip object
  109. */
  110. function close()
  111. {
  112. geoip_close($this->gi);
  113. }
  114. }
  115. ?>