<?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");

/*******************/
/* 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,'utf8');
    }
    else
    {
        if (function_exists ('mysql_connect')) {
            $db = new ezSQL_mysql($dbuser, $dbpassword, $dbname, $dbhost, 'utf8');
        }
    }
    /*
    $db->query("SHOW CHARACTER SET");
    $rows2 = $db->get_results($rech2, ARRAY_A);
    foreach ($rows2 as $row2)
    {
        echo "cs : ".$row2["Charset"]."<br>";
    }
    */
    //$db->query("SET NAMES 'utf8'");

    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;
}

function close_database()
{
    global
        $db;

    $db->disconnect();
}