<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.2
 *
 * Copyright (C) 2006-2015  Sylvain FATOME
 * 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 the Free Software Foundation;
 * either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program
 * (in the directory docs);
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 * Contact : galawa@free.fr
 *
 * Web : http://smi.no-ip.org
 ****************************************************************************************/


// Vérification des tentatives de hack
$pos = strpos($_SERVER['PHP_SELF'], "inc.php");
if ($pos === FALSE)
    $ok_inc = TRUE;
else
    $ok_inc = FALSE;
if ($ok_inc == FALSE) header("Location: ../index.php");


/***********************/
/*   Géolocalisation   */
/***********************/

/**
 * Retoune le pays du connecté
 *
 * @param $no_ip
 * @param bool $open
 * @return string
 */
function pays_connect($no_ip, $open = TRUE)
{
    global
    $db;

    $geoloc = new geoPlugin();
    if (trim($no_ip) <> '')
        $geoloc->locate($no_ip);
    else
        $geoloc->locate();

    $pays = '';
    if (trim($geoloc->countryCode) <> '')
    {
        if ($open == TRUE)
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "SELECT pays_nom FROM `" . TBPAYS . "` WHERE pays_code='" . trim($geoloc->countryCode) . "'";
        $row = $db->get_row($rech, ARRAY_A);
        if ($row) {
            extract($row);
            $pays = $pays_nom;
        }
        if ($open == TRUE)
            $db->disconnect();
    }

    return $pays;
}

/**
 * Retoune le code du pays du connecté
 *
 * @param $no_ip
 * @param bool $open
 * @return string
 */
function pays_connect_code($no_ip, $open = TRUE)
{
    $geoloc = new geoPlugin();
    if (trim($no_ip) <> '')
        $geoloc->locate($no_ip);
    else
        $geoloc->locate();

    /*
    echo "Geolocation results for {$geoloc->ip}: <br />\n".
        "City: {$geoloc->city} <br />\n".
        "Region: {$geoloc->region} <br />\n".
        "Area Code: {$geoloc->areaCode} <br />\n".
        "DMA Code: {$geoloc->dmaCode} <br />\n".
        "Country Name: {$geoloc->countryName} <br />\n".
        "Country Code: {$geoloc->countryCode} <br />\n".
        "Longitude: {$geoloc->longitude} <br />\n".
        "Latitude: {$geoloc->latitude} <br />\n".
        "Currency Code: {$geoloc->currencyCode} <br />\n".
        "Currency Symbol: {$geoloc->currencySymbol} <br />\n".
        "Exchange Rate: {$geoloc->currencyConverter} <br />\n";
    */
    $codepays = '';
    if (trim($geoloc->countryCode) <> '')
    {
        if ($open == TRUE)
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
        $rech = "SELECT pays_code FROM `" . TBPAYS . "` WHERE pays_code='" . trim($geoloc->countryCode) . "'";
        $row = $db->get_row($rech, ARRAY_A);
        if ($row) {
            extract($row);
            $codepays = $pays_code;
        }
        if ($open == TRUE)
            $db->disconnect();
    }

    return $codepays;
}
