home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / class / auth / auth_ldap.php < prev    next >
Encoding:
PHP Script  |  2007-09-09  |  8.6 KB  |  206 lines

  1. <?php
  2. // $Id: auth_ldap.php 1029 2007-09-09 03:49:25Z phppp $
  3. // auth_ldap.php - LDAP authentification class
  4. //  ------------------------------------------------------------------------ //
  5. //                XOOPS - PHP Content Management System                      //
  6. //                    Copyright (c) 2000 XOOPS.org                           //
  7. //                       <http://www.xoops.org/>                             //
  8. //  ------------------------------------------------------------------------ //
  9. //  This program is free software; you can redistribute it and/or modify     //
  10. //  it under the terms of the GNU General Public License as published by     //
  11. //  the Free Software Foundation; either version 2 of the License, or        //
  12. //  (at your option) any later version.                                      //
  13. //                                                                           //
  14. //  You may not change or alter any portion of this comment or credits       //
  15. //  of supporting developers from this source code or any supporting         //
  16. //  source code which is considered copyrighted (c) material of the          //
  17. //  original comment or credit authors.                                      //
  18. //                                                                           //
  19. //  This program is distributed in the hope that it will be useful,          //
  20. //  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
  21. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
  22. //  GNU General Public License for more details.                             //
  23. //                                                                           //
  24. //  You should have received a copy of the GNU General Public License        //
  25. //  along with this program; if not, write to the Free Software              //
  26. //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
  27. //  ------------------------------------------------------------------------ //
  28. /**
  29.  * @package     kernel
  30.  * @subpackage  auth
  31.  * @description    Authentification class for standard LDAP Server V2 or V3
  32.  * @author        Pierre-Eric MENUET    <pemphp@free.fr>
  33.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  34.  */
  35. include_once XOOPS_ROOT_PATH . '/class/auth/auth_provisionning.php';
  36.  
  37. class XoopsAuthLdap extends XoopsAuth {
  38.  
  39. var $cp1252_map = array (
  40.     "\xc2\x80" => "\xe2\x82\xac", /* EURO SIGN */
  41.     "\xc2\x82" => "\xe2\x80\x9a", /* SINGLE LOW-9 QUOTATION MARK */
  42.     "\xc2\x83" => "\xc6\x92",     /* LATIN SMALL LETTER F WITH HOOK */
  43.     "\xc2\x84" => "\xe2\x80\x9e", /* DOUBLE LOW-9 QUOTATION MARK */
  44.     "\xc2\x85" => "\xe2\x80\xa6", /* HORIZONTAL ELLIPSIS */
  45.     "\xc2\x86" => "\xe2\x80\xa0", /* DAGGER */
  46.     "\xc2\x87" => "\xe2\x80\xa1", /* DOUBLE DAGGER */
  47.     "\xc2\x88" => "\xcb\x86",     /* MODIFIER LETTER CIRCUMFLEX ACCENT */
  48.     "\xc2\x89" => "\xe2\x80\xb0", /* PER MILLE SIGN */
  49.     "\xc2\x8a" => "\xc5\xa0",     /* LATIN CAPITAL LETTER S WITH CARON */
  50.     "\xc2\x8b" => "\xe2\x80\xb9", /* SINGLE LEFT-POINTING ANGLE QUOTATION */
  51.     "\xc2\x8c" => "\xc5\x92",     /* LATIN CAPITAL LIGATURE OE */
  52.     "\xc2\x8e" => "\xc5\xbd",     /* LATIN CAPITAL LETTER Z WITH CARON */
  53.     "\xc2\x91" => "\xe2\x80\x98", /* LEFT SINGLE QUOTATION MARK */
  54.     "\xc2\x92" => "\xe2\x80\x99", /* RIGHT SINGLE QUOTATION MARK */
  55.     "\xc2\x93" => "\xe2\x80\x9c", /* LEFT DOUBLE QUOTATION MARK */
  56.     "\xc2\x94" => "\xe2\x80\x9d", /* RIGHT DOUBLE QUOTATION MARK */
  57.     "\xc2\x95" => "\xe2\x80\xa2", /* BULLET */
  58.     "\xc2\x96" => "\xe2\x80\x93", /* EN DASH */
  59.     "\xc2\x97" => "\xe2\x80\x94", /* EM DASH */
  60.     "\xc2\x98" => "\xcb\x9c",     /* SMALL TILDE */
  61.     "\xc2\x99" => "\xe2\x84\xa2", /* TRADE MARK SIGN */
  62.     "\xc2\x9a" => "\xc5\xa1",     /* LATIN SMALL LETTER S WITH CARON */
  63.     "\xc2\x9b" => "\xe2\x80\xba", /* SINGLE RIGHT-POINTING ANGLE QUOTATION*/
  64.     "\xc2\x9c" => "\xc5\x93",     /* LATIN SMALL LIGATURE OE */
  65.     "\xc2\x9e" => "\xc5\xbe",     /* LATIN SMALL LETTER Z WITH CARON */
  66.     "\xc2\x9f" => "\xc5\xb8"      /* LATIN CAPITAL LETTER Y WITH DIAERESIS*/        
  67.     );
  68.     
  69.     var $ldap_server;
  70.     var $ldap_port = '389';
  71.     var $ldap_version = '3';
  72.     var $ldap_base_dn;
  73.     var $ldap_loginname_asdn;
  74.     var $ldap_loginldap_attr;
  75.     var $ldap_mail_attr;
  76.     var $ldap_name_attr;
  77.     var $ldap_surname_attr;
  78.     var $ldap_givenname_attr;
  79.     var $ldap_manager_dn;
  80.     var $ldap_manager_pass;
  81.     var $_ds;
  82.  
  83.     /**
  84.      * Authentication Service constructor
  85.      */
  86.     function XoopsAuthLdap (&$dao) {
  87.         $this->_dao = $dao;
  88.         //The config handler object allows us to look at the configuration options that are stored in the database
  89.         $config_handler =& xoops_gethandler('config');    
  90.         $config =& $config_handler->getConfigsByCat(XOOPS_CONF_AUTH);
  91.         $confcount = count($config);
  92.         foreach ($config as $key => $val) {
  93.             $this->$key = $val;
  94.         }    
  95.     }
  96.  
  97.  
  98.     function cp1252_to_utf8($str) {
  99.             return  strtr(utf8_encode($str), $this->cp1252_map);
  100.     }
  101.  
  102.     /**
  103.      *  Authenticate  user again LDAP directory (Bind)
  104.      *  2 options : 
  105.      *         Authenticate directly with uname in the DN
  106.      *         Authenticate with manager, search the dn
  107.      *
  108.      * @param string $uname Username
  109.      * @param string $pwd Password
  110.      *
  111.      * @return bool
  112.      */    
  113.     function authenticate($uname, $pwd = null) {
  114.         $authenticated = false;
  115.         if (!extension_loaded('ldap')) {
  116.             $this->setErrors(0, _AUTH_LDAP_EXTENSION_NOT_LOAD);
  117.             return $authenticated;
  118.         }
  119.         $this->_ds = ldap_connect($this->ldap_server, $this->ldap_port);
  120.         if ($this->_ds) {
  121.             ldap_set_option($this->_ds, LDAP_OPT_PROTOCOL_VERSION, $this->ldap_version);
  122.             if ($this->ldap_use_TLS) { // We use TLS secure connection
  123.                    if (!ldap_start_tls($this->_ds))
  124.                     $this->setErrors(0, _AUTH_LDAP_START_TLS_FAILED);
  125.             }
  126.             // If the uid is not in the DN we proceed to a search
  127.             // The uid is not always in the dn
  128.             $userDN = $this->getUserDN($uname);
  129.             if (!$userDN) return false;
  130.             // We bind as user to test the credentials   
  131.             $authenticated = ldap_bind($this->_ds, $userDN, stripslashes($pwd));
  132.             if ($authenticated) {
  133.                 // We load the Xoops User database
  134.                 return $this->loadXoopsUser($userDN, $uname, $pwd);                               
  135.             } else $this->setErrors(ldap_errno($this->_ds), ldap_err2str(ldap_errno($this->_ds)) . '(' . $userDN . ')');
  136.         }
  137.         else {
  138.             $this->setErrors(0, _AUTH_LDAP_SERVER_NOT_FOUND);            
  139.         }
  140.         @ldap_close($this->_ds);
  141.         return $authenticated;
  142.     }
  143.     
  144.     
  145.     /**
  146.      *  Compose the user DN with the configuration.
  147.      * 
  148.      *
  149.      * @return userDN or false
  150.      */        
  151.     function getUserDN($uname) {
  152.         $userDN = false;
  153.         if (!$this->ldap_loginname_asdn) {
  154.             // Bind with the manager
  155.             if (!ldap_bind($this->_ds, $this->ldap_manager_dn, stripslashes($this->ldap_manager_pass))) {
  156.                  $this->setErrors(ldap_errno($this->_ds), ldap_err2str(ldap_errno($this->_ds)) . '(' . $this->ldap_manager_dn . ')');
  157.                  return false;
  158.             }
  159.             $filter = $this->getFilter($uname);
  160.             $sr = ldap_search($this->_ds, $this->ldap_base_dn, $filter);
  161.             $info = ldap_get_entries($this->_ds, $sr);
  162.             if ($info["count"] > 0) {
  163.                 $userDN = $info[0]['dn'];
  164.             } else $this->setErrors(0, sprintf(_AUTH_LDAP_USER_NOT_FOUND, $uname, $filter, $this->ldap_base_dn));            
  165.         }
  166.         else {
  167.             $userDN = $this->ldap_loginldap_attr."=".$uname.",".$this->ldap_base_dn;
  168.         }
  169.         return $userDN;
  170.     }
  171.  
  172.  
  173.     /**
  174.      *  Load user from XOOPS Database
  175.      * 
  176.      * @return XoopsUser object
  177.      */            
  178.      function getFilter($uname) {
  179.          $filter = '';
  180.          if ($this->ldap_filter_person != '') {
  181.             $filter = str_replace('@@loginname@@',$uname, $this->ldap_filter_person);                 
  182.          }    
  183.          else {
  184.              $filter = $this->ldap_loginldap_attr . "=" . $uname;
  185.          }     
  186.          return $filter;
  187.      } 
  188.  
  189.               
  190.     function loadXoopsUser($userdn, $uname, $pwd = null) {
  191.         $provisHandler = XoopsAuthProvisionning::getInstance($this);
  192.         $sr = ldap_read($this->_ds, $userdn, '(objectclass=*)');
  193.         $entries = ldap_get_entries($this->_ds, $sr);        
  194.         if ($entries["count"] > 0) {
  195.             $xoopsUser = $provisHandler->sync($entries[0], $uname, $pwd);
  196.         }
  197.         else $this->setErrors(0, sprintf('loadXoopsUser - ' . _AUTH_LDAP_CANT_READ_ENTRY, $userdn));
  198.         return $xoopsUser;
  199.     }
  200.  
  201.     
  202. } // end class
  203.  
  204.  
  205. ?>
  206.