<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.0
 *
 * Copyright (C) 2006-2014  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");

/*******************/
/* Fonctions MySQL */
/*******************/

/**
 * Crée une connexion MySQL
 *
 * @param string $dbuser
 * @param string $dbpassword
 * @param string $dbname
 * @param string $dbhost
 *
 * @return ezSQL_mysql|ezSQL_mysqli
 */
function open_database($dbuser = '', $dbpassword = '', $dbname = '', $dbhost = 'localhost')
{
    // $db = new ezSQL_mysql($dbuser, $dbpassword, $dbname, $dbhost);
    if (function_exists ('mysqli_connect'))
        $db = new ezSQL_mysqli($dbuser, $dbpassword, $dbname, $dbhost);
    else
    {
        if (function_exists ('mysql_connect'))
            $db = new ezSQL_mysql($dbuser, $dbpassword, $dbname, $dbhost);
    }
    $db->query("SET NAMES latin1");
    // $db->query("SET CHARACTER SET 'latin1'");


    return $db;
}

/**
 * Récupère les variables de la connexion
 *
 * @param bool $open
 *
 * @return array
 */
function get_mysql_vars($open = TRUE)
{
    global
        $db;

    $tab_vars = [];

    if ($open == TRUE)
        $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $row = $db->get_row("SHOW VARIABLES LIKE 'collation_server'",ARRAY_A);
    $tab_vars[$row['Variable_name']] = $row['Value'];
    if ($open == TRUE)
        $db->disconnect();
    return $tab_vars;
}

/**
 * Crée la collation MySQL
 *
 * @param bool $open
 */
function set_utf8($open = TRUE)
{
    global
        $_SESSION;
    $tab_vars = get_mysql_vars($open);
    if (stristr($tab_vars['collation_server'],'utf8') === FALSE)
        $_SESSION['utf8'] = FALSE;
    else
        $_SESSION['utf8'] = TRUE;
}

?>
