Share.php 11 KB

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