2016-07-04 09:04:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace manager;
|
|
|
|
|
|
|
|
use \manager\Repo;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Authentification{
|
|
|
|
|
|
|
|
// Constantes
|
|
|
|
public static $config_path = 'f/json/modules/auth';
|
|
|
|
|
|
|
|
// Contiendra les erreurs
|
|
|
|
public $error;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* INITIALISATION DU SYSTEME ET MISE A JOUR CONSTANTES D'AUTHENTIFICATION
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function init(){
|
|
|
|
/* (1) Initialisation
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( !isset($_SESSION['AUTH']) ) $_SESSION['AUTH'] = array();
|
|
|
|
if( !isset($_SESSION['PERM']) ) $_SESSION['PERM'] = array();
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Gestion de AUTH (authentification)
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Si Auth dans HEADER, on le récupère */
|
|
|
|
$AUTH = isset($_SERVER['PHP_AUTH_DIGEST']) ? $_SERVER['PHP_AUTH_DIGEST'] : '';
|
|
|
|
|
|
|
|
/* (2) Si Auth dans SESSION, on le récupère */
|
|
|
|
if( $AUTH == '' && isset($_SESSION['AUTH']) )
|
|
|
|
$AUTH = implode('', $_SESSION['AUTH']);
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Gestion de AUTH en fonction des tokens
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Double authentification */
|
|
|
|
if( preg_match('/^([a-f0-9]{40})([a-f0-9]{40})$/', $AUTH, $match) )
|
|
|
|
$_SESSION['AUTH'] = array( $match[1], $match[2] );
|
|
|
|
|
|
|
|
/* (2) Authentification unique */
|
|
|
|
else if( preg_match('/^[a-f0-9]{40}$/', $AUTH, $match) )
|
|
|
|
$_SESSION['AUTH'] = array( $match[0] );
|
|
|
|
|
|
|
|
/* (3) Aucune authentification */
|
|
|
|
else
|
|
|
|
$_SESSION['AUTH'] = array();
|
|
|
|
|
|
|
|
|
2016-07-04 09:29:48 +00:00
|
|
|
/* (4) On vérifie l'authentification par BDD
|
2016-07-04 09:04:49 +00:00
|
|
|
---------------------------------------------------------*/
|
2016-07-04 09:29:48 +00:00
|
|
|
return self::check();
|
2016-07-04 09:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* VERIFICATION DE L'AUTHENTIFICATION
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function check(){
|
|
|
|
/* [1] Si aucune authentification
|
|
|
|
=========================================================*/
|
2016-07-04 09:29:48 +00:00
|
|
|
if( authLevel() == 0 )
|
|
|
|
return true;
|
2016-07-04 09:04:49 +00:00
|
|
|
|
|
|
|
/* [2] Si authentification unique
|
|
|
|
=========================================================*/
|
2016-07-04 09:29:48 +00:00
|
|
|
if( authLevel() >= 1 ){
|
|
|
|
$checkRoot = new Repo('warehouse/getByToken', array($_SESSION['AUTH'][0]));
|
|
|
|
|
|
|
|
// Si le token n'existe pas
|
|
|
|
if( $checkRoot->answer() === false )
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-04 09:04:49 +00:00
|
|
|
|
|
|
|
/* [3] Si authentification double
|
|
|
|
=========================================================*/
|
2016-07-04 09:29:48 +00:00
|
|
|
if( authLevel() >= 2 ){
|
|
|
|
$checkBranch = new Repo('admin/getByToken', array($_SESSION['AUTH'][1]));
|
|
|
|
|
|
|
|
// Si le token n'existe pas
|
|
|
|
if( $checkBranch->answer() === false )
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-04 09:04:49 +00:00
|
|
|
|
2016-07-04 09:29:48 +00:00
|
|
|
/* [3] Si pas d'erreur d'authentification, on retourne TRUE
|
|
|
|
=========================================================*/
|
|
|
|
return true;
|
2016-07-04 09:04:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|