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.

578 lines
10 KiB

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