1
0

ExternalSharesController.php 3.0 KB

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