function UpdateGeoip()
{
    $database = "/usr/share/GeoIP/GeoIP.dat";
    if (!is_file($database)) {
        RTMevents("Unable to stat /usr/share/GeoIP/GeoIP.dat");
        installgeoip();
        return null;
    }
    if (!is_file("/usr/local/share/GeoIP/GeoIPCity.dat")) {
        if (is_file("/usr/local/share/GeoIP/GeoLiteCity.dat")) {
            RTMevents("Linking /usr/local/share/GeoIP/GeoLiteCity.dat");
            system('/bin/ln -s /usr/local/share/GeoIP/GeoLiteCity.dat /usr/local/share/GeoIP/GeoIPCity.dat >/dev/null 2>&1');
        }
    }
    if (!is_file("/usr/share/GeoIP/GeoIPCity.dat")) {
        if (is_file("/usr/share/GeoIP/GeoLiteCity.dat")) {
            RTMevents("Linking /usr/share/GeoIP/GeoLiteCity.dat");
            system('/bin/ln -s /usr/share/GeoIP/GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat >/dev/null 2>&1');
        }
    }
    if (!function_exists("geoip_record_by_name")) {
        RTMevents("Unable to sat geoip_record_by_name() function");
        installgeoip();
        return null;
    }
    $db_info = geoip_database_info(GEOIP_COUNTRY_EDITION);
    RTMevents("Using {$db_info}");
    $q = new mysql();
    $sql = "SELECT smtp_sender  FROM `smtp_logs` WHERE LENGTH(smtp_sender)>0 AND smtp_sender!='127.0.0.1' AND (Country IS NULL or Country='undefined')";
    $results = $q->QUERY_SQL($sql, "artica_events");
    if (!$q->ok) {
        RTMevents("Wrong sql query {$q->mysql_error}");
        return null;
    }
    while ($ligne = mysql_fetch_array($results, MYSQL_ASSOC)) {
        $smtp_sender = $ligne["smtp_sender"];
        if ($_GET["smtp_cache"][$smtp_sender]) {
            continue;
        }
        $record = geoip_record_by_name($smtp_sender);
        if (!$record) {
            RTMevents("unable to locate this IP {$smtp_sender}");
            $_GET["smtp_cache"][$smtp_sender] = true;
            continue;
        }
        $Country = $record["country_name"];
        $_GET["smtp_cache"][$smtp_sender] = true;
        RTMevents("{$smtp_sender} =  {$Country}");
        updategeo($smtp_sender, $Country);
    }
}
function UpdateGeoip()
{
    if (isset($GLOBALS["UpdateGeoip_executed"])) {
        return;
    }
    $GLOBALS["UpdateGeoip_executed"] = true;
    $unix = new unix();
    $ln = $unix->find_program("ln");
    $database = "/usr/share/GeoIP/GeoIP.dat";
    if (!is_file($database)) {
        installgeoip();
        return null;
    }
    if (!is_file("/usr/local/share/GeoIP/GeoIPCity.dat")) {
        if (is_file("/usr/local/share/GeoIP/GeoLiteCity.dat")) {
            shell_exec("{$ln} -s /usr/local/share/GeoIP/GeoLiteCity.dat /usr/local/share/GeoIP/GeoIPCity.dat >/dev/null 2>&1");
        }
    }
    if (!is_file("/usr/share/GeoIP/GeoIPCity.dat")) {
        if (is_file("/usr/share/GeoIP/GeoLiteCity.dat")) {
            system("{$ln} -s /usr/share/GeoIP/GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat >/dev/null 2>&1");
        }
    }
    if (!function_exists("geoip_record_by_name")) {
        installgeoip();
        return null;
    }
}