From 5574a296c4dbfd178d0a9ca883dde094ac243ed7 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 3 Jul 2011 18:31:13 +0000 Subject: [PATCH] Add function to return user country --- htdocs/lib/functions.lib.php | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/htdocs/lib/functions.lib.php b/htdocs/lib/functions.lib.php index 71561d53c57..69b57e0336f 100644 --- a/htdocs/lib/functions.lib.php +++ b/htdocs/lib/functions.lib.php @@ -29,7 +29,7 @@ * \file htdocs/lib/functions.lib.php * \brief A set of functions for Dolibarr * This file contains all frequently used functions. - * \version $Id: functions.lib.php,v 1.533 2011/07/02 16:48:31 eldy Exp $ + * \version $Id: functions.lib.php,v 1.534 2011/07/03 18:31:13 eldy Exp $ */ // For compatibility during upgrade @@ -1092,10 +1092,12 @@ function dol_print_ip($ip,$mode=0) { $datafile=$conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE; //$ip='24.24.24.24'; - //$datafile='E:\Mes Sites\Web\Admin1\awstats\maxmind\GeoIP.dat'; + //$datafile='E:\Mes Sites\Web\Admin1\awstats\maxmind\GeoIP.dat'; Note that this must be downloaded datafile (not same than datafile provided with ubuntu packages) include_once(DOL_DOCUMENT_ROOT.'/lib/dolgeoip.class.php'); $geoip=new DolGeoIP('country',$datafile); + //print 'ip='.$ip.' databaseType='.$geoip->gi->databaseType." GEOIP_CITY_EDITION_REV1=".GEOIP_CITY_EDITION_REV1."\n"; + //print "geoip_country_id_by_addr=".geoip_country_id_by_addr($geoip->gi,$ip)."\n"; $countrycode=$geoip->getCountryCodeFromIP($ip); if ($countrycode) // If success, countrycode is us, fr, ... { @@ -1110,6 +1112,31 @@ function dol_print_ip($ip,$mode=0) return $ret; } +/** + * Return country code for current user. + * If software is used inside a local network, detection may fails (we need a public ip) + * @return string country code (fr, es, it, us, ...) + */ +function dol_user_country() +{ + global $conf,$langs,$user; + + //$ret=$user->xxx; + $ret=''; + if (! empty($conf->geoipmaxmind->enabled)) + { + $ip=$_SERVER["REMOTE_ADDR"]; + $datafile=$conf->global->GEOIPMAXMIND_COUNTRY_DATAFILE; + //$ip='24.24.24.24'; + //$datafile='E:\Mes Sites\Web\Admin1\awstats\maxmind\GeoIP.dat'; + include_once(DOL_DOCUMENT_ROOT.'/lib/dolgeoip.class.php'); + $geoip=new DolGeoIP('country',$datafile); + $countrycode=$geoip->getCountryCodeFromIP($ip); + $ret=$countrycode; + } + return $ret; +} + /** * Format address string * @param address Address