Passed
Push — master ( 814bf0...daee22 )
by Morris
13:03
created
apps/dav/lib/Connector/Sabre/FilesPlugin.php 2 patches
Indentation   +444 added lines, -444 removed lines patch added patch discarded remove patch
@@ -51,452 +51,452 @@
 block discarded – undo
51 51
 
52 52
 class FilesPlugin extends ServerPlugin {
53 53
 
54
-	// namespace
55
-	const NS_OWNCLOUD = 'http://owncloud.org/ns';
56
-	const NS_NEXTCLOUD = 'http://nextcloud.org/ns';
57
-	const FILEID_PROPERTYNAME = '{http://owncloud.org/ns}id';
58
-	const INTERNAL_FILEID_PROPERTYNAME = '{http://owncloud.org/ns}fileid';
59
-	const PERMISSIONS_PROPERTYNAME = '{http://owncloud.org/ns}permissions';
60
-	const SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-collaboration-services.org/ns}share-permissions';
61
-	const OCM_SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-cloud-mesh.org/ns}share-permissions';
62
-	const DOWNLOADURL_PROPERTYNAME = '{http://owncloud.org/ns}downloadURL';
63
-	const SIZE_PROPERTYNAME = '{http://owncloud.org/ns}size';
64
-	const GETETAG_PROPERTYNAME = '{DAV:}getetag';
65
-	const LASTMODIFIED_PROPERTYNAME = '{DAV:}lastmodified';
66
-	const OWNER_ID_PROPERTYNAME = '{http://owncloud.org/ns}owner-id';
67
-	const OWNER_DISPLAY_NAME_PROPERTYNAME = '{http://owncloud.org/ns}owner-display-name';
68
-	const CHECKSUMS_PROPERTYNAME = '{http://owncloud.org/ns}checksums';
69
-	const DATA_FINGERPRINT_PROPERTYNAME = '{http://owncloud.org/ns}data-fingerprint';
70
-	const HAS_PREVIEW_PROPERTYNAME = '{http://nextcloud.org/ns}has-preview';
71
-	const MOUNT_TYPE_PROPERTYNAME = '{http://nextcloud.org/ns}mount-type';
72
-	const IS_ENCRYPTED_PROPERTYNAME = '{http://nextcloud.org/ns}is-encrypted';
73
-	const SHARE_NOTE = '{http://nextcloud.org/ns}note';
74
-
75
-	/**
76
-	 * Reference to main server object
77
-	 *
78
-	 * @var \Sabre\DAV\Server
79
-	 */
80
-	private $server;
81
-
82
-	/**
83
-	 * @var Tree
84
-	 */
85
-	private $tree;
86
-
87
-	/**
88
-	 * Whether this is public webdav.
89
-	 * If true, some returned information will be stripped off.
90
-	 *
91
-	 * @var bool
92
-	 */
93
-	private $isPublic;
94
-
95
-	/**
96
-	 * @var bool
97
-	 */
98
-	private $downloadAttachment;
99
-
100
-	/**
101
-	 * @var IConfig
102
-	 */
103
-	private $config;
104
-
105
-	/**
106
-	 * @var IRequest
107
-	 */
108
-	private $request;
109
-
110
-	/**
111
-	 * @var IPreview
112
-	 */
113
-	private $previewManager;
114
-
115
-	/**
116
-	 * @param Tree $tree
117
-	 * @param IConfig $config
118
-	 * @param IRequest $request
119
-	 * @param IPreview $previewManager
120
-	 * @param bool $isPublic
121
-	 * @param bool $downloadAttachment
122
-	 */
123
-	public function __construct(Tree $tree,
124
-								IConfig $config,
125
-								IRequest $request,
126
-								IPreview $previewManager,
127
-								$isPublic = false,
128
-								$downloadAttachment = true) {
129
-		$this->tree = $tree;
130
-		$this->config = $config;
131
-		$this->request = $request;
132
-		$this->isPublic = $isPublic;
133
-		$this->downloadAttachment = $downloadAttachment;
134
-		$this->previewManager = $previewManager;
135
-	}
136
-
137
-	/**
138
-	 * This initializes the plugin.
139
-	 *
140
-	 * This function is called by \Sabre\DAV\Server, after
141
-	 * addPlugin is called.
142
-	 *
143
-	 * This method should set up the required event subscriptions.
144
-	 *
145
-	 * @param \Sabre\DAV\Server $server
146
-	 * @return void
147
-	 */
148
-	public function initialize(\Sabre\DAV\Server $server) {
149
-		$server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
150
-		$server->xml->namespaceMap[self::NS_NEXTCLOUD] = 'nc';
151
-		$server->protectedProperties[] = self::FILEID_PROPERTYNAME;
152
-		$server->protectedProperties[] = self::INTERNAL_FILEID_PROPERTYNAME;
153
-		$server->protectedProperties[] = self::PERMISSIONS_PROPERTYNAME;
154
-		$server->protectedProperties[] = self::SHARE_PERMISSIONS_PROPERTYNAME;
155
-		$server->protectedProperties[] = self::OCM_SHARE_PERMISSIONS_PROPERTYNAME;
156
-		$server->protectedProperties[] = self::SIZE_PROPERTYNAME;
157
-		$server->protectedProperties[] = self::DOWNLOADURL_PROPERTYNAME;
158
-		$server->protectedProperties[] = self::OWNER_ID_PROPERTYNAME;
159
-		$server->protectedProperties[] = self::OWNER_DISPLAY_NAME_PROPERTYNAME;
160
-		$server->protectedProperties[] = self::CHECKSUMS_PROPERTYNAME;
161
-		$server->protectedProperties[] = self::DATA_FINGERPRINT_PROPERTYNAME;
162
-		$server->protectedProperties[] = self::HAS_PREVIEW_PROPERTYNAME;
163
-		$server->protectedProperties[] = self::MOUNT_TYPE_PROPERTYNAME;
164
-		$server->protectedProperties[] = self::IS_ENCRYPTED_PROPERTYNAME;
165
-		$server->protectedProperties[] = self::SHARE_NOTE;
166
-
167
-		// normally these cannot be changed (RFC4918), but we want them modifiable through PROPPATCH
168
-		$allowedProperties = ['{DAV:}getetag'];
169
-		$server->protectedProperties = array_diff($server->protectedProperties, $allowedProperties);
170
-
171
-		$this->server = $server;
172
-		$this->server->on('propFind', array($this, 'handleGetProperties'));
173
-		$this->server->on('propPatch', array($this, 'handleUpdateProperties'));
174
-		$this->server->on('afterBind', array($this, 'sendFileIdHeader'));
175
-		$this->server->on('afterWriteContent', array($this, 'sendFileIdHeader'));
176
-		$this->server->on('afterMethod:GET', [$this,'httpGet']);
177
-		$this->server->on('afterMethod:GET', array($this, 'handleDownloadToken'));
178
-		$this->server->on('afterResponse', function($request, ResponseInterface $response) {
179
-			$body = $response->getBody();
180
-			if (is_resource($body)) {
181
-				fclose($body);
182
-			}
183
-		});
184
-		$this->server->on('beforeMove', [$this, 'checkMove']);
185
-	}
186
-
187
-	/**
188
-	 * Plugin that checks if a move can actually be performed.
189
-	 *
190
-	 * @param string $source source path
191
-	 * @param string $destination destination path
192
-	 * @throws Forbidden
193
-	 * @throws NotFound
194
-	 */
195
-	function checkMove($source, $destination) {
196
-		$sourceNode = $this->tree->getNodeForPath($source);
197
-		if (!$sourceNode instanceof Node) {
198
-			return;
199
-		}
200
-		list($sourceDir,) = \Sabre\Uri\split($source);
201
-		list($destinationDir,) = \Sabre\Uri\split($destination);
202
-
203
-		if ($sourceDir !== $destinationDir) {
204
-			$sourceNodeFileInfo = $sourceNode->getFileInfo();
205
-			if ($sourceNodeFileInfo === null) {
206
-				throw new NotFound($source . ' does not exist');
207
- 			}
208
-
209
-			if (!$sourceNodeFileInfo->isDeletable()) {
210
-				throw new Forbidden($source . " cannot be deleted");
211
-			}
212
-		}
213
-	}
214
-
215
-	/**
216
-	 * This sets a cookie to be able to recognize the start of the download
217
-	 * the content must not be longer than 32 characters and must only contain
218
-	 * alphanumeric characters
219
-	 *
220
-	 * @param RequestInterface $request
221
-	 * @param ResponseInterface $response
222
-	 */
223
-	function handleDownloadToken(RequestInterface $request, ResponseInterface $response) {
224
-		$queryParams = $request->getQueryParameters();
225
-
226
-		/**
227
-		 * this sets a cookie to be able to recognize the start of the download
228
-		 * the content must not be longer than 32 characters and must only contain
229
-		 * alphanumeric characters
230
-		 */
231
-		if (isset($queryParams['downloadStartSecret'])) {
232
-			$token = $queryParams['downloadStartSecret'];
233
-			if (!isset($token[32])
234
-				&& preg_match('!^[a-zA-Z0-9]+$!', $token) === 1) {
235
-				// FIXME: use $response->setHeader() instead
236
-				setcookie('ocDownloadStarted', $token, time() + 20, '/');
237
-			}
238
-		}
239
-	}
240
-
241
-	/**
242
-	 * Add headers to file download
243
-	 *
244
-	 * @param RequestInterface $request
245
-	 * @param ResponseInterface $response
246
-	 */
247
-	function httpGet(RequestInterface $request, ResponseInterface $response) {
248
-		// Only handle valid files
249
-		$node = $this->tree->getNodeForPath($request->getPath());
250
-		if (!($node instanceof IFile)) return;
251
-
252
-		// adds a 'Content-Disposition: attachment' header in case no disposition
253
-		// header has been set before
254
-		if ($this->downloadAttachment &&
255
-			$response->getHeader('Content-Disposition') === null) {
256
-			$filename = $node->getName();
257
-			if ($this->request->isUserAgent(
258
-				[
259
-					Request::USER_AGENT_IE,
260
-					Request::USER_AGENT_ANDROID_MOBILE_CHROME,
261
-					Request::USER_AGENT_FREEBOX,
262
-				])) {
263
-				$response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"');
264
-			} else {
265
-				$response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename)
266
-													 . '; filename="' . rawurlencode($filename) . '"');
267
-			}
268
-		}
269
-
270
-		if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
271
-			//Add OC-Checksum header
272
-			/** @var $node File */
273
-			$checksum = $node->getChecksum();
274
-			if ($checksum !== null && $checksum !== '') {
275
-				$response->addHeader('OC-Checksum', $checksum);
276
-			}
277
-		}
278
-	}
279
-
280
-	/**
281
-	 * Adds all ownCloud-specific properties
282
-	 *
283
-	 * @param PropFind $propFind
284
-	 * @param \Sabre\DAV\INode $node
285
-	 * @return void
286
-	 */
287
-	public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) {
288
-
289
-		$httpRequest = $this->server->httpRequest;
290
-
291
-		if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
292
-			/**
293
-			 * This was disabled, because it made dir listing throw an exception,
294
-			 * so users were unable to navigate into folders where one subitem
295
-			 * is blocked by the files_accesscontrol app, see:
296
-			 * https://github.com/nextcloud/files_accesscontrol/issues/65
54
+    // namespace
55
+    const NS_OWNCLOUD = 'http://owncloud.org/ns';
56
+    const NS_NEXTCLOUD = 'http://nextcloud.org/ns';
57
+    const FILEID_PROPERTYNAME = '{http://owncloud.org/ns}id';
58
+    const INTERNAL_FILEID_PROPERTYNAME = '{http://owncloud.org/ns}fileid';
59
+    const PERMISSIONS_PROPERTYNAME = '{http://owncloud.org/ns}permissions';
60
+    const SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-collaboration-services.org/ns}share-permissions';
61
+    const OCM_SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-cloud-mesh.org/ns}share-permissions';
62
+    const DOWNLOADURL_PROPERTYNAME = '{http://owncloud.org/ns}downloadURL';
63
+    const SIZE_PROPERTYNAME = '{http://owncloud.org/ns}size';
64
+    const GETETAG_PROPERTYNAME = '{DAV:}getetag';
65
+    const LASTMODIFIED_PROPERTYNAME = '{DAV:}lastmodified';
66
+    const OWNER_ID_PROPERTYNAME = '{http://owncloud.org/ns}owner-id';
67
+    const OWNER_DISPLAY_NAME_PROPERTYNAME = '{http://owncloud.org/ns}owner-display-name';
68
+    const CHECKSUMS_PROPERTYNAME = '{http://owncloud.org/ns}checksums';
69
+    const DATA_FINGERPRINT_PROPERTYNAME = '{http://owncloud.org/ns}data-fingerprint';
70
+    const HAS_PREVIEW_PROPERTYNAME = '{http://nextcloud.org/ns}has-preview';
71
+    const MOUNT_TYPE_PROPERTYNAME = '{http://nextcloud.org/ns}mount-type';
72
+    const IS_ENCRYPTED_PROPERTYNAME = '{http://nextcloud.org/ns}is-encrypted';
73
+    const SHARE_NOTE = '{http://nextcloud.org/ns}note';
74
+
75
+    /**
76
+     * Reference to main server object
77
+     *
78
+     * @var \Sabre\DAV\Server
79
+     */
80
+    private $server;
81
+
82
+    /**
83
+     * @var Tree
84
+     */
85
+    private $tree;
86
+
87
+    /**
88
+     * Whether this is public webdav.
89
+     * If true, some returned information will be stripped off.
90
+     *
91
+     * @var bool
92
+     */
93
+    private $isPublic;
94
+
95
+    /**
96
+     * @var bool
97
+     */
98
+    private $downloadAttachment;
99
+
100
+    /**
101
+     * @var IConfig
102
+     */
103
+    private $config;
104
+
105
+    /**
106
+     * @var IRequest
107
+     */
108
+    private $request;
109
+
110
+    /**
111
+     * @var IPreview
112
+     */
113
+    private $previewManager;
114
+
115
+    /**
116
+     * @param Tree $tree
117
+     * @param IConfig $config
118
+     * @param IRequest $request
119
+     * @param IPreview $previewManager
120
+     * @param bool $isPublic
121
+     * @param bool $downloadAttachment
122
+     */
123
+    public function __construct(Tree $tree,
124
+                                IConfig $config,
125
+                                IRequest $request,
126
+                                IPreview $previewManager,
127
+                                $isPublic = false,
128
+                                $downloadAttachment = true) {
129
+        $this->tree = $tree;
130
+        $this->config = $config;
131
+        $this->request = $request;
132
+        $this->isPublic = $isPublic;
133
+        $this->downloadAttachment = $downloadAttachment;
134
+        $this->previewManager = $previewManager;
135
+    }
136
+
137
+    /**
138
+     * This initializes the plugin.
139
+     *
140
+     * This function is called by \Sabre\DAV\Server, after
141
+     * addPlugin is called.
142
+     *
143
+     * This method should set up the required event subscriptions.
144
+     *
145
+     * @param \Sabre\DAV\Server $server
146
+     * @return void
147
+     */
148
+    public function initialize(\Sabre\DAV\Server $server) {
149
+        $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
150
+        $server->xml->namespaceMap[self::NS_NEXTCLOUD] = 'nc';
151
+        $server->protectedProperties[] = self::FILEID_PROPERTYNAME;
152
+        $server->protectedProperties[] = self::INTERNAL_FILEID_PROPERTYNAME;
153
+        $server->protectedProperties[] = self::PERMISSIONS_PROPERTYNAME;
154
+        $server->protectedProperties[] = self::SHARE_PERMISSIONS_PROPERTYNAME;
155
+        $server->protectedProperties[] = self::OCM_SHARE_PERMISSIONS_PROPERTYNAME;
156
+        $server->protectedProperties[] = self::SIZE_PROPERTYNAME;
157
+        $server->protectedProperties[] = self::DOWNLOADURL_PROPERTYNAME;
158
+        $server->protectedProperties[] = self::OWNER_ID_PROPERTYNAME;
159
+        $server->protectedProperties[] = self::OWNER_DISPLAY_NAME_PROPERTYNAME;
160
+        $server->protectedProperties[] = self::CHECKSUMS_PROPERTYNAME;
161
+        $server->protectedProperties[] = self::DATA_FINGERPRINT_PROPERTYNAME;
162
+        $server->protectedProperties[] = self::HAS_PREVIEW_PROPERTYNAME;
163
+        $server->protectedProperties[] = self::MOUNT_TYPE_PROPERTYNAME;
164
+        $server->protectedProperties[] = self::IS_ENCRYPTED_PROPERTYNAME;
165
+        $server->protectedProperties[] = self::SHARE_NOTE;
166
+
167
+        // normally these cannot be changed (RFC4918), but we want them modifiable through PROPPATCH
168
+        $allowedProperties = ['{DAV:}getetag'];
169
+        $server->protectedProperties = array_diff($server->protectedProperties, $allowedProperties);
170
+
171
+        $this->server = $server;
172
+        $this->server->on('propFind', array($this, 'handleGetProperties'));
173
+        $this->server->on('propPatch', array($this, 'handleUpdateProperties'));
174
+        $this->server->on('afterBind', array($this, 'sendFileIdHeader'));
175
+        $this->server->on('afterWriteContent', array($this, 'sendFileIdHeader'));
176
+        $this->server->on('afterMethod:GET', [$this,'httpGet']);
177
+        $this->server->on('afterMethod:GET', array($this, 'handleDownloadToken'));
178
+        $this->server->on('afterResponse', function($request, ResponseInterface $response) {
179
+            $body = $response->getBody();
180
+            if (is_resource($body)) {
181
+                fclose($body);
182
+            }
183
+        });
184
+        $this->server->on('beforeMove', [$this, 'checkMove']);
185
+    }
186
+
187
+    /**
188
+     * Plugin that checks if a move can actually be performed.
189
+     *
190
+     * @param string $source source path
191
+     * @param string $destination destination path
192
+     * @throws Forbidden
193
+     * @throws NotFound
194
+     */
195
+    function checkMove($source, $destination) {
196
+        $sourceNode = $this->tree->getNodeForPath($source);
197
+        if (!$sourceNode instanceof Node) {
198
+            return;
199
+        }
200
+        list($sourceDir,) = \Sabre\Uri\split($source);
201
+        list($destinationDir,) = \Sabre\Uri\split($destination);
202
+
203
+        if ($sourceDir !== $destinationDir) {
204
+            $sourceNodeFileInfo = $sourceNode->getFileInfo();
205
+            if ($sourceNodeFileInfo === null) {
206
+                throw new NotFound($source . ' does not exist');
207
+                }
208
+
209
+            if (!$sourceNodeFileInfo->isDeletable()) {
210
+                throw new Forbidden($source . " cannot be deleted");
211
+            }
212
+        }
213
+    }
214
+
215
+    /**
216
+     * This sets a cookie to be able to recognize the start of the download
217
+     * the content must not be longer than 32 characters and must only contain
218
+     * alphanumeric characters
219
+     *
220
+     * @param RequestInterface $request
221
+     * @param ResponseInterface $response
222
+     */
223
+    function handleDownloadToken(RequestInterface $request, ResponseInterface $response) {
224
+        $queryParams = $request->getQueryParameters();
225
+
226
+        /**
227
+         * this sets a cookie to be able to recognize the start of the download
228
+         * the content must not be longer than 32 characters and must only contain
229
+         * alphanumeric characters
230
+         */
231
+        if (isset($queryParams['downloadStartSecret'])) {
232
+            $token = $queryParams['downloadStartSecret'];
233
+            if (!isset($token[32])
234
+                && preg_match('!^[a-zA-Z0-9]+$!', $token) === 1) {
235
+                // FIXME: use $response->setHeader() instead
236
+                setcookie('ocDownloadStarted', $token, time() + 20, '/');
237
+            }
238
+        }
239
+    }
240
+
241
+    /**
242
+     * Add headers to file download
243
+     *
244
+     * @param RequestInterface $request
245
+     * @param ResponseInterface $response
246
+     */
247
+    function httpGet(RequestInterface $request, ResponseInterface $response) {
248
+        // Only handle valid files
249
+        $node = $this->tree->getNodeForPath($request->getPath());
250
+        if (!($node instanceof IFile)) return;
251
+
252
+        // adds a 'Content-Disposition: attachment' header in case no disposition
253
+        // header has been set before
254
+        if ($this->downloadAttachment &&
255
+            $response->getHeader('Content-Disposition') === null) {
256
+            $filename = $node->getName();
257
+            if ($this->request->isUserAgent(
258
+                [
259
+                    Request::USER_AGENT_IE,
260
+                    Request::USER_AGENT_ANDROID_MOBILE_CHROME,
261
+                    Request::USER_AGENT_FREEBOX,
262
+                ])) {
263
+                $response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"');
264
+            } else {
265
+                $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename)
266
+                                                        . '; filename="' . rawurlencode($filename) . '"');
267
+            }
268
+        }
269
+
270
+        if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
271
+            //Add OC-Checksum header
272
+            /** @var $node File */
273
+            $checksum = $node->getChecksum();
274
+            if ($checksum !== null && $checksum !== '') {
275
+                $response->addHeader('OC-Checksum', $checksum);
276
+            }
277
+        }
278
+    }
279
+
280
+    /**
281
+     * Adds all ownCloud-specific properties
282
+     *
283
+     * @param PropFind $propFind
284
+     * @param \Sabre\DAV\INode $node
285
+     * @return void
286
+     */
287
+    public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) {
288
+
289
+        $httpRequest = $this->server->httpRequest;
290
+
291
+        if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
292
+            /**
293
+             * This was disabled, because it made dir listing throw an exception,
294
+             * so users were unable to navigate into folders where one subitem
295
+             * is blocked by the files_accesscontrol app, see:
296
+             * https://github.com/nextcloud/files_accesscontrol/issues/65
297 297
 			if (!$node->getFileInfo()->isReadable()) {
298 298
 				// avoid detecting files through this means
299 299
 				throw new NotFound();
300 300
 			}
301
-			 */
302
-
303
-			$propFind->handle(self::FILEID_PROPERTYNAME, function() use ($node) {
304
-				return $node->getFileId();
305
-			});
306
-
307
-			$propFind->handle(self::INTERNAL_FILEID_PROPERTYNAME, function() use ($node) {
308
-				return $node->getInternalFileId();
309
-			});
310
-
311
-			$propFind->handle(self::PERMISSIONS_PROPERTYNAME, function() use ($node) {
312
-				$perms = $node->getDavPermissions();
313
-				if ($this->isPublic) {
314
-					// remove mount information
315
-					$perms = str_replace(['S', 'M'], '', $perms);
316
-				}
317
-				return $perms;
318
-			});
319
-
320
-			$propFind->handle(self::SHARE_PERMISSIONS_PROPERTYNAME, function() use ($node, $httpRequest) {
321
-				return $node->getSharePermissions(
322
-					$httpRequest->getRawServerValue('PHP_AUTH_USER')
323
-				);
324
-			});
325
-
326
-			$propFind->handle(self::OCM_SHARE_PERMISSIONS_PROPERTYNAME, function() use ($node, $httpRequest) {
327
-				$ncPermissions = $node->getSharePermissions(
328
-					$httpRequest->getRawServerValue('PHP_AUTH_USER')
329
-				);
330
-				$ocmPermissions = $this->ncPermissions2ocmPermissions($ncPermissions);
331
-				return json_encode($ocmPermissions);
332
-			});
333
-
334
-			$propFind->handle(self::GETETAG_PROPERTYNAME, function() use ($node) {
335
-				return $node->getETag();
336
-			});
337
-
338
-			$propFind->handle(self::OWNER_ID_PROPERTYNAME, function() use ($node) {
339
-				$owner = $node->getOwner();
340
-				if (!$owner) {
341
-					return null;
342
-				} else {
343
-					return $owner->getUID();
344
-				}
345
-			});
346
-			$propFind->handle(self::OWNER_DISPLAY_NAME_PROPERTYNAME, function() use ($node) {
347
-				$owner = $node->getOwner();
348
-				if (!$owner) {
349
-					return null;
350
-				} else {
351
-					return $owner->getDisplayName();
352
-				}
353
-			});
354
-
355
-			$propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function () use ($node) {
356
-				return json_encode($this->previewManager->isAvailable($node->getFileInfo()));
357
-			});
358
-			$propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) {
359
-				return $node->getSize();
360
-			});
361
-			$propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function () use ($node) {
362
-				return $node->getFileInfo()->getMountPoint()->getMountType();
363
-			});
364
-
365
-			$propFind->handle(self::SHARE_NOTE, function() use ($node, $httpRequest) {
366
-				return $node->getNoteFromShare(
367
-					$httpRequest->getRawServerValue('PHP_AUTH_USER')
368
-				);
369
-			});
370
-		}
371
-
372
-		if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
373
-			$propFind->handle(self::DATA_FINGERPRINT_PROPERTYNAME, function() use ($node) {
374
-				return $this->config->getSystemValue('data-fingerprint', '');
375
-			});
376
-		}
377
-
378
-		if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
379
-			$propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function() use ($node) {
380
-				/** @var $node \OCA\DAV\Connector\Sabre\File */
381
-				try {
382
-					$directDownloadUrl = $node->getDirectDownload();
383
-					if (isset($directDownloadUrl['url'])) {
384
-						return $directDownloadUrl['url'];
385
-					}
386
-				} catch (StorageNotAvailableException $e) {
387
-					return false;
388
-				} catch (ForbiddenException $e) {
389
-					return false;
390
-				}
391
-				return false;
392
-			});
393
-
394
-			$propFind->handle(self::CHECKSUMS_PROPERTYNAME, function() use ($node) {
395
-				$checksum = $node->getChecksum();
396
-				if ($checksum === NULL || $checksum === '') {
397
-					return null;
398
-				}
399
-
400
-				return new ChecksumList($checksum);
401
-			});
402
-
403
-		}
404
-
405
-		if ($node instanceof \OCA\DAV\Connector\Sabre\Directory) {
406
-			$propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) {
407
-				return $node->getSize();
408
-			});
409
-
410
-			$propFind->handle(self::IS_ENCRYPTED_PROPERTYNAME, function() use ($node) {
411
-				return $node->getFileInfo()->isEncrypted() ? '1' : '0';
412
-			});
413
-		}
414
-	}
415
-
416
-	/**
417
-	 * translate Nextcloud permissions to OCM Permissions
418
-	 *
419
-	 * @param $ncPermissions
420
-	 * @return array
421
-	 */
422
-	protected function ncPermissions2ocmPermissions($ncPermissions) {
423
-
424
-		$ocmPermissions = [];
425
-
426
-		if ($ncPermissions & Constants::PERMISSION_SHARE) {
427
-			$ocmPermissions[] = 'share';
428
-		}
429
-
430
-		if ($ncPermissions & Constants::PERMISSION_READ) {
431
-			$ocmPermissions[] = 'read';
432
-		}
433
-
434
-		if (($ncPermissions & Constants::PERMISSION_CREATE) ||
435
-			($ncPermissions & Constants::PERMISSION_UPDATE)) {
436
-			$ocmPermissions[] = 'write';
437
-		}
438
-
439
-		return $ocmPermissions;
440
-
441
-	}
442
-
443
-	/**
444
-	 * Update ownCloud-specific properties
445
-	 *
446
-	 * @param string $path
447
-	 * @param PropPatch $propPatch
448
-	 *
449
-	 * @return void
450
-	 */
451
-	public function handleUpdateProperties($path, PropPatch $propPatch) {
452
-		$node = $this->tree->getNodeForPath($path);
453
-		if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) {
454
-			return;
455
-		}
456
-
457
-		$propPatch->handle(self::LASTMODIFIED_PROPERTYNAME, function($time) use ($node) {
458
-			if (empty($time)) {
459
-				return false;
460
-			}
461
-			$node->touch($time);
462
-			return true;
463
-		});
464
-		$propPatch->handle(self::GETETAG_PROPERTYNAME, function($etag) use ($node) {
465
-			if (empty($etag)) {
466
-				return false;
467
-			}
468
-			if ($node->setEtag($etag) !== -1) {
469
-				return true;
470
-			}
471
-			return false;
472
-		});
473
-	}
474
-
475
-	/**
476
-	 * @param string $filePath
477
-	 * @param \Sabre\DAV\INode $node
478
-	 * @throws \Sabre\DAV\Exception\BadRequest
479
-	 */
480
-	public function sendFileIdHeader($filePath, \Sabre\DAV\INode $node = null) {
481
-		// chunked upload handling
482
-		if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
483
-			list($path, $name) = \Sabre\Uri\split($filePath);
484
-			$info = \OC_FileChunking::decodeName($name);
485
-			if (!empty($info)) {
486
-				$filePath = $path . '/' . $info['name'];
487
-			}
488
-		}
489
-
490
-		// we get the node for the given $filePath here because in case of afterCreateFile $node is the parent folder
491
-		if (!$this->server->tree->nodeExists($filePath)) {
492
-			return;
493
-		}
494
-		$node = $this->server->tree->getNodeForPath($filePath);
495
-		if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
496
-			$fileId = $node->getFileId();
497
-			if (!is_null($fileId)) {
498
-				$this->server->httpResponse->setHeader('OC-FileId', $fileId);
499
-			}
500
-		}
501
-	}
301
+             */
302
+
303
+            $propFind->handle(self::FILEID_PROPERTYNAME, function() use ($node) {
304
+                return $node->getFileId();
305
+            });
306
+
307
+            $propFind->handle(self::INTERNAL_FILEID_PROPERTYNAME, function() use ($node) {
308
+                return $node->getInternalFileId();
309
+            });
310
+
311
+            $propFind->handle(self::PERMISSIONS_PROPERTYNAME, function() use ($node) {
312
+                $perms = $node->getDavPermissions();
313
+                if ($this->isPublic) {
314
+                    // remove mount information
315
+                    $perms = str_replace(['S', 'M'], '', $perms);
316
+                }
317
+                return $perms;
318
+            });
319
+
320
+            $propFind->handle(self::SHARE_PERMISSIONS_PROPERTYNAME, function() use ($node, $httpRequest) {
321
+                return $node->getSharePermissions(
322
+                    $httpRequest->getRawServerValue('PHP_AUTH_USER')
323
+                );
324
+            });
325
+
326
+            $propFind->handle(self::OCM_SHARE_PERMISSIONS_PROPERTYNAME, function() use ($node, $httpRequest) {
327
+                $ncPermissions = $node->getSharePermissions(
328
+                    $httpRequest->getRawServerValue('PHP_AUTH_USER')
329
+                );
330
+                $ocmPermissions = $this->ncPermissions2ocmPermissions($ncPermissions);
331
+                return json_encode($ocmPermissions);
332
+            });
333
+
334
+            $propFind->handle(self::GETETAG_PROPERTYNAME, function() use ($node) {
335
+                return $node->getETag();
336
+            });
337
+
338
+            $propFind->handle(self::OWNER_ID_PROPERTYNAME, function() use ($node) {
339
+                $owner = $node->getOwner();
340
+                if (!$owner) {
341
+                    return null;
342
+                } else {
343
+                    return $owner->getUID();
344
+                }
345
+            });
346
+            $propFind->handle(self::OWNER_DISPLAY_NAME_PROPERTYNAME, function() use ($node) {
347
+                $owner = $node->getOwner();
348
+                if (!$owner) {
349
+                    return null;
350
+                } else {
351
+                    return $owner->getDisplayName();
352
+                }
353
+            });
354
+
355
+            $propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function () use ($node) {
356
+                return json_encode($this->previewManager->isAvailable($node->getFileInfo()));
357
+            });
358
+            $propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) {
359
+                return $node->getSize();
360
+            });
361
+            $propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function () use ($node) {
362
+                return $node->getFileInfo()->getMountPoint()->getMountType();
363
+            });
364
+
365
+            $propFind->handle(self::SHARE_NOTE, function() use ($node, $httpRequest) {
366
+                return $node->getNoteFromShare(
367
+                    $httpRequest->getRawServerValue('PHP_AUTH_USER')
368
+                );
369
+            });
370
+        }
371
+
372
+        if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
373
+            $propFind->handle(self::DATA_FINGERPRINT_PROPERTYNAME, function() use ($node) {
374
+                return $this->config->getSystemValue('data-fingerprint', '');
375
+            });
376
+        }
377
+
378
+        if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
379
+            $propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function() use ($node) {
380
+                /** @var $node \OCA\DAV\Connector\Sabre\File */
381
+                try {
382
+                    $directDownloadUrl = $node->getDirectDownload();
383
+                    if (isset($directDownloadUrl['url'])) {
384
+                        return $directDownloadUrl['url'];
385
+                    }
386
+                } catch (StorageNotAvailableException $e) {
387
+                    return false;
388
+                } catch (ForbiddenException $e) {
389
+                    return false;
390
+                }
391
+                return false;
392
+            });
393
+
394
+            $propFind->handle(self::CHECKSUMS_PROPERTYNAME, function() use ($node) {
395
+                $checksum = $node->getChecksum();
396
+                if ($checksum === NULL || $checksum === '') {
397
+                    return null;
398
+                }
399
+
400
+                return new ChecksumList($checksum);
401
+            });
402
+
403
+        }
404
+
405
+        if ($node instanceof \OCA\DAV\Connector\Sabre\Directory) {
406
+            $propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) {
407
+                return $node->getSize();
408
+            });
409
+
410
+            $propFind->handle(self::IS_ENCRYPTED_PROPERTYNAME, function() use ($node) {
411
+                return $node->getFileInfo()->isEncrypted() ? '1' : '0';
412
+            });
413
+        }
414
+    }
415
+
416
+    /**
417
+     * translate Nextcloud permissions to OCM Permissions
418
+     *
419
+     * @param $ncPermissions
420
+     * @return array
421
+     */
422
+    protected function ncPermissions2ocmPermissions($ncPermissions) {
423
+
424
+        $ocmPermissions = [];
425
+
426
+        if ($ncPermissions & Constants::PERMISSION_SHARE) {
427
+            $ocmPermissions[] = 'share';
428
+        }
429
+
430
+        if ($ncPermissions & Constants::PERMISSION_READ) {
431
+            $ocmPermissions[] = 'read';
432
+        }
433
+
434
+        if (($ncPermissions & Constants::PERMISSION_CREATE) ||
435
+            ($ncPermissions & Constants::PERMISSION_UPDATE)) {
436
+            $ocmPermissions[] = 'write';
437
+        }
438
+
439
+        return $ocmPermissions;
440
+
441
+    }
442
+
443
+    /**
444
+     * Update ownCloud-specific properties
445
+     *
446
+     * @param string $path
447
+     * @param PropPatch $propPatch
448
+     *
449
+     * @return void
450
+     */
451
+    public function handleUpdateProperties($path, PropPatch $propPatch) {
452
+        $node = $this->tree->getNodeForPath($path);
453
+        if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) {
454
+            return;
455
+        }
456
+
457
+        $propPatch->handle(self::LASTMODIFIED_PROPERTYNAME, function($time) use ($node) {
458
+            if (empty($time)) {
459
+                return false;
460
+            }
461
+            $node->touch($time);
462
+            return true;
463
+        });
464
+        $propPatch->handle(self::GETETAG_PROPERTYNAME, function($etag) use ($node) {
465
+            if (empty($etag)) {
466
+                return false;
467
+            }
468
+            if ($node->setEtag($etag) !== -1) {
469
+                return true;
470
+            }
471
+            return false;
472
+        });
473
+    }
474
+
475
+    /**
476
+     * @param string $filePath
477
+     * @param \Sabre\DAV\INode $node
478
+     * @throws \Sabre\DAV\Exception\BadRequest
479
+     */
480
+    public function sendFileIdHeader($filePath, \Sabre\DAV\INode $node = null) {
481
+        // chunked upload handling
482
+        if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
483
+            list($path, $name) = \Sabre\Uri\split($filePath);
484
+            $info = \OC_FileChunking::decodeName($name);
485
+            if (!empty($info)) {
486
+                $filePath = $path . '/' . $info['name'];
487
+            }
488
+        }
489
+
490
+        // we get the node for the given $filePath here because in case of afterCreateFile $node is the parent folder
491
+        if (!$this->server->tree->nodeExists($filePath)) {
492
+            return;
493
+        }
494
+        $node = $this->server->tree->getNodeForPath($filePath);
495
+        if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
496
+            $fileId = $node->getFileId();
497
+            if (!is_null($fileId)) {
498
+                $this->server->httpResponse->setHeader('OC-FileId', $fileId);
499
+            }
500
+        }
501
+    }
502 502
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -173,7 +173,7 @@  discard block
 block discarded – undo
