@@ -59,7 +59,7 @@ public function __construct()
5959 }
6060
6161 // Thanks to: https://www.php.net/manual/de/function.ldap-connect.php#115662
62- private function serviceping ($ host , $ port = 389 , $ timeout = 3 )
62+ private function serviceping ($ host , $ port = 389 , $ timeout = 5 )
6363 {
6464 if ($ port === null ) {
6565 $ port = 389 ;
@@ -70,7 +70,7 @@ private function serviceping($host, $port = 389, $timeout = 3)
7070 try {
7171 $ op = fsockopen ($ host , $ port , $ errno , $ errstr , $ timeout );
7272 } catch (ErrorException $ e ) {
73- Yii::error ('fsockopen failure! ' );
73+ Yii::error ('fsockopen failure! ' , __METHOD__ );
7474 return false ;
7575 }
7676 if (!$ op ) return false ; //DC is N/A
@@ -119,13 +119,13 @@ public function autoDetect($overrideIp = false)
119119 public function login ($ username , $ password , $ domainKey )
120120 {
121121
122- Yii::debug ('Hello! :) Trying to log you in via LDAP! ' );
122+ Yii::debug ('Hello! :) Trying to log you in via LDAP! ' , __METHOD__ );
123123
124124
125125 $ domainData = $ this ->domains [$ domainKey ];
126126
127127 $ ssl = isset ($ domainData ['useSSL ' ]) && $ domainData ['useSSL ' ];
128- Yii::debug ('Use SSL here? ' . ($ ssl ? 'Yes ' : 'No ' ));
128+ Yii::debug ('Use SSL here? ' . ($ ssl ? 'Yes ' : 'No ' ), __METHOD__ );
129129
130130 if ($ ssl ) {
131131 // When using SSL, we have to set some env variables and create an ldap controlfile - otherwirse a connect with non valid certificat will fail!
@@ -140,22 +140,22 @@ public function login($username, $password, $domainKey)
140140 if (!file_exists ($ ldaprcfile )) {
141141 // Try to create the file
142142 if (!@file_put_contents ($ ldaprcfile , 'TLS_REQCERT allow ' )) {
143- Yii::error ('Cannot create required .ldaprc control file! ' );
143+ Yii::error ('Cannot create required .ldaprc control file! ' , __METHOD__ );
144144 return false ;
145145 }
146146 } else {
147- Yii::debug ('.ldaprc file exists! ' );
147+ Yii::debug ('.ldaprc file exists! ' , __METHOD__ );
148148 }
149149
150150 putenv ('LDAPCONF= ' . $ ldaprcfile );
151151 putenv ('LDAPTLS_REQCERT=allow ' );
152152 putenv ('TLS_REQCERT=allow ' );
153153 }
154154
155- Yii::debug ('Trying to connect to Domain # ' . $ domainKey . ' ( ' . $ domainData ['hostname ' ] . ') ' );
155+ Yii::debug ('Trying to connect to Domain # ' . $ domainKey . ' ( ' . $ domainData ['hostname ' ] . ') ' , __METHOD__ );
156156
157157 if (!self ::serviceping ($ domainData ['hostname ' ], $ ssl ? 636 : null )) {
158- Yii::error ('Connection failed! ' );
158+ Yii::error ('Connection failed! ' , __METHOD__ );
159159 return false ;
160160 }
161161
@@ -166,7 +166,7 @@ public function login($username, $password, $domainKey)
166166
167167 $ l = @ldap_connect ($ hostPrefix , $ port );
168168 if (!$ l ) {
169- Yii::warning ('Connect failed! ' . ldap_error ($ l ), ' ldapAuth ' );
169+ Yii::warning ('Connect failed! ' . ldap_error ($ l ), __METHOD__ );
170170 return false ;
171171 }
172172
@@ -176,12 +176,12 @@ public function login($username, $password, $domainKey)
176176
177177 $ bind_dn = strpos ($ username , '@ ' ) === false ? $ username . '@ ' . $ domainData ['name ' ] : $ username ;
178178
179- Yii::debug ('Trying to authenticate with DN ' . $ bind_dn );
179+ Yii::debug ('Trying to authenticate with DN ' . $ bind_dn, __METHOD__ );
180180
181181 $ b = @ldap_bind ($ l , $ bind_dn , $ password );
182182
183183 if (!$ b ) {
184- Yii::warning ('Bind failed! ' . ldap_error ($ l ), ' ldapAuth ' );
184+ Yii::warning ('Bind failed! ' . ldap_error ($ l ), __METHOD__ );
185185 return false ;
186186 }
187187
@@ -261,14 +261,18 @@ public function searchUser($searchFor, $attributes = "", $searchFilter = "", $au
261261
262262 $ return = [];
263263 foreach ($ domains as $ domain ) {
264- Yii::debug ($ domain , ' ldapAuth ' );
264+ Yii::debug ($ domain , __METHOD__ );
265265 if (!$ this ->login ($ domain ['publicSearchUser ' ], $ domain ['publicSearchUserPassword ' ], $ i )) {
266- throw new ErrorException ('LDAP Connect or Bind error ( ' . ldap_errno ($ this ->_l ) . ' - ' . ldap_error ($ this ->_l ) . ') on ' . $ domain ['hostname ' ]);
266+ if (empty ($ this ->_l )) {
267+ throw new ErrorException ('LDAP Connect or Bind error on ' . $ domain ['hostname ' ]);
268+ } else {
269+ throw new ErrorException ('LDAP Connect or Bind error ( ' . ldap_errno ($ this ->_l ) . ' - ' . ldap_error ($ this ->_l ) . ') on ' . $ domain ['hostname ' ]);
270+ }
267271 }
268272
269273 $ searchFilter = str_replace ("%searchFor% " , addslashes ($ searchFor ), $ searchFilter );
270274
271- Yii::debug ('Search-Filter: ' . $ searchFilter );
275+ Yii::debug ('Search-Filter: ' . $ searchFilter, __METHOD__ );
272276
273277 $ result = ldap_search ($ this ->_l , $ this ->_ldapBaseDn , $ searchFilter , $ attributes );
274278
@@ -279,7 +283,7 @@ public function searchUser($searchFor, $attributes = "", $searchFilter = "", $au
279283 continue ;
280284 }
281285 if (!isset ($ entry ['objectsid ' ])) {
282- Yii::warning ('No objectsid! ignoring! ' );
286+ Yii::warning ('No objectsid! ignoring! ' , __METHOD__ );
283287 continue ;
284288 }
285289 $ sid = self ::SIDtoString ($ entry ['objectsid ' ])[0 ];
@@ -290,15 +294,15 @@ public function searchUser($searchFor, $attributes = "", $searchFilter = "", $au
290294 // Check if this user is maybe already listed in the results - ifo so, determine which one is newer
291295 foreach ($ return as $ _sid => $ _data ) {
292296 if (!empty ($ _data ['sidhistory ' ]) && in_array ($ sid , $ _data ['sidhistory ' ])) {
293- Yii::debug ('This user is listed in another users history - skipping ' );
297+ Yii::debug ('This user is listed in another users history - skipping ' , __METHOD__ );
294298 continue 2 ;
295299 }
296300 }
297301
298302 if ($ sidHistory ) {
299303 foreach ($ sidHistory as $ item ) {
300304 if (array_key_exists ($ item , $ return )) {
301- Yii::debug ('User already exists with its sidhistory in results! Unsetting the old entry... ' );
305+ Yii::debug ('User already exists with its sidhistory in results! Unsetting the old entry... ' , __METHOD__ );
302306 unset($ return [$ item ]);
303307 }
304308 }
@@ -315,6 +319,10 @@ public function searchUser($searchFor, $attributes = "", $searchFilter = "", $au
315319 }
316320 }
317321 $ i ++;
322+
323+ // Reset LDAP Link
324+ ldap_close ($ this ->_l );
325+ $ this ->_l = null ;
318326 }
319327
320328 return empty ($ return ) ? [] : $ return ;
0 commit comments