2016-04-04 09:47:17 +00:00
|
|
|
<?php
|
2016-04-18 14:21:24 +00:00
|
|
|
|
2016-10-18 13:11:37 +00:00
|
|
|
namespace database\core;
|
|
|
|
use \manager\ManagerError;
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
class DataBaseDriver{
|
2016-04-18 14:21:24 +00:00
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
/* STATIC ATTRIBUTES */
|
2016-11-05 13:58:31 +00:00
|
|
|
private static function conf(){
|
|
|
|
// YOUR CONFIGURATION BEHIND
|
|
|
|
$path = __CONFIG__.'/database-driver.json';
|
|
|
|
|
|
|
|
/* (1) Checks the file */
|
|
|
|
if( !is_file($path) )
|
|
|
|
return [];
|
|
|
|
|
|
|
|
/* (2) Checks json */
|
|
|
|
$parsed = json_decode( file_get_contents($path), true );
|
|
|
|
|
|
|
|
if( !is_array($parsed) )
|
|
|
|
return [];
|
|
|
|
|
|
|
|
/* (3) Returns configuration */
|
|
|
|
return $parsed;
|
2016-10-17 08:14:50 +00:00
|
|
|
}
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
private static $path; // Databases configurations files
|
|
|
|
private static $config; // PDO configurations
|
|
|
|
private static $instance = []; // Database driver instance list
|
2016-04-04 09:47:17 +00:00
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
public $error;
|
|
|
|
|
|
|
|
/* ATTRIBUTES */
|
2016-04-04 09:47:17 +00:00
|
|
|
private $host;
|
|
|
|
private $dbname;
|
|
|
|
private $username;
|
|
|
|
private $password;
|
2016-11-05 10:56:03 +00:00
|
|
|
private $pdo;
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
/* CONSTRUCTOR OF A DATABASE DRIVER
|
|
|
|
*
|
|
|
|
* @host<String> Database Server's host
|
|
|
|
* @dbname<String> Database name
|
|
|
|
* @username<String> Database username
|
|
|
|
* @password<String> Database password
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function __construct($host, $dbname, $username, $password){
|
|
|
|
/* (2) Stores configuration */
|
2016-04-04 09:47:17 +00:00
|
|
|
$this->host = $host;
|
|
|
|
$this->dbname = $dbname;
|
|
|
|
$this->username = $username;
|
|
|
|
$this->password = $password;
|
2016-04-18 14:21:24 +00:00
|
|
|
|
|
|
|
try{
|
2016-11-05 10:56:03 +00:00
|
|
|
|
|
|
|
$this->pdo = new \PDO('mysql:host='.$this->host.';dbname='.$this->dbname, $this->username, $this->password);
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
// On signale que tout s'est bien passe
|
2016-11-05 10:56:03 +00:00
|
|
|
$this->error = ManagerError::Success;
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
}catch(Exception $e){
|
|
|
|
// On signale qu'il y a une erreur
|
2016-11-05 10:56:03 +00:00
|
|
|
$this->error = ManagerError::PDOConnection;
|
2016-04-04 09:47:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
/************************************************
|
|
|
|
**** Multiton Management (static) ****
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
/* ADDS A NEW CONNECTION
|
|
|
|
*
|
|
|
|
* @label<String> [optional] Database Label
|
|
|
|
*
|
|
|
|
* @return status<Boolean> If added successfully
|
|
|
|
*
|
|
|
|
*/
|
2016-11-05 13:58:31 +00:00
|
|
|
private static function add($label=null){
|
|
|
|
$conf = self::conf();
|
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
/* [1] Default values
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) If label isn't given */
|
|
|
|
is_null($label) && ($label = 'default');
|
|
|
|
|
|
|
|
/* (2) If label and no path */
|
2016-11-05 13:58:31 +00:00
|
|
|
if( $label !== 'default' && !isset($conf[$label]) )
|
2016-11-05 10:56:03 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
2016-11-05 13:58:31 +00:00
|
|
|
/* [3] Instanciates the driver
|
2016-11-05 10:56:03 +00:00
|
|
|
=========================================================*/
|
|
|
|
try{
|
|
|
|
|
|
|
|
/* (1) If local -> instanciates with local configuration */
|
2016-05-11 14:27:44 +00:00
|
|
|
if( !checkdnsrr($_SERVER['SERVER_NAME'], 'NS') )
|
2016-11-05 13:58:31 +00:00
|
|
|
self::$instance[$label] = new DatabaseDriver($conf[$label]['local']['host'], $conf[$label]['local']['dbname'], $conf[$label]['local']['user'], $conf[$label]['local']['password']);
|
2016-11-05 10:56:03 +00:00
|
|
|
/* (2) If Remote -> instanciates with Remote configuration */
|
2016-04-04 09:47:17 +00:00
|
|
|
else
|
2016-11-05 13:58:31 +00:00
|
|
|
self::$instance[$label] = new DatabaseDriver($conf[$label]['remote']['host'], $conf[$label]['remote']['dbname'], $conf[$label]['remote']['user'], $conf[$label]['remote']['password']);
|
2016-04-04 09:47:17 +00:00
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
return true;
|
2016-04-04 09:47:17 +00:00
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
}catch(\Exception $e){
|
2016-04-18 14:21:24 +00:00
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
/* (3) If fails */
|
|
|
|
return false;
|
2016-04-04 09:47:17 +00:00
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
}
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
/* GET A DATABASE DRIVER INSTANCE
|
|
|
|
*
|
|
|
|
* @label<String> [optional] Driver's label
|
|
|
|
*
|
|
|
|
* @return driver<Database> Multiton
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function get($label=null){
|
2016-11-05 13:58:31 +00:00
|
|
|
$conf = self::conf();
|
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
/* [1] Checks arguments
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Label default value */
|
|
|
|
is_null($label) && ($label = 'default');
|
2016-04-04 09:47:17 +00:00
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
/* (2) If no label, or unknown label */
|
2016-11-05 13:58:31 +00:00
|
|
|
if( is_null($label) || !isset(self::$instance[$label]) ){
|
|
|
|
|
|
|
|
/* (2.1) Try to add the configuration if exists */
|
|
|
|
if( isset($conf[$label]) ){
|
|
|
|
self::add($label);
|
|
|
|
return self::get($label);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
throw new \Exception('Database @label is incorrect.');
|
2016-11-05 13:58:31 +00:00
|
|
|
}
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
/* [2] Returns instance
|
|
|
|
=========================================================*/
|
|
|
|
return self::$instance[$label];
|
|
|
|
}
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
|
2016-11-05 10:56:03 +00:00
|
|
|
/* retourne la connection statique */
|
|
|
|
public static function getPDO($label=null){
|
|
|
|
$instance = self::get($label);
|
|
|
|
|
|
|
|
return $instance->pdo;
|
|
|
|
}
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
public function getConfig(){
|
2016-09-12 16:04:53 +00:00
|
|
|
return [
|
2016-04-04 09:47:17 +00:00
|
|
|
'host' => $this->host,
|
2016-11-05 13:58:31 +00:00
|
|
|
'dbname' => $this->dbname,
|
2016-04-04 09:47:17 +00:00
|
|
|
'username' => $this->username
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-04-04 09:47:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************/
|
|
|
|
/* _____ ______ _ _ ______ _____ _ */
|
|
|
|
/* / ____| ____| \ | | ____| __ \ /\ | | */
|
|
|
|
/* | | __| |__ | \| | |__ | |__) | / \ | | */
|
|
|
|
/* | | |_ | __| | . ` | __| | _ / / /\ \ | | */
|
|
|
|
/* | |__| | |____| |\ | |____| | \ \ / ____ \| |____ */
|
|
|
|
/* \_____|______|_| \_|______|_| \_\/_/ \_\______| */
|
|
|
|
/* */
|
|
|
|
/*************************************************************/
|
|
|
|
|
|
|
|
/* SUPPRIME LES VALEURS À CLÉS NUMÉRIQUES DANS UN FETCH D'UNE TABLE DE LA BDD
|
|
|
|
*
|
|
|
|
* @fetchData<Array> le résultat d'une $requeteSQL->fetchAll()
|
|
|
|
* @oneDimension<Boolean> FAUX <=> fetchAll ; VRAI <=> fetch
|
|
|
|
*
|
2016-04-18 14:21:24 +00:00
|
|
|
* @return newFetchData<Array> retourne le tableau donné en paramètre mais sans les valeurs à clés numériques
|
2016-04-04 09:47:17 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function delNumeric($fetchData, $oneDimension=false){
|
|
|
|
// On quitte si ce n'est pas un tableau
|
|
|
|
if( !is_array($fetchData) )
|
2016-09-12 16:04:53 +00:00
|
|
|
return [];
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
$nextEquivalent = false; // Vaut VRAI si le prochain est peut-etre un equivalent numerique
|
|
|
|
|
|
|
|
/* [1] 2 dimensions
|
|
|
|
===============================================*/
|
|
|
|
if( !$oneDimension && isset($fetchData[0]) && is_array($fetchData[0]) ){
|
|
|
|
|
|
|
|
// on supprime les doublons des entrées (indice numérique)
|
|
|
|
for( $i = 0 ; $i < count($fetchData) ; $i++ ) // pour tout les utilisateurs
|
|
|
|
foreach($fetchData[$i] as $col => $val){ // pour toutes les entrées
|
2016-04-18 14:21:24 +00:00
|
|
|
|
2016-11-05 15:13:31 +00:00
|
|
|
if( \mb_detect_encoding($val) === 'UTF-8' )
|
2016-04-04 09:47:17 +00:00
|
|
|
$fetchData[$i][$col] = utf8_encode($val);
|
2016-11-05 15:13:31 +00:00
|
|
|
else
|
|
|
|
$fetchData[$i][$col] = utf8_encode(utf8_decode($val));
|
2016-04-18 14:21:24 +00:00
|
|
|
|
2016-04-04 09:47:17 +00:00
|
|
|
if( is_int($col) ){ // Si indice numerique
|
|
|
|
if( $nextEquivalent ) // Si suit un indice textuel
|
|
|
|
unset( $fetchData[$i][$col] ); // on supprime l'indice
|
2016-04-18 14:21:24 +00:00
|
|
|
|
2016-04-04 09:47:17 +00:00
|
|
|
$nextEquivalent = false; // Dans tous les cas, on dit que le prochain ne pourra pas etre supprime si numerique
|
|
|
|
|
|
|
|
}else // Si l'indice n'est pas un entier
|
|
|
|
$nextEquivalent = true; // On signale qu'il y aura peut etre un indice numerique suivant
|
2016-04-18 14:21:24 +00:00
|
|
|
|
2016-04-04 09:47:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* [2] 1 dimensions
|
|
|
|
===============================================*/
|
|
|
|
}else{
|
|
|
|
|
|
|
|
// on supprime les doublons des entrées (indice numérique)
|
|
|
|
foreach($fetchData as $i=>$val){ // pour toutes les entrées
|
2016-04-18 14:21:24 +00:00
|
|
|
|
2016-11-05 15:13:31 +00:00
|
|
|
if( \mb_detect_encoding($val) === 'UTF-8' )
|
2016-04-04 09:47:17 +00:00
|
|
|
$fetchData[$i] = utf8_encode($val);
|
2016-11-05 15:13:31 +00:00
|
|
|
else
|
|
|
|
$fetchData[$i] = utf8_encode(utf8_decode($val));
|
2016-04-04 09:47:17 +00:00
|
|
|
|
|
|
|
if( is_int($i) ){ // Si indice numerique
|
|
|
|
if( $nextEquivalent ) // Si suit un indice textuel
|
|
|
|
unset( $fetchData[$i] ); // on supprime l'indice
|
2016-04-18 14:21:24 +00:00
|
|
|
|
2016-04-04 09:47:17 +00:00
|
|
|
$nextEquivalent = false; // Dans tous les cas, on dit que le prochain ne pourra pas etre supprime si numerique
|
|
|
|
|
|
|
|
}else // Si l'indice n'est pas un entier
|
|
|
|
$nextEquivalent = true; // On signale qu'il y aura peut etre un indice numerique suivant
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fetchData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-18 20:45:55 +00:00
|
|
|
|
|
|
|
/* FONCTION QUI FORMATTE UN NUMÉRO DE TÉLÉPHONE
|
|
|
|
*
|
|
|
|
* @number<String> Numéro de téléphone en +336/336/06/0336/00336
|
|
|
|
*
|
|
|
|
* @return formatted<String> Numéro formatté (06), on FALSE si erreur
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function formatNumber($number){
|
|
|
|
// On met en <string> quel que soit le type
|
|
|
|
$number = (string) $number;
|
|
|
|
|
|
|
|
// On supprime tous les espaces
|
|
|
|
$number = str_replace(' ', '', $number);
|
|
|
|
|
|
|
|
// On formatte le numéro
|
|
|
|
if( preg_match("/^(?:\+33|0?0?33|0)(.+)/", $number, $m) )
|
|
|
|
$number = '0'.$m[1];
|
|
|
|
|
|
|
|
// On retourne le numéro formatté
|
|
|
|
return $number;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-20 09:59:21 +00:00
|
|
|
public static function readableNumber($number){
|
|
|
|
/* (1) On formatte le numéro si c'est pas fait */
|
|
|
|
$formatted = self::formatNumber($number);
|
|
|
|
|
|
|
|
for( $i = 1 ; $i < strlen($formatted) ; $i++ )
|
|
|
|
if( ($i-2) % 3 == 0 )
|
|
|
|
$formatted = substr($formatted, 0, $i).' '.substr($formatted, $i);
|
|
|
|
|
|
|
|
return $formatted;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-10 15:01:44 +00:00
|
|
|
////////////////////////////////////
|
2016-04-18 14:21:24 +00:00
|
|
|
// _ _
|
|
|
|
// __| | __ _| |_ ___ ___
|
2016-04-10 15:01:44 +00:00
|
|
|
// / _` |/ _` | __/ _ \/ __|
|
|
|
|
// | (_| | (_| | || __/\__ \
|
|
|
|
// \__,_|\__,_|\__\___||___/
|
|
|
|
//
|
|
|
|
////////////////////////////////////
|
|
|
|
// 1) Convertis une date en en francais explicite
|
|
|
|
public static function frDate($date){
|
|
|
|
/* [1] On definit les traductions
|
|
|
|
=========================================================*/
|
|
|
|
// Jours de la semaine
|
2016-09-12 16:04:53 +00:00
|
|
|
$days = ["Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche"];
|
2016-04-10 15:01:44 +00:00
|
|
|
// Mois de l'annee
|
2016-09-12 16:04:53 +00:00
|
|
|
$months = ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"];
|
2016-04-10 15:01:44 +00:00
|
|
|
|
|
|
|
/* [2] On recupere le timestamp et les indices
|
|
|
|
=========================================================*/
|
|
|
|
$time = strtotime($date); // timestamp
|
|
|
|
$daynum = intval( date('N', $time)-1 ); // jour dans la semaine
|
|
|
|
$monthnum = intval( date('n', $time)-1 ); // numero du mois dans l'annee
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On recupere les infos independemment
|
|
|
|
=========================================================*/
|
2016-09-12 16:04:53 +00:00
|
|
|
$result = [
|
2016-04-10 15:01:44 +00:00
|
|
|
$days[$daynum], // nom de jour
|
|
|
|
date('j', $time), // jour du mois
|
|
|
|
$months[$monthnum], // nom du mois
|
|
|
|
date('Y', $time), // annee
|
2016-09-12 16:04:53 +00:00
|
|
|
];
|
2016-04-10 15:01:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
return implode(" ", $result);
|
|
|
|
}
|
|
|
|
|
2016-04-04 09:47:17 +00:00
|
|
|
}
|
|
|
|
?>
|