Fix: geoip works with debian geoip package

This commit is contained in:
Laurent Destailleur 2012-01-15 02:39:43 +01:00
parent 301c66210a
commit f75329faef
3 changed files with 49 additions and 22 deletions

View File

@ -1,5 +1,5 @@
<?php <?php
/* Copyright (C) 2009 Laurent Destailleur <eldy@users.sourceforge.org> /* Copyright (C) 2009-2012 Laurent Destailleur <eldy@users.sourceforge.org>
* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es> * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -129,20 +129,27 @@ print $langs->trans("YouCanDownloadAdvancedDatFileTo",'<a href="'.$url2.'" targe
if ($geoip) if ($geoip)
{ {
print '<br><br>'; print '<br><br>';
print '<br>'; print '<br>'.$langs->trans("TestGeoIPResult",$ip).':';
$ip='24.24.24.24'; $ip='24.24.24.24';
print $langs->trans("TestGeoIPResult",$ip).':<br>'; print '<br>'.$ip.' -> ';
print $ip.' -> ';
$result=dol_print_ip($ip,1); $result=dol_print_ip($ip,1);
if ($result) print $result; if ($result) print $result;
else print $langs->trans("Error"); else print $langs->trans("Error");
/* We disable this test because dol_print_ip need an ip as input
$ip='www.google.com';
print '<br>'.$ip.' -> ';
$result=dol_print_ip($ip,1);
if ($result) print $result;
else print $langs->trans("Error");
*/
$geoip->close(); $geoip->close();
} }
dol_htmloutput_mesg($mesg); dol_htmloutput_mesg($mesg);
$db->close();
llxFooter(); llxFooter();
$db->close();
?> ?>

View File

@ -1,5 +1,5 @@
<?php <?php
/* Copyright (C) 2009 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2009-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -19,17 +19,17 @@
/** /**
* \file htdocs/core/class/dolgeoip.class.php * \file htdocs/core/class/dolgeoip.class.php
* \ingroup geoip * \ingroup geoip
* \brief Library for managing module geoip * \brief File of class to manage module geoip
*/ */
/** /**
* \class DolGeoIP * \class DolGeoIP
* \brief Classe to manage GeoIP * \brief Classe to manage GeoIP
* \remarks Usage: * Usage:
* \remarks $geoip=new GeoIP('country',$datfile); * $geoip=new GeoIP('country',$datfile);
* \remarks $geoip->getCountryCodeFromIP($ip); * $geoip->getCountryCodeFromIP($ip);
* \remarks $geoip->close(); * $geoip->close();
*/ */
class DolGeoIP class DolGeoIP
{ {
@ -56,6 +56,7 @@ class DolGeoIP
} }
else { print 'ErrorBadParameterInConstructor'; return 0; } else { print 'ErrorBadParameterInConstructor'; return 0; }
// Here, function exists (embedded into PHP or exists because we made include)
if (empty($type) || empty($datfile)) if (empty($type) || empty($datfile))
{ {
//dol_syslog("DolGeoIP::DolGeoIP parameter datafile not defined", LOG_ERR); //dol_syslog("DolGeoIP::DolGeoIP parameter datafile not defined", LOG_ERR);
@ -73,8 +74,17 @@ class DolGeoIP
return 0; return 0;
} }
if (function_exists('geoip_open'))
{
$this->gi = geoip_open($datfile,GEOIP_STANDARD); $this->gi = geoip_open($datfile,GEOIP_STANDARD);
} }
else
{
$this->gi = 'NOGI'; // We are using embedded php geoip functions
//print 'function_exists(geoip_country_code_by_name))='.function_exists('geoip_country_code_by_name');
//print geoip_database_info();
}
}
/** /**
* Return in lower case the country code from an ip * Return in lower case the country code from an ip
@ -88,8 +98,17 @@ class DolGeoIP
{ {
return ''; return '';
} }
if ($this->gi == 'NOGI')
{
// geoip_country_code_by_addr does not exists
return strtolower(geoip_country_code_by_name($ip));
}
else
{
if (! function_exists('geoip_country_code_by_addr')) return strtolower(geoip_country_code_by_name($this->gi, $ip));
return strtolower(geoip_country_code_by_addr($this->gi, $ip)); return strtolower(geoip_country_code_by_addr($this->gi, $ip));
} }
}
/** /**
* Return in lower case the country code from a host name * Return in lower case the country code from a host name
@ -113,6 +132,7 @@ class DolGeoIP
*/ */
function getVersion() function getVersion()
{ {
if ($this->gi == 'NOGI') return geoip_database_info();
return ''; return '';
} }

View File

@ -1334,7 +1334,7 @@ function dol_print_phone($phone,$country="FR",$cid=0,$socid=0,$addlink=0,$separ=
* Return an IP formated to be shown on screen * Return an IP formated to be shown on screen
* *
* @param string $ip IP * @param string $ip IP
* @param int $mode 1=return only country/flag,2=return only IP * @param int $mode 0=return IP + country/flag, 1=return only country/flag, 2=return only IP
* @return string Formated IP, with country if GeoIP module is enabled * @return string Formated IP, with country if GeoIP module is enabled
*/ */
function dol_print_ip($ip,$mode=0) function dol_print_ip($ip,$mode=0)