ExternalSharesController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Controller;
  8. use OCP\AppFramework\Controller;
  9. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  10. use OCP\AppFramework\Http\Attribute\PublicPage;
  11. use OCP\AppFramework\Http\DataResponse;
  12. use OCP\AppFramework\Http\JSONResponse;
  13. use OCP\Http\Client\IClientService;
  14. use OCP\IConfig;
  15. use OCP\IRequest;
  16. /**
  17. * Class ExternalSharesController
  18. *
  19. * @package OCA\Files_Sharing\Controller
  20. */
  21. class ExternalSharesController extends Controller {
  22. public function __construct(
  23. string $appName,
  24. IRequest $request,
  25. private \OCA\Files_Sharing\External\Manager $externalManager,
  26. private IClientService $clientService,
  27. private IConfig $config,
  28. ) {
  29. parent::__construct($appName, $request);
  30. }
  31. /**
  32. * @NoOutgoingFederatedSharingRequired
  33. *
  34. * @return JSONResponse
  35. */
  36. #[NoAdminRequired]
  37. public function index() {
  38. return new JSONResponse($this->externalManager->getOpenShares());
  39. }
  40. /**
  41. * @NoOutgoingFederatedSharingRequired
  42. *
  43. * @param int $id
  44. * @return JSONResponse
  45. */
  46. #[NoAdminRequired]
  47. public function create($id) {
  48. $this->externalManager->acceptShare($id);
  49. return new JSONResponse();
  50. }
  51. /**
  52. * @NoOutgoingFederatedSharingRequired
  53. *
  54. * @param integer $id
  55. * @return JSONResponse
  56. */
  57. #[NoAdminRequired]
  58. public function destroy($id) {
  59. $this->externalManager->declineShare($id);
  60. return new JSONResponse();
  61. }
  62. /**
  63. * Test whether the specified remote is accessible
  64. *
  65. * @param string $remote
  66. * @param bool $checkVersion
  67. * @return bool
  68. */
  69. protected function testUrl($remote, $checkVersion = false) {
  70. try {
  71. $client = $this->clientService->newClient();
  72. $response = json_decode($client->get(
  73. $remote,
  74. [
  75. 'timeout' => 3,
  76. 'connect_timeout' => 3,
  77. 'verify' => !$this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates', false),
  78. ]
  79. )->getBody());
  80. if ($checkVersion) {
  81. return !empty($response->version) && version_compare($response->version, '7.0.0', '>=');
  82. } else {
  83. return is_object($response);
  84. }
  85. } catch (\Exception $e) {
  86. return false;
  87. }
  88. }
  89. /**
  90. * @NoOutgoingFederatedSharingRequired
  91. * @NoIncomingFederatedSharingRequired
  92. *
  93. * @param string $remote
  94. * @return DataResponse
  95. */
  96. #[PublicPage]
  97. public function testRemote($remote) {
  98. if (str_contains($remote, '#') || str_contains($remote, '?') || str_contains($remote, ';')) {
  99. return new DataResponse(false);
  100. }
  101. if (
  102. $this->testUrl('https://' . $remote . '/ocm-provider/') ||
  103. $this->testUrl('https://' . $remote . '/ocm-provider/index.php') ||
  104. $this->testUrl('https://' . $remote . '/status.php', true)
  105. ) {
  106. return new DataResponse('https');
  107. } elseif (
  108. $this->testUrl('http://' . $remote . '/ocm-provider/') ||
  109. $this->testUrl('http://' . $remote . '/ocm-provider/index.php') ||
  110. $this->testUrl('http://' . $remote . '/status.php', true)
  111. ) {
  112. return new DataResponse('http');
  113. } else {
  114. return new DataResponse(false);
  115. }
  116. }
  117. }