You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Share.php 11KB

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