@@ -503,7 +503,7 @@ discard block  | 
                                                    ||
| 503 | 503 | |
| 504 | 504 | /**  | 
                                                        
| 505 | 505 | * returns the internal Nextcloud name for the given LDAP DN of the user, false on DN outside of search DN or failure  | 
                                                        
| 506 | - * @param string $dn the dn of the user object  | 
                                                        |
| 506 | + * @param string $fdn the dn of the user object  | 
                                                        |
| 507 | 507 | * @param string $ldapName optional, the display name of the object  | 
                                                        
| 508 | 508 | * @return string|false with with the name to use in Nextcloud  | 
                                                        
| 509 | 509 | */  | 
                                                        
@@ -789,7 +789,7 @@ discard block  | 
                                                    ||
| 789 | 789 | * the login filter.  | 
                                                        
| 790 | 790 | *  | 
                                                        
| 791 | 791 | * @param string $loginName  | 
                                                        
| 792 | - * @param array $attributes optional, list of attributes to read  | 
                                                        |
| 792 | + * @param string[] $attributes optional, list of attributes to read  | 
                                                        |
| 793 | 793 | * @return array  | 
                                                        
| 794 | 794 | */  | 
                                                        
| 795 | 795 |  	public function fetchUsersByLoginName($loginName, $attributes = array('dn')) { | 
                                                        
@@ -873,7 +873,7 @@ discard block  | 
                                                    ||
| 873 | 873 | |
| 874 | 874 | /**  | 
                                                        
| 875 | 875 | * @param string $filter  | 
                                                        
| 876 | - * @param string|string[] $attr  | 
                                                        |
| 876 | + * @param string[] $attr  | 
                                                        |
| 877 | 877 | * @param int $limit  | 
                                                        
| 878 | 878 | * @param int $offset  | 
                                                        
| 879 | 879 | * @return array  | 
                                                        
@@ -921,7 +921,7 @@ discard block  | 
                                                    ||
| 921 | 921 | |
| 922 | 922 | /**  | 
                                                        
| 923 | 923 | * @param string $filter  | 
                                                        
| 924 | - * @param string|string[] $attr  | 
                                                        |
| 924 | + * @param string[] $attr  | 
                                                        |
| 925 | 925 | * @param int $limit  | 
                                                        
| 926 | 926 | * @param int $offset  | 
                                                        
| 927 | 927 | * @return false|int  | 
                                                        
@@ -1020,7 +1020,7 @@ discard block  | 
                                                    ||
| 1020 | 1020 | /**  | 
                                                        
| 1021 | 1021 | * retrieved. Results will according to the order in the array.  | 
                                                        
| 1022 | 1022 | *  | 
                                                        
| 1023 | - * @param $filter  | 
                                                        |
| 1023 | + * @param string $filter  | 
                                                        |
| 1024 | 1024 | * @param $base  | 
                                                        
| 1025 | 1025 | * @param null $attr  | 
                                                        
| 1026 | 1026 | * @param int $limit optional, maximum results to be counted  | 
                                                        
@@ -1282,7 +1282,7 @@ discard block  | 
                                                    ||
| 1282 | 1282 | |
| 1283 | 1283 | /**  | 
                                                        
| 1284 | 1284 | * @param string $name  | 
                                                        
| 1285 | - * @return bool|mixed|string  | 
                                                        |
| 1285 | + * @return string  | 
                                                        |
| 1286 | 1286 | */  | 
                                                        
| 1287 | 1287 |  	public function sanitizeUsername($name) { | 
                                                        
| 1288 | 1288 |  		if($this->connection->ldapIgnoreNamingRules) { | 
                                                        
@@ -1306,6 +1306,7 @@ discard block  | 
                                                    ||
| 1306 | 1306 | * escapes (user provided) parts for LDAP filter  | 
                                                        
| 1307 | 1307 | * @param string $input, the provided value  | 
                                                        
| 1308 | 1308 | * @param bool $allowAsterisk whether in * at the beginning should be preserved  | 
                                                        
| 1309 | + * @param string $input  | 
                                                        |
| 1309 | 1310 | * @return string the escaped string  | 
                                                        
| 1310 | 1311 | */  | 
                                                        
| 1311 | 1312 |  	public function escapeFilterPart($input, $allowAsterisk = false) { | 
                                                        
@@ -58,1625 +58,1625 @@ discard block  | 
                                                    ||
| 58 | 58 | * @package OCA\User_LDAP  | 
                                                        
| 59 | 59 | */  | 
                                                        
| 60 | 60 |  class Access extends LDAPUtility implements IUserTools { | 
                                                        
| 61 | - const UUID_ATTRIBUTES = ['entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid'];  | 
                                                        |
| 62 | -  | 
                                                        |
| 63 | - /** @var \OCA\User_LDAP\Connection */  | 
                                                        |
| 64 | - public $connection;  | 
                                                        |
| 65 | - /** @var Manager */  | 
                                                        |
| 66 | - public $userManager;  | 
                                                        |
| 67 | - //never ever check this var directly, always use getPagedSearchResultState  | 
                                                        |
| 68 | - protected $pagedSearchedSuccessful;  | 
                                                        |
| 69 | -  | 
                                                        |
| 70 | - /**  | 
                                                        |
| 71 | - * @var string[] $cookies an array of returned Paged Result cookies  | 
                                                        |
| 72 | - */  | 
                                                        |
| 73 | - protected $cookies = array();  | 
                                                        |
| 74 | -  | 
                                                        |
| 75 | - /**  | 
                                                        |
| 76 | - * @var string $lastCookie the last cookie returned from a Paged Results  | 
                                                        |
| 77 | - * operation, defaults to an empty string  | 
                                                        |
| 78 | - */  | 
                                                        |
| 79 | - protected $lastCookie = '';  | 
                                                        |
| 80 | -  | 
                                                        |
| 81 | - /**  | 
                                                        |
| 82 | - * @var AbstractMapping $userMapper  | 
                                                        |
| 83 | - */  | 
                                                        |
| 84 | - protected $userMapper;  | 
                                                        |
| 85 | -  | 
                                                        |
| 86 | - /**  | 
                                                        |
| 87 | - * @var AbstractMapping $userMapper  | 
                                                        |
| 88 | - */  | 
                                                        |
| 89 | - protected $groupMapper;  | 
                                                        |
| 90 | -  | 
                                                        |
| 91 | - /**  | 
                                                        |
| 92 | - * @var \OCA\User_LDAP\Helper  | 
                                                        |
| 93 | - */  | 
                                                        |
| 94 | - private $helper;  | 
                                                        |
| 95 | - /** @var IConfig */  | 
                                                        |
| 96 | - private $config;  | 
                                                        |
| 97 | -  | 
                                                        |
| 98 | - public function __construct(  | 
                                                        |
| 99 | - Connection $connection,  | 
                                                        |
| 100 | - ILDAPWrapper $ldap,  | 
                                                        |
| 101 | - Manager $userManager,  | 
                                                        |
| 102 | - Helper $helper,  | 
                                                        |
| 103 | - IConfig $config  | 
                                                        |
| 104 | -	) { | 
                                                        |
| 105 | - parent::__construct($ldap);  | 
                                                        |
| 106 | - $this->connection = $connection;  | 
                                                        |
| 107 | - $this->userManager = $userManager;  | 
                                                        |
| 108 | - $this->userManager->setLdapAccess($this);  | 
                                                        |
| 109 | - $this->helper = $helper;  | 
                                                        |
| 110 | - $this->config = $config;  | 
                                                        |
| 111 | - }  | 
                                                        |
| 112 | -  | 
                                                        |
| 113 | - /**  | 
                                                        |
| 114 | - * sets the User Mapper  | 
                                                        |
| 115 | - * @param AbstractMapping $mapper  | 
                                                        |
| 116 | - */  | 
                                                        |
| 117 | -	public function setUserMapper(AbstractMapping $mapper) { | 
                                                        |
| 118 | - $this->userMapper = $mapper;  | 
                                                        |
| 119 | - }  | 
                                                        |
| 120 | -  | 
                                                        |
| 121 | - /**  | 
                                                        |
| 122 | - * returns the User Mapper  | 
                                                        |
| 123 | - * @throws \Exception  | 
                                                        |
| 124 | - * @return AbstractMapping  | 
                                                        |
| 125 | - */  | 
                                                        |
| 126 | -	public function getUserMapper() { | 
                                                        |
| 127 | -		if(is_null($this->userMapper)) { | 
                                                        |
| 128 | -			throw new \Exception('UserMapper was not assigned to this Access instance.'); | 
                                                        |
| 129 | - }  | 
                                                        |
| 130 | - return $this->userMapper;  | 
                                                        |
| 131 | - }  | 
                                                        |
| 132 | -  | 
                                                        |
| 133 | - /**  | 
                                                        |
| 134 | - * sets the Group Mapper  | 
                                                        |
| 135 | - * @param AbstractMapping $mapper  | 
                                                        |
| 136 | - */  | 
                                                        |
| 137 | -	public function setGroupMapper(AbstractMapping $mapper) { | 
                                                        |
| 138 | - $this->groupMapper = $mapper;  | 
                                                        |
| 139 | - }  | 
                                                        |
| 140 | -  | 
                                                        |
| 141 | - /**  | 
                                                        |
| 142 | - * returns the Group Mapper  | 
                                                        |
| 143 | - * @throws \Exception  | 
                                                        |
| 144 | - * @return AbstractMapping  | 
                                                        |
| 145 | - */  | 
                                                        |
| 146 | -	public function getGroupMapper() { | 
                                                        |
| 147 | -		if(is_null($this->groupMapper)) { | 
                                                        |
| 148 | -			throw new \Exception('GroupMapper was not assigned to this Access instance.'); | 
                                                        |
| 149 | - }  | 
                                                        |
| 150 | - return $this->groupMapper;  | 
                                                        |
| 151 | - }  | 
                                                        |
| 152 | -  | 
                                                        |
| 153 | - /**  | 
                                                        |
| 154 | - * @return bool  | 
                                                        |
| 155 | - */  | 
                                                        |
| 156 | -	private function checkConnection() { | 
                                                        |
| 157 | - return ($this->connection instanceof Connection);  | 
                                                        |
| 158 | - }  | 
                                                        |
| 159 | -  | 
                                                        |
| 160 | - /**  | 
                                                        |
| 161 | - * returns the Connection instance  | 
                                                        |
| 162 | - * @return \OCA\User_LDAP\Connection  | 
                                                        |
| 163 | - */  | 
                                                        |
| 164 | -	public function getConnection() { | 
                                                        |
| 165 | - return $this->connection;  | 
                                                        |
| 166 | - }  | 
                                                        |
| 167 | -  | 
                                                        |
| 168 | - /**  | 
                                                        |
| 169 | - * reads a given attribute for an LDAP record identified by a DN  | 
                                                        |
| 170 | - * @param string $dn the record in question  | 
                                                        |
| 171 | - * @param string $attr the attribute that shall be retrieved  | 
                                                        |
| 172 | - * if empty, just check the record's existence  | 
                                                        |
| 173 | - * @param string $filter  | 
                                                        |
| 174 | - * @return array|false an array of values on success or an empty  | 
                                                        |
| 175 | - * array if $attr is empty, false otherwise  | 
                                                        |
| 176 | - */  | 
                                                        |
| 177 | -	public function readAttribute($dn, $attr, $filter = 'objectClass=*') { | 
                                                        |
| 178 | -		if(!$this->checkConnection()) { | 
                                                        |
| 179 | -			\OCP\Util::writeLog('user_ldap', | 
                                                        |
| 180 | - 'No LDAP Connector assigned, access impossible for readAttribute.',  | 
                                                        |
| 181 | - \OCP\Util::WARN);  | 
                                                        |
| 182 | - return false;  | 
                                                        |
| 183 | - }  | 
                                                        |
| 184 | - $cr = $this->connection->getConnectionResource();  | 
                                                        |
| 185 | -		if(!$this->ldap->isResource($cr)) { | 
                                                        |
| 186 | - //LDAP not available  | 
                                                        |
| 187 | -			\OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); | 
                                                        |
| 188 | - return false;  | 
                                                        |
| 189 | - }  | 
                                                        |
| 190 | - //Cancel possibly running Paged Results operation, otherwise we run in  | 
                                                        |
| 191 | - //LDAP protocol errors  | 
                                                        |
| 192 | - $this->abandonPagedSearch();  | 
                                                        |
| 193 | - // openLDAP requires that we init a new Paged Search. Not needed by AD,  | 
                                                        |
| 194 | - // but does not hurt either.  | 
                                                        |
| 195 | - $pagingSize = intval($this->connection->ldapPagingSize);  | 
                                                        |
| 196 | - // 0 won't result in replies, small numbers may leave out groups  | 
                                                        |
| 197 | - // (cf. #12306), 500 is default for paging and should work everywhere.  | 
                                                        |
| 198 | - $maxResults = $pagingSize > 20 ? $pagingSize : 500;  | 
                                                        |
| 199 | - $attr = mb_strtolower($attr, 'UTF-8');  | 
                                                        |
| 200 | - // the actual read attribute later may contain parameters on a ranged  | 
                                                        |
| 201 | - // request, e.g. member;range=99-199. Depends on server reply.  | 
                                                        |
| 202 | - $attrToRead = $attr;  | 
                                                        |
| 203 | -  | 
                                                        |
| 204 | - $values = [];  | 
                                                        |
| 205 | - $isRangeRequest = false;  | 
                                                        |
| 206 | -		do { | 
                                                        |
| 207 | - $result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults);  | 
                                                        |
| 208 | -			if(is_bool($result)) { | 
                                                        |
| 209 | - // when an exists request was run and it was successful, an empty  | 
                                                        |
| 210 | - // array must be returned  | 
                                                        |
| 211 | - return $result ? [] : false;  | 
                                                        |
| 212 | - }  | 
                                                        |
| 213 | -  | 
                                                        |
| 214 | -			if (!$isRangeRequest) { | 
                                                        |
| 215 | - $values = $this->extractAttributeValuesFromResult($result, $attr);  | 
                                                        |
| 216 | -				if (!empty($values)) { | 
                                                        |
| 217 | - return $values;  | 
                                                        |
| 218 | - }  | 
                                                        |
| 219 | - }  | 
                                                        |
| 220 | -  | 
                                                        |
| 221 | - $isRangeRequest = false;  | 
                                                        |
| 222 | - $result = $this->extractRangeData($result, $attr);  | 
                                                        |
| 223 | -			if (!empty($result)) { | 
                                                        |
| 224 | - $normalizedResult = $this->extractAttributeValuesFromResult(  | 
                                                        |
| 225 | - [ $attr => $result['values'] ],  | 
                                                        |
| 226 | - $attr  | 
                                                        |
| 227 | - );  | 
                                                        |
| 228 | - $values = array_merge($values, $normalizedResult);  | 
                                                        |
| 229 | -  | 
                                                        |
| 230 | -				if($result['rangeHigh'] === '*') { | 
                                                        |
| 231 | - // when server replies with * as high range value, there are  | 
                                                        |
| 232 | - // no more results left  | 
                                                        |
| 233 | - return $values;  | 
                                                        |
| 234 | -				} else { | 
                                                        |
| 235 | - $low = $result['rangeHigh'] + 1;  | 
                                                        |
| 236 | - $attrToRead = $result['attributeName'] . ';range=' . $low . '-*';  | 
                                                        |
| 237 | - $isRangeRequest = true;  | 
                                                        |
| 238 | - }  | 
                                                        |
| 239 | - }  | 
                                                        |
| 240 | - } while($isRangeRequest);  | 
                                                        |
| 241 | -  | 
                                                        |
| 242 | -		\OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, \OCP\Util::DEBUG); | 
                                                        |
| 243 | - return false;  | 
                                                        |
| 244 | - }  | 
                                                        |
| 245 | -  | 
                                                        |
| 246 | - /**  | 
                                                        |
| 247 | - * Runs an read operation against LDAP  | 
                                                        |
| 248 | - *  | 
                                                        |
| 249 | - * @param resource $cr the LDAP connection  | 
                                                        |
| 250 | - * @param string $dn  | 
                                                        |
| 251 | - * @param string $attribute  | 
                                                        |
| 252 | - * @param string $filter  | 
                                                        |
| 253 | - * @param int $maxResults  | 
                                                        |
| 254 | - * @return array|bool false if there was any error, true if an exists check  | 
                                                        |
| 255 | - * was performed and the requested DN found, array with the  | 
                                                        |
| 256 | - * returned data on a successful usual operation  | 
                                                        |
| 257 | - */  | 
                                                        |
| 258 | -	public function executeRead($cr, $dn, $attribute, $filter, $maxResults) { | 
                                                        |
| 259 | - $this->initPagedSearch($filter, array($dn), array($attribute), $maxResults, 0);  | 
                                                        |
| 260 | - $dn = $this->helper->DNasBaseParameter($dn);  | 
                                                        |
| 261 | -		$rr = @$this->invokeLDAPMethod('read', $cr, $dn, $filter, array($attribute)); | 
                                                        |
| 262 | -		if (!$this->ldap->isResource($rr)) { | 
                                                        |
| 263 | -			if ($attribute !== '') { | 
                                                        |
| 264 | - //do not throw this message on userExists check, irritates  | 
                                                        |
| 265 | -				\OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, \OCP\Util::DEBUG); | 
                                                        |
| 266 | - }  | 
                                                        |
| 267 | - //in case an error occurs , e.g. object does not exist  | 
                                                        |
| 268 | - return false;  | 
                                                        |
| 269 | - }  | 
                                                        |
| 270 | -		if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) { | 
                                                        |
| 271 | -			\OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', \OCP\Util::DEBUG); | 
                                                        |
| 272 | - return true;  | 
                                                        |
| 273 | - }  | 
                                                        |
| 274 | -		$er = $this->invokeLDAPMethod('firstEntry', $cr, $rr); | 
                                                        |
| 275 | -		if (!$this->ldap->isResource($er)) { | 
                                                        |
| 276 | - //did not match the filter, return false  | 
                                                        |
| 277 | - return false;  | 
                                                        |
| 278 | - }  | 
                                                        |
| 279 | - //LDAP attributes are not case sensitive  | 
                                                        |
| 280 | - $result = \OCP\Util::mb_array_change_key_case(  | 
                                                        |
| 281 | -			$this->invokeLDAPMethod('getAttributes', $cr, $er), MB_CASE_LOWER, 'UTF-8'); | 
                                                        |
| 282 | -  | 
                                                        |
| 283 | - return $result;  | 
                                                        |
| 284 | - }  | 
                                                        |
| 285 | -  | 
                                                        |
| 286 | - /**  | 
                                                        |
| 287 | - * Normalizes a result grom getAttributes(), i.e. handles DNs and binary  | 
                                                        |
| 288 | - * data if present.  | 
                                                        |
| 289 | - *  | 
                                                        |
| 290 | - * @param array $result from ILDAPWrapper::getAttributes()  | 
                                                        |
| 291 | - * @param string $attribute the attribute name that was read  | 
                                                        |
| 292 | - * @return string[]  | 
                                                        |
| 293 | - */  | 
                                                        |
| 294 | -	public function extractAttributeValuesFromResult($result, $attribute) { | 
                                                        |
| 295 | - $values = [];  | 
                                                        |
| 296 | -		if(isset($result[$attribute]) && $result[$attribute]['count'] > 0) { | 
                                                        |
| 297 | - $lowercaseAttribute = strtolower($attribute);  | 
                                                        |
| 298 | -			for($i=0;$i<$result[$attribute]['count'];$i++) { | 
                                                        |
| 299 | -				if($this->resemblesDN($attribute)) { | 
                                                        |
| 300 | - $values[] = $this->helper->sanitizeDN($result[$attribute][$i]);  | 
                                                        |
| 301 | -				} elseif($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { | 
                                                        |
| 302 | - $values[] = $this->convertObjectGUID2Str($result[$attribute][$i]);  | 
                                                        |
| 303 | -				} else { | 
                                                        |
| 304 | - $values[] = $result[$attribute][$i];  | 
                                                        |
| 305 | - }  | 
                                                        |
| 306 | - }  | 
                                                        |
| 307 | - }  | 
                                                        |
| 308 | - return $values;  | 
                                                        |
| 309 | - }  | 
                                                        |
| 310 | -  | 
                                                        |
| 311 | - /**  | 
                                                        |
| 312 | - * Attempts to find ranged data in a getAttribute results and extracts the  | 
                                                        |
| 313 | - * returned values as well as information on the range and full attribute  | 
                                                        |
| 314 | - * name for further processing.  | 
                                                        |
| 315 | - *  | 
                                                        |
| 316 | - * @param array $result from ILDAPWrapper::getAttributes()  | 
                                                        |
| 317 | - * @param string $attribute the attribute name that was read. Without ";range=…"  | 
                                                        |
| 318 | - * @return array If a range was detected with keys 'values', 'attributeName',  | 
                                                        |
| 319 | - * 'attributeFull' and 'rangeHigh', otherwise empty.  | 
                                                        |
| 320 | - */  | 
                                                        |
| 321 | -	public function extractRangeData($result, $attribute) { | 
                                                        |
| 322 | - $keys = array_keys($result);  | 
                                                        |
| 323 | -		foreach($keys as $key) { | 
                                                        |
| 324 | -			if($key !== $attribute && strpos($key, $attribute) === 0) { | 
                                                        |
| 325 | -				$queryData = explode(';', $key); | 
                                                        |
| 326 | -				if(strpos($queryData[1], 'range=') === 0) { | 
                                                        |
| 327 | - $high = substr($queryData[1], 1 + strpos($queryData[1], '-'));  | 
                                                        |
| 328 | - $data = [  | 
                                                        |
| 329 | - 'values' => $result[$key],  | 
                                                        |
| 330 | - 'attributeName' => $queryData[0],  | 
                                                        |
| 331 | - 'attributeFull' => $key,  | 
                                                        |
| 332 | - 'rangeHigh' => $high,  | 
                                                        |
| 333 | - ];  | 
                                                        |
| 334 | - return $data;  | 
                                                        |
| 335 | - }  | 
                                                        |
| 336 | - }  | 
                                                        |
| 337 | - }  | 
                                                        |
| 338 | - return [];  | 
                                                        |
| 339 | - }  | 
                                                        |
| 61 | + const UUID_ATTRIBUTES = ['entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid'];  | 
                                                        |
| 62 | +  | 
                                                        |
| 63 | + /** @var \OCA\User_LDAP\Connection */  | 
                                                        |
| 64 | + public $connection;  | 
                                                        |
| 65 | + /** @var Manager */  | 
                                                        |
| 66 | + public $userManager;  | 
                                                        |
| 67 | + //never ever check this var directly, always use getPagedSearchResultState  | 
                                                        |
| 68 | + protected $pagedSearchedSuccessful;  | 
                                                        |
| 69 | +  | 
                                                        |
| 70 | + /**  | 
                                                        |
| 71 | + * @var string[] $cookies an array of returned Paged Result cookies  | 
                                                        |
| 72 | + */  | 
                                                        |
| 73 | + protected $cookies = array();  | 
                                                        |
| 74 | +  | 
                                                        |
| 75 | + /**  | 
                                                        |
| 76 | + * @var string $lastCookie the last cookie returned from a Paged Results  | 
                                                        |
| 77 | + * operation, defaults to an empty string  | 
                                                        |
| 78 | + */  | 
                                                        |
| 79 | + protected $lastCookie = '';  | 
                                                        |
| 80 | +  | 
                                                        |
| 81 | + /**  | 
                                                        |
| 82 | + * @var AbstractMapping $userMapper  | 
                                                        |
| 83 | + */  | 
                                                        |
| 84 | + protected $userMapper;  | 
                                                        |
| 85 | +  | 
                                                        |
| 86 | + /**  | 
                                                        |
| 87 | + * @var AbstractMapping $userMapper  | 
                                                        |
| 88 | + */  | 
                                                        |
| 89 | + protected $groupMapper;  | 
                                                        |
| 90 | +  | 
                                                        |
| 91 | + /**  | 
                                                        |
| 92 | + * @var \OCA\User_LDAP\Helper  | 
                                                        |
| 93 | + */  | 
                                                        |
| 94 | + private $helper;  | 
                                                        |
| 95 | + /** @var IConfig */  | 
                                                        |
| 96 | + private $config;  | 
                                                        |
| 97 | +  | 
                                                        |
| 98 | + public function __construct(  | 
                                                        |
| 99 | + Connection $connection,  | 
                                                        |
| 100 | + ILDAPWrapper $ldap,  | 
                                                        |
| 101 | + Manager $userManager,  | 
                                                        |
| 102 | + Helper $helper,  | 
                                                        |
| 103 | + IConfig $config  | 
                                                        |
| 104 | +    ) { | 
                                                        |
| 105 | + parent::__construct($ldap);  | 
                                                        |
| 106 | + $this->connection = $connection;  | 
                                                        |
| 107 | + $this->userManager = $userManager;  | 
                                                        |
| 108 | + $this->userManager->setLdapAccess($this);  | 
                                                        |
| 109 | + $this->helper = $helper;  | 
                                                        |
| 110 | + $this->config = $config;  | 
                                                        |
| 111 | + }  | 
                                                        |
| 112 | +  | 
                                                        |
| 113 | + /**  | 
                                                        |
| 114 | + * sets the User Mapper  | 
                                                        |
| 115 | + * @param AbstractMapping $mapper  | 
                                                        |
| 116 | + */  | 
                                                        |
| 117 | +    public function setUserMapper(AbstractMapping $mapper) { | 
                                                        |
| 118 | + $this->userMapper = $mapper;  | 
                                                        |
| 119 | + }  | 
                                                        |
| 120 | +  | 
                                                        |
| 121 | + /**  | 
                                                        |
| 122 | + * returns the User Mapper  | 
                                                        |
| 123 | + * @throws \Exception  | 
                                                        |
| 124 | + * @return AbstractMapping  | 
                                                        |
| 125 | + */  | 
                                                        |
| 126 | +    public function getUserMapper() { | 
                                                        |
| 127 | +        if(is_null($this->userMapper)) { | 
                                                        |
| 128 | +            throw new \Exception('UserMapper was not assigned to this Access instance.'); | 
                                                        |
| 129 | + }  | 
                                                        |
| 130 | + return $this->userMapper;  | 
                                                        |
| 131 | + }  | 
                                                        |
| 132 | +  | 
                                                        |
| 133 | + /**  | 
                                                        |
| 134 | + * sets the Group Mapper  | 
                                                        |
| 135 | + * @param AbstractMapping $mapper  | 
                                                        |
| 136 | + */  | 
                                                        |
| 137 | +    public function setGroupMapper(AbstractMapping $mapper) { | 
                                                        |
| 138 | + $this->groupMapper = $mapper;  | 
                                                        |
| 139 | + }  | 
                                                        |
| 140 | +  | 
                                                        |
| 141 | + /**  | 
                                                        |
| 142 | + * returns the Group Mapper  | 
                                                        |
| 143 | + * @throws \Exception  | 
                                                        |
| 144 | + * @return AbstractMapping  | 
                                                        |
| 145 | + */  | 
                                                        |
| 146 | +    public function getGroupMapper() { | 
                                                        |
| 147 | +        if(is_null($this->groupMapper)) { | 
                                                        |
| 148 | +            throw new \Exception('GroupMapper was not assigned to this Access instance.'); | 
                                                        |
| 149 | + }  | 
                                                        |
| 150 | + return $this->groupMapper;  | 
                                                        |
| 151 | + }  | 
                                                        |
| 152 | +  | 
                                                        |
| 153 | + /**  | 
                                                        |
| 154 | + * @return bool  | 
                                                        |
| 155 | + */  | 
                                                        |
| 156 | +    private function checkConnection() { | 
                                                        |
| 157 | + return ($this->connection instanceof Connection);  | 
                                                        |
| 158 | + }  | 
                                                        |
| 159 | +  | 
                                                        |
| 160 | + /**  | 
                                                        |
| 161 | + * returns the Connection instance  | 
                                                        |
| 162 | + * @return \OCA\User_LDAP\Connection  | 
                                                        |
| 163 | + */  | 
                                                        |
| 164 | +    public function getConnection() { | 
                                                        |
| 165 | + return $this->connection;  | 
                                                        |
| 166 | + }  | 
                                                        |
| 167 | +  | 
                                                        |
| 168 | + /**  | 
                                                        |
| 169 | + * reads a given attribute for an LDAP record identified by a DN  | 
                                                        |
| 170 | + * @param string $dn the record in question  | 
                                                        |
| 171 | + * @param string $attr the attribute that shall be retrieved  | 
                                                        |
| 172 | + * if empty, just check the record's existence  | 
                                                        |
| 173 | + * @param string $filter  | 
                                                        |
| 174 | + * @return array|false an array of values on success or an empty  | 
                                                        |
| 175 | + * array if $attr is empty, false otherwise  | 
                                                        |
| 176 | + */  | 
                                                        |
| 177 | +    public function readAttribute($dn, $attr, $filter = 'objectClass=*') { | 
                                                        |
| 178 | +        if(!$this->checkConnection()) { | 
                                                        |
| 179 | +            \OCP\Util::writeLog('user_ldap', | 
                                                        |
| 180 | + 'No LDAP Connector assigned, access impossible for readAttribute.',  | 
                                                        |
| 181 | + \OCP\Util::WARN);  | 
                                                        |
| 182 | + return false;  | 
                                                        |
| 183 | + }  | 
                                                        |
| 184 | + $cr = $this->connection->getConnectionResource();  | 
                                                        |
| 185 | +        if(!$this->ldap->isResource($cr)) { | 
                                                        |
| 186 | + //LDAP not available  | 
                                                        |
| 187 | +            \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); | 
                                                        |
| 188 | + return false;  | 
                                                        |
| 189 | + }  | 
                                                        |
| 190 | + //Cancel possibly running Paged Results operation, otherwise we run in  | 
                                                        |
| 191 | + //LDAP protocol errors  | 
                                                        |
| 192 | + $this->abandonPagedSearch();  | 
                                                        |
| 193 | + // openLDAP requires that we init a new Paged Search. Not needed by AD,  | 
                                                        |
| 194 | + // but does not hurt either.  | 
                                                        |
| 195 | + $pagingSize = intval($this->connection->ldapPagingSize);  | 
                                                        |
| 196 | + // 0 won't result in replies, small numbers may leave out groups  | 
                                                        |
| 197 | + // (cf. #12306), 500 is default for paging and should work everywhere.  | 
                                                        |
| 198 | + $maxResults = $pagingSize > 20 ? $pagingSize : 500;  | 
                                                        |
| 199 | + $attr = mb_strtolower($attr, 'UTF-8');  | 
                                                        |
| 200 | + // the actual read attribute later may contain parameters on a ranged  | 
                                                        |
| 201 | + // request, e.g. member;range=99-199. Depends on server reply.  | 
                                                        |
| 202 | + $attrToRead = $attr;  | 
                                                        |
| 203 | +  | 
                                                        |
| 204 | + $values = [];  | 
                                                        |
| 205 | + $isRangeRequest = false;  | 
                                                        |
| 206 | +        do { | 
                                                        |
| 207 | + $result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults);  | 
                                                        |
| 208 | +            if(is_bool($result)) { | 
                                                        |
| 209 | + // when an exists request was run and it was successful, an empty  | 
                                                        |
| 210 | + // array must be returned  | 
                                                        |
| 211 | + return $result ? [] : false;  | 
                                                        |
| 212 | + }  | 
                                                        |
| 213 | +  | 
                                                        |
| 214 | +            if (!$isRangeRequest) { | 
                                                        |
| 215 | + $values = $this->extractAttributeValuesFromResult($result, $attr);  | 
                                                        |
| 216 | +                if (!empty($values)) { | 
                                                        |
| 217 | + return $values;  | 
                                                        |
| 218 | + }  | 
                                                        |
| 219 | + }  | 
                                                        |
| 220 | +  | 
                                                        |
| 221 | + $isRangeRequest = false;  | 
                                                        |
| 222 | + $result = $this->extractRangeData($result, $attr);  | 
                                                        |
| 223 | +            if (!empty($result)) { | 
                                                        |
| 224 | + $normalizedResult = $this->extractAttributeValuesFromResult(  | 
                                                        |
| 225 | + [ $attr => $result['values'] ],  | 
                                                        |
| 226 | + $attr  | 
                                                        |
| 227 | + );  | 
                                                        |
| 228 | + $values = array_merge($values, $normalizedResult);  | 
                                                        |
| 229 | +  | 
                                                        |
| 230 | +                if($result['rangeHigh'] === '*') { | 
                                                        |
| 231 | + // when server replies with * as high range value, there are  | 
                                                        |
| 232 | + // no more results left  | 
                                                        |
| 233 | + return $values;  | 
                                                        |
| 234 | +                } else { | 
                                                        |
| 235 | + $low = $result['rangeHigh'] + 1;  | 
                                                        |
| 236 | + $attrToRead = $result['attributeName'] . ';range=' . $low . '-*';  | 
                                                        |
| 237 | + $isRangeRequest = true;  | 
                                                        |
| 238 | + }  | 
                                                        |
| 239 | + }  | 
                                                        |
| 240 | + } while($isRangeRequest);  | 
                                                        |
| 241 | +  | 
                                                        |
| 242 | +        \OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, \OCP\Util::DEBUG); | 
                                                        |
| 243 | + return false;  | 
                                                        |
| 244 | + }  | 
                                                        |
| 245 | +  | 
                                                        |
| 246 | + /**  | 
                                                        |
| 247 | + * Runs an read operation against LDAP  | 
                                                        |
| 248 | + *  | 
                                                        |
| 249 | + * @param resource $cr the LDAP connection  | 
                                                        |
| 250 | + * @param string $dn  | 
                                                        |
| 251 | + * @param string $attribute  | 
                                                        |
| 252 | + * @param string $filter  | 
                                                        |
| 253 | + * @param int $maxResults  | 
                                                        |
| 254 | + * @return array|bool false if there was any error, true if an exists check  | 
                                                        |
| 255 | + * was performed and the requested DN found, array with the  | 
                                                        |
| 256 | + * returned data on a successful usual operation  | 
                                                        |
| 257 | + */  | 
                                                        |
| 258 | +    public function executeRead($cr, $dn, $attribute, $filter, $maxResults) { | 
                                                        |
| 259 | + $this->initPagedSearch($filter, array($dn), array($attribute), $maxResults, 0);  | 
                                                        |
| 260 | + $dn = $this->helper->DNasBaseParameter($dn);  | 
                                                        |
| 261 | +        $rr = @$this->invokeLDAPMethod('read', $cr, $dn, $filter, array($attribute)); | 
                                                        |
| 262 | +        if (!$this->ldap->isResource($rr)) { | 
                                                        |
| 263 | +            if ($attribute !== '') { | 
                                                        |
| 264 | + //do not throw this message on userExists check, irritates  | 
                                                        |
| 265 | +                \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, \OCP\Util::DEBUG); | 
                                                        |
| 266 | + }  | 
                                                        |
| 267 | + //in case an error occurs , e.g. object does not exist  | 
                                                        |
| 268 | + return false;  | 
                                                        |
| 269 | + }  | 
                                                        |
| 270 | +        if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) { | 
                                                        |
| 271 | +            \OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', \OCP\Util::DEBUG); | 
                                                        |
| 272 | + return true;  | 
                                                        |
| 273 | + }  | 
                                                        |
| 274 | +        $er = $this->invokeLDAPMethod('firstEntry', $cr, $rr); | 
                                                        |
| 275 | +        if (!$this->ldap->isResource($er)) { | 
                                                        |
| 276 | + //did not match the filter, return false  | 
                                                        |
| 277 | + return false;  | 
                                                        |
| 278 | + }  | 
                                                        |
| 279 | + //LDAP attributes are not case sensitive  | 
                                                        |
| 280 | + $result = \OCP\Util::mb_array_change_key_case(  | 
                                                        |
| 281 | +            $this->invokeLDAPMethod('getAttributes', $cr, $er), MB_CASE_LOWER, 'UTF-8'); | 
                                                        |
| 282 | +  | 
                                                        |
| 283 | + return $result;  | 
                                                        |
| 284 | + }  | 
                                                        |
| 285 | +  | 
                                                        |
| 286 | + /**  | 
                                                        |
| 287 | + * Normalizes a result grom getAttributes(), i.e. handles DNs and binary  | 
                                                        |
| 288 | + * data if present.  | 
                                                        |
| 289 | + *  | 
                                                        |
| 290 | + * @param array $result from ILDAPWrapper::getAttributes()  | 
                                                        |
| 291 | + * @param string $attribute the attribute name that was read  | 
                                                        |
| 292 | + * @return string[]  | 
                                                        |
| 293 | + */  | 
                                                        |
| 294 | +    public function extractAttributeValuesFromResult($result, $attribute) { | 
                                                        |
| 295 | + $values = [];  | 
                                                        |
| 296 | +        if(isset($result[$attribute]) && $result[$attribute]['count'] > 0) { | 
                                                        |
| 297 | + $lowercaseAttribute = strtolower($attribute);  | 
                                                        |
| 298 | +            for($i=0;$i<$result[$attribute]['count'];$i++) { | 
                                                        |
| 299 | +                if($this->resemblesDN($attribute)) { | 
                                                        |
| 300 | + $values[] = $this->helper->sanitizeDN($result[$attribute][$i]);  | 
                                                        |
| 301 | +                } elseif($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { | 
                                                        |
| 302 | + $values[] = $this->convertObjectGUID2Str($result[$attribute][$i]);  | 
                                                        |
| 303 | +                } else { | 
                                                        |
| 304 | + $values[] = $result[$attribute][$i];  | 
                                                        |
| 305 | + }  | 
                                                        |
| 306 | + }  | 
                                                        |
| 307 | + }  | 
                                                        |
| 308 | + return $values;  | 
                                                        |
| 309 | + }  | 
                                                        |
| 310 | +  | 
                                                        |
| 311 | + /**  | 
                                                        |
| 312 | + * Attempts to find ranged data in a getAttribute results and extracts the  | 
                                                        |
| 313 | + * returned values as well as information on the range and full attribute  | 
                                                        |
| 314 | + * name for further processing.  | 
                                                        |
| 315 | + *  | 
                                                        |
| 316 | + * @param array $result from ILDAPWrapper::getAttributes()  | 
                                                        |
| 317 | + * @param string $attribute the attribute name that was read. Without ";range=…"  | 
                                                        |
| 318 | + * @return array If a range was detected with keys 'values', 'attributeName',  | 
                                                        |
| 319 | + * 'attributeFull' and 'rangeHigh', otherwise empty.  | 
                                                        |
| 320 | + */  | 
                                                        |
| 321 | +    public function extractRangeData($result, $attribute) { | 
                                                        |
| 322 | + $keys = array_keys($result);  | 
                                                        |
| 323 | +        foreach($keys as $key) { | 
                                                        |
| 324 | +            if($key !== $attribute && strpos($key, $attribute) === 0) { | 
                                                        |
| 325 | +                $queryData = explode(';', $key); | 
                                                        |
| 326 | +                if(strpos($queryData[1], 'range=') === 0) { | 
                                                        |
| 327 | + $high = substr($queryData[1], 1 + strpos($queryData[1], '-'));  | 
                                                        |
| 328 | + $data = [  | 
                                                        |
| 329 | + 'values' => $result[$key],  | 
                                                        |
| 330 | + 'attributeName' => $queryData[0],  | 
                                                        |
| 331 | + 'attributeFull' => $key,  | 
                                                        |
| 332 | + 'rangeHigh' => $high,  | 
                                                        |
| 333 | + ];  | 
                                                        |
| 334 | + return $data;  | 
                                                        |
| 335 | + }  | 
                                                        |
| 336 | + }  | 
                                                        |
| 337 | + }  | 
                                                        |
| 338 | + return [];  | 
                                                        |
| 339 | + }  | 
                                                        |
| 340 | 340 | |
| 341 | - /**  | 
                                                        |
| 342 | - * Set password for an LDAP user identified by a DN  | 
                                                        |
| 343 | - *  | 
                                                        |
| 344 | - * @param string $userDN the user in question  | 
                                                        |
| 345 | - * @param string $password the new password  | 
                                                        |
| 346 | - * @return bool  | 
                                                        |
| 347 | - * @throws HintException  | 
                                                        |
| 348 | - * @throws \Exception  | 
                                                        |
| 349 | - */  | 
                                                        |
| 350 | -	public function setPassword($userDN, $password) { | 
                                                        |
| 351 | -		if(intval($this->connection->turnOnPasswordChange) !== 1) { | 
                                                        |
| 352 | -			throw new \Exception('LDAP password changes are disabled.'); | 
                                                        |
| 353 | - }  | 
                                                        |
| 354 | - $cr = $this->connection->getConnectionResource();  | 
                                                        |
| 355 | -		if(!$this->ldap->isResource($cr)) { | 
                                                        |
| 356 | - //LDAP not available  | 
                                                        |
| 357 | -			\OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); | 
                                                        |
| 358 | - return false;  | 
                                                        |
| 359 | - }  | 
                                                        |
| 360 | -		try { | 
                                                        |
| 361 | -			return @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password); | 
                                                        |
| 362 | -		} catch(ConstraintViolationException $e) { | 
                                                        |
| 363 | -			throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ').$e->getMessage(), $e->getCode()); | 
                                                        |
| 364 | - }  | 
                                                        |
| 365 | - }  | 
                                                        |
| 366 | -  | 
                                                        |
| 367 | - /**  | 
                                                        |
| 368 | - * checks whether the given attributes value is probably a DN  | 
                                                        |
| 369 | - * @param string $attr the attribute in question  | 
                                                        |
| 370 | - * @return boolean if so true, otherwise false  | 
                                                        |
| 371 | - */  | 
                                                        |
| 372 | -	private function resemblesDN($attr) { | 
                                                        |
| 373 | - $resemblingAttributes = array(  | 
                                                        |
| 374 | - 'dn',  | 
                                                        |
| 375 | - 'uniquemember',  | 
                                                        |
| 376 | - 'member',  | 
                                                        |
| 377 | - // memberOf is an "operational" attribute, without a definition in any RFC  | 
                                                        |
| 378 | - 'memberof'  | 
                                                        |
| 379 | - );  | 
                                                        |
| 380 | - return in_array($attr, $resemblingAttributes);  | 
                                                        |
| 381 | - }  | 
                                                        |
| 382 | -  | 
                                                        |
| 383 | - /**  | 
                                                        |
| 384 | - * checks whether the given string is probably a DN  | 
                                                        |
| 385 | - * @param string $string  | 
                                                        |
| 386 | - * @return boolean  | 
                                                        |
| 387 | - */  | 
                                                        |
| 388 | -	public function stringResemblesDN($string) { | 
                                                        |
| 389 | - $r = $this->ldap->explodeDN($string, 0);  | 
                                                        |
| 390 | - // if exploding a DN succeeds and does not end up in  | 
                                                        |
| 391 | - // an empty array except for $r[count] being 0.  | 
                                                        |
| 392 | - return (is_array($r) && count($r) > 1);  | 
                                                        |
| 393 | - }  | 
                                                        |
| 394 | -  | 
                                                        |
| 395 | - /**  | 
                                                        |
| 396 | - * returns a DN-string that is cleaned from not domain parts, e.g.  | 
                                                        |
| 397 | - * cn=foo,cn=bar,dc=foobar,dc=server,dc=org  | 
                                                        |
| 398 | - * becomes dc=foobar,dc=server,dc=org  | 
                                                        |
| 399 | - * @param string $dn  | 
                                                        |
| 400 | - * @return string  | 
                                                        |
| 401 | - */  | 
                                                        |
| 402 | -	public function getDomainDNFromDN($dn) { | 
                                                        |
| 403 | - $allParts = $this->ldap->explodeDN($dn, 0);  | 
                                                        |
| 404 | -		if($allParts === false) { | 
                                                        |
| 405 | - //not a valid DN  | 
                                                        |
| 406 | - return '';  | 
                                                        |
| 407 | - }  | 
                                                        |
| 408 | - $domainParts = array();  | 
                                                        |
| 409 | - $dcFound = false;  | 
                                                        |
| 410 | -		foreach($allParts as $part) { | 
                                                        |
| 411 | -			if(!$dcFound && strpos($part, 'dc=') === 0) { | 
                                                        |
| 412 | - $dcFound = true;  | 
                                                        |
| 413 | - }  | 
                                                        |
| 414 | -			if($dcFound) { | 
                                                        |
| 415 | - $domainParts[] = $part;  | 
                                                        |
| 416 | - }  | 
                                                        |
| 417 | - }  | 
                                                        |
| 418 | -		$domainDN = implode(',', $domainParts); | 
                                                        |
| 419 | - return $domainDN;  | 
                                                        |
| 420 | - }  | 
                                                        |
| 421 | -  | 
                                                        |
| 422 | - /**  | 
                                                        |
| 423 | - * returns the LDAP DN for the given internal Nextcloud name of the group  | 
                                                        |
| 424 | - * @param string $name the Nextcloud name in question  | 
                                                        |
| 425 | - * @return string|false LDAP DN on success, otherwise false  | 
                                                        |
| 426 | - */  | 
                                                        |
| 427 | -	public function groupname2dn($name) { | 
                                                        |
| 428 | - return $this->groupMapper->getDNByName($name);  | 
                                                        |
| 429 | - }  | 
                                                        |
| 430 | -  | 
                                                        |
| 431 | - /**  | 
                                                        |
| 432 | - * returns the LDAP DN for the given internal Nextcloud name of the user  | 
                                                        |
| 433 | - * @param string $name the Nextcloud name in question  | 
                                                        |
| 434 | - * @return string|false with the LDAP DN on success, otherwise false  | 
                                                        |
| 435 | - */  | 
                                                        |
| 436 | -	public function username2dn($name) { | 
                                                        |
| 437 | - $fdn = $this->userMapper->getDNByName($name);  | 
                                                        |
| 438 | -  | 
                                                        |
| 439 | - //Check whether the DN belongs to the Base, to avoid issues on multi-  | 
                                                        |
| 440 | - //server setups  | 
                                                        |
| 441 | -		if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { | 
                                                        |
| 442 | - return $fdn;  | 
                                                        |
| 443 | - }  | 
                                                        |
| 444 | -  | 
                                                        |
| 445 | - return false;  | 
                                                        |
| 446 | - }  | 
                                                        |
| 447 | -  | 
                                                        |
| 448 | - /**  | 
                                                        |
| 449 | - * returns the internal Nextcloud name for the given LDAP DN of the group, false on DN outside of search DN or failure  | 
                                                        |
| 450 | - * @param string $fdn the dn of the group object  | 
                                                        |
| 451 | - * @param string $ldapName optional, the display name of the object  | 
                                                        |
| 452 | - * @return string|false with the name to use in Nextcloud, false on DN outside of search DN  | 
                                                        |
| 453 | - */  | 
                                                        |
| 454 | -	public function dn2groupname($fdn, $ldapName = null) { | 
                                                        |
| 455 | - //To avoid bypassing the base DN settings under certain circumstances  | 
                                                        |
| 456 | - //with the group support, check whether the provided DN matches one of  | 
                                                        |
| 457 | - //the given Bases  | 
                                                        |
| 458 | -		if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { | 
                                                        |
| 459 | - return false;  | 
                                                        |
| 460 | - }  | 
                                                        |
| 461 | -  | 
                                                        |
| 462 | - return $this->dn2ocname($fdn, $ldapName, false);  | 
                                                        |
| 463 | - }  | 
                                                        |
| 464 | -  | 
                                                        |
| 465 | - /**  | 
                                                        |
| 466 | - * accepts an array of group DNs and tests whether they match the user  | 
                                                        |
| 467 | - * filter by doing read operations against the group entries. Returns an  | 
                                                        |
| 468 | - * array of DNs that match the filter.  | 
                                                        |
| 469 | - *  | 
                                                        |
| 470 | - * @param string[] $groupDNs  | 
                                                        |
| 471 | - * @return string[]  | 
                                                        |
| 472 | - */  | 
                                                        |
| 473 | -	public function groupsMatchFilter($groupDNs) { | 
                                                        |
| 474 | - $validGroupDNs = [];  | 
                                                        |
| 475 | -		foreach($groupDNs as $dn) { | 
                                                        |
| 476 | - $cacheKey = 'groupsMatchFilter-'.$dn;  | 
                                                        |
| 477 | - $groupMatchFilter = $this->connection->getFromCache($cacheKey);  | 
                                                        |
| 478 | -			if(!is_null($groupMatchFilter)) { | 
                                                        |
| 479 | -				if($groupMatchFilter) { | 
                                                        |
| 480 | - $validGroupDNs[] = $dn;  | 
                                                        |
| 481 | - }  | 
                                                        |
| 482 | - continue;  | 
                                                        |
| 483 | - }  | 
                                                        |
| 484 | -  | 
                                                        |
| 485 | - // Check the base DN first. If this is not met already, we don't  | 
                                                        |
| 486 | - // need to ask the server at all.  | 
                                                        |
| 487 | -			if(!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { | 
                                                        |
| 488 | - $this->connection->writeToCache($cacheKey, false);  | 
                                                        |
| 489 | - continue;  | 
                                                        |
| 490 | - }  | 
                                                        |
| 491 | -  | 
                                                        |
| 492 | - $result = $this->readAttribute($dn, 'cn', $this->connection->ldapGroupFilter);  | 
                                                        |
| 493 | -			if(is_array($result)) { | 
                                                        |
| 494 | - $this->connection->writeToCache($cacheKey, true);  | 
                                                        |
| 495 | - $validGroupDNs[] = $dn;  | 
                                                        |
| 496 | -			} else { | 
                                                        |
| 497 | - $this->connection->writeToCache($cacheKey, false);  | 
                                                        |
| 498 | - }  | 
                                                        |
| 499 | -  | 
                                                        |
| 500 | - }  | 
                                                        |
| 501 | - return $validGroupDNs;  | 
                                                        |
| 502 | - }  | 
                                                        |
| 503 | -  | 
                                                        |
| 504 | - /**  | 
                                                        |
| 505 | - * returns the internal Nextcloud name for the given LDAP DN of the user, false on DN outside of search DN or failure  | 
                                                        |
| 506 | - * @param string $dn the dn of the user object  | 
                                                        |
| 507 | - * @param string $ldapName optional, the display name of the object  | 
                                                        |
| 508 | - * @return string|false with with the name to use in Nextcloud  | 
                                                        |
| 509 | - */  | 
                                                        |
| 510 | -	public function dn2username($fdn, $ldapName = null) { | 
                                                        |
| 511 | - //To avoid bypassing the base DN settings under certain circumstances  | 
                                                        |
| 512 | - //with the group support, check whether the provided DN matches one of  | 
                                                        |
| 513 | - //the given Bases  | 
                                                        |
| 514 | -		if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { | 
                                                        |
| 515 | - return false;  | 
                                                        |
| 516 | - }  | 
                                                        |
| 517 | -  | 
                                                        |
| 518 | - return $this->dn2ocname($fdn, $ldapName, true);  | 
                                                        |
| 519 | - }  | 
                                                        |
| 520 | -  | 
                                                        |
| 521 | - /**  | 
                                                        |
| 522 | - * returns an internal Nextcloud name for the given LDAP DN, false on DN outside of search DN  | 
                                                        |
| 523 | - *  | 
                                                        |
| 524 | - * @param string $fdn the dn of the user object  | 
                                                        |
| 525 | - * @param string|null $ldapName optional, the display name of the object  | 
                                                        |
| 526 | - * @param bool $isUser optional, whether it is a user object (otherwise group assumed)  | 
                                                        |
| 527 | - * @param bool|null $newlyMapped  | 
                                                        |
| 528 | - * @param array|null $record  | 
                                                        |
| 529 | - * @return false|string with with the name to use in Nextcloud  | 
                                                        |
| 530 | - */  | 
                                                        |
| 531 | -	public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, array $record = null) { | 
                                                        |
| 532 | - $newlyMapped = false;  | 
                                                        |
| 533 | -		if($isUser) { | 
                                                        |
| 534 | - $mapper = $this->getUserMapper();  | 
                                                        |
| 535 | - $nameAttribute = $this->connection->ldapUserDisplayName;  | 
                                                        |
| 536 | -		} else { | 
                                                        |
| 537 | - $mapper = $this->getGroupMapper();  | 
                                                        |
| 538 | - $nameAttribute = $this->connection->ldapGroupDisplayName;  | 
                                                        |
| 539 | - }  | 
                                                        |
| 540 | -  | 
                                                        |
| 541 | - //let's try to retrieve the Nextcloud name from the mappings table  | 
                                                        |
| 542 | - $ncName = $mapper->getNameByDN($fdn);  | 
                                                        |
| 543 | -		if(is_string($ncName)) { | 
                                                        |
| 544 | - return $ncName;  | 
                                                        |
| 545 | - }  | 
                                                        |
| 546 | -  | 
                                                        |
| 547 | - //second try: get the UUID and check if it is known. Then, update the DN and return the name.  | 
                                                        |
| 548 | - $uuid = $this->getUUID($fdn, $isUser, $record);  | 
                                                        |
| 549 | -		if(is_string($uuid)) { | 
                                                        |
| 550 | - $ncName = $mapper->getNameByUUID($uuid);  | 
                                                        |
| 551 | -			if(is_string($ncName)) { | 
                                                        |
| 552 | - $mapper->setDNbyUUID($fdn, $uuid);  | 
                                                        |
| 553 | - return $ncName;  | 
                                                        |
| 554 | - }  | 
                                                        |
| 555 | -		} else { | 
                                                        |
| 556 | - //If the UUID can't be detected something is foul.  | 
                                                        |
| 557 | -			\OCP\Util::writeLog('user_ldap', 'Cannot determine UUID for '.$fdn.'. Skipping.', \OCP\Util::INFO); | 
                                                        |
| 558 | - return false;  | 
                                                        |
| 559 | - }  | 
                                                        |
| 560 | -  | 
                                                        |
| 561 | -		if(is_null($ldapName)) { | 
                                                        |
| 562 | - $ldapName = $this->readAttribute($fdn, $nameAttribute);  | 
                                                        |
| 563 | -			if(!isset($ldapName[0]) && empty($ldapName[0])) { | 
                                                        |
| 564 | -				\OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.'.', \OCP\Util::INFO); | 
                                                        |
| 565 | - return false;  | 
                                                        |
| 566 | - }  | 
                                                        |
| 567 | - $ldapName = $ldapName[0];  | 
                                                        |
| 568 | - }  | 
                                                        |
| 569 | -  | 
                                                        |
| 570 | -		if($isUser) { | 
                                                        |
| 571 | - $usernameAttribute = strval($this->connection->ldapExpertUsernameAttr);  | 
                                                        |
| 572 | -			if ($usernameAttribute !== '') { | 
                                                        |
| 573 | - $username = $this->readAttribute($fdn, $usernameAttribute);  | 
                                                        |
| 574 | - $username = $username[0];  | 
                                                        |
| 575 | -			} else { | 
                                                        |
| 576 | - $username = $uuid;  | 
                                                        |
| 577 | - }  | 
                                                        |
| 578 | - $intName = $this->sanitizeUsername($username);  | 
                                                        |
| 579 | -		} else { | 
                                                        |
| 580 | - $intName = $ldapName;  | 
                                                        |
| 581 | - }  | 
                                                        |
| 582 | -  | 
                                                        |
| 583 | - //a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups  | 
                                                        |
| 584 | - //disabling Cache is required to avoid that the new user is cached as not-existing in fooExists check  | 
                                                        |
| 585 | - //NOTE: mind, disabling cache affects only this instance! Using it  | 
                                                        |
| 586 | - // outside of core user management will still cache the user as non-existing.  | 
                                                        |
| 587 | - $originalTTL = $this->connection->ldapCacheTTL;  | 
                                                        |
| 588 | -		$this->connection->setConfiguration(array('ldapCacheTTL' => 0)); | 
                                                        |
| 589 | - if(($isUser && !\OCP\User::userExists($intName))  | 
                                                        |
| 590 | -			|| (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) { | 
                                                        |
| 591 | -			if($mapper->map($fdn, $intName, $uuid)) { | 
                                                        |
| 592 | -				$this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); | 
                                                        |
| 593 | - $newlyMapped = true;  | 
                                                        |
| 594 | - return $intName;  | 
                                                        |
| 595 | - }  | 
                                                        |
| 596 | - }  | 
                                                        |
| 597 | -		$this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); | 
                                                        |
| 598 | -  | 
                                                        |
| 599 | - $altName = $this->createAltInternalOwnCloudName($intName, $isUser);  | 
                                                        |
| 600 | -		if(is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { | 
                                                        |
| 601 | - $newlyMapped = true;  | 
                                                        |
| 602 | - return $altName;  | 
                                                        |
| 603 | - }  | 
                                                        |
| 604 | -  | 
                                                        |
| 605 | - //if everything else did not help..  | 
                                                        |
| 606 | -		\OCP\Util::writeLog('user_ldap', 'Could not create unique name for '.$fdn.'.', \OCP\Util::INFO); | 
                                                        |
| 607 | - return false;  | 
                                                        |
| 608 | - }  | 
                                                        |
| 609 | -  | 
                                                        |
| 610 | - /**  | 
                                                        |
| 611 | - * gives back the user names as they are used ownClod internally  | 
                                                        |
| 612 | - * @param array $ldapUsers as returned by fetchList()  | 
                                                        |
| 613 | - * @return array an array with the user names to use in Nextcloud  | 
                                                        |
| 614 | - *  | 
                                                        |
| 615 | - * gives back the user names as they are used ownClod internally  | 
                                                        |
| 616 | - */  | 
                                                        |
| 617 | -	public function nextcloudUserNames($ldapUsers) { | 
                                                        |
| 618 | - return $this->ldap2NextcloudNames($ldapUsers, true);  | 
                                                        |
| 619 | - }  | 
                                                        |
| 620 | -  | 
                                                        |
| 621 | - /**  | 
                                                        |
| 622 | - * gives back the group names as they are used ownClod internally  | 
                                                        |
| 623 | - * @param array $ldapGroups as returned by fetchList()  | 
                                                        |
| 624 | - * @return array an array with the group names to use in Nextcloud  | 
                                                        |
| 625 | - *  | 
                                                        |
| 626 | - * gives back the group names as they are used ownClod internally  | 
                                                        |
| 627 | - */  | 
                                                        |
| 628 | -	public function nextcloudGroupNames($ldapGroups) { | 
                                                        |
| 629 | - return $this->ldap2NextcloudNames($ldapGroups, false);  | 
                                                        |
| 630 | - }  | 
                                                        |
| 631 | -  | 
                                                        |
| 632 | - /**  | 
                                                        |
| 633 | - * @param array $ldapObjects as returned by fetchList()  | 
                                                        |
| 634 | - * @param bool $isUsers  | 
                                                        |
| 635 | - * @return array  | 
                                                        |
| 636 | - */  | 
                                                        |
| 637 | -	private function ldap2NextcloudNames($ldapObjects, $isUsers) { | 
                                                        |
| 638 | -		if($isUsers) { | 
                                                        |
| 639 | - $nameAttribute = $this->connection->ldapUserDisplayName;  | 
                                                        |
| 640 | - $sndAttribute = $this->connection->ldapUserDisplayName2;  | 
                                                        |
| 641 | -		} else { | 
                                                        |
| 642 | - $nameAttribute = $this->connection->ldapGroupDisplayName;  | 
                                                        |
| 643 | - }  | 
                                                        |
| 644 | - $nextcloudNames = array();  | 
                                                        |
| 645 | -  | 
                                                        |
| 646 | -		foreach($ldapObjects as $ldapObject) { | 
                                                        |
| 647 | - $nameByLDAP = null;  | 
                                                        |
| 648 | - if( isset($ldapObject[$nameAttribute])  | 
                                                        |
| 649 | - && is_array($ldapObject[$nameAttribute])  | 
                                                        |
| 650 | - && isset($ldapObject[$nameAttribute][0])  | 
                                                        |
| 651 | -			) { | 
                                                        |
| 652 | - // might be set, but not necessarily. if so, we use it.  | 
                                                        |
| 653 | - $nameByLDAP = $ldapObject[$nameAttribute][0];  | 
                                                        |
| 654 | - }  | 
                                                        |
| 655 | -  | 
                                                        |
| 656 | - $ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers);  | 
                                                        |
| 657 | -			if($ncName) { | 
                                                        |
| 658 | - $nextcloudNames[] = $ncName;  | 
                                                        |
| 659 | -				if($isUsers) { | 
                                                        |
| 660 | - //cache the user names so it does not need to be retrieved  | 
                                                        |
| 661 | - //again later (e.g. sharing dialogue).  | 
                                                        |
| 662 | -					if(is_null($nameByLDAP)) { | 
                                                        |
| 663 | - continue;  | 
                                                        |
| 664 | - }  | 
                                                        |
| 665 | - $sndName = isset($ldapObject[$sndAttribute][0])  | 
                                                        |
| 666 | - ? $ldapObject[$sndAttribute][0] : '';  | 
                                                        |
| 667 | - $this->cacheUserDisplayName($ncName, $nameByLDAP, $sndName);  | 
                                                        |
| 668 | - }  | 
                                                        |
| 669 | - }  | 
                                                        |
| 670 | - }  | 
                                                        |
| 671 | - return $nextcloudNames;  | 
                                                        |
| 672 | - }  | 
                                                        |
| 673 | -  | 
                                                        |
| 674 | - /**  | 
                                                        |
| 675 | - * caches the user display name  | 
                                                        |
| 676 | - * @param string $ocName the internal Nextcloud username  | 
                                                        |
| 677 | - * @param string|false $home the home directory path  | 
                                                        |
| 678 | - */  | 
                                                        |
| 679 | -	public function cacheUserHome($ocName, $home) { | 
                                                        |
| 680 | - $cacheKey = 'getHome'.$ocName;  | 
                                                        |
| 681 | - $this->connection->writeToCache($cacheKey, $home);  | 
                                                        |
| 682 | - }  | 
                                                        |
| 683 | -  | 
                                                        |
| 684 | - /**  | 
                                                        |
| 685 | - * caches a user as existing  | 
                                                        |
| 686 | - * @param string $ocName the internal Nextcloud username  | 
                                                        |
| 687 | - */  | 
                                                        |
| 688 | -	public function cacheUserExists($ocName) { | 
                                                        |
| 689 | -		$this->connection->writeToCache('userExists'.$ocName, true); | 
                                                        |
| 690 | - }  | 
                                                        |
| 691 | -  | 
                                                        |
| 692 | - /**  | 
                                                        |
| 693 | - * caches the user display name  | 
                                                        |
| 694 | - * @param string $ocName the internal Nextcloud username  | 
                                                        |
| 695 | - * @param string $displayName the display name  | 
                                                        |
| 696 | - * @param string $displayName2 the second display name  | 
                                                        |
| 697 | - */  | 
                                                        |
| 698 | -	public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { | 
                                                        |
| 699 | - $user = $this->userManager->get($ocName);  | 
                                                        |
| 700 | -		if($user === null) { | 
                                                        |
| 701 | - return;  | 
                                                        |
| 702 | - }  | 
                                                        |
| 703 | - $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2);  | 
                                                        |
| 704 | - $cacheKeyTrunk = 'getDisplayName';  | 
                                                        |
| 705 | - $this->connection->writeToCache($cacheKeyTrunk.$ocName, $displayName);  | 
                                                        |
| 706 | - }  | 
                                                        |
| 707 | -  | 
                                                        |
| 708 | - /**  | 
                                                        |
| 709 | - * creates a unique name for internal Nextcloud use for users. Don't call it directly.  | 
                                                        |
| 710 | - * @param string $name the display name of the object  | 
                                                        |
| 711 | - * @return string|false with with the name to use in Nextcloud or false if unsuccessful  | 
                                                        |
| 712 | - *  | 
                                                        |
| 713 | - * Instead of using this method directly, call  | 
                                                        |
| 714 | - * createAltInternalOwnCloudName($name, true)  | 
                                                        |
| 715 | - */  | 
                                                        |
| 716 | -	private function _createAltInternalOwnCloudNameForUsers($name) { | 
                                                        |
| 717 | - $attempts = 0;  | 
                                                        |
| 718 | - //while loop is just a precaution. If a name is not generated within  | 
                                                        |
| 719 | - //20 attempts, something else is very wrong. Avoids infinite loop.  | 
                                                        |
| 720 | -		while($attempts < 20){ | 
                                                        |
| 721 | - $altName = $name . '_' . rand(1000,9999);  | 
                                                        |
| 722 | -			if(!\OCP\User::userExists($altName)) { | 
                                                        |
| 723 | - return $altName;  | 
                                                        |
| 724 | - }  | 
                                                        |
| 725 | - $attempts++;  | 
                                                        |
| 726 | - }  | 
                                                        |
| 727 | - return false;  | 
                                                        |
| 728 | - }  | 
                                                        |
| 729 | -  | 
                                                        |
| 730 | - /**  | 
                                                        |
| 731 | - * creates a unique name for internal Nextcloud use for groups. Don't call it directly.  | 
                                                        |
| 732 | - * @param string $name the display name of the object  | 
                                                        |
| 733 | - * @return string|false with with the name to use in Nextcloud or false if unsuccessful.  | 
                                                        |
| 734 | - *  | 
                                                        |
| 735 | - * Instead of using this method directly, call  | 
                                                        |
| 736 | - * createAltInternalOwnCloudName($name, false)  | 
                                                        |
| 737 | - *  | 
                                                        |
| 738 | - * Group names are also used as display names, so we do a sequential  | 
                                                        |
| 739 | - * numbering, e.g. Developers_42 when there are 41 other groups called  | 
                                                        |
| 740 | - * "Developers"  | 
                                                        |
| 741 | - */  | 
                                                        |
| 742 | -	private function _createAltInternalOwnCloudNameForGroups($name) { | 
                                                        |
| 743 | - $usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%');  | 
                                                        |
| 744 | -		if(!($usedNames) || count($usedNames) === 0) { | 
                                                        |
| 745 | - $lastNo = 1; //will become name_2  | 
                                                        |
| 746 | -		} else { | 
                                                        |
| 747 | - natsort($usedNames);  | 
                                                        |
| 748 | - $lastName = array_pop($usedNames);  | 
                                                        |
| 749 | - $lastNo = intval(substr($lastName, strrpos($lastName, '_') + 1));  | 
                                                        |
| 750 | - }  | 
                                                        |
| 751 | - $altName = $name.'_'.strval($lastNo+1);  | 
                                                        |
| 752 | - unset($usedNames);  | 
                                                        |
| 753 | -  | 
                                                        |
| 754 | - $attempts = 1;  | 
                                                        |
| 755 | -		while($attempts < 21){ | 
                                                        |
| 756 | - // Check to be really sure it is unique  | 
                                                        |
| 757 | - // while loop is just a precaution. If a name is not generated within  | 
                                                        |
| 758 | - // 20 attempts, something else is very wrong. Avoids infinite loop.  | 
                                                        |
| 759 | -			if(!\OC::$server->getGroupManager()->groupExists($altName)) { | 
                                                        |
| 760 | - return $altName;  | 
                                                        |
| 761 | - }  | 
                                                        |
| 762 | - $altName = $name . '_' . ($lastNo + $attempts);  | 
                                                        |
| 763 | - $attempts++;  | 
                                                        |
| 764 | - }  | 
                                                        |
| 765 | - return false;  | 
                                                        |
| 766 | - }  | 
                                                        |
| 767 | -  | 
                                                        |
| 768 | - /**  | 
                                                        |
| 769 | - * creates a unique name for internal Nextcloud use.  | 
                                                        |
| 770 | - * @param string $name the display name of the object  | 
                                                        |
| 771 | - * @param boolean $isUser whether name should be created for a user (true) or a group (false)  | 
                                                        |
| 772 | - * @return string|false with with the name to use in Nextcloud or false if unsuccessful  | 
                                                        |
| 773 | - */  | 
                                                        |
| 774 | -	private function createAltInternalOwnCloudName($name, $isUser) { | 
                                                        |
| 775 | - $originalTTL = $this->connection->ldapCacheTTL;  | 
                                                        |
| 776 | -		$this->connection->setConfiguration(array('ldapCacheTTL' => 0)); | 
                                                        |
| 777 | -		if($isUser) { | 
                                                        |
| 778 | - $altName = $this->_createAltInternalOwnCloudNameForUsers($name);  | 
                                                        |
| 779 | -		} else { | 
                                                        |
| 780 | - $altName = $this->_createAltInternalOwnCloudNameForGroups($name);  | 
                                                        |
| 781 | - }  | 
                                                        |
| 782 | -		$this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); | 
                                                        |
| 783 | -  | 
                                                        |
| 784 | - return $altName;  | 
                                                        |
| 785 | - }  | 
                                                        |
| 786 | -  | 
                                                        |
| 787 | - /**  | 
                                                        |
| 788 | - * fetches a list of users according to a provided loginName and utilizing  | 
                                                        |
| 789 | - * the login filter.  | 
                                                        |
| 790 | - *  | 
                                                        |
| 791 | - * @param string $loginName  | 
                                                        |
| 792 | - * @param array $attributes optional, list of attributes to read  | 
                                                        |
| 793 | - * @return array  | 
                                                        |
| 794 | - */  | 
                                                        |
| 795 | -	public function fetchUsersByLoginName($loginName, $attributes = array('dn')) { | 
                                                        |
| 796 | - $loginName = $this->escapeFilterPart($loginName);  | 
                                                        |
| 797 | -		$filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); | 
                                                        |
| 798 | - $users = $this->fetchListOfUsers($filter, $attributes);  | 
                                                        |
| 799 | - return $users;  | 
                                                        |
| 800 | - }  | 
                                                        |
| 801 | -  | 
                                                        |
| 802 | - /**  | 
                                                        |
| 803 | - * counts the number of users according to a provided loginName and  | 
                                                        |
| 804 | - * utilizing the login filter.  | 
                                                        |
| 805 | - *  | 
                                                        |
| 806 | - * @param string $loginName  | 
                                                        |
| 807 | - * @return int  | 
                                                        |
| 808 | - */  | 
                                                        |
| 809 | -	public function countUsersByLoginName($loginName) { | 
                                                        |
| 810 | - $loginName = $this->escapeFilterPart($loginName);  | 
                                                        |
| 811 | -		$filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); | 
                                                        |
| 812 | - $users = $this->countUsers($filter);  | 
                                                        |
| 813 | - return $users;  | 
                                                        |
| 814 | - }  | 
                                                        |
| 815 | -  | 
                                                        |
| 816 | - /**  | 
                                                        |
| 817 | - * @param string $filter  | 
                                                        |
| 818 | - * @param string|string[] $attr  | 
                                                        |
| 819 | - * @param int $limit  | 
                                                        |
| 820 | - * @param int $offset  | 
                                                        |
| 821 | - * @param bool $forceApplyAttributes  | 
                                                        |
| 822 | - * @return array  | 
                                                        |
| 823 | - */  | 
                                                        |
| 824 | -	public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null, $forceApplyAttributes = false) { | 
                                                        |
| 825 | - $ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset);  | 
                                                        |
| 826 | - $recordsToUpdate = $ldapRecords;  | 
                                                        |
| 827 | -		if(!$forceApplyAttributes) { | 
                                                        |
| 828 | - $isBackgroundJobModeAjax = $this->config  | 
                                                        |
| 829 | -					->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; | 
                                                        |
| 830 | -			$recordsToUpdate = array_filter($ldapRecords, function($record) use ($isBackgroundJobModeAjax) { | 
                                                        |
| 831 | - $newlyMapped = false;  | 
                                                        |
| 832 | - $uid = $this->dn2ocname($record['dn'][0], null, true, $newlyMapped, $record);  | 
                                                        |
| 833 | - $this->cacheUserExists($uid);  | 
                                                        |
| 834 | - return ($uid !== false) && ($newlyMapped || $isBackgroundJobModeAjax);  | 
                                                        |
| 835 | - });  | 
                                                        |
| 836 | - }  | 
                                                        |
| 837 | - $this->batchApplyUserAttributes($recordsToUpdate);  | 
                                                        |
| 838 | - return $this->fetchList($ldapRecords, (count($attr) > 1));  | 
                                                        |
| 839 | - }  | 
                                                        |
| 840 | -  | 
                                                        |
| 841 | - /**  | 
                                                        |
| 842 | - * provided with an array of LDAP user records the method will fetch the  | 
                                                        |
| 843 | - * user object and requests it to process the freshly fetched attributes and  | 
                                                        |
| 844 | - * and their values  | 
                                                        |
| 845 | - * @param array $ldapRecords  | 
                                                        |
| 846 | - */  | 
                                                        |
| 847 | -	public function batchApplyUserAttributes(array $ldapRecords){ | 
                                                        |
| 848 | - $displayNameAttribute = strtolower($this->connection->ldapUserDisplayName);  | 
                                                        |
| 849 | -		foreach($ldapRecords as $userRecord) { | 
                                                        |
| 850 | -			if(!isset($userRecord[$displayNameAttribute])) { | 
                                                        |
| 851 | - // displayName is obligatory  | 
                                                        |
| 852 | - continue;  | 
                                                        |
| 853 | - }  | 
                                                        |
| 854 | - $ocName = $this->dn2ocname($userRecord['dn'][0], null, true);  | 
                                                        |
| 855 | -			if($ocName === false) { | 
                                                        |
| 856 | - continue;  | 
                                                        |
| 857 | - }  | 
                                                        |
| 858 | - $user = $this->userManager->get($ocName);  | 
                                                        |
| 859 | -			if($user instanceof OfflineUser) { | 
                                                        |
| 860 | - $user->unmark();  | 
                                                        |
| 861 | - $user = $this->userManager->get($ocName);  | 
                                                        |
| 862 | - }  | 
                                                        |
| 863 | -			if ($user !== null) { | 
                                                        |
| 864 | - $user->processAttributes($userRecord);  | 
                                                        |
| 865 | -			} else { | 
                                                        |
| 866 | - \OC::$server->getLogger()->debug(  | 
                                                        |
| 867 | - "The ldap user manager returned null for $ocName",  | 
                                                        |
| 868 | - ['app'=>'user_ldap']  | 
                                                        |
| 869 | - );  | 
                                                        |
| 870 | - }  | 
                                                        |
| 871 | - }  | 
                                                        |
| 872 | - }  | 
                                                        |
| 873 | -  | 
                                                        |
| 874 | - /**  | 
                                                        |
| 875 | - * @param string $filter  | 
                                                        |
| 876 | - * @param string|string[] $attr  | 
                                                        |
| 877 | - * @param int $limit  | 
                                                        |
| 878 | - * @param int $offset  | 
                                                        |
| 879 | - * @return array  | 
                                                        |
| 880 | - */  | 
                                                        |
| 881 | -	public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) { | 
                                                        |
| 882 | - return $this->fetchList($this->searchGroups($filter, $attr, $limit, $offset), (count($attr) > 1));  | 
                                                        |
| 883 | - }  | 
                                                        |
| 884 | -  | 
                                                        |
| 885 | - /**  | 
                                                        |
| 886 | - * @param array $list  | 
                                                        |
| 887 | - * @param bool $manyAttributes  | 
                                                        |
| 888 | - * @return array  | 
                                                        |
| 889 | - */  | 
                                                        |
| 890 | -	private function fetchList($list, $manyAttributes) { | 
                                                        |
| 891 | -		if(is_array($list)) { | 
                                                        |
| 892 | -			if($manyAttributes) { | 
                                                        |
| 893 | - return $list;  | 
                                                        |
| 894 | -			} else { | 
                                                        |
| 895 | -				$list = array_reduce($list, function($carry, $item) { | 
                                                        |
| 896 | - $attribute = array_keys($item)[0];  | 
                                                        |
| 897 | - $carry[] = $item[$attribute][0];  | 
                                                        |
| 898 | - return $carry;  | 
                                                        |
| 899 | - }, array());  | 
                                                        |
| 900 | - return array_unique($list, SORT_LOCALE_STRING);  | 
                                                        |
| 901 | - }  | 
                                                        |
| 902 | - }  | 
                                                        |
| 903 | -  | 
                                                        |
| 904 | - //error cause actually, maybe throw an exception in future.  | 
                                                        |
| 905 | - return array();  | 
                                                        |
| 906 | - }  | 
                                                        |
| 907 | -  | 
                                                        |
| 908 | - /**  | 
                                                        |
| 909 | - * executes an LDAP search, optimized for Users  | 
                                                        |
| 910 | - * @param string $filter the LDAP filter for the search  | 
                                                        |
| 911 | - * @param string|string[] $attr optional, when a certain attribute shall be filtered out  | 
                                                        |
| 912 | - * @param integer $limit  | 
                                                        |
| 913 | - * @param integer $offset  | 
                                                        |
| 914 | - * @return array with the search result  | 
                                                        |
| 915 | - *  | 
                                                        |
| 916 | - * Executes an LDAP search  | 
                                                        |
| 917 | - */  | 
                                                        |
| 918 | -	public function searchUsers($filter, $attr = null, $limit = null, $offset = null) { | 
                                                        |
| 919 | - return $this->search($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset);  | 
                                                        |
| 920 | - }  | 
                                                        |
| 921 | -  | 
                                                        |
| 922 | - /**  | 
                                                        |
| 923 | - * @param string $filter  | 
                                                        |
| 924 | - * @param string|string[] $attr  | 
                                                        |
| 925 | - * @param int $limit  | 
                                                        |
| 926 | - * @param int $offset  | 
                                                        |
| 927 | - * @return false|int  | 
                                                        |
| 928 | - */  | 
                                                        |
| 929 | -	public function countUsers($filter, $attr = array('dn'), $limit = null, $offset = null) { | 
                                                        |
| 930 | - return $this->count($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset);  | 
                                                        |
| 931 | - }  | 
                                                        |
| 932 | -  | 
                                                        |
| 933 | - /**  | 
                                                        |
| 934 | - * executes an LDAP search, optimized for Groups  | 
                                                        |
| 935 | - * @param string $filter the LDAP filter for the search  | 
                                                        |
| 936 | - * @param string|string[] $attr optional, when a certain attribute shall be filtered out  | 
                                                        |
| 937 | - * @param integer $limit  | 
                                                        |
| 938 | - * @param integer $offset  | 
                                                        |
| 939 | - * @return array with the search result  | 
                                                        |
| 940 | - *  | 
                                                        |
| 941 | - * Executes an LDAP search  | 
                                                        |
| 942 | - */  | 
                                                        |
| 943 | -	public function searchGroups($filter, $attr = null, $limit = null, $offset = null) { | 
                                                        |
| 944 | - return $this->search($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset);  | 
                                                        |
| 945 | - }  | 
                                                        |
| 946 | -  | 
                                                        |
| 947 | - /**  | 
                                                        |
| 948 | - * returns the number of available groups  | 
                                                        |
| 949 | - * @param string $filter the LDAP search filter  | 
                                                        |
| 950 | - * @param string[] $attr optional  | 
                                                        |
| 951 | - * @param int|null $limit  | 
                                                        |
| 952 | - * @param int|null $offset  | 
                                                        |
| 953 | - * @return int|bool  | 
                                                        |
| 954 | - */  | 
                                                        |
| 955 | -	public function countGroups($filter, $attr = array('dn'), $limit = null, $offset = null) { | 
                                                        |
| 956 | - return $this->count($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset);  | 
                                                        |
| 957 | - }  | 
                                                        |
| 958 | -  | 
                                                        |
| 959 | - /**  | 
                                                        |
| 960 | - * returns the number of available objects on the base DN  | 
                                                        |
| 961 | - *  | 
                                                        |
| 962 | - * @param int|null $limit  | 
                                                        |
| 963 | - * @param int|null $offset  | 
                                                        |
| 964 | - * @return int|bool  | 
                                                        |
| 965 | - */  | 
                                                        |
| 966 | -	public function countObjects($limit = null, $offset = null) { | 
                                                        |
| 967 | -		return $this->count('objectclass=*', $this->connection->ldapBase, array('dn'), $limit, $offset); | 
                                                        |
| 968 | - }  | 
                                                        |
| 969 | -  | 
                                                        |
| 970 | - /**  | 
                                                        |
| 971 | - * Returns the LDAP handler  | 
                                                        |
| 972 | - * @throws \OC\ServerNotAvailableException  | 
                                                        |
| 973 | - */  | 
                                                        |
| 974 | -  | 
                                                        |
| 975 | - /**  | 
                                                        |
| 976 | - * @return mixed  | 
                                                        |
| 977 | - * @throws \OC\ServerNotAvailableException  | 
                                                        |
| 978 | - */  | 
                                                        |
| 979 | -	private function invokeLDAPMethod() { | 
                                                        |
| 980 | - $arguments = func_get_args();  | 
                                                        |
| 981 | - $command = array_shift($arguments);  | 
                                                        |
| 982 | - $cr = array_shift($arguments);  | 
                                                        |
| 983 | -		if (!method_exists($this->ldap, $command)) { | 
                                                        |
| 984 | - return null;  | 
                                                        |
| 985 | - }  | 
                                                        |
| 986 | - array_unshift($arguments, $cr);  | 
                                                        |
| 987 | - // php no longer supports call-time pass-by-reference  | 
                                                        |
| 988 | - // thus cannot support controlPagedResultResponse as the third argument  | 
                                                        |
| 989 | - // is a reference  | 
                                                        |
| 990 | -		$doMethod = function () use ($command, &$arguments) { | 
                                                        |
| 991 | -			if ($command == 'controlPagedResultResponse') { | 
                                                        |
| 992 | -				throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.'); | 
                                                        |
| 993 | -			} else { | 
                                                        |
| 994 | - return call_user_func_array(array($this->ldap, $command), $arguments);  | 
                                                        |
| 995 | - }  | 
                                                        |
| 996 | - };  | 
                                                        |
| 997 | -		try { | 
                                                        |
| 998 | - $ret = $doMethod();  | 
                                                        |
| 999 | -		} catch (ServerNotAvailableException $e) { | 
                                                        |
| 1000 | - /* Server connection lost, attempt to reestablish it  | 
                                                        |
| 341 | + /**  | 
                                                        |
| 342 | + * Set password for an LDAP user identified by a DN  | 
                                                        |
| 343 | + *  | 
                                                        |
| 344 | + * @param string $userDN the user in question  | 
                                                        |
| 345 | + * @param string $password the new password  | 
                                                        |
| 346 | + * @return bool  | 
                                                        |
| 347 | + * @throws HintException  | 
                                                        |
| 348 | + * @throws \Exception  | 
                                                        |
| 349 | + */  | 
                                                        |
| 350 | +    public function setPassword($userDN, $password) { | 
                                                        |
| 351 | +        if(intval($this->connection->turnOnPasswordChange) !== 1) { | 
                                                        |
| 352 | +            throw new \Exception('LDAP password changes are disabled.'); | 
                                                        |
| 353 | + }  | 
                                                        |
| 354 | + $cr = $this->connection->getConnectionResource();  | 
                                                        |
| 355 | +        if(!$this->ldap->isResource($cr)) { | 
                                                        |
| 356 | + //LDAP not available  | 
                                                        |
| 357 | +            \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); | 
                                                        |
| 358 | + return false;  | 
                                                        |
| 359 | + }  | 
                                                        |
| 360 | +        try { | 
                                                        |
| 361 | +            return @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password); | 
                                                        |
| 362 | +        } catch(ConstraintViolationException $e) { | 
                                                        |
| 363 | +            throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ').$e->getMessage(), $e->getCode()); | 
                                                        |
| 364 | + }  | 
                                                        |
| 365 | + }  | 
                                                        |
| 366 | +  | 
                                                        |
| 367 | + /**  | 
                                                        |
| 368 | + * checks whether the given attributes value is probably a DN  | 
                                                        |
| 369 | + * @param string $attr the attribute in question  | 
                                                        |
| 370 | + * @return boolean if so true, otherwise false  | 
                                                        |
| 371 | + */  | 
                                                        |
| 372 | +    private function resemblesDN($attr) { | 
                                                        |
| 373 | + $resemblingAttributes = array(  | 
                                                        |
| 374 | + 'dn',  | 
                                                        |
| 375 | + 'uniquemember',  | 
                                                        |
| 376 | + 'member',  | 
                                                        |
| 377 | + // memberOf is an "operational" attribute, without a definition in any RFC  | 
                                                        |
| 378 | + 'memberof'  | 
                                                        |
| 379 | + );  | 
                                                        |
| 380 | + return in_array($attr, $resemblingAttributes);  | 
                                                        |
| 381 | + }  | 
                                                        |
| 382 | +  | 
                                                        |
| 383 | + /**  | 
                                                        |
| 384 | + * checks whether the given string is probably a DN  | 
                                                        |
| 385 | + * @param string $string  | 
                                                        |
| 386 | + * @return boolean  | 
                                                        |
| 387 | + */  | 
                                                        |
| 388 | +    public function stringResemblesDN($string) { | 
                                                        |
| 389 | + $r = $this->ldap->explodeDN($string, 0);  | 
                                                        |
| 390 | + // if exploding a DN succeeds and does not end up in  | 
                                                        |
| 391 | + // an empty array except for $r[count] being 0.  | 
                                                        |
| 392 | + return (is_array($r) && count($r) > 1);  | 
                                                        |
| 393 | + }  | 
                                                        |
| 394 | +  | 
                                                        |
| 395 | + /**  | 
                                                        |
| 396 | + * returns a DN-string that is cleaned from not domain parts, e.g.  | 
                                                        |
| 397 | + * cn=foo,cn=bar,dc=foobar,dc=server,dc=org  | 
                                                        |
| 398 | + * becomes dc=foobar,dc=server,dc=org  | 
                                                        |
| 399 | + * @param string $dn  | 
                                                        |
| 400 | + * @return string  | 
                                                        |
| 401 | + */  | 
                                                        |
| 402 | +    public function getDomainDNFromDN($dn) { | 
                                                        |
| 403 | + $allParts = $this->ldap->explodeDN($dn, 0);  | 
                                                        |
| 404 | +        if($allParts === false) { | 
                                                        |
| 405 | + //not a valid DN  | 
                                                        |
| 406 | + return '';  | 
                                                        |
| 407 | + }  | 
                                                        |
| 408 | + $domainParts = array();  | 
                                                        |
| 409 | + $dcFound = false;  | 
                                                        |
| 410 | +        foreach($allParts as $part) { | 
                                                        |
| 411 | +            if(!$dcFound && strpos($part, 'dc=') === 0) { | 
                                                        |
| 412 | + $dcFound = true;  | 
                                                        |
| 413 | + }  | 
                                                        |
| 414 | +            if($dcFound) { | 
                                                        |
| 415 | + $domainParts[] = $part;  | 
                                                        |
| 416 | + }  | 
                                                        |
| 417 | + }  | 
                                                        |
| 418 | +        $domainDN = implode(',', $domainParts); | 
                                                        |
| 419 | + return $domainDN;  | 
                                                        |
| 420 | + }  | 
                                                        |
| 421 | +  | 
                                                        |
| 422 | + /**  | 
                                                        |
| 423 | + * returns the LDAP DN for the given internal Nextcloud name of the group  | 
                                                        |
| 424 | + * @param string $name the Nextcloud name in question  | 
                                                        |
| 425 | + * @return string|false LDAP DN on success, otherwise false  | 
                                                        |
| 426 | + */  | 
                                                        |
| 427 | +    public function groupname2dn($name) { | 
                                                        |
| 428 | + return $this->groupMapper->getDNByName($name);  | 
                                                        |
| 429 | + }  | 
                                                        |
| 430 | +  | 
                                                        |
| 431 | + /**  | 
                                                        |
| 432 | + * returns the LDAP DN for the given internal Nextcloud name of the user  | 
                                                        |
| 433 | + * @param string $name the Nextcloud name in question  | 
                                                        |
| 434 | + * @return string|false with the LDAP DN on success, otherwise false  | 
                                                        |
| 435 | + */  | 
                                                        |
| 436 | +    public function username2dn($name) { | 
                                                        |
| 437 | + $fdn = $this->userMapper->getDNByName($name);  | 
                                                        |
| 438 | +  | 
                                                        |
| 439 | + //Check whether the DN belongs to the Base, to avoid issues on multi-  | 
                                                        |
| 440 | + //server setups  | 
                                                        |
| 441 | +        if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { | 
                                                        |
| 442 | + return $fdn;  | 
                                                        |
| 443 | + }  | 
                                                        |
| 444 | +  | 
                                                        |
| 445 | + return false;  | 
                                                        |
| 446 | + }  | 
                                                        |
| 447 | +  | 
                                                        |
| 448 | + /**  | 
                                                        |
| 449 | + * returns the internal Nextcloud name for the given LDAP DN of the group, false on DN outside of search DN or failure  | 
                                                        |
| 450 | + * @param string $fdn the dn of the group object  | 
                                                        |
| 451 | + * @param string $ldapName optional, the display name of the object  | 
                                                        |
| 452 | + * @return string|false with the name to use in Nextcloud, false on DN outside of search DN  | 
                                                        |
| 453 | + */  | 
                                                        |
| 454 | +    public function dn2groupname($fdn, $ldapName = null) { | 
                                                        |
| 455 | + //To avoid bypassing the base DN settings under certain circumstances  | 
                                                        |
| 456 | + //with the group support, check whether the provided DN matches one of  | 
                                                        |
| 457 | + //the given Bases  | 
                                                        |
| 458 | +        if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { | 
                                                        |
| 459 | + return false;  | 
                                                        |
| 460 | + }  | 
                                                        |
| 461 | +  | 
                                                        |
| 462 | + return $this->dn2ocname($fdn, $ldapName, false);  | 
                                                        |
| 463 | + }  | 
                                                        |
| 464 | +  | 
                                                        |
| 465 | + /**  | 
                                                        |
| 466 | + * accepts an array of group DNs and tests whether they match the user  | 
                                                        |
| 467 | + * filter by doing read operations against the group entries. Returns an  | 
                                                        |
| 468 | + * array of DNs that match the filter.  | 
                                                        |
| 469 | + *  | 
                                                        |
| 470 | + * @param string[] $groupDNs  | 
                                                        |
| 471 | + * @return string[]  | 
                                                        |
| 472 | + */  | 
                                                        |
| 473 | +    public function groupsMatchFilter($groupDNs) { | 
                                                        |
| 474 | + $validGroupDNs = [];  | 
                                                        |
| 475 | +        foreach($groupDNs as $dn) { | 
                                                        |
| 476 | + $cacheKey = 'groupsMatchFilter-'.$dn;  | 
                                                        |
| 477 | + $groupMatchFilter = $this->connection->getFromCache($cacheKey);  | 
                                                        |
| 478 | +            if(!is_null($groupMatchFilter)) { | 
                                                        |
| 479 | +                if($groupMatchFilter) { | 
                                                        |
| 480 | + $validGroupDNs[] = $dn;  | 
                                                        |
| 481 | + }  | 
                                                        |
| 482 | + continue;  | 
                                                        |
| 483 | + }  | 
                                                        |
| 484 | +  | 
                                                        |
| 485 | + // Check the base DN first. If this is not met already, we don't  | 
                                                        |
| 486 | + // need to ask the server at all.  | 
                                                        |
| 487 | +            if(!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { | 
                                                        |
| 488 | + $this->connection->writeToCache($cacheKey, false);  | 
                                                        |
| 489 | + continue;  | 
                                                        |
| 490 | + }  | 
                                                        |
| 491 | +  | 
                                                        |
| 492 | + $result = $this->readAttribute($dn, 'cn', $this->connection->ldapGroupFilter);  | 
                                                        |
| 493 | +            if(is_array($result)) { | 
                                                        |
| 494 | + $this->connection->writeToCache($cacheKey, true);  | 
                                                        |
| 495 | + $validGroupDNs[] = $dn;  | 
                                                        |
| 496 | +            } else { | 
                                                        |
| 497 | + $this->connection->writeToCache($cacheKey, false);  | 
                                                        |
| 498 | + }  | 
                                                        |
| 499 | +  | 
                                                        |
| 500 | + }  | 
                                                        |
| 501 | + return $validGroupDNs;  | 
                                                        |
| 502 | + }  | 
                                                        |
| 503 | +  | 
                                                        |
| 504 | + /**  | 
                                                        |
| 505 | + * returns the internal Nextcloud name for the given LDAP DN of the user, false on DN outside of search DN or failure  | 
                                                        |
| 506 | + * @param string $dn the dn of the user object  | 
                                                        |
| 507 | + * @param string $ldapName optional, the display name of the object  | 
                                                        |
| 508 | + * @return string|false with with the name to use in Nextcloud  | 
                                                        |
| 509 | + */  | 
                                                        |
| 510 | +    public function dn2username($fdn, $ldapName = null) { | 
                                                        |
| 511 | + //To avoid bypassing the base DN settings under certain circumstances  | 
                                                        |
| 512 | + //with the group support, check whether the provided DN matches one of  | 
                                                        |
| 513 | + //the given Bases  | 
                                                        |
| 514 | +        if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { | 
                                                        |
| 515 | + return false;  | 
                                                        |
| 516 | + }  | 
                                                        |
| 517 | +  | 
                                                        |
| 518 | + return $this->dn2ocname($fdn, $ldapName, true);  | 
                                                        |
| 519 | + }  | 
                                                        |
| 520 | +  | 
                                                        |
| 521 | + /**  | 
                                                        |
| 522 | + * returns an internal Nextcloud name for the given LDAP DN, false on DN outside of search DN  | 
                                                        |
| 523 | + *  | 
                                                        |
| 524 | + * @param string $fdn the dn of the user object  | 
                                                        |
| 525 | + * @param string|null $ldapName optional, the display name of the object  | 
                                                        |
| 526 | + * @param bool $isUser optional, whether it is a user object (otherwise group assumed)  | 
                                                        |
| 527 | + * @param bool|null $newlyMapped  | 
                                                        |
| 528 | + * @param array|null $record  | 
                                                        |
| 529 | + * @return false|string with with the name to use in Nextcloud  | 
                                                        |
| 530 | + */  | 
                                                        |
| 531 | +    public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, array $record = null) { | 
                                                        |
| 532 | + $newlyMapped = false;  | 
                                                        |
| 533 | +        if($isUser) { | 
                                                        |
| 534 | + $mapper = $this->getUserMapper();  | 
                                                        |
| 535 | + $nameAttribute = $this->connection->ldapUserDisplayName;  | 
                                                        |
| 536 | +        } else { | 
                                                        |
| 537 | + $mapper = $this->getGroupMapper();  | 
                                                        |
| 538 | + $nameAttribute = $this->connection->ldapGroupDisplayName;  | 
                                                        |
| 539 | + }  | 
                                                        |
| 540 | +  | 
                                                        |
| 541 | + //let's try to retrieve the Nextcloud name from the mappings table  | 
                                                        |
| 542 | + $ncName = $mapper->getNameByDN($fdn);  | 
                                                        |
| 543 | +        if(is_string($ncName)) { | 
                                                        |
| 544 | + return $ncName;  | 
                                                        |
| 545 | + }  | 
                                                        |
| 546 | +  | 
                                                        |
| 547 | + //second try: get the UUID and check if it is known. Then, update the DN and return the name.  | 
                                                        |
| 548 | + $uuid = $this->getUUID($fdn, $isUser, $record);  | 
                                                        |
| 549 | +        if(is_string($uuid)) { | 
                                                        |
| 550 | + $ncName = $mapper->getNameByUUID($uuid);  | 
                                                        |
| 551 | +            if(is_string($ncName)) { | 
                                                        |
| 552 | + $mapper->setDNbyUUID($fdn, $uuid);  | 
                                                        |
| 553 | + return $ncName;  | 
                                                        |
| 554 | + }  | 
                                                        |
| 555 | +        } else { | 
                                                        |
| 556 | + //If the UUID can't be detected something is foul.  | 
                                                        |
| 557 | +            \OCP\Util::writeLog('user_ldap', 'Cannot determine UUID for '.$fdn.'. Skipping.', \OCP\Util::INFO); | 
                                                        |
| 558 | + return false;  | 
                                                        |
| 559 | + }  | 
                                                        |
| 560 | +  | 
                                                        |
| 561 | +        if(is_null($ldapName)) { | 
                                                        |
| 562 | + $ldapName = $this->readAttribute($fdn, $nameAttribute);  | 
                                                        |
| 563 | +            if(!isset($ldapName[0]) && empty($ldapName[0])) { | 
                                                        |
| 564 | +                \OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.'.', \OCP\Util::INFO); | 
                                                        |
| 565 | + return false;  | 
                                                        |
| 566 | + }  | 
                                                        |
| 567 | + $ldapName = $ldapName[0];  | 
                                                        |
| 568 | + }  | 
                                                        |
| 569 | +  | 
                                                        |
| 570 | +        if($isUser) { | 
                                                        |
| 571 | + $usernameAttribute = strval($this->connection->ldapExpertUsernameAttr);  | 
                                                        |
| 572 | +            if ($usernameAttribute !== '') { | 
                                                        |
| 573 | + $username = $this->readAttribute($fdn, $usernameAttribute);  | 
                                                        |
| 574 | + $username = $username[0];  | 
                                                        |
| 575 | +            } else { | 
                                                        |
| 576 | + $username = $uuid;  | 
                                                        |
| 577 | + }  | 
                                                        |
| 578 | + $intName = $this->sanitizeUsername($username);  | 
                                                        |
| 579 | +        } else { | 
                                                        |
| 580 | + $intName = $ldapName;  | 
                                                        |
| 581 | + }  | 
                                                        |
| 582 | +  | 
                                                        |
| 583 | + //a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups  | 
                                                        |
| 584 | + //disabling Cache is required to avoid that the new user is cached as not-existing in fooExists check  | 
                                                        |
| 585 | + //NOTE: mind, disabling cache affects only this instance! Using it  | 
                                                        |
| 586 | + // outside of core user management will still cache the user as non-existing.  | 
                                                        |
| 587 | + $originalTTL = $this->connection->ldapCacheTTL;  | 
                                                        |
| 588 | +        $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); | 
                                                        |
| 589 | + if(($isUser && !\OCP\User::userExists($intName))  | 
                                                        |
| 590 | +            || (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) { | 
                                                        |
| 591 | +            if($mapper->map($fdn, $intName, $uuid)) { | 
                                                        |
| 592 | +                $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); | 
                                                        |
| 593 | + $newlyMapped = true;  | 
                                                        |
| 594 | + return $intName;  | 
                                                        |
| 595 | + }  | 
                                                        |
| 596 | + }  | 
                                                        |
| 597 | +        $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); | 
                                                        |
| 598 | +  | 
                                                        |
| 599 | + $altName = $this->createAltInternalOwnCloudName($intName, $isUser);  | 
                                                        |
| 600 | +        if(is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { | 
                                                        |
| 601 | + $newlyMapped = true;  | 
                                                        |
| 602 | + return $altName;  | 
                                                        |
| 603 | + }  | 
                                                        |
| 604 | +  | 
                                                        |
| 605 | + //if everything else did not help..  | 
                                                        |
| 606 | +        \OCP\Util::writeLog('user_ldap', 'Could not create unique name for '.$fdn.'.', \OCP\Util::INFO); | 
                                                        |
| 607 | + return false;  | 
                                                        |
| 608 | + }  | 
                                                        |
| 609 | +  | 
                                                        |
| 610 | + /**  | 
                                                        |
| 611 | + * gives back the user names as they are used ownClod internally  | 
                                                        |
| 612 | + * @param array $ldapUsers as returned by fetchList()  | 
                                                        |
| 613 | + * @return array an array with the user names to use in Nextcloud  | 
                                                        |
| 614 | + *  | 
                                                        |
| 615 | + * gives back the user names as they are used ownClod internally  | 
                                                        |
| 616 | + */  | 
                                                        |
| 617 | +    public function nextcloudUserNames($ldapUsers) { | 
                                                        |
| 618 | + return $this->ldap2NextcloudNames($ldapUsers, true);  | 
                                                        |
| 619 | + }  | 
                                                        |
| 620 | +  | 
                                                        |
| 621 | + /**  | 
                                                        |
| 622 | + * gives back the group names as they are used ownClod internally  | 
                                                        |
| 623 | + * @param array $ldapGroups as returned by fetchList()  | 
                                                        |
| 624 | + * @return array an array with the group names to use in Nextcloud  | 
                                                        |
| 625 | + *  | 
                                                        |
| 626 | + * gives back the group names as they are used ownClod internally  | 
                                                        |
| 627 | + */  | 
                                                        |
| 628 | +    public function nextcloudGroupNames($ldapGroups) { | 
                                                        |
| 629 | + return $this->ldap2NextcloudNames($ldapGroups, false);  | 
                                                        |
| 630 | + }  | 
                                                        |
| 631 | +  | 
                                                        |
| 632 | + /**  | 
                                                        |
| 633 | + * @param array $ldapObjects as returned by fetchList()  | 
                                                        |
| 634 | + * @param bool $isUsers  | 
                                                        |
| 635 | + * @return array  | 
                                                        |
| 636 | + */  | 
                                                        |
| 637 | +    private function ldap2NextcloudNames($ldapObjects, $isUsers) { | 
                                                        |
| 638 | +        if($isUsers) { | 
                                                        |
| 639 | + $nameAttribute = $this->connection->ldapUserDisplayName;  | 
                                                        |
| 640 | + $sndAttribute = $this->connection->ldapUserDisplayName2;  | 
                                                        |
| 641 | +        } else { | 
                                                        |
| 642 | + $nameAttribute = $this->connection->ldapGroupDisplayName;  | 
                                                        |
| 643 | + }  | 
                                                        |
| 644 | + $nextcloudNames = array();  | 
                                                        |
| 645 | +  | 
                                                        |
| 646 | +        foreach($ldapObjects as $ldapObject) { | 
                                                        |
| 647 | + $nameByLDAP = null;  | 
                                                        |
| 648 | + if( isset($ldapObject[$nameAttribute])  | 
                                                        |
| 649 | + && is_array($ldapObject[$nameAttribute])  | 
                                                        |
| 650 | + && isset($ldapObject[$nameAttribute][0])  | 
                                                        |
| 651 | +            ) { | 
                                                        |
| 652 | + // might be set, but not necessarily. if so, we use it.  | 
                                                        |
| 653 | + $nameByLDAP = $ldapObject[$nameAttribute][0];  | 
                                                        |
| 654 | + }  | 
                                                        |
| 655 | +  | 
                                                        |
| 656 | + $ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers);  | 
                                                        |
| 657 | +            if($ncName) { | 
                                                        |
| 658 | + $nextcloudNames[] = $ncName;  | 
                                                        |
| 659 | +                if($isUsers) { | 
                                                        |
| 660 | + //cache the user names so it does not need to be retrieved  | 
                                                        |
| 661 | + //again later (e.g. sharing dialogue).  | 
                                                        |
| 662 | +                    if(is_null($nameByLDAP)) { | 
                                                        |
| 663 | + continue;  | 
                                                        |
| 664 | + }  | 
                                                        |
| 665 | + $sndName = isset($ldapObject[$sndAttribute][0])  | 
                                                        |
| 666 | + ? $ldapObject[$sndAttribute][0] : '';  | 
                                                        |
| 667 | + $this->cacheUserDisplayName($ncName, $nameByLDAP, $sndName);  | 
                                                        |
| 668 | + }  | 
                                                        |
| 669 | + }  | 
                                                        |
| 670 | + }  | 
                                                        |
| 671 | + return $nextcloudNames;  | 
                                                        |
| 672 | + }  | 
                                                        |
| 673 | +  | 
                                                        |
| 674 | + /**  | 
                                                        |
| 675 | + * caches the user display name  | 
                                                        |
| 676 | + * @param string $ocName the internal Nextcloud username  | 
                                                        |
| 677 | + * @param string|false $home the home directory path  | 
                                                        |
| 678 | + */  | 
                                                        |
| 679 | +    public function cacheUserHome($ocName, $home) { | 
                                                        |
| 680 | + $cacheKey = 'getHome'.$ocName;  | 
                                                        |
| 681 | + $this->connection->writeToCache($cacheKey, $home);  | 
                                                        |
| 682 | + }  | 
                                                        |
| 683 | +  | 
                                                        |
| 684 | + /**  | 
                                                        |
| 685 | + * caches a user as existing  | 
                                                        |
| 686 | + * @param string $ocName the internal Nextcloud username  | 
                                                        |
| 687 | + */  | 
                                                        |
| 688 | +    public function cacheUserExists($ocName) { | 
                                                        |
| 689 | +        $this->connection->writeToCache('userExists'.$ocName, true); | 
                                                        |
| 690 | + }  | 
                                                        |
| 691 | +  | 
                                                        |
| 692 | + /**  | 
                                                        |
| 693 | + * caches the user display name  | 
                                                        |
| 694 | + * @param string $ocName the internal Nextcloud username  | 
                                                        |
| 695 | + * @param string $displayName the display name  | 
                                                        |
| 696 | + * @param string $displayName2 the second display name  | 
                                                        |
| 697 | + */  | 
                                                        |
| 698 | +    public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { | 
                                                        |
| 699 | + $user = $this->userManager->get($ocName);  | 
                                                        |
| 700 | +        if($user === null) { | 
                                                        |
| 701 | + return;  | 
                                                        |
| 702 | + }  | 
                                                        |
| 703 | + $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2);  | 
                                                        |
| 704 | + $cacheKeyTrunk = 'getDisplayName';  | 
                                                        |
| 705 | + $this->connection->writeToCache($cacheKeyTrunk.$ocName, $displayName);  | 
                                                        |
| 706 | + }  | 
                                                        |
| 707 | +  | 
                                                        |
| 708 | + /**  | 
                                                        |
| 709 | + * creates a unique name for internal Nextcloud use for users. Don't call it directly.  | 
                                                        |
| 710 | + * @param string $name the display name of the object  | 
                                                        |
| 711 | + * @return string|false with with the name to use in Nextcloud or false if unsuccessful  | 
                                                        |
| 712 | + *  | 
                                                        |
| 713 | + * Instead of using this method directly, call  | 
                                                        |
| 714 | + * createAltInternalOwnCloudName($name, true)  | 
                                                        |
| 715 | + */  | 
                                                        |
| 716 | +    private function _createAltInternalOwnCloudNameForUsers($name) { | 
                                                        |
| 717 | + $attempts = 0;  | 
                                                        |
| 718 | + //while loop is just a precaution. If a name is not generated within  | 
                                                        |
| 719 | + //20 attempts, something else is very wrong. Avoids infinite loop.  | 
                                                        |
| 720 | +        while($attempts < 20){ | 
                                                        |
| 721 | + $altName = $name . '_' . rand(1000,9999);  | 
                                                        |
| 722 | +            if(!\OCP\User::userExists($altName)) { | 
                                                        |
| 723 | + return $altName;  | 
                                                        |
| 724 | + }  | 
                                                        |
| 725 | + $attempts++;  | 
                                                        |
| 726 | + }  | 
                                                        |
| 727 | + return false;  | 
                                                        |
| 728 | + }  | 
                                                        |
| 729 | +  | 
                                                        |
| 730 | + /**  | 
                                                        |
| 731 | + * creates a unique name for internal Nextcloud use for groups. Don't call it directly.  | 
                                                        |
| 732 | + * @param string $name the display name of the object  | 
                                                        |
| 733 | + * @return string|false with with the name to use in Nextcloud or false if unsuccessful.  | 
                                                        |
| 734 | + *  | 
                                                        |
| 735 | + * Instead of using this method directly, call  | 
                                                        |
| 736 | + * createAltInternalOwnCloudName($name, false)  | 
                                                        |
| 737 | + *  | 
                                                        |
| 738 | + * Group names are also used as display names, so we do a sequential  | 
                                                        |
| 739 | + * numbering, e.g. Developers_42 when there are 41 other groups called  | 
                                                        |
| 740 | + * "Developers"  | 
                                                        |
| 741 | + */  | 
                                                        |
| 742 | +    private function _createAltInternalOwnCloudNameForGroups($name) { | 
                                                        |
| 743 | + $usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%');  | 
                                                        |
| 744 | +        if(!($usedNames) || count($usedNames) === 0) { | 
                                                        |
| 745 | + $lastNo = 1; //will become name_2  | 
                                                        |
| 746 | +        } else { | 
                                                        |
| 747 | + natsort($usedNames);  | 
                                                        |
| 748 | + $lastName = array_pop($usedNames);  | 
                                                        |
| 749 | + $lastNo = intval(substr($lastName, strrpos($lastName, '_') + 1));  | 
                                                        |
| 750 | + }  | 
                                                        |
| 751 | + $altName = $name.'_'.strval($lastNo+1);  | 
                                                        |
| 752 | + unset($usedNames);  | 
                                                        |
| 753 | +  | 
                                                        |
| 754 | + $attempts = 1;  | 
                                                        |
| 755 | +        while($attempts < 21){ | 
                                                        |
| 756 | + // Check to be really sure it is unique  | 
                                                        |
| 757 | + // while loop is just a precaution. If a name is not generated within  | 
                                                        |
| 758 | + // 20 attempts, something else is very wrong. Avoids infinite loop.  | 
                                                        |
| 759 | +            if(!\OC::$server->getGroupManager()->groupExists($altName)) { | 
                                                        |
| 760 | + return $altName;  | 
                                                        |
| 761 | + }  | 
                                                        |
| 762 | + $altName = $name . '_' . ($lastNo + $attempts);  | 
                                                        |
| 763 | + $attempts++;  | 
                                                        |
| 764 | + }  | 
                                                        |
| 765 | + return false;  | 
                                                        |
| 766 | + }  | 
                                                        |
| 767 | +  | 
                                                        |
| 768 | + /**  | 
                                                        |
| 769 | + * creates a unique name for internal Nextcloud use.  | 
                                                        |
| 770 | + * @param string $name the display name of the object  | 
                                                        |
| 771 | + * @param boolean $isUser whether name should be created for a user (true) or a group (false)  | 
                                                        |
| 772 | + * @return string|false with with the name to use in Nextcloud or false if unsuccessful  | 
                                                        |
| 773 | + */  | 
                                                        |
| 774 | +    private function createAltInternalOwnCloudName($name, $isUser) { | 
                                                        |
| 775 | + $originalTTL = $this->connection->ldapCacheTTL;  | 
                                                        |
| 776 | +        $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); | 
                                                        |
| 777 | +        if($isUser) { | 
                                                        |
| 778 | + $altName = $this->_createAltInternalOwnCloudNameForUsers($name);  | 
                                                        |
| 779 | +        } else { | 
                                                        |
| 780 | + $altName = $this->_createAltInternalOwnCloudNameForGroups($name);  | 
                                                        |
| 781 | + }  | 
                                                        |
| 782 | +        $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); | 
                                                        |
| 783 | +  | 
                                                        |
| 784 | + return $altName;  | 
                                                        |
| 785 | + }  | 
                                                        |
| 786 | +  | 
                                                        |
| 787 | + /**  | 
                                                        |
| 788 | + * fetches a list of users according to a provided loginName and utilizing  | 
                                                        |
| 789 | + * the login filter.  | 
                                                        |
| 790 | + *  | 
                                                        |
| 791 | + * @param string $loginName  | 
                                                        |
| 792 | + * @param array $attributes optional, list of attributes to read  | 
                                                        |
| 793 | + * @return array  | 
                                                        |
| 794 | + */  | 
                                                        |
| 795 | +    public function fetchUsersByLoginName($loginName, $attributes = array('dn')) { | 
                                                        |
| 796 | + $loginName = $this->escapeFilterPart($loginName);  | 
                                                        |
| 797 | +        $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); | 
                                                        |
| 798 | + $users = $this->fetchListOfUsers($filter, $attributes);  | 
                                                        |
| 799 | + return $users;  | 
                                                        |
| 800 | + }  | 
                                                        |
| 801 | +  | 
                                                        |
| 802 | + /**  | 
                                                        |
| 803 | + * counts the number of users according to a provided loginName and  | 
                                                        |
| 804 | + * utilizing the login filter.  | 
                                                        |
| 805 | + *  | 
                                                        |
| 806 | + * @param string $loginName  | 
                                                        |
| 807 | + * @return int  | 
                                                        |
| 808 | + */  | 
                                                        |
| 809 | +    public function countUsersByLoginName($loginName) { | 
                                                        |
| 810 | + $loginName = $this->escapeFilterPart($loginName);  | 
                                                        |
| 811 | +        $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); | 
                                                        |
| 812 | + $users = $this->countUsers($filter);  | 
                                                        |
| 813 | + return $users;  | 
                                                        |
| 814 | + }  | 
                                                        |
| 815 | +  | 
                                                        |
| 816 | + /**  | 
                                                        |
| 817 | + * @param string $filter  | 
                                                        |
| 818 | + * @param string|string[] $attr  | 
                                                        |
| 819 | + * @param int $limit  | 
                                                        |
| 820 | + * @param int $offset  | 
                                                        |
| 821 | + * @param bool $forceApplyAttributes  | 
                                                        |
| 822 | + * @return array  | 
                                                        |
| 823 | + */  | 
                                                        |
| 824 | +    public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null, $forceApplyAttributes = false) { | 
                                                        |
| 825 | + $ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset);  | 
                                                        |
| 826 | + $recordsToUpdate = $ldapRecords;  | 
                                                        |
| 827 | +        if(!$forceApplyAttributes) { | 
                                                        |
| 828 | + $isBackgroundJobModeAjax = $this->config  | 
                                                        |
| 829 | +                    ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; | 
                                                        |
| 830 | +            $recordsToUpdate = array_filter($ldapRecords, function($record) use ($isBackgroundJobModeAjax) { | 
                                                        |
| 831 | + $newlyMapped = false;  | 
                                                        |
| 832 | + $uid = $this->dn2ocname($record['dn'][0], null, true, $newlyMapped, $record);  | 
                                                        |
| 833 | + $this->cacheUserExists($uid);  | 
                                                        |
| 834 | + return ($uid !== false) && ($newlyMapped || $isBackgroundJobModeAjax);  | 
                                                        |
| 835 | + });  | 
                                                        |
| 836 | + }  | 
                                                        |
| 837 | + $this->batchApplyUserAttributes($recordsToUpdate);  | 
                                                        |
| 838 | + return $this->fetchList($ldapRecords, (count($attr) > 1));  | 
                                                        |
| 839 | + }  | 
                                                        |
| 840 | +  | 
                                                        |
| 841 | + /**  | 
                                                        |
| 842 | + * provided with an array of LDAP user records the method will fetch the  | 
                                                        |
| 843 | + * user object and requests it to process the freshly fetched attributes and  | 
                                                        |
| 844 | + * and their values  | 
                                                        |
| 845 | + * @param array $ldapRecords  | 
                                                        |
| 846 | + */  | 
                                                        |
| 847 | +    public function batchApplyUserAttributes(array $ldapRecords){ | 
                                                        |
| 848 | + $displayNameAttribute = strtolower($this->connection->ldapUserDisplayName);  | 
                                                        |
| 849 | +        foreach($ldapRecords as $userRecord) { | 
                                                        |
| 850 | +            if(!isset($userRecord[$displayNameAttribute])) { | 
                                                        |
| 851 | + // displayName is obligatory  | 
                                                        |
| 852 | + continue;  | 
                                                        |
| 853 | + }  | 
                                                        |
| 854 | + $ocName = $this->dn2ocname($userRecord['dn'][0], null, true);  | 
                                                        |
| 855 | +            if($ocName === false) { | 
                                                        |
| 856 | + continue;  | 
                                                        |
| 857 | + }  | 
                                                        |
| 858 | + $user = $this->userManager->get($ocName);  | 
                                                        |
| 859 | +            if($user instanceof OfflineUser) { | 
                                                        |
| 860 | + $user->unmark();  | 
                                                        |
| 861 | + $user = $this->userManager->get($ocName);  | 
                                                        |
| 862 | + }  | 
                                                        |
| 863 | +            if ($user !== null) { | 
                                                        |
| 864 | + $user->processAttributes($userRecord);  | 
                                                        |
| 865 | +            } else { | 
                                                        |
| 866 | + \OC::$server->getLogger()->debug(  | 
                                                        |
| 867 | + "The ldap user manager returned null for $ocName",  | 
                                                        |
| 868 | + ['app'=>'user_ldap']  | 
                                                        |
| 869 | + );  | 
                                                        |
| 870 | + }  | 
                                                        |
| 871 | + }  | 
                                                        |
| 872 | + }  | 
                                                        |
| 873 | +  | 
                                                        |
| 874 | + /**  | 
                                                        |
| 875 | + * @param string $filter  | 
                                                        |
| 876 | + * @param string|string[] $attr  | 
                                                        |
| 877 | + * @param int $limit  | 
                                                        |
| 878 | + * @param int $offset  | 
                                                        |
| 879 | + * @return array  | 
                                                        |
| 880 | + */  | 
                                                        |
| 881 | +    public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) { | 
                                                        |
| 882 | + return $this->fetchList($this->searchGroups($filter, $attr, $limit, $offset), (count($attr) > 1));  | 
                                                        |
| 883 | + }  | 
                                                        |
| 884 | +  | 
                                                        |
| 885 | + /**  | 
                                                        |
| 886 | + * @param array $list  | 
                                                        |
| 887 | + * @param bool $manyAttributes  | 
                                                        |
| 888 | + * @return array  | 
                                                        |
| 889 | + */  | 
                                                        |
| 890 | +    private function fetchList($list, $manyAttributes) { | 
                                                        |
| 891 | +        if(is_array($list)) { | 
                                                        |
| 892 | +            if($manyAttributes) { | 
                                                        |
| 893 | + return $list;  | 
                                                        |
| 894 | +            } else { | 
                                                        |
| 895 | +                $list = array_reduce($list, function($carry, $item) { | 
                                                        |
| 896 | + $attribute = array_keys($item)[0];  | 
                                                        |
| 897 | + $carry[] = $item[$attribute][0];  | 
                                                        |
| 898 | + return $carry;  | 
                                                        |
| 899 | + }, array());  | 
                                                        |
| 900 | + return array_unique($list, SORT_LOCALE_STRING);  | 
                                                        |
| 901 | + }  | 
                                                        |
| 902 | + }  | 
                                                        |
| 903 | +  | 
                                                        |
| 904 | + //error cause actually, maybe throw an exception in future.  | 
                                                        |
| 905 | + return array();  | 
                                                        |
| 906 | + }  | 
                                                        |
| 907 | +  | 
                                                        |
| 908 | + /**  | 
                                                        |
| 909 | + * executes an LDAP search, optimized for Users  | 
                                                        |
| 910 | + * @param string $filter the LDAP filter for the search  | 
                                                        |
| 911 | + * @param string|string[] $attr optional, when a certain attribute shall be filtered out  | 
                                                        |
| 912 | + * @param integer $limit  | 
                                                        |
| 913 | + * @param integer $offset  | 
                                                        |
| 914 | + * @return array with the search result  | 
                                                        |
| 915 | + *  | 
                                                        |
| 916 | + * Executes an LDAP search  | 
                                                        |
| 917 | + */  | 
                                                        |
| 918 | +    public function searchUsers($filter, $attr = null, $limit = null, $offset = null) { | 
                                                        |
| 919 | + return $this->search($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset);  | 
                                                        |
| 920 | + }  | 
                                                        |
| 921 | +  | 
                                                        |
| 922 | + /**  | 
                                                        |
| 923 | + * @param string $filter  | 
                                                        |
| 924 | + * @param string|string[] $attr  | 
                                                        |
| 925 | + * @param int $limit  | 
                                                        |
| 926 | + * @param int $offset  | 
                                                        |
| 927 | + * @return false|int  | 
                                                        |
| 928 | + */  | 
                                                        |
| 929 | +    public function countUsers($filter, $attr = array('dn'), $limit = null, $offset = null) { | 
                                                        |
| 930 | + return $this->count($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset);  | 
                                                        |
| 931 | + }  | 
                                                        |
| 932 | +  | 
                                                        |
| 933 | + /**  | 
                                                        |
| 934 | + * executes an LDAP search, optimized for Groups  | 
                                                        |
| 935 | + * @param string $filter the LDAP filter for the search  | 
                                                        |
| 936 | + * @param string|string[] $attr optional, when a certain attribute shall be filtered out  | 
                                                        |
| 937 | + * @param integer $limit  | 
                                                        |
| 938 | + * @param integer $offset  | 
                                                        |
| 939 | + * @return array with the search result  | 
                                                        |
| 940 | + *  | 
                                                        |
| 941 | + * Executes an LDAP search  | 
                                                        |
| 942 | + */  | 
                                                        |
| 943 | +    public function searchGroups($filter, $attr = null, $limit = null, $offset = null) { | 
                                                        |
| 944 | + return $this->search($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset);  | 
                                                        |
| 945 | + }  | 
                                                        |
| 946 | +  | 
                                                        |
| 947 | + /**  | 
                                                        |
| 948 | + * returns the number of available groups  | 
                                                        |
| 949 | + * @param string $filter the LDAP search filter  | 
                                                        |
| 950 | + * @param string[] $attr optional  | 
                                                        |
| 951 | + * @param int|null $limit  | 
                                                        |
| 952 | + * @param int|null $offset  | 
                                                        |
| 953 | + * @return int|bool  | 
                                                        |
| 954 | + */  | 
                                                        |
| 955 | +    public function countGroups($filter, $attr = array('dn'), $limit = null, $offset = null) { | 
                                                        |
| 956 | + return $this->count($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset);  | 
                                                        |
| 957 | + }  | 
                                                        |
| 958 | +  | 
                                                        |
| 959 | + /**  | 
                                                        |
| 960 | + * returns the number of available objects on the base DN  | 
                                                        |
| 961 | + *  | 
                                                        |
| 962 | + * @param int|null $limit  | 
                                                        |
| 963 | + * @param int|null $offset  | 
                                                        |
| 964 | + * @return int|bool  | 
                                                        |
| 965 | + */  | 
                                                        |
| 966 | +    public function countObjects($limit = null, $offset = null) { | 
                                                        |
| 967 | +        return $this->count('objectclass=*', $this->connection->ldapBase, array('dn'), $limit, $offset); | 
                                                        |
| 968 | + }  | 
                                                        |
| 969 | +  | 
                                                        |
| 970 | + /**  | 
                                                        |
| 971 | + * Returns the LDAP handler  | 
                                                        |
| 972 | + * @throws \OC\ServerNotAvailableException  | 
                                                        |
| 973 | + */  | 
                                                        |
| 974 | +  | 
                                                        |
| 975 | + /**  | 
                                                        |
| 976 | + * @return mixed  | 
                                                        |
| 977 | + * @throws \OC\ServerNotAvailableException  | 
                                                        |
| 978 | + */  | 
                                                        |
| 979 | +    private function invokeLDAPMethod() { | 
                                                        |
| 980 | + $arguments = func_get_args();  | 
                                                        |
| 981 | + $command = array_shift($arguments);  | 
                                                        |
| 982 | + $cr = array_shift($arguments);  | 
                                                        |
| 983 | +        if (!method_exists($this->ldap, $command)) { | 
                                                        |
| 984 | + return null;  | 
                                                        |
| 985 | + }  | 
                                                        |
| 986 | + array_unshift($arguments, $cr);  | 
                                                        |
| 987 | + // php no longer supports call-time pass-by-reference  | 
                                                        |
| 988 | + // thus cannot support controlPagedResultResponse as the third argument  | 
                                                        |
| 989 | + // is a reference  | 
                                                        |
| 990 | +        $doMethod = function () use ($command, &$arguments) { | 
                                                        |
| 991 | +            if ($command == 'controlPagedResultResponse') { | 
                                                        |
| 992 | +                throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.'); | 
                                                        |
| 993 | +            } else { | 
                                                        |
| 994 | + return call_user_func_array(array($this->ldap, $command), $arguments);  | 
                                                        |
| 995 | + }  | 
                                                        |
| 996 | + };  | 
                                                        |
| 997 | +        try { | 
                                                        |
| 998 | + $ret = $doMethod();  | 
                                                        |
| 999 | +        } catch (ServerNotAvailableException $e) { | 
                                                        |
| 1000 | + /* Server connection lost, attempt to reestablish it  | 
                                                        |
| 1001 | 1001 | * Maybe implement exponential backoff?  | 
                                                        
| 1002 | 1002 | * This was enough to get solr indexer working which has large delays between LDAP fetches.  | 
                                                        
| 1003 | 1003 | */  | 
                                                        
| 1004 | -			\OCP\Util::writeLog('user_ldap', "Connection lost on $command, attempting to reestablish.", \OCP\Util::DEBUG); | 
                                                        |
| 1005 | - $this->connection->resetConnectionResource();  | 
                                                        |
| 1006 | - $cr = $this->connection->getConnectionResource();  | 
                                                        |
| 1007 | -  | 
                                                        |
| 1008 | -			if(!$this->ldap->isResource($cr)) { | 
                                                        |
| 1009 | - // Seems like we didn't find any resource.  | 
                                                        |
| 1010 | -				\OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", \OCP\Util::DEBUG); | 
                                                        |
| 1011 | - throw $e;  | 
                                                        |
| 1012 | - }  | 
                                                        |
| 1013 | -  | 
                                                        |
| 1014 | - $arguments[0] = array_pad([], count($arguments[0]), $cr);  | 
                                                        |
| 1015 | - $ret = $doMethod();  | 
                                                        |
| 1016 | - }  | 
                                                        |
| 1017 | - return $ret;  | 
                                                        |
| 1018 | - }  | 
                                                        |
| 1019 | -  | 
                                                        |
| 1020 | - /**  | 
                                                        |
| 1021 | - * retrieved. Results will according to the order in the array.  | 
                                                        |
| 1022 | - *  | 
                                                        |
| 1023 | - * @param $filter  | 
                                                        |
| 1024 | - * @param $base  | 
                                                        |
| 1025 | - * @param null $attr  | 
                                                        |
| 1026 | - * @param int $limit optional, maximum results to be counted  | 
                                                        |
| 1027 | - * @param int $offset optional, a starting point  | 
                                                        |
| 1028 | - * @return array|false array with the search result as first value and pagedSearchOK as  | 
                                                        |
| 1029 | - * second | false if not successful  | 
                                                        |
| 1030 | - * @throws ServerNotAvailableException  | 
                                                        |
| 1031 | - */  | 
                                                        |
| 1032 | -	private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) { | 
                                                        |
| 1033 | -		if(!is_null($attr) && !is_array($attr)) { | 
                                                        |
| 1034 | - $attr = array(mb_strtolower($attr, 'UTF-8'));  | 
                                                        |
| 1035 | - }  | 
                                                        |
| 1036 | -  | 
                                                        |
| 1037 | - // See if we have a resource, in case not cancel with message  | 
                                                        |
| 1038 | - $cr = $this->connection->getConnectionResource();  | 
                                                        |
| 1039 | -		if(!$this->ldap->isResource($cr)) { | 
                                                        |
| 1040 | - // Seems like we didn't find any resource.  | 
                                                        |
| 1041 | - // Return an empty array just like before.  | 
                                                        |
| 1042 | -			\OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', \OCP\Util::DEBUG); | 
                                                        |
| 1043 | - return false;  | 
                                                        |
| 1044 | - }  | 
                                                        |
| 1045 | -  | 
                                                        |
| 1046 | - //check whether paged search should be attempted  | 
                                                        |
| 1047 | - $pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, intval($limit), $offset);  | 
                                                        |
| 1048 | -  | 
                                                        |
| 1049 | - $linkResources = array_pad(array(), count($base), $cr);  | 
                                                        |
| 1050 | -		$sr = $this->invokeLDAPMethod('search', $linkResources, $base, $filter, $attr); | 
                                                        |
| 1051 | - // cannot use $cr anymore, might have changed in the previous call!  | 
                                                        |
| 1052 | - $error = $this->ldap->errno($this->connection->getConnectionResource());  | 
                                                        |
| 1053 | -		if(!is_array($sr) || $error !== 0) { | 
                                                        |
| 1054 | -			\OCP\Util::writeLog('user_ldap', 'Attempt for Paging?  '.print_r($pagedSearchOK, true), \OCP\Util::ERROR); | 
                                                        |
| 1055 | - return false;  | 
                                                        |
| 1056 | - }  | 
                                                        |
| 1057 | -  | 
                                                        |
| 1058 | - return array($sr, $pagedSearchOK);  | 
                                                        |
| 1059 | - }  | 
                                                        |
| 1060 | -  | 
                                                        |
| 1061 | - /**  | 
                                                        |
| 1062 | - * processes an LDAP paged search operation  | 
                                                        |
| 1063 | - * @param array $sr the array containing the LDAP search resources  | 
                                                        |
| 1064 | - * @param string $filter the LDAP filter for the search  | 
                                                        |
| 1065 | - * @param array $base an array containing the LDAP subtree(s) that shall be searched  | 
                                                        |
| 1066 | - * @param int $iFoundItems number of results in the single search operation  | 
                                                        |
| 1067 | - * @param int $limit maximum results to be counted  | 
                                                        |
| 1068 | - * @param int $offset a starting point  | 
                                                        |
| 1069 | - * @param bool $pagedSearchOK whether a paged search has been executed  | 
                                                        |
| 1070 | - * @param bool $skipHandling required for paged search when cookies to  | 
                                                        |
| 1071 | - * prior results need to be gained  | 
                                                        |
| 1072 | - * @return bool cookie validity, true if we have more pages, false otherwise.  | 
                                                        |
| 1073 | - */  | 
                                                        |
| 1074 | -	private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) { | 
                                                        |
| 1075 | - $cookie = null;  | 
                                                        |
| 1076 | -		if($pagedSearchOK) { | 
                                                        |
| 1077 | - $cr = $this->connection->getConnectionResource();  | 
                                                        |
| 1078 | -			foreach($sr as $key => $res) { | 
                                                        |
| 1079 | -				if($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { | 
                                                        |
| 1080 | - $this->setPagedResultCookie($base[$key], $filter, $limit, $offset, $cookie);  | 
                                                        |
| 1081 | - }  | 
                                                        |
| 1082 | - }  | 
                                                        |
| 1083 | -  | 
                                                        |
| 1084 | - //browsing through prior pages to get the cookie for the new one  | 
                                                        |
| 1085 | -			if($skipHandling) { | 
                                                        |
| 1086 | - return false;  | 
                                                        |
| 1087 | - }  | 
                                                        |
| 1088 | - // if count is bigger, then the server does not support  | 
                                                        |
| 1089 | - // paged search. Instead, he did a normal search. We set a  | 
                                                        |
| 1090 | - // flag here, so the callee knows how to deal with it.  | 
                                                        |
| 1091 | -			if($iFoundItems <= $limit) { | 
                                                        |
| 1092 | - $this->pagedSearchedSuccessful = true;  | 
                                                        |
| 1093 | - }  | 
                                                        |
| 1094 | -		} else { | 
                                                        |
| 1095 | -			if(!is_null($limit)) { | 
                                                        |
| 1096 | -				\OCP\Util::writeLog('user_ldap', 'Paged search was not available', \OCP\Util::INFO); | 
                                                        |
| 1097 | - }  | 
                                                        |
| 1098 | - }  | 
                                                        |
| 1099 | - /* ++ Fixing RHDS searches with pages with zero results ++  | 
                                                        |
| 1004 | +            \OCP\Util::writeLog('user_ldap', "Connection lost on $command, attempting to reestablish.", \OCP\Util::DEBUG); | 
                                                        |
| 1005 | + $this->connection->resetConnectionResource();  | 
                                                        |
| 1006 | + $cr = $this->connection->getConnectionResource();  | 
                                                        |
| 1007 | +  | 
                                                        |
| 1008 | +            if(!$this->ldap->isResource($cr)) { | 
                                                        |
| 1009 | + // Seems like we didn't find any resource.  | 
                                                        |
| 1010 | +                \OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", \OCP\Util::DEBUG); | 
                                                        |
| 1011 | + throw $e;  | 
                                                        |
| 1012 | + }  | 
                                                        |
| 1013 | +  | 
                                                        |
| 1014 | + $arguments[0] = array_pad([], count($arguments[0]), $cr);  | 
                                                        |
| 1015 | + $ret = $doMethod();  | 
                                                        |
| 1016 | + }  | 
                                                        |
| 1017 | + return $ret;  | 
                                                        |
| 1018 | + }  | 
                                                        |
| 1019 | +  | 
                                                        |
| 1020 | + /**  | 
                                                        |
| 1021 | + * retrieved. Results will according to the order in the array.  | 
                                                        |
| 1022 | + *  | 
                                                        |
| 1023 | + * @param $filter  | 
                                                        |
| 1024 | + * @param $base  | 
                                                        |
| 1025 | + * @param null $attr  | 
                                                        |
| 1026 | + * @param int $limit optional, maximum results to be counted  | 
                                                        |
| 1027 | + * @param int $offset optional, a starting point  | 
                                                        |
| 1028 | + * @return array|false array with the search result as first value and pagedSearchOK as  | 
                                                        |
| 1029 | + * second | false if not successful  | 
                                                        |
| 1030 | + * @throws ServerNotAvailableException  | 
                                                        |
| 1031 | + */  | 
                                                        |
| 1032 | +    private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) { | 
                                                        |
| 1033 | +        if(!is_null($attr) && !is_array($attr)) { | 
                                                        |
| 1034 | + $attr = array(mb_strtolower($attr, 'UTF-8'));  | 
                                                        |
| 1035 | + }  | 
                                                        |
| 1036 | +  | 
                                                        |
| 1037 | + // See if we have a resource, in case not cancel with message  | 
                                                        |
| 1038 | + $cr = $this->connection->getConnectionResource();  | 
                                                        |
| 1039 | +        if(!$this->ldap->isResource($cr)) { | 
                                                        |
| 1040 | + // Seems like we didn't find any resource.  | 
                                                        |
| 1041 | + // Return an empty array just like before.  | 
                                                        |
| 1042 | +            \OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', \OCP\Util::DEBUG); | 
                                                        |
| 1043 | + return false;  | 
                                                        |
| 1044 | + }  | 
                                                        |
| 1045 | +  | 
                                                        |
| 1046 | + //check whether paged search should be attempted  | 
                                                        |
| 1047 | + $pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, intval($limit), $offset);  | 
                                                        |
| 1048 | +  | 
                                                        |
| 1049 | + $linkResources = array_pad(array(), count($base), $cr);  | 
                                                        |
| 1050 | +        $sr = $this->invokeLDAPMethod('search', $linkResources, $base, $filter, $attr); | 
                                                        |
| 1051 | + // cannot use $cr anymore, might have changed in the previous call!  | 
                                                        |
| 1052 | + $error = $this->ldap->errno($this->connection->getConnectionResource());  | 
                                                        |
| 1053 | +        if(!is_array($sr) || $error !== 0) { | 
                                                        |
| 1054 | +            \OCP\Util::writeLog('user_ldap', 'Attempt for Paging?  '.print_r($pagedSearchOK, true), \OCP\Util::ERROR); | 
                                                        |
| 1055 | + return false;  | 
                                                        |
| 1056 | + }  | 
                                                        |
| 1057 | +  | 
                                                        |
| 1058 | + return array($sr, $pagedSearchOK);  | 
                                                        |
| 1059 | + }  | 
                                                        |
| 1060 | +  | 
                                                        |
| 1061 | + /**  | 
                                                        |
| 1062 | + * processes an LDAP paged search operation  | 
                                                        |
| 1063 | + * @param array $sr the array containing the LDAP search resources  | 
                                                        |
| 1064 | + * @param string $filter the LDAP filter for the search  | 
                                                        |
| 1065 | + * @param array $base an array containing the LDAP subtree(s) that shall be searched  | 
                                                        |
| 1066 | + * @param int $iFoundItems number of results in the single search operation  | 
                                                        |
| 1067 | + * @param int $limit maximum results to be counted  | 
                                                        |
| 1068 | + * @param int $offset a starting point  | 
                                                        |
| 1069 | + * @param bool $pagedSearchOK whether a paged search has been executed  | 
                                                        |
| 1070 | + * @param bool $skipHandling required for paged search when cookies to  | 
                                                        |
| 1071 | + * prior results need to be gained  | 
                                                        |
| 1072 | + * @return bool cookie validity, true if we have more pages, false otherwise.  | 
                                                        |
| 1073 | + */  | 
                                                        |
| 1074 | +    private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) { | 
                                                        |
| 1075 | + $cookie = null;  | 
                                                        |
| 1076 | +        if($pagedSearchOK) { | 
                                                        |
| 1077 | + $cr = $this->connection->getConnectionResource();  | 
                                                        |
| 1078 | +            foreach($sr as $key => $res) { | 
                                                        |
| 1079 | +                if($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { | 
                                                        |
| 1080 | + $this->setPagedResultCookie($base[$key], $filter, $limit, $offset, $cookie);  | 
                                                        |
| 1081 | + }  | 
                                                        |
| 1082 | + }  | 
                                                        |
| 1083 | +  | 
                                                        |
| 1084 | + //browsing through prior pages to get the cookie for the new one  | 
                                                        |
| 1085 | +            if($skipHandling) { | 
                                                        |
| 1086 | + return false;  | 
                                                        |
| 1087 | + }  | 
                                                        |
| 1088 | + // if count is bigger, then the server does not support  | 
                                                        |
| 1089 | + // paged search. Instead, he did a normal search. We set a  | 
                                                        |
| 1090 | + // flag here, so the callee knows how to deal with it.  | 
                                                        |
| 1091 | +            if($iFoundItems <= $limit) { | 
                                                        |
| 1092 | + $this->pagedSearchedSuccessful = true;  | 
                                                        |
| 1093 | + }  | 
                                                        |
| 1094 | +        } else { | 
                                                        |
| 1095 | +            if(!is_null($limit)) { | 
                                                        |
| 1096 | +                \OCP\Util::writeLog('user_ldap', 'Paged search was not available', \OCP\Util::INFO); | 
                                                        |
| 1097 | + }  | 
                                                        |
| 1098 | + }  | 
                                                        |
| 1099 | + /* ++ Fixing RHDS searches with pages with zero results ++  | 
                                                        |
| 1100 | 1100 | * Return cookie status. If we don't have more pages, with RHDS  | 
                                                        
| 1101 | 1101 | * cookie is null, with openldap cookie is an empty string and  | 
                                                        
| 1102 | 1102 | * to 386ds '0' is a valid cookie. Even if $iFoundItems == 0  | 
                                                        
| 1103 | 1103 | */  | 
                                                        
| 1104 | - return !empty($cookie) || $cookie === '0';  | 
                                                        |
| 1105 | - }  | 
                                                        |
| 1106 | -  | 
                                                        |
| 1107 | - /**  | 
                                                        |
| 1108 | - * executes an LDAP search, but counts the results only  | 
                                                        |
| 1109 | - * @param string $filter the LDAP filter for the search  | 
                                                        |
| 1110 | - * @param array $base an array containing the LDAP subtree(s) that shall be searched  | 
                                                        |
| 1111 | - * @param string|string[] $attr optional, array, one or more attributes that shall be  | 
                                                        |
| 1112 | - * retrieved. Results will according to the order in the array.  | 
                                                        |
| 1113 | - * @param int $limit optional, maximum results to be counted  | 
                                                        |
| 1114 | - * @param int $offset optional, a starting point  | 
                                                        |
| 1115 | - * @param bool $skipHandling indicates whether the pages search operation is  | 
                                                        |
| 1116 | - * completed  | 
                                                        |
| 1117 | - * @return int|false Integer or false if the search could not be initialized  | 
                                                        |
| 1118 | - *  | 
                                                        |
| 1119 | - */  | 
                                                        |
| 1120 | -	private function count($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { | 
                                                        |
| 1121 | -		\OCP\Util::writeLog('user_ldap', 'Count filter:  '.print_r($filter, true), \OCP\Util::DEBUG); | 
                                                        |
| 1122 | -  | 
                                                        |
| 1123 | - $limitPerPage = intval($this->connection->ldapPagingSize);  | 
                                                        |
| 1124 | -		if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { | 
                                                        |
| 1125 | - $limitPerPage = $limit;  | 
                                                        |
| 1126 | - }  | 
                                                        |
| 1127 | -  | 
                                                        |
| 1128 | - $counter = 0;  | 
                                                        |
| 1129 | - $count = null;  | 
                                                        |
| 1130 | - $this->connection->getConnectionResource();  | 
                                                        |
| 1131 | -  | 
                                                        |
| 1132 | -		do { | 
                                                        |
| 1133 | - $search = $this->executeSearch($filter, $base, $attr,  | 
                                                        |
| 1134 | - $limitPerPage, $offset);  | 
                                                        |
| 1135 | -			if($search === false) { | 
                                                        |
| 1136 | - return $counter > 0 ? $counter : false;  | 
                                                        |
| 1137 | - }  | 
                                                        |
| 1138 | - list($sr, $pagedSearchOK) = $search;  | 
                                                        |
| 1139 | -  | 
                                                        |
| 1140 | - /* ++ Fixing RHDS searches with pages with zero results ++  | 
                                                        |
| 1104 | + return !empty($cookie) || $cookie === '0';  | 
                                                        |
| 1105 | + }  | 
                                                        |
| 1106 | +  | 
                                                        |
| 1107 | + /**  | 
                                                        |
| 1108 | + * executes an LDAP search, but counts the results only  | 
                                                        |
| 1109 | + * @param string $filter the LDAP filter for the search  | 
                                                        |
| 1110 | + * @param array $base an array containing the LDAP subtree(s) that shall be searched  | 
                                                        |
| 1111 | + * @param string|string[] $attr optional, array, one or more attributes that shall be  | 
                                                        |
| 1112 | + * retrieved. Results will according to the order in the array.  | 
                                                        |
| 1113 | + * @param int $limit optional, maximum results to be counted  | 
                                                        |
| 1114 | + * @param int $offset optional, a starting point  | 
                                                        |
| 1115 | + * @param bool $skipHandling indicates whether the pages search operation is  | 
                                                        |
| 1116 | + * completed  | 
                                                        |
| 1117 | + * @return int|false Integer or false if the search could not be initialized  | 
                                                        |
| 1118 | + *  | 
                                                        |
| 1119 | + */  | 
                                                        |
| 1120 | +    private function count($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { | 
                                                        |
| 1121 | +        \OCP\Util::writeLog('user_ldap', 'Count filter:  '.print_r($filter, true), \OCP\Util::DEBUG); | 
                                                        |
| 1122 | +  | 
                                                        |
| 1123 | + $limitPerPage = intval($this->connection->ldapPagingSize);  | 
                                                        |
| 1124 | +        if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { | 
                                                        |
| 1125 | + $limitPerPage = $limit;  | 
                                                        |
| 1126 | + }  | 
                                                        |
| 1127 | +  | 
                                                        |
| 1128 | + $counter = 0;  | 
                                                        |
| 1129 | + $count = null;  | 
                                                        |
| 1130 | + $this->connection->getConnectionResource();  | 
                                                        |
| 1131 | +  | 
                                                        |
| 1132 | +        do { | 
                                                        |
| 1133 | + $search = $this->executeSearch($filter, $base, $attr,  | 
                                                        |
| 1134 | + $limitPerPage, $offset);  | 
                                                        |
| 1135 | +            if($search === false) { | 
                                                        |
| 1136 | + return $counter > 0 ? $counter : false;  | 
                                                        |
| 1137 | + }  | 
                                                        |
| 1138 | + list($sr, $pagedSearchOK) = $search;  | 
                                                        |
| 1139 | +  | 
                                                        |
| 1140 | + /* ++ Fixing RHDS searches with pages with zero results ++  | 
                                                        |
| 1141 | 1141 | * countEntriesInSearchResults() method signature changed  | 
                                                        
| 1142 | 1142 | * by removing $limit and &$hasHitLimit parameters  | 
                                                        
| 1143 | 1143 | */  | 
                                                        
| 1144 | - $count = $this->countEntriesInSearchResults($sr);  | 
                                                        |
| 1145 | - $counter += $count;  | 
                                                        |
| 1144 | + $count = $this->countEntriesInSearchResults($sr);  | 
                                                        |
| 1145 | + $counter += $count;  | 
                                                        |
| 1146 | 1146 | |
| 1147 | - $hasMorePages = $this->processPagedSearchStatus($sr, $filter, $base, $count, $limitPerPage,  | 
                                                        |
| 1148 | - $offset, $pagedSearchOK, $skipHandling);  | 
                                                        |
| 1149 | - $offset += $limitPerPage;  | 
                                                        |
| 1150 | - /* ++ Fixing RHDS searches with pages with zero results ++  | 
                                                        |
| 1147 | + $hasMorePages = $this->processPagedSearchStatus($sr, $filter, $base, $count, $limitPerPage,  | 
                                                        |
| 1148 | + $offset, $pagedSearchOK, $skipHandling);  | 
                                                        |
| 1149 | + $offset += $limitPerPage;  | 
                                                        |
| 1150 | + /* ++ Fixing RHDS searches with pages with zero results ++  | 
                                                        |
| 1151 | 1151 | * Continue now depends on $hasMorePages value  | 
                                                        
| 1152 | 1152 | */  | 
                                                        
| 1153 | - $continue = $pagedSearchOK && $hasMorePages;  | 
                                                        |
| 1154 | - } while($continue && (is_null($limit) || $limit <= 0 || $limit > $counter));  | 
                                                        |
| 1155 | -  | 
                                                        |
| 1156 | - return $counter;  | 
                                                        |
| 1157 | - }  | 
                                                        |
| 1158 | -  | 
                                                        |
| 1159 | - /**  | 
                                                        |
| 1160 | - * @param array $searchResults  | 
                                                        |
| 1161 | - * @return int  | 
                                                        |
| 1162 | - */  | 
                                                        |
| 1163 | -	private function countEntriesInSearchResults($searchResults) { | 
                                                        |
| 1164 | - $counter = 0;  | 
                                                        |
| 1165 | -  | 
                                                        |
| 1166 | -		foreach($searchResults as $res) { | 
                                                        |
| 1167 | -			$count = intval($this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res)); | 
                                                        |
| 1168 | - $counter += $count;  | 
                                                        |
| 1169 | - }  | 
                                                        |
| 1170 | -  | 
                                                        |
| 1171 | - return $counter;  | 
                                                        |
| 1172 | - }  | 
                                                        |
| 1173 | -  | 
                                                        |
| 1174 | - /**  | 
                                                        |
| 1175 | - * Executes an LDAP search  | 
                                                        |
| 1176 | - *  | 
                                                        |
| 1177 | - * @param string $filter the LDAP filter for the search  | 
                                                        |
| 1178 | - * @param array $base an array containing the LDAP subtree(s) that shall be searched  | 
                                                        |
| 1179 | - * @param string|string[] $attr optional, array, one or more attributes that shall be  | 
                                                        |
| 1180 | - * @param int $limit  | 
                                                        |
| 1181 | - * @param int $offset  | 
                                                        |
| 1182 | - * @param bool $skipHandling  | 
                                                        |
| 1183 | - * @return array with the search result  | 
                                                        |
| 1184 | - * @throws ServerNotAvailableException  | 
                                                        |
| 1185 | - */  | 
                                                        |
| 1186 | -	public function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { | 
                                                        |
| 1187 | - $limitPerPage = intval($this->connection->ldapPagingSize);  | 
                                                        |
| 1188 | -		if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { | 
                                                        |
| 1189 | - $limitPerPage = $limit;  | 
                                                        |
| 1190 | - }  | 
                                                        |
| 1191 | -  | 
                                                        |
| 1192 | - /* ++ Fixing RHDS searches with pages with zero results ++  | 
                                                        |
| 1153 | + $continue = $pagedSearchOK && $hasMorePages;  | 
                                                        |
| 1154 | + } while($continue && (is_null($limit) || $limit <= 0 || $limit > $counter));  | 
                                                        |
| 1155 | +  | 
                                                        |
| 1156 | + return $counter;  | 
                                                        |
| 1157 | + }  | 
                                                        |
| 1158 | +  | 
                                                        |
| 1159 | + /**  | 
                                                        |
| 1160 | + * @param array $searchResults  | 
                                                        |
| 1161 | + * @return int  | 
                                                        |
| 1162 | + */  | 
                                                        |
| 1163 | +    private function countEntriesInSearchResults($searchResults) { | 
                                                        |
| 1164 | + $counter = 0;  | 
                                                        |
| 1165 | +  | 
                                                        |
| 1166 | +        foreach($searchResults as $res) { | 
                                                        |
| 1167 | +            $count = intval($this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res)); | 
                                                        |
| 1168 | + $counter += $count;  | 
                                                        |
| 1169 | + }  | 
                                                        |
| 1170 | +  | 
                                                        |
| 1171 | + return $counter;  | 
                                                        |
| 1172 | + }  | 
                                                        |
| 1173 | +  | 
                                                        |
| 1174 | + /**  | 
                                                        |
| 1175 | + * Executes an LDAP search  | 
                                                        |
| 1176 | + *  | 
                                                        |
| 1177 | + * @param string $filter the LDAP filter for the search  | 
                                                        |
| 1178 | + * @param array $base an array containing the LDAP subtree(s) that shall be searched  | 
                                                        |
| 1179 | + * @param string|string[] $attr optional, array, one or more attributes that shall be  | 
                                                        |
| 1180 | + * @param int $limit  | 
                                                        |
| 1181 | + * @param int $offset  | 
                                                        |
| 1182 | + * @param bool $skipHandling  | 
                                                        |
| 1183 | + * @return array with the search result  | 
                                                        |
| 1184 | + * @throws ServerNotAvailableException  | 
                                                        |
| 1185 | + */  | 
                                                        |
| 1186 | +    public function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { | 
                                                        |
| 1187 | + $limitPerPage = intval($this->connection->ldapPagingSize);  | 
                                                        |
| 1188 | +        if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { | 
                                                        |
| 1189 | + $limitPerPage = $limit;  | 
                                                        |
| 1190 | + }  | 
                                                        |
| 1191 | +  | 
                                                        |
| 1192 | + /* ++ Fixing RHDS searches with pages with zero results ++  | 
                                                        |
| 1193 | 1193 | * As we can have pages with zero results and/or pages with less  | 
                                                        
| 1194 | 1194 | * than $limit results but with a still valid server 'cookie',  | 
                                                        
| 1195 | 1195 | * loops through until we get $continue equals true and  | 
                                                        
| 1196 | 1196 | * $findings['count'] < $limit  | 
                                                        
| 1197 | 1197 | */  | 
                                                        
| 1198 | - $findings = [];  | 
                                                        |
| 1199 | - $savedoffset = $offset;  | 
                                                        |
| 1200 | -		do { | 
                                                        |
| 1201 | - $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset);  | 
                                                        |
| 1202 | -			if($search === false) { | 
                                                        |
| 1203 | - return [];  | 
                                                        |
| 1204 | - }  | 
                                                        |
| 1205 | - list($sr, $pagedSearchOK) = $search;  | 
                                                        |
| 1206 | - $cr = $this->connection->getConnectionResource();  | 
                                                        |
| 1207 | -  | 
                                                        |
| 1208 | -			if($skipHandling) { | 
                                                        |
| 1209 | - //i.e. result do not need to be fetched, we just need the cookie  | 
                                                        |
| 1210 | - //thus pass 1 or any other value as $iFoundItems because it is not  | 
                                                        |
| 1211 | - //used  | 
                                                        |
| 1212 | - $this->processPagedSearchStatus($sr, $filter, $base, 1, $limitPerPage,  | 
                                                        |
| 1213 | - $offset, $pagedSearchOK,  | 
                                                        |
| 1214 | - $skipHandling);  | 
                                                        |
| 1215 | - return array();  | 
                                                        |
| 1216 | - }  | 
                                                        |
| 1217 | -  | 
                                                        |
| 1218 | - $iFoundItems = 0;  | 
                                                        |
| 1219 | -			foreach($sr as $res) { | 
                                                        |
| 1220 | -				$findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $res)); | 
                                                        |
| 1221 | - $iFoundItems = max($iFoundItems, $findings['count']);  | 
                                                        |
| 1222 | - unset($findings['count']);  | 
                                                        |
| 1223 | - }  | 
                                                        |
| 1224 | -  | 
                                                        |
| 1225 | - $continue = $this->processPagedSearchStatus($sr, $filter, $base, $iFoundItems,  | 
                                                        |
| 1226 | - $limitPerPage, $offset, $pagedSearchOK,  | 
                                                        |
| 1227 | - $skipHandling);  | 
                                                        |
| 1228 | - $offset += $limitPerPage;  | 
                                                        |
| 1229 | - } while ($continue && $pagedSearchOK && ($limit === null || count($findings) < $limit));  | 
                                                        |
| 1230 | - // reseting offset  | 
                                                        |
| 1231 | - $offset = $savedoffset;  | 
                                                        |
| 1232 | -  | 
                                                        |
| 1233 | - // if we're here, probably no connection resource is returned.  | 
                                                        |
| 1234 | - // to make Nextcloud behave nicely, we simply give back an empty array.  | 
                                                        |
| 1235 | -		if(is_null($findings)) { | 
                                                        |
| 1236 | - return array();  | 
                                                        |
| 1237 | - }  | 
                                                        |
| 1238 | -  | 
                                                        |
| 1239 | -		if(!is_null($attr)) { | 
                                                        |
| 1240 | - $selection = [];  | 
                                                        |
| 1241 | - $i = 0;  | 
                                                        |
| 1242 | -			foreach($findings as $item) { | 
                                                        |
| 1243 | -				if(!is_array($item)) { | 
                                                        |
| 1244 | - continue;  | 
                                                        |
| 1245 | - }  | 
                                                        |
| 1246 | - $item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8');  | 
                                                        |
| 1247 | -				foreach($attr as $key) { | 
                                                        |
| 1248 | -					if(isset($item[$key])) { | 
                                                        |
| 1249 | -						if(is_array($item[$key]) && isset($item[$key]['count'])) { | 
                                                        |
| 1250 | - unset($item[$key]['count']);  | 
                                                        |
| 1251 | - }  | 
                                                        |
| 1252 | -						if($key !== 'dn') { | 
                                                        |
| 1253 | - $selection[$i][$key] = $this->resemblesDN($key) ?  | 
                                                        |
| 1254 | - $this->helper->sanitizeDN($item[$key])  | 
                                                        |
| 1255 | - : $key === 'objectguid' || $key === 'guid' ?  | 
                                                        |
| 1256 | - $selection[$i][$key] = $this->convertObjectGUID2Str($item[$key])  | 
                                                        |
| 1257 | - : $item[$key];  | 
                                                        |
| 1258 | -						} else { | 
                                                        |
| 1259 | - $selection[$i][$key] = [$this->helper->sanitizeDN($item[$key])];  | 
                                                        |
| 1260 | - }  | 
                                                        |
| 1261 | - }  | 
                                                        |
| 1262 | -  | 
                                                        |
| 1263 | - }  | 
                                                        |
| 1264 | - $i++;  | 
                                                        |
| 1265 | - }  | 
                                                        |
| 1266 | - $findings = $selection;  | 
                                                        |
| 1267 | - }  | 
                                                        |
| 1268 | - //we slice the findings, when  | 
                                                        |
| 1269 | - //a) paged search unsuccessful, though attempted  | 
                                                        |
| 1270 | - //b) no paged search, but limit set  | 
                                                        |
| 1271 | - if((!$this->getPagedSearchResultState()  | 
                                                        |
| 1272 | - && $pagedSearchOK)  | 
                                                        |
| 1273 | - || (  | 
                                                        |
| 1274 | - !$pagedSearchOK  | 
                                                        |
| 1275 | - && !is_null($limit)  | 
                                                        |
| 1276 | - )  | 
                                                        |
| 1277 | -		) { | 
                                                        |
| 1278 | - $findings = array_slice($findings, intval($offset), $limit);  | 
                                                        |
| 1279 | - }  | 
                                                        |
| 1280 | - return $findings;  | 
                                                        |
| 1281 | - }  | 
                                                        |
| 1282 | -  | 
                                                        |
| 1283 | - /**  | 
                                                        |
| 1284 | - * @param string $name  | 
                                                        |
| 1285 | - * @return bool|mixed|string  | 
                                                        |
| 1286 | - */  | 
                                                        |
| 1287 | -	public function sanitizeUsername($name) { | 
                                                        |
| 1288 | -		if($this->connection->ldapIgnoreNamingRules) { | 
                                                        |
| 1289 | - return $name;  | 
                                                        |
| 1290 | - }  | 
                                                        |
| 1291 | -  | 
                                                        |
| 1292 | - // Transliteration  | 
                                                        |
| 1293 | - // latin characters to ASCII  | 
                                                        |
| 1294 | -		$name = iconv('UTF-8', 'ASCII//TRANSLIT', $name); | 
                                                        |
| 1295 | -  | 
                                                        |
| 1296 | - // Replacements  | 
                                                        |
| 1297 | -		$name = str_replace(' ', '_', $name); | 
                                                        |
| 1298 | -  | 
                                                        |
| 1299 | - // Every remaining disallowed characters will be removed  | 
                                                        |
| 1300 | -		$name = preg_replace('/[^a-zA-Z0-9_.@-]/u', '', $name); | 
                                                        |
| 1301 | -  | 
                                                        |
| 1302 | - return $name;  | 
                                                        |
| 1303 | - }  | 
                                                        |
| 1304 | -  | 
                                                        |
| 1305 | - /**  | 
                                                        |
| 1306 | - * escapes (user provided) parts for LDAP filter  | 
                                                        |
| 1307 | - * @param string $input, the provided value  | 
                                                        |
| 1308 | - * @param bool $allowAsterisk whether in * at the beginning should be preserved  | 
                                                        |
| 1309 | - * @return string the escaped string  | 
                                                        |
| 1310 | - */  | 
                                                        |
| 1311 | -	public function escapeFilterPart($input, $allowAsterisk = false) { | 
                                                        |
| 1312 | - $asterisk = '';  | 
                                                        |
| 1313 | -		if($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { | 
                                                        |
| 1314 | - $asterisk = '*';  | 
                                                        |
| 1315 | - $input = mb_substr($input, 1, null, 'UTF-8');  | 
                                                        |
| 1316 | - }  | 
                                                        |
| 1317 | -		$search  = array('*', '\\', '(', ')'); | 
                                                        |
| 1318 | -		$replace = array('\\*', '\\\\', '\\(', '\\)'); | 
                                                        |
| 1319 | - return $asterisk . str_replace($search, $replace, $input);  | 
                                                        |
| 1320 | - }  | 
                                                        |
| 1321 | -  | 
                                                        |
| 1322 | - /**  | 
                                                        |
| 1323 | - * combines the input filters with AND  | 
                                                        |
| 1324 | - * @param string[] $filters the filters to connect  | 
                                                        |
| 1325 | - * @return string the combined filter  | 
                                                        |
| 1326 | - */  | 
                                                        |
| 1327 | -	public function combineFilterWithAnd($filters) { | 
                                                        |
| 1328 | - return $this->combineFilter($filters, '&');  | 
                                                        |
| 1329 | - }  | 
                                                        |
| 1330 | -  | 
                                                        |
| 1331 | - /**  | 
                                                        |
| 1332 | - * combines the input filters with OR  | 
                                                        |
| 1333 | - * @param string[] $filters the filters to connect  | 
                                                        |
| 1334 | - * @return string the combined filter  | 
                                                        |
| 1335 | - * Combines Filter arguments with OR  | 
                                                        |
| 1336 | - */  | 
                                                        |
| 1337 | -	public function combineFilterWithOr($filters) { | 
                                                        |
| 1338 | - return $this->combineFilter($filters, '|');  | 
                                                        |
| 1339 | - }  | 
                                                        |
| 1340 | -  | 
                                                        |
| 1341 | - /**  | 
                                                        |
| 1342 | - * combines the input filters with given operator  | 
                                                        |
| 1343 | - * @param string[] $filters the filters to connect  | 
                                                        |
| 1344 | - * @param string $operator either & or |  | 
                                                        |
| 1345 | - * @return string the combined filter  | 
                                                        |
| 1346 | - */  | 
                                                        |
| 1347 | -	private function combineFilter($filters, $operator) { | 
                                                        |
| 1348 | -		$combinedFilter = '('.$operator; | 
                                                        |
| 1349 | -		foreach($filters as $filter) { | 
                                                        |
| 1350 | -			if ($filter !== '' && $filter[0] !== '(') { | 
                                                        |
| 1351 | -				$filter = '('.$filter.')'; | 
                                                        |
| 1352 | - }  | 
                                                        |
| 1353 | - $combinedFilter.=$filter;  | 
                                                        |
| 1354 | - }  | 
                                                        |
| 1355 | - $combinedFilter.=')';  | 
                                                        |
| 1356 | - return $combinedFilter;  | 
                                                        |
| 1357 | - }  | 
                                                        |
| 1358 | -  | 
                                                        |
| 1359 | - /**  | 
                                                        |
| 1360 | - * creates a filter part for to perform search for users  | 
                                                        |
| 1361 | - * @param string $search the search term  | 
                                                        |
| 1362 | - * @return string the final filter part to use in LDAP searches  | 
                                                        |
| 1363 | - */  | 
                                                        |
| 1364 | -	public function getFilterPartForUserSearch($search) { | 
                                                        |
| 1365 | - return $this->getFilterPartForSearch($search,  | 
                                                        |
| 1366 | - $this->connection->ldapAttributesForUserSearch,  | 
                                                        |
| 1367 | - $this->connection->ldapUserDisplayName);  | 
                                                        |
| 1368 | - }  | 
                                                        |
| 1369 | -  | 
                                                        |
| 1370 | - /**  | 
                                                        |
| 1371 | - * creates a filter part for to perform search for groups  | 
                                                        |
| 1372 | - * @param string $search the search term  | 
                                                        |
| 1373 | - * @return string the final filter part to use in LDAP searches  | 
                                                        |
| 1374 | - */  | 
                                                        |
| 1375 | -	public function getFilterPartForGroupSearch($search) { | 
                                                        |
| 1376 | - return $this->getFilterPartForSearch($search,  | 
                                                        |
| 1377 | - $this->connection->ldapAttributesForGroupSearch,  | 
                                                        |
| 1378 | - $this->connection->ldapGroupDisplayName);  | 
                                                        |
| 1379 | - }  | 
                                                        |
| 1380 | -  | 
                                                        |
| 1381 | - /**  | 
                                                        |
| 1382 | - * creates a filter part for searches by splitting up the given search  | 
                                                        |
| 1383 | - * string into single words  | 
                                                        |
| 1384 | - * @param string $search the search term  | 
                                                        |
| 1385 | - * @param string[] $searchAttributes needs to have at least two attributes,  | 
                                                        |
| 1386 | - * otherwise it does not make sense :)  | 
                                                        |
| 1387 | - * @return string the final filter part to use in LDAP searches  | 
                                                        |
| 1388 | - * @throws \Exception  | 
                                                        |
| 1389 | - */  | 
                                                        |
| 1390 | -	private function getAdvancedFilterPartForSearch($search, $searchAttributes) { | 
                                                        |
| 1391 | -		if(!is_array($searchAttributes) || count($searchAttributes) < 2) { | 
                                                        |
| 1392 | -			throw new \Exception('searchAttributes must be an array with at least two string'); | 
                                                        |
| 1393 | - }  | 
                                                        |
| 1394 | -		$searchWords = explode(' ', trim($search)); | 
                                                        |
| 1395 | - $wordFilters = array();  | 
                                                        |
| 1396 | -		foreach($searchWords as $word) { | 
                                                        |
| 1397 | - $word = $this->prepareSearchTerm($word);  | 
                                                        |
| 1398 | - //every word needs to appear at least once  | 
                                                        |
| 1399 | - $wordMatchOneAttrFilters = array();  | 
                                                        |
| 1400 | -			foreach($searchAttributes as $attr) { | 
                                                        |
| 1401 | - $wordMatchOneAttrFilters[] = $attr . '=' . $word;  | 
                                                        |
| 1402 | - }  | 
                                                        |
| 1403 | - $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters);  | 
                                                        |
| 1404 | - }  | 
                                                        |
| 1405 | - return $this->combineFilterWithAnd($wordFilters);  | 
                                                        |
| 1406 | - }  | 
                                                        |
| 1407 | -  | 
                                                        |
| 1408 | - /**  | 
                                                        |
| 1409 | - * creates a filter part for searches  | 
                                                        |
| 1410 | - * @param string $search the search term  | 
                                                        |
| 1411 | - * @param string[]|null $searchAttributes  | 
                                                        |
| 1412 | - * @param string $fallbackAttribute a fallback attribute in case the user  | 
                                                        |
| 1413 | - * did not define search attributes. Typically the display name attribute.  | 
                                                        |
| 1414 | - * @return string the final filter part to use in LDAP searches  | 
                                                        |
| 1415 | - */  | 
                                                        |
| 1416 | -	private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { | 
                                                        |
| 1417 | - $filter = array();  | 
                                                        |
| 1418 | - $haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0);  | 
                                                        |
| 1419 | -		if($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { | 
                                                        |
| 1420 | -			try { | 
                                                        |
| 1421 | - return $this->getAdvancedFilterPartForSearch($search, $searchAttributes);  | 
                                                        |
| 1422 | -			} catch(\Exception $e) { | 
                                                        |
| 1423 | - \OCP\Util::writeLog(  | 
                                                        |
| 1424 | - 'user_ldap',  | 
                                                        |
| 1425 | - 'Creating advanced filter for search failed, falling back to simple method.',  | 
                                                        |
| 1426 | - \OCP\Util::INFO  | 
                                                        |
| 1427 | - );  | 
                                                        |
| 1428 | - }  | 
                                                        |
| 1429 | - }  | 
                                                        |
| 1430 | -  | 
                                                        |
| 1431 | - $search = $this->prepareSearchTerm($search);  | 
                                                        |
| 1432 | -		if(!is_array($searchAttributes) || count($searchAttributes) === 0) { | 
                                                        |
| 1433 | -			if ($fallbackAttribute === '') { | 
                                                        |
| 1434 | - return '';  | 
                                                        |
| 1435 | - }  | 
                                                        |
| 1436 | - $filter[] = $fallbackAttribute . '=' . $search;  | 
                                                        |
| 1437 | -		} else { | 
                                                        |
| 1438 | -			foreach($searchAttributes as $attribute) { | 
                                                        |
| 1439 | - $filter[] = $attribute . '=' . $search;  | 
                                                        |
| 1440 | - }  | 
                                                        |
| 1441 | - }  | 
                                                        |
| 1442 | -		if(count($filter) === 1) { | 
                                                        |
| 1443 | -			return '('.$filter[0].')'; | 
                                                        |
| 1444 | - }  | 
                                                        |
| 1445 | - return $this->combineFilterWithOr($filter);  | 
                                                        |
| 1446 | - }  | 
                                                        |
| 1447 | -  | 
                                                        |
| 1448 | - /**  | 
                                                        |
| 1449 | - * returns the search term depending on whether we are allowed  | 
                                                        |
| 1450 | - * list users found by ldap with the current input appended by  | 
                                                        |
| 1451 | - * a *  | 
                                                        |
| 1452 | - * @return string  | 
                                                        |
| 1453 | - */  | 
                                                        |
| 1454 | -	private function prepareSearchTerm($term) { | 
                                                        |
| 1455 | - $config = \OC::$server->getConfig();  | 
                                                        |
| 1456 | -  | 
                                                        |
| 1457 | -		$allowEnum = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes'); | 
                                                        |
| 1458 | -  | 
                                                        |
| 1459 | - $result = $term;  | 
                                                        |
| 1460 | -		if ($term === '') { | 
                                                        |
| 1461 | - $result = '*';  | 
                                                        |
| 1462 | -		} else if ($allowEnum !== 'no') { | 
                                                        |
| 1463 | - $result = $term . '*';  | 
                                                        |
| 1464 | - }  | 
                                                        |
| 1465 | - return $result;  | 
                                                        |
| 1466 | - }  | 
                                                        |
| 1467 | -  | 
                                                        |
| 1468 | - /**  | 
                                                        |
| 1469 | - * returns the filter used for counting users  | 
                                                        |
| 1470 | - * @return string  | 
                                                        |
| 1471 | - */  | 
                                                        |
| 1472 | -	public function getFilterForUserCount() { | 
                                                        |
| 1473 | - $filter = $this->combineFilterWithAnd(array(  | 
                                                        |
| 1474 | - $this->connection->ldapUserFilter,  | 
                                                        |
| 1475 | - $this->connection->ldapUserDisplayName . '=*'  | 
                                                        |
| 1476 | - ));  | 
                                                        |
| 1477 | -  | 
                                                        |
| 1478 | - return $filter;  | 
                                                        |
| 1479 | - }  | 
                                                        |
| 1480 | -  | 
                                                        |
| 1481 | - /**  | 
                                                        |
| 1482 | - * @param string $name  | 
                                                        |
| 1483 | - * @param string $password  | 
                                                        |
| 1484 | - * @return bool  | 
                                                        |
| 1485 | - */  | 
                                                        |
| 1486 | -	public function areCredentialsValid($name, $password) { | 
                                                        |
| 1487 | - $name = $this->helper->DNasBaseParameter($name);  | 
                                                        |
| 1488 | - $testConnection = clone $this->connection;  | 
                                                        |
| 1489 | - $credentials = array(  | 
                                                        |
| 1490 | - 'ldapAgentName' => $name,  | 
                                                        |
| 1491 | - 'ldapAgentPassword' => $password  | 
                                                        |
| 1492 | - );  | 
                                                        |
| 1493 | -		if(!$testConnection->setConfiguration($credentials)) { | 
                                                        |
| 1494 | - return false;  | 
                                                        |
| 1495 | - }  | 
                                                        |
| 1496 | - return $testConnection->bind();  | 
                                                        |
| 1497 | - }  | 
                                                        |
| 1498 | -  | 
                                                        |
| 1499 | - /**  | 
                                                        |
| 1500 | - * reverse lookup of a DN given a known UUID  | 
                                                        |
| 1501 | - *  | 
                                                        |
| 1502 | - * @param string $uuid  | 
                                                        |
| 1503 | - * @return string  | 
                                                        |
| 1504 | - * @throws \Exception  | 
                                                        |
| 1505 | - */  | 
                                                        |
| 1506 | -	public function getUserDnByUuid($uuid) { | 
                                                        |
| 1507 | - $uuidOverride = $this->connection->ldapExpertUUIDUserAttr;  | 
                                                        |
| 1508 | - $filter = $this->connection->ldapUserFilter;  | 
                                                        |
| 1509 | - $base = $this->connection->ldapBaseUsers;  | 
                                                        |
| 1510 | -  | 
                                                        |
| 1511 | -		if ($this->connection->ldapUuidUserAttribute === 'auto' && $uuidOverride === '') { | 
                                                        |
| 1512 | - // Sacrebleu! The UUID attribute is unknown :( We need first an  | 
                                                        |
| 1513 | - // existing DN to be able to reliably detect it.  | 
                                                        |
| 1514 | - $result = $this->search($filter, $base, ['dn'], 1);  | 
                                                        |
| 1515 | -			if(!isset($result[0]) || !isset($result[0]['dn'])) { | 
                                                        |
| 1516 | -				throw new \Exception('Cannot determine UUID attribute'); | 
                                                        |
| 1517 | - }  | 
                                                        |
| 1518 | - $dn = $result[0]['dn'][0];  | 
                                                        |
| 1519 | -			if(!$this->detectUuidAttribute($dn, true)) { | 
                                                        |
| 1520 | -				throw new \Exception('Cannot determine UUID attribute'); | 
                                                        |
| 1521 | - }  | 
                                                        |
| 1522 | -		} else { | 
                                                        |
| 1523 | - // The UUID attribute is either known or an override is given.  | 
                                                        |
| 1524 | - // By calling this method we ensure that $this->connection->$uuidAttr  | 
                                                        |
| 1525 | - // is definitely set  | 
                                                        |
| 1526 | -			if(!$this->detectUuidAttribute('', true)) { | 
                                                        |
| 1527 | -				throw new \Exception('Cannot determine UUID attribute'); | 
                                                        |
| 1528 | - }  | 
                                                        |
| 1529 | - }  | 
                                                        |
| 1530 | -  | 
                                                        |
| 1531 | - $uuidAttr = $this->connection->ldapUuidUserAttribute;  | 
                                                        |
| 1532 | -		if($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { | 
                                                        |
| 1533 | - $uuid = $this->formatGuid2ForFilterUser($uuid);  | 
                                                        |
| 1534 | - }  | 
                                                        |
| 1535 | -  | 
                                                        |
| 1536 | - $filter = $uuidAttr . '=' . $uuid;  | 
                                                        |
| 1537 | - $result = $this->searchUsers($filter, ['dn'], 2);  | 
                                                        |
| 1538 | -		if(is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { | 
                                                        |
| 1539 | - // we put the count into account to make sure that this is  | 
                                                        |
| 1540 | - // really unique  | 
                                                        |
| 1541 | - return $result[0]['dn'][0];  | 
                                                        |
| 1542 | - }  | 
                                                        |
| 1543 | -  | 
                                                        |
| 1544 | -		throw new \Exception('Cannot determine UUID attribute'); | 
                                                        |
| 1545 | - }  | 
                                                        |
| 1546 | -  | 
                                                        |
| 1547 | - /**  | 
                                                        |
| 1548 | - * auto-detects the directory's UUID attribute  | 
                                                        |
| 1549 | - *  | 
                                                        |
| 1550 | - * @param string $dn a known DN used to check against  | 
                                                        |
| 1551 | - * @param bool $isUser  | 
                                                        |
| 1552 | - * @param bool $force the detection should be run, even if it is not set to auto  | 
                                                        |
| 1553 | - * @param array|null $ldapRecord  | 
                                                        |
| 1554 | - * @return bool true on success, false otherwise  | 
                                                        |
| 1555 | - */  | 
                                                        |
| 1556 | -	private function detectUuidAttribute($dn, $isUser = true, $force = false, array $ldapRecord = null) { | 
                                                        |
| 1557 | -		if($isUser) { | 
                                                        |
| 1558 | - $uuidAttr = 'ldapUuidUserAttribute';  | 
                                                        |
| 1559 | - $uuidOverride = $this->connection->ldapExpertUUIDUserAttr;  | 
                                                        |
| 1560 | -		} else { | 
                                                        |
| 1561 | - $uuidAttr = 'ldapUuidGroupAttribute';  | 
                                                        |
| 1562 | - $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr;  | 
                                                        |
| 1563 | - }  | 
                                                        |
| 1564 | -  | 
                                                        |
| 1565 | -		if(($this->connection->$uuidAttr !== 'auto') && !$force) { | 
                                                        |
| 1566 | - return true;  | 
                                                        |
| 1567 | - }  | 
                                                        |
| 1568 | -  | 
                                                        |
| 1569 | -		if (is_string($uuidOverride) && trim($uuidOverride) !== '' && !$force) { | 
                                                        |
| 1570 | - $this->connection->$uuidAttr = $uuidOverride;  | 
                                                        |
| 1571 | - return true;  | 
                                                        |
| 1572 | - }  | 
                                                        |
| 1573 | -  | 
                                                        |
| 1574 | -		foreach(self::UUID_ATTRIBUTES as $attribute) { | 
                                                        |
| 1575 | -			if($ldapRecord !== null) { | 
                                                        |
| 1576 | - // we have the info from LDAP already, we don't need to talk to the server again  | 
                                                        |
| 1577 | -				if(isset($ldapRecord[$attribute])) { | 
                                                        |
| 1578 | - $this->connection->$uuidAttr = $attribute;  | 
                                                        |
| 1579 | - return true;  | 
                                                        |
| 1580 | -				} else { | 
                                                        |
| 1581 | - continue;  | 
                                                        |
| 1582 | - }  | 
                                                        |
| 1583 | - }  | 
                                                        |
| 1584 | -  | 
                                                        |
| 1585 | - $value = $this->readAttribute($dn, $attribute);  | 
                                                        |
| 1586 | -			if(is_array($value) && isset($value[0]) && !empty($value[0])) { | 
                                                        |
| 1587 | -				\OCP\Util::writeLog('user_ldap', | 
                                                        |
| 1588 | - 'Setting '.$attribute.' as '.$uuidAttr,  | 
                                                        |
| 1589 | - \OCP\Util::DEBUG);  | 
                                                        |
| 1590 | - $this->connection->$uuidAttr = $attribute;  | 
                                                        |
| 1591 | - return true;  | 
                                                        |
| 1592 | - }  | 
                                                        |
| 1593 | - }  | 
                                                        |
| 1594 | -		\OCP\Util::writeLog('user_ldap', | 
                                                        |
| 1595 | - 'Could not autodetect the UUID attribute',  | 
                                                        |
| 1596 | - \OCP\Util::ERROR);  | 
                                                        |
| 1597 | -  | 
                                                        |
| 1598 | - return false;  | 
                                                        |
| 1599 | - }  | 
                                                        |
| 1600 | -  | 
                                                        |
| 1601 | - /**  | 
                                                        |
| 1602 | - * @param string $dn  | 
                                                        |
| 1603 | - * @param bool $isUser  | 
                                                        |
| 1604 | - * @param null $ldapRecord  | 
                                                        |
| 1605 | - * @return bool|string  | 
                                                        |
| 1606 | - */  | 
                                                        |
| 1607 | -	public function getUUID($dn, $isUser = true, $ldapRecord = null) { | 
                                                        |
| 1608 | -		if($isUser) { | 
                                                        |
| 1609 | - $uuidAttr = 'ldapUuidUserAttribute';  | 
                                                        |
| 1610 | - $uuidOverride = $this->connection->ldapExpertUUIDUserAttr;  | 
                                                        |
| 1611 | -		} else { | 
                                                        |
| 1612 | - $uuidAttr = 'ldapUuidGroupAttribute';  | 
                                                        |
| 1613 | - $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr;  | 
                                                        |
| 1614 | - }  | 
                                                        |
| 1615 | -  | 
                                                        |
| 1616 | - $uuid = false;  | 
                                                        |
| 1617 | -		if($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) { | 
                                                        |
| 1618 | - $attr = $this->connection->$uuidAttr;  | 
                                                        |
| 1619 | - $uuid = isset($ldapRecord[$attr]) ? $ldapRecord[$attr] : $this->readAttribute($dn, $attr);  | 
                                                        |
| 1620 | - if( !is_array($uuid)  | 
                                                        |
| 1621 | - && $uuidOverride !== ''  | 
                                                        |
| 1622 | - && $this->detectUuidAttribute($dn, $isUser, true, $ldapRecord))  | 
                                                        |
| 1623 | -			{ | 
                                                        |
| 1624 | - $uuid = isset($ldapRecord[$this->connection->$uuidAttr])  | 
                                                        |
| 1625 | - ? $ldapRecord[$this->connection->$uuidAttr]  | 
                                                        |
| 1626 | - : $this->readAttribute($dn, $this->connection->$uuidAttr);  | 
                                                        |
| 1627 | - }  | 
                                                        |
| 1628 | -			if(is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { | 
                                                        |
| 1629 | - $uuid = $uuid[0];  | 
                                                        |
| 1630 | - }  | 
                                                        |
| 1631 | - }  | 
                                                        |
| 1632 | -  | 
                                                        |
| 1633 | - return $uuid;  | 
                                                        |
| 1634 | - }  | 
                                                        |
| 1635 | -  | 
                                                        |
| 1636 | - /**  | 
                                                        |
| 1637 | - * converts a binary ObjectGUID into a string representation  | 
                                                        |
| 1638 | - * @param string $oguid the ObjectGUID in it's binary form as retrieved from AD  | 
                                                        |
| 1639 | - * @return string  | 
                                                        |
| 1640 | - * @link http://www.php.net/manual/en/function.ldap-get-values-len.php#73198  | 
                                                        |
| 1641 | - */  | 
                                                        |
| 1642 | -	private function convertObjectGUID2Str($oguid) { | 
                                                        |
| 1643 | - $hex_guid = bin2hex($oguid);  | 
                                                        |
| 1644 | - $hex_guid_to_guid_str = '';  | 
                                                        |
| 1645 | -		for($k = 1; $k <= 4; ++$k) { | 
                                                        |
| 1646 | - $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2);  | 
                                                        |
| 1647 | - }  | 
                                                        |
| 1648 | - $hex_guid_to_guid_str .= '-';  | 
                                                        |
| 1649 | -		for($k = 1; $k <= 2; ++$k) { | 
                                                        |
| 1650 | - $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2);  | 
                                                        |
| 1651 | - }  | 
                                                        |
| 1652 | - $hex_guid_to_guid_str .= '-';  | 
                                                        |
| 1653 | -		for($k = 1; $k <= 2; ++$k) { | 
                                                        |
| 1654 | - $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2);  | 
                                                        |
| 1655 | - }  | 
                                                        |
| 1656 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4);  | 
                                                        |
| 1657 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20);  | 
                                                        |
| 1658 | -  | 
                                                        |
| 1659 | - return strtoupper($hex_guid_to_guid_str);  | 
                                                        |
| 1660 | - }  | 
                                                        |
| 1661 | -  | 
                                                        |
| 1662 | - /**  | 
                                                        |
| 1663 | - * the first three blocks of the string-converted GUID happen to be in  | 
                                                        |
| 1664 | - * reverse order. In order to use it in a filter, this needs to be  | 
                                                        |
| 1665 | - * corrected. Furthermore the dashes need to be replaced and \\ preprended  | 
                                                        |
| 1666 | - * to every two hax figures.  | 
                                                        |
| 1667 | - *  | 
                                                        |
| 1668 | - * If an invalid string is passed, it will be returned without change.  | 
                                                        |
| 1669 | - *  | 
                                                        |
| 1670 | - * @param string $guid  | 
                                                        |
| 1671 | - * @return string  | 
                                                        |
| 1672 | - */  | 
                                                        |
| 1673 | -	public function formatGuid2ForFilterUser($guid) { | 
                                                        |
| 1674 | -		if(!is_string($guid)) { | 
                                                        |
| 1675 | -			throw new \InvalidArgumentException('String expected'); | 
                                                        |
| 1676 | - }  | 
                                                        |
| 1677 | -		$blocks = explode('-', $guid); | 
                                                        |
| 1678 | -		if(count($blocks) !== 5) { | 
                                                        |
| 1679 | - /*  | 
                                                        |
| 1198 | + $findings = [];  | 
                                                        |
| 1199 | + $savedoffset = $offset;  | 
                                                        |
| 1200 | +        do { | 
                                                        |
| 1201 | + $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset);  | 
                                                        |
| 1202 | +            if($search === false) { | 
                                                        |
| 1203 | + return [];  | 
                                                        |
| 1204 | + }  | 
                                                        |
| 1205 | + list($sr, $pagedSearchOK) = $search;  | 
                                                        |
| 1206 | + $cr = $this->connection->getConnectionResource();  | 
                                                        |
| 1207 | +  | 
                                                        |
| 1208 | +            if($skipHandling) { | 
                                                        |
| 1209 | + //i.e. result do not need to be fetched, we just need the cookie  | 
                                                        |
| 1210 | + //thus pass 1 or any other value as $iFoundItems because it is not  | 
                                                        |
| 1211 | + //used  | 
                                                        |
| 1212 | + $this->processPagedSearchStatus($sr, $filter, $base, 1, $limitPerPage,  | 
                                                        |
| 1213 | + $offset, $pagedSearchOK,  | 
                                                        |
| 1214 | + $skipHandling);  | 
                                                        |
| 1215 | + return array();  | 
                                                        |
| 1216 | + }  | 
                                                        |
| 1217 | +  | 
                                                        |
| 1218 | + $iFoundItems = 0;  | 
                                                        |
| 1219 | +            foreach($sr as $res) { | 
                                                        |
| 1220 | +                $findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $res)); | 
                                                        |
| 1221 | + $iFoundItems = max($iFoundItems, $findings['count']);  | 
                                                        |
| 1222 | + unset($findings['count']);  | 
                                                        |
| 1223 | + }  | 
                                                        |
| 1224 | +  | 
                                                        |
| 1225 | + $continue = $this->processPagedSearchStatus($sr, $filter, $base, $iFoundItems,  | 
                                                        |
| 1226 | + $limitPerPage, $offset, $pagedSearchOK,  | 
                                                        |
| 1227 | + $skipHandling);  | 
                                                        |
| 1228 | + $offset += $limitPerPage;  | 
                                                        |
| 1229 | + } while ($continue && $pagedSearchOK && ($limit === null || count($findings) < $limit));  | 
                                                        |
| 1230 | + // reseting offset  | 
                                                        |
| 1231 | + $offset = $savedoffset;  | 
                                                        |
| 1232 | +  | 
                                                        |
| 1233 | + // if we're here, probably no connection resource is returned.  | 
                                                        |
| 1234 | + // to make Nextcloud behave nicely, we simply give back an empty array.  | 
                                                        |
| 1235 | +        if(is_null($findings)) { | 
                                                        |
| 1236 | + return array();  | 
                                                        |
| 1237 | + }  | 
                                                        |
| 1238 | +  | 
                                                        |
| 1239 | +        if(!is_null($attr)) { | 
                                                        |
| 1240 | + $selection = [];  | 
                                                        |
| 1241 | + $i = 0;  | 
                                                        |
| 1242 | +            foreach($findings as $item) { | 
                                                        |
| 1243 | +                if(!is_array($item)) { | 
                                                        |
| 1244 | + continue;  | 
                                                        |
| 1245 | + }  | 
                                                        |
| 1246 | + $item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8');  | 
                                                        |
| 1247 | +                foreach($attr as $key) { | 
                                                        |
| 1248 | +                    if(isset($item[$key])) { | 
                                                        |
| 1249 | +                        if(is_array($item[$key]) && isset($item[$key]['count'])) { | 
                                                        |
| 1250 | + unset($item[$key]['count']);  | 
                                                        |
| 1251 | + }  | 
                                                        |
| 1252 | +                        if($key !== 'dn') { | 
                                                        |
| 1253 | + $selection[$i][$key] = $this->resemblesDN($key) ?  | 
                                                        |
| 1254 | + $this->helper->sanitizeDN($item[$key])  | 
                                                        |
| 1255 | + : $key === 'objectguid' || $key === 'guid' ?  | 
                                                        |
| 1256 | + $selection[$i][$key] = $this->convertObjectGUID2Str($item[$key])  | 
                                                        |
| 1257 | + : $item[$key];  | 
                                                        |
| 1258 | +                        } else { | 
                                                        |
| 1259 | + $selection[$i][$key] = [$this->helper->sanitizeDN($item[$key])];  | 
                                                        |
| 1260 | + }  | 
                                                        |
| 1261 | + }  | 
                                                        |
| 1262 | +  | 
                                                        |
| 1263 | + }  | 
                                                        |
| 1264 | + $i++;  | 
                                                        |
| 1265 | + }  | 
                                                        |
| 1266 | + $findings = $selection;  | 
                                                        |
| 1267 | + }  | 
                                                        |
| 1268 | + //we slice the findings, when  | 
                                                        |
| 1269 | + //a) paged search unsuccessful, though attempted  | 
                                                        |
| 1270 | + //b) no paged search, but limit set  | 
                                                        |
| 1271 | + if((!$this->getPagedSearchResultState()  | 
                                                        |
| 1272 | + && $pagedSearchOK)  | 
                                                        |
| 1273 | + || (  | 
                                                        |
| 1274 | + !$pagedSearchOK  | 
                                                        |
| 1275 | + && !is_null($limit)  | 
                                                        |
| 1276 | + )  | 
                                                        |
| 1277 | +        ) { | 
                                                        |
| 1278 | + $findings = array_slice($findings, intval($offset), $limit);  | 
                                                        |
| 1279 | + }  | 
                                                        |
| 1280 | + return $findings;  | 
                                                        |
| 1281 | + }  | 
                                                        |
| 1282 | +  | 
                                                        |
| 1283 | + /**  | 
                                                        |
| 1284 | + * @param string $name  | 
                                                        |
| 1285 | + * @return bool|mixed|string  | 
                                                        |
| 1286 | + */  | 
                                                        |
| 1287 | +    public function sanitizeUsername($name) { | 
                                                        |
| 1288 | +        if($this->connection->ldapIgnoreNamingRules) { | 
                                                        |
| 1289 | + return $name;  | 
                                                        |
| 1290 | + }  | 
                                                        |
| 1291 | +  | 
                                                        |
| 1292 | + // Transliteration  | 
                                                        |
| 1293 | + // latin characters to ASCII  | 
                                                        |
| 1294 | +        $name = iconv('UTF-8', 'ASCII//TRANSLIT', $name); | 
                                                        |
| 1295 | +  | 
                                                        |
| 1296 | + // Replacements  | 
                                                        |
| 1297 | +        $name = str_replace(' ', '_', $name); | 
                                                        |
| 1298 | +  | 
                                                        |
| 1299 | + // Every remaining disallowed characters will be removed  | 
                                                        |
| 1300 | +        $name = preg_replace('/[^a-zA-Z0-9_.@-]/u', '', $name); | 
                                                        |
| 1301 | +  | 
                                                        |
| 1302 | + return $name;  | 
                                                        |
| 1303 | + }  | 
                                                        |
| 1304 | +  | 
                                                        |
| 1305 | + /**  | 
                                                        |
| 1306 | + * escapes (user provided) parts for LDAP filter  | 
                                                        |
| 1307 | + * @param string $input, the provided value  | 
                                                        |
| 1308 | + * @param bool $allowAsterisk whether in * at the beginning should be preserved  | 
                                                        |
| 1309 | + * @return string the escaped string  | 
                                                        |
| 1310 | + */  | 
                                                        |
| 1311 | +    public function escapeFilterPart($input, $allowAsterisk = false) { | 
                                                        |
| 1312 | + $asterisk = '';  | 
                                                        |
| 1313 | +        if($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { | 
                                                        |
| 1314 | + $asterisk = '*';  | 
                                                        |
| 1315 | + $input = mb_substr($input, 1, null, 'UTF-8');  | 
                                                        |
| 1316 | + }  | 
                                                        |
| 1317 | +        $search  = array('*', '\\', '(', ')'); | 
                                                        |
| 1318 | +        $replace = array('\\*', '\\\\', '\\(', '\\)'); | 
                                                        |
| 1319 | + return $asterisk . str_replace($search, $replace, $input);  | 
                                                        |
| 1320 | + }  | 
                                                        |
| 1321 | +  | 
                                                        |
| 1322 | + /**  | 
                                                        |
| 1323 | + * combines the input filters with AND  | 
                                                        |
| 1324 | + * @param string[] $filters the filters to connect  | 
                                                        |
| 1325 | + * @return string the combined filter  | 
                                                        |
| 1326 | + */  | 
                                                        |
| 1327 | +    public function combineFilterWithAnd($filters) { | 
                                                        |
| 1328 | + return $this->combineFilter($filters, '&');  | 
                                                        |
| 1329 | + }  | 
                                                        |
| 1330 | +  | 
                                                        |
| 1331 | + /**  | 
                                                        |
| 1332 | + * combines the input filters with OR  | 
                                                        |
| 1333 | + * @param string[] $filters the filters to connect  | 
                                                        |
| 1334 | + * @return string the combined filter  | 
                                                        |
| 1335 | + * Combines Filter arguments with OR  | 
                                                        |
| 1336 | + */  | 
                                                        |
| 1337 | +    public function combineFilterWithOr($filters) { | 
                                                        |
| 1338 | + return $this->combineFilter($filters, '|');  | 
                                                        |
| 1339 | + }  | 
                                                        |
| 1340 | +  | 
                                                        |
| 1341 | + /**  | 
                                                        |
| 1342 | + * combines the input filters with given operator  | 
                                                        |
| 1343 | + * @param string[] $filters the filters to connect  | 
                                                        |
| 1344 | + * @param string $operator either & or |  | 
                                                        |
| 1345 | + * @return string the combined filter  | 
                                                        |
| 1346 | + */  | 
                                                        |
| 1347 | +    private function combineFilter($filters, $operator) { | 
                                                        |
| 1348 | +        $combinedFilter = '('.$operator; | 
                                                        |
| 1349 | +        foreach($filters as $filter) { | 
                                                        |
| 1350 | +            if ($filter !== '' && $filter[0] !== '(') { | 
                                                        |
| 1351 | +                $filter = '('.$filter.')'; | 
                                                        |
| 1352 | + }  | 
                                                        |
| 1353 | + $combinedFilter.=$filter;  | 
                                                        |
| 1354 | + }  | 
                                                        |
| 1355 | + $combinedFilter.=')';  | 
                                                        |
| 1356 | + return $combinedFilter;  | 
                                                        |
| 1357 | + }  | 
                                                        |
| 1358 | +  | 
                                                        |
| 1359 | + /**  | 
                                                        |
| 1360 | + * creates a filter part for to perform search for users  | 
                                                        |
| 1361 | + * @param string $search the search term  | 
                                                        |
| 1362 | + * @return string the final filter part to use in LDAP searches  | 
                                                        |
| 1363 | + */  | 
                                                        |
| 1364 | +    public function getFilterPartForUserSearch($search) { | 
                                                        |
| 1365 | + return $this->getFilterPartForSearch($search,  | 
                                                        |
| 1366 | + $this->connection->ldapAttributesForUserSearch,  | 
                                                        |
| 1367 | + $this->connection->ldapUserDisplayName);  | 
                                                        |
| 1368 | + }  | 
                                                        |
| 1369 | +  | 
                                                        |
| 1370 | + /**  | 
                                                        |
| 1371 | + * creates a filter part for to perform search for groups  | 
                                                        |
| 1372 | + * @param string $search the search term  | 
                                                        |
| 1373 | + * @return string the final filter part to use in LDAP searches  | 
                                                        |
| 1374 | + */  | 
                                                        |
| 1375 | +    public function getFilterPartForGroupSearch($search) { | 
                                                        |
| 1376 | + return $this->getFilterPartForSearch($search,  | 
                                                        |
| 1377 | + $this->connection->ldapAttributesForGroupSearch,  | 
                                                        |
| 1378 | + $this->connection->ldapGroupDisplayName);  | 
                                                        |
| 1379 | + }  | 
                                                        |
| 1380 | +  | 
                                                        |
| 1381 | + /**  | 
                                                        |
| 1382 | + * creates a filter part for searches by splitting up the given search  | 
                                                        |
| 1383 | + * string into single words  | 
                                                        |
| 1384 | + * @param string $search the search term  | 
                                                        |
| 1385 | + * @param string[] $searchAttributes needs to have at least two attributes,  | 
                                                        |
| 1386 | + * otherwise it does not make sense :)  | 
                                                        |
| 1387 | + * @return string the final filter part to use in LDAP searches  | 
                                                        |
| 1388 | + * @throws \Exception  | 
                                                        |
| 1389 | + */  | 
                                                        |
| 1390 | +    private function getAdvancedFilterPartForSearch($search, $searchAttributes) { | 
                                                        |
| 1391 | +        if(!is_array($searchAttributes) || count($searchAttributes) < 2) { | 
                                                        |
| 1392 | +            throw new \Exception('searchAttributes must be an array with at least two string'); | 
                                                        |
| 1393 | + }  | 
                                                        |
| 1394 | +        $searchWords = explode(' ', trim($search)); | 
                                                        |
| 1395 | + $wordFilters = array();  | 
                                                        |
| 1396 | +        foreach($searchWords as $word) { | 
                                                        |
| 1397 | + $word = $this->prepareSearchTerm($word);  | 
                                                        |
| 1398 | + //every word needs to appear at least once  | 
                                                        |
| 1399 | + $wordMatchOneAttrFilters = array();  | 
                                                        |
| 1400 | +            foreach($searchAttributes as $attr) { | 
                                                        |
| 1401 | + $wordMatchOneAttrFilters[] = $attr . '=' . $word;  | 
                                                        |
| 1402 | + }  | 
                                                        |
| 1403 | + $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters);  | 
                                                        |
| 1404 | + }  | 
                                                        |
| 1405 | + return $this->combineFilterWithAnd($wordFilters);  | 
                                                        |
| 1406 | + }  | 
                                                        |
| 1407 | +  | 
                                                        |
| 1408 | + /**  | 
                                                        |
| 1409 | + * creates a filter part for searches  | 
                                                        |
| 1410 | + * @param string $search the search term  | 
                                                        |
| 1411 | + * @param string[]|null $searchAttributes  | 
                                                        |
| 1412 | + * @param string $fallbackAttribute a fallback attribute in case the user  | 
                                                        |
| 1413 | + * did not define search attributes. Typically the display name attribute.  | 
                                                        |
| 1414 | + * @return string the final filter part to use in LDAP searches  | 
                                                        |
| 1415 | + */  | 
                                                        |
| 1416 | +    private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { | 
                                                        |
| 1417 | + $filter = array();  | 
                                                        |
| 1418 | + $haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0);  | 
                                                        |
| 1419 | +        if($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { | 
                                                        |
| 1420 | +            try { | 
                                                        |
| 1421 | + return $this->getAdvancedFilterPartForSearch($search, $searchAttributes);  | 
                                                        |
| 1422 | +            } catch(\Exception $e) { | 
                                                        |
| 1423 | + \OCP\Util::writeLog(  | 
                                                        |
| 1424 | + 'user_ldap',  | 
                                                        |
| 1425 | + 'Creating advanced filter for search failed, falling back to simple method.',  | 
                                                        |
| 1426 | + \OCP\Util::INFO  | 
                                                        |
| 1427 | + );  | 
                                                        |
| 1428 | + }  | 
                                                        |
| 1429 | + }  | 
                                                        |
| 1430 | +  | 
                                                        |
| 1431 | + $search = $this->prepareSearchTerm($search);  | 
                                                        |
| 1432 | +        if(!is_array($searchAttributes) || count($searchAttributes) === 0) { | 
                                                        |
| 1433 | +            if ($fallbackAttribute === '') { | 
                                                        |
| 1434 | + return '';  | 
                                                        |
| 1435 | + }  | 
                                                        |
| 1436 | + $filter[] = $fallbackAttribute . '=' . $search;  | 
                                                        |
| 1437 | +        } else { | 
                                                        |
| 1438 | +            foreach($searchAttributes as $attribute) { | 
                                                        |
| 1439 | + $filter[] = $attribute . '=' . $search;  | 
                                                        |
| 1440 | + }  | 
                                                        |
| 1441 | + }  | 
                                                        |
| 1442 | +        if(count($filter) === 1) { | 
                                                        |
| 1443 | +            return '('.$filter[0].')'; | 
                                                        |
| 1444 | + }  | 
                                                        |
| 1445 | + return $this->combineFilterWithOr($filter);  | 
                                                        |
| 1446 | + }  | 
                                                        |
| 1447 | +  | 
                                                        |
| 1448 | + /**  | 
                                                        |
| 1449 | + * returns the search term depending on whether we are allowed  | 
                                                        |
| 1450 | + * list users found by ldap with the current input appended by  | 
                                                        |
| 1451 | + * a *  | 
                                                        |
| 1452 | + * @return string  | 
                                                        |
| 1453 | + */  | 
                                                        |
| 1454 | +    private function prepareSearchTerm($term) { | 
                                                        |
| 1455 | + $config = \OC::$server->getConfig();  | 
                                                        |
| 1456 | +  | 
                                                        |
| 1457 | +        $allowEnum = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes'); | 
                                                        |
| 1458 | +  | 
                                                        |
| 1459 | + $result = $term;  | 
                                                        |
| 1460 | +        if ($term === '') { | 
                                                        |
| 1461 | + $result = '*';  | 
                                                        |
| 1462 | +        } else if ($allowEnum !== 'no') { | 
                                                        |
| 1463 | + $result = $term . '*';  | 
                                                        |
| 1464 | + }  | 
                                                        |
| 1465 | + return $result;  | 
                                                        |
| 1466 | + }  | 
                                                        |
| 1467 | +  | 
                                                        |
| 1468 | + /**  | 
                                                        |
| 1469 | + * returns the filter used for counting users  | 
                                                        |
| 1470 | + * @return string  | 
                                                        |
| 1471 | + */  | 
                                                        |
| 1472 | +    public function getFilterForUserCount() { | 
                                                        |
| 1473 | + $filter = $this->combineFilterWithAnd(array(  | 
                                                        |
| 1474 | + $this->connection->ldapUserFilter,  | 
                                                        |
| 1475 | + $this->connection->ldapUserDisplayName . '=*'  | 
                                                        |
| 1476 | + ));  | 
                                                        |
| 1477 | +  | 
                                                        |
| 1478 | + return $filter;  | 
                                                        |
| 1479 | + }  | 
                                                        |
| 1480 | +  | 
                                                        |
| 1481 | + /**  | 
                                                        |
| 1482 | + * @param string $name  | 
                                                        |
| 1483 | + * @param string $password  | 
                                                        |
| 1484 | + * @return bool  | 
                                                        |
| 1485 | + */  | 
                                                        |
| 1486 | +    public function areCredentialsValid($name, $password) { | 
                                                        |
| 1487 | + $name = $this->helper->DNasBaseParameter($name);  | 
                                                        |
| 1488 | + $testConnection = clone $this->connection;  | 
                                                        |
| 1489 | + $credentials = array(  | 
                                                        |
| 1490 | + 'ldapAgentName' => $name,  | 
                                                        |
| 1491 | + 'ldapAgentPassword' => $password  | 
                                                        |
| 1492 | + );  | 
                                                        |
| 1493 | +        if(!$testConnection->setConfiguration($credentials)) { | 
                                                        |
| 1494 | + return false;  | 
                                                        |
| 1495 | + }  | 
                                                        |
| 1496 | + return $testConnection->bind();  | 
                                                        |
| 1497 | + }  | 
                                                        |
| 1498 | +  | 
                                                        |
| 1499 | + /**  | 
                                                        |
| 1500 | + * reverse lookup of a DN given a known UUID  | 
                                                        |
| 1501 | + *  | 
                                                        |
| 1502 | + * @param string $uuid  | 
                                                        |
| 1503 | + * @return string  | 
                                                        |
| 1504 | + * @throws \Exception  | 
                                                        |
| 1505 | + */  | 
                                                        |
| 1506 | +    public function getUserDnByUuid($uuid) { | 
                                                        |
| 1507 | + $uuidOverride = $this->connection->ldapExpertUUIDUserAttr;  | 
                                                        |
| 1508 | + $filter = $this->connection->ldapUserFilter;  | 
                                                        |
| 1509 | + $base = $this->connection->ldapBaseUsers;  | 
                                                        |
| 1510 | +  | 
                                                        |
| 1511 | +        if ($this->connection->ldapUuidUserAttribute === 'auto' && $uuidOverride === '') { | 
                                                        |
| 1512 | + // Sacrebleu! The UUID attribute is unknown :( We need first an  | 
                                                        |
| 1513 | + // existing DN to be able to reliably detect it.  | 
                                                        |
| 1514 | + $result = $this->search($filter, $base, ['dn'], 1);  | 
                                                        |
| 1515 | +            if(!isset($result[0]) || !isset($result[0]['dn'])) { | 
                                                        |
| 1516 | +                throw new \Exception('Cannot determine UUID attribute'); | 
                                                        |
| 1517 | + }  | 
                                                        |
| 1518 | + $dn = $result[0]['dn'][0];  | 
                                                        |
| 1519 | +            if(!$this->detectUuidAttribute($dn, true)) { | 
                                                        |
| 1520 | +                throw new \Exception('Cannot determine UUID attribute'); | 
                                                        |
| 1521 | + }  | 
                                                        |
| 1522 | +        } else { | 
                                                        |
| 1523 | + // The UUID attribute is either known or an override is given.  | 
                                                        |
| 1524 | + // By calling this method we ensure that $this->connection->$uuidAttr  | 
                                                        |
| 1525 | + // is definitely set  | 
                                                        |
| 1526 | +            if(!$this->detectUuidAttribute('', true)) { | 
                                                        |
| 1527 | +                throw new \Exception('Cannot determine UUID attribute'); | 
                                                        |
| 1528 | + }  | 
                                                        |
| 1529 | + }  | 
                                                        |
| 1530 | +  | 
                                                        |
| 1531 | + $uuidAttr = $this->connection->ldapUuidUserAttribute;  | 
                                                        |
| 1532 | +        if($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { | 
                                                        |
| 1533 | + $uuid = $this->formatGuid2ForFilterUser($uuid);  | 
                                                        |
| 1534 | + }  | 
                                                        |
| 1535 | +  | 
                                                        |
| 1536 | + $filter = $uuidAttr . '=' . $uuid;  | 
                                                        |
| 1537 | + $result = $this->searchUsers($filter, ['dn'], 2);  | 
                                                        |
| 1538 | +        if(is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { | 
                                                        |
| 1539 | + // we put the count into account to make sure that this is  | 
                                                        |
| 1540 | + // really unique  | 
                                                        |
| 1541 | + return $result[0]['dn'][0];  | 
                                                        |
| 1542 | + }  | 
                                                        |
| 1543 | +  | 
                                                        |
| 1544 | +        throw new \Exception('Cannot determine UUID attribute'); | 
                                                        |
| 1545 | + }  | 
                                                        |
| 1546 | +  | 
                                                        |
| 1547 | + /**  | 
                                                        |
| 1548 | + * auto-detects the directory's UUID attribute  | 
                                                        |
| 1549 | + *  | 
                                                        |
| 1550 | + * @param string $dn a known DN used to check against  | 
                                                        |
| 1551 | + * @param bool $isUser  | 
                                                        |
| 1552 | + * @param bool $force the detection should be run, even if it is not set to auto  | 
                                                        |
| 1553 | + * @param array|null $ldapRecord  | 
                                                        |
| 1554 | + * @return bool true on success, false otherwise  | 
                                                        |
| 1555 | + */  | 
                                                        |
| 1556 | +    private function detectUuidAttribute($dn, $isUser = true, $force = false, array $ldapRecord = null) { | 
                                                        |
| 1557 | +        if($isUser) { | 
                                                        |
| 1558 | + $uuidAttr = 'ldapUuidUserAttribute';  | 
                                                        |
| 1559 | + $uuidOverride = $this->connection->ldapExpertUUIDUserAttr;  | 
                                                        |
| 1560 | +        } else { | 
                                                        |
| 1561 | + $uuidAttr = 'ldapUuidGroupAttribute';  | 
                                                        |
| 1562 | + $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr;  | 
                                                        |
| 1563 | + }  | 
                                                        |
| 1564 | +  | 
                                                        |
| 1565 | +        if(($this->connection->$uuidAttr !== 'auto') && !$force) { | 
                                                        |
| 1566 | + return true;  | 
                                                        |
| 1567 | + }  | 
                                                        |
| 1568 | +  | 
                                                        |
| 1569 | +        if (is_string($uuidOverride) && trim($uuidOverride) !== '' && !$force) { | 
                                                        |
| 1570 | + $this->connection->$uuidAttr = $uuidOverride;  | 
                                                        |
| 1571 | + return true;  | 
                                                        |
| 1572 | + }  | 
                                                        |
| 1573 | +  | 
                                                        |
| 1574 | +        foreach(self::UUID_ATTRIBUTES as $attribute) { | 
                                                        |
| 1575 | +            if($ldapRecord !== null) { | 
                                                        |
| 1576 | + // we have the info from LDAP already, we don't need to talk to the server again  | 
                                                        |
| 1577 | +                if(isset($ldapRecord[$attribute])) { | 
                                                        |
| 1578 | + $this->connection->$uuidAttr = $attribute;  | 
                                                        |
| 1579 | + return true;  | 
                                                        |
| 1580 | +                } else { | 
                                                        |
| 1581 | + continue;  | 
                                                        |
| 1582 | + }  | 
                                                        |
| 1583 | + }  | 
                                                        |
| 1584 | +  | 
                                                        |
| 1585 | + $value = $this->readAttribute($dn, $attribute);  | 
                                                        |
| 1586 | +            if(is_array($value) && isset($value[0]) && !empty($value[0])) { | 
                                                        |
| 1587 | +                \OCP\Util::writeLog('user_ldap', | 
                                                        |
| 1588 | + 'Setting '.$attribute.' as '.$uuidAttr,  | 
                                                        |
| 1589 | + \OCP\Util::DEBUG);  | 
                                                        |
| 1590 | + $this->connection->$uuidAttr = $attribute;  | 
                                                        |
| 1591 | + return true;  | 
                                                        |
| 1592 | + }  | 
                                                        |
| 1593 | + }  | 
                                                        |
| 1594 | +        \OCP\Util::writeLog('user_ldap', | 
                                                        |
| 1595 | + 'Could not autodetect the UUID attribute',  | 
                                                        |
| 1596 | + \OCP\Util::ERROR);  | 
                                                        |
| 1597 | +  | 
                                                        |
| 1598 | + return false;  | 
                                                        |
| 1599 | + }  | 
                                                        |
| 1600 | +  | 
                                                        |
| 1601 | + /**  | 
                                                        |
| 1602 | + * @param string $dn  | 
                                                        |
| 1603 | + * @param bool $isUser  | 
                                                        |
| 1604 | + * @param null $ldapRecord  | 
                                                        |
| 1605 | + * @return bool|string  | 
                                                        |
| 1606 | + */  | 
                                                        |
| 1607 | +    public function getUUID($dn, $isUser = true, $ldapRecord = null) { | 
                                                        |
| 1608 | +        if($isUser) { | 
                                                        |
| 1609 | + $uuidAttr = 'ldapUuidUserAttribute';  | 
                                                        |
| 1610 | + $uuidOverride = $this->connection->ldapExpertUUIDUserAttr;  | 
                                                        |
| 1611 | +        } else { | 
                                                        |
| 1612 | + $uuidAttr = 'ldapUuidGroupAttribute';  | 
                                                        |
| 1613 | + $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr;  | 
                                                        |
| 1614 | + }  | 
                                                        |
| 1615 | +  | 
                                                        |
| 1616 | + $uuid = false;  | 
                                                        |
| 1617 | +        if($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) { | 
                                                        |
| 1618 | + $attr = $this->connection->$uuidAttr;  | 
                                                        |
| 1619 | + $uuid = isset($ldapRecord[$attr]) ? $ldapRecord[$attr] : $this->readAttribute($dn, $attr);  | 
                                                        |
| 1620 | + if( !is_array($uuid)  | 
                                                        |
| 1621 | + && $uuidOverride !== ''  | 
                                                        |
| 1622 | + && $this->detectUuidAttribute($dn, $isUser, true, $ldapRecord))  | 
                                                        |
| 1623 | +            { | 
                                                        |
| 1624 | + $uuid = isset($ldapRecord[$this->connection->$uuidAttr])  | 
                                                        |
| 1625 | + ? $ldapRecord[$this->connection->$uuidAttr]  | 
                                                        |
| 1626 | + : $this->readAttribute($dn, $this->connection->$uuidAttr);  | 
                                                        |
| 1627 | + }  | 
                                                        |
| 1628 | +            if(is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { | 
                                                        |
| 1629 | + $uuid = $uuid[0];  | 
                                                        |
| 1630 | + }  | 
                                                        |
| 1631 | + }  | 
                                                        |
| 1632 | +  | 
                                                        |
| 1633 | + return $uuid;  | 
                                                        |
| 1634 | + }  | 
                                                        |
| 1635 | +  | 
                                                        |
| 1636 | + /**  | 
                                                        |
| 1637 | + * converts a binary ObjectGUID into a string representation  | 
                                                        |
| 1638 | + * @param string $oguid the ObjectGUID in it's binary form as retrieved from AD  | 
                                                        |
| 1639 | + * @return string  | 
                                                        |
| 1640 | + * @link http://www.php.net/manual/en/function.ldap-get-values-len.php#73198  | 
                                                        |
| 1641 | + */  | 
                                                        |
| 1642 | +    private function convertObjectGUID2Str($oguid) { | 
                                                        |
| 1643 | + $hex_guid = bin2hex($oguid);  | 
                                                        |
| 1644 | + $hex_guid_to_guid_str = '';  | 
                                                        |
| 1645 | +        for($k = 1; $k <= 4; ++$k) { | 
                                                        |
| 1646 | + $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2);  | 
                                                        |
| 1647 | + }  | 
                                                        |
| 1648 | + $hex_guid_to_guid_str .= '-';  | 
                                                        |
| 1649 | +        for($k = 1; $k <= 2; ++$k) { | 
                                                        |
| 1650 | + $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2);  | 
                                                        |
| 1651 | + }  | 
                                                        |
| 1652 | + $hex_guid_to_guid_str .= '-';  | 
                                                        |
| 1653 | +        for($k = 1; $k <= 2; ++$k) { | 
                                                        |
| 1654 | + $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2);  | 
                                                        |
| 1655 | + }  | 
                                                        |
| 1656 | + $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4);  | 
                                                        |
| 1657 | + $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20);  | 
                                                        |
| 1658 | +  | 
                                                        |
| 1659 | + return strtoupper($hex_guid_to_guid_str);  | 
                                                        |
| 1660 | + }  | 
                                                        |
| 1661 | +  | 
                                                        |
| 1662 | + /**  | 
                                                        |
| 1663 | + * the first three blocks of the string-converted GUID happen to be in  | 
                                                        |
| 1664 | + * reverse order. In order to use it in a filter, this needs to be  | 
                                                        |
| 1665 | + * corrected. Furthermore the dashes need to be replaced and \\ preprended  | 
                                                        |
| 1666 | + * to every two hax figures.  | 
                                                        |
| 1667 | + *  | 
                                                        |
| 1668 | + * If an invalid string is passed, it will be returned without change.  | 
                                                        |
| 1669 | + *  | 
                                                        |
| 1670 | + * @param string $guid  | 
                                                        |
| 1671 | + * @return string  | 
                                                        |
| 1672 | + */  | 
                                                        |
| 1673 | +    public function formatGuid2ForFilterUser($guid) { | 
                                                        |
| 1674 | +        if(!is_string($guid)) { | 
                                                        |
| 1675 | +            throw new \InvalidArgumentException('String expected'); | 
                                                        |
| 1676 | + }  | 
                                                        |
| 1677 | +        $blocks = explode('-', $guid); | 
                                                        |
| 1678 | +        if(count($blocks) !== 5) { | 
                                                        |
| 1679 | + /*  | 
                                                        |
| 1680 | 1680 | * Why not throw an Exception instead? This method is a utility  | 
                                                        
| 1681 | 1681 | * called only when trying to figure out whether a "missing" known  | 
                                                        
| 1682 | 1682 | * LDAP user was or was not renamed on the LDAP server. And this  | 
                                                        
@@ -1687,274 +1687,274 @@ discard block  | 
                                                    ||
| 1687 | 1687 | * an exception here would kill the experience for a valid, acting  | 
                                                        
| 1688 | 1688 | * user. Instead we write a log message.  | 
                                                        
| 1689 | 1689 | */  | 
                                                        
| 1690 | - \OC::$server->getLogger()->info(  | 
                                                        |
| 1691 | - 'Passed string does not resemble a valid GUID. Known UUID ' .  | 
                                                        |
| 1692 | -				'({uuid}) probably does not match UUID configuration.', | 
                                                        |
| 1693 | - [ 'app' => 'user_ldap', 'uuid' => $guid ]  | 
                                                        |
| 1694 | - );  | 
                                                        |
| 1695 | - return $guid;  | 
                                                        |
| 1696 | - }  | 
                                                        |
| 1697 | -		for($i=0; $i < 3; $i++) { | 
                                                        |
| 1698 | - $pairs = str_split($blocks[$i], 2);  | 
                                                        |
| 1699 | - $pairs = array_reverse($pairs);  | 
                                                        |
| 1700 | -			$blocks[$i] = implode('', $pairs); | 
                                                        |
| 1701 | - }  | 
                                                        |
| 1702 | -		for($i=0; $i < 5; $i++) { | 
                                                        |
| 1703 | - $pairs = str_split($blocks[$i], 2);  | 
                                                        |
| 1704 | -			$blocks[$i] = '\\' . implode('\\', $pairs); | 
                                                        |
| 1705 | - }  | 
                                                        |
| 1706 | -		return implode('', $blocks); | 
                                                        |
| 1707 | - }  | 
                                                        |
| 1708 | -  | 
                                                        |
| 1709 | - /**  | 
                                                        |
| 1710 | - * gets a SID of the domain of the given dn  | 
                                                        |
| 1711 | - * @param string $dn  | 
                                                        |
| 1712 | - * @return string|bool  | 
                                                        |
| 1713 | - */  | 
                                                        |
| 1714 | -	public function getSID($dn) { | 
                                                        |
| 1715 | - $domainDN = $this->getDomainDNFromDN($dn);  | 
                                                        |
| 1716 | - $cacheKey = 'getSID-'.$domainDN;  | 
                                                        |
| 1717 | - $sid = $this->connection->getFromCache($cacheKey);  | 
                                                        |
| 1718 | -		if(!is_null($sid)) { | 
                                                        |
| 1719 | - return $sid;  | 
                                                        |
| 1720 | - }  | 
                                                        |
| 1721 | -  | 
                                                        |
| 1722 | - $objectSid = $this->readAttribute($domainDN, 'objectsid');  | 
                                                        |
| 1723 | -		if(!is_array($objectSid) || empty($objectSid)) { | 
                                                        |
| 1724 | - $this->connection->writeToCache($cacheKey, false);  | 
                                                        |
| 1725 | - return false;  | 
                                                        |
| 1726 | - }  | 
                                                        |
| 1727 | - $domainObjectSid = $this->convertSID2Str($objectSid[0]);  | 
                                                        |
| 1728 | - $this->connection->writeToCache($cacheKey, $domainObjectSid);  | 
                                                        |
| 1729 | -  | 
                                                        |
| 1730 | - return $domainObjectSid;  | 
                                                        |
| 1731 | - }  | 
                                                        |
| 1732 | -  | 
                                                        |
| 1733 | - /**  | 
                                                        |
| 1734 | - * converts a binary SID into a string representation  | 
                                                        |
| 1735 | - * @param string $sid  | 
                                                        |
| 1736 | - * @return string  | 
                                                        |
| 1737 | - */  | 
                                                        |
| 1738 | -	public function convertSID2Str($sid) { | 
                                                        |
| 1739 | - // The format of a SID binary string is as follows:  | 
                                                        |
| 1740 | - // 1 byte for the revision level  | 
                                                        |
| 1741 | - // 1 byte for the number n of variable sub-ids  | 
                                                        |
| 1742 | - // 6 bytes for identifier authority value  | 
                                                        |
| 1743 | - // n*4 bytes for n sub-ids  | 
                                                        |
| 1744 | - //  | 
                                                        |
| 1745 | - // Example: 010400000000000515000000a681e50e4d6c6c2bca32055f  | 
                                                        |
| 1746 | - // Legend: RRNNAAAAAAAAAAAA11111111222222223333333344444444  | 
                                                        |
| 1747 | - $revision = ord($sid[0]);  | 
                                                        |
| 1748 | - $numberSubID = ord($sid[1]);  | 
                                                        |
| 1749 | -  | 
                                                        |
| 1750 | - $subIdStart = 8; // 1 + 1 + 6  | 
                                                        |
| 1751 | - $subIdLength = 4;  | 
                                                        |
| 1752 | -		if (strlen($sid) !== $subIdStart + $subIdLength * $numberSubID) { | 
                                                        |
| 1753 | - // Incorrect number of bytes present.  | 
                                                        |
| 1754 | - return '';  | 
                                                        |
| 1755 | - }  | 
                                                        |
| 1756 | -  | 
                                                        |
| 1757 | - // 6 bytes = 48 bits can be represented using floats without loss of  | 
                                                        |
| 1758 | - // precision (see https://gist.github.com/bantu/886ac680b0aef5812f71)  | 
                                                        |
| 1759 | - $iav = number_format(hexdec(bin2hex(substr($sid, 2, 6))), 0, '', '');  | 
                                                        |
| 1760 | -  | 
                                                        |
| 1761 | - $subIDs = array();  | 
                                                        |
| 1762 | -		for ($i = 0; $i < $numberSubID; $i++) { | 
                                                        |
| 1763 | -			$subID = unpack('V', substr($sid, $subIdStart + $subIdLength * $i, $subIdLength)); | 
                                                        |
| 1764 | -			$subIDs[] = sprintf('%u', $subID[1]); | 
                                                        |
| 1765 | - }  | 
                                                        |
| 1766 | -  | 
                                                        |
| 1767 | - // Result for example above: S-1-5-21-249921958-728525901-1594176202  | 
                                                        |
| 1768 | -		return sprintf('S-%d-%s-%s', $revision, $iav, implode('-', $subIDs)); | 
                                                        |
| 1769 | - }  | 
                                                        |
| 1770 | -  | 
                                                        |
| 1771 | - /**  | 
                                                        |
| 1772 | - * checks if the given DN is part of the given base DN(s)  | 
                                                        |
| 1773 | - * @param string $dn the DN  | 
                                                        |
| 1774 | - * @param string[] $bases array containing the allowed base DN or DNs  | 
                                                        |
| 1775 | - * @return bool  | 
                                                        |
| 1776 | - */  | 
                                                        |
| 1777 | -	public function isDNPartOfBase($dn, $bases) { | 
                                                        |
| 1778 | - $belongsToBase = false;  | 
                                                        |
| 1779 | - $bases = $this->helper->sanitizeDN($bases);  | 
                                                        |
| 1780 | -  | 
                                                        |
| 1781 | -		foreach($bases as $base) { | 
                                                        |
| 1782 | - $belongsToBase = true;  | 
                                                        |
| 1783 | -			if(mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8')-mb_strlen($base, 'UTF-8'))) { | 
                                                        |
| 1784 | - $belongsToBase = false;  | 
                                                        |
| 1785 | - }  | 
                                                        |
| 1786 | -			if($belongsToBase) { | 
                                                        |
| 1787 | - break;  | 
                                                        |
| 1788 | - }  | 
                                                        |
| 1789 | - }  | 
                                                        |
| 1790 | - return $belongsToBase;  | 
                                                        |
| 1791 | - }  | 
                                                        |
| 1792 | -  | 
                                                        |
| 1793 | - /**  | 
                                                        |
| 1794 | - * resets a running Paged Search operation  | 
                                                        |
| 1795 | - */  | 
                                                        |
| 1796 | -	private function abandonPagedSearch() { | 
                                                        |
| 1797 | -		if($this->connection->hasPagedResultSupport) { | 
                                                        |
| 1798 | - $cr = $this->connection->getConnectionResource();  | 
                                                        |
| 1799 | -			$this->invokeLDAPMethod('controlPagedResult', $cr, 0, false, $this->lastCookie); | 
                                                        |
| 1800 | - $this->getPagedSearchResultState();  | 
                                                        |
| 1801 | - $this->lastCookie = '';  | 
                                                        |
| 1802 | - $this->cookies = array();  | 
                                                        |
| 1803 | - }  | 
                                                        |
| 1804 | - }  | 
                                                        |
| 1805 | -  | 
                                                        |
| 1806 | - /**  | 
                                                        |
| 1807 | - * get a cookie for the next LDAP paged search  | 
                                                        |
| 1808 | - * @param string $base a string with the base DN for the search  | 
                                                        |
| 1809 | - * @param string $filter the search filter to identify the correct search  | 
                                                        |
| 1810 | - * @param int $limit the limit (or 'pageSize'), to identify the correct search well  | 
                                                        |
| 1811 | - * @param int $offset the offset for the new search to identify the correct search really good  | 
                                                        |
| 1812 | - * @return string containing the key or empty if none is cached  | 
                                                        |
| 1813 | - */  | 
                                                        |
| 1814 | -	private function getPagedResultCookie($base, $filter, $limit, $offset) { | 
                                                        |
| 1815 | -		if($offset === 0) { | 
                                                        |
| 1816 | - return '';  | 
                                                        |
| 1817 | - }  | 
                                                        |
| 1818 | - $offset -= $limit;  | 
                                                        |
| 1819 | - //we work with cache here  | 
                                                        |
| 1820 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . intval($limit) . '-' . intval($offset);  | 
                                                        |
| 1821 | - $cookie = '';  | 
                                                        |
| 1822 | -		if(isset($this->cookies[$cacheKey])) { | 
                                                        |
| 1823 | - $cookie = $this->cookies[$cacheKey];  | 
                                                        |
| 1824 | -			if(is_null($cookie)) { | 
                                                        |
| 1825 | - $cookie = '';  | 
                                                        |
| 1826 | - }  | 
                                                        |
| 1827 | - }  | 
                                                        |
| 1828 | - return $cookie;  | 
                                                        |
| 1829 | - }  | 
                                                        |
| 1830 | -  | 
                                                        |
| 1831 | - /**  | 
                                                        |
| 1832 | - * checks whether an LDAP paged search operation has more pages that can be  | 
                                                        |
| 1833 | - * retrieved, typically when offset and limit are provided.  | 
                                                        |
| 1834 | - *  | 
                                                        |
| 1835 | - * Be very careful to use it: the last cookie value, which is inspected, can  | 
                                                        |
| 1836 | - * be reset by other operations. Best, call it immediately after a search(),  | 
                                                        |
| 1837 | - * searchUsers() or searchGroups() call. count-methods are probably safe as  | 
                                                        |
| 1838 | - * well. Don't rely on it with any fetchList-method.  | 
                                                        |
| 1839 | - * @return bool  | 
                                                        |
| 1840 | - */  | 
                                                        |
| 1841 | -	public function hasMoreResults() { | 
                                                        |
| 1842 | -		if(!$this->connection->hasPagedResultSupport) { | 
                                                        |
| 1843 | - return false;  | 
                                                        |
| 1844 | - }  | 
                                                        |
| 1845 | -  | 
                                                        |
| 1846 | -		if(empty($this->lastCookie) && $this->lastCookie !== '0') { | 
                                                        |
| 1847 | - // as in RFC 2696, when all results are returned, the cookie will  | 
                                                        |
| 1848 | - // be empty.  | 
                                                        |
| 1849 | - return false;  | 
                                                        |
| 1850 | - }  | 
                                                        |
| 1851 | -  | 
                                                        |
| 1852 | - return true;  | 
                                                        |
| 1853 | - }  | 
                                                        |
| 1854 | -  | 
                                                        |
| 1855 | - /**  | 
                                                        |
| 1856 | - * set a cookie for LDAP paged search run  | 
                                                        |
| 1857 | - * @param string $base a string with the base DN for the search  | 
                                                        |
| 1858 | - * @param string $filter the search filter to identify the correct search  | 
                                                        |
| 1859 | - * @param int $limit the limit (or 'pageSize'), to identify the correct search well  | 
                                                        |
| 1860 | - * @param int $offset the offset for the run search to identify the correct search really good  | 
                                                        |
| 1861 | - * @param string $cookie string containing the cookie returned by ldap_control_paged_result_response  | 
                                                        |
| 1862 | - * @return void  | 
                                                        |
| 1863 | - */  | 
                                                        |
| 1864 | -	private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) { | 
                                                        |
| 1865 | - // allow '0' for 389ds  | 
                                                        |
| 1866 | -		if(!empty($cookie) || $cookie === '0') { | 
                                                        |
| 1867 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' .intval($limit) . '-' . intval($offset);  | 
                                                        |
| 1868 | - $this->cookies[$cacheKey] = $cookie;  | 
                                                        |
| 1869 | - $this->lastCookie = $cookie;  | 
                                                        |
| 1870 | - }  | 
                                                        |
| 1871 | - }  | 
                                                        |
| 1872 | -  | 
                                                        |
| 1873 | - /**  | 
                                                        |
| 1874 | - * Check whether the most recent paged search was successful. It flushed the state var. Use it always after a possible paged search.  | 
                                                        |
| 1875 | - * @return boolean|null true on success, null or false otherwise  | 
                                                        |
| 1876 | - */  | 
                                                        |
| 1877 | -	public function getPagedSearchResultState() { | 
                                                        |
| 1878 | - $result = $this->pagedSearchedSuccessful;  | 
                                                        |
| 1879 | - $this->pagedSearchedSuccessful = null;  | 
                                                        |
| 1880 | - return $result;  | 
                                                        |
| 1881 | - }  | 
                                                        |
| 1882 | -  | 
                                                        |
| 1883 | - /**  | 
                                                        |
| 1884 | - * Prepares a paged search, if possible  | 
                                                        |
| 1885 | - * @param string $filter the LDAP filter for the search  | 
                                                        |
| 1886 | - * @param string[] $bases an array containing the LDAP subtree(s) that shall be searched  | 
                                                        |
| 1887 | - * @param string[] $attr optional, when a certain attribute shall be filtered outside  | 
                                                        |
| 1888 | - * @param int $limit  | 
                                                        |
| 1889 | - * @param int $offset  | 
                                                        |
| 1890 | - * @return bool|true  | 
                                                        |
| 1891 | - */  | 
                                                        |
| 1892 | -	private function initPagedSearch($filter, $bases, $attr, $limit, $offset) { | 
                                                        |
| 1893 | - $pagedSearchOK = false;  | 
                                                        |
| 1894 | -		if($this->connection->hasPagedResultSupport && ($limit !== 0)) { | 
                                                        |
| 1895 | - $offset = intval($offset); //can be null  | 
                                                        |
| 1896 | -			\OCP\Util::writeLog('user_ldap', | 
                                                        |
| 1897 | - 'initializing paged search for Filter '.$filter.' base '.print_r($bases, true)  | 
                                                        |
| 1898 | - .' attr '.print_r($attr, true). ' limit ' .$limit.' offset '.$offset,  | 
                                                        |
| 1899 | - \OCP\Util::DEBUG);  | 
                                                        |
| 1900 | - //get the cookie from the search for the previous search, required by LDAP  | 
                                                        |
| 1901 | -			foreach($bases as $base) { | 
                                                        |
| 1902 | -  | 
                                                        |
| 1903 | - $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset);  | 
                                                        |
| 1904 | -				if(empty($cookie) && $cookie !== "0" && ($offset > 0)) { | 
                                                        |
| 1905 | - // no cookie known from a potential previous search. We need  | 
                                                        |
| 1906 | - // to start from 0 to come to the desired page. cookie value  | 
                                                        |
| 1907 | - // of '0' is valid, because 389ds  | 
                                                        |
| 1908 | - $reOffset = 0;  | 
                                                        |
| 1909 | -					while($reOffset < $offset) { | 
                                                        |
| 1910 | - $this->search($filter, array($base), $attr, $limit, $reOffset, true);  | 
                                                        |
| 1911 | - $reOffset += $limit;  | 
                                                        |
| 1912 | - }  | 
                                                        |
| 1913 | - $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset);  | 
                                                        |
| 1914 | - //still no cookie? obviously, the server does not like us. Let's skip paging efforts.  | 
                                                        |
| 1915 | - // '0' is valid, because 389ds  | 
                                                        |
| 1916 | - //TODO: remember this, probably does not change in the next request...  | 
                                                        |
| 1917 | -					if(empty($cookie) && $cookie !== '0') { | 
                                                        |
| 1918 | - $cookie = null;  | 
                                                        |
| 1919 | - }  | 
                                                        |
| 1920 | - }  | 
                                                        |
| 1921 | -				if(!is_null($cookie)) { | 
                                                        |
| 1922 | - //since offset = 0, this is a new search. We abandon other searches that might be ongoing.  | 
                                                        |
| 1923 | - $this->abandonPagedSearch();  | 
                                                        |
| 1924 | -					$pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', | 
                                                        |
| 1925 | - $this->connection->getConnectionResource(), $limit,  | 
                                                        |
| 1926 | - false, $cookie);  | 
                                                        |
| 1927 | -					if(!$pagedSearchOK) { | 
                                                        |
| 1928 | - return false;  | 
                                                        |
| 1929 | - }  | 
                                                        |
| 1930 | -					\OCP\Util::writeLog('user_ldap', 'Ready for a paged search', \OCP\Util::DEBUG); | 
                                                        |
| 1931 | -				} else { | 
                                                        |
| 1932 | -					\OCP\Util::writeLog('user_ldap', | 
                                                        |
| 1933 | - 'No paged search for us, Cpt., Limit '.$limit.' Offset '.$offset,  | 
                                                        |
| 1934 | - \OCP\Util::INFO);  | 
                                                        |
| 1935 | - }  | 
                                                        |
| 1936 | -  | 
                                                        |
| 1937 | - }  | 
                                                        |
| 1938 | - /* ++ Fixing RHDS searches with pages with zero results ++  | 
                                                        |
| 1690 | + \OC::$server->getLogger()->info(  | 
                                                        |
| 1691 | + 'Passed string does not resemble a valid GUID. Known UUID ' .  | 
                                                        |
| 1692 | +                '({uuid}) probably does not match UUID configuration.', | 
                                                        |
| 1693 | + [ 'app' => 'user_ldap', 'uuid' => $guid ]  | 
                                                        |
| 1694 | + );  | 
                                                        |
| 1695 | + return $guid;  | 
                                                        |
| 1696 | + }  | 
                                                        |
| 1697 | +        for($i=0; $i < 3; $i++) { | 
                                                        |
| 1698 | + $pairs = str_split($blocks[$i], 2);  | 
                                                        |
| 1699 | + $pairs = array_reverse($pairs);  | 
                                                        |
| 1700 | +            $blocks[$i] = implode('', $pairs); | 
                                                        |
| 1701 | + }  | 
                                                        |
| 1702 | +        for($i=0; $i < 5; $i++) { | 
                                                        |
| 1703 | + $pairs = str_split($blocks[$i], 2);  | 
                                                        |
| 1704 | +            $blocks[$i] = '\\' . implode('\\', $pairs); | 
                                                        |
| 1705 | + }  | 
                                                        |
| 1706 | +        return implode('', $blocks); | 
                                                        |
| 1707 | + }  | 
                                                        |
| 1708 | +  | 
                                                        |
| 1709 | + /**  | 
                                                        |
| 1710 | + * gets a SID of the domain of the given dn  | 
                                                        |
| 1711 | + * @param string $dn  | 
                                                        |
| 1712 | + * @return string|bool  | 
                                                        |
| 1713 | + */  | 
                                                        |
| 1714 | +    public function getSID($dn) { | 
                                                        |
| 1715 | + $domainDN = $this->getDomainDNFromDN($dn);  | 
                                                        |
| 1716 | + $cacheKey = 'getSID-'.$domainDN;  | 
                                                        |
| 1717 | + $sid = $this->connection->getFromCache($cacheKey);  | 
                                                        |
| 1718 | +        if(!is_null($sid)) { | 
                                                        |
| 1719 | + return $sid;  | 
                                                        |
| 1720 | + }  | 
                                                        |
| 1721 | +  | 
                                                        |
| 1722 | + $objectSid = $this->readAttribute($domainDN, 'objectsid');  | 
                                                        |
| 1723 | +        if(!is_array($objectSid) || empty($objectSid)) { | 
                                                        |
| 1724 | + $this->connection->writeToCache($cacheKey, false);  | 
                                                        |
| 1725 | + return false;  | 
                                                        |
| 1726 | + }  | 
                                                        |
| 1727 | + $domainObjectSid = $this->convertSID2Str($objectSid[0]);  | 
                                                        |
| 1728 | + $this->connection->writeToCache($cacheKey, $domainObjectSid);  | 
                                                        |
| 1729 | +  | 
                                                        |
| 1730 | + return $domainObjectSid;  | 
                                                        |
| 1731 | + }  | 
                                                        |
| 1732 | +  | 
                                                        |
| 1733 | + /**  | 
                                                        |
| 1734 | + * converts a binary SID into a string representation  | 
                                                        |
| 1735 | + * @param string $sid  | 
                                                        |
| 1736 | + * @return string  | 
                                                        |
| 1737 | + */  | 
                                                        |
| 1738 | +    public function convertSID2Str($sid) { | 
                                                        |
| 1739 | + // The format of a SID binary string is as follows:  | 
                                                        |
| 1740 | + // 1 byte for the revision level  | 
                                                        |
| 1741 | + // 1 byte for the number n of variable sub-ids  | 
                                                        |
| 1742 | + // 6 bytes for identifier authority value  | 
                                                        |
| 1743 | + // n*4 bytes for n sub-ids  | 
                                                        |
| 1744 | + //  | 
                                                        |
| 1745 | + // Example: 010400000000000515000000a681e50e4d6c6c2bca32055f  | 
                                                        |
| 1746 | + // Legend: RRNNAAAAAAAAAAAA11111111222222223333333344444444  | 
                                                        |
| 1747 | + $revision = ord($sid[0]);  | 
                                                        |
| 1748 | + $numberSubID = ord($sid[1]);  | 
                                                        |
| 1749 | +  | 
                                                        |
| 1750 | + $subIdStart = 8; // 1 + 1 + 6  | 
                                                        |
| 1751 | + $subIdLength = 4;  | 
                                                        |
| 1752 | +        if (strlen($sid) !== $subIdStart + $subIdLength * $numberSubID) { | 
                                                        |
| 1753 | + // Incorrect number of bytes present.  | 
                                                        |
| 1754 | + return '';  | 
                                                        |
| 1755 | + }  | 
                                                        |
| 1756 | +  | 
                                                        |
| 1757 | + // 6 bytes = 48 bits can be represented using floats without loss of  | 
                                                        |
| 1758 | + // precision (see https://gist.github.com/bantu/886ac680b0aef5812f71)  | 
                                                        |
| 1759 | + $iav = number_format(hexdec(bin2hex(substr($sid, 2, 6))), 0, '', '');  | 
                                                        |
| 1760 | +  | 
                                                        |
| 1761 | + $subIDs = array();  | 
                                                        |
| 1762 | +        for ($i = 0; $i < $numberSubID; $i++) { | 
                                                        |
| 1763 | +            $subID = unpack('V', substr($sid, $subIdStart + $subIdLength * $i, $subIdLength)); | 
                                                        |
| 1764 | +            $subIDs[] = sprintf('%u', $subID[1]); | 
                                                        |
| 1765 | + }  | 
                                                        |
| 1766 | +  | 
                                                        |
| 1767 | + // Result for example above: S-1-5-21-249921958-728525901-1594176202  | 
                                                        |
| 1768 | +        return sprintf('S-%d-%s-%s', $revision, $iav, implode('-', $subIDs)); | 
                                                        |
| 1769 | + }  | 
                                                        |
| 1770 | +  | 
                                                        |
| 1771 | + /**  | 
                                                        |
| 1772 | + * checks if the given DN is part of the given base DN(s)  | 
                                                        |
| 1773 | + * @param string $dn the DN  | 
                                                        |
| 1774 | + * @param string[] $bases array containing the allowed base DN or DNs  | 
                                                        |
| 1775 | + * @return bool  | 
                                                        |
| 1776 | + */  | 
                                                        |
| 1777 | +    public function isDNPartOfBase($dn, $bases) { | 
                                                        |
| 1778 | + $belongsToBase = false;  | 
                                                        |
| 1779 | + $bases = $this->helper->sanitizeDN($bases);  | 
                                                        |
| 1780 | +  | 
                                                        |
| 1781 | +        foreach($bases as $base) { | 
                                                        |
| 1782 | + $belongsToBase = true;  | 
                                                        |
| 1783 | +            if(mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8')-mb_strlen($base, 'UTF-8'))) { | 
                                                        |
| 1784 | + $belongsToBase = false;  | 
                                                        |
| 1785 | + }  | 
                                                        |
| 1786 | +            if($belongsToBase) { | 
                                                        |
| 1787 | + break;  | 
                                                        |
| 1788 | + }  | 
                                                        |
| 1789 | + }  | 
                                                        |
| 1790 | + return $belongsToBase;  | 
                                                        |
| 1791 | + }  | 
                                                        |
| 1792 | +  | 
                                                        |
| 1793 | + /**  | 
                                                        |
| 1794 | + * resets a running Paged Search operation  | 
                                                        |
| 1795 | + */  | 
                                                        |
| 1796 | +    private function abandonPagedSearch() { | 
                                                        |
| 1797 | +        if($this->connection->hasPagedResultSupport) { | 
                                                        |
| 1798 | + $cr = $this->connection->getConnectionResource();  | 
                                                        |
| 1799 | +            $this->invokeLDAPMethod('controlPagedResult', $cr, 0, false, $this->lastCookie); | 
                                                        |
| 1800 | + $this->getPagedSearchResultState();  | 
                                                        |
| 1801 | + $this->lastCookie = '';  | 
                                                        |
| 1802 | + $this->cookies = array();  | 
                                                        |
| 1803 | + }  | 
                                                        |
| 1804 | + }  | 
                                                        |
| 1805 | +  | 
                                                        |
| 1806 | + /**  | 
                                                        |
| 1807 | + * get a cookie for the next LDAP paged search  | 
                                                        |
| 1808 | + * @param string $base a string with the base DN for the search  | 
                                                        |
| 1809 | + * @param string $filter the search filter to identify the correct search  | 
                                                        |
| 1810 | + * @param int $limit the limit (or 'pageSize'), to identify the correct search well  | 
                                                        |
| 1811 | + * @param int $offset the offset for the new search to identify the correct search really good  | 
                                                        |
| 1812 | + * @return string containing the key or empty if none is cached  | 
                                                        |
| 1813 | + */  | 
                                                        |
| 1814 | +    private function getPagedResultCookie($base, $filter, $limit, $offset) { | 
                                                        |
| 1815 | +        if($offset === 0) { | 
                                                        |
| 1816 | + return '';  | 
                                                        |
| 1817 | + }  | 
                                                        |
| 1818 | + $offset -= $limit;  | 
                                                        |
| 1819 | + //we work with cache here  | 
                                                        |
| 1820 | + $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . intval($limit) . '-' . intval($offset);  | 
                                                        |
| 1821 | + $cookie = '';  | 
                                                        |
| 1822 | +        if(isset($this->cookies[$cacheKey])) { | 
                                                        |
| 1823 | + $cookie = $this->cookies[$cacheKey];  | 
                                                        |
| 1824 | +            if(is_null($cookie)) { | 
                                                        |
| 1825 | + $cookie = '';  | 
                                                        |
| 1826 | + }  | 
                                                        |
| 1827 | + }  | 
                                                        |
| 1828 | + return $cookie;  | 
                                                        |
| 1829 | + }  | 
                                                        |
| 1830 | +  | 
                                                        |
| 1831 | + /**  | 
                                                        |
| 1832 | + * checks whether an LDAP paged search operation has more pages that can be  | 
                                                        |
| 1833 | + * retrieved, typically when offset and limit are provided.  | 
                                                        |
| 1834 | + *  | 
                                                        |
| 1835 | + * Be very careful to use it: the last cookie value, which is inspected, can  | 
                                                        |
| 1836 | + * be reset by other operations. Best, call it immediately after a search(),  | 
                                                        |
| 1837 | + * searchUsers() or searchGroups() call. count-methods are probably safe as  | 
                                                        |
| 1838 | + * well. Don't rely on it with any fetchList-method.  | 
                                                        |
| 1839 | + * @return bool  | 
                                                        |
| 1840 | + */  | 
                                                        |
| 1841 | +    public function hasMoreResults() { | 
                                                        |
| 1842 | +        if(!$this->connection->hasPagedResultSupport) { | 
                                                        |
| 1843 | + return false;  | 
                                                        |
| 1844 | + }  | 
                                                        |
| 1845 | +  | 
                                                        |
| 1846 | +        if(empty($this->lastCookie) && $this->lastCookie !== '0') { | 
                                                        |
| 1847 | + // as in RFC 2696, when all results are returned, the cookie will  | 
                                                        |
| 1848 | + // be empty.  | 
                                                        |
| 1849 | + return false;  | 
                                                        |
| 1850 | + }  | 
                                                        |
| 1851 | +  | 
                                                        |
| 1852 | + return true;  | 
                                                        |
| 1853 | + }  | 
                                                        |
| 1854 | +  | 
                                                        |
| 1855 | + /**  | 
                                                        |
| 1856 | + * set a cookie for LDAP paged search run  | 
                                                        |
| 1857 | + * @param string $base a string with the base DN for the search  | 
                                                        |
| 1858 | + * @param string $filter the search filter to identify the correct search  | 
                                                        |
| 1859 | + * @param int $limit the limit (or 'pageSize'), to identify the correct search well  | 
                                                        |
| 1860 | + * @param int $offset the offset for the run search to identify the correct search really good  | 
                                                        |
| 1861 | + * @param string $cookie string containing the cookie returned by ldap_control_paged_result_response  | 
                                                        |
| 1862 | + * @return void  | 
                                                        |
| 1863 | + */  | 
                                                        |
| 1864 | +    private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) { | 
                                                        |
| 1865 | + // allow '0' for 389ds  | 
                                                        |
| 1866 | +        if(!empty($cookie) || $cookie === '0') { | 
                                                        |
| 1867 | + $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' .intval($limit) . '-' . intval($offset);  | 
                                                        |
| 1868 | + $this->cookies[$cacheKey] = $cookie;  | 
                                                        |
| 1869 | + $this->lastCookie = $cookie;  | 
                                                        |
| 1870 | + }  | 
                                                        |
| 1871 | + }  | 
                                                        |
| 1872 | +  | 
                                                        |
| 1873 | + /**  | 
                                                        |
| 1874 | + * Check whether the most recent paged search was successful. It flushed the state var. Use it always after a possible paged search.  | 
                                                        |
| 1875 | + * @return boolean|null true on success, null or false otherwise  | 
                                                        |
| 1876 | + */  | 
                                                        |
| 1877 | +    public function getPagedSearchResultState() { | 
                                                        |
| 1878 | + $result = $this->pagedSearchedSuccessful;  | 
                                                        |
| 1879 | + $this->pagedSearchedSuccessful = null;  | 
                                                        |
| 1880 | + return $result;  | 
                                                        |
| 1881 | + }  | 
                                                        |
| 1882 | +  | 
                                                        |
| 1883 | + /**  | 
                                                        |
| 1884 | + * Prepares a paged search, if possible  | 
                                                        |
| 1885 | + * @param string $filter the LDAP filter for the search  | 
                                                        |
| 1886 | + * @param string[] $bases an array containing the LDAP subtree(s) that shall be searched  | 
                                                        |
| 1887 | + * @param string[] $attr optional, when a certain attribute shall be filtered outside  | 
                                                        |
| 1888 | + * @param int $limit  | 
                                                        |
| 1889 | + * @param int $offset  | 
                                                        |
| 1890 | + * @return bool|true  | 
                                                        |
| 1891 | + */  | 
                                                        |
| 1892 | +    private function initPagedSearch($filter, $bases, $attr, $limit, $offset) { | 
                                                        |
| 1893 | + $pagedSearchOK = false;  | 
                                                        |
| 1894 | +        if($this->connection->hasPagedResultSupport && ($limit !== 0)) { | 
                                                        |
| 1895 | + $offset = intval($offset); //can be null  | 
                                                        |
| 1896 | +            \OCP\Util::writeLog('user_ldap', | 
                                                        |
| 1897 | + 'initializing paged search for Filter '.$filter.' base '.print_r($bases, true)  | 
                                                        |
| 1898 | + .' attr '.print_r($attr, true). ' limit ' .$limit.' offset '.$offset,  | 
                                                        |
| 1899 | + \OCP\Util::DEBUG);  | 
                                                        |
| 1900 | + //get the cookie from the search for the previous search, required by LDAP  | 
                                                        |
| 1901 | +            foreach($bases as $base) { | 
                                                        |
| 1902 | +  | 
                                                        |
| 1903 | + $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset);  | 
                                                        |
| 1904 | +                if(empty($cookie) && $cookie !== "0" && ($offset > 0)) { | 
                                                        |
| 1905 | + // no cookie known from a potential previous search. We need  | 
                                                        |
| 1906 | + // to start from 0 to come to the desired page. cookie value  | 
                                                        |
| 1907 | + // of '0' is valid, because 389ds  | 
                                                        |
| 1908 | + $reOffset = 0;  | 
                                                        |
| 1909 | +                    while($reOffset < $offset) { | 
                                                        |
| 1910 | + $this->search($filter, array($base), $attr, $limit, $reOffset, true);  | 
                                                        |
| 1911 | + $reOffset += $limit;  | 
                                                        |
| 1912 | + }  | 
                                                        |
| 1913 | + $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset);  | 
                                                        |
| 1914 | + //still no cookie? obviously, the server does not like us. Let's skip paging efforts.  | 
                                                        |
| 1915 | + // '0' is valid, because 389ds  | 
                                                        |
| 1916 | + //TODO: remember this, probably does not change in the next request...  | 
                                                        |
| 1917 | +                    if(empty($cookie) && $cookie !== '0') { | 
                                                        |
| 1918 | + $cookie = null;  | 
                                                        |
| 1919 | + }  | 
                                                        |
| 1920 | + }  | 
                                                        |
| 1921 | +                if(!is_null($cookie)) { | 
                                                        |
| 1922 | + //since offset = 0, this is a new search. We abandon other searches that might be ongoing.  | 
                                                        |
| 1923 | + $this->abandonPagedSearch();  | 
                                                        |
| 1924 | +                    $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', | 
                                                        |
| 1925 | + $this->connection->getConnectionResource(), $limit,  | 
                                                        |
| 1926 | + false, $cookie);  | 
                                                        |
| 1927 | +                    if(!$pagedSearchOK) { | 
                                                        |
| 1928 | + return false;  | 
                                                        |
| 1929 | + }  | 
                                                        |
| 1930 | +                    \OCP\Util::writeLog('user_ldap', 'Ready for a paged search', \OCP\Util::DEBUG); | 
                                                        |
| 1931 | +                } else { | 
                                                        |
| 1932 | +                    \OCP\Util::writeLog('user_ldap', | 
                                                        |
| 1933 | + 'No paged search for us, Cpt., Limit '.$limit.' Offset '.$offset,  | 
                                                        |
| 1934 | + \OCP\Util::INFO);  | 
                                                        |
| 1935 | + }  | 
                                                        |
| 1936 | +  | 
                                                        |
| 1937 | + }  | 
                                                        |
| 1938 | + /* ++ Fixing RHDS searches with pages with zero results ++  | 
                                                        |
| 1939 | 1939 | * We coudn't get paged searches working with our RHDS for login ($limit = 0),  | 
                                                        
| 1940 | 1940 | * due to pages with zero results.  | 
                                                        
| 1941 | 1941 | * So we added "&& !empty($this->lastCookie)" to this test to ignore pagination  | 
                                                        
| 1942 | 1942 | * if we don't have a previous paged search.  | 
                                                        
| 1943 | 1943 | */  | 
                                                        
| 1944 | -		} else if($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { | 
                                                        |
| 1945 | - // a search without limit was requested. However, if we do use  | 
                                                        |
| 1946 | - // Paged Search once, we always must do it. This requires us to  | 
                                                        |
| 1947 | - // initialize it with the configured page size.  | 
                                                        |
| 1948 | - $this->abandonPagedSearch();  | 
                                                        |
| 1949 | - // in case someone set it to 0 … use 500, otherwise no results will  | 
                                                        |
| 1950 | - // be returned.  | 
                                                        |
| 1951 | - $pageSize = intval($this->connection->ldapPagingSize) > 0 ? intval($this->connection->ldapPagingSize) : 500;  | 
                                                        |
| 1952 | -			$pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', | 
                                                        |
| 1953 | - $this->connection->getConnectionResource(),  | 
                                                        |
| 1954 | - $pageSize, false, '');  | 
                                                        |
| 1955 | - }  | 
                                                        |
| 1956 | -  | 
                                                        |
| 1957 | - return $pagedSearchOK;  | 
                                                        |
| 1958 | - }  | 
                                                        |
| 1944 | +        } else if($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { | 
                                                        |
| 1945 | + // a search without limit was requested. However, if we do use  | 
                                                        |
| 1946 | + // Paged Search once, we always must do it. This requires us to  | 
                                                        |
| 1947 | + // initialize it with the configured page size.  | 
                                                        |
| 1948 | + $this->abandonPagedSearch();  | 
                                                        |
| 1949 | + // in case someone set it to 0 … use 500, otherwise no results will  | 
                                                        |
| 1950 | + // be returned.  | 
                                                        |
| 1951 | + $pageSize = intval($this->connection->ldapPagingSize) > 0 ? intval($this->connection->ldapPagingSize) : 500;  | 
                                                        |
| 1952 | +            $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', | 
                                                        |
| 1953 | + $this->connection->getConnectionResource(),  | 
                                                        |
| 1954 | + $pageSize, false, '');  | 
                                                        |
| 1955 | + }  | 
                                                        |
| 1956 | +  | 
                                                        |
| 1957 | + return $pagedSearchOK;  | 
                                                        |
| 1958 | + }  | 
                                                        |
| 1959 | 1959 | |
| 1960 | 1960 | }  | 
                                                        
@@ -124,7 +124,7 @@ discard block  | 
                                                    ||
| 124 | 124 | * @return AbstractMapping  | 
                                                        
| 125 | 125 | */  | 
                                                        
| 126 | 126 |  	public function getUserMapper() { | 
                                                        
| 127 | -		if(is_null($this->userMapper)) { | 
                                                        |
| 127 | +		if (is_null($this->userMapper)) { | 
                                                        |
| 128 | 128 |  			throw new \Exception('UserMapper was not assigned to this Access instance.'); | 
                                                        
| 129 | 129 | }  | 
                                                        
| 130 | 130 | return $this->userMapper;  | 
                                                        
@@ -144,7 +144,7 @@ discard block  | 
                                                    ||
| 144 | 144 | * @return AbstractMapping  | 
                                                        
| 145 | 145 | */  | 
                                                        
| 146 | 146 |  	public function getGroupMapper() { | 
                                                        
| 147 | -		if(is_null($this->groupMapper)) { | 
                                                        |
| 147 | +		if (is_null($this->groupMapper)) { | 
                                                        |
| 148 | 148 |  			throw new \Exception('GroupMapper was not assigned to this Access instance.'); | 
                                                        
| 149 | 149 | }  | 
                                                        
| 150 | 150 | return $this->groupMapper;  | 
                                                        
@@ -175,14 +175,14 @@ discard block  | 
                                                    ||
| 175 | 175 | * array if $attr is empty, false otherwise  | 
                                                        
| 176 | 176 | */  | 
                                                        
| 177 | 177 |  	public function readAttribute($dn, $attr, $filter = 'objectClass=*') { | 
                                                        
| 178 | -		if(!$this->checkConnection()) { | 
                                                        |
| 178 | +		if (!$this->checkConnection()) { | 
                                                        |
| 179 | 179 |  			\OCP\Util::writeLog('user_ldap', | 
                                                        
| 180 | 180 | 'No LDAP Connector assigned, access impossible for readAttribute.',  | 
                                                        
| 181 | 181 | \OCP\Util::WARN);  | 
                                                        
| 182 | 182 | return false;  | 
                                                        
| 183 | 183 | }  | 
                                                        
| 184 | 184 | $cr = $this->connection->getConnectionResource();  | 
                                                        
| 185 | -		if(!$this->ldap->isResource($cr)) { | 
                                                        |
| 185 | +		if (!$this->ldap->isResource($cr)) { | 
                                                        |
| 186 | 186 | //LDAP not available  | 
                                                        
| 187 | 187 |  			\OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); | 
                                                        
| 188 | 188 | return false;  | 
                                                        
@@ -205,7 +205,7 @@ discard block  | 
                                                    ||
| 205 | 205 | $isRangeRequest = false;  | 
                                                        
| 206 | 206 |  		do { | 
                                                        
| 207 | 207 | $result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults);  | 
                                                        
| 208 | -			if(is_bool($result)) { | 
                                                        |
| 208 | +			if (is_bool($result)) { | 
                                                        |
| 209 | 209 | // when an exists request was run and it was successful, an empty  | 
                                                        
| 210 | 210 | // array must be returned  | 
                                                        
| 211 | 211 | return $result ? [] : false;  | 
                                                        
@@ -222,22 +222,22 @@ discard block  | 
                                                    ||
| 222 | 222 | $result = $this->extractRangeData($result, $attr);  | 
                                                        
| 223 | 223 |  			if (!empty($result)) { | 
                                                        
| 224 | 224 | $normalizedResult = $this->extractAttributeValuesFromResult(  | 
                                                        
| 225 | - [ $attr => $result['values'] ],  | 
                                                        |
| 225 | + [$attr => $result['values']],  | 
                                                        |
| 226 | 226 | $attr  | 
                                                        
| 227 | 227 | );  | 
                                                        
| 228 | 228 | $values = array_merge($values, $normalizedResult);  | 
                                                        
| 229 | 229 | |
| 230 | -				if($result['rangeHigh'] === '*') { | 
                                                        |
| 230 | +				if ($result['rangeHigh'] === '*') { | 
                                                        |
| 231 | 231 | // when server replies with * as high range value, there are  | 
                                                        
| 232 | 232 | // no more results left  | 
                                                        
| 233 | 233 | return $values;  | 
                                                        
| 234 | 234 |  				} else { | 
                                                        
| 235 | - $low = $result['rangeHigh'] + 1;  | 
                                                        |
| 236 | - $attrToRead = $result['attributeName'] . ';range=' . $low . '-*';  | 
                                                        |
| 235 | + $low = $result['rangeHigh'] + 1;  | 
                                                        |
| 236 | + $attrToRead = $result['attributeName'].';range='.$low.'-*';  | 
                                                        |
| 237 | 237 | $isRangeRequest = true;  | 
                                                        
| 238 | 238 | }  | 
                                                        
| 239 | 239 | }  | 
                                                        
| 240 | - } while($isRangeRequest);  | 
                                                        |
| 240 | + } while ($isRangeRequest);  | 
                                                        |
| 241 | 241 | |
| 242 | 242 |  		\OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, \OCP\Util::DEBUG); | 
                                                        
| 243 | 243 | return false;  | 
                                                        
@@ -262,13 +262,13 @@ discard block  | 
                                                    ||
| 262 | 262 |  		if (!$this->ldap->isResource($rr)) { | 
                                                        
| 263 | 263 |  			if ($attribute !== '') { | 
                                                        
| 264 | 264 | //do not throw this message on userExists check, irritates  | 
                                                        
| 265 | -				\OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, \OCP\Util::DEBUG); | 
                                                        |
| 265 | +				\OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN '.$dn, \OCP\Util::DEBUG); | 
                                                        |
| 266 | 266 | }  | 
                                                        
| 267 | 267 | //in case an error occurs , e.g. object does not exist  | 
                                                        
| 268 | 268 | return false;  | 
                                                        
| 269 | 269 | }  | 
                                                        
| 270 | 270 |  		if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) { | 
                                                        
| 271 | -			\OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', \OCP\Util::DEBUG); | 
                                                        |
| 271 | +			\OCP\Util::writeLog('user_ldap', 'readAttribute: '.$dn.' found', \OCP\Util::DEBUG); | 
                                                        |
| 272 | 272 | return true;  | 
                                                        
| 273 | 273 | }  | 
                                                        
| 274 | 274 |  		$er = $this->invokeLDAPMethod('firstEntry', $cr, $rr); | 
                                                        
@@ -293,12 +293,12 @@ discard block  | 
                                                    ||
| 293 | 293 | */  | 
                                                        
| 294 | 294 |  	public function extractAttributeValuesFromResult($result, $attribute) { | 
                                                        
| 295 | 295 | $values = [];  | 
                                                        
| 296 | -		if(isset($result[$attribute]) && $result[$attribute]['count'] > 0) { | 
                                                        |
| 296 | +		if (isset($result[$attribute]) && $result[$attribute]['count'] > 0) { | 
                                                        |
| 297 | 297 | $lowercaseAttribute = strtolower($attribute);  | 
                                                        
| 298 | -			for($i=0;$i<$result[$attribute]['count'];$i++) { | 
                                                        |
| 299 | -				if($this->resemblesDN($attribute)) { | 
                                                        |
| 298 | +			for ($i = 0; $i < $result[$attribute]['count']; $i++) { | 
                                                        |
| 299 | +				if ($this->resemblesDN($attribute)) { | 
                                                        |
| 300 | 300 | $values[] = $this->helper->sanitizeDN($result[$attribute][$i]);  | 
                                                        
| 301 | -				} elseif($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { | 
                                                        |
| 301 | +				} elseif ($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { | 
                                                        |
| 302 | 302 | $values[] = $this->convertObjectGUID2Str($result[$attribute][$i]);  | 
                                                        
| 303 | 303 |  				} else { | 
                                                        
| 304 | 304 | $values[] = $result[$attribute][$i];  | 
                                                        
@@ -320,10 +320,10 @@ discard block  | 
                                                    ||
| 320 | 320 | */  | 
                                                        
| 321 | 321 |  	public function extractRangeData($result, $attribute) { | 
                                                        
| 322 | 322 | $keys = array_keys($result);  | 
                                                        
| 323 | -		foreach($keys as $key) { | 
                                                        |
| 324 | -			if($key !== $attribute && strpos($key, $attribute) === 0) { | 
                                                        |
| 323 | +		foreach ($keys as $key) { | 
                                                        |
| 324 | +			if ($key !== $attribute && strpos($key, $attribute) === 0) { | 
                                                        |
| 325 | 325 |  				$queryData = explode(';', $key); | 
                                                        
| 326 | -				if(strpos($queryData[1], 'range=') === 0) { | 
                                                        |
| 326 | +				if (strpos($queryData[1], 'range=') === 0) { | 
                                                        |
| 327 | 327 | $high = substr($queryData[1], 1 + strpos($queryData[1], '-'));  | 
                                                        
| 328 | 328 | $data = [  | 
                                                        
| 329 | 329 | 'values' => $result[$key],  | 
                                                        
@@ -348,18 +348,18 @@ discard block  | 
                                                    ||
| 348 | 348 | * @throws \Exception  | 
                                                        
| 349 | 349 | */  | 
                                                        
| 350 | 350 |  	public function setPassword($userDN, $password) { | 
                                                        
| 351 | -		if(intval($this->connection->turnOnPasswordChange) !== 1) { | 
                                                        |
| 351 | +		if (intval($this->connection->turnOnPasswordChange) !== 1) { | 
                                                        |
| 352 | 352 |  			throw new \Exception('LDAP password changes are disabled.'); | 
                                                        
| 353 | 353 | }  | 
                                                        
| 354 | 354 | $cr = $this->connection->getConnectionResource();  | 
                                                        
| 355 | -		if(!$this->ldap->isResource($cr)) { | 
                                                        |
| 355 | +		if (!$this->ldap->isResource($cr)) { | 
                                                        |
| 356 | 356 | //LDAP not available  | 
                                                        
| 357 | 357 |  			\OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); | 
                                                        
| 358 | 358 | return false;  | 
                                                        
| 359 | 359 | }  | 
                                                        
| 360 | 360 |  		try { | 
                                                        
| 361 | 361 |  			return @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password); | 
                                                        
| 362 | -		} catch(ConstraintViolationException $e) { | 
                                                        |
| 362 | +		} catch (ConstraintViolationException $e) { | 
                                                        |
| 363 | 363 |  			throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ').$e->getMessage(), $e->getCode()); | 
                                                        
| 364 | 364 | }  | 
                                                        
| 365 | 365 | }  | 
                                                        
@@ -401,17 +401,17 @@ discard block  | 
                                                    ||
| 401 | 401 | */  | 
                                                        
| 402 | 402 |  	public function getDomainDNFromDN($dn) { | 
                                                        
| 403 | 403 | $allParts = $this->ldap->explodeDN($dn, 0);  | 
                                                        
| 404 | -		if($allParts === false) { | 
                                                        |
| 404 | +		if ($allParts === false) { | 
                                                        |
| 405 | 405 | //not a valid DN  | 
                                                        
| 406 | 406 | return '';  | 
                                                        
| 407 | 407 | }  | 
                                                        
| 408 | 408 | $domainParts = array();  | 
                                                        
| 409 | 409 | $dcFound = false;  | 
                                                        
| 410 | -		foreach($allParts as $part) { | 
                                                        |
| 411 | -			if(!$dcFound && strpos($part, 'dc=') === 0) { | 
                                                        |
| 410 | +		foreach ($allParts as $part) { | 
                                                        |
| 411 | +			if (!$dcFound && strpos($part, 'dc=') === 0) { | 
                                                        |
| 412 | 412 | $dcFound = true;  | 
                                                        
| 413 | 413 | }  | 
                                                        
| 414 | -			if($dcFound) { | 
                                                        |
| 414 | +			if ($dcFound) { | 
                                                        |
| 415 | 415 | $domainParts[] = $part;  | 
                                                        
| 416 | 416 | }  | 
                                                        
| 417 | 417 | }  | 
                                                        
@@ -438,7 +438,7 @@ discard block  | 
                                                    ||
| 438 | 438 | |
| 439 | 439 | //Check whether the DN belongs to the Base, to avoid issues on multi-  | 
                                                        
| 440 | 440 | //server setups  | 
                                                        
| 441 | -		if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { | 
                                                        |
| 441 | +		if (is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { | 
                                                        |
| 442 | 442 | return $fdn;  | 
                                                        
| 443 | 443 | }  | 
                                                        
| 444 | 444 | |
@@ -455,7 +455,7 @@ discard block  | 
                                                    ||
| 455 | 455 | //To avoid bypassing the base DN settings under certain circumstances  | 
                                                        
| 456 | 456 | //with the group support, check whether the provided DN matches one of  | 
                                                        
| 457 | 457 | //the given Bases  | 
                                                        
| 458 | -		if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { | 
                                                        |
| 458 | +		if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { | 
                                                        |
| 459 | 459 | return false;  | 
                                                        
| 460 | 460 | }  | 
                                                        
| 461 | 461 | |
@@ -472,11 +472,11 @@ discard block  | 
                                                    ||
| 472 | 472 | */  | 
                                                        
| 473 | 473 |  	public function groupsMatchFilter($groupDNs) { | 
                                                        
| 474 | 474 | $validGroupDNs = [];  | 
                                                        
| 475 | -		foreach($groupDNs as $dn) { | 
                                                        |
| 475 | +		foreach ($groupDNs as $dn) { | 
                                                        |
| 476 | 476 | $cacheKey = 'groupsMatchFilter-'.$dn;  | 
                                                        
| 477 | 477 | $groupMatchFilter = $this->connection->getFromCache($cacheKey);  | 
                                                        
| 478 | -			if(!is_null($groupMatchFilter)) { | 
                                                        |
| 479 | -				if($groupMatchFilter) { | 
                                                        |
| 478 | +			if (!is_null($groupMatchFilter)) { | 
                                                        |
| 479 | +				if ($groupMatchFilter) { | 
                                                        |
| 480 | 480 | $validGroupDNs[] = $dn;  | 
                                                        
| 481 | 481 | }  | 
                                                        
| 482 | 482 | continue;  | 
                                                        
@@ -484,13 +484,13 @@ discard block  | 
                                                    ||
| 484 | 484 | |
| 485 | 485 | // Check the base DN first. If this is not met already, we don't  | 
                                                        
| 486 | 486 | // need to ask the server at all.  | 
                                                        
| 487 | -			if(!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { | 
                                                        |
| 487 | +			if (!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { | 
                                                        |
| 488 | 488 | $this->connection->writeToCache($cacheKey, false);  | 
                                                        
| 489 | 489 | continue;  | 
                                                        
| 490 | 490 | }  | 
                                                        
| 491 | 491 | |
| 492 | 492 | $result = $this->readAttribute($dn, 'cn', $this->connection->ldapGroupFilter);  | 
                                                        
| 493 | -			if(is_array($result)) { | 
                                                        |
| 493 | +			if (is_array($result)) { | 
                                                        |
| 494 | 494 | $this->connection->writeToCache($cacheKey, true);  | 
                                                        
| 495 | 495 | $validGroupDNs[] = $dn;  | 
                                                        
| 496 | 496 |  			} else { | 
                                                        
@@ -511,7 +511,7 @@ discard block  | 
                                                    ||
| 511 | 511 | //To avoid bypassing the base DN settings under certain circumstances  | 
                                                        
| 512 | 512 | //with the group support, check whether the provided DN matches one of  | 
                                                        
| 513 | 513 | //the given Bases  | 
                                                        
| 514 | -		if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { | 
                                                        |
| 514 | +		if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { | 
                                                        |
| 515 | 515 | return false;  | 
                                                        
| 516 | 516 | }  | 
                                                        
| 517 | 517 | |
@@ -530,7 +530,7 @@ discard block  | 
                                                    ||
| 530 | 530 | */  | 
                                                        
| 531 | 531 |  	public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, array $record = null) { | 
                                                        
| 532 | 532 | $newlyMapped = false;  | 
                                                        
| 533 | -		if($isUser) { | 
                                                        |
| 533 | +		if ($isUser) { | 
                                                        |
| 534 | 534 | $mapper = $this->getUserMapper();  | 
                                                        
| 535 | 535 | $nameAttribute = $this->connection->ldapUserDisplayName;  | 
                                                        
| 536 | 536 |  		} else { | 
                                                        
@@ -540,15 +540,15 @@ discard block  | 
                                                    ||
| 540 | 540 | |
| 541 | 541 | //let's try to retrieve the Nextcloud name from the mappings table  | 
                                                        
| 542 | 542 | $ncName = $mapper->getNameByDN($fdn);  | 
                                                        
| 543 | -		if(is_string($ncName)) { | 
                                                        |
| 543 | +		if (is_string($ncName)) { | 
                                                        |
| 544 | 544 | return $ncName;  | 
                                                        
| 545 | 545 | }  | 
                                                        
| 546 | 546 | |
| 547 | 547 | //second try: get the UUID and check if it is known. Then, update the DN and return the name.  | 
                                                        
| 548 | 548 | $uuid = $this->getUUID($fdn, $isUser, $record);  | 
                                                        
| 549 | -		if(is_string($uuid)) { | 
                                                        |
| 549 | +		if (is_string($uuid)) { | 
                                                        |
| 550 | 550 | $ncName = $mapper->getNameByUUID($uuid);  | 
                                                        
| 551 | -			if(is_string($ncName)) { | 
                                                        |
| 551 | +			if (is_string($ncName)) { | 
                                                        |
| 552 | 552 | $mapper->setDNbyUUID($fdn, $uuid);  | 
                                                        
| 553 | 553 | return $ncName;  | 
                                                        
| 554 | 554 | }  | 
                                                        
@@ -558,16 +558,16 @@ discard block  | 
                                                    ||
| 558 | 558 | return false;  | 
                                                        
| 559 | 559 | }  | 
                                                        
| 560 | 560 | |
| 561 | -		if(is_null($ldapName)) { | 
                                                        |
| 561 | +		if (is_null($ldapName)) { | 
                                                        |
| 562 | 562 | $ldapName = $this->readAttribute($fdn, $nameAttribute);  | 
                                                        
| 563 | -			if(!isset($ldapName[0]) && empty($ldapName[0])) { | 
                                                        |
| 563 | +			if (!isset($ldapName[0]) && empty($ldapName[0])) { | 
                                                        |
| 564 | 564 |  				\OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.'.', \OCP\Util::INFO); | 
                                                        
| 565 | 565 | return false;  | 
                                                        
| 566 | 566 | }  | 
                                                        
| 567 | 567 | $ldapName = $ldapName[0];  | 
                                                        
| 568 | 568 | }  | 
                                                        
| 569 | 569 | |
| 570 | -		if($isUser) { | 
                                                        |
| 570 | +		if ($isUser) { | 
                                                        |
| 571 | 571 | $usernameAttribute = strval($this->connection->ldapExpertUsernameAttr);  | 
                                                        
| 572 | 572 |  			if ($usernameAttribute !== '') { | 
                                                        
| 573 | 573 | $username = $this->readAttribute($fdn, $usernameAttribute);  | 
                                                        
@@ -586,9 +586,9 @@ discard block  | 
                                                    ||
| 586 | 586 | // outside of core user management will still cache the user as non-existing.  | 
                                                        
| 587 | 587 | $originalTTL = $this->connection->ldapCacheTTL;  | 
                                                        
| 588 | 588 |  		$this->connection->setConfiguration(array('ldapCacheTTL' => 0)); | 
                                                        
| 589 | - if(($isUser && !\OCP\User::userExists($intName))  | 
                                                        |
| 589 | + if (($isUser && !\OCP\User::userExists($intName))  | 
                                                        |
| 590 | 590 |  			|| (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) { | 
                                                        
| 591 | -			if($mapper->map($fdn, $intName, $uuid)) { | 
                                                        |
| 591 | +			if ($mapper->map($fdn, $intName, $uuid)) { | 
                                                        |
| 592 | 592 |  				$this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); | 
                                                        
| 593 | 593 | $newlyMapped = true;  | 
                                                        
| 594 | 594 | return $intName;  | 
                                                        
@@ -597,7 +597,7 @@ discard block  | 
                                                    ||
| 597 | 597 |  		$this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); | 
                                                        
| 598 | 598 | |
| 599 | 599 | $altName = $this->createAltInternalOwnCloudName($intName, $isUser);  | 
                                                        
| 600 | -		if(is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { | 
                                                        |
| 600 | +		if (is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { | 
                                                        |
| 601 | 601 | $newlyMapped = true;  | 
                                                        
| 602 | 602 | return $altName;  | 
                                                        
| 603 | 603 | }  | 
                                                        
@@ -635,7 +635,7 @@ discard block  | 
                                                    ||
| 635 | 635 | * @return array  | 
                                                        
| 636 | 636 | */  | 
                                                        
| 637 | 637 |  	private function ldap2NextcloudNames($ldapObjects, $isUsers) { | 
                                                        
| 638 | -		if($isUsers) { | 
                                                        |
| 638 | +		if ($isUsers) { | 
                                                        |
| 639 | 639 | $nameAttribute = $this->connection->ldapUserDisplayName;  | 
                                                        
| 640 | 640 | $sndAttribute = $this->connection->ldapUserDisplayName2;  | 
                                                        
| 641 | 641 |  		} else { | 
                                                        
@@ -643,9 +643,9 @@ discard block  | 
                                                    ||
| 643 | 643 | }  | 
                                                        
| 644 | 644 | $nextcloudNames = array();  | 
                                                        
| 645 | 645 | |
| 646 | -		foreach($ldapObjects as $ldapObject) { | 
                                                        |
| 646 | +		foreach ($ldapObjects as $ldapObject) { | 
                                                        |
| 647 | 647 | $nameByLDAP = null;  | 
                                                        
| 648 | - if( isset($ldapObject[$nameAttribute])  | 
                                                        |
| 648 | + if (isset($ldapObject[$nameAttribute])  | 
                                                        |
| 649 | 649 | && is_array($ldapObject[$nameAttribute])  | 
                                                        
| 650 | 650 | && isset($ldapObject[$nameAttribute][0])  | 
                                                        
| 651 | 651 |  			) { | 
                                                        
@@ -654,12 +654,12 @@ discard block  | 
                                                    ||
| 654 | 654 | }  | 
                                                        
| 655 | 655 | |
| 656 | 656 | $ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers);  | 
                                                        
| 657 | -			if($ncName) { | 
                                                        |
| 657 | +			if ($ncName) { | 
                                                        |
| 658 | 658 | $nextcloudNames[] = $ncName;  | 
                                                        
| 659 | -				if($isUsers) { | 
                                                        |
| 659 | +				if ($isUsers) { | 
                                                        |
| 660 | 660 | //cache the user names so it does not need to be retrieved  | 
                                                        
| 661 | 661 | //again later (e.g. sharing dialogue).  | 
                                                        
| 662 | -					if(is_null($nameByLDAP)) { | 
                                                        |
| 662 | +					if (is_null($nameByLDAP)) { | 
                                                        |
| 663 | 663 | continue;  | 
                                                        
| 664 | 664 | }  | 
                                                        
| 665 | 665 | $sndName = isset($ldapObject[$sndAttribute][0])  | 
                                                        
@@ -697,7 +697,7 @@ discard block  | 
                                                    ||
| 697 | 697 | */  | 
                                                        
| 698 | 698 |  	public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { | 
                                                        
| 699 | 699 | $user = $this->userManager->get($ocName);  | 
                                                        
| 700 | -		if($user === null) { | 
                                                        |
| 700 | +		if ($user === null) { | 
                                                        |
| 701 | 701 | return;  | 
                                                        
| 702 | 702 | }  | 
                                                        
| 703 | 703 | $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2);  | 
                                                        
@@ -717,9 +717,9 @@ discard block  | 
                                                    ||
| 717 | 717 | $attempts = 0;  | 
                                                        
| 718 | 718 | //while loop is just a precaution. If a name is not generated within  | 
                                                        
| 719 | 719 | //20 attempts, something else is very wrong. Avoids infinite loop.  | 
                                                        
| 720 | -		while($attempts < 20){ | 
                                                        |
| 721 | - $altName = $name . '_' . rand(1000,9999);  | 
                                                        |
| 722 | -			if(!\OCP\User::userExists($altName)) { | 
                                                        |
| 720 | +		while ($attempts < 20) { | 
                                                        |
| 721 | + $altName = $name.'_'.rand(1000, 9999);  | 
                                                        |
| 722 | +			if (!\OCP\User::userExists($altName)) { | 
                                                        |
| 723 | 723 | return $altName;  | 
                                                        
| 724 | 724 | }  | 
                                                        
| 725 | 725 | $attempts++;  | 
                                                        
@@ -741,25 +741,25 @@ discard block  | 
                                                    ||
| 741 | 741 | */  | 
                                                        
| 742 | 742 |  	private function _createAltInternalOwnCloudNameForGroups($name) { | 
                                                        
| 743 | 743 | $usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%');  | 
                                                        
| 744 | -		if(!($usedNames) || count($usedNames) === 0) { | 
                                                        |
| 744 | +		if (!($usedNames) || count($usedNames) === 0) { | 
                                                        |
| 745 | 745 | $lastNo = 1; //will become name_2  | 
                                                        
| 746 | 746 |  		} else { | 
                                                        
| 747 | 747 | natsort($usedNames);  | 
                                                        
| 748 | 748 | $lastName = array_pop($usedNames);  | 
                                                        
| 749 | 749 | $lastNo = intval(substr($lastName, strrpos($lastName, '_') + 1));  | 
                                                        
| 750 | 750 | }  | 
                                                        
| 751 | - $altName = $name.'_'.strval($lastNo+1);  | 
                                                        |
| 751 | + $altName = $name.'_'.strval($lastNo + 1);  | 
                                                        |
| 752 | 752 | unset($usedNames);  | 
                                                        
| 753 | 753 | |
| 754 | 754 | $attempts = 1;  | 
                                                        
| 755 | -		while($attempts < 21){ | 
                                                        |
| 755 | +		while ($attempts < 21) { | 
                                                        |
| 756 | 756 | // Check to be really sure it is unique  | 
                                                        
| 757 | 757 | // while loop is just a precaution. If a name is not generated within  | 
                                                        
| 758 | 758 | // 20 attempts, something else is very wrong. Avoids infinite loop.  | 
                                                        
| 759 | -			if(!\OC::$server->getGroupManager()->groupExists($altName)) { | 
                                                        |
| 759 | +			if (!\OC::$server->getGroupManager()->groupExists($altName)) { | 
                                                        |
| 760 | 760 | return $altName;  | 
                                                        
| 761 | 761 | }  | 
                                                        
| 762 | - $altName = $name . '_' . ($lastNo + $attempts);  | 
                                                        |
| 762 | + $altName = $name.'_'.($lastNo + $attempts);  | 
                                                        |
| 763 | 763 | $attempts++;  | 
                                                        
| 764 | 764 | }  | 
                                                        
| 765 | 765 | return false;  | 
                                                        
@@ -774,7 +774,7 @@ discard block  | 
                                                    ||
| 774 | 774 |  	private function createAltInternalOwnCloudName($name, $isUser) { | 
                                                        
| 775 | 775 | $originalTTL = $this->connection->ldapCacheTTL;  | 
                                                        
| 776 | 776 |  		$this->connection->setConfiguration(array('ldapCacheTTL' => 0)); | 
                                                        
| 777 | -		if($isUser) { | 
                                                        |
| 777 | +		if ($isUser) { | 
                                                        |
| 778 | 778 | $altName = $this->_createAltInternalOwnCloudNameForUsers($name);  | 
                                                        
| 779 | 779 |  		} else { | 
                                                        
| 780 | 780 | $altName = $this->_createAltInternalOwnCloudNameForGroups($name);  | 
                                                        
@@ -824,7 +824,7 @@ discard block  | 
                                                    ||
| 824 | 824 |  	public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null, $forceApplyAttributes = false) { | 
                                                        
| 825 | 825 | $ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset);  | 
                                                        
| 826 | 826 | $recordsToUpdate = $ldapRecords;  | 
                                                        
| 827 | -		if(!$forceApplyAttributes) { | 
                                                        |
| 827 | +		if (!$forceApplyAttributes) { | 
                                                        |
| 828 | 828 | $isBackgroundJobModeAjax = $this->config  | 
                                                        
| 829 | 829 |  					->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; | 
                                                        
| 830 | 830 |  			$recordsToUpdate = array_filter($ldapRecords, function($record) use ($isBackgroundJobModeAjax) { | 
                                                        
@@ -844,19 +844,19 @@ discard block  | 
                                                    ||
| 844 | 844 | * and their values  | 
                                                        
| 845 | 845 | * @param array $ldapRecords  | 
                                                        
| 846 | 846 | */  | 
                                                        
| 847 | -	public function batchApplyUserAttributes(array $ldapRecords){ | 
                                                        |
| 847 | +	public function batchApplyUserAttributes(array $ldapRecords) { | 
                                                        |
| 848 | 848 | $displayNameAttribute = strtolower($this->connection->ldapUserDisplayName);  | 
                                                        
| 849 | -		foreach($ldapRecords as $userRecord) { | 
                                                        |
| 850 | -			if(!isset($userRecord[$displayNameAttribute])) { | 
                                                        |
| 849 | +		foreach ($ldapRecords as $userRecord) { | 
                                                        |
| 850 | +			if (!isset($userRecord[$displayNameAttribute])) { | 
                                                        |
| 851 | 851 | // displayName is obligatory  | 
                                                        
| 852 | 852 | continue;  | 
                                                        
| 853 | 853 | }  | 
                                                        
| 854 | - $ocName = $this->dn2ocname($userRecord['dn'][0], null, true);  | 
                                                        |
| 855 | -			if($ocName === false) { | 
                                                        |
| 854 | + $ocName = $this->dn2ocname($userRecord['dn'][0], null, true);  | 
                                                        |
| 855 | +			if ($ocName === false) { | 
                                                        |
| 856 | 856 | continue;  | 
                                                        
| 857 | 857 | }  | 
                                                        
| 858 | 858 | $user = $this->userManager->get($ocName);  | 
                                                        
| 859 | -			if($user instanceof OfflineUser) { | 
                                                        |
| 859 | +			if ($user instanceof OfflineUser) { | 
                                                        |
| 860 | 860 | $user->unmark();  | 
                                                        
| 861 | 861 | $user = $this->userManager->get($ocName);  | 
                                                        
| 862 | 862 | }  | 
                                                        
@@ -888,8 +888,8 @@ discard block  | 
                                                    ||
| 888 | 888 | * @return array  | 
                                                        
| 889 | 889 | */  | 
                                                        
| 890 | 890 |  	private function fetchList($list, $manyAttributes) { | 
                                                        
| 891 | -		if(is_array($list)) { | 
                                                        |
| 892 | -			if($manyAttributes) { | 
                                                        |
| 891 | +		if (is_array($list)) { | 
                                                        |
| 892 | +			if ($manyAttributes) { | 
                                                        |
| 893 | 893 | return $list;  | 
                                                        
| 894 | 894 |  			} else { | 
                                                        
| 895 | 895 |  				$list = array_reduce($list, function($carry, $item) { | 
                                                        
@@ -987,7 +987,7 @@ discard block  | 
                                                    ||
| 987 | 987 | // php no longer supports call-time pass-by-reference  | 
                                                        
| 988 | 988 | // thus cannot support controlPagedResultResponse as the third argument  | 
                                                        
| 989 | 989 | // is a reference  | 
                                                        
| 990 | -		$doMethod = function () use ($command, &$arguments) { | 
                                                        |
| 990 | +		$doMethod = function() use ($command, &$arguments) { | 
                                                        |
| 991 | 991 |  			if ($command == 'controlPagedResultResponse') { | 
                                                        
| 992 | 992 |  				throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.'); | 
                                                        
| 993 | 993 |  			} else { | 
                                                        
@@ -1005,7 +1005,7 @@ discard block  | 
                                                    ||
| 1005 | 1005 | $this->connection->resetConnectionResource();  | 
                                                        
| 1006 | 1006 | $cr = $this->connection->getConnectionResource();  | 
                                                        
| 1007 | 1007 | |
| 1008 | -			if(!$this->ldap->isResource($cr)) { | 
                                                        |
| 1008 | +			if (!$this->ldap->isResource($cr)) { | 
                                                        |
| 1009 | 1009 | // Seems like we didn't find any resource.  | 
                                                        
| 1010 | 1010 |  				\OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", \OCP\Util::DEBUG); | 
                                                        
| 1011 | 1011 | throw $e;  | 
                                                        
@@ -1030,13 +1030,13 @@ discard block  | 
                                                    ||
| 1030 | 1030 | * @throws ServerNotAvailableException  | 
                                                        
| 1031 | 1031 | */  | 
                                                        
| 1032 | 1032 |  	private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) { | 
                                                        
| 1033 | -		if(!is_null($attr) && !is_array($attr)) { | 
                                                        |
| 1033 | +		if (!is_null($attr) && !is_array($attr)) { | 
                                                        |
| 1034 | 1034 | $attr = array(mb_strtolower($attr, 'UTF-8'));  | 
                                                        
| 1035 | 1035 | }  | 
                                                        
| 1036 | 1036 | |
| 1037 | 1037 | // See if we have a resource, in case not cancel with message  | 
                                                        
| 1038 | 1038 | $cr = $this->connection->getConnectionResource();  | 
                                                        
| 1039 | -		if(!$this->ldap->isResource($cr)) { | 
                                                        |
| 1039 | +		if (!$this->ldap->isResource($cr)) { | 
                                                        |
| 1040 | 1040 | // Seems like we didn't find any resource.  | 
                                                        
| 1041 | 1041 | // Return an empty array just like before.  | 
                                                        
| 1042 | 1042 |  			\OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', \OCP\Util::DEBUG); | 
                                                        
@@ -1050,7 +1050,7 @@ discard block  | 
                                                    ||
| 1050 | 1050 |  		$sr = $this->invokeLDAPMethod('search', $linkResources, $base, $filter, $attr); | 
                                                        
| 1051 | 1051 | // cannot use $cr anymore, might have changed in the previous call!  | 
                                                        
| 1052 | 1052 | $error = $this->ldap->errno($this->connection->getConnectionResource());  | 
                                                        
| 1053 | -		if(!is_array($sr) || $error !== 0) { | 
                                                        |
| 1053 | +		if (!is_array($sr) || $error !== 0) { | 
                                                        |
| 1054 | 1054 |  			\OCP\Util::writeLog('user_ldap', 'Attempt for Paging?  '.print_r($pagedSearchOK, true), \OCP\Util::ERROR); | 
                                                        
| 1055 | 1055 | return false;  | 
                                                        
| 1056 | 1056 | }  | 
                                                        
@@ -1073,26 +1073,26 @@ discard block  | 
                                                    ||
| 1073 | 1073 | */  | 
                                                        
| 1074 | 1074 |  	private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) { | 
                                                        
| 1075 | 1075 | $cookie = null;  | 
                                                        
| 1076 | -		if($pagedSearchOK) { | 
                                                        |
| 1076 | +		if ($pagedSearchOK) { | 
                                                        |
| 1077 | 1077 | $cr = $this->connection->getConnectionResource();  | 
                                                        
| 1078 | -			foreach($sr as $key => $res) { | 
                                                        |
| 1079 | -				if($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { | 
                                                        |
| 1078 | +			foreach ($sr as $key => $res) { | 
                                                        |
| 1079 | +				if ($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { | 
                                                        |
| 1080 | 1080 | $this->setPagedResultCookie($base[$key], $filter, $limit, $offset, $cookie);  | 
                                                        
| 1081 | 1081 | }  | 
                                                        
| 1082 | 1082 | }  | 
                                                        
| 1083 | 1083 | |
| 1084 | 1084 | //browsing through prior pages to get the cookie for the new one  | 
                                                        
| 1085 | -			if($skipHandling) { | 
                                                        |
| 1085 | +			if ($skipHandling) { | 
                                                        |
| 1086 | 1086 | return false;  | 
                                                        
| 1087 | 1087 | }  | 
                                                        
| 1088 | 1088 | // if count is bigger, then the server does not support  | 
                                                        
| 1089 | 1089 | // paged search. Instead, he did a normal search. We set a  | 
                                                        
| 1090 | 1090 | // flag here, so the callee knows how to deal with it.  | 
                                                        
| 1091 | -			if($iFoundItems <= $limit) { | 
                                                        |
| 1091 | +			if ($iFoundItems <= $limit) { | 
                                                        |
| 1092 | 1092 | $this->pagedSearchedSuccessful = true;  | 
                                                        
| 1093 | 1093 | }  | 
                                                        
| 1094 | 1094 |  		} else { | 
                                                        
| 1095 | -			if(!is_null($limit)) { | 
                                                        |
| 1095 | +			if (!is_null($limit)) { | 
                                                        |
| 1096 | 1096 |  				\OCP\Util::writeLog('user_ldap', 'Paged search was not available', \OCP\Util::INFO); | 
                                                        
| 1097 | 1097 | }  | 
                                                        
| 1098 | 1098 | }  | 
                                                        
@@ -1121,7 +1121,7 @@ discard block  | 
                                                    ||
| 1121 | 1121 |  		\OCP\Util::writeLog('user_ldap', 'Count filter:  '.print_r($filter, true), \OCP\Util::DEBUG); | 
                                                        
| 1122 | 1122 | |
| 1123 | 1123 | $limitPerPage = intval($this->connection->ldapPagingSize);  | 
                                                        
| 1124 | -		if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { | 
                                                        |
| 1124 | +		if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) { | 
                                                        |
| 1125 | 1125 | $limitPerPage = $limit;  | 
                                                        
| 1126 | 1126 | }  | 
                                                        
| 1127 | 1127 | |
@@ -1132,7 +1132,7 @@ discard block  | 
                                                    ||
| 1132 | 1132 |  		do { | 
                                                        
| 1133 | 1133 | $search = $this->executeSearch($filter, $base, $attr,  | 
                                                        
| 1134 | 1134 | $limitPerPage, $offset);  | 
                                                        
| 1135 | -			if($search === false) { | 
                                                        |
| 1135 | +			if ($search === false) { | 
                                                        |
| 1136 | 1136 | return $counter > 0 ? $counter : false;  | 
                                                        
| 1137 | 1137 | }  | 
                                                        
| 1138 | 1138 | list($sr, $pagedSearchOK) = $search;  | 
                                                        
@@ -1151,7 +1151,7 @@ discard block  | 
                                                    ||
| 1151 | 1151 | * Continue now depends on $hasMorePages value  | 
                                                        
| 1152 | 1152 | */  | 
                                                        
| 1153 | 1153 | $continue = $pagedSearchOK && $hasMorePages;  | 
                                                        
| 1154 | - } while($continue && (is_null($limit) || $limit <= 0 || $limit > $counter));  | 
                                                        |
| 1154 | + } while ($continue && (is_null($limit) || $limit <= 0 || $limit > $counter));  | 
                                                        |
| 1155 | 1155 | |
| 1156 | 1156 | return $counter;  | 
                                                        
| 1157 | 1157 | }  | 
                                                        
@@ -1163,7 +1163,7 @@ discard block  | 
                                                    ||
| 1163 | 1163 |  	private function countEntriesInSearchResults($searchResults) { | 
                                                        
| 1164 | 1164 | $counter = 0;  | 
                                                        
| 1165 | 1165 | |
| 1166 | -		foreach($searchResults as $res) { | 
                                                        |
| 1166 | +		foreach ($searchResults as $res) { | 
                                                        |
| 1167 | 1167 |  			$count = intval($this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res)); | 
                                                        
| 1168 | 1168 | $counter += $count;  | 
                                                        
| 1169 | 1169 | }  | 
                                                        
@@ -1185,7 +1185,7 @@ discard block  | 
                                                    ||
| 1185 | 1185 | */  | 
                                                        
| 1186 | 1186 |  	public function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { | 
                                                        
| 1187 | 1187 | $limitPerPage = intval($this->connection->ldapPagingSize);  | 
                                                        
| 1188 | -		if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { | 
                                                        |
| 1188 | +		if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) { | 
                                                        |
| 1189 | 1189 | $limitPerPage = $limit;  | 
                                                        
| 1190 | 1190 | }  | 
                                                        
| 1191 | 1191 | |
@@ -1199,13 +1199,13 @@ discard block  | 
                                                    ||
| 1199 | 1199 | $savedoffset = $offset;  | 
                                                        
| 1200 | 1200 |  		do { | 
                                                        
| 1201 | 1201 | $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset);  | 
                                                        
| 1202 | -			if($search === false) { | 
                                                        |
| 1202 | +			if ($search === false) { | 
                                                        |
| 1203 | 1203 | return [];  | 
                                                        
| 1204 | 1204 | }  | 
                                                        
| 1205 | 1205 | list($sr, $pagedSearchOK) = $search;  | 
                                                        
| 1206 | 1206 | $cr = $this->connection->getConnectionResource();  | 
                                                        
| 1207 | 1207 | |
| 1208 | -			if($skipHandling) { | 
                                                        |
| 1208 | +			if ($skipHandling) { | 
                                                        |
| 1209 | 1209 | //i.e. result do not need to be fetched, we just need the cookie  | 
                                                        
| 1210 | 1210 | //thus pass 1 or any other value as $iFoundItems because it is not  | 
                                                        
| 1211 | 1211 | //used  | 
                                                        
@@ -1216,7 +1216,7 @@ discard block  | 
                                                    ||
| 1216 | 1216 | }  | 
                                                        
| 1217 | 1217 | |
| 1218 | 1218 | $iFoundItems = 0;  | 
                                                        
| 1219 | -			foreach($sr as $res) { | 
                                                        |
| 1219 | +			foreach ($sr as $res) { | 
                                                        |
| 1220 | 1220 |  				$findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $res)); | 
                                                        
| 1221 | 1221 | $iFoundItems = max($iFoundItems, $findings['count']);  | 
                                                        
| 1222 | 1222 | unset($findings['count']);  | 
                                                        
@@ -1232,24 +1232,24 @@ discard block  | 
                                                    ||
| 1232 | 1232 | |
| 1233 | 1233 | // if we're here, probably no connection resource is returned.  | 
                                                        
| 1234 | 1234 | // to make Nextcloud behave nicely, we simply give back an empty array.  | 
                                                        
| 1235 | -		if(is_null($findings)) { | 
                                                        |
| 1235 | +		if (is_null($findings)) { | 
                                                        |
| 1236 | 1236 | return array();  | 
                                                        
| 1237 | 1237 | }  | 
                                                        
| 1238 | 1238 | |
| 1239 | -		if(!is_null($attr)) { | 
                                                        |
| 1239 | +		if (!is_null($attr)) { | 
                                                        |
| 1240 | 1240 | $selection = [];  | 
                                                        
| 1241 | 1241 | $i = 0;  | 
                                                        
| 1242 | -			foreach($findings as $item) { | 
                                                        |
| 1243 | -				if(!is_array($item)) { | 
                                                        |
| 1242 | +			foreach ($findings as $item) { | 
                                                        |
| 1243 | +				if (!is_array($item)) { | 
                                                        |
| 1244 | 1244 | continue;  | 
                                                        
| 1245 | 1245 | }  | 
                                                        
| 1246 | 1246 | $item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8');  | 
                                                        
| 1247 | -				foreach($attr as $key) { | 
                                                        |
| 1248 | -					if(isset($item[$key])) { | 
                                                        |
| 1249 | -						if(is_array($item[$key]) && isset($item[$key]['count'])) { | 
                                                        |
| 1247 | +				foreach ($attr as $key) { | 
                                                        |
| 1248 | +					if (isset($item[$key])) { | 
                                                        |
| 1249 | +						if (is_array($item[$key]) && isset($item[$key]['count'])) { | 
                                                        |
| 1250 | 1250 | unset($item[$key]['count']);  | 
                                                        
| 1251 | 1251 | }  | 
                                                        
| 1252 | -						if($key !== 'dn') { | 
                                                        |
| 1252 | +						if ($key !== 'dn') { | 
                                                        |
| 1253 | 1253 | $selection[$i][$key] = $this->resemblesDN($key) ?  | 
                                                        
| 1254 | 1254 | $this->helper->sanitizeDN($item[$key])  | 
                                                        
| 1255 | 1255 | : $key === 'objectguid' || $key === 'guid' ?  | 
                                                        
@@ -1268,7 +1268,7 @@ discard block  | 
                                                    ||
| 1268 | 1268 | //we slice the findings, when  | 
                                                        
| 1269 | 1269 | //a) paged search unsuccessful, though attempted  | 
                                                        
| 1270 | 1270 | //b) no paged search, but limit set  | 
                                                        
| 1271 | - if((!$this->getPagedSearchResultState()  | 
                                                        |
| 1271 | + if ((!$this->getPagedSearchResultState()  | 
                                                        |
| 1272 | 1272 | && $pagedSearchOK)  | 
                                                        
| 1273 | 1273 | || (  | 
                                                        
| 1274 | 1274 | !$pagedSearchOK  | 
                                                        
@@ -1285,7 +1285,7 @@ discard block  | 
                                                    ||
| 1285 | 1285 | * @return bool|mixed|string  | 
                                                        
| 1286 | 1286 | */  | 
                                                        
| 1287 | 1287 |  	public function sanitizeUsername($name) { | 
                                                        
| 1288 | -		if($this->connection->ldapIgnoreNamingRules) { | 
                                                        |
| 1288 | +		if ($this->connection->ldapIgnoreNamingRules) { | 
                                                        |
| 1289 | 1289 | return $name;  | 
                                                        
| 1290 | 1290 | }  | 
                                                        
| 1291 | 1291 | |
@@ -1310,13 +1310,13 @@ discard block  | 
                                                    ||
| 1310 | 1310 | */  | 
                                                        
| 1311 | 1311 |  	public function escapeFilterPart($input, $allowAsterisk = false) { | 
                                                        
| 1312 | 1312 | $asterisk = '';  | 
                                                        
| 1313 | -		if($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { | 
                                                        |
| 1313 | +		if ($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { | 
                                                        |
| 1314 | 1314 | $asterisk = '*';  | 
                                                        
| 1315 | 1315 | $input = mb_substr($input, 1, null, 'UTF-8');  | 
                                                        
| 1316 | 1316 | }  | 
                                                        
| 1317 | 1317 |  		$search  = array('*', '\\', '(', ')'); | 
                                                        
| 1318 | 1318 |  		$replace = array('\\*', '\\\\', '\\(', '\\)'); | 
                                                        
| 1319 | - return $asterisk . str_replace($search, $replace, $input);  | 
                                                        |
| 1319 | + return $asterisk.str_replace($search, $replace, $input);  | 
                                                        |
| 1320 | 1320 | }  | 
                                                        
| 1321 | 1321 | |
| 1322 | 1322 | /**  | 
                                                        
@@ -1346,13 +1346,13 @@ discard block  | 
                                                    ||
| 1346 | 1346 | */  | 
                                                        
| 1347 | 1347 |  	private function combineFilter($filters, $operator) { | 
                                                        
| 1348 | 1348 |  		$combinedFilter = '('.$operator; | 
                                                        
| 1349 | -		foreach($filters as $filter) { | 
                                                        |
| 1349 | +		foreach ($filters as $filter) { | 
                                                        |
| 1350 | 1350 |  			if ($filter !== '' && $filter[0] !== '(') { | 
                                                        
| 1351 | 1351 |  				$filter = '('.$filter.')'; | 
                                                        
| 1352 | 1352 | }  | 
                                                        
| 1353 | - $combinedFilter.=$filter;  | 
                                                        |
| 1353 | + $combinedFilter .= $filter;  | 
                                                        |
| 1354 | 1354 | }  | 
                                                        
| 1355 | - $combinedFilter.=')';  | 
                                                        |
| 1355 | + $combinedFilter .= ')';  | 
                                                        |
| 1356 | 1356 | return $combinedFilter;  | 
                                                        
| 1357 | 1357 | }  | 
                                                        
| 1358 | 1358 | |
@@ -1388,17 +1388,17 @@ discard block  | 
                                                    ||
| 1388 | 1388 | * @throws \Exception  | 
                                                        
| 1389 | 1389 | */  | 
                                                        
| 1390 | 1390 |  	private function getAdvancedFilterPartForSearch($search, $searchAttributes) { | 
                                                        
| 1391 | -		if(!is_array($searchAttributes) || count($searchAttributes) < 2) { | 
                                                        |
| 1391 | +		if (!is_array($searchAttributes) || count($searchAttributes) < 2) { | 
                                                        |
| 1392 | 1392 |  			throw new \Exception('searchAttributes must be an array with at least two string'); | 
                                                        
| 1393 | 1393 | }  | 
                                                        
| 1394 | 1394 |  		$searchWords = explode(' ', trim($search)); | 
                                                        
| 1395 | 1395 | $wordFilters = array();  | 
                                                        
| 1396 | -		foreach($searchWords as $word) { | 
                                                        |
| 1396 | +		foreach ($searchWords as $word) { | 
                                                        |
| 1397 | 1397 | $word = $this->prepareSearchTerm($word);  | 
                                                        
| 1398 | 1398 | //every word needs to appear at least once  | 
                                                        
| 1399 | 1399 | $wordMatchOneAttrFilters = array();  | 
                                                        
| 1400 | -			foreach($searchAttributes as $attr) { | 
                                                        |
| 1401 | - $wordMatchOneAttrFilters[] = $attr . '=' . $word;  | 
                                                        |
| 1400 | +			foreach ($searchAttributes as $attr) { | 
                                                        |
| 1401 | + $wordMatchOneAttrFilters[] = $attr.'='.$word;  | 
                                                        |
| 1402 | 1402 | }  | 
                                                        
| 1403 | 1403 | $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters);  | 
                                                        
| 1404 | 1404 | }  | 
                                                        
@@ -1416,10 +1416,10 @@ discard block  | 
                                                    ||
| 1416 | 1416 |  	private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { | 
                                                        
| 1417 | 1417 | $filter = array();  | 
                                                        
| 1418 | 1418 | $haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0);  | 
                                                        
| 1419 | -		if($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { | 
                                                        |
| 1419 | +		if ($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { | 
                                                        |
| 1420 | 1420 |  			try { | 
                                                        
| 1421 | 1421 | return $this->getAdvancedFilterPartForSearch($search, $searchAttributes);  | 
                                                        
| 1422 | -			} catch(\Exception $e) { | 
                                                        |
| 1422 | +			} catch (\Exception $e) { | 
                                                        |
| 1423 | 1423 | \OCP\Util::writeLog(  | 
                                                        
| 1424 | 1424 | 'user_ldap',  | 
                                                        
| 1425 | 1425 | 'Creating advanced filter for search failed, falling back to simple method.',  | 
                                                        
@@ -1429,17 +1429,17 @@ discard block  | 
                                                    ||
| 1429 | 1429 | }  | 
                                                        
| 1430 | 1430 | |
| 1431 | 1431 | $search = $this->prepareSearchTerm($search);  | 
                                                        
| 1432 | -		if(!is_array($searchAttributes) || count($searchAttributes) === 0) { | 
                                                        |
| 1432 | +		if (!is_array($searchAttributes) || count($searchAttributes) === 0) { | 
                                                        |
| 1433 | 1433 |  			if ($fallbackAttribute === '') { | 
                                                        
| 1434 | 1434 | return '';  | 
                                                        
| 1435 | 1435 | }  | 
                                                        
| 1436 | - $filter[] = $fallbackAttribute . '=' . $search;  | 
                                                        |
| 1436 | + $filter[] = $fallbackAttribute.'='.$search;  | 
                                                        |
| 1437 | 1437 |  		} else { | 
                                                        
| 1438 | -			foreach($searchAttributes as $attribute) { | 
                                                        |
| 1439 | - $filter[] = $attribute . '=' . $search;  | 
                                                        |
| 1438 | +			foreach ($searchAttributes as $attribute) { | 
                                                        |
| 1439 | + $filter[] = $attribute.'='.$search;  | 
                                                        |
| 1440 | 1440 | }  | 
                                                        
| 1441 | 1441 | }  | 
                                                        
| 1442 | -		if(count($filter) === 1) { | 
                                                        |
| 1442 | +		if (count($filter) === 1) { | 
                                                        |
| 1443 | 1443 |  			return '('.$filter[0].')'; | 
                                                        
| 1444 | 1444 | }  | 
                                                        
| 1445 | 1445 | return $this->combineFilterWithOr($filter);  | 
                                                        
@@ -1460,7 +1460,7 @@ discard block  | 
                                                    ||
| 1460 | 1460 |  		if ($term === '') { | 
                                                        
| 1461 | 1461 | $result = '*';  | 
                                                        
| 1462 | 1462 |  		} else if ($allowEnum !== 'no') { | 
                                                        
| 1463 | - $result = $term . '*';  | 
                                                        |
| 1463 | + $result = $term.'*';  | 
                                                        |
| 1464 | 1464 | }  | 
                                                        
| 1465 | 1465 | return $result;  | 
                                                        
| 1466 | 1466 | }  | 
                                                        
@@ -1472,7 +1472,7 @@ discard block  | 
                                                    ||
| 1472 | 1472 |  	public function getFilterForUserCount() { | 
                                                        
| 1473 | 1473 | $filter = $this->combineFilterWithAnd(array(  | 
                                                        
| 1474 | 1474 | $this->connection->ldapUserFilter,  | 
                                                        
| 1475 | - $this->connection->ldapUserDisplayName . '=*'  | 
                                                        |
| 1475 | + $this->connection->ldapUserDisplayName.'=*'  | 
                                                        |
| 1476 | 1476 | ));  | 
                                                        
| 1477 | 1477 | |
| 1478 | 1478 | return $filter;  | 
                                                        
@@ -1490,7 +1490,7 @@ discard block  | 
                                                    ||
| 1490 | 1490 | 'ldapAgentName' => $name,  | 
                                                        
| 1491 | 1491 | 'ldapAgentPassword' => $password  | 
                                                        
| 1492 | 1492 | );  | 
                                                        
| 1493 | -		if(!$testConnection->setConfiguration($credentials)) { | 
                                                        |
| 1493 | +		if (!$testConnection->setConfiguration($credentials)) { | 
                                                        |
| 1494 | 1494 | return false;  | 
                                                        
| 1495 | 1495 | }  | 
                                                        
| 1496 | 1496 | return $testConnection->bind();  | 
                                                        
@@ -1512,30 +1512,30 @@ discard block  | 
                                                    ||
| 1512 | 1512 | // Sacrebleu! The UUID attribute is unknown :( We need first an  | 
                                                        
| 1513 | 1513 | // existing DN to be able to reliably detect it.  | 
                                                        
| 1514 | 1514 | $result = $this->search($filter, $base, ['dn'], 1);  | 
                                                        
| 1515 | -			if(!isset($result[0]) || !isset($result[0]['dn'])) { | 
                                                        |
| 1515 | +			if (!isset($result[0]) || !isset($result[0]['dn'])) { | 
                                                        |
| 1516 | 1516 |  				throw new \Exception('Cannot determine UUID attribute'); | 
                                                        
| 1517 | 1517 | }  | 
                                                        
| 1518 | 1518 | $dn = $result[0]['dn'][0];  | 
                                                        
| 1519 | -			if(!$this->detectUuidAttribute($dn, true)) { | 
                                                        |
| 1519 | +			if (!$this->detectUuidAttribute($dn, true)) { | 
                                                        |
| 1520 | 1520 |  				throw new \Exception('Cannot determine UUID attribute'); | 
                                                        
| 1521 | 1521 | }  | 
                                                        
| 1522 | 1522 |  		} else { | 
                                                        
| 1523 | 1523 | // The UUID attribute is either known or an override is given.  | 
                                                        
| 1524 | 1524 | // By calling this method we ensure that $this->connection->$uuidAttr  | 
                                                        
| 1525 | 1525 | // is definitely set  | 
                                                        
| 1526 | -			if(!$this->detectUuidAttribute('', true)) { | 
                                                        |
| 1526 | +			if (!$this->detectUuidAttribute('', true)) { | 
                                                        |
| 1527 | 1527 |  				throw new \Exception('Cannot determine UUID attribute'); | 
                                                        
| 1528 | 1528 | }  | 
                                                        
| 1529 | 1529 | }  | 
                                                        
| 1530 | 1530 | |
| 1531 | 1531 | $uuidAttr = $this->connection->ldapUuidUserAttribute;  | 
                                                        
| 1532 | -		if($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { | 
                                                        |
| 1532 | +		if ($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { | 
                                                        |
| 1533 | 1533 | $uuid = $this->formatGuid2ForFilterUser($uuid);  | 
                                                        
| 1534 | 1534 | }  | 
                                                        
| 1535 | 1535 | |
| 1536 | - $filter = $uuidAttr . '=' . $uuid;  | 
                                                        |
| 1536 | + $filter = $uuidAttr.'='.$uuid;  | 
                                                        |
| 1537 | 1537 | $result = $this->searchUsers($filter, ['dn'], 2);  | 
                                                        
| 1538 | -		if(is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { | 
                                                        |
| 1538 | +		if (is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { | 
                                                        |
| 1539 | 1539 | // we put the count into account to make sure that this is  | 
                                                        
| 1540 | 1540 | // really unique  | 
                                                        
| 1541 | 1541 | return $result[0]['dn'][0];  | 
                                                        
@@ -1554,7 +1554,7 @@ discard block  | 
                                                    ||
| 1554 | 1554 | * @return bool true on success, false otherwise  | 
                                                        
| 1555 | 1555 | */  | 
                                                        
| 1556 | 1556 |  	private function detectUuidAttribute($dn, $isUser = true, $force = false, array $ldapRecord = null) { | 
                                                        
| 1557 | -		if($isUser) { | 
                                                        |
| 1557 | +		if ($isUser) { | 
                                                        |
| 1558 | 1558 | $uuidAttr = 'ldapUuidUserAttribute';  | 
                                                        
| 1559 | 1559 | $uuidOverride = $this->connection->ldapExpertUUIDUserAttr;  | 
                                                        
| 1560 | 1560 |  		} else { | 
                                                        
@@ -1562,7 +1562,7 @@ discard block  | 
                                                    ||
| 1562 | 1562 | $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr;  | 
                                                        
| 1563 | 1563 | }  | 
                                                        
| 1564 | 1564 | |
| 1565 | -		if(($this->connection->$uuidAttr !== 'auto') && !$force) { | 
                                                        |
| 1565 | +		if (($this->connection->$uuidAttr !== 'auto') && !$force) { | 
                                                        |
| 1566 | 1566 | return true;  | 
                                                        
| 1567 | 1567 | }  | 
                                                        
| 1568 | 1568 | |
@@ -1571,10 +1571,10 @@ discard block  | 
                                                    ||
| 1571 | 1571 | return true;  | 
                                                        
| 1572 | 1572 | }  | 
                                                        
| 1573 | 1573 | |
| 1574 | -		foreach(self::UUID_ATTRIBUTES as $attribute) { | 
                                                        |
| 1575 | -			if($ldapRecord !== null) { | 
                                                        |
| 1574 | +		foreach (self::UUID_ATTRIBUTES as $attribute) { | 
                                                        |
| 1575 | +			if ($ldapRecord !== null) { | 
                                                        |
| 1576 | 1576 | // we have the info from LDAP already, we don't need to talk to the server again  | 
                                                        
| 1577 | -				if(isset($ldapRecord[$attribute])) { | 
                                                        |
| 1577 | +				if (isset($ldapRecord[$attribute])) { | 
                                                        |
| 1578 | 1578 | $this->connection->$uuidAttr = $attribute;  | 
                                                        
| 1579 | 1579 | return true;  | 
                                                        
| 1580 | 1580 |  				} else { | 
                                                        
@@ -1583,7 +1583,7 @@ discard block  | 
                                                    ||
| 1583 | 1583 | }  | 
                                                        
| 1584 | 1584 | |
| 1585 | 1585 | $value = $this->readAttribute($dn, $attribute);  | 
                                                        
| 1586 | -			if(is_array($value) && isset($value[0]) && !empty($value[0])) { | 
                                                        |
| 1586 | +			if (is_array($value) && isset($value[0]) && !empty($value[0])) { | 
                                                        |
| 1587 | 1587 |  				\OCP\Util::writeLog('user_ldap', | 
                                                        
| 1588 | 1588 | 'Setting '.$attribute.' as '.$uuidAttr,  | 
                                                        
| 1589 | 1589 | \OCP\Util::DEBUG);  | 
                                                        
@@ -1605,7 +1605,7 @@ discard block  | 
                                                    ||
| 1605 | 1605 | * @return bool|string  | 
                                                        
| 1606 | 1606 | */  | 
                                                        
| 1607 | 1607 |  	public function getUUID($dn, $isUser = true, $ldapRecord = null) { | 
                                                        
| 1608 | -		if($isUser) { | 
                                                        |
| 1608 | +		if ($isUser) { | 
                                                        |
| 1609 | 1609 | $uuidAttr = 'ldapUuidUserAttribute';  | 
                                                        
| 1610 | 1610 | $uuidOverride = $this->connection->ldapExpertUUIDUserAttr;  | 
                                                        
| 1611 | 1611 |  		} else { | 
                                                        
@@ -1614,10 +1614,10 @@ discard block  | 
                                                    ||
| 1614 | 1614 | }  | 
                                                        
| 1615 | 1615 | |
| 1616 | 1616 | $uuid = false;  | 
                                                        
| 1617 | -		if($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) { | 
                                                        |
| 1617 | +		if ($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) { | 
                                                        |
| 1618 | 1618 | $attr = $this->connection->$uuidAttr;  | 
                                                        
| 1619 | 1619 | $uuid = isset($ldapRecord[$attr]) ? $ldapRecord[$attr] : $this->readAttribute($dn, $attr);  | 
                                                        
| 1620 | - if( !is_array($uuid)  | 
                                                        |
| 1620 | + if (!is_array($uuid)  | 
                                                        |
| 1621 | 1621 | && $uuidOverride !== ''  | 
                                                        
| 1622 | 1622 | && $this->detectUuidAttribute($dn, $isUser, true, $ldapRecord))  | 
                                                        
| 1623 | 1623 |  			{ | 
                                                        
@@ -1625,7 +1625,7 @@ discard block  | 
                                                    ||
| 1625 | 1625 | ? $ldapRecord[$this->connection->$uuidAttr]  | 
                                                        
| 1626 | 1626 | : $this->readAttribute($dn, $this->connection->$uuidAttr);  | 
                                                        
| 1627 | 1627 | }  | 
                                                        
| 1628 | -			if(is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { | 
                                                        |
| 1628 | +			if (is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { | 
                                                        |
| 1629 | 1629 | $uuid = $uuid[0];  | 
                                                        
| 1630 | 1630 | }  | 
                                                        
| 1631 | 1631 | }  | 
                                                        
@@ -1642,19 +1642,19 @@ discard block  | 
                                                    ||
| 1642 | 1642 |  	private function convertObjectGUID2Str($oguid) { | 
                                                        
| 1643 | 1643 | $hex_guid = bin2hex($oguid);  | 
                                                        
| 1644 | 1644 | $hex_guid_to_guid_str = '';  | 
                                                        
| 1645 | -		for($k = 1; $k <= 4; ++$k) { | 
                                                        |
| 1645 | +		for ($k = 1; $k <= 4; ++$k) { | 
                                                        |
| 1646 | 1646 | $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2);  | 
                                                        
| 1647 | 1647 | }  | 
                                                        
| 1648 | 1648 | $hex_guid_to_guid_str .= '-';  | 
                                                        
| 1649 | -		for($k = 1; $k <= 2; ++$k) { | 
                                                        |
| 1649 | +		for ($k = 1; $k <= 2; ++$k) { | 
                                                        |
| 1650 | 1650 | $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2);  | 
                                                        
| 1651 | 1651 | }  | 
                                                        
| 1652 | 1652 | $hex_guid_to_guid_str .= '-';  | 
                                                        
| 1653 | -		for($k = 1; $k <= 2; ++$k) { | 
                                                        |
| 1653 | +		for ($k = 1; $k <= 2; ++$k) { | 
                                                        |
| 1654 | 1654 | $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2);  | 
                                                        
| 1655 | 1655 | }  | 
                                                        
| 1656 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4);  | 
                                                        |
| 1657 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20);  | 
                                                        |
| 1656 | + $hex_guid_to_guid_str .= '-'.substr($hex_guid, 16, 4);  | 
                                                        |
| 1657 | + $hex_guid_to_guid_str .= '-'.substr($hex_guid, 20);  | 
                                                        |
| 1658 | 1658 | |
| 1659 | 1659 | return strtoupper($hex_guid_to_guid_str);  | 
                                                        
| 1660 | 1660 | }  | 
                                                        
@@ -1671,11 +1671,11 @@ discard block  | 
                                                    ||
| 1671 | 1671 | * @return string  | 
                                                        
| 1672 | 1672 | */  | 
                                                        
| 1673 | 1673 |  	public function formatGuid2ForFilterUser($guid) { | 
                                                        
| 1674 | -		if(!is_string($guid)) { | 
                                                        |
| 1674 | +		if (!is_string($guid)) { | 
                                                        |
| 1675 | 1675 |  			throw new \InvalidArgumentException('String expected'); | 
                                                        
| 1676 | 1676 | }  | 
                                                        
| 1677 | 1677 |  		$blocks = explode('-', $guid); | 
                                                        
| 1678 | -		if(count($blocks) !== 5) { | 
                                                        |
| 1678 | +		if (count($blocks) !== 5) { | 
                                                        |
| 1679 | 1679 | /*  | 
                                                        
| 1680 | 1680 | * Why not throw an Exception instead? This method is a utility  | 
                                                        
| 1681 | 1681 | * called only when trying to figure out whether a "missing" known  | 
                                                        
@@ -1688,20 +1688,20 @@ discard block  | 
                                                    ||
| 1688 | 1688 | * user. Instead we write a log message.  | 
                                                        
| 1689 | 1689 | */  | 
                                                        
| 1690 | 1690 | \OC::$server->getLogger()->info(  | 
                                                        
| 1691 | - 'Passed string does not resemble a valid GUID. Known UUID ' .  | 
                                                        |
| 1691 | + 'Passed string does not resemble a valid GUID. Known UUID '.  | 
                                                        |
| 1692 | 1692 |  				'({uuid}) probably does not match UUID configuration.', | 
                                                        
| 1693 | - [ 'app' => 'user_ldap', 'uuid' => $guid ]  | 
                                                        |
| 1693 | + ['app' => 'user_ldap', 'uuid' => $guid]  | 
                                                        |
| 1694 | 1694 | );  | 
                                                        
| 1695 | 1695 | return $guid;  | 
                                                        
| 1696 | 1696 | }  | 
                                                        
| 1697 | -		for($i=0; $i < 3; $i++) { | 
                                                        |
| 1697 | +		for ($i = 0; $i < 3; $i++) { | 
                                                        |
| 1698 | 1698 | $pairs = str_split($blocks[$i], 2);  | 
                                                        
| 1699 | 1699 | $pairs = array_reverse($pairs);  | 
                                                        
| 1700 | 1700 |  			$blocks[$i] = implode('', $pairs); | 
                                                        
| 1701 | 1701 | }  | 
                                                        
| 1702 | -		for($i=0; $i < 5; $i++) { | 
                                                        |
| 1702 | +		for ($i = 0; $i < 5; $i++) { | 
                                                        |
| 1703 | 1703 | $pairs = str_split($blocks[$i], 2);  | 
                                                        
| 1704 | -			$blocks[$i] = '\\' . implode('\\', $pairs); | 
                                                        |
| 1704 | +			$blocks[$i] = '\\'.implode('\\', $pairs); | 
                                                        |
| 1705 | 1705 | }  | 
                                                        
| 1706 | 1706 |  		return implode('', $blocks); | 
                                                        
| 1707 | 1707 | }  | 
                                                        
@@ -1715,12 +1715,12 @@ discard block  | 
                                                    ||
| 1715 | 1715 | $domainDN = $this->getDomainDNFromDN($dn);  | 
                                                        
| 1716 | 1716 | $cacheKey = 'getSID-'.$domainDN;  | 
                                                        
| 1717 | 1717 | $sid = $this->connection->getFromCache($cacheKey);  | 
                                                        
| 1718 | -		if(!is_null($sid)) { | 
                                                        |
| 1718 | +		if (!is_null($sid)) { | 
                                                        |
| 1719 | 1719 | return $sid;  | 
                                                        
| 1720 | 1720 | }  | 
                                                        
| 1721 | 1721 | |
| 1722 | 1722 | $objectSid = $this->readAttribute($domainDN, 'objectsid');  | 
                                                        
| 1723 | -		if(!is_array($objectSid) || empty($objectSid)) { | 
                                                        |
| 1723 | +		if (!is_array($objectSid) || empty($objectSid)) { | 
                                                        |
| 1724 | 1724 | $this->connection->writeToCache($cacheKey, false);  | 
                                                        
| 1725 | 1725 | return false;  | 
                                                        
| 1726 | 1726 | }  | 
                                                        
@@ -1778,12 +1778,12 @@ discard block  | 
                                                    ||
| 1778 | 1778 | $belongsToBase = false;  | 
                                                        
| 1779 | 1779 | $bases = $this->helper->sanitizeDN($bases);  | 
                                                        
| 1780 | 1780 | |
| 1781 | -		foreach($bases as $base) { | 
                                                        |
| 1781 | +		foreach ($bases as $base) { | 
                                                        |
| 1782 | 1782 | $belongsToBase = true;  | 
                                                        
| 1783 | -			if(mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8')-mb_strlen($base, 'UTF-8'))) { | 
                                                        |
| 1783 | +			if (mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8') - mb_strlen($base, 'UTF-8'))) { | 
                                                        |
| 1784 | 1784 | $belongsToBase = false;  | 
                                                        
| 1785 | 1785 | }  | 
                                                        
| 1786 | -			if($belongsToBase) { | 
                                                        |
| 1786 | +			if ($belongsToBase) { | 
                                                        |
| 1787 | 1787 | break;  | 
                                                        
| 1788 | 1788 | }  | 
                                                        
| 1789 | 1789 | }  | 
                                                        
@@ -1794,7 +1794,7 @@ discard block  | 
                                                    ||
| 1794 | 1794 | * resets a running Paged Search operation  | 
                                                        
| 1795 | 1795 | */  | 
                                                        
| 1796 | 1796 |  	private function abandonPagedSearch() { | 
                                                        
| 1797 | -		if($this->connection->hasPagedResultSupport) { | 
                                                        |
| 1797 | +		if ($this->connection->hasPagedResultSupport) { | 
                                                        |
| 1798 | 1798 | $cr = $this->connection->getConnectionResource();  | 
                                                        
| 1799 | 1799 |  			$this->invokeLDAPMethod('controlPagedResult', $cr, 0, false, $this->lastCookie); | 
                                                        
| 1800 | 1800 | $this->getPagedSearchResultState();  | 
                                                        
@@ -1812,16 +1812,16 @@ discard block  | 
                                                    ||
| 1812 | 1812 | * @return string containing the key or empty if none is cached  | 
                                                        
| 1813 | 1813 | */  | 
                                                        
| 1814 | 1814 |  	private function getPagedResultCookie($base, $filter, $limit, $offset) { | 
                                                        
| 1815 | -		if($offset === 0) { | 
                                                        |
| 1815 | +		if ($offset === 0) { | 
                                                        |
| 1816 | 1816 | return '';  | 
                                                        
| 1817 | 1817 | }  | 
                                                        
| 1818 | 1818 | $offset -= $limit;  | 
                                                        
| 1819 | 1819 | //we work with cache here  | 
                                                        
| 1820 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . intval($limit) . '-' . intval($offset);  | 
                                                        |
| 1820 | + $cacheKey = 'lc'.crc32($base).'-'.crc32($filter).'-'.intval($limit).'-'.intval($offset);  | 
                                                        |
| 1821 | 1821 | $cookie = '';  | 
                                                        
| 1822 | -		if(isset($this->cookies[$cacheKey])) { | 
                                                        |
| 1822 | +		if (isset($this->cookies[$cacheKey])) { | 
                                                        |
| 1823 | 1823 | $cookie = $this->cookies[$cacheKey];  | 
                                                        
| 1824 | -			if(is_null($cookie)) { | 
                                                        |
| 1824 | +			if (is_null($cookie)) { | 
                                                        |
| 1825 | 1825 | $cookie = '';  | 
                                                        
| 1826 | 1826 | }  | 
                                                        
| 1827 | 1827 | }  | 
                                                        
@@ -1839,11 +1839,11 @@ discard block  | 
                                                    ||
| 1839 | 1839 | * @return bool  | 
                                                        
| 1840 | 1840 | */  | 
                                                        
| 1841 | 1841 |  	public function hasMoreResults() { | 
                                                        
| 1842 | -		if(!$this->connection->hasPagedResultSupport) { | 
                                                        |
| 1842 | +		if (!$this->connection->hasPagedResultSupport) { | 
                                                        |
| 1843 | 1843 | return false;  | 
                                                        
| 1844 | 1844 | }  | 
                                                        
| 1845 | 1845 | |
| 1846 | -		if(empty($this->lastCookie) && $this->lastCookie !== '0') { | 
                                                        |
| 1846 | +		if (empty($this->lastCookie) && $this->lastCookie !== '0') { | 
                                                        |
| 1847 | 1847 | // as in RFC 2696, when all results are returned, the cookie will  | 
                                                        
| 1848 | 1848 | // be empty.  | 
                                                        
| 1849 | 1849 | return false;  | 
                                                        
@@ -1863,8 +1863,8 @@ discard block  | 
                                                    ||
| 1863 | 1863 | */  | 
                                                        
| 1864 | 1864 |  	private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) { | 
                                                        
| 1865 | 1865 | // allow '0' for 389ds  | 
                                                        
| 1866 | -		if(!empty($cookie) || $cookie === '0') { | 
                                                        |
| 1867 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' .intval($limit) . '-' . intval($offset);  | 
                                                        |
| 1866 | +		if (!empty($cookie) || $cookie === '0') { | 
                                                        |
| 1867 | + $cacheKey = 'lc'.crc32($base).'-'.crc32($filter).'-'.intval($limit).'-'.intval($offset);  | 
                                                        |
| 1868 | 1868 | $this->cookies[$cacheKey] = $cookie;  | 
                                                        
| 1869 | 1869 | $this->lastCookie = $cookie;  | 
                                                        
| 1870 | 1870 | }  | 
                                                        
@@ -1891,22 +1891,22 @@ discard block  | 
                                                    ||
| 1891 | 1891 | */  | 
                                                        
| 1892 | 1892 |  	private function initPagedSearch($filter, $bases, $attr, $limit, $offset) { | 
                                                        
| 1893 | 1893 | $pagedSearchOK = false;  | 
                                                        
| 1894 | -		if($this->connection->hasPagedResultSupport && ($limit !== 0)) { | 
                                                        |
| 1894 | +		if ($this->connection->hasPagedResultSupport && ($limit !== 0)) { | 
                                                        |
| 1895 | 1895 | $offset = intval($offset); //can be null  | 
                                                        
| 1896 | 1896 |  			\OCP\Util::writeLog('user_ldap', | 
                                                        
| 1897 | 1897 | 'initializing paged search for Filter '.$filter.' base '.print_r($bases, true)  | 
                                                        
| 1898 | - .' attr '.print_r($attr, true). ' limit ' .$limit.' offset '.$offset,  | 
                                                        |
| 1898 | + .' attr '.print_r($attr, true).' limit '.$limit.' offset '.$offset,  | 
                                                        |
| 1899 | 1899 | \OCP\Util::DEBUG);  | 
                                                        
| 1900 | 1900 | //get the cookie from the search for the previous search, required by LDAP  | 
                                                        
| 1901 | -			foreach($bases as $base) { | 
                                                        |
| 1901 | +			foreach ($bases as $base) { | 
                                                        |
| 1902 | 1902 | |
| 1903 | 1903 | $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset);  | 
                                                        
| 1904 | -				if(empty($cookie) && $cookie !== "0" && ($offset > 0)) { | 
                                                        |
| 1904 | +				if (empty($cookie) && $cookie !== "0" && ($offset > 0)) { | 
                                                        |
| 1905 | 1905 | // no cookie known from a potential previous search. We need  | 
                                                        
| 1906 | 1906 | // to start from 0 to come to the desired page. cookie value  | 
                                                        
| 1907 | 1907 | // of '0' is valid, because 389ds  | 
                                                        
| 1908 | 1908 | $reOffset = 0;  | 
                                                        
| 1909 | -					while($reOffset < $offset) { | 
                                                        |
| 1909 | +					while ($reOffset < $offset) { | 
                                                        |
| 1910 | 1910 | $this->search($filter, array($base), $attr, $limit, $reOffset, true);  | 
                                                        
| 1911 | 1911 | $reOffset += $limit;  | 
                                                        
| 1912 | 1912 | }  | 
                                                        
@@ -1914,17 +1914,17 @@ discard block  | 
                                                    ||
| 1914 | 1914 | //still no cookie? obviously, the server does not like us. Let's skip paging efforts.  | 
                                                        
| 1915 | 1915 | // '0' is valid, because 389ds  | 
                                                        
| 1916 | 1916 | //TODO: remember this, probably does not change in the next request...  | 
                                                        
| 1917 | -					if(empty($cookie) && $cookie !== '0') { | 
                                                        |
| 1917 | +					if (empty($cookie) && $cookie !== '0') { | 
                                                        |
| 1918 | 1918 | $cookie = null;  | 
                                                        
| 1919 | 1919 | }  | 
                                                        
| 1920 | 1920 | }  | 
                                                        
| 1921 | -				if(!is_null($cookie)) { | 
                                                        |
| 1921 | +				if (!is_null($cookie)) { | 
                                                        |
| 1922 | 1922 | //since offset = 0, this is a new search. We abandon other searches that might be ongoing.  | 
                                                        
| 1923 | 1923 | $this->abandonPagedSearch();  | 
                                                        
| 1924 | 1924 |  					$pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', | 
                                                        
| 1925 | 1925 | $this->connection->getConnectionResource(), $limit,  | 
                                                        
| 1926 | 1926 | false, $cookie);  | 
                                                        
| 1927 | -					if(!$pagedSearchOK) { | 
                                                        |
| 1927 | +					if (!$pagedSearchOK) { | 
                                                        |
| 1928 | 1928 | return false;  | 
                                                        
| 1929 | 1929 | }  | 
                                                        
| 1930 | 1930 |  					\OCP\Util::writeLog('user_ldap', 'Ready for a paged search', \OCP\Util::DEBUG); | 
                                                        
@@ -1941,7 +1941,7 @@ discard block  | 
                                                    ||
| 1941 | 1941 | * So we added "&& !empty($this->lastCookie)" to this test to ignore pagination  | 
                                                        
| 1942 | 1942 | * if we don't have a previous paged search.  | 
                                                        
| 1943 | 1943 | */  | 
                                                        
| 1944 | -		} else if($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { | 
                                                        |
| 1944 | +		} else if ($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { | 
                                                        |
| 1945 | 1945 | // a search without limit was requested. However, if we do use  | 
                                                        
| 1946 | 1946 | // Paged Search once, we always must do it. This requires us to  | 
                                                        
| 1947 | 1947 | // initialize it with the configured page size.  | 
                                                        
@@ -37,126 +37,126 @@ discard block  | 
                                                    ||
| 37 | 37 | |
| 38 | 38 |  class Helper { | 
                                                        
| 39 | 39 | |
| 40 | - /** @var IConfig */  | 
                                                        |
| 41 | - private $config;  | 
                                                        |
| 42 | -  | 
                                                        |
| 43 | - /**  | 
                                                        |
| 44 | - * Helper constructor.  | 
                                                        |
| 45 | - *  | 
                                                        |
| 46 | - * @param IConfig $config  | 
                                                        |
| 47 | - */  | 
                                                        |
| 48 | -	public function __construct(IConfig $config) { | 
                                                        |
| 49 | - $this->config = $config;  | 
                                                        |
| 50 | - }  | 
                                                        |
| 51 | -  | 
                                                        |
| 52 | - /**  | 
                                                        |
| 53 | - * returns prefixes for each saved LDAP/AD server configuration.  | 
                                                        |
| 54 | - * @param bool $activeConfigurations optional, whether only active configuration shall be  | 
                                                        |
| 55 | - * retrieved, defaults to false  | 
                                                        |
| 56 | - * @return array with a list of the available prefixes  | 
                                                        |
| 57 | - *  | 
                                                        |
| 58 | - * Configuration prefixes are used to set up configurations for n LDAP or  | 
                                                        |
| 59 | - * AD servers. Since configuration is stored in the database, table  | 
                                                        |
| 60 | - * appconfig under appid user_ldap, the common identifiers in column  | 
                                                        |
| 61 | - * 'configkey' have a prefix. The prefix for the very first server  | 
                                                        |
| 62 | - * configuration is empty.  | 
                                                        |
| 63 | - * Configkey Examples:  | 
                                                        |
| 64 | - * Server 1: ldap_login_filter  | 
                                                        |
| 65 | - * Server 2: s1_ldap_login_filter  | 
                                                        |
| 66 | - * Server 3: s2_ldap_login_filter  | 
                                                        |
| 67 | - *  | 
                                                        |
| 68 | - * The prefix needs to be passed to the constructor of Connection class,  | 
                                                        |
| 69 | - * except the default (first) server shall be connected to.  | 
                                                        |
| 70 | - *  | 
                                                        |
| 71 | - */  | 
                                                        |
| 72 | -	public function getServerConfigurationPrefixes($activeConfigurations = false) { | 
                                                        |
| 73 | - $referenceConfigkey = 'ldap_configuration_active';  | 
                                                        |
| 74 | -  | 
                                                        |
| 75 | - $keys = $this->getServersConfig($referenceConfigkey);  | 
                                                        |
| 76 | -  | 
                                                        |
| 77 | - $prefixes = [];  | 
                                                        |
| 78 | -		foreach ($keys as $key) { | 
                                                        |
| 79 | -			if ($activeConfigurations && $this->config->getAppValue('user_ldap', $key, '0') !== '1') { | 
                                                        |
| 80 | - continue;  | 
                                                        |
| 81 | - }  | 
                                                        |
| 82 | -  | 
                                                        |
| 83 | - $len = strlen($key) - strlen($referenceConfigkey);  | 
                                                        |
| 84 | - $prefixes[] = substr($key, 0, $len);  | 
                                                        |
| 85 | - }  | 
                                                        |
| 86 | -  | 
                                                        |
| 87 | - return $prefixes;  | 
                                                        |
| 88 | - }  | 
                                                        |
| 89 | -  | 
                                                        |
| 90 | - /**  | 
                                                        |
| 91 | - *  | 
                                                        |
| 92 | - * determines the host for every configured connection  | 
                                                        |
| 93 | - * @return array an array with configprefix as keys  | 
                                                        |
| 94 | - *  | 
                                                        |
| 95 | - */  | 
                                                        |
| 96 | -	public function getServerConfigurationHosts() { | 
                                                        |
| 97 | - $referenceConfigkey = 'ldap_host';  | 
                                                        |
| 98 | -  | 
                                                        |
| 99 | - $keys = $this->getServersConfig($referenceConfigkey);  | 
                                                        |
| 100 | -  | 
                                                        |
| 101 | - $result = array();  | 
                                                        |
| 102 | -		foreach($keys as $key) { | 
                                                        |
| 103 | - $len = strlen($key) - strlen($referenceConfigkey);  | 
                                                        |
| 104 | - $prefix = substr($key, 0, $len);  | 
                                                        |
| 105 | -			$result[$prefix] = $this->config->getAppValue('user_ldap', $key); | 
                                                        |
| 106 | - }  | 
                                                        |
| 107 | -  | 
                                                        |
| 108 | - return $result;  | 
                                                        |
| 109 | - }  | 
                                                        |
| 110 | -  | 
                                                        |
| 111 | - /**  | 
                                                        |
| 112 | - * return the next available configuration prefix  | 
                                                        |
| 113 | - *  | 
                                                        |
| 114 | - * @return string  | 
                                                        |
| 115 | - */  | 
                                                        |
| 116 | -	public function getNextServerConfigurationPrefix() { | 
                                                        |
| 117 | - $serverConnections = $this->getServerConfigurationPrefixes();  | 
                                                        |
| 118 | -  | 
                                                        |
| 119 | -		if(count($serverConnections) === 0) { | 
                                                        |
| 120 | - return 's01';  | 
                                                        |
| 121 | - }  | 
                                                        |
| 122 | -  | 
                                                        |
| 123 | - sort($serverConnections);  | 
                                                        |
| 124 | - $lastKey = array_pop($serverConnections);  | 
                                                        |
| 125 | -		$lastNumber = intval(str_replace('s', '', $lastKey)); | 
                                                        |
| 126 | - $nextPrefix = 's' . str_pad($lastNumber + 1, 2, '0', STR_PAD_LEFT);  | 
                                                        |
| 127 | - return $nextPrefix;  | 
                                                        |
| 128 | - }  | 
                                                        |
| 129 | -  | 
                                                        |
| 130 | -	private function getServersConfig($value) { | 
                                                        |
| 131 | - $regex = '/' . $value . '$/S';  | 
                                                        |
| 132 | -  | 
                                                        |
| 133 | -		$keys = $this->config->getAppKeys('user_ldap'); | 
                                                        |
| 134 | - $result = [];  | 
                                                        |
| 135 | -		foreach ($keys as $key) { | 
                                                        |
| 136 | -			if (preg_match($regex, $key) === 1) { | 
                                                        |
| 137 | - $result[] = $key;  | 
                                                        |
| 138 | - }  | 
                                                        |
| 139 | - }  | 
                                                        |
| 140 | -  | 
                                                        |
| 141 | - return $result;  | 
                                                        |
| 142 | - }  | 
                                                        |
| 143 | -  | 
                                                        |
| 144 | - /**  | 
                                                        |
| 145 | - * deletes a given saved LDAP/AD server configuration.  | 
                                                        |
| 146 | - * @param string $prefix the configuration prefix of the config to delete  | 
                                                        |
| 147 | - * @return bool true on success, false otherwise  | 
                                                        |
| 148 | - */  | 
                                                        |
| 149 | -	public function deleteServerConfiguration($prefix) { | 
                                                        |
| 150 | -		if(!in_array($prefix, self::getServerConfigurationPrefixes())) { | 
                                                        |
| 151 | - return false;  | 
                                                        |
| 152 | - }  | 
                                                        |
| 153 | -  | 
                                                        |
| 154 | - $saveOtherConfigurations = '';  | 
                                                        |
| 155 | -		if(empty($prefix)) { | 
                                                        |
| 156 | - $saveOtherConfigurations = 'AND `configkey` NOT LIKE \'s%\'';  | 
                                                        |
| 157 | - }  | 
                                                        |
| 158 | -  | 
                                                        |
| 159 | -		$query = \OCP\DB::prepare(' | 
                                                        |
| 40 | + /** @var IConfig */  | 
                                                        |
| 41 | + private $config;  | 
                                                        |
| 42 | +  | 
                                                        |
| 43 | + /**  | 
                                                        |
| 44 | + * Helper constructor.  | 
                                                        |
| 45 | + *  | 
                                                        |
| 46 | + * @param IConfig $config  | 
                                                        |
| 47 | + */  | 
                                                        |
| 48 | +    public function __construct(IConfig $config) { | 
                                                        |
| 49 | + $this->config = $config;  | 
                                                        |
| 50 | + }  | 
                                                        |
| 51 | +  | 
                                                        |
| 52 | + /**  | 
                                                        |
| 53 | + * returns prefixes for each saved LDAP/AD server configuration.  | 
                                                        |
| 54 | + * @param bool $activeConfigurations optional, whether only active configuration shall be  | 
                                                        |
| 55 | + * retrieved, defaults to false  | 
                                                        |
| 56 | + * @return array with a list of the available prefixes  | 
                                                        |
| 57 | + *  | 
                                                        |
| 58 | + * Configuration prefixes are used to set up configurations for n LDAP or  | 
                                                        |
| 59 | + * AD servers. Since configuration is stored in the database, table  | 
                                                        |
| 60 | + * appconfig under appid user_ldap, the common identifiers in column  | 
                                                        |
| 61 | + * 'configkey' have a prefix. The prefix for the very first server  | 
                                                        |
| 62 | + * configuration is empty.  | 
                                                        |
| 63 | + * Configkey Examples:  | 
                                                        |
| 64 | + * Server 1: ldap_login_filter  | 
                                                        |
| 65 | + * Server 2: s1_ldap_login_filter  | 
                                                        |
| 66 | + * Server 3: s2_ldap_login_filter  | 
                                                        |
| 67 | + *  | 
                                                        |
| 68 | + * The prefix needs to be passed to the constructor of Connection class,  | 
                                                        |
| 69 | + * except the default (first) server shall be connected to.  | 
                                                        |
| 70 | + *  | 
                                                        |
| 71 | + */  | 
                                                        |
| 72 | +    public function getServerConfigurationPrefixes($activeConfigurations = false) { | 
                                                        |
| 73 | + $referenceConfigkey = 'ldap_configuration_active';  | 
                                                        |
| 74 | +  | 
                                                        |
| 75 | + $keys = $this->getServersConfig($referenceConfigkey);  | 
                                                        |
| 76 | +  | 
                                                        |
| 77 | + $prefixes = [];  | 
                                                        |
| 78 | +        foreach ($keys as $key) { | 
                                                        |
| 79 | +            if ($activeConfigurations && $this->config->getAppValue('user_ldap', $key, '0') !== '1') { | 
                                                        |
| 80 | + continue;  | 
                                                        |
| 81 | + }  | 
                                                        |
| 82 | +  | 
                                                        |
| 83 | + $len = strlen($key) - strlen($referenceConfigkey);  | 
                                                        |
| 84 | + $prefixes[] = substr($key, 0, $len);  | 
                                                        |
| 85 | + }  | 
                                                        |
| 86 | +  | 
                                                        |
| 87 | + return $prefixes;  | 
                                                        |
| 88 | + }  | 
                                                        |
| 89 | +  | 
                                                        |
| 90 | + /**  | 
                                                        |
| 91 | + *  | 
                                                        |
| 92 | + * determines the host for every configured connection  | 
                                                        |
| 93 | + * @return array an array with configprefix as keys  | 
                                                        |
| 94 | + *  | 
                                                        |
| 95 | + */  | 
                                                        |
| 96 | +    public function getServerConfigurationHosts() { | 
                                                        |
| 97 | + $referenceConfigkey = 'ldap_host';  | 
                                                        |
| 98 | +  | 
                                                        |
| 99 | + $keys = $this->getServersConfig($referenceConfigkey);  | 
                                                        |
| 100 | +  | 
                                                        |
| 101 | + $result = array();  | 
                                                        |
| 102 | +        foreach($keys as $key) { | 
                                                        |
| 103 | + $len = strlen($key) - strlen($referenceConfigkey);  | 
                                                        |
| 104 | + $prefix = substr($key, 0, $len);  | 
                                                        |
| 105 | +            $result[$prefix] = $this->config->getAppValue('user_ldap', $key); | 
                                                        |
| 106 | + }  | 
                                                        |
| 107 | +  | 
                                                        |
| 108 | + return $result;  | 
                                                        |
| 109 | + }  | 
                                                        |
| 110 | +  | 
                                                        |
| 111 | + /**  | 
                                                        |
| 112 | + * return the next available configuration prefix  | 
                                                        |
| 113 | + *  | 
                                                        |
| 114 | + * @return string  | 
                                                        |
| 115 | + */  | 
                                                        |
| 116 | +    public function getNextServerConfigurationPrefix() { | 
                                                        |
| 117 | + $serverConnections = $this->getServerConfigurationPrefixes();  | 
                                                        |
| 118 | +  | 
                                                        |
| 119 | +        if(count($serverConnections) === 0) { | 
                                                        |
| 120 | + return 's01';  | 
                                                        |
| 121 | + }  | 
                                                        |
| 122 | +  | 
                                                        |
| 123 | + sort($serverConnections);  | 
                                                        |
| 124 | + $lastKey = array_pop($serverConnections);  | 
                                                        |
| 125 | +        $lastNumber = intval(str_replace('s', '', $lastKey)); | 
                                                        |
| 126 | + $nextPrefix = 's' . str_pad($lastNumber + 1, 2, '0', STR_PAD_LEFT);  | 
                                                        |
| 127 | + return $nextPrefix;  | 
                                                        |
| 128 | + }  | 
                                                        |
| 129 | +  | 
                                                        |
| 130 | +    private function getServersConfig($value) { | 
                                                        |
| 131 | + $regex = '/' . $value . '$/S';  | 
                                                        |
| 132 | +  | 
                                                        |
| 133 | +        $keys = $this->config->getAppKeys('user_ldap'); | 
                                                        |
| 134 | + $result = [];  | 
                                                        |
| 135 | +        foreach ($keys as $key) { | 
                                                        |
| 136 | +            if (preg_match($regex, $key) === 1) { | 
                                                        |
| 137 | + $result[] = $key;  | 
                                                        |
| 138 | + }  | 
                                                        |
| 139 | + }  | 
                                                        |
| 140 | +  | 
                                                        |
| 141 | + return $result;  | 
                                                        |
| 142 | + }  | 
                                                        |
| 143 | +  | 
                                                        |
| 144 | + /**  | 
                                                        |
| 145 | + * deletes a given saved LDAP/AD server configuration.  | 
                                                        |
| 146 | + * @param string $prefix the configuration prefix of the config to delete  | 
                                                        |
| 147 | + * @return bool true on success, false otherwise  | 
                                                        |
| 148 | + */  | 
                                                        |
| 149 | +    public function deleteServerConfiguration($prefix) { | 
                                                        |
| 150 | +        if(!in_array($prefix, self::getServerConfigurationPrefixes())) { | 
                                                        |
| 151 | + return false;  | 
                                                        |
| 152 | + }  | 
                                                        |
| 153 | +  | 
                                                        |
| 154 | + $saveOtherConfigurations = '';  | 
                                                        |
| 155 | +        if(empty($prefix)) { | 
                                                        |
| 156 | + $saveOtherConfigurations = 'AND `configkey` NOT LIKE \'s%\'';  | 
                                                        |
| 157 | + }  | 
                                                        |
| 158 | +  | 
                                                        |
| 159 | +        $query = \OCP\DB::prepare(' | 
                                                        |
| 160 | 160 | DELETE  | 
                                                        
| 161 | 161 | FROM `*PREFIX*appconfig`  | 
                                                        
| 162 | 162 | WHERE `configkey` LIKE ?  | 
                                                        
@@ -164,149 +164,149 @@ discard block  | 
                                                    ||
| 164 | 164 | AND `appid` = \'user_ldap\'  | 
                                                        
| 165 | 165 | AND `configkey` NOT IN (\'enabled\', \'installed_version\', \'types\', \'bgjUpdateGroupsLastRun\')  | 
                                                        
| 166 | 166 | ');  | 
                                                        
| 167 | - $delRows = $query->execute(array($prefix.'%'));  | 
                                                        |
| 168 | -  | 
                                                        |
| 169 | -		if(\OCP\DB::isError($delRows)) { | 
                                                        |
| 170 | - return false;  | 
                                                        |
| 171 | - }  | 
                                                        |
| 172 | -  | 
                                                        |
| 173 | -		if($delRows === 0) { | 
                                                        |
| 174 | - return false;  | 
                                                        |
| 175 | - }  | 
                                                        |
| 176 | -  | 
                                                        |
| 177 | - return true;  | 
                                                        |
| 178 | - }  | 
                                                        |
| 179 | -  | 
                                                        |
| 180 | - /**  | 
                                                        |
| 181 | - * checks whether there is one or more disabled LDAP configurations  | 
                                                        |
| 182 | - * @throws \Exception  | 
                                                        |
| 183 | - * @return bool  | 
                                                        |
| 184 | - */  | 
                                                        |
| 185 | -	public function haveDisabledConfigurations() { | 
                                                        |
| 186 | - $all = $this->getServerConfigurationPrefixes(false);  | 
                                                        |
| 187 | - $active = $this->getServerConfigurationPrefixes(true);  | 
                                                        |
| 188 | -  | 
                                                        |
| 189 | -		if(!is_array($all) || !is_array($active)) { | 
                                                        |
| 190 | -			throw new \Exception('Unexpected Return Value'); | 
                                                        |
| 191 | - }  | 
                                                        |
| 192 | -  | 
                                                        |
| 193 | - return count($all) !== count($active) || count($all) === 0;  | 
                                                        |
| 194 | - }  | 
                                                        |
| 195 | -  | 
                                                        |
| 196 | - /**  | 
                                                        |
| 197 | - * extracts the domain from a given URL  | 
                                                        |
| 198 | - * @param string $url the URL  | 
                                                        |
| 199 | - * @return string|false domain as string on success, false otherwise  | 
                                                        |
| 200 | - */  | 
                                                        |
| 201 | -	public function getDomainFromURL($url) { | 
                                                        |
| 202 | - $uinfo = parse_url($url);  | 
                                                        |
| 203 | -		if(!is_array($uinfo)) { | 
                                                        |
| 204 | - return false;  | 
                                                        |
| 205 | - }  | 
                                                        |
| 206 | -  | 
                                                        |
| 207 | - $domain = false;  | 
                                                        |
| 208 | -		if(isset($uinfo['host'])) { | 
                                                        |
| 209 | - $domain = $uinfo['host'];  | 
                                                        |
| 210 | -		} else if(isset($uinfo['path'])) { | 
                                                        |
| 211 | - $domain = $uinfo['path'];  | 
                                                        |
| 212 | - }  | 
                                                        |
| 213 | -  | 
                                                        |
| 214 | - return $domain;  | 
                                                        |
| 215 | - }  | 
                                                        |
| 167 | + $delRows = $query->execute(array($prefix.'%'));  | 
                                                        |
| 168 | +  | 
                                                        |
| 169 | +        if(\OCP\DB::isError($delRows)) { | 
                                                        |
| 170 | + return false;  | 
                                                        |
| 171 | + }  | 
                                                        |
| 172 | +  | 
                                                        |
| 173 | +        if($delRows === 0) { | 
                                                        |
| 174 | + return false;  | 
                                                        |
| 175 | + }  | 
                                                        |
| 176 | +  | 
                                                        |
| 177 | + return true;  | 
                                                        |
| 178 | + }  | 
                                                        |
| 179 | +  | 
                                                        |
| 180 | + /**  | 
                                                        |
| 181 | + * checks whether there is one or more disabled LDAP configurations  | 
                                                        |
| 182 | + * @throws \Exception  | 
                                                        |
| 183 | + * @return bool  | 
                                                        |
| 184 | + */  | 
                                                        |
| 185 | +    public function haveDisabledConfigurations() { | 
                                                        |
| 186 | + $all = $this->getServerConfigurationPrefixes(false);  | 
                                                        |
| 187 | + $active = $this->getServerConfigurationPrefixes(true);  | 
                                                        |
| 188 | +  | 
                                                        |
| 189 | +        if(!is_array($all) || !is_array($active)) { | 
                                                        |
| 190 | +            throw new \Exception('Unexpected Return Value'); | 
                                                        |
| 191 | + }  | 
                                                        |
| 192 | +  | 
                                                        |
| 193 | + return count($all) !== count($active) || count($all) === 0;  | 
                                                        |
| 194 | + }  | 
                                                        |
| 195 | +  | 
                                                        |
| 196 | + /**  | 
                                                        |
| 197 | + * extracts the domain from a given URL  | 
                                                        |
| 198 | + * @param string $url the URL  | 
                                                        |
| 199 | + * @return string|false domain as string on success, false otherwise  | 
                                                        |
| 200 | + */  | 
                                                        |
| 201 | +    public function getDomainFromURL($url) { | 
                                                        |
| 202 | + $uinfo = parse_url($url);  | 
                                                        |
| 203 | +        if(!is_array($uinfo)) { | 
                                                        |
| 204 | + return false;  | 
                                                        |
| 205 | + }  | 
                                                        |
| 206 | +  | 
                                                        |
| 207 | + $domain = false;  | 
                                                        |
| 208 | +        if(isset($uinfo['host'])) { | 
                                                        |
| 209 | + $domain = $uinfo['host'];  | 
                                                        |
| 210 | +        } else if(isset($uinfo['path'])) { | 
                                                        |
| 211 | + $domain = $uinfo['path'];  | 
                                                        |
| 212 | + }  | 
                                                        |
| 213 | +  | 
                                                        |
| 214 | + return $domain;  | 
                                                        |
| 215 | + }  | 
                                                        |
| 216 | 216 | |
| 217 | - /**  | 
                                                        |
| 218 | - *  | 
                                                        |
| 219 | - * Set the LDAPProvider in the config  | 
                                                        |
| 220 | - *  | 
                                                        |
| 221 | - */  | 
                                                        |
| 222 | -	public function setLDAPProvider() { | 
                                                        |
| 223 | -		$current = \OC::$server->getConfig()->getSystemValue('ldapProviderFactory', null); | 
                                                        |
| 224 | -		if(is_null($current)) { | 
                                                        |
| 225 | -			\OC::$server->getConfig()->setSystemValue('ldapProviderFactory', '\\OCA\\User_LDAP\\LDAPProviderFactory'); | 
                                                        |
| 226 | - }  | 
                                                        |
| 227 | - }  | 
                                                        |
| 217 | + /**  | 
                                                        |
| 218 | + *  | 
                                                        |
| 219 | + * Set the LDAPProvider in the config  | 
                                                        |
| 220 | + *  | 
                                                        |
| 221 | + */  | 
                                                        |
| 222 | +    public function setLDAPProvider() { | 
                                                        |
| 223 | +        $current = \OC::$server->getConfig()->getSystemValue('ldapProviderFactory', null); | 
                                                        |
| 224 | +        if(is_null($current)) { | 
                                                        |
| 225 | +            \OC::$server->getConfig()->setSystemValue('ldapProviderFactory', '\\OCA\\User_LDAP\\LDAPProviderFactory'); | 
                                                        |
| 226 | + }  | 
                                                        |
| 227 | + }  | 
                                                        |
| 228 | 228 | |
| 229 | - /**  | 
                                                        |
| 230 | - * sanitizes a DN received from the LDAP server  | 
                                                        |
| 231 | - * @param array $dn the DN in question  | 
                                                        |
| 232 | - * @return array|string the sanitized DN  | 
                                                        |
| 233 | - */  | 
                                                        |
| 234 | -	public function sanitizeDN($dn) { | 
                                                        |
| 235 | - //treating multiple base DNs  | 
                                                        |
| 236 | -		if(is_array($dn)) { | 
                                                        |
| 237 | - $result = array();  | 
                                                        |
| 238 | -			foreach($dn as $singleDN) { | 
                                                        |
| 239 | - $result[] = $this->sanitizeDN($singleDN);  | 
                                                        |
| 240 | - }  | 
                                                        |
| 241 | - return $result;  | 
                                                        |
| 242 | - }  | 
                                                        |
| 243 | -  | 
                                                        |
| 244 | - //OID sometimes gives back DNs with whitespace after the comma  | 
                                                        |
| 245 | - // a la "uid=foo, cn=bar, dn=..." We need to tackle this!  | 
                                                        |
| 246 | -		$dn = preg_replace('/([^\\\]),(\s+)/u', '\1,', $dn); | 
                                                        |
| 247 | -  | 
                                                        |
| 248 | - //make comparisons and everything work  | 
                                                        |
| 249 | - $dn = mb_strtolower($dn, 'UTF-8');  | 
                                                        |
| 250 | -  | 
                                                        |
| 251 | - //escape DN values according to RFC 2253 – this is already done by ldap_explode_dn  | 
                                                        |
| 252 | - //to use the DN in search filters, \ needs to be escaped to \5c additionally  | 
                                                        |
| 253 | - //to use them in bases, we convert them back to simple backslashes in readAttribute()  | 
                                                        |
| 254 | - $replacements = array(  | 
                                                        |
| 255 | - '\,' => '\5c2C',  | 
                                                        |
| 256 | - '\=' => '\5c3D',  | 
                                                        |
| 257 | - '\+' => '\5c2B',  | 
                                                        |
| 258 | - '\<' => '\5c3C',  | 
                                                        |
| 259 | - '\>' => '\5c3E',  | 
                                                        |
| 260 | - '\;' => '\5c3B',  | 
                                                        |
| 261 | - '\"' => '\5c22',  | 
                                                        |
| 262 | - '\#' => '\5c23',  | 
                                                        |
| 263 | -			'('  => '\28', | 
                                                        |
| 264 | - ')' => '\29',  | 
                                                        |
| 265 | - '*' => '\2A',  | 
                                                        |
| 266 | - );  | 
                                                        |
| 267 | - $dn = str_replace(array_keys($replacements), array_values($replacements), $dn);  | 
                                                        |
| 268 | -  | 
                                                        |
| 269 | - return $dn;  | 
                                                        |
| 270 | - }  | 
                                                        |
| 229 | + /**  | 
                                                        |
| 230 | + * sanitizes a DN received from the LDAP server  | 
                                                        |
| 231 | + * @param array $dn the DN in question  | 
                                                        |
| 232 | + * @return array|string the sanitized DN  | 
                                                        |
| 233 | + */  | 
                                                        |
| 234 | +    public function sanitizeDN($dn) { | 
                                                        |
| 235 | + //treating multiple base DNs  | 
                                                        |
| 236 | +        if(is_array($dn)) { | 
                                                        |
| 237 | + $result = array();  | 
                                                        |
| 238 | +            foreach($dn as $singleDN) { | 
                                                        |
| 239 | + $result[] = $this->sanitizeDN($singleDN);  | 
                                                        |
| 240 | + }  | 
                                                        |
| 241 | + return $result;  | 
                                                        |
| 242 | + }  | 
                                                        |
| 243 | +  | 
                                                        |
| 244 | + //OID sometimes gives back DNs with whitespace after the comma  | 
                                                        |
| 245 | + // a la "uid=foo, cn=bar, dn=..." We need to tackle this!  | 
                                                        |
| 246 | +        $dn = preg_replace('/([^\\\]),(\s+)/u', '\1,', $dn); | 
                                                        |
| 247 | +  | 
                                                        |
| 248 | + //make comparisons and everything work  | 
                                                        |
| 249 | + $dn = mb_strtolower($dn, 'UTF-8');  | 
                                                        |
| 250 | +  | 
                                                        |
| 251 | + //escape DN values according to RFC 2253 – this is already done by ldap_explode_dn  | 
                                                        |
| 252 | + //to use the DN in search filters, \ needs to be escaped to \5c additionally  | 
                                                        |
| 253 | + //to use them in bases, we convert them back to simple backslashes in readAttribute()  | 
                                                        |
| 254 | + $replacements = array(  | 
                                                        |
| 255 | + '\,' => '\5c2C',  | 
                                                        |
| 256 | + '\=' => '\5c3D',  | 
                                                        |
| 257 | + '\+' => '\5c2B',  | 
                                                        |
| 258 | + '\<' => '\5c3C',  | 
                                                        |
| 259 | + '\>' => '\5c3E',  | 
                                                        |
| 260 | + '\;' => '\5c3B',  | 
                                                        |
| 261 | + '\"' => '\5c22',  | 
                                                        |
| 262 | + '\#' => '\5c23',  | 
                                                        |
| 263 | +            '('  => '\28', | 
                                                        |
| 264 | + ')' => '\29',  | 
                                                        |
| 265 | + '*' => '\2A',  | 
                                                        |
| 266 | + );  | 
                                                        |
| 267 | + $dn = str_replace(array_keys($replacements), array_values($replacements), $dn);  | 
                                                        |
| 268 | +  | 
                                                        |
| 269 | + return $dn;  | 
                                                        |
| 270 | + }  | 
                                                        |
| 271 | 271 | |
| 272 | - /**  | 
                                                        |
| 273 | - * converts a stored DN so it can be used as base parameter for LDAP queries, internally we store them for usage in LDAP filters  | 
                                                        |
| 274 | - * @param string $dn the DN  | 
                                                        |
| 275 | - * @return string  | 
                                                        |
| 276 | - */  | 
                                                        |
| 277 | -	public function DNasBaseParameter($dn) { | 
                                                        |
| 278 | -		return str_ireplace('\\5c', '\\', $dn); | 
                                                        |
| 279 | - }  | 
                                                        |
| 280 | -  | 
                                                        |
| 281 | - /**  | 
                                                        |
| 282 | - * listens to a hook thrown by server2server sharing and replaces the given  | 
                                                        |
| 283 | - * login name by a username, if it matches an LDAP user.  | 
                                                        |
| 284 | - *  | 
                                                        |
| 285 | - * @param array $param  | 
                                                        |
| 286 | - * @throws \Exception  | 
                                                        |
| 287 | - */  | 
                                                        |
| 288 | -	public static function loginName2UserName($param) { | 
                                                        |
| 289 | -		if(!isset($param['uid'])) { | 
                                                        |
| 290 | -			throw new \Exception('key uid is expected to be set in $param'); | 
                                                        |
| 291 | - }  | 
                                                        |
| 292 | -  | 
                                                        |
| 293 | - //ain't it ironic?  | 
                                                        |
| 294 | - $helper = new Helper(\OC::$server->getConfig());  | 
                                                        |
| 295 | -  | 
                                                        |
| 296 | - $configPrefixes = $helper->getServerConfigurationPrefixes(true);  | 
                                                        |
| 297 | - $ldapWrapper = new LDAP();  | 
                                                        |
| 298 | - $ocConfig = \OC::$server->getConfig();  | 
                                                        |
| 299 | - $notificationManager = \OC::$server->getNotificationManager();  | 
                                                        |
| 300 | -  | 
                                                        |
| 301 | - $userSession = \OC::$server->getUserSession();  | 
                                                        |
| 302 | -		$userPluginManager = \OC::$server->query('LDAPUserPluginManager'); | 
                                                        |
| 303 | -  | 
                                                        |
| 304 | - $userBackend = new User_Proxy(  | 
                                                        |
| 305 | - $configPrefixes, $ldapWrapper, $ocConfig, $notificationManager, $userSession, $userPluginManager  | 
                                                        |
| 306 | - );  | 
                                                        |
| 307 | - $uid = $userBackend->loginName2UserName($param['uid'] );  | 
                                                        |
| 308 | -		if($uid !== false) { | 
                                                        |
| 309 | - $param['uid'] = $uid;  | 
                                                        |
| 310 | - }  | 
                                                        |
| 311 | - }  | 
                                                        |
| 272 | + /**  | 
                                                        |
| 273 | + * converts a stored DN so it can be used as base parameter for LDAP queries, internally we store them for usage in LDAP filters  | 
                                                        |
| 274 | + * @param string $dn the DN  | 
                                                        |
| 275 | + * @return string  | 
                                                        |
| 276 | + */  | 
                                                        |
| 277 | +    public function DNasBaseParameter($dn) { | 
                                                        |
| 278 | +        return str_ireplace('\\5c', '\\', $dn); | 
                                                        |
| 279 | + }  | 
                                                        |
| 280 | +  | 
                                                        |
| 281 | + /**  | 
                                                        |
| 282 | + * listens to a hook thrown by server2server sharing and replaces the given  | 
                                                        |
| 283 | + * login name by a username, if it matches an LDAP user.  | 
                                                        |
| 284 | + *  | 
                                                        |
| 285 | + * @param array $param  | 
                                                        |
| 286 | + * @throws \Exception  | 
                                                        |
| 287 | + */  | 
                                                        |
| 288 | +    public static function loginName2UserName($param) { | 
                                                        |
| 289 | +        if(!isset($param['uid'])) { | 
                                                        |
| 290 | +            throw new \Exception('key uid is expected to be set in $param'); | 
                                                        |
| 291 | + }  | 
                                                        |
| 292 | +  | 
                                                        |
| 293 | + //ain't it ironic?  | 
                                                        |
| 294 | + $helper = new Helper(\OC::$server->getConfig());  | 
                                                        |
| 295 | +  | 
                                                        |
| 296 | + $configPrefixes = $helper->getServerConfigurationPrefixes(true);  | 
                                                        |
| 297 | + $ldapWrapper = new LDAP();  | 
                                                        |
| 298 | + $ocConfig = \OC::$server->getConfig();  | 
                                                        |
| 299 | + $notificationManager = \OC::$server->getNotificationManager();  | 
                                                        |
| 300 | +  | 
                                                        |
| 301 | + $userSession = \OC::$server->getUserSession();  | 
                                                        |
| 302 | +        $userPluginManager = \OC::$server->query('LDAPUserPluginManager'); | 
                                                        |
| 303 | +  | 
                                                        |
| 304 | + $userBackend = new User_Proxy(  | 
                                                        |
| 305 | + $configPrefixes, $ldapWrapper, $ocConfig, $notificationManager, $userSession, $userPluginManager  | 
                                                        |
| 306 | + );  | 
                                                        |
| 307 | + $uid = $userBackend->loginName2UserName($param['uid'] );  | 
                                                        |
| 308 | +        if($uid !== false) { | 
                                                        |
| 309 | + $param['uid'] = $uid;  | 
                                                        |
| 310 | + }  | 
                                                        |
| 311 | + }  | 
                                                        |
| 312 | 312 | }  |