Issues (2553)

apps/dav/lib/Connector/Sabre/ObjectTree.php (1 issue)

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bjoern Schiessle <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Roeland Jago Douma <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 * @author Vincent Petry <[email protected]>
13
 *
14
 * @license AGPL-3.0
15
 *
16
 * This code is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License, version 3,
18
 * as published by the Free Software Foundation.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
26
 * along with this program. If not, see <http://www.gnu.org/licenses/>
27
 *
28
 */
29
namespace OCA\DAV\Connector\Sabre;
30
31
use OC\Files\FileInfo;
32
use OC\Files\Storage\FailedStorage;
33
use OCA\DAV\Connector\Sabre\Exception\FileLocked;
34
use OCA\DAV\Connector\Sabre\Exception\Forbidden;
35
use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
36
use OCP\Files\ForbiddenException;
37
use OCP\Files\StorageInvalidException;
38
use OCP\Files\StorageNotAvailableException;
39
use OCP\Lock\LockedException;
40
41
class ObjectTree extends CachingTree {
42
43
	/**
44
	 * @var \OC\Files\View
45
	 */
46
	protected $fileView;
47
48
	/**
49
	 * @var  \OCP\Files\Mount\IMountManager
50
	 */
51
	protected $mountManager;
52
53
	/**
54
	 * Creates the object
55
	 */
56
	public function __construct() {
57
	}
58
59
	/**
60
	 * @param \Sabre\DAV\INode $rootNode
61
	 * @param \OC\Files\View $view
62
	 * @param  \OCP\Files\Mount\IMountManager $mountManager
63
	 */
64
	public function init(\Sabre\DAV\INode $rootNode, \OC\Files\View $view, \OCP\Files\Mount\IMountManager $mountManager) {
65
		$this->rootNode = $rootNode;
0 ignored issues
show
Documentation Bug introduced by
$rootNode is of type Sabre\DAV\INode, but the property $rootNode was declared to be of type Sabre\DAV\ICollection. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
66
		$this->fileView = $view;
67
		$this->mountManager = $mountManager;
68
	}
69
70
	/**
71
	 * If the given path is a chunked file name, converts it
72
	 * to the real file name. Only applies if the OC-CHUNKED header
73
	 * is present.
74
	 *
75
	 * @param string $path chunk file path to convert
76
	 *
77
	 * @return string path to real file
78
	 */
79
	private function resolveChunkFile($path) {
80
		if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
81
			// resolve to real file name to find the proper node
82
			[$dir, $name] = \Sabre\Uri\split($path);
83
			if ($dir === '/' || $dir === '.') {
84
				$dir = '';
85
			}
86
87
			$info = \OC_FileChunking::decodeName($name);
88
			// only replace path if it was really the chunked file
89
			if (isset($info['transferid'])) {
90
				// getNodePath is called for multiple nodes within a chunk
91
				// upload call
92
				$path = $dir . '/' . $info['name'];
93
				$path = ltrim($path, '/');
94
			}
95
		}
96
		return $path;
97
	}
98
99
	/**
100
	 * Returns the INode object for the requested path
101
	 *
102
	 * @param string $path
103
	 * @return \Sabre\DAV\INode
104
	 * @throws InvalidPath
105
	 * @throws \Sabre\DAV\Exception\Locked
106
	 * @throws \Sabre\DAV\Exception\NotFound
107
	 * @throws \Sabre\DAV\Exception\ServiceUnavailable
108
	 */
