Share.php 11 KB

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