Node.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Jakob Sack <mail@jakobsack.de>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Klaas Freitag <freitag@owncloud.com>
  11. * @author Markus Goetz <markus@woboq.com>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. * @author Vincent Petry <pvince81@owncloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OCA\DAV\Connector\Sabre;
  34. use OC\Files\Mount\MoveableMount;
  35. use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
  36. use OCP\Share\Exceptions\ShareNotFound;
  37. use OCP\Share\IManager;
  38. abstract class Node implements \Sabre\DAV\INode {
  39. /**
  40. * @var \OC\Files\View
  41. */
  42. protected $fileView;
  43. /**
  44. * The path to the current node
  45. *
  46. * @var string
  47. */
  48. protected $path;
  49. /**
  50. * node properties cache
  51. *
  52. * @var array
  53. */
  54. protected $property_cache = null;
  55. /**
  56. * @var \OCP\Files\FileInfo
  57. */
  58. protected $info;
  59. /**
  60. * @var IManager
  61. */
  62. protected $shareManager;
  63. /**
  64. * Sets up the node, expects a full path name
  65. *
  66. * @param \OC\Files\View $view
  67. * @param \OCP\Files\FileInfo $info
  68. * @param IManager $shareManager
  69. */
  70. public function __construct($view, $info, IManager $shareManager = null) {
  71. $this->fileView = $view;
  72. $this->path = $this->fileView->getRelativePath($info->getPath());
  73. $this->info = $info;
  74. if ($shareManager) {
  75. $this->shareManager = $shareManager;
  76. } else {
  77. $this->shareManager = \OC::$server->getShareManager();
  78. }
  79. }
  80. protected function refreshInfo() {
  81. $this->info = $this->fileView->getFileInfo($this->path);
  82. }
  83. /**
  84. * Returns the name of the node
  85. *
  86. * @return string
  87. */
  88. public function getName() {
  89. return $this->info->getName();
  90. }
  91. /**
  92. * Returns the full path
  93. *
  94. * @return string
  95. */
  96. public function getPath() {
  97. return $this->path;
  98. }
  99. /**
  100. * Renames the node
  101. *
  102. * @param string $name The new name
  103. * @throws \Sabre\DAV\Exception\BadRequest
  104. * @throws \Sabre\DAV\Exception\Forbidden
  105. */
  106. public function setName($name) {
  107. // rename is only allowed if the update privilege is granted
  108. if (!$this->info->isUpdateable()) {
  109. throw new \Sabre\DAV\Exception\Forbidden();
  110. }
  111. list($parentPath,) = \Sabre\HTTP\URLUtil::splitPath($this->path);
  112. list(, $newName) = \Sabre\HTTP\URLUtil::splitPath($name);
  113. // verify path of the target
  114. $this->verifyPath();
  115. $newPath = $parentPath . '/' . $newName;
  116. $this->fileView->rename($this->path, $newPath);
  117. $this->path = $newPath;
  118. $this->refreshInfo();
  119. }
  120. public function setPropertyCache($property_cache) {
  121. $this->property_cache = $property_cache;
  122. }
  123. /**
  124. * Returns the last modification time, as a unix timestamp
  125. *
  126. * @return int timestamp as integer
  127. */
  128. public function getLastModified() {
  129. $timestamp = $this->info->getMtime();
  130. if (!empty($timestamp)) {
  131. return (int)$timestamp;
  132. }
  133. return $timestamp;
  134. }
  135. /**
  136. * sets the last modification time of the file (mtime) to the value given
  137. * in the second parameter or to now if the second param is empty.
  138. * Even if the modification time is set to a custom value the access time is set to now.
  139. */
  140. public function touch($mtime) {
  141. $this->fileView->touch($this->path, $mtime);
  142. $this->refreshInfo();
  143. }
  144. /**
  145. * Returns the ETag for a file
  146. *
  147. * An ETag is a unique identifier representing the current version of the
  148. * file. If the file changes, the ETag MUST change. The ETag is an
  149. * arbitrary string, but MUST be surrounded by double-quotes.
  150. *
  151. * Return null if the ETag can not effectively be determined
  152. *
  153. * @return string
  154. */
  155. public function getETag() {
  156. return '"' . $this->info->getEtag() . '"';
  157. }
  158. /**
  159. * Sets the ETag
  160. *
  161. * @param string $etag
  162. *
  163. * @return int file id of updated file or -1 on failure
  164. */
  165. public function setETag($etag) {
  166. return $this->fileView->putFileInfo($this->path, array('etag' => $etag));
  167. }
  168. /**
  169. * Returns the size of the node, in bytes
  170. *
  171. * @return integer
  172. */
  173. public function getSize() {
  174. return $this->info->getSize();
  175. }
  176. /**
  177. * Returns the cache's file id
  178. *
  179. * @return int
  180. */
  181. public function getId() {
  182. return $this->info->getId();
  183. }
  184. /**
  185. * @return string|null
  186. */
  187. public function getFileId() {
  188. if ($this->info->getId()) {
  189. $instanceId = \OC_Util::getInstanceId();
  190. $id = sprintf('%08d', $this->info->getId());
  191. return $id . $instanceId;
  192. }
  193. return null;
  194. }
  195. /**
  196. * @return integer
  197. */
  198. public function getInternalFileId() {
  199. return $this->info->getId();
  200. }
  201. /**
  202. * @param string $user
  203. * @return int
  204. */
  205. public function getSharePermissions($user) {
  206. // check of we access a federated share
  207. if ($user !== null) {
  208. try {
  209. $share = $this->shareManager->getShareByToken($user);
  210. return $share->getPermissions();
  211. } catch (ShareNotFound $e) {
  212. // ignore
  213. }
  214. }
  215. $storage = $this->info->getStorage();
  216. $path = $this->info->getInternalPath();
  217. if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
  218. /** @var \OCA\Files_Sharing\SharedStorage $storage */
  219. $permissions = (int)$storage->getShare()->getPermissions();
  220. } else {
  221. $permissions = $storage->getPermissions($path);
  222. }
  223. /*
  224. * We can always share non moveable mount points with DELETE and UPDATE
  225. * Eventually we need to do this properly
  226. */
  227. $mountpoint = $this->info->getMountPoint();
  228. if (!($mountpoint instanceof MoveableMount)) {
  229. $mountpointpath = $mountpoint->getMountPoint();
  230. if (substr($mountpointpath, -1) === '/') {
  231. $mountpointpath = substr($mountpointpath, 0, -1);
  232. }
  233. if ($mountpointpath === $this->info->getPath()) {
  234. $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE;
  235. }
  236. }
  237. /*
  238. * Files can't have create or delete permissions
  239. */
  240. if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
  241. $permissions &= ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE);
  242. }
  243. return $permissions;
  244. }
  245. /**
  246. * @return string
  247. */
  248. public function getDavPermissions() {
  249. $p = '';
  250. if ($this->info->isShared()) {
  251. $p .= 'S';
  252. }
  253. if ($this->info->isShareable()) {
  254. $p .= 'R';
  255. }
  256. if ($this->info->isMounted()) {
  257. $p .= 'M';
  258. }
  259. if ($this->info->isDeletable()) {
  260. $p .= 'D';
  261. }
  262. if ($this->info->isUpdateable()) {
  263. $p .= 'NV'; // Renameable, Moveable
  264. }
  265. if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
  266. if ($this->info->isUpdateable()) {
  267. $p .= 'W';
  268. }
  269. } else {
  270. if ($this->info->isCreatable()) {
  271. $p .= 'CK';
  272. }
  273. }
  274. return $p;
  275. }
  276. public function getOwner() {
  277. return $this->info->getOwner();
  278. }
  279. protected function verifyPath() {
  280. try {
  281. $fileName = basename($this->info->getPath());
  282. $this->fileView->verifyPath($this->path, $fileName);
  283. } catch (\OCP\Files\InvalidPathException $ex) {
  284. throw new InvalidPath($ex->getMessage());
  285. }
  286. }
  287. /**
  288. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  289. */
  290. public function acquireLock($type) {
  291. $this->fileView->lockFile($this->path, $type);
  292. }
  293. /**
  294. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  295. */
  296. public function releaseLock($type) {
  297. $this->fileView->unlockFile($this->path, $type);
  298. }
  299. /**
  300. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  301. */
  302. public function changeLock($type) {
  303. $this->fileView->changeLock($this->path, $type);
  304. }
  305. public function getFileInfo() {
  306. return $this->info;
  307. }
  308. }