You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
4.0 KiB

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