Passed
Push — master ( d9b2b3...754932 )
by Morris
11:34 queued 11s
created
apps/user_ldap/lib/Access.php 1 patch
Spacing   +188 added lines, -188 removed lines patch added patch discarded remove patch
@@ -129,7 +129,7 @@  discard block
 block discarded – undo
129 129
 	 * @return AbstractMapping
130 130
 	 */
131 131
 	public function getUserMapper() {
132
-		if(is_null($this->userMapper)) {
132
+		if (is_null($this->userMapper)) {
133 133
 			throw new \Exception('UserMapper was not assigned to this Access instance.');
134 134
 		}
135 135
 		return $this->userMapper;
@@ -149,7 +149,7 @@  discard block
 block discarded – undo
149 149
 	 * @return AbstractMapping
150 150
 	 */
151 151
 	public function getGroupMapper() {
152
-		if(is_null($this->groupMapper)) {
152
+		if (is_null($this->groupMapper)) {
153 153
 			throw new \Exception('GroupMapper was not assigned to this Access instance.');
154 154
 		}
155 155
 		return $this->groupMapper;
@@ -182,14 +182,14 @@  discard block
 block discarded – undo
182 182
 	 * @throws ServerNotAvailableException
183 183
 	 */
184 184
 	public function readAttribute($dn, $attr, $filter = 'objectClass=*') {
185
-		if(!$this->checkConnection()) {
185
+		if (!$this->checkConnection()) {
186 186
 			\OCP\Util::writeLog('user_ldap',
187 187
 				'No LDAP Connector assigned, access impossible for readAttribute.',
188 188
 				ILogger::WARN);
189 189
 			return false;
190 190
 		}
191 191
 		$cr = $this->connection->getConnectionResource();
192
-		if(!$this->ldap->isResource($cr)) {
192
+		if (!$this->ldap->isResource($cr)) {
193 193
 			//LDAP not available
194 194
 			\OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', ILogger::DEBUG);
195 195
 			return false;
@@ -199,7 +199,7 @@  discard block
 block discarded – undo
199 199
 		$this->abandonPagedSearch();
200 200
 		// openLDAP requires that we init a new Paged Search. Not needed by AD,
201 201
 		// but does not hurt either.
202
-		$pagingSize = (int)$this->connection->ldapPagingSize;
202
+		$pagingSize = (int) $this->connection->ldapPagingSize;
203 203
 		// 0 won't result in replies, small numbers may leave out groups
204 204
 		// (cf. #12306), 500 is default for paging and should work everywhere.
205 205
 		$maxResults = $pagingSize > 20 ? $pagingSize : 500;
@@ -212,7 +212,7 @@  discard block
 block discarded – undo
212 212
 		$isRangeRequest = false;
213 213
 		do {
214 214
 			$result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults);
215
-			if(is_bool($result)) {
215
+			if (is_bool($result)) {
216 216
 				// when an exists request was run and it was successful, an empty
217 217
 				// array must be returned
218 218
 				return $result ? [] : false;
@@ -229,22 +229,22 @@  discard block
 block discarded – undo
229 229
 			$result = $this->extractRangeData($result, $attr);
230 230
 			if (!empty($result)) {
231 231
 				$normalizedResult = $this->extractAttributeValuesFromResult(
232
-					[ $attr => $result['values'] ],
232
+					[$attr => $result['values']],
233 233
 					$attr
234 234
 				);
235 235
 				$values = array_merge($values, $normalizedResult);
236 236
 
237
-				if($result['rangeHigh'] === '*') {
237
+				if ($result['rangeHigh'] === '*') {
238 238
 					// when server replies with * as high range value, there are
239 239
 					// no more results left
240 240
 					return $values;
241 241
 				} else {
242
-					$low  = $result['rangeHigh'] + 1;
243
-					$attrToRead = $result['attributeName'] . ';range=' . $low . '-*';
242
+					$low = $result['rangeHigh'] + 1;
243
+					$attrToRead = $result['attributeName'].';range='.$low.'-*';
244 244
 					$isRangeRequest = true;
245 245
 				}
246 246
 			}
247
-		} while($isRangeRequest);
247
+		} while ($isRangeRequest);
248 248
 
249 249
 		\OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, ILogger::DEBUG);
250 250
 		return false;
@@ -270,13 +270,13 @@  discard block
 block discarded – undo
270 270
 		if (!$this->ldap->isResource($rr)) {
271 271
 			if ($attribute !== '') {
272 272
 				//do not throw this message on userExists check, irritates
273
-				\OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, ILogger::DEBUG);
273
+				\OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN '.$dn, ILogger::DEBUG);
274 274
 			}
275 275
 			//in case an error occurs , e.g. object does not exist
276 276
 			return false;
277 277
 		}
278 278
 		if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) {
279
-			\OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', ILogger::DEBUG);
279
+			\OCP\Util::writeLog('user_ldap', 'readAttribute: '.$dn.' found', ILogger::DEBUG);
280 280
 			return true;
281 281
 		}
282 282
 		$er = $this->invokeLDAPMethod('firstEntry', $cr, $rr);
@@ -301,12 +301,12 @@  discard block
 block discarded – undo
301 301
 	 */
302 302
 	public function extractAttributeValuesFromResult($result, $attribute) {
303 303
 		$values = [];
304
-		if(isset($result[$attribute]) && $result[$attribute]['count'] > 0) {
304
+		if (isset($result[$attribute]) && $result[$attribute]['count'] > 0) {
305 305
 			$lowercaseAttribute = strtolower($attribute);
306
-			for($i=0;$i<$result[$attribute]['count'];$i++) {
307
-				if($this->resemblesDN($attribute)) {
306
+			for ($i = 0; $i < $result[$attribute]['count']; $i++) {
307
+				if ($this->resemblesDN($attribute)) {
308 308
 					$values[] = $this->helper->sanitizeDN($result[$attribute][$i]);
309
-				} elseif($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') {
309
+				} elseif ($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') {
310 310
 					$values[] = $this->convertObjectGUID2Str($result[$attribute][$i]);
311 311
 				} else {
312 312
 					$values[] = $result[$attribute][$i];
@@ -328,10 +328,10 @@  discard block
 block discarded – undo
328 328
 	 */
329 329
 	public function extractRangeData($result, $attribute) {
330 330
 		$keys = array_keys($result);
331
-		foreach($keys as $key) {
332
-			if($key !== $attribute && strpos($key, $attribute) === 0) {
331
+		foreach ($keys as $key) {
332
+			if ($key !== $attribute && strpos($key, $attribute) === 0) {
333 333
 				$queryData = explode(';', $key);
334
-				if(strpos($queryData[1], 'range=') === 0) {
334
+				if (strpos($queryData[1], 'range=') === 0) {
335 335
 					$high = substr($queryData[1], 1 + strpos($queryData[1], '-'));
336 336
 					$data = [
337 337
 						'values' => $result[$key],
@@ -356,18 +356,18 @@  discard block
 block discarded – undo
356 356
 	 * @throws \Exception
357 357
 	 */
358 358
 	public function setPassword($userDN, $password) {
359
-		if((int)$this->connection->turnOnPasswordChange !== 1) {
359
+		if ((int) $this->connection->turnOnPasswordChange !== 1) {
360 360
 			throw new \Exception('LDAP password changes are disabled.');
361 361
 		}
362 362
 		$cr = $this->connection->getConnectionResource();
363
-		if(!$this->ldap->isResource($cr)) {
363
+		if (!$this->ldap->isResource($cr)) {
364 364
 			//LDAP not available
365 365
 			\OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', ILogger::DEBUG);
366 366
 			return false;
367 367
 		}
368 368
 		try {
369 369
 			return @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password);
370
-		} catch(ConstraintViolationException $e) {
370
+		} catch (ConstraintViolationException $e) {
371 371
 			throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ').$e->getMessage(), $e->getCode());
372 372
 		}
373 373
 	}
@@ -409,17 +409,17 @@  discard block
 block discarded – undo
409 409
 	 */
410 410
 	public function getDomainDNFromDN($dn) {
411 411
 		$allParts = $this->ldap->explodeDN($dn, 0);
412
-		if($allParts === false) {
412
+		if ($allParts === false) {
413 413
 			//not a valid DN
414 414
 			return '';
415 415
 		}
416 416
 		$domainParts = array();
417 417
 		$dcFound = false;
418
-		foreach($allParts as $part) {
419
-			if(!$dcFound && strpos($part, 'dc=') === 0) {
418
+		foreach ($allParts as $part) {
419
+			if (!$dcFound && strpos($part, 'dc=') === 0) {
420 420
 				$dcFound = true;
421 421
 			}
422
-			if($dcFound) {
422
+			if ($dcFound) {
423 423
 				$domainParts[] = $part;
424 424
 			}
425 425
 		}
@@ -445,7 +445,7 @@  discard block
 block discarded – undo
445 445
 
446 446
 		//Check whether the DN belongs to the Base, to avoid issues on multi-
447 447
 		//server setups
448
-		if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) {
448
+		if (is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) {
449 449
 			return $fdn;
450 450
 		}
451 451
 
@@ -462,7 +462,7 @@  discard block
 block discarded – undo
462 462
 		//To avoid bypassing the base DN settings under certain circumstances
463 463
 		//with the group support, check whether the provided DN matches one of
464 464
 		//the given Bases
465
-		if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) {
465
+		if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) {
466 466
 			return false;
467 467
 		}
468 468
 
@@ -480,11 +480,11 @@  discard block
 block discarded – undo
480 480
 	 */
481 481
 	public function groupsMatchFilter($groupDNs) {
482 482
 		$validGroupDNs = [];
483
-		foreach($groupDNs as $dn) {
483
+		foreach ($groupDNs as $dn) {
484 484
 			$cacheKey = 'groupsMatchFilter-'.$dn;
485 485
 			$groupMatchFilter = $this->connection->getFromCache($cacheKey);
486
-			if(!is_null($groupMatchFilter)) {
487
-				if($groupMatchFilter) {
486
+			if (!is_null($groupMatchFilter)) {
487
+				if ($groupMatchFilter) {
488 488
 					$validGroupDNs[] = $dn;
489 489
 				}
490 490
 				continue;
@@ -492,13 +492,13 @@  discard block
 block discarded – undo
492 492
 
493 493
 			// Check the base DN first. If this is not met already, we don't
494 494
 			// need to ask the server at all.
495
-			if(!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) {
495
+			if (!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) {
496 496
 				$this->connection->writeToCache($cacheKey, false);
497 497
 				continue;
498 498
 			}
499 499
 
500 500
 			$result = $this->readAttribute($dn, '', $this->connection->ldapGroupFilter);
501
-			if(is_array($result)) {
501
+			if (is_array($result)) {
502 502
 				$this->connection->writeToCache($cacheKey, true);
503 503
 				$validGroupDNs[] = $dn;
504 504
 			} else {
@@ -519,7 +519,7 @@  discard block
 block discarded – undo
519 519
 		//To avoid bypassing the base DN settings under certain circumstances
520 520
 		//with the group support, check whether the provided DN matches one of
521 521
 		//the given Bases
522
-		if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) {
522
+		if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) {
523 523
 			return false;
524 524
 		}
525 525
 
@@ -539,7 +539,7 @@  discard block
 block discarded – undo
539 539
 	 */
540 540
 	public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, array $record = null) {
541 541
 		$newlyMapped = false;
542
-		if($isUser) {
542
+		if ($isUser) {
543 543
 			$mapper = $this->getUserMapper();
544 544
 			$nameAttribute = $this->connection->ldapUserDisplayName;
545 545
 			$filter = $this->connection->ldapUserFilter;
@@ -551,15 +551,15 @@  discard block
 block discarded – undo
551 551
 
552 552
 		//let's try to retrieve the Nextcloud name from the mappings table
553 553
 		$ncName = $mapper->getNameByDN($fdn);
554
-		if(is_string($ncName)) {
554
+		if (is_string($ncName)) {
555 555
 			return $ncName;
556 556
 		}
557 557
 
558 558
 		//second try: get the UUID and check if it is known. Then, update the DN and return the name.
559 559
 		$uuid = $this->getUUID($fdn, $isUser, $record);
560
-		if(is_string($uuid)) {
560
+		if (is_string($uuid)) {
561 561
 			$ncName = $mapper->getNameByUUID($uuid);
562
-			if(is_string($ncName)) {
562
+			if (is_string($ncName)) {
563 563
 				$mapper->setDNbyUUID($fdn, $uuid);
564 564
 				return $ncName;
565 565
 			}
@@ -569,17 +569,17 @@  discard block
 block discarded – undo
569 569
 			return false;
570 570
 		}
571 571
 
572
-		if(is_null($ldapName)) {
572
+		if (is_null($ldapName)) {
573 573
 			$ldapName = $this->readAttribute($fdn, $nameAttribute, $filter);
574
-			if(!isset($ldapName[0]) && empty($ldapName[0])) {
574
+			if (!isset($ldapName[0]) && empty($ldapName[0])) {
575 575
 				\OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.' with filter '.$filter.'.', ILogger::INFO);
576 576
 				return false;
577 577
 			}
578 578
 			$ldapName = $ldapName[0];
579 579
 		}
580 580
 
581
-		if($isUser) {
582
-			$usernameAttribute = (string)$this->connection->ldapExpertUsernameAttr;
581
+		if ($isUser) {
582
+			$usernameAttribute = (string) $this->connection->ldapExpertUsernameAttr;
583 583
 			if ($usernameAttribute !== '') {
584 584
 				$username = $this->readAttribute($fdn, $usernameAttribute);
585 585
 				$username = $username[0];
@@ -609,14 +609,14 @@  discard block
 block discarded – undo
609 609
 		// outside of core user management will still cache the user as non-existing.
610 610
 		$originalTTL = $this->connection->ldapCacheTTL;
611 611
 		$this->connection->setConfiguration(['ldapCacheTTL' => 0]);
612
-		if( $intName !== ''
612
+		if ($intName !== ''
613 613
 			&& (($isUser && !$this->ncUserManager->userExists($intName))
614 614
 				|| (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))
615 615
 			)
616 616
 		) {
617 617
 			$this->connection->setConfiguration(['ldapCacheTTL' => $originalTTL]);
618 618
 			$newlyMapped = $this->mapAndAnnounceIfApplicable($mapper, $fdn, $intName, $uuid, $isUser);
619
-			if($newlyMapped) {
619
+			if ($newlyMapped) {
620 620
 				return $intName;
621 621
 			}
622 622
 		}
@@ -624,7 +624,7 @@  discard block
 block discarded – undo
624 624
 		$this->connection->setConfiguration(['ldapCacheTTL' => $originalTTL]);
625 625
 		$altName = $this->createAltInternalOwnCloudName($intName, $isUser);
626 626
 		if (is_string($altName)) {
627
-			if($this->mapAndAnnounceIfApplicable($mapper, $fdn, $altName, $uuid, $isUser)) {
627
+			if ($this->mapAndAnnounceIfApplicable($mapper, $fdn, $altName, $uuid, $isUser)) {
628 628
 				$newlyMapped = true;
629 629
 				return $altName;
630 630
 			}
@@ -642,7 +642,7 @@  discard block
 block discarded – undo
642 642
 		string $uuid,
643 643
 		bool $isUser
644 644
 	) :bool {
645
-		if($mapper->map($fdn, $name, $uuid)) {
645
+		if ($mapper->map($fdn, $name, $uuid)) {
646 646
 			if ($this->ncUserManager instanceof PublicEmitter && $isUser) {
647 647
 				$this->cacheUserExists($name);
648 648
 				$this->ncUserManager->emit('\OC\User', 'assignedUserId', [$name]);
@@ -681,7 +681,7 @@  discard block
 block discarded – undo
681 681
 	 * @throws \Exception
682 682
 	 */
683 683
 	private function ldap2NextcloudNames($ldapObjects, $isUsers) {
684
-		if($isUsers) {
684
+		if ($isUsers) {
685 685
 			$nameAttribute = $this->connection->ldapUserDisplayName;
686 686
 			$sndAttribute  = $this->connection->ldapUserDisplayName2;
687 687
 		} else {
@@ -689,9 +689,9 @@  discard block
 block discarded – undo
689 689
 		}
690 690
 		$nextcloudNames = [];
691 691
 
692
-		foreach($ldapObjects as $ldapObject) {
692
+		foreach ($ldapObjects as $ldapObject) {
693 693
 			$nameByLDAP = null;
694
-			if(    isset($ldapObject[$nameAttribute])
694
+			if (isset($ldapObject[$nameAttribute])
695 695
 				&& is_array($ldapObject[$nameAttribute])
696 696
 				&& isset($ldapObject[$nameAttribute][0])
697 697
 			) {
@@ -700,13 +700,13 @@  discard block
 block discarded – undo
700 700
 			}
701 701
 
702 702
 			$ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers);
703
-			if($ncName) {
703
+			if ($ncName) {
704 704
 				$nextcloudNames[] = $ncName;
705
-				if($isUsers) {
705
+				if ($isUsers) {
706 706
 					$this->updateUserState($ncName);
707 707
 					//cache the user names so it does not need to be retrieved
708 708
 					//again later (e.g. sharing dialogue).
709
-					if(is_null($nameByLDAP)) {
709
+					if (is_null($nameByLDAP)) {
710 710
 						continue;
711 711
 					}
712 712
 					$sndName = isset($ldapObject[$sndAttribute][0])
@@ -726,7 +726,7 @@  discard block
 block discarded – undo
726 726
 	 */
727 727
 	public function updateUserState($ncname) {
728 728
 		$user = $this->userManager->get($ncname);
729
-		if($user instanceof OfflineUser) {
729
+		if ($user instanceof OfflineUser) {
730 730
 			$user->unmark();
731 731
 		}
732 732
 	}
@@ -757,7 +757,7 @@  discard block
 block discarded – undo
757 757
 	 */
758 758
 	public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') {
759 759
 		$user = $this->userManager->get($ocName);
760
-		if($user === null) {
760
+		if ($user === null) {
761 761
 			return;
762 762
 		}
763 763
 		$displayName = $user->composeAndStoreDisplayName($displayName, $displayName2);
@@ -777,9 +777,9 @@  discard block
 block discarded – undo
777 777
 		$attempts = 0;
778 778
 		//while loop is just a precaution. If a name is not generated within
779 779
 		//20 attempts, something else is very wrong. Avoids infinite loop.
780
-		while($attempts < 20){
781
-			$altName = $name . '_' . rand(1000,9999);
782
-			if(!$this->ncUserManager->userExists($altName)) {
780
+		while ($attempts < 20) {
781
+			$altName = $name.'_'.rand(1000, 9999);
782
+			if (!$this->ncUserManager->userExists($altName)) {
783 783
 				return $altName;
784 784
 			}
785 785
 			$attempts++;
@@ -801,25 +801,25 @@  discard block
 block discarded – undo
801 801
 	 */
802 802
 	private function _createAltInternalOwnCloudNameForGroups($name) {
803 803
 		$usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%');
804
-		if(!$usedNames || count($usedNames) === 0) {
804
+		if (!$usedNames || count($usedNames) === 0) {
805 805
 			$lastNo = 1; //will become name_2
806 806
 		} else {
807 807
 			natsort($usedNames);
808 808
 			$lastName = array_pop($usedNames);
809
-			$lastNo = (int)substr($lastName, strrpos($lastName, '_') + 1);
809
+			$lastNo = (int) substr($lastName, strrpos($lastName, '_') + 1);
810 810
 		}
811
-		$altName = $name.'_'. (string)($lastNo+1);
811
+		$altName = $name.'_'.(string) ($lastNo + 1);
812 812
 		unset($usedNames);
813 813
 
814 814
 		$attempts = 1;
815
-		while($attempts < 21){
815
+		while ($attempts < 21) {
816 816
 			// Check to be really sure it is unique
817 817
 			// while loop is just a precaution. If a name is not generated within
818 818
 			// 20 attempts, something else is very wrong. Avoids infinite loop.
819
-			if(!\OC::$server->getGroupManager()->groupExists($altName)) {
819
+			if (!\OC::$server->getGroupManager()->groupExists($altName)) {
820 820
 				return $altName;
821 821
 			}
822
-			$altName = $name . '_' . ($lastNo + $attempts);
822
+			$altName = $name.'_'.($lastNo + $attempts);
823 823
 			$attempts++;
824 824
 		}
825 825
 		return false;
@@ -834,7 +834,7 @@  discard block
 block discarded – undo
834 834
 	private function createAltInternalOwnCloudName($name, $isUser) {
835 835
 		$originalTTL = $this->connection->ldapCacheTTL;
836 836
 		$this->connection->setConfiguration(array('ldapCacheTTL' => 0));
837
-		if($isUser) {
837
+		if ($isUser) {
838 838
 			$altName = $this->_createAltInternalOwnCloudNameForUsers($name);
839 839
 		} else {
840 840
 			$altName = $this->_createAltInternalOwnCloudNameForGroups($name);
@@ -882,13 +882,13 @@  discard block
 block discarded – undo
882 882
 	public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null, $forceApplyAttributes = false) {
883 883
 		$ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset);
884 884
 		$recordsToUpdate = $ldapRecords;
885
-		if(!$forceApplyAttributes) {
885
+		if (!$forceApplyAttributes) {
886 886
 			$isBackgroundJobModeAjax = $this->config
887 887
 					->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax';
888 888
 			$recordsToUpdate = array_filter($ldapRecords, function($record) use ($isBackgroundJobModeAjax) {
889 889
 				$newlyMapped = false;
890 890
 				$uid = $this->dn2ocname($record['dn'][0], null, true, $newlyMapped, $record);
891
-				if(is_string($uid)) {
891
+				if (is_string($uid)) {
892 892
 					$this->cacheUserExists($uid);
893 893
 				}
894 894
 				return ($uid !== false) && ($newlyMapped || $isBackgroundJobModeAjax);
@@ -906,15 +906,15 @@  discard block
 block discarded – undo
906 906
 	 * @param array $ldapRecords
907 907
 	 * @throws \Exception
908 908
 	 */
909
-	public function batchApplyUserAttributes(array $ldapRecords){
909
+	public function batchApplyUserAttributes(array $ldapRecords) {
910 910
 		$displayNameAttribute = strtolower($this->connection->ldapUserDisplayName);
911
-		foreach($ldapRecords as $userRecord) {
912
-			if(!isset($userRecord[$displayNameAttribute])) {
911
+		foreach ($ldapRecords as $userRecord) {
912
+			if (!isset($userRecord[$displayNameAttribute])) {
913 913
 				// displayName is obligatory
914 914
 				continue;
915 915
 			}
916
-			$ocName  = $this->dn2ocname($userRecord['dn'][0], null, true);
917
-			if($ocName === false) {
916
+			$ocName = $this->dn2ocname($userRecord['dn'][0], null, true);
917
+			if ($ocName === false) {
918 918
 				continue;
919 919
 			}
920 920
 			$this->updateUserState($ocName);
@@ -947,8 +947,8 @@  discard block
 block discarded – undo
947 947
 	 * @return array
948 948
 	 */
949 949
 	private function fetchList($list, $manyAttributes) {
950
-		if(is_array($list)) {
951
-			if($manyAttributes) {
950
+		if (is_array($list)) {
951
+			if ($manyAttributes) {
952 952
 				return $list;
953 953
 			} else {
954 954
 				$list = array_reduce($list, function($carry, $item) {
@@ -976,7 +976,7 @@  discard block
 block discarded – undo
976 976
 	 */
977 977
 	public function searchUsers($filter, $attr = null, $limit = null, $offset = null) {
978 978
 		$result = [];
979
-		foreach($this->connection->ldapBaseUsers as $base) {
979
+		foreach ($this->connection->ldapBaseUsers as $base) {
980 980
 			$result = array_merge($result, $this->search($filter, [$base], $attr, $limit, $offset));
981 981
 		}
982 982
 		return $result;
@@ -991,9 +991,9 @@  discard block
 block discarded – undo
991 991
 	 */
992 992
 	public function countUsers($filter, $attr = array('dn'), $limit = null, $offset = null) {
993 993
 		$result = false;
994
-		foreach($this->connection->ldapBaseUsers as $base) {
994
+		foreach ($this->connection->ldapBaseUsers as $base) {
995 995
 			$count = $this->count($filter, [$base], $attr, $limit, $offset);
996
-			$result = is_int($count) ? (int)$result + $count : $result;
996
+			$result = is_int($count) ? (int) $result + $count : $result;
997 997
 		}
998 998
 		return $result;
999 999
 	}
@@ -1010,7 +1010,7 @@  discard block
 block discarded – undo
1010 1010
 	 */
1011 1011
 	public function searchGroups($filter, $attr = null, $limit = null, $offset = null) {
1012 1012
 		$result = [];
1013
-		foreach($this->connection->ldapBaseGroups as $base) {
1013
+		foreach ($this->connection->ldapBaseGroups as $base) {
1014 1014
 			$result = array_merge($result, $this->search($filter, [$base], $attr, $limit, $offset));
1015 1015
 		}
1016 1016
 		return $result;
@@ -1026,9 +1026,9 @@  discard block
 block discarded – undo
1026 1026
 	 */
1027 1027
 	public function countGroups($filter, $attr = array('dn'), $limit = null, $offset = null) {
1028 1028
 		$result = false;
1029
-		foreach($this->connection->ldapBaseGroups as $base) {
1029
+		foreach ($this->connection->ldapBaseGroups as $base) {
1030 1030
 			$count = $this->count($filter, [$base], $attr, $limit, $offset);
1031
-			$result = is_int($count) ? (int)$result + $count : $result;
1031
+			$result = is_int($count) ? (int) $result + $count : $result;
1032 1032
 		}
1033 1033
 		return $result;
1034 1034
 	}
@@ -1042,9 +1042,9 @@  discard block
 block discarded – undo
1042 1042
 	 */
1043 1043
 	public function countObjects($limit = null, $offset = null) {
1044 1044
 		$result = false;
1045
-		foreach($this->connection->ldapBase as $base) {
1045
+		foreach ($this->connection->ldapBase as $base) {
1046 1046
 			$count = $this->count('objectclass=*', [$base], ['dn'], $limit, $offset);
1047
-			$result = is_int($count) ? (int)$result + $count : $result;
1047
+			$result = is_int($count) ? (int) $result + $count : $result;
1048 1048
 		}
1049 1049
 		return $result;
1050 1050
 	}
@@ -1069,7 +1069,7 @@  discard block
 block discarded – undo
1069 1069
 		// php no longer supports call-time pass-by-reference
1070 1070
 		// thus cannot support controlPagedResultResponse as the third argument
1071 1071
 		// is a reference
1072
-		$doMethod = function () use ($command, &$arguments) {
1072
+		$doMethod = function() use ($command, &$arguments) {
1073 1073
 			if ($command == 'controlPagedResultResponse') {
1074 1074
 				throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.');
1075 1075
 			} else {
@@ -1087,7 +1087,7 @@  discard block
 block discarded – undo
1087 1087
 			$this->connection->resetConnectionResource();
1088 1088
 			$cr = $this->connection->getConnectionResource();
1089 1089
 
1090
-			if(!$this->ldap->isResource($cr)) {
1090
+			if (!$this->ldap->isResource($cr)) {
1091 1091
 				// Seems like we didn't find any resource.
1092 1092
 				\OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", ILogger::DEBUG);
1093 1093
 				throw $e;
@@ -1112,13 +1112,13 @@  discard block
 block discarded – undo
1112 1112
 	 * @throws ServerNotAvailableException
1113 1113
 	 */
1114 1114
 	private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) {
1115
-		if(!is_null($attr) && !is_array($attr)) {
1115
+		if (!is_null($attr) && !is_array($attr)) {
1116 1116
 			$attr = array(mb_strtolower($attr, 'UTF-8'));
1117 1117
 		}
1118 1118
 
1119 1119
 		// See if we have a resource, in case not cancel with message
1120 1120
 		$cr = $this->connection->getConnectionResource();
1121
-		if(!$this->ldap->isResource($cr)) {
1121
+		if (!$this->ldap->isResource($cr)) {
1122 1122
 			// Seems like we didn't find any resource.
1123 1123
 			// Return an empty array just like before.
1124 1124
 			\OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', ILogger::DEBUG);
@@ -1126,13 +1126,13 @@  discard block
 block discarded – undo
1126 1126
 		}
1127 1127
 
1128 1128
 		//check whether paged search should be attempted
1129
-		$pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, (int)$limit, $offset);
1129
+		$pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, (int) $limit, $offset);
1130 1130
 
1131 1131
 		$linkResources = array_pad(array(), count($base), $cr);
1132 1132
 		$sr = $this->invokeLDAPMethod('search', $linkResources, $base, $filter, $attr);
1133 1133
 		// cannot use $cr anymore, might have changed in the previous call!
1134 1134
 		$error = $this->ldap->errno($this->connection->getConnectionResource());
1135
-		if(!is_array($sr) || $error !== 0) {
1135
+		if (!is_array($sr) || $error !== 0) {
1136 1136
 			\OCP\Util::writeLog('user_ldap', 'Attempt for Paging?  '.print_r($pagedSearchOK, true), ILogger::ERROR);
1137 1137
 			return false;
1138 1138
 		}
@@ -1155,29 +1155,29 @@  discard block
 block discarded – undo
1155 1155
 	 */
1156 1156
 	private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) {
1157 1157
 		$cookie = null;
1158
-		if($pagedSearchOK) {
1158
+		if ($pagedSearchOK) {
1159 1159
 			$cr = $this->connection->getConnectionResource();
1160
-			foreach($sr as $key => $res) {
1161
-				if($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) {
1160
+			foreach ($sr as $key => $res) {
1161
+				if ($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) {
1162 1162
 					$this->setPagedResultCookie($base[$key], $filter, $limit, $offset, $cookie);
1163 1163
 				}
1164 1164
 			}
1165 1165
 
1166 1166
 			//browsing through prior pages to get the cookie for the new one
1167
-			if($skipHandling) {
1167
+			if ($skipHandling) {
1168 1168
 				return false;
1169 1169
 			}
1170 1170
 			// if count is bigger, then the server does not support
1171 1171
 			// paged search. Instead, he did a normal search. We set a
1172 1172
 			// flag here, so the callee knows how to deal with it.
1173
-			if($iFoundItems <= $limit) {
1173
+			if ($iFoundItems <= $limit) {
1174 1174
 				$this->pagedSearchedSuccessful = true;
1175 1175
 			}
1176 1176
 		} else {
1177
-			if(!is_null($limit) && (int)$this->connection->ldapPagingSize !== 0) {
1177
+			if (!is_null($limit) && (int) $this->connection->ldapPagingSize !== 0) {
1178 1178
 				\OC::$server->getLogger()->debug(
1179 1179
 					'Paged search was not available',
1180
-					[ 'app' => 'user_ldap' ]
1180
+					['app' => 'user_ldap']
1181 1181
 				);
1182 1182
 			}
1183 1183
 		}
@@ -1206,8 +1206,8 @@  discard block
 block discarded – undo
1206 1206
 	private function count($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) {
1207 1207
 		\OCP\Util::writeLog('user_ldap', 'Count filter:  '.print_r($filter, true), ILogger::DEBUG);
1208 1208
 
1209
-		$limitPerPage = (int)$this->connection->ldapPagingSize;
1210
-		if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) {
1209
+		$limitPerPage = (int) $this->connection->ldapPagingSize;
1210
+		if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) {
1211 1211
 			$limitPerPage = $limit;
1212 1212
 		}
1213 1213
 
@@ -1217,7 +1217,7 @@  discard block
 block discarded – undo
1217 1217
 
1218 1218
 		do {
1219 1219
 			$search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset);
1220
-			if($search === false) {
1220
+			if ($search === false) {
1221 1221
 				return $counter > 0 ? $counter : false;
1222 1222
 			}
1223 1223
 			list($sr, $pagedSearchOK) = $search;
@@ -1236,7 +1236,7 @@  discard block
 block discarded – undo
1236 1236
 			 * Continue now depends on $hasMorePages value
1237 1237
 			 */
1238 1238
 			$continue = $pagedSearchOK && $hasMorePages;
1239
-		} while($continue && (is_null($limit) || $limit <= 0 || $limit > $counter));
1239
+		} while ($continue && (is_null($limit) || $limit <= 0 || $limit > $counter));
1240 1240
 
1241 1241
 		return $counter;
1242 1242
 	}
@@ -1248,8 +1248,8 @@  discard block
 block discarded – undo
1248 1248
 	private function countEntriesInSearchResults($searchResults) {
1249 1249
 		$counter = 0;
1250 1250
 
1251
-		foreach($searchResults as $res) {
1252
-			$count = (int)$this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res);
1251
+		foreach ($searchResults as $res) {
1252
+			$count = (int) $this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res);
1253 1253
 			$counter += $count;
1254 1254
 		}
1255 1255
 
@@ -1269,8 +1269,8 @@  discard block
 block discarded – undo
1269 1269
 	 * @throws ServerNotAvailableException
1270 1270
 	 */
1271 1271
 	public function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) {
1272
-		$limitPerPage = (int)$this->connection->ldapPagingSize;
1273
-		if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) {
1272
+		$limitPerPage = (int) $this->connection->ldapPagingSize;
1273
+		if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) {
1274 1274
 			$limitPerPage = $limit;
1275 1275
 		}
1276 1276
 
@@ -1284,13 +1284,13 @@  discard block
 block discarded – undo
1284 1284
 		$savedoffset = $offset;
1285 1285
 		do {
1286 1286
 			$search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset);
1287
-			if($search === false) {
1287
+			if ($search === false) {
1288 1288
 				return [];
1289 1289
 			}
1290 1290
 			list($sr, $pagedSearchOK) = $search;
1291 1291
 			$cr = $this->connection->getConnectionResource();
1292 1292
 
1293
-			if($skipHandling) {
1293
+			if ($skipHandling) {
1294 1294
 				//i.e. result do not need to be fetched, we just need the cookie
1295 1295
 				//thus pass 1 or any other value as $iFoundItems because it is not
1296 1296
 				//used
@@ -1301,7 +1301,7 @@  discard block
 block discarded – undo
1301 1301
 			}
1302 1302
 
1303 1303
 			$iFoundItems = 0;
1304
-			foreach($sr as $res) {
1304
+			foreach ($sr as $res) {
1305 1305
 				$findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $res));
1306 1306
 				$iFoundItems = max($iFoundItems, $findings['count']);
1307 1307
 				unset($findings['count']);
@@ -1317,27 +1317,27 @@  discard block
 block discarded – undo
1317 1317
 
1318 1318
 		// if we're here, probably no connection resource is returned.
1319 1319
 		// to make Nextcloud behave nicely, we simply give back an empty array.
1320
-		if(is_null($findings)) {
1320
+		if (is_null($findings)) {
1321 1321
 			return array();
1322 1322
 		}
1323 1323
 
1324
-		if(!is_null($attr)) {
1324
+		if (!is_null($attr)) {
1325 1325
 			$selection = [];
1326 1326
 			$i = 0;
1327
-			foreach($findings as $item) {
1328
-				if(!is_array($item)) {
1327
+			foreach ($findings as $item) {
1328
+				if (!is_array($item)) {
1329 1329
 					continue;
1330 1330
 				}
1331 1331
 				$item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8');
1332
-				foreach($attr as $key) {
1333
-					if(isset($item[$key])) {
1334
-						if(is_array($item[$key]) && isset($item[$key]['count'])) {
1332
+				foreach ($attr as $key) {
1333
+					if (isset($item[$key])) {
1334
+						if (is_array($item[$key]) && isset($item[$key]['count'])) {
1335 1335
 							unset($item[$key]['count']);
1336 1336
 						}
1337
-						if($key !== 'dn') {
1338
-							if($this->resemblesDN($key)) {
1337
+						if ($key !== 'dn') {
1338
+							if ($this->resemblesDN($key)) {
1339 1339
 								$selection[$i][$key] = $this->helper->sanitizeDN($item[$key]);
1340
-							} else if($key === 'objectguid' || $key === 'guid') {
1340
+							} else if ($key === 'objectguid' || $key === 'guid') {
1341 1341
 								$selection[$i][$key] = [$this->convertObjectGUID2Str($item[$key][0])];
1342 1342
 							} else {
1343 1343
 								$selection[$i][$key] = $item[$key];
@@ -1355,14 +1355,14 @@  discard block
 block discarded – undo
1355 1355
 		//we slice the findings, when
1356 1356
 		//a) paged search unsuccessful, though attempted
1357 1357
 		//b) no paged search, but limit set
1358
-		if((!$this->getPagedSearchResultState()
1358
+		if ((!$this->getPagedSearchResultState()
1359 1359
 			&& $pagedSearchOK)
1360 1360
 			|| (
1361 1361
 				!$pagedSearchOK
1362 1362
 				&& !is_null($limit)
1363 1363
 			)
1364 1364
 		) {
1365
-			$findings = array_slice($findings, (int)$offset, $limit);
1365
+			$findings = array_slice($findings, (int) $offset, $limit);
1366 1366
 		}
1367 1367
 		return $findings;
1368 1368
 	}
@@ -1375,13 +1375,13 @@  discard block
 block discarded – undo
1375 1375
 	public function sanitizeUsername($name) {
1376 1376
 		$name = trim($name);
1377 1377
 
1378
-		if($this->connection->ldapIgnoreNamingRules) {
1378
+		if ($this->connection->ldapIgnoreNamingRules) {
1379 1379
 			return $name;
1380 1380
 		}
1381 1381
 
1382 1382
 		// Transliteration to ASCII
1383 1383
 		$transliterated = @iconv('UTF-8', 'ASCII//TRANSLIT', $name);
1384
-		if($transliterated !== false) {
1384
+		if ($transliterated !== false) {
1385 1385
 			// depending on system config iconv can work or not
1386 1386
 			$name = $transliterated;
1387 1387
 		}
@@ -1392,7 +1392,7 @@  discard block
 block discarded – undo
1392 1392
 		// Every remaining disallowed characters will be removed
1393 1393
 		$name = preg_replace('/[^a-zA-Z0-9_.@-]/u', '', $name);
1394 1394
 
1395
-		if($name === '') {
1395
+		if ($name === '') {
1396 1396
 			throw new \InvalidArgumentException('provided name template for username does not contain any allowed characters');
1397 1397
 		}
1398 1398
 
@@ -1407,13 +1407,13 @@  discard block
 block discarded – undo
1407 1407
 	*/
1408 1408
 	public function escapeFilterPart($input, $allowAsterisk = false) {
1409 1409
 		$asterisk = '';
1410
-		if($allowAsterisk && strlen($input) > 0 && $input[0] === '*') {
1410
+		if ($allowAsterisk && strlen($input) > 0 && $input[0] === '*') {
1411 1411
 			$asterisk = '*';
1412 1412
 			$input = mb_substr($input, 1, null, 'UTF-8');
1413 1413
 		}
1414 1414
 		$search  = array('*', '\\', '(', ')');
1415 1415
 		$replace = array('\\*', '\\\\', '\\(', '\\)');
1416
-		return $asterisk . str_replace($search, $replace, $input);
1416
+		return $asterisk.str_replace($search, $replace, $input);
1417 1417
 	}
1418 1418
 
1419 1419
 	/**
@@ -1443,13 +1443,13 @@  discard block
 block discarded – undo
1443 1443
 	 */
1444 1444
 	private function combineFilter($filters, $operator) {
1445 1445
 		$combinedFilter = '('.$operator;
1446
-		foreach($filters as $filter) {
1446
+		foreach ($filters as $filter) {
1447 1447
 			if ($filter !== '' && $filter[0] !== '(') {
1448 1448
 				$filter = '('.$filter.')';
1449 1449
 			}
1450
-			$combinedFilter.=$filter;
1450
+			$combinedFilter .= $filter;
1451 1451
 		}
1452
-		$combinedFilter.=')';
1452
+		$combinedFilter .= ')';
1453 1453
 		return $combinedFilter;
1454 1454
 	}
1455 1455
 
@@ -1485,17 +1485,17 @@  discard block
 block discarded – undo
1485 1485
 	 * @throws \Exception
1486 1486
 	 */
1487 1487
 	private function getAdvancedFilterPartForSearch($search, $searchAttributes) {
1488
-		if(!is_array($searchAttributes) || count($searchAttributes) < 2) {
1488
+		if (!is_array($searchAttributes) || count($searchAttributes) < 2) {
1489 1489
 			throw new \Exception('searchAttributes must be an array with at least two string');
1490 1490
 		}
1491 1491
 		$searchWords = explode(' ', trim($search));
1492 1492
 		$wordFilters = array();
1493
-		foreach($searchWords as $word) {
1493
+		foreach ($searchWords as $word) {
1494 1494
 			$word = $this->prepareSearchTerm($word);
1495 1495
 			//every word needs to appear at least once
1496 1496
 			$wordMatchOneAttrFilters = array();
1497
-			foreach($searchAttributes as $attr) {
1498
-				$wordMatchOneAttrFilters[] = $attr . '=' . $word;
1497
+			foreach ($searchAttributes as $attr) {
1498
+				$wordMatchOneAttrFilters[] = $attr.'='.$word;
1499 1499
 			}
1500 1500
 			$wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters);
1501 1501
 		}
@@ -1513,10 +1513,10 @@  discard block
 block discarded – undo
1513 1513
 	private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) {
1514 1514
 		$filter = array();
1515 1515
 		$haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0);
1516
-		if($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) {
1516
+		if ($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) {
1517 1517
 			try {
1518 1518
 				return $this->getAdvancedFilterPartForSearch($search, $searchAttributes);
1519
-			} catch(\Exception $e) {
1519
+			} catch (\Exception $e) {
1520 1520
 				\OCP\Util::writeLog(
1521 1521
 					'user_ldap',
1522 1522
 					'Creating advanced filter for search failed, falling back to simple method.',
@@ -1526,17 +1526,17 @@  discard block
 block discarded – undo
1526 1526
 		}
1527 1527
 
1528 1528
 		$search = $this->prepareSearchTerm($search);
1529
-		if(!is_array($searchAttributes) || count($searchAttributes) === 0) {
1529
+		if (!is_array($searchAttributes) || count($searchAttributes) === 0) {
1530 1530
 			if ($fallbackAttribute === '') {
1531 1531
 				return '';
1532 1532
 			}
1533
-			$filter[] = $fallbackAttribute . '=' . $search;
1533
+			$filter[] = $fallbackAttribute.'='.$search;
1534 1534
 		} else {
1535
-			foreach($searchAttributes as $attribute) {
1536
-				$filter[] = $attribute . '=' . $search;
1535
+			foreach ($searchAttributes as $attribute) {
1536
+				$filter[] = $attribute.'='.$search;
1537 1537
 			}
1538 1538
 		}
1539
-		if(count($filter) === 1) {
1539
+		if (count($filter) === 1) {
1540 1540
 			return '('.$filter[0].')';
1541 1541
 		}
1542 1542
 		return $this->combineFilterWithOr($filter);
@@ -1557,7 +1557,7 @@  discard block
 block discarded – undo
1557 1557
 		if ($term === '') {
1558 1558
 			$result = '*';
1559 1559
 		} else if ($allowEnum !== 'no') {
1560
-			$result = $term . '*';
1560
+			$result = $term.'*';
1561 1561
 		}
1562 1562
 		return $result;
1563 1563
 	}
@@ -1569,7 +1569,7 @@  discard block
 block discarded – undo
1569 1569
 	public function getFilterForUserCount() {
1570 1570
 		$filter = $this->combineFilterWithAnd(array(
1571 1571
 			$this->connection->ldapUserFilter,
1572
-			$this->connection->ldapUserDisplayName . '=*'
1572
+			$this->connection->ldapUserDisplayName.'=*'
1573 1573
 		));
1574 1574
 
1575 1575
 		return $filter;
@@ -1587,7 +1587,7 @@  discard block
 block discarded – undo
1587 1587
 			'ldapAgentName' => $name,
1588 1588
 			'ldapAgentPassword' => $password
1589 1589
 		);
1590
-		if(!$testConnection->setConfiguration($credentials)) {
1590
+		if (!$testConnection->setConfiguration($credentials)) {
1591 1591
 			return false;
1592 1592
 		}
1593 1593
 		return $testConnection->bind();
@@ -1609,30 +1609,30 @@  discard block
 block discarded – undo
1609 1609
 			// Sacrebleu! The UUID attribute is unknown :( We need first an
1610 1610
 			// existing DN to be able to reliably detect it.
1611 1611
 			$result = $this->search($filter, $base, ['dn'], 1);
1612
-			if(!isset($result[0]) || !isset($result[0]['dn'])) {
1612
+			if (!isset($result[0]) || !isset($result[0]['dn'])) {
1613 1613
 				throw new \Exception('Cannot determine UUID attribute');
1614 1614
 			}
1615 1615
 			$dn = $result[0]['dn'][0];
1616
-			if(!$this->detectUuidAttribute($dn, true)) {
1616
+			if (!$this->detectUuidAttribute($dn, true)) {
1617 1617
 				throw new \Exception('Cannot determine UUID attribute');
1618 1618
 			}
1619 1619
 		} else {
1620 1620
 			// The UUID attribute is either known or an override is given.
1621 1621
 			// By calling this method we ensure that $this->connection->$uuidAttr
1622 1622
 			// is definitely set
1623
-			if(!$this->detectUuidAttribute('', true)) {
1623
+			if (!$this->detectUuidAttribute('', true)) {
1624 1624
 				throw new \Exception('Cannot determine UUID attribute');
1625 1625
 			}
1626 1626
 		}
1627 1627
 
1628 1628
 		$uuidAttr = $this->connection->ldapUuidUserAttribute;
1629
-		if($uuidAttr === 'guid' || $uuidAttr === 'objectguid') {
1629
+		if ($uuidAttr === 'guid' || $uuidAttr === 'objectguid') {
1630 1630
 			$uuid = $this->formatGuid2ForFilterUser($uuid);
1631 1631
 		}
1632 1632
 
1633
-		$filter = $uuidAttr . '=' . $uuid;
1633
+		$filter = $uuidAttr.'='.$uuid;
1634 1634
 		$result = $this->searchUsers($filter, ['dn'], 2);
1635
-		if(is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) {
1635
+		if (is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) {
1636 1636
 			// we put the count into account to make sure that this is
1637 1637
 			// really unique
1638 1638
 			return $result[0]['dn'][0];
@@ -1651,7 +1651,7 @@  discard block
 block discarded – undo
1651 1651
 	 * @return bool true on success, false otherwise
1652 1652
 	 */
1653 1653
 	private function detectUuidAttribute($dn, $isUser = true, $force = false, array $ldapRecord = null) {
1654
-		if($isUser) {
1654
+		if ($isUser) {
1655 1655
 			$uuidAttr     = 'ldapUuidUserAttribute';
1656 1656
 			$uuidOverride = $this->connection->ldapExpertUUIDUserAttr;
1657 1657
 		} else {
@@ -1659,7 +1659,7 @@  discard block
 block discarded – undo
1659 1659
 			$uuidOverride = $this->connection->ldapExpertUUIDGroupAttr;
1660 1660
 		}
1661 1661
 
1662
-		if(($this->connection->$uuidAttr !== 'auto') && !$force) {
1662
+		if (($this->connection->$uuidAttr !== 'auto') && !$force) {
1663 1663
 			return true;
1664 1664
 		}
1665 1665
 
@@ -1668,10 +1668,10 @@  discard block
 block discarded – undo
1668 1668
 			return true;
1669 1669
 		}
1670 1670
 
1671
-		foreach(self::UUID_ATTRIBUTES as $attribute) {
1672
-			if($ldapRecord !== null) {
1671
+		foreach (self::UUID_ATTRIBUTES as $attribute) {
1672
+			if ($ldapRecord !== null) {
1673 1673
 				// we have the info from LDAP already, we don't need to talk to the server again
1674
-				if(isset($ldapRecord[$attribute])) {
1674
+				if (isset($ldapRecord[$attribute])) {
1675 1675
 					$this->connection->$uuidAttr = $attribute;
1676 1676
 					return true;
1677 1677
 				} else {
@@ -1680,7 +1680,7 @@  discard block
 block discarded – undo
1680 1680
 			}
1681 1681
 
1682 1682
 			$value = $this->readAttribute($dn, $attribute);
1683
-			if(is_array($value) && isset($value[0]) && !empty($value[0])) {
1683
+			if (is_array($value) && isset($value[0]) && !empty($value[0])) {
1684 1684
 				\OCP\Util::writeLog(
1685 1685
 					'user_ldap',
1686 1686
 					'Setting '.$attribute.' as '.$uuidAttr,
@@ -1706,7 +1706,7 @@  discard block
 block discarded – undo
1706 1706
 	 * @return bool|string
1707 1707
 	 */
1708 1708
 	public function getUUID($dn, $isUser = true, $ldapRecord = null) {
1709
-		if($isUser) {
1709
+		if ($isUser) {
1710 1710
 			$uuidAttr     = 'ldapUuidUserAttribute';
1711 1711
 			$uuidOverride = $this->connection->ldapExpertUUIDUserAttr;
1712 1712
 		} else {
@@ -1715,10 +1715,10 @@  discard block
 block discarded – undo
1715 1715
 		}
1716 1716
 
1717 1717
 		$uuid = false;
1718
-		if($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) {
1718
+		if ($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) {
1719 1719
 			$attr = $this->connection->$uuidAttr;
1720 1720
 			$uuid = isset($ldapRecord[$attr]) ? $ldapRecord[$attr] : $this->readAttribute($dn, $attr);
1721
-			if( !is_array($uuid)
1721
+			if (!is_array($uuid)
1722 1722
 				&& $uuidOverride !== ''
1723 1723
 				&& $this->detectUuidAttribute($dn, $isUser, true, $ldapRecord))
1724 1724
 			{
@@ -1726,7 +1726,7 @@  discard block
 block discarded – undo
1726 1726
 					? $ldapRecord[$this->connection->$uuidAttr]
1727 1727
 					: $this->readAttribute($dn, $this->connection->$uuidAttr);
1728 1728
 			}
1729
-			if(is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) {
1729
+			if (is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) {
1730 1730
 				$uuid = $uuid[0];
1731 1731
 			}
1732 1732
 		}
@@ -1743,19 +1743,19 @@  discard block
 block discarded – undo
1743 1743
 	private function convertObjectGUID2Str($oguid) {
1744 1744
 		$hex_guid = bin2hex($oguid);
1745 1745
 		$hex_guid_to_guid_str = '';
1746
-		for($k = 1; $k <= 4; ++$k) {
1746
+		for ($k = 1; $k <= 4; ++$k) {
1747 1747
 			$hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2);
1748 1748
 		}
1749 1749
 		$hex_guid_to_guid_str .= '-';
1750
-		for($k = 1; $k <= 2; ++$k) {
1750
+		for ($k = 1; $k <= 2; ++$k) {
1751 1751
 			$hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2);
1752 1752
 		}
1753 1753
 		$hex_guid_to_guid_str .= '-';
1754
-		for($k = 1; $k <= 2; ++$k) {
1754
+		for ($k = 1; $k <= 2; ++$k) {
1755 1755
 			$hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2);
1756 1756
 		}
1757
-		$hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4);
1758
-		$hex_guid_to_guid_str .= '-' . substr($hex_guid, 20);
1757
+		$hex_guid_to_guid_str .= '-'.substr($hex_guid, 16, 4);
1758
+		$hex_guid_to_guid_str .= '-'.substr($hex_guid, 20);
1759 1759
 
1760 1760
 		return strtoupper($hex_guid_to_guid_str);
1761 1761
 	}
@@ -1772,11 +1772,11 @@  discard block
 block discarded – undo
1772 1772
 	 * @return string
1773 1773
 	 */
1774 1774
 	public function formatGuid2ForFilterUser($guid) {
1775
-		if(!is_string($guid)) {
1775
+		if (!is_string($guid)) {
1776 1776
 			throw new \InvalidArgumentException('String expected');
1777 1777
 		}
1778 1778
 		$blocks = explode('-', $guid);
1779
-		if(count($blocks) !== 5) {
1779
+		if (count($blocks) !== 5) {
1780 1780
 			/*
1781 1781
 			 * Why not throw an Exception instead? This method is a utility
1782 1782
 			 * called only when trying to figure out whether a "missing" known
@@ -1789,20 +1789,20 @@  discard block
 block discarded – undo
1789 1789
 			 * user. Instead we write a log message.
1790 1790
 			 */
1791 1791
 			\OC::$server->getLogger()->info(
1792
-				'Passed string does not resemble a valid GUID. Known UUID ' .
1792
+				'Passed string does not resemble a valid GUID. Known UUID '.
1793 1793
 				'({uuid}) probably does not match UUID configuration.',
1794
-				[ 'app' => 'user_ldap', 'uuid' => $guid ]
1794
+				['app' => 'user_ldap', 'uuid' => $guid]
1795 1795
 			);
1796 1796
 			return $guid;
1797 1797
 		}
1798
-		for($i=0; $i < 3; $i++) {
1798
+		for ($i = 0; $i < 3; $i++) {
1799 1799
 			$pairs = str_split($blocks[$i], 2);
1800 1800
 			$pairs = array_reverse($pairs);
1801 1801
 			$blocks[$i] = implode('', $pairs);
1802 1802
 		}
1803
-		for($i=0; $i < 5; $i++) {
1803
+		for ($i = 0; $i < 5; $i++) {
1804 1804
 			$pairs = str_split($blocks[$i], 2);
1805
-			$blocks[$i] = '\\' . implode('\\', $pairs);
1805
+			$blocks[$i] = '\\'.implode('\\', $pairs);
1806 1806
 		}
1807 1807
 		return implode('', $blocks);
1808 1808
 	}
@@ -1816,12 +1816,12 @@  discard block
 block discarded – undo
1816 1816
 		$domainDN = $this->getDomainDNFromDN($dn);
1817 1817
 		$cacheKey = 'getSID-'.$domainDN;
1818 1818
 		$sid = $this->connection->getFromCache($cacheKey);
1819
-		if(!is_null($sid)) {
1819
+		if (!is_null($sid)) {
1820 1820
 			return $sid;
1821 1821
 		}
1822 1822
 
1823 1823
 		$objectSid = $this->readAttribute($domainDN, 'objectsid');
1824
-		if(!is_array($objectSid) || empty($objectSid)) {
1824
+		if (!is_array($objectSid) || empty($objectSid)) {
1825 1825
 			$this->connection->writeToCache($cacheKey, false);
1826 1826
 			return false;
1827 1827
 		}
@@ -1879,12 +1879,12 @@  discard block
 block discarded – undo
1879 1879
 		$belongsToBase = false;
1880 1880
 		$bases = $this->helper->sanitizeDN($bases);
1881 1881
 
1882
-		foreach($bases as $base) {
1882
+		foreach ($bases as $base) {
1883 1883
 			$belongsToBase = true;
1884
-			if(mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8')-mb_strlen($base, 'UTF-8'))) {
1884
+			if (mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8') - mb_strlen($base, 'UTF-8'))) {
1885 1885
 				$belongsToBase = false;
1886 1886
 			}
1887
-			if($belongsToBase) {
1887
+			if ($belongsToBase) {
1888 1888
 				break;
1889 1889
 			}
1890 1890
 		}
@@ -1913,16 +1913,16 @@  discard block
 block discarded – undo
1913 1913
 	 * @return string containing the key or empty if none is cached
1914 1914
 	 */
1915 1915
 	private function getPagedResultCookie($base, $filter, $limit, $offset) {
1916
-		if($offset === 0) {
1916
+		if ($offset === 0) {
1917 1917
 			return '';
1918 1918
 		}
1919 1919
 		$offset -= $limit;
1920 1920
 		//we work with cache here
1921
-		$cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . (int)$limit . '-' . (int)$offset;
1921
+		$cacheKey = 'lc'.crc32($base).'-'.crc32($filter).'-'.(int) $limit.'-'.(int) $offset;
1922 1922
 		$cookie = '';
1923
-		if(isset($this->cookies[$cacheKey])) {
1923
+		if (isset($this->cookies[$cacheKey])) {
1924 1924
 			$cookie = $this->cookies[$cacheKey];
1925
-			if(is_null($cookie)) {
1925
+			if (is_null($cookie)) {
1926 1926
 				$cookie = '';
1927 1927
 			}
1928 1928
 		}
@@ -1940,7 +1940,7 @@  discard block
 block discarded – undo
1940 1940
 	 * @return bool
1941 1941
 	 */
1942 1942
 	public function hasMoreResults() {
1943
-		if(empty($this->lastCookie) && $this->lastCookie !== '0') {
1943
+		if (empty($this->lastCookie) && $this->lastCookie !== '0') {
1944 1944
 			// as in RFC 2696, when all results are returned, the cookie will
1945 1945
 			// be empty.
1946 1946
 			return false;
@@ -1960,8 +1960,8 @@  discard block
 block discarded – undo
1960 1960
 	 */
1961 1961
 	private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) {
1962 1962
 		// allow '0' for 389ds
1963
-		if(!empty($cookie) || $cookie === '0') {
1964
-			$cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . (int)$limit . '-' . (int)$offset;
1963
+		if (!empty($cookie) || $cookie === '0') {
1964
+			$cacheKey = 'lc'.crc32($base).'-'.crc32($filter).'-'.(int) $limit.'-'.(int) $offset;
1965 1965
 			$this->cookies[$cacheKey] = $cookie;
1966 1966
 			$this->lastCookie = $cookie;
1967 1967
 		}
@@ -1989,16 +1989,16 @@  discard block
 block discarded – undo
1989 1989
 	private function initPagedSearch($filter, $bases, $attr, $limit, $offset) {
1990 1990
 		$pagedSearchOK = false;
1991 1991
 		if ($limit !== 0) {
1992
-			$offset = (int)$offset; //can be null
1992
+			$offset = (int) $offset; //can be null
1993 1993
 			\OCP\Util::writeLog('user_ldap',
1994 1994
 				'initializing paged search for  Filter '.$filter.' base '.print_r($bases, true)
1995
-				.' attr '.print_r($attr, true). ' limit ' .$limit.' offset '.$offset,
1995
+				.' attr '.print_r($attr, true).' limit '.$limit.' offset '.$offset,
1996 1996
 				ILogger::DEBUG);
1997 1997
 			//get the cookie from the search for the previous search, required by LDAP
1998
-			foreach($bases as $base) {
1998
+			foreach ($bases as $base) {
1999 1999
 
2000 2000
 				$cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset);
2001
-				if(empty($cookie) && $cookie !== "0" && ($offset > 0)) {
2001
+				if (empty($cookie) && $cookie !== "0" && ($offset > 0)) {
2002 2002
 					// no cookie known from a potential previous search. We need
2003 2003
 					// to start from 0 to come to the desired page. cookie value
2004 2004
 					// of '0' is valid, because 389ds
@@ -2008,17 +2008,17 @@  discard block
 block discarded – undo
2008 2008
 					//still no cookie? obviously, the server does not like us. Let's skip paging efforts.
2009 2009
 					// '0' is valid, because 389ds
2010 2010
 					//TODO: remember this, probably does not change in the next request...
2011
-					if(empty($cookie) && $cookie !== '0') {
2011
+					if (empty($cookie) && $cookie !== '0') {
2012 2012
 						$cookie = null;
2013 2013
 					}
2014 2014
 				}
2015
-				if(!is_null($cookie)) {
2015
+				if (!is_null($cookie)) {
2016 2016
 					//since offset = 0, this is a new search. We abandon other searches that might be ongoing.
2017 2017
 					$this->abandonPagedSearch();
2018 2018
 					$pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult',
2019 2019
 						$this->connection->getConnectionResource(), $limit,
2020 2020
 						false, $cookie);
2021
-					if(!$pagedSearchOK) {
2021
+					if (!$pagedSearchOK) {
2022 2022
 						return false;
2023 2023
 					}
2024 2024
 					\OCP\Util::writeLog('user_ldap', 'Ready for a paged search', ILogger::DEBUG);
@@ -2041,7 +2041,7 @@  discard block
 block discarded – undo
2041 2041
 			$this->abandonPagedSearch();
2042 2042
 			// in case someone set it to 0 … use 500, otherwise no results will
2043 2043
 			// be returned.
2044
-			$pageSize = (int)$this->connection->ldapPagingSize > 0 ? (int)$this->connection->ldapPagingSize : 500;
2044
+			$pageSize = (int) $this->connection->ldapPagingSize > 0 ? (int) $this->connection->ldapPagingSize : 500;
2045 2045
 			$pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult',
2046 2046
 				$this->connection->getConnectionResource(),
2047 2047
 				$pageSize, false, '');
Please login to merge, or discard this patch.