Passed
Push — master ( 2ce0fe...eb92c5 )
by Julius
17:52 queued 13s
created
apps/files/lib/Controller/ViewController.php 1 patch
Indentation   +358 added lines, -358 removed lines patch added patch discarded remove patch
@@ -64,362 +64,362 @@
 block discarded – undo
64 64
  * @package OCA\Files\Controller
65 65
  */
66 66
 class ViewController extends Controller {
67
-	/** @var string */
68
-	protected $appName;
69
-	/** @var IRequest */
70
-	protected $request;
71
-	/** @var IURLGenerator */
72
-	protected $urlGenerator;
73
-	/** @var IL10N */
74
-	protected $l10n;
75
-	/** @var IConfig */
76
-	protected $config;
77
-	/** @var IEventDispatcher */
78
-	protected $eventDispatcher;
79
-	/** @var IUserSession */
80
-	protected $userSession;
81
-	/** @var IAppManager */
82
-	protected $appManager;
83
-	/** @var IRootFolder */
84
-	protected $rootFolder;
85
-	/** @var Helper */
86
-	protected $activityHelper;
87
-	/** @var IInitialState */
88
-	private $initialState;
89
-	/** @var ITemplateManager */
90
-	private $templateManager;
91
-	/** @var IManager */
92
-	private $shareManager;
93
-
94
-	public function __construct(string $appName,
95
-		IRequest $request,
96
-		IURLGenerator $urlGenerator,
97
-		IL10N $l10n,
98
-		IConfig $config,
99
-		IEventDispatcher $eventDispatcher,
100
-		IUserSession $userSession,
101
-		IAppManager $appManager,
102
-		IRootFolder $rootFolder,
103
-		Helper $activityHelper,
104
-		IInitialState $initialState,
105
-		ITemplateManager $templateManager,
106
-		IManager $shareManager
107
-	) {
108
-		parent::__construct($appName, $request);
109
-		$this->appName = $appName;
110
-		$this->request = $request;
111
-		$this->urlGenerator = $urlGenerator;
112
-		$this->l10n = $l10n;
113
-		$this->config = $config;
114
-		$this->eventDispatcher = $eventDispatcher;
115
-		$this->userSession = $userSession;
116
-		$this->appManager = $appManager;
117
-		$this->rootFolder = $rootFolder;
118
-		$this->activityHelper = $activityHelper;
119
-		$this->initialState = $initialState;
120
-		$this->templateManager = $templateManager;
121
-		$this->shareManager = $shareManager;
122
-	}
123
-
124
-	/**
125
-	 * @param string $appName
126
-	 * @param string $scriptName
127
-	 * @return string
128
-	 */
129
-	protected function renderScript($appName, $scriptName) {
130
-		$content = '';
131
-		$appPath = \OC_App::getAppPath($appName);
132
-		$scriptPath = $appPath . '/' . $scriptName;
133
-		if (file_exists($scriptPath)) {
134
-			// TODO: sanitize path / script name ?
135
-			ob_start();
136
-			include $scriptPath;
137
-			$content = ob_get_contents();
138
-			@ob_end_clean();
139
-		}
140
-
141
-		return $content;
142
-	}
143
-
144
-	/**
145
-	 * FIXME: Replace with non static code
146
-	 *
147
-	 * @return array
148
-	 * @throws \OCP\Files\NotFoundException
149
-	 */
150
-	protected function getStorageInfo() {
151
-		\OC_Util::setupFS();
152
-		$dirInfo = \OC\Files\Filesystem::getFileInfo('/', false);
153
-
154
-		return \OC_Helper::getStorageInfo('/', $dirInfo);
155
-	}
156
-
157
-	/**
158
-	 * @NoCSRFRequired
159
-	 * @NoAdminRequired
160
-	 *
161
-	 * @param string $fileid
162
-	 * @return TemplateResponse|RedirectResponse
163
-	 * @throws NotFoundException
164
-	 */
165
-	public function showFile(string $fileid = null): Response {
166
-		// This is the entry point from the `/f/{fileid}` URL which is hardcoded in the server.
167
-		try {
168
-			return $this->redirectToFile($fileid);
169
-		} catch (NotFoundException $e) {
170
-			return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true]));
171
-		}
172
-	}
173
-
174
-	/**
175
-	 * @NoCSRFRequired
176
-	 * @NoAdminRequired
177
-	 *
178
-	 * @param string $dir
179
-	 * @param string $view
180
-	 * @param string $fileid
181
-	 * @param bool $fileNotFound
182
-	 * @param string $openfile
183
-	 * @return TemplateResponse|RedirectResponse
184
-	 * @throws NotFoundException
185
-	 */
186
-	public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false, $openfile = null) {
187
-		if ($fileid !== null) {
188
-			try {
189
-				return $this->redirectToFile($fileid);
190
-			} catch (NotFoundException $e) {
191
-				return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true]));
192
-			}
193
-		}
194
-
195
-		$nav = new \OCP\Template('files', 'appnavigation', '');
196
-
197
-		// Load the files we need
198
-		\OCP\Util::addStyle('files', 'merged');
199
-		\OCP\Util::addScript('files', 'merged-index');
200
-		\OCP\Util::addScript('files', 'dist/templates');
201
-
202
-		// mostly for the home storage's free space
203
-		// FIXME: Make non static
204
-		$storageInfo = $this->getStorageInfo();
205
-
206
-		$user = $this->userSession->getUser()->getUID();
207
-
208
-		// Get all the user favorites to create a submenu
209
-		try {
210
-			$favElements = $this->activityHelper->getFavoriteFilePaths($this->userSession->getUser()->getUID());
211
-		} catch (\RuntimeException $e) {
212
-			$favElements['folders'] = [];
213
-		}
214
-
215
-		$collapseClasses = '';
216
-		if (count($favElements['folders']) > 0) {
217
-			$collapseClasses = 'collapsible';
218
-		}
219
-
220
-		$favoritesSublistArray = [];
221
-
222
-		$navBarPositionPosition = 6;
223
-		$currentCount = 0;
224
-		foreach ($favElements['folders'] as $dir) {
225
-			$link = $this->urlGenerator->linkToRoute('files.view.index', ['dir' => $dir, 'view' => 'files']);
226
-			$sortingValue = ++$currentCount;
227
-			$element = [
228
-				'id' => str_replace('/', '-', $dir),
229
-				'view' => 'files',
230
-				'href' => $link,
231
-				'dir' => $dir,
232
-				'order' => $navBarPositionPosition,
233
-				'folderPosition' => $sortingValue,
234
-				'name' => basename($dir),
235
-				'icon' => 'files',
236
-				'quickaccesselement' => 'true'
237
-			];
238
-
239
-			array_push($favoritesSublistArray, $element);
240
-			$navBarPositionPosition++;
241
-		}
242
-
243
-		$navItems = \OCA\Files\App::getNavigationManager()->getAll();
244
-
245
-		// add the favorites entry in menu
246
-		$navItems['favorites']['sublist'] = $favoritesSublistArray;
247
-		$navItems['favorites']['classes'] = $collapseClasses;
248
-
249
-		// parse every menu and add the expandedState user value
250
-		foreach ($navItems as $key => $item) {
251
-			if (isset($item['expandedState'])) {
252
-				$navItems[$key]['defaultExpandedState'] = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', $item['expandedState'], '0') === '1';
253
-			}
254
-		}
255
-
256
-		$nav->assign('navigationItems', $navItems);
257
-
258
-		$nav->assign('usage', \OC_Helper::humanFileSize($storageInfo['used']));
259
-		if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) {
260
-			$totalSpace = $this->l10n->t('Unlimited');
261
-		} else {
262
-			$totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
263
-		}
264
-		$nav->assign('total_space', $totalSpace);
265
-		$nav->assign('quota', $storageInfo['quota']);
266
-		$nav->assign('usage_relative', $storageInfo['relative']);
267
-
268
-		$nav->assign('webdav_url', \OCP\Util::linkToRemote('dav/files/' . $user));
269
-
270
-		$contentItems = [];
271
-
272
-		// render the container content for every navigation item
273
-		foreach ($navItems as $item) {
274
-			$content = '';
275
-			if (isset($item['script'])) {
276
-				$content = $this->renderScript($item['appname'], $item['script']);
277
-			}
278
-			// parse submenus
279
-			if (isset($item['sublist'])) {
280
-				foreach ($item['sublist'] as $subitem) {
281
-					$subcontent = '';
282
-					if (isset($subitem['script'])) {
283
-						$subcontent = $this->renderScript($subitem['appname'], $subitem['script']);
284
-					}
285
-					$contentItems[$subitem['id']] = [
286
-						'id' => $subitem['id'],
287
-						'content' => $subcontent
288
-					];
289
-				}
290
-			}
291
-			$contentItems[$item['id']] = [
292
-				'id' => $item['id'],
293
-				'content' => $content
294
-			];
295
-		}
296
-
297
-		$event = new LoadAdditionalScriptsEvent();
298
-		$this->eventDispatcher->dispatchTyped($event);
299
-		$this->eventDispatcher->dispatchTyped(new LoadSidebar());
300
-		// Load Viewer scripts
301
-		if (class_exists(LoadViewer::class)) {
302
-			$this->eventDispatcher->dispatchTyped(new LoadViewer());
303
-		}
304
-		$this->initialState->provideInitialState('templates_path', $this->templateManager->hasTemplateDirectory() ? $this->templateManager->getTemplatePath() : false);
305
-		$this->initialState->provideInitialState('templates', $this->templateManager->listCreators());
306
-
307
-		$params = [];
308
-		$params['usedSpacePercent'] = (int) $storageInfo['relative'];
309
-		$params['owner'] = $storageInfo['owner'] ?? '';
310
-		$params['ownerDisplayName'] = $storageInfo['ownerDisplayName'] ?? '';
311
-		$params['isPublic'] = false;
312
-		$params['allowShareWithLink'] = $this->shareManager->shareApiAllowLinks() ? 'yes' : 'no';
313
-		$params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name');
314
-		$params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc');
315
-		$params['showgridview'] = $this->config->getUserValue($user, 'files', 'show_grid', false);
316
-		$params['isIE'] = \OC_Util::isIe();
317
-		$showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false);
318
-		$params['showHiddenFiles'] = $showHidden ? 1 : 0;
319
-		$cropImagePreviews = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'crop_image_previews', true);
320
-		$params['cropImagePreviews'] = $cropImagePreviews ? 1 : 0;
321
-		$params['fileNotFound'] = $fileNotFound ? 1 : 0;
322
-		$params['appNavigation'] = $nav;
323
-		$params['appContents'] = $contentItems;
324
-		$params['hiddenFields'] = $event->getHiddenFields();
325
-
326
-		$response = new TemplateResponse(
327
-			$this->appName,
328
-			'index',
329
-			$params
330
-		);
331
-		$policy = new ContentSecurityPolicy();
332
-		$policy->addAllowedFrameDomain('\'self\'');
333
-		$response->setContentSecurityPolicy($policy);
334
-
335
-		$this->provideInitialState($dir, $openfile);
336
-
337
-		return $response;
338
-	}
339
-
340
-	/**
341
-	 * Add openFileInfo in initialState if $openfile is set.
342
-	 * @param string $dir - the ?dir= URL param
343
-	 * @param string $openfile - the ?openfile= URL param
344
-	 * @return void
345
-	 */
346
-	private function provideInitialState(string $dir, ?string $openfile): void {
347
-		if ($openfile === null) {
348
-			return;
349
-		}
350
-
351
-		$user = $this->userSession->getUser();
352
-
353
-		if ($user === null) {
354
-			return;
355
-		}
356
-
357
-		$uid = $user->getUID();
358
-		$userFolder = $this->rootFolder->getUserFolder($uid);
359
-		$nodes = $userFolder->getById((int) $openfile);
360
-		$node = array_shift($nodes);
361
-
362
-		if ($node === null) {
363
-			return;
364
-		}
365
-
366
-		// properly format full path and make sure
367
-		// we're relative to the user home folder
368
-		$isRoot = $node === $userFolder;
369
-		$path = $userFolder->getRelativePath($node->getPath());
370
-		$directory = $userFolder->getRelativePath($node->getParent()->getPath());
371
-
372
-		// Prevent opening a file from another folder.
373
-		if ($dir !== $directory) {
374
-			return;
375
-		}
376
-
377
-		$this->initialState->provideInitialState(
378
-			'openFileInfo', [
379
-				'id' => $node->getId(),
380
-				'name' => $isRoot ? '' : $node->getName(),
381
-				'path' => $path,
382
-				'directory' => $directory,
383
-				'mime' => $node->getMimetype(),
384
-				'type' => $node->getType(),
385
-				'permissions' => $node->getPermissions(),
386
-			]
387
-		);
388
-	}
389
-
390
-	/**
391
-	 * Redirects to the file list and highlight the given file id
392
-	 *
393
-	 * @param string $fileId file id to show
394
-	 * @return RedirectResponse redirect response or not found response
395
-	 * @throws \OCP\Files\NotFoundException
396
-	 */
397
-	private function redirectToFile($fileId) {
398
-		$uid = $this->userSession->getUser()->getUID();
399
-		$baseFolder = $this->rootFolder->getUserFolder($uid);
400
-		$files = $baseFolder->getById($fileId);
401
-		$params = [];
402
-
403
-		if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) {
404
-			$baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/');
405
-			$files = $baseFolder->getById($fileId);
406
-			$params['view'] = 'trashbin';
407
-		}
408
-
409
-		if (!empty($files)) {
410
-			$file = current($files);
411
-			if ($file instanceof Folder) {
412
-				// set the full path to enter the folder
413
-				$params['dir'] = $baseFolder->getRelativePath($file->getPath());
414
-			} else {
415
-				// set parent path as dir
416
-				$params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath());
417
-				// and scroll to the entry
418
-				$params['scrollto'] = $file->getName();
419
-			}
420
-
421
-			return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params));
422
-		}
423
-		throw new \OCP\Files\NotFoundException();
424
-	}
67
+    /** @var string */
68
+    protected $appName;
69
+    /** @var IRequest */
70
+    protected $request;
71
+    /** @var IURLGenerator */
72
+    protected $urlGenerator;
73
+    /** @var IL10N */
74
+    protected $l10n;
75
+    /** @var IConfig */
76
+    protected $config;
77
+    /** @var IEventDispatcher */
78
+    protected $eventDispatcher;
79
+    /** @var IUserSession */
80
+    protected $userSession;
81
+    /** @var IAppManager */
82
+    protected $appManager;
83
+    /** @var IRootFolder */
84
+    protected $rootFolder;
85
+    /** @var Helper */
86
+    protected $activityHelper;
87
+    /** @var IInitialState */
88
+    private $initialState;
89
+    /** @var ITemplateManager */
90
+    private $templateManager;
91
+    /** @var IManager */
92
+    private $shareManager;
93
+
94
+    public function __construct(string $appName,
95
+        IRequest $request,
96
+        IURLGenerator $urlGenerator,
97
+        IL10N $l10n,
98
+        IConfig $config,
99
+        IEventDispatcher $eventDispatcher,
100
+        IUserSession $userSession,
101
+        IAppManager $appManager,
102
+        IRootFolder $rootFolder,
103
+        Helper $activityHelper,
104
+        IInitialState $initialState,
105
+        ITemplateManager $templateManager,
106
+        IManager $shareManager
107
+    ) {
108
+        parent::__construct($appName, $request);
109
+        $this->appName = $appName;
110
+        $this->request = $request;
111
+        $this->urlGenerator = $urlGenerator;
112
+        $this->l10n = $l10n;
113
+        $this->config = $config;
114
+        $this->eventDispatcher = $eventDispatcher;
115
+        $this->userSession = $userSession;
116
+        $this->appManager = $appManager;
117
+        $this->rootFolder = $rootFolder;
118
+        $this->activityHelper = $activityHelper;
119
+        $this->initialState = $initialState;
120
+        $this->templateManager = $templateManager;
121
+        $this->shareManager = $shareManager;
122
+    }
123
+
124
+    /**
125
+     * @param string $appName
126
+     * @param string $scriptName
127
+     * @return string
128
+     */
129
+    protected function renderScript($appName, $scriptName) {
130
+        $content = '';
131
+        $appPath = \OC_App::getAppPath($appName);
132
+        $scriptPath = $appPath . '/' . $scriptName;
133
+        if (file_exists($scriptPath)) {
134
+            // TODO: sanitize path / script name ?
135
+            ob_start();
136
+            include $scriptPath;
137
+            $content = ob_get_contents();
138
+            @ob_end_clean();
139
+        }
140
+
141
+        return $content;
142
+    }
143
+
144
+    /**
145
+     * FIXME: Replace with non static code
146
+     *
147
+     * @return array
148
+     * @throws \OCP\Files\NotFoundException
149
+     */
150
+    protected function getStorageInfo() {
151
+        \OC_Util::setupFS();
152
+        $dirInfo = \OC\Files\Filesystem::getFileInfo('/', false);
153
+
154
+        return \OC_Helper::getStorageInfo('/', $dirInfo);
155
+    }
156
+
157
+    /**
158
+     * @NoCSRFRequired
159
+     * @NoAdminRequired
160
+     *
161
+     * @param string $fileid
162
+     * @return TemplateResponse|RedirectResponse
163
+     * @throws NotFoundException
164
+     */
165
+    public function showFile(string $fileid = null): Response {
166
+        // This is the entry point from the `/f/{fileid}` URL which is hardcoded in the server.
167
+        try {
168
+            return $this->redirectToFile($fileid);
169
+        } catch (NotFoundException $e) {
170
+            return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true]));
171
+        }
172
+    }
173
+
174
+    /**
175
+     * @NoCSRFRequired
176
+     * @NoAdminRequired
177
+     *
178
+     * @param string $dir
179
+     * @param string $view
180
+     * @param string $fileid
181
+     * @param bool $fileNotFound
182
+     * @param string $openfile
183
+     * @return TemplateResponse|RedirectResponse
184
+     * @throws NotFoundException
185
+     */
186
+    public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false, $openfile = null) {
187
+        if ($fileid !== null) {
188
+            try {
189
+                return $this->redirectToFile($fileid);
190
+            } catch (NotFoundException $e) {
191
+                return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true]));
192
+            }
193
+        }
194
+
195
+        $nav = new \OCP\Template('files', 'appnavigation', '');
196
+
197
+        // Load the files we need
198
+        \OCP\Util::addStyle('files', 'merged');
199
+        \OCP\Util::addScript('files', 'merged-index');
200
+        \OCP\Util::addScript('files', 'dist/templates');
201
+
202
+        // mostly for the home storage's free space
203
+        // FIXME: Make non static
204
+        $storageInfo = $this->getStorageInfo();
205
+
206
+        $user = $this->userSession->getUser()->getUID();
207
+
208
+        // Get all the user favorites to create a submenu
209
+        try {
210
+            $favElements = $this->activityHelper->getFavoriteFilePaths($this->userSession->getUser()->getUID());
211
+        } catch (\RuntimeException $e) {
212
+            $favElements['folders'] = [];
213
+        }
214
+
215
+        $collapseClasses = '';
216
+        if (count($favElements['folders']) > 0) {
217
+            $collapseClasses = 'collapsible';
218
+        }
219
+
220
+        $favoritesSublistArray = [];
221
+
222
+        $navBarPositionPosition = 6;
223
+        $currentCount = 0;
224
+        foreach ($favElements['folders'] as $dir) {
225
+            $link = $this->urlGenerator->linkToRoute('files.view.index', ['dir' => $dir, 'view' => 'files']);
226
+            $sortingValue = ++$currentCount;
227
+            $element = [
228
+                'id' => str_replace('/', '-', $dir),
229
+                'view' => 'files',
230
+                'href' => $link,
231
+                'dir' => $dir,
232
+                'order' => $navBarPositionPosition,
233
+                'folderPosition' => $sortingValue,
234
+                'name' => basename($dir),
235
+                'icon' => 'files',
236
+                'quickaccesselement' => 'true'
237
+            ];
238
+
239
+            array_push($favoritesSublistArray, $element);
240
+            $navBarPositionPosition++;
241
+        }
242
+
243
+        $navItems = \OCA\Files\App::getNavigationManager()->getAll();
244
+
245
+        // add the favorites entry in menu
246
+        $navItems['favorites']['sublist'] = $favoritesSublistArray;
247
+        $navItems['favorites']['classes'] = $collapseClasses;
248
+
249
+        // parse every menu and add the expandedState user value
250
+        foreach ($navItems as $key => $item) {
251
+            if (isset($item['expandedState'])) {
252
+                $navItems[$key]['defaultExpandedState'] = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', $item['expandedState'], '0') === '1';
253
+            }
254
+        }
255
+
256
+        $nav->assign('navigationItems', $navItems);
257
+
258
+        $nav->assign('usage', \OC_Helper::humanFileSize($storageInfo['used']));
259
+        if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) {
260
+            $totalSpace = $this->l10n->t('Unlimited');
261
+        } else {
262
+            $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
263
+        }
264
+        $nav->assign('total_space', $totalSpace);
265
+        $nav->assign('quota', $storageInfo['quota']);
266
+        $nav->assign('usage_relative', $storageInfo['relative']);
267
+
268
+        $nav->assign('webdav_url', \OCP\Util::linkToRemote('dav/files/' . $user));
269
+
270
+        $contentItems = [];
271
+
272
+        // render the container content for every navigation item
273
+        foreach ($navItems as $item) {
274
+            $content = '';
275
+            if (isset($item['script'])) {
276
+                $content = $this->renderScript($item['appname'], $item['script']);
277
+            }
278
+            // parse submenus
279
+            if (isset($item['sublist'])) {
280
+                foreach ($item['sublist'] as $subitem) {
281
+                    $subcontent = '';
282
+                    if (isset($subitem['script'])) {
283
+                        $subcontent = $this->renderScript($subitem['appname'], $subitem['script']);
284
+                    }
285
+                    $contentItems[$subitem['id']] = [
286
+                        'id' => $subitem['id'],
287
+                        'content' => $subcontent
288
+                    ];
289
+                }
290
+            }
291
+            $contentItems[$item['id']] = [
292
+                'id' => $item['id'],
293
+                'content' => $content
294
+            ];
295
+        }
296
+
297
+        $event = new LoadAdditionalScriptsEvent();
298
+        $this->eventDispatcher->dispatchTyped($event);
299
+        $this->eventDispatcher->dispatchTyped(new LoadSidebar());
300
+        // Load Viewer scripts
301
+        if (class_exists(LoadViewer::class)) {
302
+            $this->eventDispatcher->dispatchTyped(new LoadViewer());
303
+        }
304
+        $this->initialState->provideInitialState('templates_path', $this->templateManager->hasTemplateDirectory() ? $this->templateManager->getTemplatePath() : false);
305
+        $this->initialState->provideInitialState('templates', $this->templateManager->listCreators());
306
+
307
+        $params = [];
308
+        $params['usedSpacePercent'] = (int) $storageInfo['relative'];
309
+        $params['owner'] = $storageInfo['owner'] ?? '';
310
+        $params['ownerDisplayName'] = $storageInfo['ownerDisplayName'] ?? '';
311
+        $params['isPublic'] = false;
312
+        $params['allowShareWithLink'] = $this->shareManager->shareApiAllowLinks() ? 'yes' : 'no';
313
+        $params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name');
314
+        $params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc');
315
+        $params['showgridview'] = $this->config->getUserValue($user, 'files', 'show_grid', false);
316
+        $params['isIE'] = \OC_Util::isIe();
317
+        $showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false);
318
+        $params['showHiddenFiles'] = $showHidden ? 1 : 0;
319
+        $cropImagePreviews = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'crop_image_previews', true);
320
+        $params['cropImagePreviews'] = $cropImagePreviews ? 1 : 0;
321
+        $params['fileNotFound'] = $fileNotFound ? 1 : 0;
322
+        $params['appNavigation'] = $nav;
323
+        $params['appContents'] = $contentItems;
324
+        $params['hiddenFields'] = $event->getHiddenFields();
325
+
326
+        $response = new TemplateResponse(
327
+            $this->appName,
328
+            'index',
329
+            $params
330
+        );
331
+        $policy = new ContentSecurityPolicy();
332
+        $policy->addAllowedFrameDomain('\'self\'');
333
+        $response->setContentSecurityPolicy($policy);
334
+
335
+        $this->provideInitialState($dir, $openfile);
336
+
337
+        return $response;
338
+    }
339
+
340
+    /**
341
+     * Add openFileInfo in initialState if $openfile is set.
342
+     * @param string $dir - the ?dir= URL param
343
+     * @param string $openfile - the ?openfile= URL param
344
+     * @return void
345
+     */
346
+    private function provideInitialState(string $dir, ?string $openfile): void {
347
+        if ($openfile === null) {
348
+            return;
349
+        }
350
+
351
+        $user = $this->userSession->getUser();
352
+
353
+        if ($user === null) {
354
+            return;
355
+        }
356
+
357
+        $uid = $user->getUID();
358
+        $userFolder = $this->rootFolder->getUserFolder($uid);
359
+        $nodes = $userFolder->getById((int) $openfile);
360
+        $node = array_shift($nodes);
361
+
362
+        if ($node === null) {
363
+            return;
364
+        }
365
+
366
+        // properly format full path and make sure
367
+        // we're relative to the user home folder
368
+        $isRoot = $node === $userFolder;
369
+        $path = $userFolder->getRelativePath($node->getPath());
370
+        $directory = $userFolder->getRelativePath($node->getParent()->getPath());
371
+
372
+        // Prevent opening a file from another folder.
373
+        if ($dir !== $directory) {
374
+            return;
375
+        }
376
+
377
+        $this->initialState->provideInitialState(
378
+            'openFileInfo', [
379
+                'id' => $node->getId(),
380
+                'name' => $isRoot ? '' : $node->getName(),
381
+                'path' => $path,
382
+                'directory' => $directory,
383
+                'mime' => $node->getMimetype(),
384
+                'type' => $node->getType(),
385
+                'permissions' => $node->getPermissions(),
386
+            ]
387
+        );
388
+    }
389
+
390
+    /**
391
+     * Redirects to the file list and highlight the given file id
392
+     *
393
+     * @param string $fileId file id to show
394
+     * @return RedirectResponse redirect response or not found response
395
+     * @throws \OCP\Files\NotFoundException
396
+     */
397
+    private function redirectToFile($fileId) {
398
+        $uid = $this->userSession->getUser()->getUID();
399
+        $baseFolder = $this->rootFolder->getUserFolder($uid);
400
+        $files = $baseFolder->getById($fileId);
401
+        $params = [];
402
+
403
+        if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) {
404
+            $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/');
405
+            $files = $baseFolder->getById($fileId);
406
+            $params['view'] = 'trashbin';
407
+        }
408
+
409
+        if (!empty($files)) {
410
+            $file = current($files);
411
+            if ($file instanceof Folder) {
412
+                // set the full path to enter the folder
413
+                $params['dir'] = $baseFolder->getRelativePath($file->getPath());
414
+            } else {
415
+                // set parent path as dir
416
+                $params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath());
417
+                // and scroll to the entry
418
+                $params['scrollto'] = $file->getName();
419
+            }
420
+
421
+            return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params));
422
+        }
423
+        throw new \OCP\Files\NotFoundException();
424
+    }
425 425
 }
