Completed
Pull Request — master (#26700)
by Philipp
08:19
created

apps/files_sharing/ajax/publicpreview.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @author Björn Schießle <[email protected]>
4
 * @author Georg Ehrke <[email protected]>
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 * @author Thomas Müller <[email protected]>
9
 *
10
 * @copyright Copyright (c) 2016, ownCloud GmbH.
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
27
OCP\JSON::checkAppEnabled('files_sharing');
28
29
\OC_User::setIncognitoMode(true);
30
31
$file = array_key_exists('file', $_GET) ? (string) $_GET['file'] : '';
32
$maxX = array_key_exists('x', $_GET) ? (int) $_GET['x'] : '32';
33
$maxY = array_key_exists('y', $_GET) ? (int) $_GET['y'] : '32';
34
$scalingUp = array_key_exists('scalingup', $_GET) ? (bool) $_GET['scalingup'] : true;
35
$token = array_key_exists('t', $_GET) ? (string) $_GET['t'] : '';
36
$keepAspect = array_key_exists('a', $_GET) ? true : false;
37
38
if($token === ''){
39
	\OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST);
40
	\OCP\Util::writeLog('core-preview', 'No token parameter was passed', \OCP\Util::DEBUG);
41
	exit;
42
}
43
44
$linkedItem = \OCP\Share::getShareByToken($token);
45 View Code Duplication
if($linkedItem === false || ($linkedItem['item_type'] !== 'file' && $linkedItem['item_type'] !== 'folder')) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
	\OC_Response::setStatus(\OC_Response::STATUS_NOT_FOUND);
47
	\OCP\Util::writeLog('core-preview', 'Passed token parameter is not valid', \OCP\Util::DEBUG);
48
	exit;
49
}
50
51 View Code Duplication
if(!isset($linkedItem['uid_owner']) || !isset($linkedItem['file_source'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
	\OC_Response::setStatus(\OC_Response::STATUS_INTERNAL_SERVER_ERROR);
53
	\OCP\Util::writeLog('core-preview', 'Passed token seems to be valid, but it does not contain all necessary information . ("' . $token . '")', \OCP\Util::WARN);
54
	exit;
55
}
56
57
$rootLinkItem = OCP\Share::resolveReShare($linkedItem);
58
$userId = $rootLinkItem['uid_owner'];
59
60
OCP\JSON::checkUserExists($rootLinkItem['uid_owner']);
61
\OC_Util::setupFS($userId);
62
\OC\Files\Filesystem::initMountPoints($userId);
63
$view = new \OC\Files\View('/' . $userId . '/files');
64
65
$pathId = $linkedItem['file_source'];
66
$path = $view->getPath($pathId);
67
68 View Code Duplication
if($path === null) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
	\OC_Response::setStatus(\OC_Response::STATUS_NOT_FOUND);
70
	\OCP\Util::writeLog('core-preview', 'Could not resolve file for shared item', \OCP\Util::WARN);
71
	exit;
72
}
73
74
$pathInfo = $view->getFileInfo($path);
75
$sharedFile = null;
76
77
if($linkedItem['item_type'] === 'folder') {
78
	$isValid = \OC\Files\Filesystem::isValidPath($file);
79
	if(!$isValid) {
80
		\OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST);
81
		\OCP\Util::writeLog('core-preview', 'Passed filename is not valid, might be malicious (file:"' . $file . '";ip:"' . \OC::$server->getRequest()->getRemoteAddress() . '")', \OCP\Util::WARN);
82
		exit;
83
	}
84
	$sharedFile = \OC\Files\Filesystem::normalizePath($file);
85
}
86
87
if($linkedItem['item_type'] === 'file') {
88
	$parent = $pathInfo['parent'];
89
	$path = $view->getPath($parent);
90
	$sharedFile = $pathInfo['name'];
91
}
92
93
$path = \OC\Files\Filesystem::normalizePath($path, false);
94
if(substr($path, 0, 1) === '/') {
95
	$path = substr($path, 1);
96
}
97
98 View Code Duplication
if($maxX === 0 || $maxY === 0) {
99
	\OC_Response::setStatus(\OC_Response::STATUS_BAD_REQUEST);
100
	\OCP\Util::writeLog('core-preview', 'x and/or y set to 0', \OCP\Util::DEBUG);
101
	exit;
102
}
103
104
$root = 'files/' . $path;
105
106
try{
107
	$preview = new \OC\Preview($userId, $root);
108
	$preview->setFile($sharedFile);
109
	$preview->setMaxX($maxX);
110
	$preview->setMaxY($maxY);
111
	$preview->setScalingUp($scalingUp);
112
	$preview->setKeepAspect($keepAspect);
113
114
	$preview->showPreview();
115
} catch (\Exception $e) {
116
	\OC_Response::setStatus(\OC_Response::STATUS_INTERNAL_SERVER_ERROR);
117
	\OCP\Util::writeLog('core', $e->getmessage(), \OCP\Util::DEBUG);
118
}
119