filesplugin.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Robin Appelman <icewind@owncloud.com>
  6. * @author Robin McCorkell <robin@mccorkell.me.uk>
  7. * @author Roeland Jago Douma <rullzer@owncloud.com>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2016, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\Connector\Sabre;
  28. use Sabre\DAV\IFile;
  29. use \Sabre\DAV\PropFind;
  30. use \Sabre\DAV\PropPatch;
  31. use \Sabre\HTTP\RequestInterface;
  32. use \Sabre\HTTP\ResponseInterface;
  33. use OCP\Files\StorageNotAvailableException;
  34. class FilesPlugin extends \Sabre\DAV\ServerPlugin {
  35. // namespace
  36. const NS_OWNCLOUD = 'http://owncloud.org/ns';
  37. const FILEID_PROPERTYNAME = '{http://owncloud.org/ns}id';
  38. const INTERNAL_FILEID_PROPERTYNAME = '{http://owncloud.org/ns}fileid';
  39. const PERMISSIONS_PROPERTYNAME = '{http://owncloud.org/ns}permissions';
  40. const DOWNLOADURL_PROPERTYNAME = '{http://owncloud.org/ns}downloadURL';
  41. const SIZE_PROPERTYNAME = '{http://owncloud.org/ns}size';
  42. const GETETAG_PROPERTYNAME = '{DAV:}getetag';
  43. const LASTMODIFIED_PROPERTYNAME = '{DAV:}lastmodified';
  44. const OWNER_ID_PROPERTYNAME = '{http://owncloud.org/ns}owner-id';
  45. const OWNER_DISPLAY_NAME_PROPERTYNAME = '{http://owncloud.org/ns}owner-display-name';
  46. const CHECKSUMS_PROPERTYNAME = '{http://owncloud.org/ns}checksums';
  47. /**
  48. * Reference to main server object
  49. *
  50. * @var \Sabre\DAV\Server
  51. */
  52. private $server;
  53. /**
  54. * @var \Sabre\DAV\Tree
  55. */
  56. private $tree;
  57. /**
  58. * Whether this is public webdav.
  59. * If true, some returned information will be stripped off.
  60. *
  61. * @var bool
  62. */
  63. private $isPublic;
  64. /**
  65. * @var \OC\Files\View
  66. */
  67. private $fileView;
  68. /**
  69. * @param \Sabre\DAV\Tree $tree
  70. * @param \OC\Files\View $view
  71. * @param bool $isPublic
  72. */
  73. public function __construct(\Sabre\DAV\Tree $tree,
  74. \OC\Files\View $view,
  75. $isPublic = false) {
  76. $this->tree = $tree;
  77. $this->fileView = $view;
  78. $this->isPublic = $isPublic;
  79. }
  80. /**
  81. * This initializes the plugin.
  82. *
  83. * This function is called by \Sabre\DAV\Server, after
  84. * addPlugin is called.
  85. *
  86. * This method should set up the required event subscriptions.
  87. *
  88. * @param \Sabre\DAV\Server $server
  89. * @return void
  90. */
  91. public function initialize(\Sabre\DAV\Server $server) {
  92. $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
  93. $server->protectedProperties[] = self::FILEID_PROPERTYNAME;
  94. $server->protectedProperties[] = self::INTERNAL_FILEID_PROPERTYNAME;
  95. $server->protectedProperties[] = self::PERMISSIONS_PROPERTYNAME;
  96. $server->protectedProperties[] = self::SIZE_PROPERTYNAME;
  97. $server->protectedProperties[] = self::DOWNLOADURL_PROPERTYNAME;
  98. $server->protectedProperties[] = self::OWNER_ID_PROPERTYNAME;
  99. $server->protectedProperties[] = self::OWNER_DISPLAY_NAME_PROPERTYNAME;
  100. $server->protectedProperties[] = self::CHECKSUMS_PROPERTYNAME;
  101. // normally these cannot be changed (RFC4918), but we want them modifiable through PROPPATCH
  102. $allowedProperties = ['{DAV:}getetag'];
  103. $server->protectedProperties = array_diff($server->protectedProperties, $allowedProperties);
  104. $this->server = $server;
  105. $this->server->on('propFind', array($this, 'handleGetProperties'));
  106. $this->server->on('propPatch', array($this, 'handleUpdateProperties'));
  107. $this->server->on('afterBind', array($this, 'sendFileIdHeader'));
  108. $this->server->on('afterWriteContent', array($this, 'sendFileIdHeader'));
  109. $this->server->on('afterMethod:GET', [$this,'httpGet']);
  110. $this->server->on('afterMethod:GET', array($this, 'handleDownloadToken'));
  111. $this->server->on('afterResponse', function($request, ResponseInterface $response) {
  112. $body = $response->getBody();
  113. if (is_resource($body)) {
  114. fclose($body);
  115. }
  116. });
  117. $this->server->on('beforeMove', [$this, 'checkMove']);
  118. }
  119. /**
  120. * Plugin that checks if a move can actually be performed.
  121. * @param string $source source path
  122. * @param string $destination destination path
  123. * @throws \Sabre\DAV\Exception\Forbidden
  124. */
  125. function checkMove($source, $destination) {
  126. list($sourceDir,) = \Sabre\HTTP\URLUtil::splitPath($source);
  127. list($destinationDir,) = \Sabre\HTTP\URLUtil::splitPath($destination);
  128. if ($sourceDir !== $destinationDir) {
  129. $sourceFileInfo = $this->fileView->getFileInfo($source);
  130. if ($sourceFileInfo === false) {
  131. throw new \Sabre\DAV\Exception\NotFound($source . ' does not exist');
  132. }
  133. if (!$sourceFileInfo->isDeletable()) {
  134. throw new \Sabre\DAV\Exception\Forbidden($source . " cannot be deleted");
  135. }
  136. }
  137. }
  138. /**
  139. * This sets a cookie to be able to recognize the start of the download
  140. * the content must not be longer than 32 characters and must only contain
  141. * alphanumeric characters
  142. *
  143. * @param RequestInterface $request
  144. * @param ResponseInterface $response
  145. */
  146. function handleDownloadToken(RequestInterface $request, ResponseInterface $response) {
  147. $queryParams = $request->getQueryParameters();
  148. /**
  149. * this sets a cookie to be able to recognize the start of the download
  150. * the content must not be longer than 32 characters and must only contain
  151. * alphanumeric characters
  152. */
  153. if (isset($queryParams['downloadStartSecret'])) {
  154. $token = $queryParams['downloadStartSecret'];
  155. if (!isset($token[32])
  156. && preg_match('!^[a-zA-Z0-9]+$!', $token) === 1) {
  157. // FIXME: use $response->setHeader() instead
  158. setcookie('ocDownloadStarted', $token, time() + 20, '/');
  159. }
  160. }
  161. }
  162. /**
  163. * Add headers to file download
  164. *
  165. * @param RequestInterface $request
  166. * @param ResponseInterface $response
  167. */
  168. function httpGet(RequestInterface $request, ResponseInterface $response) {
  169. // Only handle valid files
  170. $node = $this->tree->getNodeForPath($request->getPath());
  171. if (!($node instanceof IFile)) return;
  172. // adds a 'Content-Disposition: attachment' header
  173. $response->addHeader('Content-Disposition', 'attachment');
  174. //Add OC-Checksum header
  175. /** @var $node File */
  176. $checksum = $node->getChecksum();
  177. if ($checksum !== null) {
  178. $response->addHeader('OC-Checksum', $checksum);
  179. }
  180. }
  181. /**
  182. * Adds all ownCloud-specific properties
  183. *
  184. * @param PropFind $propFind
  185. * @param \Sabre\DAV\INode $node
  186. * @return void
  187. */
  188. public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) {
  189. if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
  190. $propFind->handle(self::FILEID_PROPERTYNAME, function() use ($node) {
  191. return $node->getFileId();
  192. });
  193. $propFind->handle(self::INTERNAL_FILEID_PROPERTYNAME, function() use ($node) {
  194. return $node->getInternalFileId();
  195. });
  196. $propFind->handle(self::PERMISSIONS_PROPERTYNAME, function() use ($node) {
  197. $perms = $node->getDavPermissions();
  198. if ($this->isPublic) {
  199. // remove mount information
  200. $perms = str_replace(['S', 'M'], '', $perms);
  201. }
  202. return $perms;
  203. });
  204. $propFind->handle(self::GETETAG_PROPERTYNAME, function() use ($node) {
  205. return $node->getEtag();
  206. });
  207. }
  208. if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
  209. $propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function() use ($node) {
  210. /** @var $node \OCA\DAV\Connector\Sabre\File */
  211. try {
  212. $directDownloadUrl = $node->getDirectDownload();
  213. if (isset($directDownloadUrl['url'])) {
  214. return $directDownloadUrl['url'];
  215. }
  216. } catch (StorageNotAvailableException $e) {
  217. return false;
  218. }
  219. return false;
  220. });
  221. $propFind->handle(self::CHECKSUMS_PROPERTYNAME, function() use ($node) {
  222. $checksum = $node->getChecksum();
  223. return new ChecksumList($checksum);
  224. });
  225. }
  226. if ($node instanceof \OCA\DAV\Connector\Sabre\Directory) {
  227. $propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) {
  228. return $node->getSize();
  229. });
  230. }
  231. $propFind->handle(self::OWNER_ID_PROPERTYNAME, function() use ($node) {
  232. $owner = $node->getOwner();
  233. return $owner->getUID();
  234. });
  235. $propFind->handle(self::OWNER_DISPLAY_NAME_PROPERTYNAME, function() use ($node) {
  236. $owner = $node->getOwner();
  237. $displayName = $owner->getDisplayName();
  238. return $displayName;
  239. });
  240. }
  241. /**
  242. * Update ownCloud-specific properties
  243. *
  244. * @param string $path
  245. * @param PropPatch $propPatch
  246. *
  247. * @return void
  248. */
  249. public function handleUpdateProperties($path, PropPatch $propPatch) {
  250. $propPatch->handle(self::LASTMODIFIED_PROPERTYNAME, function($time) use ($path) {
  251. if (empty($time)) {
  252. return false;
  253. }
  254. $node = $this->tree->getNodeForPath($path);
  255. if (is_null($node)) {
  256. return 404;
  257. }
  258. $node->touch($time);
  259. return true;
  260. });
  261. $propPatch->handle(self::GETETAG_PROPERTYNAME, function($etag) use ($path) {
  262. if (empty($etag)) {
  263. return false;
  264. }
  265. $node = $this->tree->getNodeForPath($path);
  266. if (is_null($node)) {
  267. return 404;
  268. }
  269. if ($node->setEtag($etag) !== -1) {
  270. return true;
  271. }
  272. return false;
  273. });
  274. }
  275. /**
  276. * @param string $filePath
  277. * @param \Sabre\DAV\INode $node
  278. * @throws \Sabre\DAV\Exception\BadRequest
  279. */
  280. public function sendFileIdHeader($filePath, \Sabre\DAV\INode $node = null) {
  281. // chunked upload handling
  282. if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
  283. list($path, $name) = \Sabre\HTTP\URLUtil::splitPath($filePath);
  284. $info = \OC_FileChunking::decodeName($name);
  285. if (!empty($info)) {
  286. $filePath = $path . '/' . $info['name'];
  287. }
  288. }
  289. // we get the node for the given $filePath here because in case of afterCreateFile $node is the parent folder
  290. if (!$this->server->tree->nodeExists($filePath)) {
  291. return;
  292. }
  293. $node = $this->server->tree->getNodeForPath($filePath);
  294. if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
  295. $fileId = $node->getFileId();
  296. if (!is_null($fileId)) {
  297. $this->server->httpResponse->setHeader('OC-FileId', $fileId);
  298. }
  299. }
  300. }
  301. }