Please login to merge, or discard this patch.
apps/files/lib/Search/FilesSearchProvider.php 1 patch
Indentation   +109 added lines, -109 removed lines patch added patch discarded remove patch
@@ -48,113 +48,113 @@
 block discarded – undo
48 48
 
49 49
 class FilesSearchProvider implements IProvider {
50 50
 
51
-	/** @var IL10N */
52
-	private $l10n;
53
-
54
-	/** @var IURLGenerator */
55
-	private $urlGenerator;
56
-
57
-	/** @var IMimeTypeDetector */
58
-	private $mimeTypeDetector;
59
-
60
-	/** @var IRootFolder */
61
-	private $rootFolder;
62
-
63
-	public function __construct(
64
-		IL10N $l10n,
65
-		IURLGenerator $urlGenerator,
66
-		IMimeTypeDetector $mimeTypeDetector,
67
-		IRootFolder $rootFolder
68
-	) {
69
-		$this->l10n = $l10n;
70
-		$this->urlGenerator = $urlGenerator;
71
-		$this->mimeTypeDetector = $mimeTypeDetector;
72
-		$this->rootFolder = $rootFolder;
73
-	}
74
-
75
-	/**
76
-	 * @inheritDoc
77
-	 */
78
-	public function getId(): string {
79
-		return 'files';
80
-	}
81
-
82
-	/**
83
-	 * @inheritDoc
84
-	 */
85
-	public function getName(): string {
86
-		return $this->l10n->t('Files');
87
-	}
88
-
89
-	/**
90
-	 * @inheritDoc
91
-	 */
92
-	public function getOrder(string $route, array $routeParameters): int {
93
-		if ($route === 'files.View.index') {
94
-			// Before comments
95
-			return -5;
96
-		}
97
-		return 5;
98
-	}
99
-
100
-	/**
101
-	 * @inheritDoc
102
-	 */
103
-	public function search(IUser $user, ISearchQuery $query): SearchResult {
104
-		$userFolder = $this->rootFolder->getUserFolder($user->getUID());
105
-		$fileQuery = new SearchQuery(
106
-			new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query->getTerm() . '%'),
107
-			$query->getLimit(),
108
-			(int)$query->getCursor(),
109
-			$query->getSortOrder() === ISearchQuery::SORT_DATE_DESC ? [
110
-				new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'),
111
-			] : [],
112
-			$user
113
-		);
114
-
115
-		return SearchResult::paginated(
116
-			$this->l10n->t('Files'),
117
-			array_map(function (Node $result) use ($userFolder) {
118
-				// Generate thumbnail url
119
-				$thumbnailUrl = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->getId()]);
120
-				$path = $userFolder->getRelativePath($result->getPath());
121
-				$link = $this->urlGenerator->linkToRoute(
122
-					'files.view.index',
123
-					[
124
-						'dir' => dirname($path),
125
-						'scrollto' => $result->getName(),
126
-						'openfile' => $result->getId(),
127
-					]
128
-				);
129
-
130
-				$searchResultEntry = new SearchResultEntry(
131
-					$thumbnailUrl,
132
-					$result->getName(),
133
-					$this->formatSubline($path),
134
-					$this->urlGenerator->getAbsoluteURL($link),
135
-					$result->getMimetype() === FileInfo::MIMETYPE_FOLDER ? 'icon-folder' : $this->mimeTypeDetector->mimeTypeIcon($result->getMimetype())
136
-				);
137
-				$searchResultEntry->addAttribute('fileId', (string)$result->getId());
138
-				$searchResultEntry->addAttribute('path', $path);
139
-				return $searchResultEntry;
140
-			}, $userFolder->search($fileQuery)),
141
-			$query->getCursor() + $query->getLimit()
142
-		);
143
-	}
144
-
145
-	/**
146
-	 * Format subline for files
147
-	 *
148
-	 * @param string $path
149
-	 * @return string
150
-	 */
151
-	private function formatSubline(string $path): string {
152
-		// Do not show the location if the file is in root
153
-		if (strrpos($path, '/') > 0) {
154
-			$path = ltrim(dirname($path), '/');
155
-			return $this->l10n->t('in %s', [$path]);
156
-		} else {
157
-			return '';
158
-		}
159
-	}
51
+    /** @var IL10N */
52
+    private $l10n;
53
+
54
+    /** @var IURLGenerator */
55
+    private $urlGenerator;
56
+
57
+    /** @var IMimeTypeDetector */
58
+    private $mimeTypeDetector;
59
+
60
+    /** @var IRootFolder */
61
+    private $rootFolder;
62
+
63
+    public function __construct(
64
+        IL10N $l10n,
65
+        IURLGenerator $urlGenerator,
66
+        IMimeTypeDetector $mimeTypeDetector,
67
+        IRootFolder $rootFolder
68
+    ) {
69
+        $this->l10n = $l10n;
70
+        $this->urlGenerator = $urlGenerator;
71
+        $this->mimeTypeDetector = $mimeTypeDetector;
72
+        $this->rootFolder = $rootFolder;
73
+    }
74
+
75
+    /**
76
+     * @inheritDoc
77
+     */
78
+    public function getId(): string {
79
+        return 'files';
80
+    }
81
+
82
+    /**
83
+     * @inheritDoc
84
+     */
85
+    public function getName(): string {
86
+        return $this->l10n->t('Files');
87
+    }
88
+
89
+    /**
90
+     * @inheritDoc
91
+     */
92
+    public function getOrder(string $route, array $routeParameters): int {
93
+        if ($route === 'files.View.index') {
94
+            // Before comments
95
+            return -5;
96
+        }
97
+        return 5;
98
+    }
99
+
100
+    /**
101
+     * @inheritDoc
102
+     */
103
+    public function search(IUser $user, ISearchQuery $query): SearchResult {
104
+        $userFolder = $this->rootFolder->getUserFolder($user->getUID());
105
+        $fileQuery = new SearchQuery(
106
+            new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%' . $query->getTerm() . '%'),
107
+            $query->getLimit(),
108
+            (int)$query->getCursor(),
109
+            $query->getSortOrder() === ISearchQuery::SORT_DATE_DESC ? [
110
+                new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime'),
111
+            ] : [],
112
+            $user
113
+        );
114
+
115
+        return SearchResult::paginated(
116
+            $this->l10n->t('Files'),
117
+            array_map(function (Node $result) use ($userFolder) {
118
+                // Generate thumbnail url
119
+                $thumbnailUrl = $this->urlGenerator->linkToRouteAbsolute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->getId()]);
120
+                $path = $userFolder->getRelativePath($result->getPath());
121
+                $link = $this->urlGenerator->linkToRoute(
122
+                    'files.view.index',
123
+                    [
124
+                        'dir' => dirname($path),
125
+                        'scrollto' => $result->getName(),
126
+                        'openfile' => $result->getId(),
127
+                    ]
128
+                );
129
+
130
+                $searchResultEntry = new SearchResultEntry(
131
+                    $thumbnailUrl,
132
+                    $result->getName(),
133
+                    $this->formatSubline($path),
134
+                    $this->urlGenerator->getAbsoluteURL($link),
135
+                    $result->getMimetype() === FileInfo::MIMETYPE_FOLDER ? 'icon-folder' : $this->mimeTypeDetector->mimeTypeIcon($result->getMimetype())
136
+                );
137
+                $searchResultEntry->addAttribute('fileId', (string)$result->getId());
138
+                $searchResultEntry->addAttribute('path', $path);
139
+                return $searchResultEntry;
140
+            }, $userFolder->search($fileQuery)),
141
+            $query->getCursor() + $query->getLimit()
142
+        );
143
+    }
144
+
145
+    /**
146
+     * Format subline for files
147
+     *
148
+     * @param string $path
149
+     * @return string
150
+     */
151
+    private function formatSubline(string $path): string {
152
+        // Do not show the location if the file is in root
153
+        if (strrpos($path, '/') > 0) {
154
+            $path = ltrim(dirname($path), '/');
155
+            return $this->l10n->t('in %s', [$path]);
156
+        } else {
157
+            return '';
158
+        }
159
+    }
160 160
 }
Please login to merge, or discard this patch.