Share.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. $node = $userFolder->getFirstNodeById($this->fileId);
  173. if (!$node) {
  174. throw new NotFoundException('Node for share not found, fileid: ' . $this->fileId);
  175. }
  176. $this->node = $node;
  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(): int {
  192. if ($this->fileId === null) {
  193. $this->fileId = $this->getNode()->getId();
  194. }
  195. if ($this->fileId === null) {
  196. throw new NotFoundException("Share source not found");
  197. } else {
  198. return $this->fileId;
  199. }
  200. }
  201. /**
  202. * @inheritdoc
  203. */
  204. public function setNodeType($type) {
  205. if ($type !== 'file' && $type !== 'folder') {
  206. throw new \InvalidArgumentException();
  207. }
  208. $this->nodeType = $type;
  209. return $this;
  210. }
  211. /**
  212. * @inheritdoc
  213. */
  214. public function getNodeType() {
  215. if ($this->nodeType === null) {
  216. if ($this->getNodeCacheEntry()) {
  217. $info = $this->getNodeCacheEntry();
  218. $this->nodeType = $info->getMimeType() === FileInfo::MIMETYPE_FOLDER ? 'folder' : 'file';
  219. } else {
  220. $node = $this->getNode();
  221. $this->nodeType = $node instanceof File ? 'file' : 'folder';
  222. }
  223. }
  224. return $this->nodeType;
  225. }
  226. /**
  227. * @inheritdoc
  228. */
  229. public function setShareType($shareType) {
  230. $this->shareType = $shareType;
  231. return $this;
  232. }
  233. /**
  234. * @inheritdoc
  235. */
  236. public function getShareType() {
  237. return $this->shareType;
  238. }
  239. /**
  240. * @inheritdoc
  241. */
  242. public function setSharedWith($sharedWith) {
  243. if (!is_string($sharedWith)) {
  244. throw new \InvalidArgumentException();
  245. }
  246. $this->sharedWith = $sharedWith;
  247. return $this;
  248. }
  249. /**
  250. * @inheritdoc
  251. */
  252. public function getSharedWith() {
  253. return $this->sharedWith;
  254. }
  255. /**
  256. * @inheritdoc
  257. */
  258. public function setSharedWithDisplayName($displayName) {
  259. if (!is_string($displayName)) {
  260. throw new \InvalidArgumentException();
  261. }
  262. $this->sharedWithDisplayName = $displayName;
  263. return $this;
  264. }
  265. /**
  266. * @inheritdoc
  267. */
  268. public function getSharedWithDisplayName() {
  269. return $this->sharedWithDisplayName;
  270. }
  271. /**
  272. * @inheritdoc
  273. */
  274. public function setSharedWithAvatar($src) {
  275. if (!is_string($src)) {
  276. throw new \InvalidArgumentException();
  277. }
  278. $this->sharedWithAvatar = $src;
  279. return $this;
  280. }
  281. /**
  282. * @inheritdoc
  283. */
  284. public function getSharedWithAvatar() {
  285. return $this->sharedWithAvatar;
  286. }
  287. /**
  288. * @inheritdoc
  289. */
  290. public function setPermissions($permissions) {
  291. //TODO checks
  292. $this->permissions = $permissions;
  293. return $this;
  294. }
  295. /**
  296. * @inheritdoc
  297. */
  298. public function getPermissions() {
  299. return $this->permissions;
  300. }
  301. /**
  302. * @inheritdoc
  303. */
  304. public function newAttributes(): IAttributes {
  305. return new ShareAttributes();
  306. }
  307. /**
  308. * @inheritdoc
  309. */
  310. public function setAttributes(?IAttributes $attributes) {
  311. $this->attributes = $attributes;
  312. return $this;
  313. }
  314. /**
  315. * @inheritdoc
  316. */
  317. public function getAttributes(): ?IAttributes {
  318. return $this->attributes;
  319. }
  320. /**
  321. * @inheritdoc
  322. */
  323. public function setStatus(int $status): IShare {
  324. $this->status = $status;
  325. return $this;
  326. }
  327. /**
  328. * @inheritdoc
  329. */
  330. public function getStatus(): int {
  331. return $this->status;
  332. }
  333. /**
  334. * @inheritdoc
  335. */
  336. public function setNote($note) {
  337. $this->note = $note;
  338. return $this;
  339. }
  340. /**
  341. * @inheritdoc
  342. */
  343. public function getNote() {
  344. if (is_string($this->note)) {
  345. return $this->note;
  346. }
  347. return '';
  348. }
  349. /**
  350. * @inheritdoc
  351. */
  352. public function setLabel($label) {
  353. $this->label = $label;
  354. return $this;
  355. }
  356. /**
  357. * @inheritdoc
  358. */
  359. public function getLabel() {
  360. return $this->label;
  361. }
  362. /**
  363. * @inheritdoc
  364. */
  365. public function setExpirationDate($expireDate) {
  366. //TODO checks
  367. $this->expireDate = $expireDate;
  368. return $this;
  369. }
  370. /**
  371. * @inheritdoc
  372. */
  373. public function getExpirationDate() {
  374. return $this->expireDate;
  375. }
  376. /**
  377. * @inheritdoc
  378. */
  379. public function setNoExpirationDate(bool $noExpirationDate) {
  380. $this->noExpirationDate = $noExpirationDate;
  381. return $this;
  382. }
  383. /**
  384. * @inheritdoc
  385. */
  386. public function getNoExpirationDate(): bool {
  387. return $this->noExpirationDate;
  388. }
  389. /**
  390. * @inheritdoc
  391. */
  392. public function isExpired() {
  393. return $this->getExpirationDate() !== null &&
  394. $this->getExpirationDate() <= new \DateTime();
  395. }
  396. /**
  397. * @inheritdoc
  398. */
  399. public function setSharedBy($sharedBy) {
  400. if (!is_string($sharedBy)) {
  401. throw new \InvalidArgumentException();
  402. }
  403. //TODO checks
  404. $this->sharedBy = $sharedBy;
  405. return $this;
  406. }
  407. /**
  408. * @inheritdoc
  409. */
  410. public function getSharedBy() {
  411. //TODO check if set
  412. return $this->sharedBy;
  413. }
  414. /**
  415. * @inheritdoc
  416. */
  417. public function setShareOwner($shareOwner) {
  418. if (!is_string($shareOwner)) {
  419. throw new \InvalidArgumentException();
  420. }
  421. //TODO checks
  422. $this->shareOwner = $shareOwner;
  423. return $this;
  424. }
  425. /**
  426. * @inheritdoc
  427. */
  428. public function getShareOwner() {
  429. //TODO check if set
  430. return $this->shareOwner;
  431. }
  432. /**
  433. * @inheritdoc
  434. */
  435. public function setPassword($password) {
  436. $this->password = $password;
  437. return $this;
  438. }
  439. /**
  440. * @inheritdoc
  441. */
  442. public function getPassword() {
  443. return $this->password;
  444. }
  445. /**
  446. * @inheritdoc
  447. */
  448. public function setPasswordExpirationTime(?\DateTimeInterface $passwordExpirationTime = null): IShare {
  449. $this->passwordExpirationTime = $passwordExpirationTime;
  450. return $this;
  451. }
  452. /**
  453. * @inheritdoc
  454. */
  455. public function getPasswordExpirationTime(): ?\DateTimeInterface {
  456. return $this->passwordExpirationTime;
  457. }
  458. /**
  459. * @inheritdoc
  460. */
  461. public function setSendPasswordByTalk(bool $sendPasswordByTalk) {
  462. $this->sendPasswordByTalk = $sendPasswordByTalk;
  463. return $this;
  464. }
  465. /**
  466. * @inheritdoc
  467. */
  468. public function getSendPasswordByTalk(): bool {
  469. return $this->sendPasswordByTalk;
  470. }
  471. /**
  472. * @inheritdoc
  473. */
  474. public function setToken($token) {
  475. $this->token = $token;
  476. return $this;
  477. }
  478. /**
  479. * @inheritdoc
  480. */
  481. public function getToken() {
  482. return $this->token;
  483. }
  484. /**
  485. * Set the parent of this share
  486. *
  487. * @param int parent
  488. * @return IShare
  489. * @deprecated The new shares do not have parents. This is just here for legacy reasons.
  490. */
  491. public function setParent($parent) {
  492. $this->parent = $parent;
  493. return $this;
  494. }
  495. /**
  496. * Get the parent of this share.
  497. *
  498. * @return int
  499. * @deprecated The new shares do not have parents. This is just here for legacy reasons.
  500. */
  501. public function getParent() {
  502. return $this->parent;
  503. }
  504. /**
  505. * @inheritdoc
  506. */
  507. public function setTarget($target) {
  508. $this->target = $target;
  509. return $this;
  510. }
  511. /**
  512. * @inheritdoc
  513. */
  514. public function getTarget() {
  515. return $this->target;
  516. }
  517. /**
  518. * @inheritdoc
  519. */
  520. public function setShareTime(\DateTime $shareTime) {
  521. $this->shareTime = $shareTime;
  522. return $this;
  523. }
  524. /**
  525. * @inheritdoc
  526. */
  527. public function getShareTime() {
  528. return $this->shareTime;
  529. }
  530. /**
  531. * @inheritdoc
  532. */
  533. public function setMailSend($mailSend) {
  534. $this->mailSend = $mailSend;
  535. return $this;
  536. }
  537. /**
  538. * @inheritdoc
  539. */
  540. public function getMailSend() {
  541. return $this->mailSend;
  542. }
  543. /**
  544. * @inheritdoc
  545. */
  546. public function setNodeCacheEntry(ICacheEntry $entry) {
  547. $this->nodeCacheEntry = $entry;
  548. }
  549. /**
  550. * @inheritdoc
  551. */
  552. public function getNodeCacheEntry() {
  553. return $this->nodeCacheEntry;
  554. }
  555. public function setHideDownload(bool $hide): IShare {
  556. $this->hideDownload = $hide;
  557. return $this;
  558. }
  559. public function getHideDownload(): bool {
  560. return $this->hideDownload;
  561. }
  562. }