Share.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author John Molakvoæ <skjnldsv@protonmail.com>
  11. * @author Maxence Lange <maxence@nextcloud.com>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Share20;
  31. use OCP\Files\Cache\ICacheEntry;
  32. use OCP\Files\File;
  33. use OCP\Files\FileInfo;
  34. use OCP\Files\IRootFolder;
  35. use OCP\Files\Node;
  36. use OCP\Files\NotFoundException;
  37. use OCP\IUserManager;
  38. use OCP\Share\Exceptions\IllegalIDChangeException;
  39. use OCP\Share\IAttributes;
  40. use OCP\Share\IShare;
  41. class Share implements IShare {
  42. /** @var string */
  43. private $id;
  44. /** @var string */
  45. private $providerId;
  46. /** @var Node */
  47. private $node;
  48. /** @var int */
  49. private $fileId;
  50. /** @var string */
  51. private $nodeType;
  52. /** @var int */
  53. private $shareType;
  54. /** @var string */
  55. private $sharedWith;
  56. /** @var string */
  57. private $sharedWithDisplayName;
  58. /** @var string */
  59. private $sharedWithAvatar;
  60. /** @var string */
  61. private $sharedBy;
  62. /** @var string */
  63. private $shareOwner;
  64. /** @var int */
  65. private $permissions;
  66. /** @var IAttributes */
  67. private $attributes;
  68. /** @var int */
  69. private $status;
  70. /** @var string */
  71. private $note = '';
  72. /** @var \DateTime */
  73. private $expireDate;
  74. /** @var string */
  75. private $password;
  76. private ?\DateTimeInterface $passwordExpirationTime = null;
  77. /** @var bool */
  78. private $sendPasswordByTalk = false;
  79. /** @var string */
  80. private $token;
  81. /** @var int */
  82. private $parent;
  83. /** @var string */
  84. private $target;
  85. /** @var \DateTime */
  86. private $shareTime;
  87. /** @var bool */
  88. private $mailSend;
  89. /** @var string */
  90. private $label = '';
  91. /** @var IRootFolder */
  92. private $rootFolder;
  93. /** @var IUserManager */
  94. private $userManager;
  95. /** @var ICacheEntry|null */
  96. private $nodeCacheEntry;
  97. /** @var bool */
  98. private $hideDownload = false;
  99. public function __construct(IRootFolder $rootFolder, IUserManager $userManager) {
  100. $this->rootFolder = $rootFolder;
  101. $this->userManager = $userManager;
  102. }
  103. /**
  104. * @inheritdoc
  105. */
  106. public function setId($id) {
  107. if (is_int($id)) {
  108. $id = (string)$id;
  109. }
  110. if (!is_string($id)) {
  111. throw new \InvalidArgumentException('String expected.');
  112. }
  113. if ($this->id !== null) {
  114. throw new IllegalIDChangeException('Not allowed to assign a new internal id to a share');
  115. }
  116. $this->id = trim($id);
  117. return $this;
  118. }
  119. /**
  120. * @inheritdoc
  121. */
  122. public function getId() {
  123. return $this->id;
  124. }
  125. /**
  126. * @inheritdoc
  127. */
  128. public function getFullId() {
  129. if ($this->providerId === null || $this->id === null) {
  130. throw new \UnexpectedValueException;
  131. }
  132. return $this->providerId . ':' . $this->id;
  133. }
  134. /**
  135. * @inheritdoc
  136. */
  137. public function setProviderId($id) {
  138. if (!is_string($id)) {
  139. throw new \InvalidArgumentException('String expected.');
  140. }
  141. if ($this->providerId !== null) {
  142. throw new IllegalIDChangeException('Not allowed to assign a new provider id to a share');
  143. }
  144. $this->providerId = trim($id);
  145. return $this;
  146. }
  147. /**
  148. * @inheritdoc
  149. */
  150. public function setNode(Node $node) {
  151. $this->fileId = null;
  152. $this->nodeType = null;
  153. $this->node = $node;
  154. return $this;
  155. }
  156. /**
  157. * @inheritdoc
  158. */
  159. public function getNode() {
  160. if ($this->node === null) {
  161. if ($this->shareOwner === null || $this->fileId === null) {
  162. throw new NotFoundException();
  163. }
  164. // for federated shares the owner can be a remote user, in this
  165. // case we use the initiator
  166. if ($this->userManager->userExists($this->shareOwner)) {
  167. $userFolder = $this->rootFolder->getUserFolder($this->shareOwner);
  168. } else {
  169. $userFolder = $this->rootFolder->getUserFolder($this->sharedBy);
  170. }
  171. $node = $userFolder->getFirstNodeById($this->fileId);
  172. if (!$node) {
  173. throw new NotFoundException('Node for share not found, fileid: ' . $this->fileId);
  174. }
  175. $this->node = $node;
  176. }
  177. return $this->node;
  178. }
  179. /**
  180. * @inheritdoc
  181. */
  182. public function setNodeId($fileId) {
  183. $this->node = null;
  184. $this->fileId = $fileId;
  185. return $this;
  186. }
  187. /**
  188. * @inheritdoc
  189. */
  190. public function getNodeId(): int {
  191. if ($this->fileId === null) {
  192. $this->fileId = $this->getNode()->getId();
  193. }
  194. if ($this->fileId === null) {
  195. throw new NotFoundException("Share source not found");
  196. } else {
  197. return $this->fileId;
  198. }
  199. }
  200. /**
  201. * @inheritdoc
  202. */
  203. public function setNodeType($type) {
  204. if ($type !== 'file' && $type !== 'folder') {
  205. throw new \InvalidArgumentException();
  206. }
  207. $this->nodeType = $type;
  208. return $this;
  209. }
  210. /**
  211. * @inheritdoc
  212. */
  213. public function getNodeType() {
  214. if ($this->nodeType === null) {
  215. if ($this->getNodeCacheEntry()) {
  216. $info = $this->getNodeCacheEntry();
  217. $this->nodeType = $info->getMimeType() === FileInfo::MIMETYPE_FOLDER ? 'folder' : 'file';
  218. } else {
  219. $node = $this->getNode();
  220. $this->nodeType = $node instanceof File ? 'file' : 'folder';
  221. }
  222. }
  223. return $this->nodeType;
  224. }
  225. /**
  226. * @inheritdoc
  227. */
  228. public function setShareType($shareType) {
  229. $this->shareType = $shareType;
  230. return $this;
  231. }
  232. /**
  233. * @inheritdoc
  234. */
  235. public function getShareType() {
  236. return $this->shareType;
  237. }
  238. /**
  239. * @inheritdoc
  240. */
  241. public function setSharedWith($sharedWith) {
  242. if (!is_string($sharedWith)) {
  243. throw new \InvalidArgumentException();
  244. }
  245. $this->sharedWith = $sharedWith;
  246. return $this;
  247. }
  248. /**
  249. * @inheritdoc
  250. */
  251. public function getSharedWith() {
  252. return $this->sharedWith;
  253. }
  254. /**
  255. * @inheritdoc
  256. */
  257. public function setSharedWithDisplayName($displayName) {
  258. if (!is_string($displayName)) {
  259. throw new \InvalidArgumentException();
  260. }
  261. $this->sharedWithDisplayName = $displayName;
  262. return $this;
  263. }
  264. /**
  265. * @inheritdoc
  266. */
  267. public function getSharedWithDisplayName() {
  268. return $this->sharedWithDisplayName;
  269. }
  270. /**
  271. * @inheritdoc
  272. */
  273. public function setSharedWithAvatar($src) {
  274. if (!is_string($src)) {
  275. throw new \InvalidArgumentException();
  276. }
  277. $this->sharedWithAvatar = $src;
  278. return $this;
  279. }
  280. /**
  281. * @inheritdoc
  282. */
  283. public function getSharedWithAvatar() {
  284. return $this->sharedWithAvatar;
  285. }
  286. /**
  287. * @inheritdoc
  288. */
  289. public function setPermissions($permissions) {
  290. //TODO checks
  291. $this->permissions = $permissions;
  292. return $this;
  293. }
  294. /**
  295. * @inheritdoc
  296. */
  297. public function getPermissions() {
  298. return $this->permissions;
  299. }
  300. /**
  301. * @inheritdoc
  302. */
  303. public function newAttributes(): IAttributes {
  304. return new ShareAttributes();
  305. }
  306. /**
  307. * @inheritdoc
  308. */
  309. public function setAttributes(?IAttributes $attributes) {
  310. $this->attributes = $attributes;
  311. return $this;
  312. }
  313. /**
  314. * @inheritdoc
  315. */
  316. public function getAttributes(): ?IAttributes {
  317. return $this->attributes;
  318. }
  319. /**
  320. * @inheritdoc
  321. */
  322. public function setStatus(int $status): IShare {
  323. $this->status = $status;
  324. return $this;
  325. }
  326. /**
  327. * @inheritdoc
  328. */
  329. public function getStatus(): int {
  330. return $this->status;
  331. }
  332. /**
  333. * @inheritdoc
  334. */
  335. public function setNote($note) {
  336. $this->note = $note;
  337. return $this;
  338. }
  339. /**
  340. * @inheritdoc
  341. */
  342. public function getNote() {
  343. if (is_string($this->note)) {
  344. return $this->note;
  345. }
  346. return '';
  347. }
  348. /**
  349. * @inheritdoc
  350. */
  351. public function setLabel($label) {
  352. $this->label = $label;
  353. return $this;
  354. }
  355. /**
  356. * @inheritdoc
  357. */
  358. public function getLabel() {
  359. return $this->label;
  360. }
  361. /**
  362. * @inheritdoc
  363. */
  364. public function setExpirationDate($expireDate) {
  365. //TODO checks
  366. $this->expireDate = $expireDate;
  367. return $this;
  368. }
  369. /**
  370. * @inheritdoc
  371. */
  372. public function getExpirationDate() {
  373. return $this->expireDate;
  374. }
  375. /**
  376. * @inheritdoc
  377. */
  378. public function isExpired() {
  379. return $this->getExpirationDate() !== null &&
  380. $this->getExpirationDate() <= new \DateTime();
  381. }
  382. /**
  383. * @inheritdoc
  384. */
  385. public function setSharedBy($sharedBy) {
  386. if (!is_string($sharedBy)) {
  387. throw new \InvalidArgumentException();
  388. }
  389. //TODO checks
  390. $this->sharedBy = $sharedBy;
  391. return $this;
  392. }
  393. /**
  394. * @inheritdoc
  395. */
  396. public function getSharedBy() {
  397. //TODO check if set
  398. return $this->sharedBy;
  399. }
  400. /**
  401. * @inheritdoc
  402. */
  403. public function setShareOwner($shareOwner) {
  404. if (!is_string($shareOwner)) {
  405. throw new \InvalidArgumentException();
  406. }
  407. //TODO checks
  408. $this->shareOwner = $shareOwner;
  409. return $this;
  410. }
  411. /**
  412. * @inheritdoc
  413. */
  414. public function getShareOwner() {
  415. //TODO check if set
  416. return $this->shareOwner;
  417. }
  418. /**
  419. * @inheritdoc
  420. */
  421. public function setPassword($password) {
  422. $this->password = $password;
  423. return $this;
  424. }
  425. /**
  426. * @inheritdoc
  427. */
  428. public function getPassword() {
  429. return $this->password;
  430. }
  431. /**
  432. * @inheritdoc
  433. */
  434. public function setPasswordExpirationTime(?\DateTimeInterface $passwordExpirationTime = null): IShare {
  435. $this->passwordExpirationTime = $passwordExpirationTime;
  436. return $this;
  437. }
  438. /**
  439. * @inheritdoc
  440. */
  441. public function getPasswordExpirationTime(): ?\DateTimeInterface {
  442. return $this->passwordExpirationTime;
  443. }
  444. /**
  445. * @inheritdoc
  446. */
  447. public function setSendPasswordByTalk(bool $sendPasswordByTalk) {
  448. $this->sendPasswordByTalk = $sendPasswordByTalk;
  449. return $this;
  450. }
  451. /**
  452. * @inheritdoc
  453. */
  454. public function getSendPasswordByTalk(): bool {
  455. return $this->sendPasswordByTalk;
  456. }
  457. /**
  458. * @inheritdoc
  459. */
  460. public function setToken($token) {
  461. $this->token = $token;
  462. return $this;
  463. }
  464. /**
  465. * @inheritdoc
  466. */
  467. public function getToken() {
  468. return $this->token;
  469. }
  470. /**
  471. * Set the parent of this share
  472. *
  473. * @param int parent
  474. * @return IShare
  475. * @deprecated The new shares do not have parents. This is just here for legacy reasons.
  476. */
  477. public function setParent($parent) {
  478. $this->parent = $parent;
  479. return $this;
  480. }
  481. /**
  482. * Get the parent of this share.
  483. *
  484. * @return int
  485. * @deprecated The new shares do not have parents. This is just here for legacy reasons.
  486. */
  487. public function getParent() {
  488. return $this->parent;
  489. }
  490. /**
  491. * @inheritdoc
  492. */
  493. public function setTarget($target) {
  494. $this->target = $target;
  495. return $this;
  496. }
  497. /**
  498. * @inheritdoc
  499. */
  500. public function getTarget() {
  501. return $this->target;
  502. }
  503. /**
  504. * @inheritdoc
  505. */
  506. public function setShareTime(\DateTime $shareTime) {
  507. $this->shareTime = $shareTime;
  508. return $this;
  509. }
  510. /**
  511. * @inheritdoc
  512. */
  513. public function getShareTime() {
  514. return $this->shareTime;
  515. }
  516. /**
  517. * @inheritdoc
  518. */
  519. public function setMailSend($mailSend) {
  520. $this->mailSend = $mailSend;
  521. return $this;
  522. }
  523. /**
  524. * @inheritdoc
  525. */
  526. public function getMailSend() {
  527. return $this->mailSend;
  528. }
  529. /**
  530. * @inheritdoc
  531. */
  532. public function setNodeCacheEntry(ICacheEntry $entry) {
  533. $this->nodeCacheEntry = $entry;
  534. }
  535. /**
  536. * @inheritdoc
  537. */
  538. public function getNodeCacheEntry() {
  539. return $this->nodeCacheEntry;
  540. }
  541. public function setHideDownload(bool $hide): IShare {
  542. $this->hideDownload = $hide;
  543. return $this;
  544. }
  545. public function getHideDownload(): bool {
  546. return $this->hideDownload;
  547. }
  548. }