2 /*******************************************************************************
3 * Copyright (C) 2007 Easter-eggs
4 * http://ldapsaisie.labs.libre-entreprise.org
6 * Author: See AUTHORS file in top-level directory.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 ******************************************************************************/
24 * Gestion des sessions
26 * Cette classe gère les sessions d'utilisateurs.
28 * @author Benjamin Renard <brenard@easter-eggs.com>
32 // La configuration du serveur Ldap utilisé
33 public static $ldapServer = NULL;
35 // L'id du serveur Ldap utilisé
36 private static $ldapServerId = NULL;
39 private static $topDn = NULL;
41 // Le DN de l'utilisateur connecté
42 private static $dn = NULL;
44 // Le RDN de l'utilisateur connecté (son identifiant)
45 private static $rdn = NULL;
47 // Les LSprofiles de l'utilisateur
48 private static $LSprofiles = array();
50 // Les droits d'accès de l'utilisateur
51 private static $LSaccess = array();
53 // Les fichiers temporaires
54 private static $tmp_file = array();
56 // Langue et encodage actuel
57 private static $lang = NULL;
58 private static $encoding = NULL;
61 * Constante de classe non stockée en session
63 // Le template à afficher
64 private static $template = NULL;
66 // Les subDn des serveurs Ldap
67 private static $_subDnLdapServer = array();
70 private static $ajaxDisplay = false;
72 // Les fichiers JS à charger dans la page
73 private static $JSscripts = array();
75 // Les paramètres JS à communiquer dans la page
76 private static $_JSconfigParams = array();
78 // Les fichiers CSS à charger dans la page
79 private static $CssFiles = array();
81 // L'objet de l'utilisateur connecté
82 private static $LSuserObject = NULL;
84 // The LSauht object of the session
85 private static $LSauthObject = false;
88 * Include un fichier PHP
90 * @author Benjamin Renard <brenard@easter-eggs.com>
92 * @retval true si tout c'est bien passé, false sinon
94 public static function includeFile($file) {
95 if (!file_exists($file)) {
98 if (defined('LSdebug') && constant('LSdebug')) {
99 return include_once($file);
102 return @include_once($file);
108 * Lancement de LSconfig
110 * @author Benjamin Renard <brenard@easter-eggs.com>
112 * @retval true si tout c'est bien passé, false sinon
114 private static function startLSconfig() {
115 if (self :: loadLSclass('LSconfig')) {
116 if (LSconfig :: start()) {
120 die("ERROR : Can't load configuration files.");
125 * Lancement et initialisation de Smarty
127 * @author Benjamin Renard <brenard@easter-eggs.com>
129 * @retval true si tout c'est bien passé, false sinon
131 private static function startLStemplate() {
132 if ( self :: includeFile(LSconfig :: get('Smarty')) ) {
133 $GLOBALS['Smarty'] = new Smarty();
134 $GLOBALS['Smarty'] -> template_dir = LS_TEMPLATES_DIR;
135 $GLOBALS['Smarty'] -> compile_dir = LS_TMP_DIR;
136 if ( ! is_writable(LS_TMP_DIR) ) {
137 die('Smarty compile directory is not writable (dir : '.LS_TMP_DIR.')');
141 $GLOBALS['Smarty'] -> caching = 0;
142 // cache files are always regenerated
143 $GLOBALS['Smarty'] -> force_compile = TRUE;
144 // recompile template if it is changed
145 $GLOBALS['Smarty'] -> compile_check = TRUE;
146 if (isset($_REQUEST['debug_smarty'])) {
148 $GLOBALS['Smarty'] -> debugging = true;
152 $GLOBALS['Smarty'] -> register_function('getFData','smarty_getFData');
154 $GLOBALS['Smarty'] -> assign('LS_CSS_DIR',LS_CSS_DIR);
155 $GLOBALS['Smarty'] -> assign('LS_IMAGES_DIR',LS_IMAGES_DIR);
157 self :: addJSconfigParam('LS_IMAGES_DIR',LS_IMAGES_DIR);
160 die("ERROR : Can't load Smarty.");
165 * Retourne le topDn de la session
167 * @author Benjamin Renard <brenard@easter-eggs.com>
169 * @retval string le topDn de la session
171 public static function getTopDn() {
172 if (!is_null(self :: $topDn)) {
173 return self :: $topDn;
176 return self :: getRootDn();
181 * Retourne le rootDn de la session
183 * @author Benjamin Renard <brenard@easter-eggs.com>
185 * @retval string le rootDn de la session
187 public static function getRootDn() {
188 return self :: $ldapServer['ldap_config']['basedn'];
192 * Initialisation de la gestion des erreurs
194 * Création de l'objet LSerror
196 * @author Benjamin Renard <brenard@easter-eggs.com
198 * @retval boolean true si l'initialisation a réussi, false sinon.
200 private static function startLSerror() {
201 if(!self :: loadLSclass('LSerror')) {
204 self :: defineLSerrors();
209 * Chargement d'une classe d'LdapSaisie
211 * @param[in] $class Nom de la classe à charger (Exemple : LSpeople)
212 * @param[in] $type (Optionnel) Type de classe à charger (Exemple : LSobjects)
214 * @author Benjamin Renard <brenard@easter-eggs.com
216 * @retval boolean true si le chargement a réussi, false sinon.
218 public static function loadLSclass($class,$type='') {
219 if (class_exists($class))
223 return self :: includeFile(LS_CLASS_DIR .'class.'.$type.$class.'.php');
227 * Chargement d'un object LdapSaisie
229 * @param[in] $object Nom de l'objet à charger
231 * @retval boolean true si le chargement a réussi, false sinon.
233 public static function loadLSobject($object) {
234 if(class_exists($object)) {
238 self :: loadLSclass('LSldapObject');
239 if (!self :: loadLSclass($object,'LSobjects')) {
242 if (!self :: includeFile( LS_OBJECTS_DIR . 'config.LSobjects.'.$object.'.php' )) {
246 if (!LSconfig :: set("LSobjects.$object",$GLOBALS['LSobjects'][$object])) {
249 else if (isset($GLOBALS['LSobjects'][$object]['LSaddons'])){
250 if (is_array($GLOBALS['LSobjects'][$object]['LSaddons'])) {
251 foreach ($GLOBALS['LSobjects'][$object]['LSaddons'] as $addon) {
252 if (!self :: loadLSaddon($addon)) {
258 if (!self :: loadLSaddon($GLOBALS['LSobjects'][$object]['LSaddons'])) {
265 LSerror :: addErrorCode('LSsession_04',$object);
272 * Chargement d'un addons d'LdapSaisie
274 * @param[in] $addon Nom de l'addon à charger (Exemple : samba)
276 * @author Benjamin Renard <brenard@easter-eggs.com
278 * @retval boolean true si le chargement a réussi, false sinon.
280 public static function loadLSaddon($addon) {
281 if(self :: includeFile(LS_ADDONS_DIR .'LSaddons.'.$addon.'.php')) {
282 self :: includeFile(LS_CONF_DIR."LSaddons/config.LSaddons.".$addon.".php");
283 if (!call_user_func('LSaddon_'. $addon .'_support')) {
284 LSerror :: addErrorCode('LSsession_02',$addon);
293 * Chargement d'une classe d'authentification d'LdapSaisie
295 * @author Benjamin Renard <brenard@easter-eggs.com
297 * @retval boolean true si le chargement a reussi, false sinon.
299 public static function loadLSauth() {
300 if (self :: loadLSclass('LSauth')) {
304 LSerror :: addErrorCode('LSsession_05','LSauth');
310 * Chargement des addons LdapSaisie
312 * Chargement des LSaddons contenue dans la variable
313 * $GLOBALS['LSaddons']['loads']
315 * @retval boolean true si le chargement a réussi, false sinon.
317 public static function loadLSaddons() {
318 $conf=LSconfig :: get('LSaddons.loads');
319 if(!is_array($conf)) {
320 LSerror :: addErrorCode('LSsession_01',"LSaddons['loads']");
324 foreach ($conf as $addon) {
325 self :: loadLSaddon($addon);
335 public static function setLocale() {
336 if (isset($_REQUEST['lang'])) {
337 $lang = $_REQUEST['lang'];
339 elseif (isset($_SESSION['LSlang'])) {
340 $lang = $_SESSION['LSlang'];
342 elseif (isset(self :: $ldapServer['lang'])) {
343 $lang = self :: $ldapServer['lang'];
346 $lang = LSconfig :: get('lang');
349 if (isset($_REQUEST['encoding'])) {
350 $encoding = $_REQUEST['encoding'];
352 elseif (isset($_SESSION['LSencoding'])) {
353 $encoding = $_SESSION['LSencoding'];
355 elseif (isset(self :: $ldapServer['encoding'])) {
356 $encoding = self :: $ldapServer['encoding'];
359 $encoding = LSconfig :: get('encoding');
362 $_SESSION['LSlang']=$lang;
364 $_SESSION['LSencoding']=$encoding;
365 self :: $encoding=$encoding;
368 if (self :: localeExist($lang,$encoding)) {
370 $lang.='.'.$encoding;
372 setlocale(LC_ALL, $lang);
373 bindtextdomain(LS_TEXT_DOMAIN, LS_I18N_DIR);
374 textdomain(LS_TEXT_DOMAIN);
376 if (is_file(LS_I18N_DIR.'/'.$lang.'/lang.php')) {
377 include(LS_I18N_DIR.'/'.$lang.'/lang.php');
379 foreach (listFiles(LS_I18N_DIR.'/'.$lang,'/^lang.+\.php$/') as $file) {
380 include(LS_I18N_DIR."/$lang/$file");
384 if ($encoding && $lang) {
385 $lang.='.'.$encoding;
387 LSdebug('La locale "'.$lang.'" n\'existe pas, utilisation de la locale par défaut.');
392 * Retourne la liste des langues disponibles
394 * @retval array Tableau/Liste des langues disponibles
396 public static function getLangList() {
397 $list=array('en_US');
398 if (self :: $encoding) {
399 $regex = '^([a-zA-Z_]*)\.'.self :: $encoding.'$';
402 $regex = '^([a-zA-Z_]*)$';
404 if ($handle = opendir(LS_I18N_DIR)) {
405 while (false !== ($file = readdir($handle))) {
406 if(is_dir(LS_I18N_DIR.'/'.$file)) {
407 if (ereg($regex,$file,$regs)) {
408 if (!in_array($regs[1],$list)) {
419 * Retourne la langue courante de la session
421 * @param[in] boolean Si true, le code langue retourné sera court
423 * @retval string La langue de la session
425 public static function getLang($short=false) {
427 return strtolower(self :: $lang[0].self :: $lang[1]);
429 return self :: $lang;
433 * Vérifie si une locale est disponible
435 * @param[in] $lang string La langue (Ex : fr_FR)
436 * @param[in] $encoding string L'encodage de caractère (Ex : UTF8)
438 * @retval boolean True si la locale est disponible, False sinon
440 public static function localeExist($lang,$encoding) {
441 if ( !$lang && !$encoding ) {
444 $locale=$lang.(($encoding)?'.'.$encoding:'');
445 if ($locale=='en_US.UTF8') {
448 return (is_dir(LS_I18N_DIR.'/'.$locale));
452 * Initialisation LdapSaisie
454 * @retval boolean True si l'initialisation à réussi, false sinon.
456 public static function initialize() {
457 if (!self :: startLSconfig()) {
461 self :: startLStemplate();
467 self :: startLSerror();
468 self :: loadLSaddons();
469 self :: loadLSauth();
474 * Initialisation de la session LdapSaisie
476 * Initialisation d'une LSsession :
477 * - Authentification et activation du mécanisme de session de LdapSaisie
478 * - ou Chargement des paramètres de la session à partir de la variable
479 * $_SESSION['LSsession'].
480 * - ou Destruction de la session en cas de $_GET['LSsession_logout'].
482 * @retval boolean True si l'initialisation à réussi (utilisateur authentifié), false sinon.
484 public static function startLSsession() {
485 if (!self :: initialize()) {
489 if(isset($_SESSION['LSsession']['dn']) && !isset($_GET['LSsession_recoverPassword'])) {
490 LSdebug('LSsession : Session existente');
491 // --------------------- Session existante --------------------- //
492 self :: $topDn = $_SESSION['LSsession']['topDn'];
493 self :: $dn = $_SESSION['LSsession']['dn'];
494 self :: $rdn = $_SESSION['LSsession']['rdn'];
495 self :: $ldapServerId = $_SESSION['LSsession']['ldapServerId'];
496 self :: $tmp_file = $_SESSION['LSsession']['tmp_file'];
498 if ( self :: cacheLSprofiles() && !isset($_REQUEST['LSsession_refresh']) ) {
499 self :: setLdapServer(self :: $ldapServerId);
500 if (!LSauth :: start()) {
501 LSdebug("LSsession : can't start LSauth -> stop");
504 self :: $LSprofiles = $_SESSION['LSsession']['LSprofiles'];
505 self :: $LSaccess = $_SESSION['LSsession']['LSaccess'];
506 if (!self :: LSldapConnect())
510 self :: setLdapServer(self :: $ldapServerId);
511 if (!LSauth :: start()) {
512 LSdebug("LSsession : can't start LSauth -> stop");
515 if (!self :: LSldapConnect())
517 self :: loadLSprofiles();
520 if ( self :: cacheSudDn() && (!isset($_REQUEST['LSsession_refresh'])) ) {
521 self :: $_subDnLdapServer = ((isset($_SESSION['LSsession_subDnLdapServer']))?$_SESSION['LSsession_subDnLdapServer']:NULL);
524 if (!self :: loadLSobject(self :: $ldapServer['authObjectType'])) {
528 if (isset($_GET['LSsession_logout'])) {
532 if (is_array($_SESSION['LSsession']['tmp_file'])) {
533 self :: $tmp_file = $_SESSION['LSsession']['tmp_file'];
535 self :: deleteTmpFile();
536 unset($_SESSION['LSsession']);
538 self :: redirect('index.php');
542 if ( !self :: cacheLSprofiles() || isset($_REQUEST['LSsession_refresh']) ) {
543 self :: loadLSaccess();
546 $GLOBALS['Smarty'] -> assign('LSsession_username',self :: getLSuserObject() -> getDisplayName());
548 if (isset ($_POST['LSsession_topDn']) && $_POST['LSsession_topDn']) {
549 if (self :: validSubDnLdapServer($_POST['LSsession_topDn'])) {
550 self :: $topDn = $_POST['LSsession_topDn'];
551 $_SESSION['LSsession']['topDn'] = $_POST['LSsession_topDn'];
559 // --------------------- Session inexistante --------------------- //
560 if (isset($_GET['LSsession_recoverPassword'])) {
563 // Session inexistante
564 if (isset($_POST['LSsession_ldapserver'])) {
565 self :: setLdapServer($_POST['LSsession_ldapserver']);
568 self :: setLdapServer(0);
571 // Connexion au serveur LDAP
572 if (self :: LSldapConnect()) {
575 if (isset($_POST['LSsession_topDn']) && $_POST['LSsession_topDn'] != '' ){
576 self :: $topDn = $_POST['LSsession_topDn'];
579 self :: $topDn = self :: $ldapServer['ldap_config']['basedn'];
581 $_SESSION['LSsession_topDn']=self :: $topDn;
583 if (!LSauth :: start()) {
584 LSdebug("LSsession : can't start LSauth -> stop");
588 if (isset($_GET['LSsession_recoverPassword'])) {
589 $recoveryPasswordInfos = self :: recoverPasswd(
590 $_REQUEST['LSsession_user'],
591 $_GET['recoveryHash']
595 $LSuserObject = LSauth :: forceAuthentication();
597 // Authentication successful
598 self :: $LSuserObject = $LSuserObject;
599 self :: $dn = $LSuserObject->getValue('dn');
600 self :: $rdn = $LSuserObject->getValue('rdn');
601 self :: loadLSprofiles();
602 self :: loadLSaccess();
603 $GLOBALS['Smarty'] -> assign('LSsession_username',self :: getLSuserObject() -> getDisplayName());
604 $_SESSION['LSsession']=self :: getContextInfos();
610 LSerror :: addErrorCode('LSsession_09');
613 if (self :: $ldapServerId) {
614 $GLOBALS['Smarty'] -> assign('ldapServerId',self :: $ldapServerId);
616 $GLOBALS['Smarty'] -> assign('topDn',self :: $topDn);
617 if (isset($_GET['LSsession_recoverPassword'])) {
618 self :: displayRecoverPasswordForm($recoveryPasswordInfos);
620 elseif(LSauth :: displayLoginForm()) {
621 self :: displayLoginForm();
624 self :: setTemplate('blank.tpl');
625 LSerror :: addErrorCode('LSsession_10');
632 * Do recover password
634 * @param[in] $username string The submited username
635 * @param[in] $recoveryHash string The submited recoveryHash
637 * @retval array The recoveryPassword infos for template
639 private static function recoverPasswd($username,$recoveryHash) {
640 $recoveryPasswordInfos=array();
641 if ( self :: loadLSobject(self :: $ldapServer['authObjectType']) ) {
642 $authobject = new self :: $ldapServer['authObjectType']();
643 if (!empty($recoveryHash)) {
644 $filter=Net_LDAP2_Filter::create(
645 self :: $ldapServer['recoverPassword']['recoveryHashAttr'],
649 $result = $authobject -> listObjects($filter,self :: $topDn);
651 elseif (!empty($username)) {
652 $result = $authobject -> searchObject(
655 self :: $ldapServer['authObjectFilter']
659 return $recoveryPasswordInfos;
662 $nbresult=count($result);
665 LSdebug('hash/username incorrect');
666 LSerror :: addErrorCode('LSsession_06');
668 elseif ($nbresult>1) {
669 LSerror :: addErrorCode('LSsession_07');
672 $rdn = $result[0] -> getValue('rdn');
674 LSdebug('Recover : Id trouvé : '.$username);
675 if (self :: $ldapServer['recoverPassword']) {
676 if (self :: loadLSaddon('mail')) {
677 LSdebug('Récupération active');
679 $emailAddress = $user -> getValue(self :: $ldapServer['recoverPassword']['mailAttr']);
680 $emailAddress = $emailAddress[0];
682 if (checkEmail($emailAddress)) {
683 LSdebug('Email : '.$emailAddress);
684 self :: $dn = $user -> getDn();
686 // 1ère étape : envoie du recoveryHash
687 if (empty($recoveryHash)) {
688 $hash=self :: recoverPasswdFirstStep($user);
690 if (self :: recoverPasswdSendMail($emailAddress,1,$hash)) {
691 // Mail a bien été envoyé
692 $recoveryPasswordInfos['recoveryHashMail']=$emailAddress;
696 // 2nd étape : génération du mot de passe + envoie par mail
698 $pwd=self :: recoverPasswdSecondStep($user);
700 if (self :: recoverPasswdSendMail($emailAddress,2,$pwd)){
701 // Mail a bien été envoyé
702 $recoveryPasswordInfos['newPasswordMail']=$emailAddress;
708 LSerror :: addErrorCode('LSsession_19');
713 LSerror :: addErrorCode('LSsession_18');
717 return $recoveryPasswordInfos;
721 * Send recover password mail
723 * @param[in] $mail string The user's mail
724 * @param[in] $step integer The step
725 * @param[in] $info string The info for formatted message
727 * @retval boolean True on success or False
729 private static function recoverPasswdSendMail($mail,$step,$info) {
732 if (self :: $ldapServer['recoverPassword']['recoveryEmailSender']) {
733 $sendParams['From']=self :: $ldapServer['recoverPassword']['recoveryEmailSender'];
737 if ($_SERVER['HTTPS']=='on') {
738 $recovery_url='https://';
741 $recovery_url='http://';
743 $recovery_url .= $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'&recoveryHash='.$info;
745 $subject = self :: $ldapServer['recoverPassword']['recoveryHashMail']['subject'];
747 self :: $ldapServer['recoverPassword']['recoveryHashMail']['msg'],
752 $subject = self :: $ldapServer['recoverPassword']['newPasswordMail']['subject'];
754 self :: $ldapServer['recoverPassword']['newPasswordMail']['msg'],
759 if (!sendMail($mail,$subject,$msg,$sendParams)) {
760 LSdebug("Problème durant l'envoie du mail");
761 LSerror :: addErrorCode('LSsession_20',4);
769 * Do first step of recovering password
771 * @param[in] $user LSldapObject The LSldapObject of the user
773 * @retval string|False The recory hash on success or False
775 private static function recoverPasswdFirstStep($user) {
777 $rdn=$user -> getValue('rdn');
779 $recovery_hash = md5($rdn . strval(time()) . strval(rand()));
781 $lostPasswdForm = $user -> getForm('lostPassword');
782 $lostPasswdForm -> setPostData(
784 self :: $ldapServer['recoverPassword']['recoveryHashAttr'] => $recovery_hash
789 if($lostPasswdForm -> validate()) {
790 if ($user -> updateData('lostPassword')) {
791 // recoveryHash de l'utilisateur mis à jour
792 return $recovery_hash;
795 // Erreur durant la mise à jour de l'objet
796 LSdebug("Erreur durant la mise à jour de l'objet");
797 LSerror :: addErrorCode('LSsession_20',6);
801 // Erreur durant la validation du formulaire de modification de perte de password
802 LSdebug("Erreur durant la validation du formulaire de modification de perte de password");
803 LSerror :: addErrorCode('LSsession_20',5);
809 * Do second step of recovering password
811 * @param[in] $user LSldapObject The LSldapObject of the user
813 * @retval string|False The new password on success or False
815 private static function recoverPasswdSecondStep($user) {
816 $attr=$user -> attrs[self :: $ldapServer['authObjectTypeAttrPwd']];
817 if ($attr instanceof LSattribute) {
818 $mdp = generatePassword(
819 $attr -> config['html_options']['chars'],
820 $attr -> config['html_options']['lenght']
822 LSdebug('Nvx mpd : '.$mdp);
823 $lostPasswdForm = $user -> getForm('lostPassword');
824 $lostPasswdForm -> setPostData(
826 self :: $ldapServer['recoverPassword']['recoveryHashAttr'] => array(''),
827 self :: $ldapServer['authObjectTypeAttrPwd'] => array($mdp)
831 if($lostPasswdForm -> validate()) {
832 if ($user -> updateData('lostPassword')) {
836 // Erreur durant la mise à jour de l'objet
837 LSdebug("Erreur durant la mise à jour de l'objet");
838 LSerror :: addErrorCode('LSsession_20',3);
842 // Erreur durant la validation du formulaire de modification de perte de password
843 LSdebug("Erreur durant la validation du formulaire de modification de perte de password");
844 LSerror :: addErrorCode('LSsession_20',2);
848 // l'attribut password n'existe pas
849 LSdebug("L'attribut password n'existe pas");
850 LSerror :: addErrorCode('LSsession_20',1);
856 * Retourne les informations du contexte
858 * @author Benjamin Renard <brenard@easter-eggs.com
860 * @retval array Tableau associatif des informations du contexte
862 private static function getContextInfos() {
864 'tmp_file' => self :: $tmp_file,
865 'topDn' => self :: $topDn,
867 'rdn' => self :: $rdn,
868 'ldapServerId' => self :: $ldapServerId,
869 'ldapServer' => self :: $ldapServer,
870 'LSprofiles' => self :: $LSprofiles,
871 'LSaccess' => self :: $LSaccess
876 * Retourne l'objet de l'utilisateur connecté
878 * @author Benjamin Renard <brenard@easter-eggs.com
880 * @retval mixed L'objet de l'utilisateur connecté ou false si il n'a pas put
883 public static function getLSuserObject($dn=null) {
887 if (!self :: $LSuserObject) {
888 if (self :: loadLSobject(self :: $ldapServer['authObjectType'])) {
889 self :: $LSuserObject = new self :: $ldapServer['authObjectType']();
890 self :: $LSuserObject -> loadData(self :: $dn);
896 return self :: $LSuserObject;
900 * Retourne le DN de l'utilisateur connecté
902 * @author Benjamin Renard <brenard@easter-eggs.com
904 * @retval string Le DN de l'utilisateur connecté
906 public static function getLSuserObjectDn() {
911 * Modifie l'utilisateur connecté à la volé
913 * @param[in] $object Mixed L'objet Ldap du nouvel utilisateur
914 * le type doit correspondre à
915 * self :: $ldapServer['authObjectType']
917 * @retval boolean True en cas de succès, false sinon
919 public static function changeAuthUser($object) {
920 if ($object instanceof self :: $ldapServer['authObjectType']) {
921 self :: $dn = $object -> getDn();
922 $rdn = $object -> getValue('rdn');
927 self :: $LSuserObject = $object;
929 if(self :: loadLSprofiles()) {
930 self :: loadLSaccess();
931 $_SESSION['LSsession']=self :: getContextInfos();
939 * Définition du serveur Ldap de la session
941 * Définition du serveur Ldap de la session à partir de son ID dans
942 * le tableau LSconfig :: get('ldap_servers').
944 * @param[in] integer Index du serveur Ldap
946 * @retval boolean True sinon false.
948 public static function setLdapServer($id) {
949 $conf = LSconfig :: get("ldap_servers.$id");
950 if ( is_array($conf) ) {
951 self :: $ldapServerId = $id;
952 self :: $ldapServer = $conf;
962 * Connexion au serveur Ldap
964 * @retval boolean True sinon false.
966 public static function LSldapConnect() {
967 if (self :: $ldapServer) {
968 self :: includeFile(LSconfig :: get('NetLDAP2'));
969 if (!self :: loadLSclass('LSldap')) {
972 LSldap :: connect(self :: $ldapServer['ldap_config']);
973 if (LSldap :: isConnected()) {
981 LSerror :: addErrorCode('LSsession_03');
987 * Use this function to know if subDn is enabled for the curent LdapServer
991 public static function subDnIsEnabled() {
992 if (!isset(self :: $ldapServer['subDn'])) {
995 if ( !is_array(self :: $ldapServer['subDn']) ) {
1002 * Retourne les sous-dns du serveur Ldap courant
1004 * @retval mixed Tableau des subDn, false si une erreur est survenue.
1006 public static function getSubDnLdapServer($login=false) {
1007 $login=(bool)$login;
1008 if (self :: cacheSudDn() && isset(self :: $_subDnLdapServer[self :: $ldapServerId][$login])) {
1009 return self :: $_subDnLdapServer[self :: $ldapServerId][$login];
1011 if (!self::subDnIsEnabled()) {
1015 foreach(self :: $ldapServer['subDn'] as $subDn_name => $subDn_config) {
1016 if ($login && isset($subDn_config['nologin']) && $subDn_config['nologin']) continue;
1017 if ($subDn_name == 'LSobject') {
1018 if (is_array($subDn_config)) {
1019 foreach($subDn_config as $LSobject_name => $LSoject_config) {
1020 if (isset($LSoject_config['basedn']) && !empty($LSoject_config['basedn'])) {
1021 $basedn = $LSoject_config['basedn'];
1024 $basedn = self::getRootDn();
1026 if (isset($LSoject_config['displayName']) && !empty($LSoject_config['displayName'])) {
1027 $displayName = $LSoject_config['displayName'];
1030 $displayName = NULL;
1032 if( self :: loadLSobject($LSobject_name) ) {
1033 if ($subdnobject = new $LSobject_name()) {
1034 $tbl_return = $subdnobject -> getSelectArray(NULL,$basedn,$displayName);
1035 if (is_array($tbl_return)) {
1036 $return=array_merge($return,$tbl_return);
1039 LSerror :: addErrorCode('LSsession_17',3);
1043 LSerror :: addErrorCode('LSsession_17',2);
1049 LSerror :: addErrorCode('LSsession_17',1);
1053 if ((isCompatibleDNs($subDn_config['dn'],self :: $ldapServer['ldap_config']['basedn']))&&($subDn_config['dn']!="")) {
1054 $return[$subDn_config['dn']] = __($subDn_name);
1058 if (self :: cacheSudDn()) {
1059 self :: $_subDnLdapServer[self :: $ldapServerId][$login]=$return;
1060 $_SESSION['LSsession_subDnLdapServer'] = self :: $_subDnLdapServer;
1066 * Retourne la liste de subDn du serveur Ldap utilise
1067 * trié par la profondeur dans l'arboressence (ordre décroissant)
1069 * @return array() Tableau des subDn trié
1071 public static function getSortSubDnLdapServer($login=false) {
1072 $subDnLdapServer = self :: getSubDnLdapServer($login);
1073 if (!$subDnLdapServer) {
1076 uksort($subDnLdapServer,"compareDn");
1077 return $subDnLdapServer;
1081 * Retourne les options d'une liste déroulante pour le choix du topDn
1082 * de connexion au serveur Ldap
1084 * Liste les subdn (self :: $ldapServer['subDn'])
1086 * @retval string Les options (<option>) pour la sélection du topDn.
1088 public static function getSubDnLdapServerOptions($selected=NULL,$login=false) {
1089 $list = self :: getSubDnLdapServer($login);
1093 foreach($list as $dn => $txt) {
1094 if ($selected && ($selected==$dn)) {
1095 $selected_txt = ' selected';
1100 $display.="<option value=\"".$dn."\"$selected_txt>".$txt."</option>\n";
1108 * Vérifie qu'un subDn est déclaré
1110 * @param[in] string Un subDn
1112 * @retval boolean True si le subDn existe, False sinon
1114 public static function validSubDnLdapServer($subDn) {
1115 $listTopDn = self :: getSubDnLdapServer();
1116 if(is_array($listTopDn)) {
1117 foreach($listTopDn as $dn => $txt) {
1127 * Test un couple LSobject/pwd
1129 * Test un bind sur le serveur avec le dn de l'objet et le mot de passe fourni.
1131 * @param[in] LSobject L'object "user" pour l'authentification
1132 * @param[in] string Le mot de passe à tester
1134 * @retval boolean True si l'authentification à réussi, false sinon.
1136 public static function checkUserPwd($object,$pwd) {
1137 return LSldap :: checkBind($object -> getValue('dn'),$pwd);
1141 * Affiche le formulaire de login
1143 * Défini les informations pour le template Smarty du formulaire de login.
1147 public static function displayLoginForm() {
1148 $GLOBALS['Smarty'] -> assign('pagetitle',_('Connection'));
1149 if (isset($_GET['LSsession_logout'])) {
1150 $GLOBALS['Smarty'] -> assign('loginform_action','index.php');
1153 $GLOBALS['Smarty'] -> assign('loginform_action',$_SERVER['REQUEST_URI']);
1155 if (count(LSconfig :: get('ldap_servers'))==1) {
1156 $GLOBALS['Smarty'] -> assign('loginform_ldapserver_style','style="display: none"');
1158 $GLOBALS['Smarty'] -> assign('loginform_label_ldapserver',_('LDAP server'));
1159 $ldapservers_name=array();
1160 $ldapservers_index=array();
1161 foreach(LSconfig :: get('ldap_servers') as $id => $infos) {
1162 $ldapservers_index[]=$id;
1163 $ldapservers_name[]=__($infos['name']);
1165 $GLOBALS['Smarty'] -> assign('loginform_ldapservers_name',$ldapservers_name);
1166 $GLOBALS['Smarty'] -> assign('loginform_ldapservers_index',$ldapservers_index);
1168 $GLOBALS['Smarty'] -> assign('loginform_label_level',_('Level'));
1169 $GLOBALS['Smarty'] -> assign('loginform_label_user',_('Identifier'));
1170 $GLOBALS['Smarty'] -> assign('loginform_label_pwd',_('Password'));
1171 $GLOBALS['Smarty'] -> assign('loginform_label_submit',_('Connect'));
1172 $GLOBALS['Smarty'] -> assign('loginform_label_recoverPassword',_('Forgot your password ?'));
1174 self :: setTemplate('login.tpl');
1175 self :: addJSscript('LSsession_login.js');
1179 * Affiche le formulaire de récupération de mot de passe
1181 * Défini les informations pour le template Smarty du formulaire de
1182 * récupération de mot de passe
1184 * @param[in] $infos array() Information sur le status du processus de
1185 * recouvrement de mot de passe
1189 public static function displayRecoverPasswordForm($recoveryPasswordInfos) {
1190 $GLOBALS['Smarty'] -> assign('pagetitle',_('Recovery of your credentials'));
1191 $GLOBALS['Smarty'] -> assign('recoverpasswordform_action','index.php?LSsession_recoverPassword');
1193 if (count(LSconfig :: get('ldap_servers'))==1) {
1194 $GLOBALS['Smarty'] -> assign('recoverpasswordform_ldapserver_style','style="display: none"');
1197 $GLOBALS['Smarty'] -> assign('recoverpasswordform_label_ldapserver',_('LDAP server'));
1198 $ldapservers_name=array();
1199 $ldapservers_index=array();
1200 foreach(LSconfig :: get('ldap_servers') as $id => $infos) {
1201 $ldapservers_index[]=$id;
1202 $ldapservers_name[]=$infos['name'];
1204 $GLOBALS['Smarty'] -> assign('recoverpasswordform_ldapservers_name',$ldapservers_name);
1205 $GLOBALS['Smarty'] -> assign('recoverpasswordform_ldapservers_index',$ldapservers_index);
1207 $GLOBALS['Smarty'] -> assign('recoverpasswordform_label_user',_('Identifier'));
1208 $GLOBALS['Smarty'] -> assign('recoverpasswordform_label_submit',_('Validate'));
1209 $GLOBALS['Smarty'] -> assign('recoverpasswordform_label_back',_('Back'));
1211 $recoverpassword_msg = _('Please fill the identifier field to proceed recovery procedure');
1213 if (isset($recoveryPasswordInfos['recoveryHashMail'])) {
1214 $recoverpassword_msg = getFData(
1215 _("An email has been sent to %{mail}. " .
1216 "Please follow the instructions on it."),
1217 $recoveryPasswordInfos['recoveryHashMail']
1221 if (isset($recoveryPasswordInfos['newPasswordMail'])) {
1222 $recoverpassword_msg = getFData(
1223 _("Your new password has been sent to %{mail}. "),
1224 $recoveryPasswordInfos['newPasswordMail']
1228 $GLOBALS['Smarty'] -> assign('recoverpassword_msg',$recoverpassword_msg);
1230 self :: setTemplate('recoverpassword.tpl');
1231 self :: addJSscript('LSsession_recoverPassword.js');
1235 * Défini le template Smarty à utiliser
1237 * Remarque : les fichiers de templates doivent se trouver dans le dossier
1240 * @param[in] string Le nom du fichier de template
1244 public static function setTemplate($template) {
1245 self :: $template = $template;
1249 * Ajoute un script JS au chargement de la page
1251 * Remarque : les scripts doivents être dans le dossier LS_JS_DIR.
1253 * @param[in] $script Le nom du fichier de script à charger.
1257 public static function addJSscript($file,$path=NULL) {
1262 self :: $JSscripts[$path.$file]=$script;
1266 * Ajouter un paramètre de configuration Javascript
1268 * @param[in] $name string Nom de la variable de configuration
1269 * @param[in] $val mixed Valeur de la variable de configuration
1273 public static function addJSconfigParam($name,$val) {
1274 self :: $_JSconfigParams[$name]=$val;
1278 * Ajoute une feuille de style au chargement de la page
1280 * Remarque : les scripts doivents être dans le dossier LS_CSS_DIR.
1282 * @param[in] $script Le nom du fichier css à charger.
1286 public static function addCssFile($file,$path=NULL) {
1291 self :: $CssFiles[$path.$file]=$cssFile;
1295 * Affiche le template Smarty
1297 * Charge les dépendances et affiche le template Smarty
1301 public static function displayTemplate() {
1304 foreach ($GLOBALS['defaultJSscipts'] as $script) {
1305 $JSscript_txt.="<script src='".LS_JS_DIR.$script."' type='text/javascript'></script>\n";
1308 foreach (self :: $JSscripts as $script) {
1309 if (!$script['path']) {
1310 $script['path']=LS_JS_DIR;
1313 $script['path'].='/';
1315 $JSscript_txt.="<script src='".$script['path'].$script['file']."' type='text/javascript'></script>\n";
1318 $KAconf = LSconfig :: get('keepLSsessionActive');
1321 (!isset(self :: $ldapServer['keepLSsessionActive']))
1323 (!($KAconf === false))
1326 (self :: $ldapServer['keepLSsessionActive'])
1328 self :: addJSconfigParam('keepLSsessionActive',ini_get('session.gc_maxlifetime'));
1331 $GLOBALS['Smarty'] -> assign('LSjsConfig',json_encode(self :: $_JSconfigParams));
1334 $JSscript_txt.="<script type='text/javascript'>LSdebug_active = 1;</script>\n";
1337 $JSscript_txt.="<script type='text/javascript'>LSdebug_active = 0;</script>\n";
1340 $GLOBALS['Smarty'] -> assign('LSsession_js',$JSscript_txt);
1343 self :: addCssFile("LSdefault.css");
1345 foreach (self :: $CssFiles as $file) {
1346 if (!$file['path']) {
1347 $file['path']=LS_CSS_DIR.'/';
1349 $Css_txt.="<link rel='stylesheet' type='text/css' href='".$file['path'].$file['file']."' />\n";
1351 $GLOBALS['Smarty'] -> assign('LSsession_css',$Css_txt);
1353 if (isset(self :: $LSaccess[self :: $topDn])) {
1354 $GLOBALS['Smarty'] -> assign('LSaccess',self :: $LSaccess[self :: $topDn]);
1358 $listTopDn = self :: getSubDnLdapServer();
1359 if (is_array($listTopDn)) {
1361 $GLOBALS['Smarty'] -> assign('label_level',self :: getSubDnLabel());
1362 $GLOBALS['Smarty'] -> assign('_refresh',_('Refresh'));
1363 $LSsession_topDn_index = array();
1364 $LSsession_topDn_name = array();
1365 foreach($listTopDn as $index => $name) {
1366 $LSsession_topDn_index[] = $index;
1367 $LSsession_topDn_name[] = $name;
1369 $GLOBALS['Smarty'] -> assign('LSsession_subDn_indexes',$LSsession_topDn_index);
1370 $GLOBALS['Smarty'] -> assign('LSsession_subDn_names',$LSsession_topDn_name);
1371 $GLOBALS['Smarty'] -> assign('LSsession_subDn',self :: $topDn);
1372 $GLOBALS['Smarty'] -> assign('LSsession_subDnName',self :: getSubDnName());
1375 $GLOBALS['Smarty'] -> assign('LSlanguages',self :: getLangList());
1376 $GLOBALS['Smarty'] -> assign('LSlang',self :: $lang);
1377 $GLOBALS['Smarty'] -> assign('LSencoding',self :: $encoding);
1378 $GLOBALS['Smarty'] -> assign('lang_label',_('Language'));
1380 $GLOBALS['Smarty'] -> assign('displayLogoutBtn',LSauth :: displayLogoutBtn());
1381 $GLOBALS['Smarty'] -> assign('displaySelfAccess',LSauth :: displaySelfAccess());
1384 if((!empty($_SESSION['LSsession_infos']))&&(is_array($_SESSION['LSsession_infos']))) {
1385 $txt_infos="<ul>\n";
1386 foreach($_SESSION['LSsession_infos'] as $info) {
1387 $txt_infos.="<li>$info</li>\n";
1389 $txt_infos.="</ul>\n";
1390 $GLOBALS['Smarty'] -> assign('LSinfos',$txt_infos);
1391 $_SESSION['LSsession_infos']=array();
1394 if (self :: $ajaxDisplay) {
1395 $GLOBALS['Smarty'] -> assign('LSerror_txt',LSerror :: getErrors());
1396 $GLOBALS['Smarty'] -> assign('LSdebug_txt',LSdebug_print(true));
1399 LSerror :: display();
1402 if (!self :: $template)
1403 self :: setTemplate('empty.tpl');
1405 $GLOBALS['Smarty'] -> assign('connected_as',_("Connected as"));
1407 $GLOBALS['Smarty'] -> display(self :: $template);
1411 * Défini que l'affichage se fera ou non via un retour Ajax
1413 * @param[in] $val boolean True pour que l'affichage se fasse par un retour
1417 public static function setAjaxDisplay($val=true) {
1418 self :: $ajaxDisplay = (boolean)$val;
1422 * Affiche un retour Ajax
1426 public static function displayAjaxReturn($data=array()) {
1427 if (isset($data['LSredirect']) && (!LSdebugDefined()) ) {
1428 echo json_encode($data);
1432 $data['LSjsConfig'] = self :: $_JSconfigParams;
1435 if((!empty($_SESSION['LSsession_infos']))&&(is_array($_SESSION['LSsession_infos']))) {
1436 $txt_infos="<ul>\n";
1437 foreach($_SESSION['LSsession_infos'] as $info) {
1438 $txt_infos.="<li>$info</li>\n";
1440 $txt_infos.="</ul>\n";
1441 $data['LSinfos'] = $txt_infos;
1442 $_SESSION['LSsession_infos']=array();
1445 if (LSerror :: errorsDefined()) {
1446 $data['LSerror'] = LSerror :: getErrors();
1449 if (isset($_REQUEST['imgload'])) {
1450 $data['imgload'] = $_REQUEST['imgload'];
1453 if (LSdebugDefined()) {
1454 $data['LSdebug'] = LSdebug_print(true,false);
1457 echo json_encode($data);
1461 * Retournne un template Smarty compilé
1463 * @param[in] string $template Le template à retourner
1464 * @param[in] array $variables Variables Smarty à assigner avant l'affichage
1466 * @retval string Le HTML compilé du template
1468 public static function fetchTemplate($template,$variables=array()) {
1469 foreach($variables as $name => $val) {
1470 $GLOBALS['Smarty'] -> assign($name,$val);
1472 return $GLOBALS['Smarty'] -> fetch($template);
1476 * Charge les droits LS de l'utilisateur
1478 * @retval boolean True si le chargement à réussi, false sinon.
1480 private static function loadLSprofiles() {
1481 if (is_array(self :: $ldapServer['LSprofiles'])) {
1482 foreach (self :: $ldapServer['LSprofiles'] as $profile => $profileInfos) {
1483 if (is_array($profileInfos)) {
1484 foreach ($profileInfos as $topDn => $rightsInfos) {
1486 * If $topDn == 'LSobject', we search for each LSobject type to find
1487 * all items on witch the user will have powers.
1489 if ($topDn == 'LSobjects') {
1490 if (is_array($rightsInfos)) {
1491 foreach ($rightsInfos as $LSobject => $listInfos) {
1492 if (self :: loadLSclass('LSsearch')) {
1493 if (isset($listInfos['filter'])) {
1494 $filter = self :: getLSuserObject() -> getFData($listInfos['filter']);
1497 $filter = '('.$listInfos['attr'].'='.self :: getLSuserObject() -> getFData($listInfos['attr_value']).')';
1501 'basedn' => (isset($listInfos['basedn'])?$listInfos['basedn']:null),
1505 if (isset($listInfos['params']) && is_array($listInfos['params'])) {
1506 $params = array_merge($listInfos['params'],$params);
1509 $LSsearch = new LSsearch($LSobject,'LSsession :: loadLSprofiles',$params,true);
1510 $LSsearch -> run(false);
1512 $LSprofiles[$profile] = $LSsearch -> listObjectsDn();
1517 LSdebug('LSobjects => [] doit etre un tableau');
1521 if (is_array($rightsInfos)) {
1522 foreach($rightsInfos as $dn => $conf) {
1523 if ((isset($conf['attr'])) && (isset($conf['LSobject']))) {
1524 if( self :: loadLSobject($conf['LSobject']) ) {
1525 if ($object = new $conf['LSobject']()) {
1526 if ($object -> loadData($dn)) {
1527 $listDns=$object -> getValue($conf['attr']);
1528 $valKey = (isset($conf['attr_value']))?$conf['attr_value']:'%{dn}';
1529 $val = self :: getLSuserObject() -> getFData($valKey);
1530 if (is_array($listDns)) {
1531 if (in_array($val,$listDns)) {
1532 self :: $LSprofiles[$profile][] = $topDn;
1537 LSdebug('Impossible de chargé le dn : '.$dn);
1541 LSdebug('Impossible de créer l\'objet de type : '.$conf['LSobject']);
1546 if (self :: $dn == $dn) {
1547 self :: $LSprofiles[$profile][] = $topDn;
1553 if ( self :: $dn == $rightsInfos ) {
1554 self :: $LSprofiles[$profile][] = $topDn;
1557 } // fin else ($topDn == 'LSobjects')
1558 } // fin foreach($profileInfos)
1559 } // fin is_array($profileInfos)
1560 } // fin foreach LSprofiles
1561 LSdebug(self :: $LSprofiles);
1570 * Charge les droits d'accès de l'utilisateur pour construire le menu de l'interface
1574 private static function loadLSaccess() {
1576 if (isset(self :: $ldapServer['subDn']) && is_array(self :: $ldapServer['subDn'])) {
1577 foreach(self :: $ldapServer['subDn'] as $name => $config) {
1578 if ($name=='LSobject') {
1579 if (is_array($config)) {
1581 // Définition des subDns
1582 foreach($config as $objectType => $objectConf) {
1583 if (self :: loadLSobject($objectType)) {
1584 if ($subdnobject = new $objectType()) {
1585 $tbl = $subdnobject -> getSelectArray(NULL,self::getRootDn(),NULL,NULL,false);
1586 if (is_array($tbl)) {
1587 // Définition des accès
1589 if (is_array($objectConf['LSobjects'])) {
1590 foreach($objectConf['LSobjects'] as $type) {
1591 if (self :: loadLSobject($type)) {
1592 if (self :: canAccess($type)) {
1593 $access[$type] = LSconfig :: get('LSobjects.'.$type.'.label');
1598 foreach($tbl as $dn => $dn_name) {
1599 $LSaccess[$dn]=$access;
1608 if ((isCompatibleDNs(self :: $ldapServer['ldap_config']['basedn'],$config['dn']))&&($config['dn']!='')) {
1610 if (is_array($config['LSobjects'])) {
1611 foreach($config['LSobjects'] as $objectType) {
1612 if (self :: loadLSobject($objectType)) {
1613 if (self :: canAccess($objectType)) {
1614 $access[$objectType] = LSconfig :: get('LSobjects.'.$objectType.'.label');
1619 $LSaccess[$config['dn']]=$access;
1625 if(is_array(self :: $ldapServer['LSaccess'])) {
1627 foreach(self :: $ldapServer['LSaccess'] as $objectType) {
1628 if (self :: loadLSobject($objectType)) {
1629 if (self :: canAccess($objectType)) {
1630 $access[$objectType] = LSconfig :: get('LSobjects.'.$objectType.'.label');
1634 $LSaccess[self :: $topDn] = $access;
1637 if (LSauth :: displaySelfAccess()) {
1638 foreach($LSaccess as $dn => $access) {
1639 $LSaccess[$dn] = array_merge(
1641 'SELF' => 'My account'
1647 self :: $LSaccess = $LSaccess;
1648 $_SESSION['LSsession']['LSaccess'] = $LSaccess;
1652 * Dit si l'utilisateur est du profil pour le DN spécifié
1654 * @param[in] string $profile de l'objet
1655 * @param[in] string $dn DN de l'objet
1657 * @retval boolean True si l'utilisateur est du profil sur l'objet, false sinon.
1659 public static function isLSprofile($dn,$profile) {
1660 if (is_array(self :: $LSprofiles[$profile])) {
1661 foreach(self :: $LSprofiles[$profile] as $topDn) {
1665 else if ( isCompatibleDNs($dn,$topDn) ) {
1674 * Retourne qui est l'utilisateur par rapport à l'object
1676 * @param[in] string Le DN de l'objet
1678 * @retval string 'admin'/'self'/'user' pour Admin , l'utilisateur lui même ou un simple utilisateur
1680 public static function whoami($dn) {
1681 $retval = array('user');
1683 foreach(self :: $LSprofiles as $profile => $infos) {
1684 if(self :: isLSprofile($dn,$profile)) {
1689 if (self :: $dn == $dn) {
1697 * Retourne le droit de l'utilisateur à accèder à un objet
1699 * @param[in] string $LSobject Le type de l'objet
1700 * @param[in] string $dn Le DN de l'objet (le container_dn du type de l'objet par défaut)
1701 * @param[in] string $right Le type de droit d'accès à tester ('r'/'w')
1702 * @param[in] string $attr Le nom de l'attribut auquel on test l'accès
1704 * @retval boolean True si l'utilisateur a accès, false sinon
1706 public static function canAccess($LSobject,$dn=NULL,$right=NULL,$attr=NULL) {
1707 if (!self :: loadLSobject($LSobject)) {
1711 $whoami = self :: whoami($dn);
1712 if ($dn==self :: getLSuserObject() -> getValue('dn')) {
1713 if (!self :: in_menu('SELF')) {
1718 $obj = new $LSobject();
1720 if (!self :: in_menu($LSobject,$obj -> subDnValue)) {
1726 $objectdn=LSconfig :: get('LSobjects.'.$LSobject.'.container_dn').','.self :: $topDn;
1727 $whoami = self :: whoami($objectdn);
1730 // Pour un attribut particulier
1733 $attr=LSconfig :: get('LSobjects.'.$LSobject.'.rdn');
1735 if (!is_array(LSconfig :: get('LSobjects.'.$LSobject.'.attrs.'.$attr))) {
1740 foreach($whoami as $who) {
1741 $nr = LSconfig :: get('LSobjects.'.$LSobject.'.attrs.'.$attr.'.rights.'.$who);
1745 else if($nr == 'r') {
1752 if (($right=='r')||($right=='w')) {
1759 if ( ($r=='r') || ($r=='w') ) {
1766 // Pour un attribut quelconque
1767 $attrs_conf=LSconfig :: get('LSobjects.'.$LSobject.'.attrs');
1768 if (is_array($attrs_conf)) {
1769 if (($right=='r')||($right=='w')) {
1770 foreach($whoami as $who) {
1771 foreach ($attrs_conf as $attr_name => $attr_config) {
1772 if (isset($attr_config['rights'][$who]) && $attr_config['rights'][$who]==$right) {
1779 foreach($whoami as $who) {
1780 foreach ($attrs_conf as $attr_name => $attr_config) {
1781 if ( (isset($attr_config['rights'][$who])) && ( ($attr_config['rights'][$who]=='r') || ($attr_config['rights'][$who]=='w') ) ) {
1792 * Retourne le droit de l'utilisateur à editer à un objet
1794 * @param[in] string $LSobject Le type de l'objet
1795 * @param[in] string $dn Le DN de l'objet (le container_dn du type de l'objet par défaut)
1796 * @param[in] string $attr Le nom de l'attribut auquel on test l'accès
1798 * @retval boolean True si l'utilisateur a accès, false sinon
1800 public static function canEdit($LSobject,$dn=NULL,$attr=NULL) {
1801 return self :: canAccess($LSobject,$dn,'w',$attr);
1805 * Retourne le droit de l'utilisateur à supprimer un objet
1807 * @param[in] string $LSobject Le type de l'objet
1808 * @param[in] string $dn Le DN de l'objet (le container_dn du type de l'objet par défaut)
1810 * @retval boolean True si l'utilisateur a accès, false sinon
1812 public static function canRemove($LSobject,$dn) {
1813 return self :: canAccess($LSobject,$dn,'w','rdn');
1817 * Retourne le droit de l'utilisateur à créer un objet
1819 * @param[in] string $LSobject Le type de l'objet
1821 * @retval boolean True si l'utilisateur a accès, false sinon
1823 public static function canCreate($LSobject) {
1824 if (!self :: loadLSobject($LSobject)) {
1827 if (LSconfig :: get("LSobjects.$LSobject.disable_creation")) {
1830 return self :: canAccess($LSobject,NULL,'w','rdn');
1834 * Retourne le droit de l'utilisateur à gérer la relation d'objet
1836 * @param[in] string $dn Le DN de l'objet (le container_dn du type de l'objet par défaut)
1837 * @param[in] string $LSobject Le type de l'objet
1838 * @param[in] string $relationName Le nom de la relation avec l'objet
1839 * @param[in] string $right Le type de droit a vérifier ('r' ou 'w')
1841 * @retval boolean True si l'utilisateur a accès, false sinon
1843 public static function relationCanAccess($dn,$LSobject,$relationName,$right=NULL) {
1844 $relConf=LSconfig :: get('LSobjects.'.$LSobject.'.LSrelation.'.$relationName);
1845 if (!is_array($relConf))
1847 $whoami = self :: whoami($dn);
1849 if (($right=='w') || ($right=='r')) {
1851 foreach($whoami as $who) {
1852 $nr = ((isset($relConf['rights'][$who]))?$relConf['rights'][$who]:'');
1856 else if($nr == 'r') {
1868 foreach($whoami as $who) {
1869 if ((isset($relConf['rights'][$who])) && ( ($relConf['rights'][$who] == 'w') || ($relConf['rights'][$who] == 'r') ) ) {
1878 * Retourne le droit de l'utilisateur à modifier la relation d'objet
1880 * @param[in] string $dn Le DN de l'objet (le container_dn du type de l'objet par défaut)
1881 * @param[in] string $LSobject Le type de l'objet
1882 * @param[in] string $relationName Le nom de la relation avec l'objet
1884 * @retval boolean True si l'utilisateur a accès, false sinon
1886 public static function relationCanEdit($dn,$LSobject,$relationName) {
1887 return self :: relationCanAccess($dn,$LSobject,$relationName,'w');
1891 * Retourne le droit de l'utilisateur a executer une customAction
1893 * @param[in] string $dn Le DN de l'objet
1894 * @param[in] string $LSobject Le type de l'objet
1895 * @param[in] string $customActionName Le nom de la customAction
1897 * @retval boolean True si l'utilisateur peut executer cette customAction, false sinon
1899 public static function canExecuteCustomAction($dn,$LSobject,$customActionName) {
1900 $conf=LSconfig :: get('LSobjects.'.$LSobject.'.customActions.'.$customActionName);
1901 if (!is_array($conf))
1903 $whoami = self :: whoami($dn);
1905 if (isset($conf['rights']) && is_array($conf['rights'])) {
1906 foreach($whoami as $who) {
1907 if (in_array($who,$conf['rights'])) {
1917 * Ajoute un fichier temporaire
1919 * @author Benjamin Renard <brenard@easter-eggs.com>
1923 public static function addTmpFile($value,$filePath) {
1924 $hash = mhash(MHASH_MD5,$value);
1925 self :: $tmp_file[$filePath] = $hash;
1926 $_SESSION['LSsession']['tmp_file'][$filePath] = $hash;
1930 * Retourne le chemin du fichier temporaire si l'existe
1932 * @author Benjamin Renard <brenard@easter-eggs.com>
1934 * @param[in] $value La valeur du fichier
1938 public static function tmpFileExist($value) {
1939 $hash = mhash(MHASH_MD5,$value);
1940 foreach(self :: $tmp_file as $filePath => $contentHash) {
1941 if ($hash == $contentHash) {
1949 * Retourne le chemin du fichier temporaire
1951 * Retourne le chemin du fichier temporaire qu'il créera à partir de la valeur
1952 * s'il n'existe pas déjà .
1954 * @author Benjamin Renard <brenard@easter-eggs.com>
1956 * @param[in] $value La valeur du fichier
1960 public static function getTmpFile($value) {
1961 $exist = self :: tmpFileExist($value);
1963 $img_path = LS_TMP_DIR .rand().'.tmp';
1964 $fp = fopen($img_path, "w");
1965 fwrite($fp, $value);
1967 self :: addTmpFile($value,$img_path);
1976 * Supprime les fichiers temporaires
1978 * @author Benjamin Renard <brenard@easter-eggs.com>
1982 public static function deleteTmpFile($filePath=NULL) {
1985 unset(self :: $tmp_file[$filePath]);
1986 unset($_SESSION['LSsession']['tmp_file'][$filePath]);
1989 foreach(self :: $tmp_file as $file => $content) {
1992 self :: $tmp_file = array();
1993 $_SESSION['LSsession']['tmp_file'] = array();
1998 * Retourne true si le cache des droits est activé
2000 * @author Benjamin Renard <brenard@easter-eggs.com>
2002 * @retval boolean True si le cache des droits est activé, false sinon.
2004 public static function cacheLSprofiles() {
2005 return ( (LSconfig :: get('cacheLSprofiles')) || (self :: $ldapServer['cacheLSprofiles']) );
2009 * Retourne true si le cache des subDn est activé
2011 * @author Benjamin Renard <brenard@easter-eggs.com>
2013 * @retval boolean True si le cache des subDn est activé, false sinon.
2015 public static function cacheSudDn() {
2016 return ( (LSconfig :: get('cacheSubDn')) || (self :: $ldapServer['cacheSubDn']));
2020 * Retourne true si le cache des recherches est activé
2022 * @author Benjamin Renard <brenard@easter-eggs.com>
2024 * @retval boolean True si le cache des recherches est activé, false sinon.
2026 public static function cacheSearch() {
2027 return ( (LSconfig :: get('cacheSearch')) || (self :: $ldapServer['cacheSearch']));
2031 * Retourne le label des niveaux pour le serveur ldap courant
2033 * @author Benjamin Renard <brenard@easter-eggs.com>
2035 * @retval string Le label des niveaux pour le serveur ldap dourant
2037 public static function getSubDnLabel() {
2038 return (self :: $ldapServer['subDnLabel']!='')?__(self :: $ldapServer['subDnLabel']):_('Level');
2042 * Retourne le nom du subDn
2044 * @param[in] $subDn string subDn
2046 * @retval string Le nom du subDn ou '' sinon
2048 public static function getSubDnName($subDn=false) {
2050 $subDn = self :: $topDn;
2052 if (self :: getSubDnLdapServer(false)) {
2053 if (isset(self :: $_subDnLdapServer[self :: $ldapServerId][false][$subDn])) {
2054 return self :: $_subDnLdapServer[self :: $ldapServerId][false][$subDn];
2061 * L'objet est t-il utilisé pour listé les subDnS
2063 * @param[in] $type string Le type d'objet
2065 * @retval boolean true si le type d'objet est un subDnObject, false sinon
2067 public static function isSubDnLSobject($type) {
2069 if (isset(self :: $ldapServer['subDn']['LSobject']) && is_array(self :: $ldapServer['subDn']['LSobject'])) {
2070 foreach(self :: $ldapServer['subDn']['LSobject'] as $key => $value) {
2080 * Indique si un type d'objet est dans le menu courant
2082 * @retval boolean true si le type d'objet est dans le menu, false sinon
2084 public static function in_menu($LSobject,$topDn=NULL) {
2086 $topDn=self :: $topDn;
2088 return isset(self :: $LSaccess[$topDn][$LSobject]);
2092 * Indique si le serveur LDAP courant a des subDn
2094 * @retval boolean true si le serveur LDAP courant a des subDn, false sinon
2096 public static function haveSubDn() {
2097 return (isset(self :: $ldapServer['subDn']) && is_array(self :: $ldapServer['subDn']));
2101 * Ajoute une information à afficher
2103 * @param[in] $msg string Le message à afficher
2107 public static function addInfo($msg) {
2108 $_SESSION['LSsession_infos'][]=$msg;
2112 * Redirection de l'utilisateur vers une autre URL
2114 * @param[in] $url string L'URL
2115 * @param[in] $exit boolean Si true, l'execution script s'arrête après la redirection
2119 public static function redirect($url,$exit=true) {
2120 $GLOBALS['Smarty'] -> assign('url',$url);
2121 $GLOBALS['Smarty'] -> display('redirect.tpl');
2128 * Retourne l'adresse mail d'emission configurée pour le serveur courant
2130 * @retval string Adresse mail d'emission
2132 public static function getEmailSender() {
2133 return self :: $ldapServer['emailSender'];
2137 * Ajout d'une information d'aide
2139 * @param[in] $group string Le nom du groupe d'infos dans lequels ajouter
2141 * @param[in] $infos array Tableau array(name => value) des infos
2145 public static function addHelpInfos($group,$infos) {
2146 if (is_array($infos)) {
2147 if (isset(self :: $_JSconfigParams['helpInfos'][$group]) && is_array(self :: $_JSconfigParams['helpInfos'][$group])) {
2148 self :: $_JSconfigParams['helpInfos'][$group] = array_merge(self :: $_JSconfigParams['helpInfos'][$group],$infos);
2151 self :: $_JSconfigParams['helpInfos'][$group] = $infos;
2157 * Défini les codes erreur relative à la classe LSsession
2161 private static function defineLSerrors() {
2165 LSerror :: defineError('LSsession_01',
2166 _("LSsession : The constant %{const} is not defined.")
2168 LSerror :: defineError('LSsession_02',
2169 _("LSsession : The %{addon} support is uncertain. Verify system compatibility and the add-on configuration.")
2171 LSerror :: defineError('LSsession_03',
2172 _("LSsession : LDAP server's configuration data are invalid. Can't connect.")
2174 LSerror :: defineError('LSsession_04',
2175 _("LSsession : Failed to load LSobject type %{type} : unknon type.")
2177 LSerror :: defineError('LSsession_05',
2178 _("LSsession : Failed to load LSclass %{class}.")
2180 LSerror :: defineError('LSsession_06',
2181 _("LSsession : Login or password incorrect.")
2183 LSerror :: defineError('LSsession_07',
2184 _("LSsession : Impossible to identify you : Duplication of identities.")
2186 LSerror :: defineError('LSsession_08',
2187 _("LSsession : Can't load class of authentification (%{class}).")
2189 LSerror :: defineError('LSsession_09',
2190 _("LSsession : Can't connect to LDAP server.")
2192 LSerror :: defineError('LSsession_10',
2193 _("LSsession : Impossible to authenticate you.")
2195 LSerror :: defineError('LSsession_11',
2196 _("LSsession : Your are not authorized to do this action.")
2198 LSerror :: defineError('LSsession_12',
2199 _("LSsession : Some informations are missing to display this page.")
2201 LSerror :: defineError('LSsession_13',
2202 _("LSsession : The function of the custom action %{name} does not exists or is not configured.")
2204 // 14 -> 16 : not yet used
2205 LSerror :: defineError('LSsession_17',
2206 _("LSsession : Error during creation of list of levels. Contact administrators. (Code : %{code})")
2208 LSerror :: defineError('LSsession_18',
2209 _("LSsession : The password recovery is disabled for this LDAP server.")
2211 LSerror :: defineError('LSsession_19',
2212 _("LSsession : Some informations are missing to recover your password. Contact administrators.")
2214 LSerror :: defineError('LSsession_20',
2215 _("LSsession : Error during password recovery. Contact administrators.(Step : %{step})")
2217 // 21 : not yet used
2218 LSerror :: defineError('LSsession_22',
2219 _("LSsession : problem during initialisation.")
2224 * Ajax method when change ldapserver on login form
2226 * @param[in] $data array The return data address
2230 public static function ajax_onLdapServerChangedLogin(&$data) {
2231 if ( isset($_REQUEST['server']) ) {
2232 self :: setLdapServer($_REQUEST['server']);
2234 if ( self :: LSldapConnect() ) {
2235 if (session_id()=="") session_start();
2236 if (isset($_SESSION['LSsession_topDn'])) {
2237 $sel = $_SESSION['LSsession_topDn'];
2242 $list = self :: getSubDnLdapServerOptions($sel,true);
2243 if (is_string($list)) {
2244 $data['list_topDn'] = "<select name='LSsession_topDn' id='LSsession_topDn'>".$list."</select>";
2245 $data['subDnLabel'] = self :: getSubDnLabel();
2248 $data['recoverPassword'] = isset(self :: $ldapServer['recoverPassword']);
2253 * Ajax method when change ldapserver on recoverPassword form
2255 * @param[in] $data array The return data address
2259 public static function ajax_onLdapServerChangedRecoverPassword(&$data) {
2260 if ( isset($_REQUEST['server']) ) {
2261 self :: setLdapServer($_REQUEST['server']);
2262 $data=array('recoverPassword' => isset(self :: $ldapServer['recoverPassword']));