<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.4
 *
 * Copyright (C) 2006-2016  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) {
    header('Location: ../index.php');
}

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

/**
 * @param string $dbuser
 * @param string $dbpassword
 * @param string $dbname
 * @param string $dbhost
 * @param string $encoding
 * @return ezSQL_cubrid|ezSQL_mssql|ezSQL_mysql|ezSQL_mysqli|ezSQL_oracle8_9|ezSQL_pdo|ezSQL_postgresql|ezSQL_sqlite|ezSQL_sqlsrv
 */
function open_database($dbuser = '', $dbpassword = '', $dbname = '', $dbhost = 'localhost', $encoding = '')
{
    global
    $db;
//echo $dbuser.' '.$dbpassword.' '.$dbname.' '.' '.$dbhost.' '.$encoding.'<br>';

    /*
    $dbh = new mysqli($dbhost, $dbuser, $dbpassword, $dbname);
    if ($dbh->connect_error) {
        die('Connect Error (' . $dbh->connect_errno . ') : ' . $dbh->connect_error);
    } else
        echo 'Success... ' . $dbh->host_info . "\n";
    $dbh->close();
    */
    $db = new ezSQL_mysqli($dbuser, $dbpassword, $dbname, $dbname, $encoding);
    $db->connect($dbuser, $dbpassword, $dbhost);
    //$db->debug();
    $db->select($dbname, $encoding);
    //$db->debug();

    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, 'utf8');
    }
    $row = $db->get_row("SHOW VARIABLES LIKE 'collation_server'", ARRAY_A);
    $tab_vars[$row['Variable_name']] = $row['Value'];
    if ($open == TRUE) {
        close_database();
    }
    return $tab_vars;
}

/**
 * Crée la collation MySQL
 *
 * @param bool $open
 */
function set_utf8($open = TRUE)
{
    $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();
}