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 // Authentification parameters
54 private static $authParams = array();
56 // Les fichiers temporaires
57 private static $tmp_file = array();
59 // Langue et encodage actuel
60 private static $lang = NULL;
61 private static $encoding = NULL;
64 * Constante de classe non stockée en session
66 // Le template à afficher
67 private static $template = NULL;
69 // Les subDn des serveurs Ldap
70 private static $_subDnLdapServer = array();
73 private static $ajaxDisplay = false;
75 // Les fichiers JS à charger dans la page
76 private static $JSscripts = array();
78 // Les paramètres JS à communiquer dans la page
79 private static $_JSconfigParams = array();
81 // Les fichiers CSS à charger dans la page
82 private static $CssFiles = array();
84 // L'objet de l'utilisateur connecté
85 private static $LSuserObject = NULL;
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)) {
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;
138 $GLOBALS['Smarty'] -> caching = 0;
139 // cache files are always regenerated
140 $GLOBALS['Smarty'] -> force_compile = TRUE;
141 // recompile template if it is changed
142 $GLOBALS['Smarty'] -> compile_check = TRUE;
143 if (isset($_REQUEST['debug_smarty'])) {
145 $GLOBALS['Smarty'] -> debugging = true;
149 $GLOBALS['Smarty'] -> assign('LS_CSS_DIR',LS_CSS_DIR);
150 $GLOBALS['Smarty'] -> assign('LS_IMAGES_DIR',LS_IMAGES_DIR);
152 self :: addJSconfigParam('LS_IMAGES_DIR',LS_IMAGES_DIR);
155 die("ERROR : Can't load Smarty.");
160 * Retourne le topDn de la session
162 * @author Benjamin Renard <brenard@easter-eggs.com>
164 * @retval string le topDn de la session
166 public static function getTopDn() {
167 if (!is_null(self :: $topDn)) {
168 return self :: $topDn;
171 return self :: getRootDn();
176 * Retourne le rootDn de la session
178 * @author Benjamin Renard <brenard@easter-eggs.com>
180 * @retval string le rootDn de la session
182 public static function getRootDn() {
183 return self :: $ldapServer['ldap_config']['basedn'];
187 * Initialisation de la gestion des erreurs
189 * Création de l'objet LSerror
191 * @author Benjamin Renard <brenard@easter-eggs.com
193 * @retval boolean true si l'initialisation a réussi, false sinon.
195 private static function startLSerror() {
196 if(!self :: loadLSclass('LSerror')) {
199 self :: defineLSerrors();
204 * Chargement d'une classe d'LdapSaisie
206 * @param[in] $class Nom de la classe à charger (Exemple : LSpeople)
207 * @param[in] $type (Optionnel) Type de classe à charger (Exemple : LSobjects)
209 * @author Benjamin Renard <brenard@easter-eggs.com
211 * @retval boolean true si le chargement a réussi, false sinon.
213 public static function loadLSclass($class,$type='') {
214 if (class_exists($class))
218 return self :: includeFile(LS_CLASS_DIR .'class.'.$type.$class.'.php');
222 * Chargement d'un object LdapSaisie
224 * @param[in] $object Nom de l'objet à charger
226 * @retval boolean true si le chargement a réussi, false sinon.
228 public static function loadLSobject($object) {
229 if(class_exists($object)) {
233 self :: loadLSclass('LSldapObject');
234 if (!self :: loadLSclass($object,'LSobjects')) {
237 if (!self :: includeFile( LS_OBJECTS_DIR . 'config.LSobjects.'.$object.'.php' )) {
241 if (!LSconfig :: set("LSobjects.$object",$GLOBALS['LSobjects'][$object])) {
246 LSerror :: addErrorCode('LSsession_04',$object);
253 * Chargement d'un addons d'LdapSaisie
255 * @param[in] $addon Nom de l'addon à charger (Exemple : samba)
257 * @author Benjamin Renard <brenard@easter-eggs.com
259 * @retval boolean true si le chargement a réussi, false sinon.
261 public static function loadLSaddon($addon) {
262 if(self :: includeFile(LS_ADDONS_DIR .'LSaddons.'.$addon.'.php')) {
263 self :: includeFile(LS_CONF_DIR."LSaddons/config.LSaddons.".$addon.".php");
264 if (!call_user_func('LSaddon_'. $addon .'_support')) {
265 LSerror :: addErrorCode('LSsession_02',$addon);
274 * Chargement des addons LdapSaisie
276 * Chargement des LSaddons contenue dans la variable
277 * $GLOBALS['LSaddons']['loads']
279 * @retval boolean true si le chargement a réussi, false sinon.
281 public static function loadLSaddons() {
282 $conf=LSconfig :: get('LSaddons.loads');
283 if(!is_array($conf)) {
284 LSerror :: addErrorCode('LSsession_01',"LSaddons['loads']");
288 foreach ($conf as $addon) {
289 self :: loadLSaddon($addon);
299 public static function setLocale() {
300 if (isset($_REQUEST['lang'])) {
301 $lang = $_REQUEST['lang'];
303 elseif (isset($_SESSION['LSlang'])) {
304 $lang = $_SESSION['LSlang'];
306 elseif (isset(self :: $ldapServer['lang'])) {
307 $lang = self :: $ldapServer['lang'];
310 $lang = LSconfig :: get('lang');
313 if (isset($_REQUEST['encoding'])) {
314 $encoding = $_REQUEST['encoding'];
316 elseif (isset($_SESSION['LSencoding'])) {
317 $encoding = $_SESSION['LSencoding'];
319 elseif (isset(self :: $ldapServer['encoding'])) {
320 $encoding = self :: $ldapServer['encoding'];
323 $encoding = LSconfig :: get('encoding');
326 $_SESSION['LSlang']=$lang;
328 $_SESSION['LSencoding']=$encoding;
329 self :: $encoding=$encoding;
332 if (self :: localeExist($lang,$encoding)) {
334 $lang.='.'.$encoding;
336 setlocale(LC_ALL, $lang);
337 bindtextdomain(LS_TEXT_DOMAIN, LS_I18N_DIR);
338 textdomain(LS_TEXT_DOMAIN);
340 if (is_file(LS_I18N_DIR.'/'.$lang.'/lang.php')) {
341 include(LS_I18N_DIR.'/'.$lang.'/lang.php');
345 if ($encoding && $lang) {
346 $lang.='.'.$encoding;
348 LSdebug('La locale "'.$lang.'" n\'existe pas, utilisation de la locale par défaut.');
353 * Retourne la liste des langues disponibles
355 * @retval array Tableau/Liste des langues disponibles
357 public static function getLangList() {
358 $list=array('en_US');
359 if (self :: $encoding) {
360 $regex = '^([a-zA-Z_]*)\.'.self :: $encoding.'$';
363 $regex = '^([a-zA-Z_]*)$';
365 if ($handle = opendir(LS_I18N_DIR)) {
366 while (false !== ($file = readdir($handle))) {
367 if(is_dir(LS_I18N_DIR.'/'.$file)) {
368 if (ereg($regex,$file,$regs)) {
369 if (!in_array($regs[1],$list)) {
380 * Retourne la langue courante de la session
382 * @param[in] boolean Si true, le code langue retourné sera court
384 * @retval string La langue de la session
386 public static function getLang($short=false) {
388 return strtolower(self :: $lang[0].self :: $lang[1]);
390 return self :: $lang;
394 * Vérifie si une locale est disponible
396 * @param[in] $lang string La langue (Ex : fr_FR)
397 * @param[in] $encoding string L'encodage de caractère (Ex : UTF8)
399 * @retval boolean True si la locale est disponible, False sinon
401 public static function localeExist($lang,$encoding) {
402 if ( !$lang && !$encoding ) {
405 $locale=$lang.(($encoding)?'.'.$encoding:'');
406 if ($locale=='en_US.UTF8') {
409 return (is_dir(LS_I18N_DIR.'/'.$locale));
413 * Initialisation LdapSaisie
415 * @retval boolean True si l'initialisation à réussi, false sinon.
417 public static function initialize() {
418 if (!self :: startLSconfig()) {
422 self :: startLStemplate();
428 self :: startLSerror();
429 self :: loadLSaddons();
434 * Initialisation de la session LdapSaisie
436 * Initialisation d'une LSsession :
437 * - Authentification et activation du mécanisme de session de LdapSaisie
438 * - ou Chargement des paramètres de la session à partir de la variable
439 * $_SESSION['LSsession'].
440 * - ou Destruction de la session en cas de $_GET['LSsession_logout'].
442 * @retval boolean True si l'initialisation à réussi (utilisateur authentifié), false sinon.
444 public static function startLSsession() {
445 if (!self :: initialize()) {
450 if (isset($_GET['LSsession_logout'])||isset($_GET['LSsession_recoverPassword'])) {
453 if (is_array($_SESSION['LSsession']['tmp_file'])) {
454 self :: $tmp_file = $_SESSION['LSsession']['tmp_file'];
456 self :: deleteTmpFile();
457 unset($_SESSION['LSsession']);
460 // Récupération de mot de passe
461 if (isset($_GET['recoveryHash'])) {
462 $_POST['LSsession_user'] = 'a determiner plus tard';
465 if(isset($_SESSION['LSsession']['dn'])) {
467 self :: $topDn = $_SESSION['LSsession']['topDn'];
468 self :: $dn = $_SESSION['LSsession']['dn'];
469 self :: $rdn = $_SESSION['LSsession']['rdn'];
470 self :: $ldapServerId = $_SESSION['LSsession']['ldapServerId'];
471 self :: $tmp_file = $_SESSION['LSsession']['tmp_file'];
472 self :: $authParams = $_SESSION['LSsession']['authParams'];
474 if ( self :: cacheLSprofiles() && !isset($_REQUEST['LSsession_refresh']) ) {
475 self :: setLdapServer(self :: $ldapServerId);
476 self :: $LSprofiles = $_SESSION['LSsession']['LSprofiles'];
477 self :: $LSaccess = $_SESSION['LSsession']['LSaccess'];
478 if (!self :: LSldapConnect())
482 self :: setLdapServer(self :: $ldapServerId);
483 if (!self :: LSldapConnect())
485 self :: loadLSprofiles();
488 if ( self :: cacheSudDn() && (!isset($_REQUEST['LSsession_refresh'])) ) {
489 self :: $_subDnLdapServer = $_SESSION['LSsession_subDnLdapServer'];
492 if (!self :: loadLSobject(self :: $ldapServer['authObjectType'])) {
496 self :: getLSuserObject();
498 if ( !self :: cacheLSprofiles() || isset($_REQUEST['LSsession_refresh']) ) {
499 self :: loadLSaccess();
502 $GLOBALS['Smarty'] -> assign('LSsession_username',self :: getLSuserObject() -> getDisplayName());
504 if ($_POST['LSsession_topDn']) {
505 if (self :: validSubDnLdapServer($_POST['LSsession_topDn'])) {
506 self :: $topDn = $_POST['LSsession_topDn'];
507 $_SESSION['LSsession']['topDn'] = $_POST['LSsession_topDn'];
515 // Session inexistante
516 if (isset($_POST['LSsession_ldapserver'])) {
517 self :: setLdapServer($_POST['LSsession_ldapserver']);
520 self :: setLdapServer(0);
523 // Connexion au serveur LDAP
524 if (self :: LSldapConnect()) {
527 if ( $_POST['LSsession_topDn'] != '' ){
528 self :: $topDn = $_POST['LSsession_topDn'];
531 self :: $topDn = self :: $ldapServer['ldap_config']['basedn'];
533 $_SESSION['LSsession_topDn']=self :: $topDn;
535 if (isset($_GET['LSsession_recoverPassword'])) {
536 $recoveryPasswordInfos = self :: recoverPasswd(
537 $_REQUEST['LSsession_user'],
538 $_GET['recoveryHash']
542 if (self :: loadLSclass('LSauth')) {
543 if (isset(self :: $ldapServer['LSauth']['method'])) {
544 $LSauthClass = 'LSauth'.self :: $ldapServer['LSauth']['method'];
545 if (!self :: loadLSclass($LSauthClass)) {
546 LSerror :: addErrorCode('LSsession_08',$LSauthClass);
547 $LSauthClass = 'LSauth';
551 $LSauthClass = 'LSauth';
554 $authObj = new $LSauthClass();
555 self :: $authParams = $authObj->params;
556 if ($authObj -> getPostData()) {
557 $LSuserObject = $authObj -> authenticate();
559 // Authentification réussi
560 self :: $LSuserObject = $LSuserObject;
561 self :: $dn = $LSuserObject->getValue('dn');
562 self :: $rdn = $LSuserObject->getValue('rdn');
563 self :: loadLSprofiles();
564 self :: loadLSaccess();
565 $GLOBALS['Smarty'] -> assign('LSsession_username',self :: getLSuserObject() -> getDisplayName());
566 $_SESSION['LSsession']=self :: getContextInfos();
572 LSerror :: addErrorCode('LSsession_05','LSauth');
577 LSerror :: addErrorCode('LSsession_09');
579 if (self :: $ldapServerId) {
580 $GLOBALS['Smarty'] -> assign('ldapServerId',self :: $ldapServerId);
582 $GLOBALS['Smarty'] -> assign('topDn',self :: $topDn);
583 if (isset($_GET['LSsession_recoverPassword'])) {
584 self :: displayRecoverPasswordForm($recoveryPasswordInfos);
586 elseif(self :: $authParams['displayLoginForm']) {
587 self :: displayLoginForm();
590 self :: setTemplate('blank.tpl');
591 LSerror :: addErrorCode('LSsession_10');
598 * Do recover password
600 * @param[in] $username string The submited username
601 * @param[in] $recoveryHash string The submited recoveryHash
603 * @retval array The recoveryPassword infos for template
605 private static function recoverPasswd($username,$recoveryHash) {
606 $recoveryPasswordInfos=array();
607 if ( self :: loadLSobject(self :: $ldapServer['authObjectType']) ) {
608 $authobject = new self :: $ldapServer['authObjectType']();
609 if (!empty($recoveryHash)) {
610 $filter=Net_LDAP2_Filter::create(
611 self :: $ldapServer['recoverPassword']['recoveryHashAttr'],
615 $result = $authobject -> listObjects($filter,self :: $topDn);
618 $result = $authobject -> searchObject(
621 self :: $ldapServer['authObjectFilter']
625 $nbresult=count($result);
628 LSdebug('hash/username incorrect');
629 LSerror :: addErrorCode('LSsession_06');
631 elseif ($nbresult>1) {
632 LSerror :: addErrorCode('LSsession_07');
635 $rdn = $result[0] -> getValue('rdn');
637 LSdebug('Recover : Id trouvé : '.$username);
638 if (self :: $ldapServer['recoverPassword']) {
639 if (self :: loadLSaddon('mail')) {
640 LSdebug('Récupération active');
642 $emailAddress = $user -> getValue(self :: $ldapServer['recoverPassword']['mailAttr']);
643 $emailAddress = $emailAddress[0];
645 if (checkEmail($emailAddress)) {
646 LSdebug('Email : '.$emailAddress);
647 self :: $dn = $user -> getDn();
649 // 1ère étape : envoie du recoveryHash
650 if (empty($recoveryHash)) {
651 $hash=self :: recoverPasswdFirstStep($user);
653 if (self :: recoverPasswdSendMail($emailAddress,1,$hash)) {
654 // Mail a bien été envoyé
655 $recoveryPasswordInfos['recoveryHashMail']=$emailAddress;
659 // 2nd étape : génération du mot de passe + envoie par mail
661 $pwd=self :: recoverPasswdSecondStep($user);
663 if (self :: recoverPasswdSendMail($emailAddress,2,$pwd)){
664 // Mail a bien été envoyé
665 $recoveryPasswordInfos['newPasswordMail']=$emailAddress;
671 LSerror :: addErrorCode('LSsession_19');
676 LSerror :: addErrorCode('LSsession_18');
680 return $recoveryPasswordInfos;
684 * Send recover password mail
686 * @param[in] $mail string The user's mail
687 * @param[in] $step integer The step
688 * @param[in] $info string The info for formatted message
690 * @retval boolean True on success or False
692 private static function recoverPasswdSendMail($mail,$step,$info) {
695 if (self :: $ldapServer['recoverPassword']['recoveryEmailSender']) {
696 $sendParams['From']=self :: $ldapServer['recoverPassword']['recoveryEmailSender'];
700 if ($_SERVER['HTTPS']=='on') {
701 $recovery_url='https://';
704 $recovery_url='http://';
706 $recovery_url .= $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'&recoveryHash='.$info;
708 $subject = self :: $ldapServer['recoverPassword']['recoveryHashMail']['subject'];
710 self :: $ldapServer['recoverPassword']['recoveryHashMail']['msg'],
715 $subject = self :: $ldapServer['recoverPassword']['newPasswordMail']['subject'];
717 self :: $ldapServer['recoverPassword']['newPasswordMail']['msg'],
722 if (!sendMail($mail,$subject,$msg,$sendParams)) {
723 LSdebug("Problème durant l'envoie du mail");
724 LSerror :: addErrorCode('LSsession_20',4);
732 * Do first step of recovering password
734 * @param[in] $user LSldapObject The LSldapObject of the user
736 * @retval string|False The recory hash on success or False
738 private static function recoverPasswdFirstStep($user) {
740 $rdn=$user -> getValue('rdn');
742 $recovery_hash = md5($rdn . strval(time()) . strval(rand()));
744 $lostPasswdForm = $user -> getForm('lostPassword');
745 $lostPasswdForm -> setPostData(
747 self :: $ldapServer['recoverPassword']['recoveryHashAttr'] => $recovery_hash
752 if($lostPasswdForm -> validate()) {
753 if ($user -> updateData('lostPassword')) {
754 // recoveryHash de l'utilisateur mis à jour
755 return $recovery_hash;
758 // Erreur durant la mise à jour de l'objet
759 LSdebug("Erreur durant la mise à jour de l'objet");
760 LSerror :: addErrorCode('LSsession_20',6);
764 // Erreur durant la validation du formulaire de modification de perte de password
765 LSdebug("Erreur durant la validation du formulaire de modification de perte de password");
766 LSerror :: addErrorCode('LSsession_20',5);
772 * Do second step of recovering password
774 * @param[in] $user LSldapObject The LSldapObject of the user
776 * @retval string|False The new password on success or False
778 private static function recoverPasswdSecondStep($user) {
779 $attr=$user -> attrs[self :: $ldapServer['authObjectTypeAttrPwd']];
780 if ($attr instanceof LSattribute) {
781 $mdp = generatePassword(
782 $attr -> config['html_options']['chars'],
783 $attr -> config['html_options']['lenght']
785 LSdebug('Nvx mpd : '.$mdp);
786 $lostPasswdForm = $user -> getForm('lostPassword');
787 $lostPasswdForm -> setPostData(
789 self :: $ldapServer['recoverPassword']['recoveryHashAttr'] => array(''),
790 self :: $ldapServer['authObjectTypeAttrPwd'] => array($mdp)
794 if($lostPasswdForm -> validate()) {
795 if ($user -> updateData('lostPassword')) {
799 // Erreur durant la mise à jour de l'objet
800 LSdebug("Erreur durant la mise à jour de l'objet");
801 LSerror :: addErrorCode('LSsession_20',3);
805 // Erreur durant la validation du formulaire de modification de perte de password
806 LSdebug("Erreur durant la validation du formulaire de modification de perte de password");
807 LSerror :: addErrorCode('LSsession_20',2);
811 // l'attribut password n'existe pas
812 LSdebug("L'attribut password n'existe pas");
813 LSerror :: addErrorCode('LSsession_20',1);
819 * Retourne les informations du contexte
821 * @author Benjamin Renard <brenard@easter-eggs.com
823 * @retval array Tableau associatif des informations du contexte
825 private static function getContextInfos() {
827 'tmp_file' => self :: $tmp_file,
828 'topDn' => self :: $topDn,
830 'rdn' => self :: $rdn,
831 'ldapServerId' => self :: $ldapServerId,
832 'ldapServer' => self :: $ldapServer,
833 'LSprofiles' => self :: $LSprofiles,
834 'LSaccess' => self :: $LSaccess,
835 'authParams' => self :: $authParams
840 * Retourne l'objet de l'utilisateur connecté
842 * @author Benjamin Renard <brenard@easter-eggs.com
844 * @retval mixed L'objet de l'utilisateur connecté ou false si il n'a pas put
847 public static function getLSuserObject($dn=null) {
851 if (!self :: $LSuserObject) {
852 if (self :: loadLSobject(self :: $ldapServer['authObjectType'])) {
853 self :: $LSuserObject = new self :: $ldapServer['authObjectType']();
854 self :: $LSuserObject -> loadData(self :: $dn);
860 return self :: $LSuserObject;
864 * Retourne le DN de l'utilisateur connecté
866 * @author Benjamin Renard <brenard@easter-eggs.com
868 * @retval string Le DN de l'utilisateur connecté
870 public static function getLSuserObjectDn() {
875 * Modifie l'utilisateur connecté à la volé
877 * @param[in] $object Mixed L'objet Ldap du nouvel utilisateur
878 * le type doit correspondre à
879 * self :: $ldapServer['authObjectType']
881 * @retval boolean True en cas de succès, false sinon
883 public static function changeAuthUser($object) {
884 if ($object instanceof self :: $ldapServer['authObjectType']) {
885 self :: $dn = $object -> getDn();
886 $rdn = $object -> getValue('rdn');
891 self :: $LSuserObject = $object;
893 if(self :: loadLSprofiles()) {
894 self :: loadLSaccess();
895 $_SESSION['LSsession']=self :: getContextInfos();
903 * Définition du serveur Ldap de la session
905 * Définition du serveur Ldap de la session à partir de son ID dans
906 * le tableau LSconfig :: get('ldap_servers').
908 * @param[in] integer Index du serveur Ldap
910 * @retval boolean True sinon false.
912 public static function setLdapServer($id) {
913 $conf = LSconfig :: get("ldap_servers.$id");
914 if ( is_array($conf) ) {
915 self :: $ldapServerId = $id;
916 self :: $ldapServer = $conf;
926 * Connexion au serveur Ldap
928 * @retval boolean True sinon false.
930 public static function LSldapConnect() {
931 if (self :: $ldapServer) {
932 self :: includeFile(LSconfig :: get('NetLDAP2'));
933 if (!self :: loadLSclass('LSldap')) {
936 LSldap :: connect(self :: $ldapServer['ldap_config']);
937 if (LSldap :: isConnected()) {
945 LSerror :: addErrorCode('LSsession_03');
951 * Use this function to know if subDn is enabled for the curent LdapServer
955 public static function subDnIsEnabled() {
956 if (!isset(self :: $ldapServer['subDn'])) {
959 if ( !is_array(self :: $ldapServer['subDn']) ) {
966 * Retourne les sous-dns du serveur Ldap courant
968 * @retval mixed Tableau des subDn, false si une erreur est survenue.
970 public static function getSubDnLdapServer() {
971 if (self :: cacheSudDn() && isset(self :: $_subDnLdapServer[self :: $ldapServerId])) {
972 return self :: $_subDnLdapServer[self :: $ldapServerId];
974 if (!self::subDnIsEnabled()) {
978 foreach(self :: $ldapServer['subDn'] as $subDn_name => $subDn_config) {
979 if ($subDn_name == 'LSobject') {
980 if (is_array($subDn_config)) {
981 foreach($subDn_config as $LSobject_name => $LSoject_config) {
982 if ($LSoject_config['basedn']) {
983 $basedn = $LSoject_config['basedn'];
986 $basedn = self::getRootDn();
988 if ($LSoject_config['displayName']) {
989 $displayName = $LSoject_config['displayName'];
994 if( self :: loadLSobject($LSobject_name) ) {
995 if ($subdnobject = new $LSobject_name()) {
996 $tbl_return = $subdnobject -> getSelectArray(NULL,$basedn,$displayName);
997 if (is_array($tbl_return)) {
998 $return=array_merge($return,$tbl_return);
1001 LSerror :: addErrorCode('LSsession_17',3);
1005 LSerror :: addErrorCode('LSsession_17',2);
1011 LSerror :: addErrorCode('LSsession_17',1);
1015 if ((isCompatibleDNs($subDn_config['dn'],self :: $ldapServer['ldap_config']['basedn']))&&($subDn_config['dn']!="")) {
1016 $return[$subDn_config['dn']] = __($subDn_name);
1020 if (self :: cacheSudDn()) {
1021 self :: $_subDnLdapServer[self :: $ldapServerId]=$return;
1022 $_SESSION['LSsession_subDnLdapServer'] = self :: $_subDnLdapServer;
1028 * Retourne la liste de subDn du serveur Ldap utilise
1029 * trié par la profondeur dans l'arboressence (ordre décroissant)
1031 * @return array() Tableau des subDn trié
1033 public static function getSortSubDnLdapServer() {
1034 $subDnLdapServer = self :: getSubDnLdapServer();
1035 if (!$subDnLdapServer) {
1038 uksort($subDnLdapServer,"compareDn");
1039 return $subDnLdapServer;
1043 * Retourne les options d'une liste déroulante pour le choix du topDn
1044 * de connexion au serveur Ldap
1046 * Liste les subdn (self :: $ldapServer['subDn'])
1048 * @retval string Les options (<option>) pour la sélection du topDn.
1050 public static function getSubDnLdapServerOptions($selected=NULL) {
1051 $list = self :: getSubDnLdapServer();
1055 foreach($list as $dn => $txt) {
1056 if ($selected && ($selected==$dn)) {
1057 $selected_txt = ' selected';
1062 $display.="<option value=\"".$dn."\"$selected_txt>".$txt."</option>\n";
1070 * Vérifie qu'un subDn est déclaré
1072 * @param[in] string Un subDn
1074 * @retval boolean True si le subDn existe, False sinon
1076 public static function validSubDnLdapServer($subDn) {
1077 $listTopDn = self :: getSubDnLdapServer();
1078 if(is_array($listTopDn)) {
1079 foreach($listTopDn as $dn => $txt) {
1089 * Test un couple LSobject/pwd
1091 * Test un bind sur le serveur avec le dn de l'objet et le mot de passe fourni.
1093 * @param[in] LSobject L'object "user" pour l'authentification
1094 * @param[in] string Le mot de passe à tester
1096 * @retval boolean True si l'authentification à réussi, false sinon.
1098 public static function checkUserPwd($object,$pwd) {
1099 return LSldap :: checkBind($object -> getValue('dn'),$pwd);
1103 * Affiche le formulaire de login
1105 * Défini les informations pour le template Smarty du formulaire de login.
1109 public static function displayLoginForm() {
1110 $GLOBALS['Smarty'] -> assign('pagetitle',_('Connection'));
1111 if (isset($_GET['LSsession_logout'])) {
1112 $GLOBALS['Smarty'] -> assign('loginform_action','index.php');
1115 $GLOBALS['Smarty'] -> assign('loginform_action',$_SERVER['REQUEST_URI']);
1117 if (count(LSconfig :: get('ldap_servers'))==1) {
1118 $GLOBALS['Smarty'] -> assign('loginform_ldapserver_style','style="display: none"');
1120 $GLOBALS['Smarty'] -> assign('loginform_label_ldapserver',_('LDAP server'));
1121 $ldapservers_name=array();
1122 $ldapservers_index=array();
1123 foreach(LSconfig :: get('ldap_servers') as $id => $infos) {
1124 $ldapservers_index[]=$id;
1125 $ldapservers_name[]=__($infos['name']);
1127 $GLOBALS['Smarty'] -> assign('loginform_ldapservers_name',$ldapservers_name);
1128 $GLOBALS['Smarty'] -> assign('loginform_ldapservers_index',$ldapservers_index);
1130 $GLOBALS['Smarty'] -> assign('loginform_label_level',_('Level'));
1131 $GLOBALS['Smarty'] -> assign('loginform_label_user',_('Identifier'));
1132 $GLOBALS['Smarty'] -> assign('loginform_label_pwd',_('Password'));
1133 $GLOBALS['Smarty'] -> assign('loginform_label_submit',_('Connect'));
1134 $GLOBALS['Smarty'] -> assign('loginform_label_recoverPassword',_('Forgot your password ?'));
1136 self :: setTemplate('login.tpl');
1137 self :: addJSscript('LSsession_login.js');
1141 * Affiche le formulaire de récupération de mot de passe
1143 * Défini les informations pour le template Smarty du formulaire de
1144 * récupération de mot de passe
1146 * @param[in] $infos array() Information sur le status du processus de
1147 * recouvrement de mot de passe
1151 public static function displayRecoverPasswordForm($recoveryPasswordInfos) {
1152 $GLOBALS['Smarty'] -> assign('pagetitle',_('Recovery of your credentials'));
1153 $GLOBALS['Smarty'] -> assign('recoverpasswordform_action','index.php?LSsession_recoverPassword');
1155 if (count(LSconfig :: get('ldap_servers'))==1) {
1156 $GLOBALS['Smarty'] -> assign('recoverpasswordform_ldapserver_style','style="display: none"');
1159 $GLOBALS['Smarty'] -> assign('recoverpasswordform_label_ldapserver',_('LDAP server'));
1160 $ldapservers_name=array();
1161 $ldapservers_index=array();
1162 foreach(LSconfig :: get('ldap_servers') as $id => $infos) {
1163 $ldapservers_index[]=$id;
1164 $ldapservers_name[]=$infos['name'];
1166 $GLOBALS['Smarty'] -> assign('recoverpasswordform_ldapservers_name',$ldapservers_name);
1167 $GLOBALS['Smarty'] -> assign('recoverpasswordform_ldapservers_index',$ldapservers_index);
1169 $GLOBALS['Smarty'] -> assign('recoverpasswordform_label_user',_('Identifier'));
1170 $GLOBALS['Smarty'] -> assign('recoverpasswordform_label_submit',_('Validate'));
1171 $GLOBALS['Smarty'] -> assign('recoverpasswordform_label_back',_('Back'));
1173 $recoverpassword_msg = _('Please fill the identifier field to proceed recovery procedure');
1175 if (isset($recoveryPasswordInfos['recoveryHashMail'])) {
1176 $recoverpassword_msg = getFData(
1177 _("An email has been sent to %{mail}. " .
1178 "Please follow the instructions on it."),
1179 $recoveryPasswordInfos['recoveryHashMail']
1183 if (isset($recoveryPasswordInfos['newPasswordMail'])) {
1184 $recoverpassword_msg = getFData(
1185 _("Your new password has been sent to %{mail}. "),
1186 $recoveryPasswordInfos['newPasswordMail']
1190 $GLOBALS['Smarty'] -> assign('recoverpassword_msg',$recoverpassword_msg);
1192 self :: setTemplate('recoverpassword.tpl');
1193 self :: addJSscript('LSsession_recoverPassword.js');
1197 * Défini le template Smarty à utiliser
1199 * Remarque : les fichiers de templates doivent se trouver dans le dossier
1202 * @param[in] string Le nom du fichier de template
1206 public static function setTemplate($template) {
1207 self :: $template = $template;
1211 * Ajoute un script JS au chargement de la page
1213 * Remarque : les scripts doivents être dans le dossier LS_JS_DIR.
1215 * @param[in] $script Le nom du fichier de script à charger.
1219 public static function addJSscript($file,$path=NULL) {
1224 self :: $JSscripts[$path.$file]=$script;
1228 * Ajouter un paramètre de configuration Javascript
1230 * @param[in] $name string Nom de la variable de configuration
1231 * @param[in] $val mixed Valeur de la variable de configuration
1235 public static function addJSconfigParam($name,$val) {
1236 self :: $_JSconfigParams[$name]=$val;
1240 * Ajoute une feuille de style au chargement de la page
1242 * Remarque : les scripts doivents être dans le dossier LS_CSS_DIR.
1244 * @param[in] $script Le nom du fichier css à charger.
1248 public static function addCssFile($file,$path=NULL) {
1253 self :: $CssFiles[$path.$file]=$cssFile;
1257 * Affiche le template Smarty
1259 * Charge les dépendances et affiche le template Smarty
1263 public static function displayTemplate() {
1266 foreach ($GLOBALS['defaultJSscipts'] as $script) {
1267 $JSscript_txt.="<script src='".LS_JS_DIR.$script."' type='text/javascript'></script>\n";
1270 foreach (self :: $JSscripts as $script) {
1271 if (!$script['path']) {
1272 $script['path']=LS_JS_DIR;
1275 $script['path'].='/';
1277 $JSscript_txt.="<script src='".$script['path'].$script['file']."' type='text/javascript'></script>\n";
1280 $KAconf = LSconfig :: get('keepLSsessionActive');
1283 (!isset(self :: $ldapServer['keepLSsessionActive']))
1285 (!($KAconf === false))
1288 (self :: $ldapServer['keepLSsessionActive'])
1290 self :: addJSconfigParam('keepLSsessionActive',ini_get('session.gc_maxlifetime'));
1293 $GLOBALS['Smarty'] -> assign('LSjsConfig',json_encode(self :: $_JSconfigParams));
1296 $JSscript_txt.="<script type='text/javascript'>LSdebug_active = 1;</script>\n";
1299 $JSscript_txt.="<script type='text/javascript'>LSdebug_active = 0;</script>\n";
1302 $GLOBALS['Smarty'] -> assign('LSsession_js',$JSscript_txt);
1305 self :: addCssFile("LSdefault.css");
1307 foreach (self :: $CssFiles as $file) {
1308 if (!$file['path']) {
1309 $file['path']=LS_CSS_DIR.'/';
1311 $Css_txt.="<link rel='stylesheet' type='text/css' href='".$file['path'].$file['file']."' />\n";
1313 $GLOBALS['Smarty'] -> assign('LSsession_css',$Css_txt);
1315 if (isset(self :: $LSaccess[self :: $topDn])) {
1316 $GLOBALS['Smarty'] -> assign('LSaccess',self :: $LSaccess[self :: $topDn]);
1320 $listTopDn = self :: getSubDnLdapServer();
1321 if (is_array($listTopDn)) {
1323 $GLOBALS['Smarty'] -> assign('label_level',self :: getSubDnLabel());
1324 $GLOBALS['Smarty'] -> assign('_refresh',_('Refresh'));
1325 $LSsession_topDn_index = array();
1326 $LSsession_topDn_name = array();
1327 foreach($listTopDn as $index => $name) {
1328 $LSsession_topDn_index[] = $index;
1329 $LSsession_topDn_name[] = $name;
1331 $GLOBALS['Smarty'] -> assign('LSsession_subDn_indexes',$LSsession_topDn_index);
1332 $GLOBALS['Smarty'] -> assign('LSsession_subDn_names',$LSsession_topDn_name);
1333 $GLOBALS['Smarty'] -> assign('LSsession_subDn',self :: $topDn);
1334 $GLOBALS['Smarty'] -> assign('LSsession_subDnName',self :: getSubDnName());
1337 $GLOBALS['Smarty'] -> assign('LSlanguages',self :: getLangList());
1338 $GLOBALS['Smarty'] -> assign('LSlang',self :: $lang);
1339 $GLOBALS['Smarty'] -> assign('LSencoding',self :: $encoding);
1340 $GLOBALS['Smarty'] -> assign('lang_label',_('Language'));
1342 $GLOBALS['Smarty'] -> assign('displayLogoutBtn',self :: $authParams['displayLogoutBtn']);
1345 if((!empty($_SESSION['LSsession_infos']))&&(is_array($_SESSION['LSsession_infos']))) {
1346 $txt_infos="<ul>\n";
1347 foreach($_SESSION['LSsession_infos'] as $info) {
1348 $txt_infos.="<li>$info</li>\n";
1350 $txt_infos.="</ul>\n";
1351 $GLOBALS['Smarty'] -> assign('LSinfos',$txt_infos);
1352 $_SESSION['LSsession_infos']=array();
1355 if (self :: $ajaxDisplay) {
1356 $GLOBALS['Smarty'] -> assign('LSerror_txt',LSerror :: getErrors());
1357 $GLOBALS['Smarty'] -> assign('LSdebug_txt',LSdebug_print(true));
1360 LSerror :: display();
1363 if (!self :: $template)
1364 self :: setTemplate('empty.tpl');
1366 $GLOBALS['Smarty'] -> assign('connected_as',_("Connected as"));
1368 $GLOBALS['Smarty'] -> display(self :: $template);
1372 * Défini que l'affichage se fera ou non via un retour Ajax
1374 * @param[in] $val boolean True pour que l'affichage se fasse par un retour
1378 public static function setAjaxDisplay($val=true) {
1379 self :: $ajaxDisplay = (boolean)$val;
1383 * Affiche un retour Ajax
1387 public static function displayAjaxReturn($data=array()) {
1388 if (isset($data['LSredirect']) && (!LSdebugDefined()) ) {
1389 echo json_encode($data);
1393 $data['LSjsConfig'] = self :: $_JSconfigParams;
1396 if((!empty($_SESSION['LSsession_infos']))&&(is_array($_SESSION['LSsession_infos']))) {
1397 $txt_infos="<ul>\n";
1398 foreach($_SESSION['LSsession_infos'] as $info) {
1399 $txt_infos.="<li>$info</li>\n";
1401 $txt_infos.="</ul>\n";
1402 $data['LSinfos'] = $txt_infos;
1403 $_SESSION['LSsession_infos']=array();
1406 if (LSerror :: errorsDefined()) {
1407 $data['LSerror'] = LSerror :: getErrors();
1410 if (isset($_REQUEST['imgload'])) {
1411 $data['imgload'] = $_REQUEST['imgload'];
1414 if (LSdebugDefined()) {
1415 $data['LSdebug'] = LSdebug_print(true,false);
1418 echo json_encode($data);
1422 * Retournne un template Smarty compilé
1424 * @param[in] string $template Le template à retourner
1425 * @param[in] array $variables Variables Smarty à assigner avant l'affichage
1427 * @retval string Le HTML compilé du template
1429 public static function fetchTemplate($template,$variables=array()) {
1430 foreach($variables as $name => $val) {
1431 $GLOBALS['Smarty'] -> assign($name,$val);
1433 return $GLOBALS['Smarty'] -> fetch($template);
1437 * Charge les droits LS de l'utilisateur
1439 * @retval boolean True si le chargement à réussi, false sinon.
1441 private static function loadLSprofiles() {
1442 if (is_array(self :: $ldapServer['LSprofiles'])) {
1443 foreach (self :: $ldapServer['LSprofiles'] as $profile => $profileInfos) {
1444 if (is_array($profileInfos)) {
1445 foreach ($profileInfos as $topDn => $rightsInfos) {
1447 * If $topDn == 'LSobject', we search for each LSobject type to find
1448 * all items on witch the user will have powers.
1450 if ($topDn == 'LSobjects') {
1451 if (is_array($rightsInfos)) {
1452 foreach ($rightsInfos as $LSobject => $listInfos) {
1453 if (self :: loadLSclass('LSsearch')) {
1454 if ($listInfos['filter']) {
1455 $filter = self :: getLSuserObject() -> getFData($listInfos['filter']);
1458 $filter = '('.$listInfos['attr'].'='.self :: getLSuserObject() -> getFData($listInfos['attr_value']).')';
1462 'basedn' => $listInfos['basedn'],
1466 if (is_array($listInfos['params'])) {
1467 $params = array_merge($listInfos['params'],$params);
1470 $LSsearch = new LSsearch($LSobject,'LSsession :: loadLSprofiles',$params,true);
1471 $LSsearch -> run(false);
1473 $LSprofiles[$profile] = $LSsearch -> listObjectsDn();
1478 LSdebug('LSobjects => [] doit etre un tableau');
1482 if (is_array($rightsInfos)) {
1483 foreach($rightsInfos as $dn => $conf) {
1484 if ((isset($conf['attr'])) && (isset($conf['LSobject']))) {
1485 if( self :: loadLSobject($conf['LSobject']) ) {
1486 if ($object = new $conf['LSobject']()) {
1487 if ($object -> loadData($dn)) {
1488 $listDns=$object -> getValue($conf['attr']);
1489 $valKey = (isset($conf['attr_value']))?$conf['attr_value']:'%{dn}';
1490 $val = self :: getLSuserObject() -> getFData($valKey);
1491 if (is_array($listDns)) {
1492 if (in_array($val,$listDns)) {
1493 self :: $LSprofiles[$profile][] = $topDn;
1498 LSdebug('Impossible de chargé le dn : '.$dn);
1502 LSdebug('Impossible de créer l\'objet de type : '.$conf['LSobject']);
1507 if (self :: $dn == $dn) {
1508 self :: $LSprofiles[$profile][] = $topDn;
1514 if ( self :: $dn == $rightsInfos ) {
1515 self :: $LSprofiles[$profile][] = $topDn;
1518 } // fin else ($topDn == 'LSobjects')
1519 } // fin foreach($profileInfos)
1520 } // fin is_array($profileInfos)
1521 } // fin foreach LSprofiles
1522 LSdebug(self :: $LSprofiles);
1531 * Charge les droits d'accès de l'utilisateur pour construire le menu de l'interface
1535 private static function loadLSaccess() {
1537 if (is_array(self :: $ldapServer['subDn'])) {
1538 foreach(self :: $ldapServer['subDn'] as $name => $config) {
1539 if ($name=='LSobject') {
1540 if (is_array($config)) {
1542 // Définition des subDns
1543 foreach($config as $objectType => $objectConf) {
1544 if (self :: loadLSobject($objectType)) {
1545 if ($subdnobject = new $objectType()) {
1546 $tbl = $subdnobject -> getSelectArray(NULL,self::getRootDn(),NULL,NULL,false);
1547 if (is_array($tbl)) {
1548 // Définition des accès
1550 if (is_array($objectConf['LSobjects'])) {
1551 foreach($objectConf['LSobjects'] as $type) {
1552 if (self :: loadLSobject($type)) {
1553 if (self :: canAccess($type)) {
1554 $access[$type] = LSconfig :: get('LSobjects.'.$type.'.label');
1559 foreach($tbl as $dn => $dn_name) {
1560 $LSaccess[$dn]=$access;
1569 if ((isCompatibleDNs(self :: $ldapServer['ldap_config']['basedn'],$config['dn']))&&($config['dn']!='')) {
1571 if (is_array($config['LSobjects'])) {
1572 foreach($config['LSobjects'] as $objectType) {
1573 if (self :: loadLSobject($objectType)) {
1574 if (self :: canAccess($objectType)) {
1575 $access[$objectType] = LSconfig :: get('LSobjects.'.$objectType.'.label');
1580 $LSaccess[$config['dn']]=$access;
1586 if(is_array(self :: $ldapServer['LSaccess'])) {
1588 foreach(self :: $ldapServer['LSaccess'] as $objectType) {
1589 if (self :: loadLSobject($objectType)) {
1590 if (self :: canAccess($objectType)) {
1591 $access[$objectType] = LSconfig :: get('LSobjects.'.$objectType.'.label');
1595 $LSaccess[self :: $topDn] = $access;
1598 foreach($LSaccess as $dn => $access) {
1599 $LSaccess[$dn] = array_merge(
1601 'SELF' => 'My account'
1607 self :: $LSaccess = $LSaccess;
1608 $_SESSION['LSsession']['LSaccess'] = $LSaccess;
1612 * Dit si l'utilisateur est du profil pour le DN spécifié
1614 * @param[in] string $profile de l'objet
1615 * @param[in] string $dn DN de l'objet
1617 * @retval boolean True si l'utilisateur est du profil sur l'objet, false sinon.
1619 public static function isLSprofile($dn,$profile) {
1620 if (is_array(self :: $LSprofiles[$profile])) {
1621 foreach(self :: $LSprofiles[$profile] as $topDn) {
1625 else if ( isCompatibleDNs($dn,$topDn) ) {
1634 * Retourne qui est l'utilisateur par rapport à l'object
1636 * @param[in] string Le DN de l'objet
1638 * @retval string 'admin'/'self'/'user' pour Admin , l'utilisateur lui même ou un simple utilisateur
1640 public static function whoami($dn) {
1641 $retval = array('user');
1643 foreach(self :: $LSprofiles as $profile => $infos) {
1644 if(self :: isLSprofile($dn,$profile)) {
1649 if (self :: $dn == $dn) {
1657 * Retourne le droit de l'utilisateur à accèder à un objet
1659 * @param[in] string $LSobject Le type de l'objet
1660 * @param[in] string $dn Le DN de l'objet (le container_dn du type de l'objet par défaut)
1661 * @param[in] string $right Le type de droit d'accès à tester ('r'/'w')
1662 * @param[in] string $attr Le nom de l'attribut auquel on test l'accès
1664 * @retval boolean True si l'utilisateur a accès, false sinon
1666 public static function canAccess($LSobject,$dn=NULL,$right=NULL,$attr=NULL) {
1667 if (!self :: loadLSobject($LSobject)) {
1671 $whoami = self :: whoami($dn);
1672 if ($dn==self :: getLSuserObject() -> getValue('dn')) {
1673 if (!self :: in_menu('SELF')) {
1678 $obj = new $LSobject();
1680 if (!self :: in_menu($LSobject,$obj -> subDnValue)) {
1686 $objectdn=LSconfig :: get('LSobjects.'.$LSobject.'.container_dn').','.self :: $topDn;
1687 $whoami = self :: whoami($objectdn);
1690 // Pour un attribut particulier
1693 $attr=LSconfig :: get('LSobjects.'.$LSobject.'.rdn');
1695 if (!is_array(LSconfig :: get('LSobjects.'.$LSobject.'.attrs.'.$attr))) {
1700 foreach($whoami as $who) {
1701 $nr = LSconfig :: get('LSobjects.'.$LSobject.'.attrs.'.$attr.'.rights.'.$who);
1705 else if($nr == 'r') {
1712 if (($right=='r')||($right=='w')) {
1719 if ( ($r=='r') || ($r=='w') ) {
1726 // Pour un attribut quelconque
1727 $attrs_conf=LSconfig :: get('LSobjects.'.$LSobject.'.attrs');
1728 if (is_array($attrs_conf)) {
1729 if (($right=='r')||($right=='w')) {
1730 foreach($whoami as $who) {
1731 foreach ($attrs_conf as $attr_name => $attr_config) {
1732 if ($attr_config['rights'][$who]==$right) {
1739 foreach($whoami as $who) {
1740 foreach ($attrs_conf as $attr_name => $attr_config) {
1741 if ( ($attr_config['rights'][$who]=='r') || ($attr_config['rights'][$who]=='w') ) {
1752 * Retourne le droit de l'utilisateur à editer à un objet
1754 * @param[in] string $LSobject Le type de l'objet
1755 * @param[in] string $dn Le DN de l'objet (le container_dn du type de l'objet par défaut)
1756 * @param[in] string $attr Le nom de l'attribut auquel on test l'accès
1758 * @retval boolean True si l'utilisateur a accès, false sinon
1760 public static function canEdit($LSobject,$dn=NULL,$attr=NULL) {
1761 return self :: canAccess($LSobject,$dn,'w',$attr);
1765 * Retourne le droit de l'utilisateur à supprimer un objet
1767 * @param[in] string $LSobject Le type de l'objet
1768 * @param[in] string $dn Le DN de l'objet (le container_dn du type de l'objet par défaut)
1770 * @retval boolean True si l'utilisateur a accès, false sinon
1772 public static function canRemove($LSobject,$dn) {
1773 return self :: canAccess($LSobject,$dn,'w','rdn');
1777 * Retourne le droit de l'utilisateur à créer un objet
1779 * @param[in] string $LSobject Le type de l'objet
1781 * @retval boolean True si l'utilisateur a accès, false sinon
1783 public static function canCreate($LSobject) {
1784 return self :: canAccess($LSobject,NULL,'w','rdn');
1788 * Retourne le droit de l'utilisateur à gérer la relation d'objet
1790 * @param[in] string $dn Le DN de l'objet (le container_dn du type de l'objet par défaut)
1791 * @param[in] string $LSobject Le type de l'objet
1792 * @param[in] string $relationName Le nom de la relation avec l'objet
1793 * @param[in] string $right Le type de droit a vérifier ('r' ou 'w')
1795 * @retval boolean True si l'utilisateur a accès, false sinon
1797 public static function relationCanAccess($dn,$LSobject,$relationName,$right=NULL) {
1798 $relConf=LSconfig :: get('LSobjects.'.$LSobject.'.LSrelation.'.$relationName);
1799 if (!is_array($relConf))
1801 $whoami = self :: whoami($dn);
1803 if (($right=='w') || ($right=='r')) {
1805 foreach($whoami as $who) {
1806 $nr = $relConf['rights'][$who];
1810 else if($nr == 'r') {
1822 foreach($whoami as $who) {
1823 if (($relConf['rights'][$who] == 'w') || ($relConf['rights'][$who] == 'r')) {
1832 * Retourne le droit de l'utilisateur à modifier la relation d'objet
1834 * @param[in] string $dn Le DN de l'objet (le container_dn du type de l'objet par défaut)
1835 * @param[in] string $LSobject Le type de l'objet
1836 * @param[in] string $relationName Le nom de la relation avec l'objet
1838 * @retval boolean True si l'utilisateur a accès, false sinon
1840 public static function relationCanEdit($dn,$LSobject,$relationName) {
1841 return self :: relationCanAccess($dn,$LSobject,$relationName,'w');
1845 * Ajoute un fichier temporaire
1847 * @author Benjamin Renard <brenard@easter-eggs.com>
1851 public static function addTmpFile($value,$filePath) {
1852 $hash = mhash(MHASH_MD5,$value);
1853 self :: $tmp_file[$filePath] = $hash;
1854 $_SESSION['LSsession']['tmp_file'][$filePath] = $hash;
1858 * Retourne le chemin du fichier temporaire si l'existe
1860 * @author Benjamin Renard <brenard@easter-eggs.com>
1862 * @param[in] $value La valeur du fichier
1866 public static function tmpFileExist($value) {
1867 $hash = mhash(MHASH_MD5,$value);
1868 foreach(self :: $tmp_file as $filePath => $contentHash) {
1869 if ($hash == $contentHash) {
1877 * Retourne le chemin du fichier temporaire
1879 * Retourne le chemin du fichier temporaire qu'il créera à partir de la valeur
1880 * s'il n'existe pas déjà .
1882 * @author Benjamin Renard <brenard@easter-eggs.com>
1884 * @param[in] $value La valeur du fichier
1888 public static function getTmpFile($value) {
1889 $exist = self :: tmpFileExist($value);
1891 $img_path = LS_TMP_DIR .rand().'.tmp';
1892 $fp = fopen($img_path, "w");
1893 fwrite($fp, $value);
1895 self :: addTmpFile($value,$img_path);
1904 * Supprime les fichiers temporaires
1906 * @author Benjamin Renard <brenard@easter-eggs.com>
1910 public static function deleteTmpFile($filePath=NULL) {
1913 unset(self :: $tmp_file[$filePath]);
1914 unset($_SESSION['LSsession']['tmp_file'][$filePath]);
1917 foreach(self :: $tmp_file as $file => $content) {
1920 self :: $tmp_file = array();
1921 $_SESSION['LSsession']['tmp_file'] = array();
1926 * Retourne true si le cache des droits est activé
1928 * @author Benjamin Renard <brenard@easter-eggs.com>
1930 * @retval boolean True si le cache des droits est activé, false sinon.
1932 public static function cacheLSprofiles() {
1933 return ( (LSconfig :: get('cacheLSprofiles')) || (self :: $ldapServer['cacheLSprofiles']) );
1937 * Retourne true si le cache des subDn est activé
1939 * @author Benjamin Renard <brenard@easter-eggs.com>
1941 * @retval boolean True si le cache des subDn est activé, false sinon.
1943 public static function cacheSudDn() {
1944 return ( (LSconfig :: get('cacheSubDn')) || (self :: $ldapServer['cacheSubDn']));
1948 * Retourne true si le cache des recherches est activé
1950 * @author Benjamin Renard <brenard@easter-eggs.com>
1952 * @retval boolean True si le cache des recherches est activé, false sinon.
1954 public static function cacheSearch() {
1955 return ( (LSconfig :: get('cacheSearch')) || (self :: $ldapServer['cacheSearch']));
1959 * Retourne le label des niveaux pour le serveur ldap courant
1961 * @author Benjamin Renard <brenard@easter-eggs.com>
1963 * @retval string Le label des niveaux pour le serveur ldap dourant
1965 public static function getSubDnLabel() {
1966 return (self :: $ldapServer['subDnLabel']!='')?__(self :: $ldapServer['subDnLabel']):_('Level');
1970 * Retourne le nom du subDn
1972 * @param[in] $subDn string subDn
1974 * @retval string Le nom du subDn ou '' sinon
1976 public static function getSubDnName($subDn=false) {
1978 $subDn = self :: $topDn;
1980 if (self :: getSubDnLdapServer()) {
1981 if (isset(self :: $_subDnLdapServer[self :: $ldapServerId][$subDn])) {
1982 return self :: $_subDnLdapServer[self :: $ldapServerId][$subDn];
1989 * L'objet est t-il utilisé pour listé les subDnS
1991 * @param[in] $type string Le type d'objet
1993 * @retval boolean true si le type d'objet est un subDnObject, false sinon
1995 public static function isSubDnLSobject($type) {
1997 if (is_array(self :: $ldapServer['subDn']['LSobject'])) {
1998 foreach(self :: $ldapServer['subDn']['LSobject'] as $key => $value) {
2008 * Indique si un type d'objet est dans le menu courant
2010 * @retval boolean true si le type d'objet est dans le menu, false sinon
2012 public static function in_menu($LSobject,$topDn=NULL) {
2014 $topDn=self :: $topDn;
2016 return isset(self :: $LSaccess[$topDn][$LSobject]);
2020 * Indique si le serveur LDAP courant a des subDn
2022 * @retval boolean true si le serveur LDAP courant a des subDn, false sinon
2024 public static function haveSubDn() {
2025 return (is_array(self :: $ldapServer['subDn']));
2029 * Ajoute une information à afficher
2031 * @param[in] $msg string Le message à afficher
2035 public static function addInfo($msg) {
2036 $_SESSION['LSsession_infos'][]=$msg;
2040 * Redirection de l'utilisateur vers une autre URL
2042 * @param[in] $url string L'URL
2043 * @param[in] $exit boolean Si true, l'execution script s'arrête après la redirection
2047 public static function redirect($url,$exit=true) {
2048 $GLOBALS['Smarty'] -> assign('url',$url);
2049 $GLOBALS['Smarty'] -> display('redirect.tpl');
2056 * Retourne l'adresse mail d'emission configurée pour le serveur courant
2058 * @retval string Adresse mail d'emission
2060 public static function getEmailSender() {
2061 return self :: $ldapServer['emailSender'];
2065 * Ajout d'une information d'aide
2067 * @param[in] $group string Le nom du groupe d'infos dans lequels ajouter
2069 * @param[in] $infos array Tableau array(name => value) des infos
2073 public static function addHelpInfos($group,$infos) {
2074 if (is_array($infos)) {
2075 if (is_array(self :: $_JSconfigParams['helpInfos'][$group])) {
2076 self :: $_JSconfigParams['helpInfos'][$group] = array_merge(self :: $_JSconfigParams['helpInfos'][$group],$infos);
2079 self :: $_JSconfigParams['helpInfos'][$group] = $infos;
2085 * Défini les codes erreur relative à la classe LSsession
2089 private static function defineLSerrors() {
2093 LSerror :: defineError('LSsession_01',
2094 _("LSsession : The constant %{const} is not defined.")
2096 LSerror :: defineError('LSsession_02',
2097 _("LSsession : The %{addon} support is uncertain. Verify system compatibility and the add-on configuration.")
2099 LSerror :: defineError('LSsession_03',
2100 _("LSsession : LDAP server's configuration data are invalid. Can't connect.")
2102 LSerror :: defineError('LSsession_04',
2103 _("LSsession : Failed to load LSobject type %{type} : unknon type.")
2105 LSerror :: defineError('LSsession_05',
2106 _("LSsession : Failed to load LSclass %{class}.")
2108 LSerror :: defineError('LSsession_06',
2109 _("LSsession : Login or password incorrect.")
2111 LSerror :: defineError('LSsession_07',
2112 _("LSsession : Impossible to identify you : Duplication of identities.")
2114 LSerror :: defineError('LSsession_08',
2115 _("LSsession : Can't load class of authentification (%{class}).")
2117 LSerror :: defineError('LSsession_09',
2118 _("LSsession : Can't connect to LDAP server.")
2120 LSerror :: defineError('LSsession_10',
2121 _("LSsession : Impossible to authenticate you.")
2123 LSerror :: defineError('LSsession_11',
2124 _("LSsession : Your are not authorized to do this action.")
2126 LSerror :: defineError('LSsession_12',
2127 _("LSsession : Some informations are missing to display this page.")
2129 // 13 -> 16 : not yet used
2130 LSerror :: defineError('LSsession_17',
2131 _("LSsession : Error during creation of list of levels. Contact administrators. (Code : %{code})")
2133 LSerror :: defineError('LSsession_18',
2134 _("LSsession : The password recovery is disabled for this LDAP server.")
2136 LSerror :: defineError('LSsession_19',
2137 _("LSsession : Some informations are missing to recover your password. Contact administrators.")
2139 LSerror :: defineError('LSsession_20',
2140 _("LSsession : Error during password recovery. Contact administrators.(Step : %{step})")
2142 // 21 : not yet used
2143 LSerror :: defineError('LSsession_22',
2144 _("LSsession : problem during initialisation.")
2149 * Ajax method when change ldapserver on login form
2151 * @param[in] $data array The return data address
2155 public static function ajax_onLdapServerChangedLogin(&$data) {
2156 if ( isset($_REQUEST['server']) ) {
2157 self :: setLdapServer($_REQUEST['server']);
2159 if ( self :: LSldapConnect() ) {
2161 if (isset($_SESSION['LSsession_topDn'])) {
2162 $sel = $_SESSION['LSsession_topDn'];
2167 $list = self :: getSubDnLdapServerOptions($sel);
2168 if (is_string($list)) {
2169 $data['list_topDn'] = "<select name='LSsession_topDn' id='LSsession_topDn'>".$list."</select>";
2170 $data['subDnLabel'] = self :: getSubDnLabel();
2173 $data['recoverPassword'] = isset(self :: $ldapServer['recoverPassword']);
2178 * Ajax method when change ldapserver on recoverPassword form
2180 * @param[in] $data array The return data address
2184 public static function ajax_onLdapServerChangedRecoverPassword(&$data) {
2185 if ( isset($_REQUEST['server']) ) {
2186 self :: setLdapServer($_REQUEST['server']);
2187 $data=array('recoverPassword' => isset(self :: $ldapServer['recoverPassword']));