Share.php 11 KB

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