2015-12-06 18:42:02 +00:00
|
|
|
<?php define('__ROOT__', dirname(dirname(__FILE__)) );
|
2015-10-21 20:56:56 +00:00
|
|
|
|
|
|
|
/***********************************************************
|
|
|
|
* *
|
|
|
|
* MANAGER DE SECURITE GENERALE ET SPECIFIQUE *
|
|
|
|
* *
|
|
|
|
************************************************************
|
|
|
|
* *
|
|
|
|
* [0] Constantes *
|
|
|
|
* [1] Session & redirection *
|
2015-12-06 18:42:02 +00:00
|
|
|
* [2] autoloader *
|
2015-10-21 20:56:56 +00:00
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
* *
|
|
|
|
***********************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/* [0] CONSTANTES
|
|
|
|
============================================================*/
|
2015-11-06 22:00:08 +00:00
|
|
|
function getPermissions(){ // retourne la liste des droits existants
|
|
|
|
return array('student', 'teacher', 'master', 'admin');
|
|
|
|
}
|
|
|
|
|
|
|
|
/** retourne l'état "connecté" de l'utilisateur courant
|
|
|
|
*
|
|
|
|
* @return state<Boolean> VRAI si connecté / FAUX si pas connecté
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function connected(){
|
|
|
|
return $_SESSION['identifiant'] != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** retourne si un utilisateur a un droit ou non
|
|
|
|
*
|
|
|
|
* @return permission<Boolean> VRAI si l'utilisateur a ce droit / FAUX s'il ne l'a pas
|
|
|
|
*
|
|
|
|
* */
|
|
|
|
function permission($perm){
|
|
|
|
return connected() && is_int( array_search($perm, $_SESSION['droits']) );
|
|
|
|
}
|
|
|
|
|
2015-10-21 20:56:56 +00:00
|
|
|
|
2015-10-28 17:08:12 +00:00
|
|
|
function debug(){
|
|
|
|
ini_set('display_errors',1);
|
|
|
|
ini_set('display_startup_errors',1);
|
|
|
|
error_reporting(-1);
|
2015-11-19 22:32:54 +00:00
|
|
|
}debug();
|
2015-11-11 17:23:37 +00:00
|
|
|
|
2015-11-15 18:10:33 +00:00
|
|
|
|
2015-11-30 18:17:39 +00:00
|
|
|
function getCurrentYear(){
|
|
|
|
if( Date('m', time()) < 8 ) return Date('Y', time()) - 1;
|
|
|
|
else return Date('Y', time());
|
|
|
|
}
|
2015-10-21 20:56:56 +00:00
|
|
|
|
|
|
|
|
2015-11-30 18:17:39 +00:00
|
|
|
function anneeScolaire($year){
|
|
|
|
return $year.' - '.($year+1);
|
|
|
|
}
|
2015-11-13 23:47:11 +00:00
|
|
|
|
|
|
|
function secure_sha1($text){
|
|
|
|
$salt = '!!-vi_v93DFeswgf9de2b4d34ev!XX!x%';
|
|
|
|
$pepper = ':!;,°1832%0%QMSLµQ°++Q==!';
|
|
|
|
return sha1( sha1($salt.$text).$pepper );
|
2015-11-10 21:33:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-10-21 20:56:56 +00:00
|
|
|
/* [1] SESSIONS & REDIRECTION
|
|
|
|
============================================================*/
|
|
|
|
|
|
|
|
|
|
|
|
/* ETABLIT UNE SESSION SÉCURISÉE
|
|
|
|
*
|
|
|
|
* [1] Définit un id_session (PHPSESSID) unique et propre à une connection
|
|
|
|
* + propre à un navigateur (navigateur, version, OS, système X, ...)
|
|
|
|
* + propre à un accès (ip publique = box)
|
|
|
|
*
|
|
|
|
* [2] Démarre la session
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function session_init(){
|
2015-10-22 08:29:43 +00:00
|
|
|
/*
|
2015-10-21 20:56:56 +00:00
|
|
|
session_id( // on définit le session id
|
|
|
|
sha1( // qui est un Hash MD5
|
|
|
|
$_SERVER['HTTP_USER_AGENT']. // qui correspond aux infos système disponibles de l'utilisateur
|
|
|
|
$_SERVER['REMOTE_ADDR'] // et de son ip publique
|
|
|
|
)
|
|
|
|
);
|
2015-10-22 08:29:43 +00:00
|
|
|
*/
|
|
|
|
|
2015-10-21 20:56:56 +00:00
|
|
|
session_start(); // on démarre la session
|
|
|
|
|
|
|
|
|
|
|
|
// on vérifie l'intégrité des variables session
|
2015-10-28 17:08:12 +00:00
|
|
|
$identifiantDefinedProperly = isset($_SESSION['identifiant']) && !empty($_SESSION['identifiant']) && gettype($_SESSION['identifiant']) == 'string' && strlen($_SESSION['identifiant']) > 0;
|
2015-11-09 21:05:57 +00:00
|
|
|
$droitsDefinedProperly = isset($_SESSION['droits']) && !empty($_SESSION['droits']) && is_array($_SESSION['droits']) && count($_SESSION['droits']) > 0;
|
2015-11-06 22:00:08 +00:00
|
|
|
|
|
|
|
if( $droitsDefinedProperly )
|
|
|
|
foreach($_SESSION['droits'] as $droit)
|
|
|
|
if( !is_int( array_search($droit, getPermissions()) ) ) // si le droit n'est pas dans la liste des possibilitées, on retourne false
|
|
|
|
$droitsDefinedProperly = false;
|
|
|
|
|
2015-10-21 20:56:56 +00:00
|
|
|
|
|
|
|
// si les variables sessions ne sont pas toutes les 2 correctes
|
2015-11-06 22:00:08 +00:00
|
|
|
if( !$identifiantDefinedProperly || !$droitsDefinedProperly ){
|
2015-10-28 17:08:12 +00:00
|
|
|
$_SESSION['identifiant'] = null; // on les initialise à NULL
|
2015-11-06 22:00:08 +00:00
|
|
|
$_SESSION['droits'] = array();
|
2015-10-21 20:56:56 +00:00
|
|
|
}
|
2015-11-06 22:00:08 +00:00
|
|
|
}session_init();
|
2015-11-10 19:55:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [2] GESTION DES PARAMÈTRES
|
|
|
|
============================================================*/
|
|
|
|
|
|
|
|
/** Vérifie le bon format, le bon type, ainsi que la bonne valeur des variables envoyées
|
|
|
|
*
|
2015-11-10 21:49:27 +00:00
|
|
|
* @param $tabVar tableau contenant l'ensemble des variables a vérifier
|
|
|
|
* @param $tabType tableau contenant l'ensemble des types
|
|
|
|
* @param $tabForm tableau contenant l'ensemble des conditions attendues
|
2015-11-10 19:55:40 +00:00
|
|
|
*
|
2015-11-10 21:49:27 +00:00
|
|
|
* @return toutOK<Boolean> VRAI si variables bonnes / FAUX sinon
|
|
|
|
*
|
2015-11-10 19:55:40 +00:00
|
|
|
*/
|
2015-11-19 07:58:15 +00:00
|
|
|
function checkParam($variable, $type=null){
|
2015-11-18 08:48:08 +00:00
|
|
|
$checker = isset($variable);
|
|
|
|
|
|
|
|
// traitement en fonction du type
|
|
|
|
switch($type){
|
|
|
|
case 'utilisateur.identifiant':
|
|
|
|
return $checker && is_string($variable) && preg_match('/^[\w -]{3,50}$/i', $variable);
|
|
|
|
break;
|
|
|
|
case '<string>':
|
|
|
|
return $checker && is_string($variable);
|
|
|
|
break;
|
|
|
|
case '<int>':
|
|
|
|
return $checker && is_int($variable);
|
|
|
|
break;
|
|
|
|
case '<numeric>':
|
|
|
|
return $checker && is_numeric($variable);
|
|
|
|
break;
|
|
|
|
case '<array>':
|
|
|
|
return $checker && is_array($variable);
|
|
|
|
break;
|
|
|
|
case '<boolean>':
|
|
|
|
return $checker && is_bool($variable);
|
|
|
|
break;
|
2015-11-10 19:55:40 +00:00
|
|
|
}
|
2015-11-18 08:48:08 +00:00
|
|
|
|
2015-11-19 07:58:15 +00:00
|
|
|
return $checker;
|
2015-11-11 07:05:51 +00:00
|
|
|
|
2015-11-10 19:55:40 +00:00
|
|
|
}
|
2015-11-11 17:23:37 +00:00
|
|
|
|
2015-11-17 18:02:28 +00:00
|
|
|
|
2015-12-06 18:42:02 +00:00
|
|
|
/* [2] AUTOLOADER
|
|
|
|
================================================================*/
|
|
|
|
function autoLoader($className) {
|
|
|
|
// si on charge un Repo
|
|
|
|
if( preg_match('/^([a-z]+)Repo$/', $className, $match) ){
|
2015-12-06 19:08:28 +00:00
|
|
|
// si le fichier existe, on charge le repo
|
|
|
|
if( is_file(__ROOT__.DIRECTORY_SEPARATOR.'manager'.DIRECTORY_SEPARATOR.'repo'.DIRECTORY_SEPARATOR.$match[1].'.php') )
|
2015-12-06 18:42:02 +00:00
|
|
|
require_once __ROOT__.DIRECTORY_SEPARATOR.'manager'.DIRECTORY_SEPARATOR.'repo'.DIRECTORY_SEPARATOR.$match[1].'.php';
|
|
|
|
}
|
2015-11-17 18:02:28 +00:00
|
|
|
|
2015-12-06 18:42:02 +00:00
|
|
|
// si c'est un manager
|
2015-12-06 19:08:28 +00:00
|
|
|
elseif( preg_match('/^([a-z]+)Manager$/', $className, $match) ){
|
|
|
|
// si le fichier existe, on charge le manager
|
|
|
|
if( is_file(__ROOT__.DIRECTORY_SEPARATOR.'manager'.DIRECTORY_SEPARATOR.$match[1].'.php') )
|
|
|
|
require_once __ROOT__.DIRECTORY_SEPARATOR.'manager'.DIRECTORY_SEPARATOR.$match[1].'.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
// sinon => DataBase, on le charge
|
2015-12-06 18:42:02 +00:00
|
|
|
elseif( is_file(__ROOT__.DIRECTORY_SEPARATOR.'manager'.DIRECTORY_SEPARATOR.strtolower($className).'.php') )
|
2015-12-06 19:08:28 +00:00
|
|
|
require_once __ROOT__.DIRECTORY_SEPARATOR.'manager'.DIRECTORY_SEPARATOR.strtolower($className).'.php';
|
|
|
|
else
|
|
|
|
var_dump('ok');
|
2015-11-17 18:02:28 +00:00
|
|
|
|
|
|
|
|
2015-12-06 18:42:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
spl_autoload_register('autoLoader', false, true);
|
2015-11-11 17:25:22 +00:00
|
|
|
?>
|