173 173
 		$this->server->on('propPatch', array($this, 'handleUpdateProperties'));
174 174
 		$this->server->on('afterBind', array($this, 'sendFileIdHeader'));
175 175
 		$this->server->on('afterWriteContent', array($this, 'sendFileIdHeader'));
176
-		$this->server->on('afterMethod:GET', [$this,'httpGet']);
176
+		$this->server->on('afterMethod:GET', [$this, 'httpGet']);
177 177
 		$this->server->on('afterMethod:GET', array($this, 'handleDownloadToken'));
178 178
 		$this->server->on('afterResponse', function($request, ResponseInterface $response) {
179 179
 			$body = $response->getBody();
@@ -203,11 +203,11 @@  discard block
 block discarded – undo
203 203
 		if ($sourceDir !== $destinationDir) {
204 204
 			$sourceNodeFileInfo = $sourceNode->getFileInfo();
205 205
 			if ($sourceNodeFileInfo === null) {
206
-				throw new NotFound($source . ' does not exist');
206
+				throw new NotFound($source.' does not exist');
207 207
  			}
208 208
 
209 209
 			if (!$sourceNodeFileInfo->isDeletable()) {
210
-				throw new Forbidden($source . " cannot be deleted");
210
+				throw new Forbidden($source." cannot be deleted");
211 211
 			}
212 212
 		}
213 213
 	}
@@ -260,10 +260,10 @@  discard block
 block discarded – undo
260 260
 					Request::USER_AGENT_ANDROID_MOBILE_CHROME,
261 261
 					Request::USER_AGENT_FREEBOX,
262 262
 				])) {
263
-				$response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"');
263
+				$response->addHeader('Content-Disposition', 'attachment; filename="'.rawurlencode($filename).'"');
264 264
 			} else {
265
-				$response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename)
266
-													 . '; filename="' . rawurlencode($filename) . '"');
265
+				$response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\''.rawurlencode($filename)
266
+													 . '; filename="'.rawurlencode($filename).'"');
267 267
 			}
