Source for file Authentification.php

Documentation is available at Authentification.php

  1. <?php
  2. /*-----------------------------------------------------------------------                             
  3.                              modules : Authentification
  4.                              projet : Ldap2Mysql-Authentification
  5.                              CHEFS DE PROJETS : P.BAFFALIE, S.GRESSARD
  6.  
  7.  Description :      gestion du mode d'authentifications externes         
  8.                              -
  9.  Environnement PHP   : PHP4 OU PHP5
  10.  author                    : nordine.zetoutou<nzetoutou@educagri.fr>
  11.  date de création     : 25 sept. 06 
  12.  Historique de modification :
  13.                              -
  14.                              -
  15.  version                   :    1.0
  16. -----------------------------------------------------------------------*/
  17. /**
  18.    * @author Nordine Zetoutou  - <nzetoutou@educagri.fr>
  19.    * @package Ldap2Mysql-Authentification
  20.    * @module Authentification
  21.    * @description gestion des bases d'authentification mysql, ldap et cas
  22.    * @dependance  activation de l'extension php_ldap
  23.    * @dependance  si CAS comme serveur d'authentification activation  de l'extension=php_curl.dll
  24.    * 
  25.    * @license GPL
  26.    * @version 1.0
  27.    * @date :  25 sept. 2006
  28.    
  29. */
  30. {
  31.     var $sTableLogin = 'user_id';
  32.     //
  33.         function Authentification()
  34.     {
  35.         define('CONFIG_DIR''Ldap2Mysql/config');
  36.     }
  37.     //
  38.         /**
  39.      * @description charge le mode d'authentification extrene
  40.      * @return mixed object|bool $oBase ou false
  41.      */
  42.     function getAuthMode()
  43.     {
  44.         //
  45.         if (file_exists(CONFIG_DIR))
  46.         {
  47.             //baseAuth est le nom du fichier du mode d'authentification
  48.             $fFile CONFIG_DIR '/baseAuth';
  49.             if (file_exists($fFile))
  50.             {
  51.                 $sFileContent file_get_contents($fFile);
  52.                 $oBase unserialize($sFileContent);
  53.                 return $oBase;
  54.             }
  55.         }
  56.         else
  57.         {
  58.             $sMessage "<p style='color:red;font-weight:bold;'> ************* </br>";
  59.             $sMessage .= "le répertoire de configuration des authentifications externes ne peut être atteint";
  60.             $sMessage .= "</br>*************</p>";
  61.             echo $sMessage;
  62.         }
  63.     }
  64.     //
  65.         /**
  66.      * @description authentifie un utilisateur dans l'annuaire ldap
  67.      * @param object $oBase : config du serveur d'authentification
  68.      * @param string $login 
  69.      * @param string $pass 
  70.      * 
  71.      * @return object $oUser
  72.      */
  73.     function ldap_authenticate($oBase$login$pass)
  74.     {
  75.         //
  76.         $ressource_link ldap_connect($oBase->host$oBase->port);
  77.         //
  78.         $bLdap_bind ldap_bind($ressource_link$login$pass);
  79.         //
  80.         $bLdap_bind true;
  81.         ///
  82.         if ($bLdap_bind)
  83.         {
  84.             //
  85.             $result ldap_search($ressource_link$oBase->dn'(' $oBase->login_field '=' $login ')');
  86.             $aEntries ldap_get_entries($ressource_link$result);
  87.             //si une entré existe alors l'authentification est ok
  88.             if ($aEntries["count"== (int) 1)
  89.             {
  90.                 $entry ldap_first_entry($ressource_link$result);
  91.                 $attrs ldap_get_attributes($ressource_link$entry);
  92.                 //on construit un objet $oUser
  93.                 $oUser new stdClass();
  94.                 //
  95.                 $aKeys array_keys($attrs);
  96.                 foreach ($aKeys as $value)
  97.                 {
  98.                     //
  99.                     if (!is_int($value))
  100.                     {
  101.                         //les clés attributs ldap contiennent des majuscules
  102.                         $propriete strtolower($value);
  103.                         //
  104.                         $oUser-> $propriete $attrs[$value][0];
  105.                         //on récupère le nom du champ contenant l'id ldap
  106.                         if ($propriete == $oBase->ldap_user_id_field)
  107.                         {
  108.                             //on récupère la valeur de ce champ
  109.                             $oUser->ldap_user_id $attrs[$value][0];
  110.                         }
  111.                     }
  112.                 }
  113.                 return $oUser;
  114.             }
  115.             else
  116.             {
  117.                 return 'notExist';
  118.             }
  119.             //
  120.             ldap_free_result($result);
  121.             //
  122.         }
  123.         else
  124.         {
  125.             return 'noConnexion';
  126.         }
  127.     }
  128.     //
  129.         /**
  130.      * @description récupère un utilisateur de la base mysql
  131.      * @param object $oUser : possédant entre autres son id ldap
  132.      * 
  133.      *  return object $oUser
  134.      */
  135.     function mysql_authenticate($oMysql$ldap_user_id)
  136.     {
  137.         $ressource_link mysql_connect($oMysql->host$oMysql->root$oMysql->port);
  138.         //
  139.         if ($ressource_link)
  140.         {
  141.             mysql_select_db($oMysql->db);
  142.             $sQuery 'SELECT * FROM ' $this->sTableLogin;
  143.             $sQuery .= " WHERE ldap_user_id='" $ldap_user_id "'";
  144.             $result mysql_query($sQuery);
  145.             //
  146.             if ($result)
  147.             {
  148.                 //on ne récupère qu'un objet
  149.                 return mysql_fetch_object($result);
  150.             //fin if
  151.             //
  152.             //
  153.         //fin if
  154.         //
  155.     }
  156. }
  157. ?>

Documentation generated on Wed, 22 Nov 2006 11:41:03 +0100 by phpDocumentor 1.3.0RC6