Share.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Share20;
  25. use OCP\Files\Cache\ICacheEntry;
  26. use OCP\Files\File;
  27. use OCP\Files\IRootFolder;
  28. use OCP\Files\Node;
  29. use OCP\Files\NotFoundException;
  30. use OCP\IUserManager;
  31. use OCP\Share\Exceptions\IllegalIDChangeException;
  32. use OCP\Share\IShare;
  33. class Share implements \OCP\Share\IShare {
  34. /** @var string */
  35. private $id;
  36. /** @var string */
  37. private $providerId;
  38. /** @var Node */
  39. private $node;
  40. /** @var int */
  41. private $fileId;
  42. /** @var string */
  43. private $nodeType;
  44. /** @var int */
  45. private $shareType;
  46. /** @var string */
  47. private $sharedWith;
  48. /** @var string */
  49. private $sharedWithDisplayName;
  50. /** @var string */
  51. private $sharedWithAvatar;
  52. /** @var string */
  53. private $sharedBy;
  54. /** @var string */
  55. private $shareOwner;
  56. /** @var int */
  57. private $permissions;
  58. /** @var string */
  59. private $note = '';
  60. /** @var \DateTime */
  61. private $expireDate;
  62. /** @var string */
  63. private $password;
  64. /** @var bool */
  65. private $sendPasswordByTalk = false;
  66. /** @var string */
  67. private $token;
  68. /** @var int */
  69. private $parent;
  70. /** @var string */
  71. private $target;
  72. /** @var \DateTime */
  73. private $shareTime;
  74. /** @var bool */
  75. private $mailSend;
  76. /** @var string */
  77. private $label = '';
  78. /** @var IRootFolder */
  79. private $rootFolder;
  80. /** @var IUserManager */
  81. private $userManager;
  82. /** @var ICacheEntry|null */
  83. private $nodeCacheEntry;
  84. /** @var bool */
  85. private $hideDownload = false;
  86. public function __construct(IRootFolder $rootFolder, IUserManager $userManager) {
  87. $this->rootFolder = $rootFolder;
  88. $this->userManager = $userManager;
  89. }
  90. /**
  91. * @inheritdoc
  92. */
  93. public function setId($id) {
  94. if (is_int($id)) {
  95. $id = (string)$id;
  96. }
  97. if(!is_string($id)) {
  98. throw new \InvalidArgumentException('String expected.');
  99. }
  100. if ($this->id !== null) {
  101. throw new IllegalIDChangeException('Not allowed to assign a new internal id to a share');
  102. }
  103. $this->id = trim($id);
  104. return $this;
  105. }
  106. /**
  107. * @inheritdoc
  108. */
  109. public function getId() {
  110. return $this->id;
  111. }
  112. /**
  113. * @inheritdoc
  114. */
  115. public function getFullId() {
  116. if ($this->providerId === null || $this->id === null) {
  117. throw new \UnexpectedValueException;
  118. }
  119. return $this->providerId . ':' . $this->id;
  120. }
  121. /**
  122. * @inheritdoc
  123. */
  124. public function setProviderId($id) {
  125. if(!is_string($id)) {
  126. throw new \InvalidArgumentException('String expected.');
  127. }
  128. if ($this->providerId !== null) {
  129. throw new IllegalIDChangeException('Not allowed to assign a new provider id to a share');
  130. }
  131. $this->providerId = trim($id);
  132. return $this;
  133. }
  134. /**
  135. * @inheritdoc
  136. */
  137. public function setNode(Node $node) {
  138. $this->fileId = null;
  139. $this->nodeType = null;
  140. $this->node = $node;
  141. return $this;
  142. }
  143. /**
  144. * @inheritdoc
  145. */
  146. public function getNode() {
  147. if ($this->node === null) {
  148. if ($this->shareOwner === null || $this->fileId === null) {
  149. throw new NotFoundException();
  150. }
  151. // for federated shares the owner can be a remote user, in this
  152. // case we use the initiator
  153. if($this->userManager->userExists($this->shareOwner)) {
  154. $userFolder = $this->rootFolder->getUserFolder($this->shareOwner);
  155. } else {
  156. $userFolder = $this->rootFolder->getUserFolder($this->sharedBy);
  157. }
  158. $nodes = $userFolder->getById($this->fileId);
  159. if (empty($nodes)) {
  160. throw new NotFoundException('Node for share not found, fileid: ' . $this->fileId);
  161. }
  162. $this->node = $nodes[0];
  163. }
  164. return $this->node;
  165. }
  166. /**
  167. * @inheritdoc
  168. */
  169. public function setNodeId($fileId) {
  170. $this->node = null;
  171. $this->fileId = $fileId;
  172. return $this;
  173. }
  174. /**
  175. * @inheritdoc
  176. */
  177. public function getNodeId() {
  178. if ($this->fileId === null) {
  179. $this->fileId = $this->getNode()->getId();
  180. }
  181. return $this->fileId;
  182. }
  183. /**
  184. * @inheritdoc
  185. */
  186. public function setNodeType($type) {
  187. if ($type !== 'file' && $type !== 'folder') {
  188. throw new \InvalidArgumentException();
  189. }
  190. $this->nodeType = $type;
  191. return $this;
  192. }
  193. /**
  194. * @inheritdoc
  195. */
  196. public function getNodeType() {
  197. if ($this->nodeType === null) {
  198. $node = $this->getNode();
  199. $this->nodeType = $node instanceof File ? 'file' : 'folder';
  200. }
  201. return $this->nodeType;
  202. }
  203. /**
  204. * @inheritdoc
  205. */
  206. public function setShareType($shareType) {
  207. $this->shareType = $shareType;
  208. return $this;
  209. }
  210. /**
  211. * @inheritdoc
  212. */
  213. public function getShareType() {
  214. return $this->shareType;
  215. }
  216. /**
  217. * @inheritdoc
  218. */
  219. public function setSharedWith($sharedWith) {
  220. if (!is_string($sharedWith)) {
  221. throw new \InvalidArgumentException();
  222. }
  223. $this->sharedWith = $sharedWith;
  224. return $this;
  225. }
  226. /**
  227. * @inheritdoc
  228. */
  229. public function getSharedWith() {
  230. return $this->sharedWith;
  231. }
  232. /**
  233. * @inheritdoc
  234. */
  235. public function setSharedWithDisplayName($displayName) {
  236. if (!is_string($displayName)) {
  237. throw new \InvalidArgumentException();
  238. }
  239. $this->sharedWithDisplayName = $displayName;
  240. return $this;
  241. }
  242. /**
  243. * @inheritdoc
  244. */
  245. public function getSharedWithDisplayName() {
  246. return $this->sharedWithDisplayName;
  247. }
  248. /**
  249. * @inheritdoc
  250. */
  251. public function setSharedWithAvatar($src) {
  252. if (!is_string($src)) {
  253. throw new \InvalidArgumentException();
  254. }
  255. $this->sharedWithAvatar = $src;
  256. return $this;
  257. }
  258. /**
  259. * @inheritdoc
  260. */
  261. public function getSharedWithAvatar() {
  262. return $this->sharedWithAvatar;
  263. }
  264. /**
  265. * @inheritdoc
  266. */
  267. public function setPermissions($permissions) {
  268. //TODO checkes
  269. $this->permissions = $permissions;
  270. return $this;
  271. }
  272. /**
  273. * @inheritdoc
  274. */
  275. public function getPermissions() {
  276. return $this->permissions;
  277. }
  278. /**
  279. * @inheritdoc
  280. */
  281. public function setNote($note) {
  282. $this->note = $note;
  283. return $this;
  284. }
  285. /**
  286. * @inheritdoc
  287. */
  288. public function getNote() {
  289. if (is_string($this->note)) {
  290. return $this->note;
  291. }
  292. return '';
  293. }
  294. /**
  295. * @inheritdoc
  296. */
  297. public function setLabel($label) {
  298. $this->label = $label;
  299. return $this;
  300. }
  301. /**
  302. * @inheritdoc
  303. */
  304. public function getLabel() {
  305. return $this->label;
  306. }
  307. /**
  308. * @inheritdoc
  309. */
  310. public function setExpirationDate($expireDate) {
  311. //TODO checks
  312. $this->expireDate = $expireDate;
  313. return $this;
  314. }
  315. /**
  316. * @inheritdoc
  317. */
  318. public function getExpirationDate() {
  319. return $this->expireDate;
  320. }
  321. /**
  322. * @inheritdoc
  323. */
  324. public function setSharedBy($sharedBy) {
  325. if (!is_string($sharedBy)) {
  326. throw new \InvalidArgumentException();
  327. }
  328. //TODO checks
  329. $this->sharedBy = $sharedBy;
  330. return $this;
  331. }
  332. /**
  333. * @inheritdoc
  334. */
  335. public function getSharedBy() {
  336. //TODO check if set
  337. return $this->sharedBy;
  338. }
  339. /**
  340. * @inheritdoc
  341. */
  342. public function setShareOwner($shareOwner) {
  343. if (!is_string($shareOwner)) {
  344. throw new \InvalidArgumentException();
  345. }
  346. //TODO checks
  347. $this->shareOwner = $shareOwner;
  348. return $this;
  349. }
  350. /**
  351. * @inheritdoc
  352. */
  353. public function getShareOwner() {
  354. //TODO check if set
  355. return $this->shareOwner;
  356. }
  357. /**
  358. * @inheritdoc
  359. */
  360. public function setPassword($password) {
  361. $this->password = $password;
  362. return $this;
  363. }
  364. /**
  365. * @inheritdoc
  366. */
  367. public function getPassword() {
  368. return $this->password;
  369. }
  370. /**
  371. * @inheritdoc
  372. */
  373. public function setSendPasswordByTalk(bool $sendPasswordByTalk) {
  374. $this->sendPasswordByTalk = $sendPasswordByTalk;
  375. return $this;
  376. }
  377. /**
  378. * @inheritdoc
  379. */
  380. public function getSendPasswordByTalk(): bool {
  381. return $this->sendPasswordByTalk;
  382. }
  383. /**
  384. * @inheritdoc
  385. */
  386. public function setToken($token) {
  387. $this->token = $token;
  388. return $this;
  389. }
  390. /**
  391. * @inheritdoc
  392. */
  393. public function getToken() {
  394. return $this->token;
  395. }
  396. /**
  397. * Set the parent of this share
  398. *
  399. * @param int parent
  400. * @return \OCP\Share\IShare
  401. * @deprecated The new shares do not have parents. This is just here for legacy reasons.
  402. */
  403. public function setParent($parent) {
  404. $this->parent = $parent;
  405. return $this;
  406. }
  407. /**
  408. * Get the parent of this share.
  409. *
  410. * @return int
  411. * @deprecated The new shares do not have parents. This is just here for legacy reasons.
  412. */
  413. public function getParent() {
  414. return $this->parent;
  415. }
  416. /**
  417. * @inheritdoc
  418. */
  419. public function setTarget($target) {
  420. $this->target = $target;
  421. return $this;
  422. }
  423. /**
  424. * @inheritdoc
  425. */
  426. public function getTarget() {
  427. return $this->target;
  428. }
  429. /**
  430. * @inheritdoc
  431. */
  432. public function setShareTime(\DateTime $shareTime) {
  433. $this->shareTime = $shareTime;
  434. return $this;
  435. }
  436. /**
  437. * @inheritdoc
  438. */
  439. public function getShareTime() {
  440. return $this->shareTime;
  441. }
  442. /**
  443. * @inheritdoc
  444. */
  445. public function setMailSend($mailSend) {
  446. $this->mailSend = $mailSend;
  447. return $this;
  448. }
  449. /**
  450. * @inheritdoc
  451. */
  452. public function getMailSend() {
  453. return $this->mailSend;
  454. }
  455. /**
  456. * @inheritdoc
  457. */
  458. public function setNodeCacheEntry(ICacheEntry $entry) {
  459. $this->nodeCacheEntry = $entry;
  460. }
  461. /**
  462. * @inheritdoc
  463. */
  464. public function getNodeCacheEntry() {
  465. return $this->nodeCacheEntry;
  466. }
  467. public function setHideDownload(bool $hide): IShare {
  468. $this->hideDownload = $hide;
  469. return $this;
  470. }
  471. public function getHideDownload(): bool {
  472. return $this->hideDownload;
  473. }
  474. }