Completed
Push — stable13 ( 30dea9...761dae )
by Morris
36:15 queued 15:31
created
apps/user_ldap/lib/Connection.php 1 patch
Spacing   +63 added lines, -63 removed lines patch added patch discarded remove patch
@@ -105,7 +105,7 @@  discard block
 block discarded – undo
105 105
 		$this->configuration = new Configuration($configPrefix,
106 106
 												 !is_null($configID));
107 107
 		$memcache = \OC::$server->getMemCacheFactory();
108
-		if($memcache->isAvailable()) {
108
+		if ($memcache->isAvailable()) {
109 109
 			$this->cache = $memcache->createDistributed();
110 110
 		}
111 111
 		$helper = new Helper(\OC::$server->getConfig());
@@ -117,7 +117,7 @@  discard block
 block discarded – undo
117 117
 	}
118 118
 
119 119
 	public function __destruct() {
120
-		if(!$this->dontDestruct && $this->ldap->isResource($this->ldapConnectionRes)) {
120
+		if (!$this->dontDestruct && $this->ldap->isResource($this->ldapConnectionRes)) {
121 121
 			@$this->ldap->unbind($this->ldapConnectionRes);
122 122
 		}
123 123
 		$this->bindResult = [];
@@ -129,7 +129,7 @@  discard block
 block discarded – undo
129 129
 	public function __clone() {
130 130
 		$this->configuration = new Configuration($this->configPrefix,
131 131
 												 !is_null($this->configID));
132
-		if(count($this->bindResult) !== 0 && $this->bindResult['result'] === true) {
132
+		if (count($this->bindResult) !== 0 && $this->bindResult['result'] === true) {
133 133
 			$this->bindResult = [];
134 134
 		}
135 135
 		$this->ldapConnectionRes = null;
@@ -141,11 +141,11 @@  discard block
 block discarded – undo
141 141
 	 * @return bool|mixed
142 142
 	 */
143 143
 	public function __get($name) {
144
-		if(!$this->configured) {
144
+		if (!$this->configured) {
145 145
 			$this->readConfiguration();
146 146
 		}
147 147
 
148
-		if($name === 'hasPagedResultSupport') {
148
+		if ($name === 'hasPagedResultSupport') {
149 149
 			return $this->hasPagedResultSupport;
150 150
 		}
151 151
 
@@ -161,7 +161,7 @@  discard block
 block discarded – undo
161 161
 		$before = $this->configuration->$name;
162 162
 		$this->configuration->$name = $value;
163 163
 		$after = $this->configuration->$name;
164
-		if($before !== $after) {
164
+		if ($before !== $after) {
165 165
 			if ($this->configID !== '' && $this->configID !== null) {
166 166
 				$this->configuration->saveConfiguration();
167 167
 			}
@@ -185,7 +185,7 @@  discard block
 block discarded – undo
185 185
 	 * @param bool $state
186 186
 	 */
187 187
 	public function setIgnoreValidation($state) {
188
-		$this->ignoreValidation = (bool)$state;
188
+		$this->ignoreValidation = (bool) $state;
189 189
 	}
190 190
 
191 191
 	/**
@@ -201,14 +201,14 @@  discard block
 block discarded – undo
201 201
 	 * Returns the LDAP handler
202 202
 	 */
203 203
 	public function getConnectionResource() {
204
-		if(!$this->ldapConnectionRes) {
204
+		if (!$this->ldapConnectionRes) {
205 205
 			$this->init();
206
-		} else if(!$this->ldap->isResource($this->ldapConnectionRes)) {
206
+		} else if (!$this->ldap->isResource($this->ldapConnectionRes)) {
207 207
 			$this->ldapConnectionRes = null;
208 208
 			$this->establishConnection();
209 209
 		}
210
-		if(is_null($this->ldapConnectionRes)) {
211
-			\OCP\Util::writeLog('user_ldap', 'No LDAP Connection to server ' . $this->configuration->ldapHost, \OCP\Util::ERROR);
210
+		if (is_null($this->ldapConnectionRes)) {
211
+			\OCP\Util::writeLog('user_ldap', 'No LDAP Connection to server '.$this->configuration->ldapHost, \OCP\Util::ERROR);
212 212
 			throw new ServerNotAvailableException('Connection to LDAP server could not be established');
213 213
 		}
214 214
 		return $this->ldapConnectionRes;
@@ -218,7 +218,7 @@  discard block
 block discarded – undo
218 218
 	 * resets the connection resource
219 219
 	 */
220 220
 	public function resetConnectionResource() {
221
-		if(!is_null($this->ldapConnectionRes)) {
221
+		if (!is_null($this->ldapConnectionRes)) {
222 222
 			@$this->ldap->unbind($this->ldapConnectionRes);
223 223
 			$this->ldapConnectionRes = null;
224 224
 			$this->bindResult = [];
@@ -231,7 +231,7 @@  discard block
 block discarded – undo
231 231
 	 */
232 232
 	private function getCacheKey($key) {
233 233
 		$prefix = 'LDAP-'.$this->configID.'-'.$this->configPrefix.'-';
234
-		if(is_null($key)) {
234
+		if (is_null($key)) {
235 235
 			return $prefix;
236 236
 		}
237 237
 		return $prefix.hash('sha256', $key);
@@ -242,10 +242,10 @@  discard block
 block discarded – undo
242 242
 	 * @return mixed|null
243 243
 	 */
244 244
 	public function getFromCache($key) {
245
-		if(!$this->configured) {
245
+		if (!$this->configured) {
246 246
 			$this->readConfiguration();
247 247
 		}
248
-		if(is_null($this->cache) || !$this->configuration->ldapCacheTTL) {
248
+		if (is_null($this->cache) || !$this->configuration->ldapCacheTTL) {
249 249
 			return null;
250 250
 		}
251 251
 		$key = $this->getCacheKey($key);
@@ -260,10 +260,10 @@  discard block
 block discarded – undo
260 260
 	 * @return string
261 261
 	 */
262 262
 	public function writeToCache($key, $value) {
263
-		if(!$this->configured) {
263
+		if (!$this->configured) {
264 264
 			$this->readConfiguration();
265 265
 		}
266
-		if(is_null($this->cache)
266
+		if (is_null($this->cache)
267 267
 			|| !$this->configuration->ldapCacheTTL
268 268
 			|| !$this->configuration->ldapConfigurationActive) {
269 269
 			return null;
@@ -274,7 +274,7 @@  discard block
 block discarded – undo
274 274
 	}
275 275
 
276 276
 	public function clearCache() {
277
-		if(!is_null($this->cache)) {
277
+		if (!is_null($this->cache)) {
278 278
 			$this->cache->clear($this->getCacheKey(null));
279 279
 		}
280 280
 	}
@@ -286,7 +286,7 @@  discard block
 block discarded – undo
286 286
 	 * @return null
287 287
 	 */
288 288
 	private function readConfiguration($force = false) {
289
-		if((!$this->configured || $force) && !is_null($this->configID)) {
289
+		if ((!$this->configured || $force) && !is_null($this->configID)) {
290 290
 			$this->configuration->readConfiguration();
291 291
 			$this->configured = $this->validateConfiguration();
292 292
 		}
@@ -299,12 +299,12 @@  discard block
 block discarded – undo
299 299
 	 * @return boolean true if config validates, false otherwise. Check with $setParameters for detailed success on single parameters
300 300
 	 */
301 301
 	public function setConfiguration($config, &$setParameters = null) {
302
-		if(is_null($setParameters)) {
302
+		if (is_null($setParameters)) {
303 303
 			$setParameters = array();
304 304
 		}
305 305
 		$this->doNotValidate = false;
306 306
 		$this->configuration->setConfiguration($config, $setParameters);
307
-		if(count($setParameters) > 0) {
307
+		if (count($setParameters) > 0) {
308 308
 			$this->configured = $this->validateConfiguration();
309 309
 		}
310 310
 
@@ -331,10 +331,10 @@  discard block
 block discarded – undo
331 331
 		$config = $this->configuration->getConfiguration();
332 332
 		$cta = $this->configuration->getConfigTranslationArray();
333 333
 		$result = array();
334
-		foreach($cta as $dbkey => $configkey) {
335
-			switch($configkey) {
334
+		foreach ($cta as $dbkey => $configkey) {
335
+			switch ($configkey) {
336 336
 				case 'homeFolderNamingRule':
337
-					if(strpos($config[$configkey], 'attr:') === 0) {
337
+					if (strpos($config[$configkey], 'attr:') === 0) {
338 338
 						$result[$dbkey] = substr($config[$configkey], 5);
339 339
 					} else {
340 340
 						$result[$dbkey] = '';
@@ -345,7 +345,7 @@  discard block
 block discarded – undo
345 345
 				case 'ldapBaseGroups':
346 346
 				case 'ldapAttributesForUserSearch':
347 347
 				case 'ldapAttributesForGroupSearch':
348
-					if(is_array($config[$configkey])) {
348
+					if (is_array($config[$configkey])) {
349 349
 						$result[$dbkey] = implode("\n", $config[$configkey]);
350 350
 						break;
351 351
 					} //else follows default
@@ -358,23 +358,23 @@  discard block
 block discarded – undo
358 358
 
359 359
 	private function doSoftValidation() {
360 360
 		//if User or Group Base are not set, take over Base DN setting
361
-		foreach(array('ldapBaseUsers', 'ldapBaseGroups') as $keyBase) {
361
+		foreach (array('ldapBaseUsers', 'ldapBaseGroups') as $keyBase) {
362 362
 			$val = $this->configuration->$keyBase;
363
-			if(empty($val)) {
363
+			if (empty($val)) {
364 364
 				$this->configuration->$keyBase = $this->configuration->ldapBase;
365 365
 			}
366 366
 		}
367 367
 
368
-		foreach(array('ldapExpertUUIDUserAttr'  => 'ldapUuidUserAttribute',
368
+		foreach (array('ldapExpertUUIDUserAttr'  => 'ldapUuidUserAttribute',
369 369
 					  'ldapExpertUUIDGroupAttr' => 'ldapUuidGroupAttribute')
370 370
 				as $expertSetting => $effectiveSetting) {
371 371
 			$uuidOverride = $this->configuration->$expertSetting;
372
-			if(!empty($uuidOverride)) {
372
+			if (!empty($uuidOverride)) {
373 373
 				$this->configuration->$effectiveSetting = $uuidOverride;
374 374
 			} else {
375 375
 				$uuidAttributes = Access::UUID_ATTRIBUTES;
376 376
 				array_unshift($uuidAttributes, 'auto');
377
-				if(!in_array($this->configuration->$effectiveSetting,
377
+				if (!in_array($this->configuration->$effectiveSetting,
378 378
 							$uuidAttributes)
379 379
 					&& (!is_null($this->configID))) {
380 380
 					$this->configuration->$effectiveSetting = 'auto';
@@ -396,14 +396,14 @@  discard block
 block discarded – undo
396 396
 		//make sure empty search attributes are saved as simple, empty array
397 397
 		$saKeys = array('ldapAttributesForUserSearch',
398 398
 						'ldapAttributesForGroupSearch');
399
-		foreach($saKeys as $key) {
399
+		foreach ($saKeys as $key) {
400 400
 			$val = $this->configuration->$key;
401
-			if(is_array($val) && count($val) === 1 && empty($val[0])) {
401
+			if (is_array($val) && count($val) === 1 && empty($val[0])) {
402 402
 				$this->configuration->$key = array();
403 403
 			}
404 404
 		}
405 405
 
406
-		if((stripos($this->configuration->ldapHost, 'ldaps://') === 0)
406
+		if ((stripos($this->configuration->ldapHost, 'ldaps://') === 0)
407 407
 			&& $this->configuration->ldapTLS) {
408 408
 			$this->configuration->ldapTLS = false;
409 409
 			\OCP\Util::writeLog('user_ldap',
@@ -424,10 +424,10 @@  discard block
 block discarded – undo
424 424
 		//options that shall not be empty
425 425
 		$options = array('ldapHost', 'ldapPort', 'ldapUserDisplayName',
426 426
 						 'ldapGroupDisplayName', 'ldapLoginFilter');
427
-		foreach($options as $key) {
427
+		foreach ($options as $key) {
428 428
 			$val = $this->configuration->$key;
429
-			if(empty($val)) {
430
-				switch($key) {
429
+			if (empty($val)) {
430
+				switch ($key) {
431 431
 					case 'ldapHost':
432 432
 						$subj = 'LDAP Host';
433 433
 						break;
@@ -458,7 +458,7 @@  discard block
 block discarded – undo
458 458
 		$agent = $this->configuration->ldapAgentName;
459 459
 		$pwd = $this->configuration->ldapAgentPassword;
460 460
 		if (
461
-			($agent === ''  && $pwd !== '')
461
+			($agent === '' && $pwd !== '')
462 462
 			|| ($agent !== '' && $pwd === '')
463 463
 		) {
464 464
 			\OCP\Util::writeLog('user_ldap',
@@ -473,14 +473,14 @@  discard block
 block discarded – undo
473 473
 		$baseUsers = $this->configuration->ldapBaseUsers;
474 474
 		$baseGroups = $this->configuration->ldapBaseGroups;
475 475
 
476
-		if(empty($base) && empty($baseUsers) && empty($baseGroups)) {
476
+		if (empty($base) && empty($baseUsers) && empty($baseGroups)) {
477 477
 			\OCP\Util::writeLog('user_ldap',
478 478
 								$errorStr.'Not a single Base DN given.',
479 479
 								\OCP\Util::WARN);
480 480
 			$configurationOK = false;
481 481
 		}
482 482
 
483
-		if(mb_strpos($this->configuration->ldapLoginFilter, '%uid', 0, 'UTF-8')
483
+		if (mb_strpos($this->configuration->ldapLoginFilter, '%uid', 0, 'UTF-8')
484 484
 		   === false) {
485 485
 			\OCP\Util::writeLog('user_ldap',
486 486
 								$errorStr.'login filter does not contain %uid '.
@@ -498,7 +498,7 @@  discard block
 block discarded – undo
498 498
 	 */
499 499
 	private function validateConfiguration() {
500 500
 
501
-		if($this->doNotValidate) {
501
+		if ($this->doNotValidate) {
502 502
 			//don't do a validation if it is a new configuration with pure
503 503
 			//default values. Will be allowed on changes via __set or
504 504
 			//setConfiguration
@@ -521,21 +521,21 @@  discard block
 block discarded – undo
521 521
 	 * @throws ServerNotAvailableException
522 522
 	 */
523 523
 	private function establishConnection() {
524
-		if(!$this->configuration->ldapConfigurationActive) {
524
+		if (!$this->configuration->ldapConfigurationActive) {
525 525
 			return null;
526 526
 		}
527 527
 		static $phpLDAPinstalled = true;
528
-		if(!$phpLDAPinstalled) {
528
+		if (!$phpLDAPinstalled) {
529 529
 			return false;
530 530
 		}
531
-		if(!$this->ignoreValidation && !$this->configured) {
531
+		if (!$this->ignoreValidation && !$this->configured) {
532 532
 			\OCP\Util::writeLog('user_ldap',
533 533
 								'Configuration is invalid, cannot connect',
534 534
 								\OCP\Util::WARN);
535 535
 			return false;
536 536
 		}
537
-		if(!$this->ldapConnectionRes) {
538
-			if(!$this->ldap->areLDAPFunctionsAvailable()) {
537
+		if (!$this->ldapConnectionRes) {
538
+			if (!$this->ldap->areLDAPFunctionsAvailable()) {
539 539
 				$phpLDAPinstalled = false;
540 540
 				\OCP\Util::writeLog('user_ldap',
541 541
 									'function ldap_connect is not available. Make '.
@@ -544,8 +544,8 @@  discard block
 block discarded – undo
544 544
 
545 545
 				return false;
546 546
 			}
547
-			if($this->configuration->turnOffCertCheck) {
548
-				if(putenv('LDAPTLS_REQCERT=never')) {
547
+			if ($this->configuration->turnOffCertCheck) {
548
+				if (putenv('LDAPTLS_REQCERT=never')) {
549 549
 					\OCP\Util::writeLog('user_ldap',
550 550
 						'Turned off SSL certificate validation successfully.',
551 551
 						\OCP\Util::DEBUG);
@@ -567,20 +567,20 @@  discard block
 block discarded – undo
567 567
 					return $this->bind();
568 568
 				}
569 569
 			} catch (ServerNotAvailableException $e) {
570
-				if(!$isBackupHost) {
570
+				if (!$isBackupHost) {
571 571
 					throw $e;
572 572
 				}
573 573
 			}
574 574
 
575 575
 			//if LDAP server is not reachable, try the Backup (Replica!) Server
576
-			if($isBackupHost || $isOverrideMainServer) {
576
+			if ($isBackupHost || $isOverrideMainServer) {
577 577
 				$this->doConnect($this->configuration->ldapBackupHost,
578 578
 								 $this->configuration->ldapBackupPort);
579 579
 				$this->bindResult = [];
580 580
 				$bindStatus = $this->bind();
581 581
 				$error = $this->ldap->isResource($this->ldapConnectionRes) ?
582 582
 					$this->ldap->errno($this->ldapConnectionRes) : -1;
583
-				if($bindStatus && $error === 0 && !$this->getFromCache('overrideMainServer')) {
583
+				if ($bindStatus && $error === 0 && !$this->getFromCache('overrideMainServer')) {
584 584
 					//when bind to backup server succeeded and failed to main server,
585 585
 					//skip contacting him until next cache refresh
586 586
 					$this->writeToCache('overrideMainServer', true);
@@ -605,17 +605,17 @@  discard block
 block discarded – undo
605 605
 
606 606
 		$this->ldapConnectionRes = $this->ldap->connect($host, $port);
607 607
 
608
-		if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) {
608
+		if (!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) {
609 609
 			throw new ServerNotAvailableException('Could not set required LDAP Protocol version.');
610 610
 		}
611 611
 
612
-		if(!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_REFERRALS, 0)) {
612
+		if (!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_REFERRALS, 0)) {
613 613
 			throw new ServerNotAvailableException('Could not disable LDAP referrals.');
614 614
 		}
615 615
 
616
-		if($this->configuration->ldapTLS) {
617
-			if(!$this->ldap->startTls($this->ldapConnectionRes)) {
618
-				throw new ServerNotAvailableException('Start TLS failed, when connecting to LDAP host ' . $host . '.');
616
+		if ($this->configuration->ldapTLS) {
617
+			if (!$this->ldap->startTls($this->ldapConnectionRes)) {
618
+				throw new ServerNotAvailableException('Start TLS failed, when connecting to LDAP host '.$host.'.');
619 619
 			}
620 620
 		}
621 621
 
@@ -626,19 +626,19 @@  discard block
 block discarded – undo
626 626
 	 * Binds to LDAP
627 627
 	 */
628 628
 	public function bind() {
629
-		if(!$this->configuration->ldapConfigurationActive) {
629
+		if (!$this->configuration->ldapConfigurationActive) {
630 630
 			return false;
631 631
 		}
632 632
 		$cr = $this->ldapConnectionRes;
633
-		if(!$this->ldap->isResource($cr)) {
633
+		if (!$this->ldap->isResource($cr)) {
634 634
 			$cr = $this->getConnectionResource();
635 635
 		}
636 636
 
637
-		if(
637
+		if (
638 638
 			count($this->bindResult) !== 0
639 639
 			&& $this->bindResult['dn'] === $this->configuration->ldapAgentName
640 640
 			&& \OC::$server->getHasher()->verify(
641
-				$this->configPrefix . $this->configuration->ldapAgentPassword,
641
+				$this->configPrefix.$this->configuration->ldapAgentPassword,
642 642
 				$this->bindResult['hash']
643 643
 			)
644 644
 		) {
@@ -654,19 +654,19 @@  discard block
 block discarded – undo
654 654
 
655 655
 		$this->bindResult = [
656 656
 			'dn' => $this->configuration->ldapAgentName,
657
-			'hash' => \OC::$server->getHasher()->hash($this->configPrefix . $this->configuration->ldapAgentPassword),
657
+			'hash' => \OC::$server->getHasher()->hash($this->configPrefix.$this->configuration->ldapAgentPassword),
658 658
 			'result' => $ldapLogin,
659 659
 		];
660 660
 
661
-		if(!$ldapLogin) {
661
+		if (!$ldapLogin) {
662 662
 			$errno = $this->ldap->errno($cr);
663 663
 
664 664
 			\OCP\Util::writeLog('user_ldap',
665
-				'Bind failed: ' . $errno . ': ' . $this->ldap->error($cr),
665
+				'Bind failed: '.$errno.': '.$this->ldap->error($cr),
666 666
 				\OCP\Util::WARN);
667 667
 
668 668
 			// Set to failure mode, if LDAP error code is not LDAP_SUCCESS or LDAP_INVALID_CREDENTIALS
669
-			if($errno !== 0x00 && $errno !== 0x31) {
669
+			if ($errno !== 0x00 && $errno !== 0x31) {
670 670
 				$this->ldapConnectionRes = null;
671 671
 			}
672 672
 
Please login to merge, or discard this patch.