109
	public function getNodeForPath($path) {
110
		if (!$this->fileView) {
111
			throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
112
		}
113
114
		$path = trim($path, '/');
115
116
		if (isset($this->cache[$path])) {
117
			return $this->cache[$path];
118
		}
119
120
		if ($path) {
121
			try {
122
				$this->fileView->verifyPath($path, basename($path));
123
			} catch (\OCP\Files\InvalidPathException $ex) {
124
				throw new InvalidPath($ex->getMessage());
125
			}
126
		}
127
128
		// Is it the root node?
129
		if (!strlen($path)) {
130
			return $this->rootNode;
131
		}
132
133
		if (pathinfo($path, PATHINFO_EXTENSION) === 'part') {
134
			// read from storage
135
			$absPath = $this->fileView->getAbsolutePath($path);
136
			$mount = $this->fileView->getMount($path);
137
			$storage = $mount->getStorage();
138
			$internalPath = $mount->getInternalPath($absPath);
139
			if ($storage && $storage->file_exists($internalPath)) {
140
				/**
141
				 * @var \OC\Files\Storage\Storage $storage
142
				 */
143
				// get data directly
144
				$data = $storage->getMetaData($internalPath);
145
				$info = new FileInfo($absPath, $storage, $internalPath, $data, $mount);
146
			} else {
147
				$info = null;
148
			}
149
		} else {
150
			// resolve chunk file name to real name, if applicable
151
			$path = $this->resolveChunkFile($path);
152
153
			// read from cache
154
			try {
155
				$info = $this->fileView->getFileInfo($path);
156
157
				if ($info instanceof \OCP\Files\FileInfo && $info->getStorage()->instanceOfStorage(FailedStorage::class)) {
158
					throw new StorageNotAvailableException();
159
				}
160
			} catch (StorageNotAvailableException $e) {
161
				throw new \Sabre\DAV\Exception\ServiceUnavailable('Storage is temporarily not available', 0, $e);
162
			} catch (StorageInvalidException $e) {
163
				throw new \Sabre\DAV\Exception\NotFound('Storage ' . $path . ' is invalid');
164
			} catch (LockedException $e) {
165
				throw new \Sabre\DAV\Exception\Locked();
166
			} catch (ForbiddenException $e) {
167
				throw new \Sabre\DAV\Exception\Forbidden();
168
			}
169
		}
170
171
		if (!$info) {
172
			throw new \Sabre\DAV\Exception\NotFound('File with name ' . $path . ' could not be located');
173
		}
174
175
		if ($info->getType() === 'dir') {
176
			$node = new \OCA\DAV\Connector\Sabre\Directory($this->fileView, $info, $this);
177
		} else {
178
			$node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info);
179
		}
180
181
		$this->cache[$path] = $node;
182
		return $node;
183
	}
184
185
	/**
186
	 * Copies a file or directory.
187
	 *
188
	 * This method must work recursively and delete the destination
189
	 * if it exists
190
	 *
191
	 * @param string $sourcePath
192
	 * @param string $destinationPath
193
	 * @throws FileLocked
194
	 * @throws Forbidden
195
	 * @throws InvalidPath
196
	 * @throws \Exception
197
	 * @throws \Sabre\DAV\Exception\Forbidden
198
	 * @throws \Sabre\DAV\Exception\Locked
199
	 * @throws \Sabre\DAV\Exception\NotFound
200
	 * @throws \Sabre\DAV\Exception\ServiceUnavailable
201
	 * @return void
202
	 */
203
	public function copy($sourcePath, $destinationPath) {
204
		if (!$this->fileView) {
205
			throw new \Sabre\DAV\Exception\ServiceUnavailable('filesystem not setup');
206
		}
207
208
209
		$info = $this->fileView->getFileInfo(dirname($destinationPath));
210
		if ($this->fileView->file_exists($destinationPath)) {
211
			$destinationPermission = $info && $info->isUpdateable();
212
		} else {
213
			$destinationPermission = $info && $info->isCreatable();
214
		}
215
		if (!$destinationPermission) {
216
			throw new Forbidden('No permissions to copy object.');
217
		}
218
219
		// this will trigger existence check
220
		$this->getNodeForPath($sourcePath);
221
222
		[$destinationDir, $destinationName] = \Sabre\Uri\split($destinationPath);
223
		try {
224
			$this->fileView->verifyPath($destinationDir, $destinationName);
225
		} catch (\OCP\Files\InvalidPathException $ex) {
226
			throw new InvalidPath($ex->getMessage());
227
		}
228
229
		try {
230
			$this->fileView->copy($sourcePath, $destinationPath);
231
		} catch (StorageNotAvailableException $e) {
232
			throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
233
		} catch (ForbiddenException $ex) {
234
			throw new Forbidden($ex->getMessage(), $ex->getRetry());
235
		} catch (LockedException $e) {
236
			throw new FileLocked($e->getMessage(), $e->getCode(), $e);
237
		}
238
239
		[$destinationDir,] = \Sabre\Uri\split($destinationPath);
240
		$this->markDirty($destinationDir);
241
	}
242
}
243