ExternalSharesController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files_Sharing\Controller;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\AppFramework\Http\JSONResponse;
  30. use OCP\Http\Client\IClientService;
  31. use OCP\IConfig;
  32. use OCP\IRequest;
  33. /**
  34. * Class ExternalSharesController
  35. *
  36. * @package OCA\Files_Sharing\Controller
  37. */
  38. class ExternalSharesController extends Controller {
  39. public function __construct(
  40. string $appName,
  41. IRequest $request,
  42. private \OCA\Files_Sharing\External\Manager $externalManager,
  43. private IClientService $clientService,
  44. private IConfig $config,
  45. ) {
  46. parent::__construct($appName, $request);
  47. }
  48. /**
  49. * @NoAdminRequired
  50. * @NoOutgoingFederatedSharingRequired
  51. *
  52. * @return JSONResponse
  53. */
  54. public function index() {
  55. return new JSONResponse($this->externalManager->getOpenShares());
  56. }
  57. /**
  58. * @NoAdminRequired
  59. * @NoOutgoingFederatedSharingRequired
  60. *
  61. * @param int $id
  62. * @return JSONResponse
  63. */
  64. public function create($id) {
  65. $this->externalManager->acceptShare($id);
  66. return new JSONResponse();
  67. }
  68. /**
  69. * @NoAdminRequired
  70. * @NoOutgoingFederatedSharingRequired
  71. *
  72. * @param integer $id
  73. * @return JSONResponse
  74. */
  75. public function destroy($id) {
  76. $this->externalManager->declineShare($id);
  77. return new JSONResponse();
  78. }
  79. /**
  80. * Test whether the specified remote is accessible
  81. *
  82. * @param string $remote
  83. * @param bool $checkVersion
  84. * @return bool
  85. */
  86. protected function testUrl($remote, $checkVersion = false) {
  87. try {
  88. $client = $this->clientService->newClient();
  89. $response = json_decode($client->get(
  90. $remote,
  91. [
  92. 'timeout' => 3,
  93. 'connect_timeout' => 3,
  94. 'verify' => !$this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates', false),
  95. ]
  96. )->getBody());
  97. if ($checkVersion) {
  98. return !empty($response->version) && version_compare($response->version, '7.0.0', '>=');
  99. } else {
  100. return is_object($response);
  101. }
  102. } catch (\Exception $e) {
  103. return false;
  104. }
  105. }
  106. /**
  107. * @PublicPage
  108. * @NoOutgoingFederatedSharingRequired
  109. * @NoIncomingFederatedSharingRequired
  110. *
  111. * @param string $remote
  112. * @return DataResponse
  113. */
  114. public function testRemote($remote) {
  115. if (str_contains($remote, '#') || str_contains($remote, '?') || str_contains($remote, ';')) {
  116. return new DataResponse(false);
  117. }
  118. if (
  119. $this->testUrl('https://' . $remote . '/ocm-provider/') ||
  120. $this->testUrl('https://' . $remote . '/ocm-provider/index.php') ||
  121. $this->testUrl('https://' . $remote . '/status.php', true)
  122. ) {
  123. return new DataResponse('https');
  124. } elseif (
  125. $this->testUrl('http://' . $remote . '/ocm-provider/') ||
  126. $this->testUrl('http://' . $remote . '/ocm-provider/index.php') ||
  127. $this->testUrl('http://' . $remote . '/status.php', true)
  128. ) {
  129. return new DataResponse('http');
  130. } else {
  131. return new DataResponse(false);
  132. }
  133. }
  134. }