DirectHome.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\Direct;
  26. use OC\Security\Bruteforce\Throttler;
  27. use OCA\DAV\Db\DirectMapper;
  28. use OCP\AppFramework\Db\DoesNotExistException;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\EventDispatcher\IEventDispatcher;
  31. use OCP\Files\IRootFolder;
  32. use OCP\IRequest;
  33. use Sabre\DAV\Exception\Forbidden;
  34. use Sabre\DAV\Exception\MethodNotAllowed;
  35. use Sabre\DAV\Exception\NotFound;
  36. use Sabre\DAV\ICollection;
  37. class DirectHome implements ICollection {
  38. /** @var IRootFolder */
  39. private $rootFolder;
  40. /** @var DirectMapper */
  41. private $mapper;
  42. /** @var ITimeFactory */
  43. private $timeFactory;
  44. /** @var Throttler */
  45. private $throttler;
  46. /** @var IRequest */
  47. private $request;
  48. private $eventDispatcher;
  49. public function __construct(
  50. IRootFolder $rootFolder,
  51. DirectMapper $mapper,
  52. ITimeFactory $timeFactory,
  53. Throttler $throttler,
  54. IRequest $request,
  55. IEventDispatcher $eventDispatcher
  56. ) {
  57. $this->rootFolder = $rootFolder;
  58. $this->mapper = $mapper;
  59. $this->timeFactory = $timeFactory;
  60. $this->throttler = $throttler;
  61. $this->request = $request;
  62. $this->eventDispatcher = $eventDispatcher;
  63. }
  64. public function createFile($name, $data = null) {
  65. throw new Forbidden();
  66. }
  67. public function createDirectory($name) {
  68. throw new Forbidden();
  69. }
  70. public function getChild($name): DirectFile {
  71. try {
  72. $direct = $this->mapper->getByToken($name);
  73. // Expired
  74. if ($direct->getExpiration() < $this->timeFactory->getTime()) {
  75. throw new NotFound();
  76. }
  77. return new DirectFile($direct, $this->rootFolder, $this->eventDispatcher);
  78. } catch (DoesNotExistException $e) {
  79. // Since the token space is so huge only throttle on non-existing token
  80. $this->throttler->registerAttempt('directlink', $this->request->getRemoteAddress());
  81. $this->throttler->sleepDelay($this->request->getRemoteAddress(), 'directlink');
  82. throw new NotFound();
  83. }
  84. }
  85. public function getChildren() {
  86. throw new MethodNotAllowed('Listing members of this collection is disabled');
  87. }
  88. public function childExists($name): bool {
  89. return false;
  90. }
  91. public function delete() {
  92. throw new Forbidden();
  93. }
  94. public function getName(): string {
  95. return 'direct';
  96. }
  97. public function setName($name) {
  98. throw new Forbidden();
  99. }
  100. public function getLastModified(): int {
  101. return 0;
  102. }
  103. }