LSsession : use LSlog_logger

This commit is contained in:
Benjamin Renard 2020-05-08 15:51:21 +02:00
parent 108b14560d
commit ca7a6e821e

View file

@ -122,7 +122,7 @@ class LSsession {
return false; return false;
$log_msg = "includeFile($file, external=$external) : file $path not found in include path."; $log_msg = "includeFile($file, external=$external) : file $path not found in include path.";
if (class_exists('LSlog')) if (class_exists('LSlog'))
LSlog :: warning($log_msg); self :: log_warning($log_msg);
else else
error_log($log_msg); error_log($log_msg);
return false; return false;
@ -136,7 +136,7 @@ class LSsession {
return false; return false;
$log_msg = "includeFile($file, external=$external) : file not found ($local_path / $path)"; $log_msg = "includeFile($file, external=$external) : file not found ($local_path / $path)";
if (class_exists('LSlog')) if (class_exists('LSlog'))
LSlog :: warning($log_msg); self :: log_warning($log_msg);
else else
error_log($log_msg); error_log($log_msg);
return false; return false;
@ -177,6 +177,88 @@ class LSsession {
return False; return False;
} }
/*
* Log a message via class logger (of other method if LSlog is not loaded)
*
* @param[in] $level string The log level (see LSlog)
* @param[in] $message string The message to log
*
* @retval void
**/
protected static function log($level, $message) {
if (class_exists('LSlog')) {
LSlog :: get_logger(get_called_class()) -> logging($level, $message);
return;
}
// Alternative logging if LSlog is not already started
$formated_message = "LSsession - $level - $message";
switch ($level) {
case 'FATAL':
case 'ERROR':
error_log($formated_message);
if ($level == 'FATAL')
die($formated_message);
break;
default:
LSdebug($formated_message);
}
}
/**
* Log a message with level DEBUG
*
* @param[in] $message The message to log
*
* @retval void
**/
protected static function log_debug($message) {
self :: log('DEBUG', $message);
}
/**
* Log a message with level INFO
*
* @param[in] $message The message to log
*
* @retval void
**/
protected static function log_info($message) {
self :: log('INFO', $message);
}
/**
* Log a message with level WARNING
*
* @param[in] $message The message to log
*
* @retval void
**/
protected static function log_warning($message) {
self :: log('WARNING', $message);
}
/**
* Log a message with level ERROR
*
* @param[in] $message The message to log
*
* @retval void
**/
protected static function log_error($message) {
self :: log('ERROR', $message);
}
/**
* Log a message with level FATAL
*
* @param[in] $message The message to log
*
* @retval void
**/
protected static function log_fatal($message) {
self :: log('FATAL', $message);
}
/** /**
* Lancement de LSurl * Lancement de LSurl
* *
@ -298,30 +380,30 @@ class LSsession {
$error = 0; $error = 0;
self :: loadLSclass('LSldapObject'); self :: loadLSclass('LSldapObject');
if (!self :: loadLSclass($object,'LSobjects')) { if (!self :: loadLSclass($object,'LSobjects')) {
LSlog :: error("LSsession :: loadLSobject($object) : Fail to load LSldapObject class"); self :: log_error("loadLSobject($object): Fail to load LSldapObject class");
$error = 1; $error = 1;
} }
if (!self :: includeFile( LS_OBJECTS_DIR . 'config.LSobjects.'.$object.'.php' )) { if (!self :: includeFile( LS_OBJECTS_DIR . 'config.LSobjects.'.$object.'.php' )) {
LSlog :: error("LSsession :: loadLSobject($object) : Fail to include 'config.LSobjects.$object.php' file"); self :: log_error("loadLSobject($object): Fail to include 'config.LSobjects.$object.php' file");
$error = 1; $error = 1;
} }
else { else {
if (!LSconfig :: set("LSobjects.$object",$GLOBALS['LSobjects'][$object])) { if (!LSconfig :: set("LSobjects.$object",$GLOBALS['LSobjects'][$object])) {
LSlog :: error("LSsession :: loadLSobject($object) : Fail to LSconfig :: set('LSobjects.$object', \$GLOBALS['LSobjects'][$object])"); self :: log_error("loadLSobject($object): Fail to LSconfig :: set('LSobjects.$object', \$GLOBALS['LSobjects'][$object])");
$error = 1; $error = 1;
} }
else if (isset($GLOBALS['LSobjects'][$object]['LSaddons'])){ else if (isset($GLOBALS['LSobjects'][$object]['LSaddons'])){
if (is_array($GLOBALS['LSobjects'][$object]['LSaddons'])) { if (is_array($GLOBALS['LSobjects'][$object]['LSaddons'])) {
foreach ($GLOBALS['LSobjects'][$object]['LSaddons'] as $addon) { foreach ($GLOBALS['LSobjects'][$object]['LSaddons'] as $addon) {
if (!self :: loadLSaddon($addon)) { if (!self :: loadLSaddon($addon)) {
LSlog :: error("LSsession :: loadLSobject($object) : Fail to load LSaddon '$addon'"); self :: log_error("loadLSobject($object): Fail to load LSaddon '$addon'");
$error = 1; $error = 1;
} }
} }
} }
else { else {
if (!self :: loadLSaddon($GLOBALS['LSobjects'][$object]['LSaddons'])) { if (!self :: loadLSaddon($GLOBALS['LSobjects'][$object]['LSaddons'])) {
LSlog :: error("LSsession :: loadLSobject($object) : Fail to load LSaddon '".$GLOBALS['LSobjects'][$object]['LSaddons']."'"); self :: log_error("loadLSobject($object): Fail to load LSaddon '".$GLOBALS['LSobjects'][$object]['LSaddons']."'");
$error = 1; $error = 1;
} }
} }
@ -348,9 +430,9 @@ class LSsession {
// Load LSaddon config file (without warning if not found) // Load LSaddon config file (without warning if not found)
$conf_file = LS_CONF_DIR."LSaddons/config.LSaddons.".$addon.".php"; $conf_file = LS_CONF_DIR."LSaddons/config.LSaddons.".$addon.".php";
if (self :: includeFile($conf_file, false, false)) if (self :: includeFile($conf_file, false, false))
LSlog :: debug("LSsession :: loadLSaddon($addon): config file '$conf_file' loaded."); self :: log_debug("loadLSaddon($addon): config file '$conf_file' loaded.");
else else
LSlog :: debug("LSsession :: loadLSaddon($addon): config file '$conf_file' not found."); self :: log_debug("loadLSaddon($addon): config file '$conf_file' not found.");
if (!call_user_func('LSaddon_'. $addon .'_support')) { if (!call_user_func('LSaddon_'. $addon .'_support')) {
LSerror :: addErrorCode('LSsession_02',$addon); LSerror :: addErrorCode('LSsession_02',$addon);
return; return;
@ -488,7 +570,7 @@ class LSsession {
} }
if(isset($_SESSION['LSsession']['dn']) && !isset($_GET['LSsession_recoverPassword'])) { if(isset($_SESSION['LSsession']['dn']) && !isset($_GET['LSsession_recoverPassword'])) {
LSlog :: debug('LSsession : existing session'); self :: log_debug('existing session');
// --------------------- Session existante --------------------- // // --------------------- Session existante --------------------- //
self :: $topDn = $_SESSION['LSsession']['topDn']; self :: $topDn = $_SESSION['LSsession']['topDn'];
self :: $dn = $_SESSION['LSsession']['dn']; self :: $dn = $_SESSION['LSsession']['dn'];
@ -500,7 +582,7 @@ class LSsession {
if ( self :: cacheLSprofiles() && !isset($_REQUEST['LSsession_refresh']) ) { if ( self :: cacheLSprofiles() && !isset($_REQUEST['LSsession_refresh']) ) {
self :: setLdapServer(self :: $ldapServerId); self :: setLdapServer(self :: $ldapServerId);
if (!LSauth :: start()) { if (!LSauth :: start()) {
LSlog :: error("LSsession :: startLSsession() : can't start LSauth -> stop"); self :: log_error("startLSsession(): can't start LSauth -> stop");
return; return;
} }
self :: $LSprofiles = $_SESSION['LSsession']['LSprofiles']; self :: $LSprofiles = $_SESSION['LSsession']['LSprofiles'];
@ -512,7 +594,7 @@ class LSsession {
else { else {
self :: setLdapServer(self :: $ldapServerId); self :: setLdapServer(self :: $ldapServerId);
if (!LSauth :: start()) { if (!LSauth :: start()) {
LSlog :: error("LSsession :: startLSsession() : can't start LSauth -> stop"); self :: log_error("startLSsession(): can't start LSauth -> stop");
return; return;
} }
if (!self :: LSldapConnect()) if (!self :: LSldapConnect())
@ -597,7 +679,7 @@ class LSsession {
$_SESSION['LSsession_topDn']=self :: $topDn; $_SESSION['LSsession_topDn']=self :: $topDn;
if (!LSauth :: start()) { if (!LSauth :: start()) {
LSlog :: error("LSsession :: startLSsession() : can't start LSauth -> stop"); self :: log_error("startLSsession(): can't start LSauth -> stop");
return; return;
} }
@ -705,26 +787,26 @@ class LSsession {
$nbresult=count($result); $nbresult=count($result);
if ($nbresult==0) { if ($nbresult==0) {
LSlog :: debug('LSsession :: recoverPasswd() : incorrect hash/username'); self :: log_debug('recoverPasswd(): incorrect hash/username');
LSerror :: addErrorCode('LSsession_06'); LSerror :: addErrorCode('LSsession_06');
} }
elseif ($nbresult>1) { elseif ($nbresult>1) {
LSlog :: debug("LSsession :: recoverPasswd() : duplicated user found with hash/username '$username'"); self :: log_debug("recoverPasswd(): duplicated user found with hash/username '$username'");
LSerror :: addErrorCode('LSsession_07'); LSerror :: addErrorCode('LSsession_07');
} }
else { else {
$rdn = $result[0] -> getValue('rdn'); $rdn = $result[0] -> getValue('rdn');
$username = $rdn[0]; $username = $rdn[0];
LSlog :: debug("LSsession :: recoverPasswd() : user found, username = '$username'"); self :: log_debug("recoverPasswd(): user found, username = '$username'");
if (self :: $ldapServer['recoverPassword']) { if (self :: $ldapServer['recoverPassword']) {
if (self :: loadLSaddon('mail')) { if (self :: loadLSaddon('mail')) {
LSlog :: debug("LSsession :: recoverPasswd() : start recovering password"); self :: log_debug("recoverPasswd(): start recovering password");
$user=$result[0]; $user=$result[0];
$emailAddress = $user -> getValue(self :: $ldapServer['recoverPassword']['mailAttr']); $emailAddress = $user -> getValue(self :: $ldapServer['recoverPassword']['mailAttr']);
$emailAddress = $emailAddress[0]; $emailAddress = $emailAddress[0];
if (checkEmail($emailAddress)) { if (checkEmail($emailAddress)) {
LSlog :: debug("LSsession :: recoverPasswd() : Email = '$emailAddress'"); self :: log_debug("recoverPasswd(): Email = '$emailAddress'");
self :: $dn = $user -> getDn(); self :: $dn = $user -> getDn();
// 1ère étape : envoie du recoveryHash // 1ère étape : envoie du recoveryHash
@ -793,7 +875,7 @@ class LSsession {
} }
if (!sendMail($mail,$subject,$msg,$sendParams)) { if (!sendMail($mail,$subject,$msg,$sendParams)) {
LSlog :: debug("LSsession :: recoverPasswdSendMail($mail, $step) : error sending email."); self :: log_debug("recoverPasswdSendMail($mail, $step): error sending email.");
LSerror :: addErrorCode('LSsession_20',4); LSerror :: addErrorCode('LSsession_20',4);
return; return;
} }
@ -829,13 +911,13 @@ class LSsession {
} }
else { else {
// Erreur durant la mise à jour de l'objet // Erreur durant la mise à jour de l'objet
LSlog :: error("LSsession :: recoverPasswdFirstStep($user) : error updating user."); self :: log_error("recoverPasswdFirstStep($user): error updating user.");
LSerror :: addErrorCode('LSsession_20',6); LSerror :: addErrorCode('LSsession_20',6);
} }
} }
else { else {
// Erreur durant la validation du formulaire de modification de perte de password // Erreur durant la validation du formulaire de modification de perte de password
LSlog :: error("LSsession :: recoverPasswdFirstStep($user) : error validating form."); self :: log_error("recoverPasswdFirstStep($user): error validating form.");
LSerror :: addErrorCode('LSsession_20',5); LSerror :: addErrorCode('LSsession_20',5);
} }
return; return;
@ -855,7 +937,7 @@ class LSsession {
$attr -> config['html_options']['chars'], $attr -> config['html_options']['chars'],
$attr -> config['html_options']['lenght'] $attr -> config['html_options']['lenght']
); );
LSlog :: debug("LSsession :: recoverPasswdSecondStep($user) : new password = '$mdp'."); self :: log_debug("recoverPasswdSecondStep($user): new password = '$mdp'.");
$lostPasswdForm = $user -> getForm('lostPassword'); $lostPasswdForm = $user -> getForm('lostPassword');
$lostPasswdForm -> setPostData( $lostPasswdForm -> setPostData(
array( array(
@ -870,19 +952,19 @@ class LSsession {
} }
else { else {
// Erreur durant la mise à jour de l'objet // Erreur durant la mise à jour de l'objet
LSlog :: error("LSsession :: recoverPasswdSecondStep($user) : error updating user."); self :: log_error("recoverPasswdSecondStep($user): error updating user.");
LSerror :: addErrorCode('LSsession_20',3); LSerror :: addErrorCode('LSsession_20',3);
} }
} }
else { else {
// Erreur durant la validation du formulaire de modification de perte de password // Erreur durant la validation du formulaire de modification de perte de password
LSlog :: error("LSsession :: recoverPasswdSecondStep($user) : error validating form."); self :: log_error("recoverPasswdSecondStep($user): error validating form.");
LSerror :: addErrorCode('LSsession_20',2); LSerror :: addErrorCode('LSsession_20',2);
} }
} }
else { else {
// l'attribut password n'existe pas // l'attribut password n'existe pas
LSlog :: error("LSsession :: recoverPasswdSecondStep($user) : password attribute '$attr' does not exists."); self :: log_error("recoverPasswdSecondStep($user): password attribute '$attr' does not exists.");
LSerror :: addErrorCode('LSsession_20',1); LSerror :: addErrorCode('LSsession_20',1);
} }
return; return;
@ -1530,17 +1612,17 @@ class LSsession {
if (! isset($filter_def['filter']) && if (! isset($filter_def['filter']) &&
(! isset($filter_def['attr']) || (! isset($filter_def['attr']) ||
! isset($filter_def['attr_value']))) { ! isset($filter_def['attr_value']))) {
LSlog :: debug("LSsession :: reduceLdapSet() : LSobject LSprofil filter invalid : " . varDump($filter_def)); self :: log_debug("reduceLdapSet(): LSobject LSprofil filter invalid : " . varDump($filter_def));
return array(); return array();
} }
LSlog :: debug('LSsession :: reduceLdapSet() : reducing set of'); self :: log_debug('reduceLdapSet(): reducing set of');
foreach ($set as $object) { foreach ($set as $object) {
LSdebug('LSsession :: -> ' . $object -> getDn()); LSdebug('LSsession :: -> ' . $object -> getDn());
} }
$LSobject = isset($filter_def['LSObject']) ? $filter_def['LSobject'] : $LSobject; $LSobject = isset($filter_def['LSObject']) ? $filter_def['LSobject'] : $LSobject;
LSlog :: debug('LSsession :: reduceLdapSet() : LSobject = ' . $LSobject); self :: log_debug('reduceLdapSet(): LSobject = ' . $LSobject);
$filters = array(); $filters = array();
foreach ($set as $object) { foreach ($set as $object) {
if (isset($filter_def['filter'])) { if (isset($filter_def['filter'])) {
@ -1564,9 +1646,9 @@ class LSsession {
$LSsearch -> run(false); $LSsearch -> run(false);
$set = $LSsearch -> listObjects(); $set = $LSsearch -> listObjects();
LSlog :: debug('LSsession :: reduceLdapSet() : reduced set to'); self :: log_debug('reduceLdapSet(): reduced set to');
foreach ($set as $object) { foreach ($set as $object) {
LSlog :: debug('LSsession :: reduceLdapSet() : -> ' . $object -> getDn()); self :: log_debug('reduceLdapSet(): -> ' . $object -> getDn());
} }
return $set; return $set;
} }
@ -1580,7 +1662,7 @@ class LSsession {
*/ */
private static function loadLSprofilesLSobjects($profile, $LSobject, $listInfos) { private static function loadLSprofilesLSobjects($profile, $LSobject, $listInfos) {
if (! self :: loadLSclass('LSsearch')) { if (! self :: loadLSclass('LSsearch')) {
LSlog :: error('Fail to load class LSsearch'); self :: log_error('Fail to load class LSsearch');
return; return;
} }
# we are gonna grow a set of objects progressively, we start from the user # we are gonna grow a set of objects progressively, we start from the user
@ -1635,12 +1717,12 @@ class LSsession {
elseif ($topDn == 'LSobjects') { elseif ($topDn == 'LSobjects') {
if (is_array($rightsInfos)) { if (is_array($rightsInfos)) {
foreach ($rightsInfos as $LSobject => $listInfos) { foreach ($rightsInfos as $LSobject => $listInfos) {
LSlog :: debug('LSsession :: loadLSprofiles() : loading LSprofile ' . $profile . ' for LSobject ' . $LSobject . ' with params ' . var_export($listInfos, true)); self :: log_debug('loadLSprofiles(): loading LSprofile ' . $profile . ' for LSobject ' . $LSobject . ' with params ' . var_export($listInfos, true));
self :: loadLSprofilesLSobjects($profile, $LSobject, $listInfos); self :: loadLSprofilesLSobjects($profile, $LSobject, $listInfos);
} }
} }
else { else {
LSlog :: warning('LSsession :: loadLSprofiles() : LSobjects => [] must be an array'); self :: log_warning('loadLSprofiles(): LSobjects => [] must be an array');
} }
} }
else { else {
@ -1660,11 +1742,11 @@ class LSsession {
} }
} }
else { else {
LSlog :: warning("LSsession :: loadLSprofiles() : fail to load DN '$dn'."); self :: log_warning("loadLSprofiles(): fail to load DN '$dn'.");
} }
} }
else { else {
LSlog :: warning("LSsession :: loadLSprofiles() : fail to instanciate LSobject type '".$conf['LSobject']."'."); self :: log_warning("loadLSprofiles(): fail to instanciate LSobject type '".$conf['LSobject']."'.");
} }
} }
} }
@ -1684,7 +1766,7 @@ class LSsession {
} // fin foreach($profileInfos) } // fin foreach($profileInfos)
} // fin is_array($profileInfos) } // fin is_array($profileInfos)
} // fin foreach LSprofiles } // fin foreach LSprofiles
LSlog :: debug("LSsession :: loadLSprofiles() : LSprofiles = ".print_r(self :: $LSprofiles,1)); self :: log_debug("loadLSprofiles(): LSprofiles = ".print_r(self :: $LSprofiles,1));
return true; return true;
} }
else { else {