ExternalSharesController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\IRequest;
  32. /**
  33. * Class ExternalSharesController
  34. *
  35. * @package OCA\Files_Sharing\Controller
  36. */
  37. class ExternalSharesController extends Controller {
  38. /** @var \OCA\Files_Sharing\External\Manager */
  39. private $externalManager;
  40. /** @var IClientService */
  41. private $clientService;
  42. /**
  43. * @param string $appName
  44. * @param IRequest $request
  45. * @param \OCA\Files_Sharing\External\Manager $externalManager
  46. * @param IClientService $clientService
  47. */
  48. public function __construct($appName,
  49. IRequest $request,
  50. \OCA\Files_Sharing\External\Manager $externalManager,
  51. IClientService $clientService) {
  52. parent::__construct($appName, $request);
  53. $this->externalManager = $externalManager;
  54. $this->clientService = $clientService;
  55. }
  56. /**
  57. * @NoAdminRequired
  58. * @NoOutgoingFederatedSharingRequired
  59. *
  60. * @return JSONResponse
  61. */
  62. public function index() {
  63. return new JSONResponse($this->externalManager->getOpenShares());
  64. }
  65. /**
  66. * @NoAdminRequired
  67. * @NoOutgoingFederatedSharingRequired
  68. *
  69. * @param int $id
  70. * @return JSONResponse
  71. */
  72. public function create($id) {
  73. $this->externalManager->acceptShare($id);
  74. return new JSONResponse();
  75. }
  76. /**
  77. * @NoAdminRequired
  78. * @NoOutgoingFederatedSharingRequired
  79. *
  80. * @param integer $id
  81. * @return JSONResponse
  82. */
  83. public function destroy($id) {
  84. $this->externalManager->declineShare($id);
  85. return new JSONResponse();
  86. }
  87. /**
  88. * Test whether the specified remote is accessible
  89. *
  90. * @param string $remote
  91. * @param bool $checkVersion
  92. * @return bool
  93. */
  94. protected function testUrl($remote, $checkVersion = false) {
  95. try {
  96. $client = $this->clientService->newClient();
  97. $response = json_decode($client->get(
  98. $remote,
  99. [
  100. 'timeout' => 3,
  101. 'connect_timeout' => 3,
  102. ]
  103. )->getBody());
  104. if ($checkVersion) {
  105. return !empty($response->version) && version_compare($response->version, '7.0.0', '>=');
  106. } else {
  107. return is_object($response);
  108. }
  109. } catch (\Exception $e) {
  110. return false;
  111. }
  112. }
  113. /**
  114. * @PublicPage
  115. * @NoOutgoingFederatedSharingRequired
  116. * @NoIncomingFederatedSharingRequired
  117. *
  118. * @param string $remote
  119. * @return DataResponse
  120. */
  121. public function testRemote($remote) {
  122. if (str_contains($remote, '#') || str_contains($remote, '?') || str_contains($remote, ';')) {
  123. return new DataResponse(false);
  124. }
  125. if (
  126. $this->testUrl('https://' . $remote . '/ocs-provider/') ||
  127. $this->testUrl('https://' . $remote . '/ocs-provider/index.php') ||
  128. $this->testUrl('https://' . $remote . '/status.php', true)
  129. ) {
  130. return new DataResponse('https');
  131. } elseif (
  132. $this->testUrl('http://' . $remote . '/ocs-provider/') ||
  133. $this->testUrl('http://' . $remote . '/ocs-provider/index.php') ||
  134. $this->testUrl('http://' . $remote . '/status.php', true)
  135. ) {
  136. return new DataResponse('http');
  137. } else {
  138. return new DataResponse(false);
  139. }
  140. }
  141. }