NxTIC/manager/Database.php

272 lines
8.0 KiB
PHP
Raw Normal View History

<?php
namespace manager;
class DataBase{
/* ATTRIBUTS STATIQUES */
public static $config_path = array(
'local' => 'f/json/database-local/conf',
'remote' => 'f/json/database/conf'
);
private static $pdo;
private static $instance;
/* ATTRIBUTS */
private $host;
private $dbname;
private $username;
private $password;
public static $error;
public function __construct($host, $dbname, $username, $password){
$this->host = $host;
$this->dbname = $dbname;
$this->username = $username;
$this->password = $password;
try{
self::$pdo = new \PDO('mysql:host='.$this->host.';dbname='.$this->dbname, $this->username, $this->password);
// On signale que tout s'est bien passe
self::$error = ManagerError::Success;
}catch(Exception $e){
// On signale qu'il y a une erreur
self::$error = ManagerError::PDOConnection;
}
}
/* retourne une instance de la classe */
public static function getInstance(){
if( self::$instance == null || self::$error != ManagerError::Success ){ // Si aucune instance existante OU erreur de connection
// chargement de la configuration du server SQL
if( !isset($_SERVER['HTTP_HOST']) || isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] == 'socioview' )
$conf = json_decode( ResourceDispatcher::getResource(self::$config_path['local']), true );
else
$conf = json_decode( ResourceDispatcher::getResource(self::$config_path['remote']), true );
// creation de l'instance en fonction des parametres
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;
}
public function getConfig(){
return array(
'host' => $this->host,
'username' => $this->username
);
}
/*************************************************************/
/* _____ ______ _ _ ______ _____ _ */
/* / ____| ____| \ | | ____| __ \ /\ | | */
/* | | __| |__ | \| | |__ | |__) | / \ | | */
/* | | |_ | __| | . ` | __| | _ / / /\ \ | | */
/* | |__| | |____| |\ | |____| | \ \ / ____ \| |____ */
/* \_____|______|_| \_|______|_| \_\/_/ \_\______| */
/* */
/*************************************************************/
/* 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
*
* @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){
// On quitte si ce n'est pas un tableau
if( !is_array($fetchData) )
return array();
$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
if( !mb_detect_encoding($val, 'UTF-8') )
$fetchData[$i][$col] = utf8_encode($val);
if( is_int($col) ){ // Si indice numerique
if( $nextEquivalent ) // Si suit un indice textuel
unset( $fetchData[$i][$col] ); // on supprime l'indice
$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
}
/* [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 indice numerique
if( $nextEquivalent ) // Si suit un indice textuel
unset( $fetchData[$i] ); // on supprime l'indice
$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;
}
////////////////////////////////////////////////////////////////
// _ __ _ _ _
// __ _____ _ __(_)/ _(_) ___ __ _| |_(_) ___ _ __ ___
// \ \ / / _ \ '__| | |_| |/ __/ _` | __| |/ _ \| '_ \/ __|
// \ 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 'id':
return $checker && is_numeric($value) && $value <= 2147483647 && $value >= -2147483647;
break;
case 'int':
return $checker && is_numeric($value) && $value <= 2147483647 && $value >= 0;
break;
case 'varchar(255)':
return $checker && is_string($value) && strlen($value) <= 255 && strlen($value) > 0;
break;
case 'varchar(50)':
return $checker && is_string($value) && strlen($value) <= 50 && strlen($value) > 0;
break;
2016-04-12 12:27:54 +00:00
case 'varchar(30)':
return $checker && is_string($value) && strlen($value) <= 30 && strlen($value) > 0;
break;
2016-04-12 12:27:54 +00:00
case 'text':
return $checker && is_string($value);
case 'mail':
return $checker && is_string($value) && strlen($value) <= 50 && preg_match('/^[\w\.-]+@[\w\.-]+\.[a-z]{2,4}$/i', $value);
break;
case 'sha1':
return $checker && is_string($value) && preg_match('/^[\da-f]{40}$/i', $value);
break;
case 'array':
return $checker && is_array($value);
break;
}
return $checker;
}
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
$days = array("Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche");
// Mois de l'annee
$months = array("Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre");
/* [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
=========================================================*/
$result = array(
$days[$daynum], // nom de jour
date('j', $time), // jour du mois
$months[$monthnum], // nom du mois
date('Y', $time), // annee
);
return implode(" ", $result);
}
}
?>