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