268 268
 		}
269 269
 
@@ -352,13 +352,13 @@  discard block
 block discarded – undo
352 352
 				}
353 353
 			});
354 354
 
355
-			$propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function () use ($node) {
355
+			$propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function() use ($node) {
356 356
 				return json_encode($this->previewManager->isAvailable($node->getFileInfo()));
357 357
 			});
358 358
 			$propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) {
359 359
 				return $node->getSize();
360 360
 			});
361
-			$propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function () use ($node) {
361
+			$propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function() use ($node) {
362 362
 				return $node->getFileInfo()->getMountPoint()->getMountType();
363 363
 			});
364 364
 
@@ -483,7 +483,7 @@  discard block
 block discarded – undo
483 483
 			list($path, $name) = \Sabre\Uri\split($filePath);
484 484
 			$info = \OC_FileChunking::decodeName($name);
485 485
 			if (!empty($info)) {
486
-				$filePath = $path . '/' . $info['name'];
486
+				$filePath = $path.'/'.$info['name'];
487 487
 			}
488 488
 		}
489 489
 
Please login to merge, or discard this patch.
apps/dav/lib/Connector/Sabre/Node.php 2 patches
Indentation   +353 added lines, -353 removed lines patch added patch discarded remove patch
@@ -47,363 +47,363 @@
 block discarded – undo
