Ldap.class.php

gehe zur Dokumentation dieser Datei
00001 <?php
00002 #
00003 #  DaCMS Content Management System
00004 #  Copyright (C) 2002 Jan Dankert, jandankert@jandankert.de
00005 #
00006 #  This program is free software; you can redistribute it and/or
00007 #  modify it under the terms of the GNU General Public License
00008 #  as published by the Free Software Foundation; either version 2
00009 #  of the License, or (at your option) any later version.
00010 #
00011 #  This program is distributed in the hope that it will be useful,
00012 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 #  GNU General Public License for more details.
00015 #
00016 #  You should have received a copy of the GNU General Public License
00017 #  along with this program; if not, write to the Free Software
00018 #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 #
00020 
00027 class Ldap
00028 {
00029      var $connection;
00030      var $timeout;
00031      var $aliases;
00032      
00033      
00037      function Ldap()
00038      {
00039           global $conf;
00040           
00041           $this->timeout = intval($conf['ldap']['search']['timeout']);
00042 
00043           if   ( $conf['ldap']['search']['aliases'] )
00044                $this->aliases = LDAP_DEREF_ALWAYS;
00045           else
00046                $this->aliases = LDAP_DEREF_NEVER;
00047      }
00048      
00049      
00050      
00054      function connect()
00055      {
00056           global $conf;
00057           
00058           $ldapHost = $conf['ldap']['host'];
00059           $ldapPort = $conf['ldap']['port'];
00060 
00061           // Verbindung zum LDAP-Server herstellen
00062           $this->connection = @ldap_connect( $ldapHost,$ldapPort );
00063           
00064           // Protokollversion setzen.
00065           $j = ldap_set_option( $this->connection, LDAP_OPT_PROTOCOL_VERSION,intval($conf['ldap']['protocol']) );
00066           if   ( ! $j )
00067                die( 'LDAP error while setting protocol version'.ldap_errno().'/'.ldap_error().')' );
00068           
00069           // siehe http://bugs.php.net/bug.php?id=15637
00070           // Unter bestimmten Bedingungen wird trotz nicht erreichbarem LDAP-Server eine PHP-Resource
00071           // zurueck gegeben. Dann erscheint zwar keine Fehlermeldung, aber zumindestens misslingt
00072           // der nachfolgende Bind-Befehl.
00073           if   ( !is_resource($this->connection) || $this->connection === false )
00074           {
00075                Logger::error( "connect to ldap server '$ldapHost:$ldapPort' failed" );
00076                // Abbruch, wenn LDAP-Server nicht erreichbar
00077                die( "Connection failed to $ldapHost:$ldapPort (".ldap_errno().'/'.ldap_error().'). Please contact your administrator.' );
00078           }
00079      }    
00080      
00081      
00082      
00086      function bind( $user,$pw )
00087      {
00088           return @ldap_bind( $this->connection,$user,$pw);
00089      }
00090      
00091      
00092      
00096      function bindAnonymous()
00097      {
00098           return @ldap_bind( $this->connection );
00099      }
00100      
00101      
00102      
00106      function unbind()
00107      {
00108           ldap_unbind( $this->connection );
00109      }
00110      
00111      
00112      
00116      function searchUser( $username )
00117      {
00118           global $conf;
00119           
00120           $techUser = $conf['ldap']['search']['user'];
00121           $techPass = $conf['ldap']['search']['password'];
00122           
00123           if   ( $conf['ldap']['search']['anonymous'] )
00124                $this->bindAnonymous();
00125           else
00126                $this->bind( $techUser, $techPass );
00127                                           
00128           $dn      = $conf['ldap']['search']['basedn'];
00129           $filter  = $conf['ldap']['search']['filter'];
00130           $filter  = str_replace('{user}', $username, $filter);
00131                
00132           
00133           $s = ldap_search( $this->connection,$dn,$filter,array(),0,1,$this->timeout,$this->aliases );
00134           $dn = ldap_get_dn($this->connection, ldap_first_entry($this->connection,$s) );
00135           
00136           return $dn;
00137      }
00138 
00139 
00140 
00144      function searchAttribute( $filter,$attr )
00145      {
00146           global $conf;
00147           
00148           $timeout = intval($conf['ldap']['search']['timeout']);
00149 
00150           if   ( $conf['ldap']['search']['aliases'] )
00151                $aliases = LDAP_DEREF_ALWAYS;
00152           else
00153                $aliases = LDAP_DEREF_NEVER;
00154                
00155           
00156           $base_dn = $conf['ldap']['search']['basedn'];
00157           $s = ldap_search( $this->connection,$base_dn,$filter,array(),0,0,$this->timeout,$this->aliases );
00158           $ergebnisse = ldap_get_entries($this->connection,$s);
00159           
00160           $liste = array();
00161 //        Html::debug($ergebnisse);
00162           for( $i=0; $i<=$ergebnisse['count']-1; $i++ )
00163                $liste[] = $ergebnisse[$i][$attr][0];
00164 
00165           return $liste;
00166      }
00167      
00168      
00169      
00173      function close()
00174      {
00175           // Verbindung zum LDAP-Server brav beenden
00176           ldap_close( $this->connection );
00177      }    
00178 }
00179 
00180 ?>

Erzeugt am Thu May 14 00:55:49 2009 für OpenRat von  doxygen 1.5.8