2016-02-03 22:22:18 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace manager;
|
|
|
|
|
|
|
|
|
|
|
|
class DataBase{
|
|
|
|
|
2016-02-04 20:15:43 +00:00
|
|
|
/* ATTRIBUTS STATIQUES */
|
2016-02-08 21:43:35 +00:00
|
|
|
public static $config_path = array('f/json/database/conf', 'f/json/database-local/conf');
|
2016-02-04 20:15:43 +00:00
|
|
|
private static $pdo;
|
|
|
|
private static $instance;
|
|
|
|
|
|
|
|
|
2016-02-03 22:22:18 +00:00
|
|
|
/* ATTRIBUTS */
|
|
|
|
private $host;
|
|
|
|
private $dbname;
|
|
|
|
private $username;
|
|
|
|
private $password;
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct($host, $dbname, $username, $password){
|
|
|
|
$this->host = $host;
|
|
|
|
$this->dbname = $dbname;
|
|
|
|
$this->username = $username;
|
|
|
|
$this->password = $password;
|
|
|
|
|
|
|
|
// password: Qt358nUdyeTxLDM8
|
|
|
|
self::$pdo = new \PDO('mysql:host='.$host.';dbname='.$dbname, $username, $password);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* retourne une instance de la classe */
|
|
|
|
public static function getInstance(){
|
|
|
|
if( self::$instance == null ){
|
|
|
|
|
|
|
|
// chargement de la configuration du server SQL
|
2016-02-10 07:54:23 +00:00
|
|
|
if( $_SERVER['HTTP_HOST'] != 'stefproject' )
|
2016-02-08 20:46:11 +00:00
|
|
|
$conf = json_decode( ResourceDispatcher::getResource(self::$config_path[0]), true );
|
|
|
|
else
|
|
|
|
$conf = json_decode( ResourceDispatcher::getResource(self::$config_path[1]), true );
|
2016-02-04 20:15:43 +00:00
|
|
|
|
|
|
|
// creation de l'instance en fonction des parametres
|
2016-02-03 22:22:18 +00:00
|
|
|
self::$instance = new DataBase($conf['host'], $conf['dbname'], $conf['user'], $conf['password']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* retourne la connection statique */
|
|
|
|
public static function getPDO(){
|
|
|
|
$instance = self::getInstance();
|
|
|
|
return self::$pdo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************/
|
|
|
|
/* _____ ______ _ _ ______ _____ _ */
|
|
|
|
/* / ____| ____| \ | | ____| __ \ /\ | | */
|
|
|
|
/* | | __| |__ | \| | |__ | |__) | / \ | | */
|
|
|
|
/* | | |_ | __| | . ` | __| | _ / / /\ \ | | */
|
|
|
|
/* | |__| | |____| |\ | |____| | \ \ / ____ \| |____ */
|
|
|
|
/* \_____|______|_| \_|______|_| \_\/_/ \_\______| */
|
|
|
|
/* */
|
|
|
|
/*************************************************************/
|
|
|
|
|
|
|
|
/* 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()
|
2016-02-11 15:00:41 +00:00
|
|
|
* @oneDimension<Boolean> FAUX <=> fetchAll ; VRAI <=> fetch
|
2016-02-03 22:22:18 +00:00
|
|
|
*
|
|
|
|
* @return newFetchData<Array> retourne le tableau donné en paramètre mais sans les valeurs à clés numériques
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function delNumeric($fetchData, $oneDimension=false){
|
|
|
|
|
|
|
|
/* [1] 2 dimensions
|
|
|
|
===============================================*/
|
|
|
|
if( !$oneDimension ){
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
if( !mb_detect_encoding($val, 'UTF-8') )
|
|
|
|
$fetchData[$i][$col] = utf8_encode($val);
|
|
|
|
|
|
|
|
if( is_int($col) ) // si l'indice est un entier
|
|
|
|
unset( $fetchData[$i][$col] ); // on le supprime
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [2] 1 dimensions
|
|
|
|
===============================================*/
|
|
|
|
}else{
|
|
|
|
|
|
|
|
// on supprime les doublons des entrées (indice numérique)
|
|
|
|
foreach($fetchData as $i=>$val){ // pour toutes les entrées
|
|
|
|
|
|
|
|
if( !mb_detect_encoding($val, 'UTF-8') )
|
|
|
|
$fetchData[$i] = utf8_encode($val);
|
|
|
|
|
|
|
|
if( is_int($i) ) // si l'indice est un entier
|
|
|
|
unset( $fetchData[$i] ); // on le supprime
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fetchData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-11 15:00:41 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
// _ __ _ _ _
|
|
|
|
// __ _____ _ __(_)/ _(_) ___ __ _| |_(_) ___ _ __ ___
|
|
|
|
// \ \ / / _ \ '__| | |_| |/ __/ _` | __| |/ _ \| '_ \/ __|
|
|
|
|
// \ V / __/ | | | _| | (_| (_| | |_| | (_) | | | \__ \
|
|
|
|
// \_/ \___|_| |_|_| |_|\___\__,_|\__|_|\___/|_| |_|___/
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
/* VERIFICATIONS DES TYPES UTILES GENERIQUES
|
|
|
|
*
|
|
|
|
* @type<String> Type que l'on veut verifier
|
|
|
|
* @value<mixed*> Valeur a verifier
|
|
|
|
*
|
|
|
|
* @return match<Boolean> Retourne si oui ou non la valeur @value est du bon type @type
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function check($type, $value){
|
|
|
|
$checker = !is_null($value);
|
|
|
|
|
|
|
|
switch($type){
|
|
|
|
/* (1) Global */
|
|
|
|
case 'auto_increment_id':
|
|
|
|
return $checker && is_numeric($value) && $value <= 2147483647 && $value >= -2147483647;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* (2) Utilisateur */
|
|
|
|
case 'user.code':
|
|
|
|
return $checker && is_string($value) && preg_match('/^[\dA-F]{2}(\-[\dA-F]{2}){3,5}$/i', $value);
|
|
|
|
break;
|
2016-02-12 15:20:26 +00:00
|
|
|
case 'user.username': case 'user.lastname':
|
|
|
|
return $checker && is_string($value) && preg_match('/^[\w-]{3,30}$/i', $value);
|
|
|
|
break;
|
|
|
|
case 'user.firstname': case 'user.lastname':
|
2016-02-11 15:00:41 +00:00
|
|
|
return $checker && is_string($value) && preg_match('/^[a-z -]{3,30}$/i', $value);
|
|
|
|
break;
|
|
|
|
case 'user.mail':
|
|
|
|
return $checker && is_string($value) && strlen($value) <= 50 && preg_match('/^[\w\.-]+@[\w\.-]+\.[a-z]{2,4}$/i', $value);
|
|
|
|
break;
|
|
|
|
case 'user.password':
|
|
|
|
return $checker && is_numeric($value) && preg_match('/^[\da-f]{40}$/i', $value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* (3) Machine */
|
|
|
|
case 'groupe.nom':
|
|
|
|
return $checker && is_string($value) && preg_match('/^[a-z0-9 -]{1,10}$/i', $value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* (4) Groupe */
|
|
|
|
case 'formation.code':
|
|
|
|
return $checker && is_string($value) && preg_match('/[\w]{0,10}/i', $value);
|
|
|
|
break;
|
|
|
|
case 'formation.nom':
|
|
|
|
return $checker && is_string($value) && preg_match('/[\w ]{0,100}/i', $value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $checker;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2016-02-03 22:22:18 +00:00
|
|
|
?>
|