47 47
 
48 48
 abstract class Node implements \Sabre\DAV\INode {
49 49
 
50
-	/**
51
-	 * @var \OC\Files\View
52
-	 */
53
-	protected $fileView;
54
-
55
-	/**
56
-	 * The path to the current node
57
-	 *
58
-	 * @var string
59
-	 */
60
-	protected $path;
61
-
62
-	/**
63
-	 * node properties cache
64
-	 *
65
-	 * @var array
66
-	 */
67
-	protected $property_cache = null;
68
-
69
-	/**
70
-	 * @var \OCP\Files\FileInfo
71
-	 */
72
-	protected $info;
73
-
74
-	/**
75
-	 * @var IManager
76
-	 */
77
-	protected $shareManager;
78
-
79
-	/**
80
-	 * Sets up the node, expects a full path name
81
-	 *
82
-	 * @param \OC\Files\View $view
83
-	 * @param \OCP\Files\FileInfo $info
84
-	 * @param IManager $shareManager
85
-	 */
86
-	public function __construct(View $view, FileInfo $info, IManager $shareManager = null) {
87
-		$this->fileView = $view;
88
-		$this->path = $this->fileView->getRelativePath($info->getPath());
89
-		$this->info = $info;
90
-		if ($shareManager) {
91
-			$this->shareManager = $shareManager;
92
-		} else {
93
-			$this->shareManager = \OC::$server->getShareManager();
94
-		}
95
-	}
96
-
97
-	protected function refreshInfo() {
98
-		$this->info = $this->fileView->getFileInfo($this->path);
99
-	}
100
-
101
-	/**
102
-	 *  Returns the name of the node
103
-	 *
104
-	 * @return string
105
-	 */
106
-	public function getName() {
107
-		return $this->info->getName();
108
-	}
109
-
110
-	/**
111
-	 * Returns the full path
112
-	 *
113
-	 * @return string
114
-	 */
115
-	public function getPath() {
116
-		return $this->path;
117
-	}
118
-
119
-	/**
120
-	 * Renames the node
121
-	 *
122
-	 * @param string $name The new name
123
-	 * @throws \Sabre\DAV\Exception\BadRequest
124
-	 * @throws \Sabre\DAV\Exception\Forbidden
125
-	 */
126
-	public function setName($name) {
127
-
128
-		// rename is only allowed if the update privilege is granted
129
-		if (!$this->info->isUpdateable()) {
130
-			throw new \Sabre\DAV\Exception\Forbidden();
131
-		}
132
-
133
-		list($parentPath,) = \Sabre\Uri\split($this->path);
134
-		list(, $newName) = \Sabre\Uri\split($name);
135
-
136
-		// verify path of the target
137
-		$this->verifyPath();
138
-
139
-		$newPath = $parentPath . '/' . $newName;
140
-
141
-		$this->fileView->rename($this->path, $newPath);
142
-
143
-		$this->path = $newPath;
144
-
145
-		$this->refreshInfo();
146
-	}
147
-
148
-	public function setPropertyCache($property_cache) {
149
-		$this->property_cache = $property_cache;
150
-	}
151
-
152
-	/**
153
-	 * Returns the last modification time, as a unix timestamp
154
-	 *
155
-	 * @return int timestamp as integer
156
-	 */
157
-	public function getLastModified() {
158
-		$timestamp = $this->info->getMtime();
159
-		if (!empty($timestamp)) {
160
-			return (int)$timestamp;
161
-		}
162
-		return $timestamp;
163
-	}
164
-
165
-	/**
166
-	 *  sets the last modification time of the file (mtime) to the value given
167
-	 *  in the second parameter or to now if the second param is empty.
168
-	 *  Even if the modification time is set to a custom value the access time is set to now.
169
-	 */
170
-	public function touch($mtime) {
171
-		$mtime = $this->sanitizeMtime($mtime);
172
-		$this->fileView->touch($this->path, $mtime);
173
-		$this->refreshInfo();
174
-	}
175
-
176
-	/**
177
-	 * Returns the ETag for a file
178
-	 *
179
-	 * An ETag is a unique identifier representing the current version of the
180
-	 * file. If the file changes, the ETag MUST change.  The ETag is an
181
-	 * arbitrary string, but MUST be surrounded by double-quotes.
182
-	 *
183
-	 * Return null if the ETag can not effectively be determined
184
-	 *
185
-	 * @return string
186
-	 */
187
-	public function getETag() {
188
-		return '"' . $this->info->getEtag() . '"';
189
-	}
190
-
191
-	/**
192
-	 * Sets the ETag
193
-	 *
194
-	 * @param string $etag
195
-	 *
196
-	 * @return int file id of updated file or -1 on failure
197
-	 */
198
-	public function setETag($etag) {
199
-		return $this->fileView->putFileInfo($this->path, array('etag' => $etag));
200
-	}
201
-
202
-	/**
203
-	 * Returns the size of the node, in bytes
204
-	 *
205
-	 * @return integer
206
-	 */
207
-	public function getSize() {
208
-		return $this->info->getSize();
209
-	}
210
-
211
-	/**
212
-	 * Returns the cache's file id
213
-	 *
214
-	 * @return int
215
-	 */
216
-	public function getId() {
217
-		return $this->info->getId();
218
-	}
219
-
220
-	/**
221
-	 * @return string|null
222
-	 */
223
-	public function getFileId() {
224
-		if ($this->info->getId()) {
225
-			$instanceId = \OC_Util::getInstanceId();
226
-			$id = sprintf('%08d', $this->info->getId());
227
-			return $id . $instanceId;
228
-		}
229
-
230
-		return null;
231
-	}
232
-
233
-	/**
234
-	 * @return integer
235
-	 */
236
-	public function getInternalFileId() {
237
-		return $this->info->getId();
238
-	}
239
-
240
-	/**
241
-	 * @param string $user
242
-	 * @return int
243
-	 */
244
-	public function getSharePermissions($user) {
245
-
246
-		// check of we access a federated share
247
-		if ($user !== null) {
248
-			try {
249
-				$share = $this->shareManager->getShareByToken($user);
250
-				return $share->getPermissions();
251
-			} catch (ShareNotFound $e) {
252
-				// ignore
253
-			}
254
-		}
255
-
256
-		try {
257
-			$storage = $this->info->getStorage();
258
-		} catch (StorageNotAvailableException $e) {
259
-			$storage = null;
260
-		}
261
-
262
-		if ($storage && $storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
263
-			/** @var \OCA\Files_Sharing\SharedStorage $storage */
264
-			$permissions = (int)$storage->getShare()->getPermissions();
265
-		} else {
266
-			$permissions = $this->info->getPermissions();
267
-		}
268
-
269
-		/*
50
+    /**
51
+     * @var \OC\Files\View
52
+     */
53
+    protected $fileView;
54
+
55
+    /**
56
+     * The path to the current node
57
+     *
58
+     * @var string
59
+     */
60
+    protected $path;
61
+
62
+    /**
63
+     * node properties cache
64
+     *
65
+     * @var array
66
+     */
67
+    protected $property_cache = null;
68
+
69
+    /**
70
+     * @var \OCP\Files\FileInfo
71
+     */
72
+    protected $info;
73
+
74
+    /**
75
+     * @var IManager
76
+     */
77
+    protected $shareManager;
78
+
79
+    /**
80
+     * Sets up the node, expects a full path name
81
+     *
82
+     * @param \OC\Files\View $view
83
+     * @param \OCP\Files\FileInfo $info
84
+     * @param IManager $shareManager
85
+     */
86
+    public function __construct(View $view, FileInfo $info, IManager $shareManager = null) {
87
+        $this->fileView = $view;
88
+        $this->path = $this->fileView->getRelativePath($info->getPath());
89
+        $this->info = $info;
90
+        if ($shareManager) {
91
+            $this->shareManager = $shareManager;
92
+        } else {
93
+            $this->shareManager = \OC::$server->getShareManager();
94
+        }
95
+    }
96
+
97
+    protected function refreshInfo() {
98
+        $this->info = $this->fileView->getFileInfo($this->path);
99
+    }
100
+
101
+    /**
102
+     *  Returns the name of the node
103
+     *
104
+     * @return string
105
+     */
106
+    public function getName() {
107
+        return $this->info->getName();
108
+    }
109
+
110
+    /**
111
+     * Returns the full path
112
+     *
113
+     * @return string
114
+     */
115
+    public function getPath() {
116
+        return $this->path;
117
+    }
118
+
119
+    /**
120
+     * Renames the node
121
+     *
122
+     * @param string $name The new name
123
+     * @throws \Sabre\DAV\Exception\BadRequest
124
+     * @throws \Sabre\DAV\Exception\Forbidden
125
+     */
126
+    public function setName($name) {
127
+
128
+        // rename is only allowed if the update privilege is granted
129
+        if (!$this->info->isUpdateable()) {
130
+            throw new \Sabre\DAV\Exception\Forbidden();
131
+        }
132
+
133
+        list($parentPath,) = \Sabre\Uri\split($this->path);
134
+        list(, $newName) = \Sabre\Uri\split($name);
135
+
136
+        // verify path of the target
137
+        $this->verifyPath();
138
+
139
+        $newPath = $parentPath . '/' . $newName;
140
+
141
+        $this->fileView->rename($this->path, $newPath);
142
+
143
+        $this->path = $newPath;
144
+
145
+        $this->refreshInfo();
146
+    }
147
+
148
+    public function setPropertyCache($property_cache) {
149
+        $this->property_cache = $property_cache;
150
+    }
151
+
152
+    /**
153
+     * Returns the last modification time, as a unix timestamp
154
+     *
155
+     * @return int timestamp as integer
156
+     */
157
+    public function getLastModified() {
158
+        $timestamp = $this->info->getMtime();
159
+        if (!empty($timestamp)) {
160
+            return (int)$timestamp;
161
+        }
162
+        return $timestamp;
163
+    }
164
+
165
+    /**
166
+     *  sets the last modification time of the file (mtime) to the value given
167
+     *  in the second parameter or to now if the second param is empty.
168
+     *  Even if the modification time is set to a custom value the access time is set to now.
169
+     */
170
+    public function touch($mtime) {
171
+        $mtime = $this->sanitizeMtime($mtime);
172
+        $this->fileView->touch($this->path, $mtime);
173
+        $this->refreshInfo();
174
+    }
175
+
176
+    /**
177
+     * Returns the ETag for a file
178
+     *
179
+     * An ETag is a unique identifier representing the current version of the
180
+     * file. If the file changes, the ETag MUST change.  The ETag is an
181
+     * arbitrary string, but MUST be surrounded by double-quotes.
182
+     *
183
+     * Return null if the ETag can not effectively be determined
184
+     *
185
+     * @return string
186
+     */
187
+    public function getETag() {
188
+        return '"' . $this->info->getEtag() . '"';
189
+    }
190
+
191
+    /**
192
+     * Sets the ETag
193
+     *
194
+     * @param string $etag
195
+     *
196
+     * @return int file id of updated file or -1 on failure
197
+     */
198
+    public function setETag($etag) {
199
+        return $this->fileView->putFileInfo($this->path, array('etag' => $etag));
200
+    }
201
+
202
+    /**
203
+     * Returns the size of the node, in bytes
204
+     *
205
+     * @return integer
206
+     */
207
+    public function getSize() {
208
+        return $this->info->getSize();
209
+    }
210
+
211
+    /**
212
+     * Returns the cache's file id
213
+     *
214
+     * @return int
215
+     */
216
+    public function getId() {
217
+        return $this->info->getId();
218
+    }
219
+
220
+    /**
221
+     * @return string|null
222
+     */
223
+    public function getFileId() {
224
+        if ($this->info->getId()) {
225
+            $instanceId = \OC_Util::getInstanceId();
226
+            $id = sprintf('%08d', $this->info->getId());
227
+            return $id . $instanceId;
228
+        }
229
+
230
+        return null;
231
+    }
232
+
233
+    /**
234
+     * @return integer
235
+     */
236
+    public function getInternalFileId() {
237
+        return $this->info->getId();
238
+    }
239
+
240
+    /**
241
+     * @param string $user
242
+     * @return int
243
+     */
244
+    public function getSharePermissions($user) {
245
+
246
+        // check of we access a federated share
247
+        if ($user !== null) {
248
+            try {
249
+                $share = $this->shareManager->getShareByToken($user);
250
+                return $share->getPermissions();
251
+            } catch (ShareNotFound $e) {
252
+                // ignore
253
+            }
254
+        }
255
+
256
+        try {
257
+            $storage = $this->info->getStorage();
258
+        } catch (StorageNotAvailableException $e) {
259
+            $storage = null;
260
+        }
261
+
262
+        if ($storage && $storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
263
+            /** @var \OCA\Files_Sharing\SharedStorage $storage */
264
+            $permissions = (int)$storage->getShare()->getPermissions();
265
+        } else {
266
+            $permissions = $this->info->getPermissions();
267
+        }
268
+
269
+        /*
270 270
 		 * We can always share non moveable mount points with DELETE and UPDATE
271 271
 		 * Eventually we need to do this properly
272 272
 		 */
273
-		$mountpoint = $this->info->getMountPoint();
274
-		if (!($mountpoint instanceof MoveableMount)) {
275
-			$mountpointpath = $mountpoint->getMountPoint();
276
-			if (substr($mountpointpath, -1) === '/') {
277
-				$mountpointpath = substr($mountpointpath, 0, -1);
278
-			}
279
-
280
-			if (!$mountpoint->getOption('readonly', false) && $mountpointpath === $this->info->getPath()) {
281
-				$permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE;
282
-			}
283
-		}
284
-
285
-		/*
273
+        $mountpoint = $this->info->getMountPoint();
274
+        if (!($mountpoint instanceof MoveableMount)) {
275
+            $mountpointpath = $mountpoint->getMountPoint();
276
+            if (substr($mountpointpath, -1) === '/') {
277
+                $mountpointpath = substr($mountpointpath, 0, -1);
278
+            }
279
+
280
+            if (!$mountpoint->getOption('readonly', false) && $mountpointpath === $this->info->getPath()) {
281
+                $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE;
282
+            }
283
+        }
284
+
285
+        /*
286 286
 		 * Files can't have create or delete permissions
287 287
 		 */
288
-		if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
289
-			$permissions &= ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE);
290
-		}
291
-
292
-		return $permissions;
293
-	}
294
-
295
-	/**
296
-	 * @param string $user
297
-	 * @return string
298
-	 */
299
-	public function getNoteFromShare($user) {
300
-		if ($user === null) {
301
-			return '';
302
-		}
303
-
304
-		$types = [
305
-			Share::SHARE_TYPE_USER,
306
-			Share::SHARE_TYPE_GROUP,
307
-			Share::SHARE_TYPE_CIRCLE,
308
-			Share::SHARE_TYPE_ROOM
309
-		];
310
-
311
-		foreach ($types as $shareType) {
312
-			$shares = $this->shareManager->getSharedWith($user, $shareType, $this, -1);
313
-			foreach ($shares as $share) {
314
-				$note = $share->getNote();
315
-				if($share->getShareOwner() !== $user && !empty($note)) {
316
-					return $note;
317
-				}
318
-			}
319
-		}
320
-
321
-		return '';
322
-	}
323
-
324
-	/**
325
-	 * @return string
326
-	 */
327
-	public function getDavPermissions() {
328
-		$p = '';
329
-		if ($this->info->isShared()) {
330
-			$p .= 'S';
331
-		}
332
-		if ($this->info->isShareable()) {
333
-			$p .= 'R';
334
-		}
335
-		if ($this->info->isMounted()) {
336
-			$p .= 'M';
337
-		}
338
-		if ($this->info->isReadable()) {
339
-			$p .= 'G';
340
-		}
341
-		if ($this->info->isDeletable()) {
342
-			$p .= 'D';
343
-		}
344
-		if ($this->info->isUpdateable()) {
345
-			$p .= 'NV'; // Renameable, Moveable
346
-		}
347
-		if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
348
-			if ($this->info->isUpdateable()) {
349
-				$p .= 'W';
350
-			}
351
-		} else {
352
-			if ($this->info->isCreatable()) {
353
-				$p .= 'CK';
354
-			}
355
-		}
356
-		return $p;
357
-	}
358
-
359
-	public function getOwner() {
360
-		return $this->info->getOwner();
361
-	}
362
-
363
-	protected function verifyPath() {
364
-		try {
365
-			$fileName = basename($this->info->getPath());
366
-			$this->fileView->verifyPath($this->path, $fileName);
367
-		} catch (\OCP\Files\InvalidPathException $ex) {
368
-			throw new InvalidPath($ex->getMessage());
369
-		}
370
-	}
371
-
372
-	/**
373
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
374
-	 */
375
-	public function acquireLock($type) {
376
-		$this->fileView->lockFile($this->path, $type);
377
-	}
378
-
379
-	/**
380
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
381
-	 */
382
-	public function releaseLock($type) {
383
-		$this->fileView->unlockFile($this->path, $type);
384
-	}
385
-
386
-	/**
387
-	 * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
388
-	 */
389
-	public function changeLock($type) {
390
-		$this->fileView->changeLock($this->path, $type);
391
-	}
392
-
393
-	public function getFileInfo() {
394
-		return $this->info;
395
-	}
396
-
397
-	protected function sanitizeMtime($mtimeFromRequest) {
398
-		// In PHP 5.X "is_numeric" returns true for strings in hexadecimal
399
-		// notation. This is no longer the case in PHP 7.X, so this check
400
-		// ensures that strings with hexadecimal notations fail too in PHP 5.X.
401
-		$isHexadecimal = is_string($mtimeFromRequest) && preg_match('/^\s*0[xX]/', $mtimeFromRequest);
402
-		if ($isHexadecimal || !is_numeric($mtimeFromRequest)) {
403
-			throw new \InvalidArgumentException('X-OC-MTime header must be an integer (unix timestamp).');
404
-		}
405
-
406
-		return (int)$mtimeFromRequest;
407
-	}
288
+        if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
289
+            $permissions &= ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE);
290
+        }
291
+
292
+        return $permissions;
293
+    }
294
+
295
+    /**
296
+     * @param string $user
297
+     * @return string
298
+     */
299
+    public function getNoteFromShare($user) {
300
+        if ($user === null) {
301
+            return '';
302
+        }
303
+
304
+        $types = [
305
+            Share::SHARE_TYPE_USER,
306
+            Share::SHARE_TYPE_GROUP,
307
+            Share::SHARE_TYPE_CIRCLE,
308
+            Share::SHARE_TYPE_ROOM
309
+        ];
310
+
311
+        foreach ($types as $shareType) {
312
+            $shares = $this->shareManager->getSharedWith($user, $shareType, $this, -1);
313
+            foreach ($shares as $share) {
314
+                $note = $share->getNote();
315
+                if($share->getShareOwner() !== $user && !empty($note)) {
316
+                    return $note;
317
+                }
318
+            }
319
+        }
320
+
321
+        return '';
322
+    }
323
+
324
+    /**
325
+     * @return string
326
+     */
327
+    public function getDavPermissions() {
328
+        $p = '';
329
+        if ($this->info->isShared()) {
330
+            $p .= 'S';
331
+        }
332
+        if ($this->info->isShareable()) {
333
+            $p .= 'R';
334
+        }
335
+        if ($this->info->isMounted()) {
336
+            $p .= 'M';
337
+        }
338
+        if ($this->info->isReadable()) {
339
+            $p .= 'G';
340
+        }
341
+        if ($this->info->isDeletable()) {
342
+            $p .= 'D';
343
+        }
344
+        if ($this->info->isUpdateable()) {
345
+            $p .= 'NV'; // Renameable, Moveable
346
+        }
347
+        if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
348
+            if ($this->info->isUpdateable()) {
349
+                $p .= 'W';
350
+            }
351
+        } else {
352
+            if ($this->info->isCreatable()) {
353
+                $p .= 'CK';
354
+            }
355
+        }
356
+        return $p;
357
+    }
358
+
359
+    public function getOwner() {
360
+        return $this->info->getOwner();
361
+    }
362
+
363
+    protected function verifyPath() {
364
+        try {
365
+            $fileName = basename($this->info->getPath());
366
+            $this->fileView->verifyPath($this->path, $fileName);
367
+        } catch (\OCP\Files\InvalidPathException $ex) {
368
+            throw new InvalidPath($ex->getMessage());
369
+        }
370
+    }
371
+
372
+    /**
373
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
374
+     */
375
+    public function acquireLock($type) {
376
+        $this->fileView->lockFile($this->path, $type);
377
+    }
378
+
379
+    /**
380
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
381
+     */
382
+    public function releaseLock($type) {
383
+        $this->fileView->unlockFile($this->path, $type);
384
+    }
385
+
386
+    /**
387
+     * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
388
+     */
389
+    public function changeLock($type) {
390
+        $this->fileView->changeLock($this->path, $type);
391
+    }
392
+
393
+    public function getFileInfo() {
394
+        return $this->info;
395
+    }
396
+
397
+    protected function sanitizeMtime($mtimeFromRequest) {
398
+        // In PHP 5.X "is_numeric" returns true for strings in hexadecimal
399
+        // notation. This is no longer the case in PHP 7.X, so this check
400
+        // ensures that strings with hexadecimal notations fail too in PHP 5.X.
401
+        $isHexadecimal = is_string($mtimeFromRequest) && preg_match('/^\s*0[xX]/', $mtimeFromRequest);
402
+        if ($isHexadecimal || !is_numeric($mtimeFromRequest)) {
403
+            throw new \InvalidArgumentException('X-OC-MTime header must be an integer (unix timestamp).');
404
+        }
405
+
406
+        return (int)$mtimeFromRequest;
407
+    }
408 408
 
