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