Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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