File modules/util/Ldap.class.php

Last commit: Thu Nov 19 14:53:31 2020 +0100	Jan Dankert	Using warn() instead of error()
1 <?php 2 // OpenRat Content Management System 3 // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de 4 // 5 // This program is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU General Public License 7 // as published by the Free Software Foundation; either version 2 8 // of the License, or (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with this program; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 19 namespace util; 20 use logger\Logger; 21 use LogicException; 22 use RuntimeException; 23 24 /** 25 * Bereitstellen von LDAP-Funktionen. 26 * @author $Author$ 27 * @version $Revision$ 28 * @package openrat.services 29 * @deprecated 30 */ 31 class Ldap 32 { 33 var $connection; 34 var $timeout; 35 var $aliases; 36 37 38 /** 39 * 40 */ 41 function Ldap() 42 { 43 $conf = \cms\base\Configuration::rawConfig(); 44 45 $this->timeout = intval($conf['ldap']['search']['timeout']); 46 47 if ($conf['ldap']['search']['aliases']) 48 $this->aliases = LDAP_DEREF_ALWAYS; 49 else 50 $this->aliases = LDAP_DEREF_NEVER; 51 } 52 53 54 /** 55 * Verbindung �ffnen. 56 */ 57 function connect() 58 { 59 $conf = \cms\base\Configuration::rawConfig(); 60 61 $ldapHost = $conf['ldap']['host']; 62 $ldapPort = $conf['ldap']['port']; 63 64 // Verbindung zum LDAP-Server herstellen 65 $this->connection = @ldap_connect($ldapHost, $ldapPort); 66 67 // siehe http://bugs.php.net/bug.php?id=15637 68 // Unter bestimmten Bedingungen wird trotz nicht erreichbarem LDAP-Server eine PHP-Resource 69 // zurueck gegeben. Dann erscheint zwar keine Fehlermeldung, aber zumindestens misslingt 70 // der nachfolgende Bind-Befehl. 71 if (!is_resource($this->connection) || $this->connection === false) { 72 Logger::warn("connect to ldap server '$ldapHost:$ldapPort' failed"); 73 // Abbruch, wenn LDAP-Server nicht erreichbar 74 throw new RuntimeException("Connection failed to $ldapHost:$ldapPort (" . ldap_errno() . '/' . ldap_error() . '). Please contact your administrator.'); 75 } 76 77 // Protokollversion setzen. 78 $j = ldap_set_option($this->connection, LDAP_OPT_PROTOCOL_VERSION, intval($conf['ldap']['protocol'])); 79 if (!$j) 80 throw new LogicException('LDAP error while setting protocol version' . ldap_errno() . '/' . ldap_error() . ')'); 81 82 } 83 84 85 /** 86 * Ein Binding auf den LDAP-Server durchf�hren. 87 */ 88 function bind($user, $pw) 89 { 90 return @ldap_bind($this->connection, $user, $pw); 91 } 92 93 94 /** 95 * Ein Binding auf den LDAP-Server durchf�hren. 96 */ 97 function bindAnonymous() 98 { 99 return @ldap_bind($this->connection); 100 } 101 102 103 /** 104 * Das Bindung wird entfernt. 105 */ 106 function unbind() 107 { 108 ldap_unbind($this->connection); 109 } 110 111 112 /** 113 * Eine Suche auf den LDAP-Server durchf�hren. 114 */ 115 function searchUser($username) 116 { 117 $conf = \cms\base\Configuration::rawConfig(); 118 119 $techUser = $conf['ldap']['search']['user']; 120 $techPass = $conf['ldap']['search']['password']; 121 122 if ($conf['ldap']['search']['anonymous']) 123 $this->bindAnonymous(); 124 else 125 $this->bind($techUser, $techPass); 126 127 $dn = $conf['ldap']['search']['basedn']; 128 $filter = $conf['ldap']['search']['filter']; 129 $filter = str_replace('{user}', $username, $filter); 130 131 $s = @ldap_search($this->connection, $dn, $filter, array(), 0, 1, $this->timeout, $this->aliases); 132 133 if (!is_resource($s)) 134 return null; 135 136 $dn = @ldap_get_dn($this->connection, ldap_first_entry($this->connection, $s)); 137 138 return $dn; 139 } 140 141 142 /** 143 * Ein Binding auf den LDAP-Server durchf�hren. 144 */ 145 function searchAttribute($filter, $attr) 146 { 147 $conf = \cms\base\Configuration::rawConfig(); 148 149 $timeout = intval($conf['ldap']['search']['timeout']); 150 151 if ($conf['ldap']['search']['aliases']) 152 $aliases = LDAP_DEREF_ALWAYS; 153 else 154 $aliases = LDAP_DEREF_NEVER; 155 156 157 $base_dn = $conf['ldap']['search']['basedn']; 158 $s = ldap_search($this->connection, $base_dn, $filter, array(), 0, 0, $this->timeout, $this->aliases); 159 $ergebnisse = ldap_get_entries($this->connection, $s); 160 161 $liste = array(); 162 // Html::debug($ergebnisse); 163 for ($i = 0; $i <= $ergebnisse['count'] - 1; $i++) 164 $liste[] = $ergebnisse[$i][$attr][0]; 165 166 return $liste; 167 } 168 169 170 /** 171 * Verbindung schlie�en. 172 */ 173 function close() 174 { 175 // Verbindung zum LDAP-Server brav beenden 176 ldap_close($this->connection); 177 } 178 } 179 180 ?>
Download modules/util/Ldap.class.php
History Thu, 19 Nov 2020 14:53:31 +0100 Jan Dankert Using warn() instead of error() Sat, 26 Sep 2020 10:32:02 +0200 Jan Dankert Refactoring: No global $conf array any more. Sun, 23 Feb 2020 00:03:40 +0100 Jan Dankert Refactoring: Namespaces for modules 'logger' and 'language' Sat, 22 Feb 2020 23:58:02 +0100 Jan Dankert Refactoring: Namespacing for module 'util'. Wed, 12 Dec 2018 23:05:08 +0100 Jan Dankert die() grundsätzlich nicht verwenden, sondern stattdessen Exceptions werfen. Sat, 16 Dec 2017 23:21:31 +0100 Jan Dankert Eigenes Modul für alle Util-Klassen.