Share.php 12 KB

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