1<?php
2/**
3 * @copyright Copyright (c) 2016, ownCloud, Inc.
4 *
5 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
6 * @author Jörn Friedrich Dreyer <jfd@butonic.de>
7 * @author Lukas Reschke <lukas@statuscode.ch>
8 * @author Morris Jobke <hey@morrisjobke.de>
9 * @author Robin Appelman <robin@icewind.nl>
10 * @author Roeland Jago Douma <roeland@famdouma.nl>
11 * @author Vincent Petry <vincent@nextcloud.com>
12 *
13 * @license AGPL-3.0
14 *
15 * This code is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Affero General Public License, version 3,
17 * as published by the Free Software Foundation.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Affero General Public License for more details.
23 *
24 * You should have received a copy of the GNU Affero General Public License, version 3,
25 * along with this program. If not, see <http://www.gnu.org/licenses/>
26 *
27 */
28use OCP\Files\StorageNotAvailableException;
29use OCP\Files\StorageInvalidException;
30
31\OC_JSON::checkLoggedIn();
32\OC::$server->getSession()->close();
33$l = \OC::$server->getL10N('files');
34
35// Load the files
36$dir = isset($_GET['dir']) ? (string)$_GET['dir'] : '';
37$dir = \OC\Files\Filesystem::normalizePath($dir);
38
39try {
40	$dirInfo = \OC\Files\Filesystem::getFileInfo($dir);
41	if (!$dirInfo || !$dirInfo->getType() === 'dir') {
42		http_response_code(404);
43		exit();
44	}
45
46	$data = [];
47	$baseUrl = \OC::$server->getURLGenerator()->linkTo('files', 'index.php') . '?dir=';
48
49	$permissions = $dirInfo->getPermissions();
50
51	$sortAttribute = isset($_GET['sort']) ? (string)$_GET['sort'] : 'name';
52	$sortDirection = isset($_GET['sortdirection']) ? ($_GET['sortdirection'] === 'desc') : false;
53	$mimetypeFilters = isset($_GET['mimetypes']) ? json_decode($_GET['mimetypes']) : '';
54
55	$files = [];
56	// Clean up duplicates from array
57	if (is_array($mimetypeFilters) && count($mimetypeFilters)) {
58		$mimetypeFilters = array_unique($mimetypeFilters);
59
60		if (!in_array('httpd/unix-directory', $mimetypeFilters)) {
61			// append folder filter to be able to browse folders
62			$mimetypeFilters[] = 'httpd/unix-directory';
63		}
64
65		// create filelist with mimetype filter - as getFiles only supports on
66		// mimetype filter at once we will filter this folder for each
67		// mimetypeFilter
68		foreach ($mimetypeFilters as $mimetypeFilter) {
69			$files = array_merge($files, \OCA\Files\Helper::getFiles($dir, $sortAttribute, $sortDirection, $mimetypeFilter));
70		}
71
72		// sort the files accordingly
73		$files = \OCA\Files\Helper::sortFiles($files, $sortAttribute, $sortDirection);
74	} else {
75		// create file list without mimetype filter
76		$files = \OCA\Files\Helper::getFiles($dir, $sortAttribute, $sortDirection);
77	}
78
79	$data['directory'] = $dir;
80	$data['files'] = \OCA\Files\Helper::formatFileInfos($files);
81	$data['permissions'] = $permissions;
82
83	\OC_JSON::success(['data' => $data]);
84} catch (\OCP\Files\StorageNotAvailableException $e) {
85	\OC::$server->getLogger()->logException($e, ['app' => 'files']);
86	\OC_JSON::error([
87		'data' => [
88			'exception' => StorageNotAvailableException::class,
89			'message' => $l->t('Storage is temporarily not available')
90		]
91	]);
92} catch (\OCP\Files\StorageInvalidException $e) {
93	\OC::$server->getLogger()->logException($e, ['app' => 'files']);
94	\OC_JSON::error([
95		'data' => [
96			'exception' => StorageInvalidException::class,
97			'message' => $l->t('Storage invalid')
98		]
99	]);
100} catch (\Exception $e) {
101	\OC::$server->getLogger()->logException($e, ['app' => 'files']);
102	\OC_JSON::error([
103		'data' => [
104			'exception' => \Exception::class,
105			'message' => $l->t('Unknown error')
106		]
107	]);
108}
109