409 409
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -136,7 +136,7 @@  discard block
 block discarded – undo
136 136
 		// verify path of the target
137 137
 		$this->verifyPath();
138 138
 
139
-		$newPath = $parentPath . '/' . $newName;
139
+		$newPath = $parentPath.'/'.$newName;
140 140
 
141 141
 		$this->fileView->rename($this->path, $newPath);
142 142
 
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
 	public function getLastModified() {
158 158
 		$timestamp = $this->info->getMtime();
159 159
 		if (!empty($timestamp)) {
160
-			return (int)$timestamp;
160
+			return (int) $timestamp;
161 161
 		}
162 162
 		return $timestamp;
163 163
 	}
@@ -185,7 +185,7 @@  discard block
 block discarded – undo
185 185
 	 * @return string
186 186
 	 */
187 187
 	public function getETag() {
188
-		return '"' . $this->info->getEtag() . '"';
188
+		return '"'.$this->info->getEtag().'"';
189 189
 	}
190 190
 
191 191
 	/**
@@ -224,7 +224,7 @@  discard block
 block discarded – undo
224 224
 		if ($this->info->getId()) {
225 225
 			$instanceId = \OC_Util::getInstanceId();
226 226
 			$id = sprintf('%08d', $this->info->getId());
227
-			return $id . $instanceId;
227
+			return $id.$instanceId;
228 228
 		}
229 229
 
230 230
 		return null;
@@ -261,7 +261,7 @@  discard block
 block discarded – undo
261 261
 
262 262
 		if ($storage && $storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
263 263
 			/** @var \OCA\Files_Sharing\SharedStorage $storage */
264
-			$permissions = (int)$storage->getShare()->getPermissions();
264
+			$permissions = (int) $storage->getShare()->getPermissions();
265 265
 		} else {
266 266
 			$permissions = $this->info->getPermissions();
267 267
 		}
@@ -312,7 +312,7 @@  discard block
 block discarded – undo
312 312
 			$shares = $this->shareManager->getSharedWith($user, $shareType, $this, -1);
313 313
 			foreach ($shares as $share) {
314 314
 				$note = $share->getNote();
315
-				if($share->getShareOwner() !== $user && !empty($note)) {
315
+				if ($share->getShareOwner() !== $user && !empty($note)) {
316 316
 					return $note;
317 317
 				}
318 318
 			}
@@ -403,7 +403,7 @@  discard block
 block discarded – undo
403 403
 			throw new \InvalidArgumentException('X-OC-MTime header must be an integer (unix timestamp).');
404 404
 		}
405 405
 
406
-		return (int)$mtimeFromRequest;
406
+		return (int) $mtimeFromRequest;
407 407
 	}
408 408
 
409 409
 }
Please login to merge, or discard this patch.