external.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @author Björn Schießle <bjoern@schiessle.org>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <icewind@owncloud.com>
  8. * @author Roeland Jago Douma <rullzer@owncloud.com>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2016, ownCloud, Inc.
  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. OCP\JSON::callCheck();
  28. OCP\JSON::checkLoggedIn();
  29. OCP\JSON::checkAppEnabled('files_sharing');
  30. $l = \OC::$server->getL10N('files_sharing');
  31. $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application('federatedfilesharing');
  32. $federatedShareProvider = $federatedSharingApp->getFederatedShareProvider();
  33. // check if server admin allows to mount public links from other servers
  34. if ($federatedShareProvider->isIncomingServer2serverShareEnabled() === false) {
  35. \OCP\JSON::error(array('data' => array('message' => $l->t('Server to server sharing is not enabled on this server'))));
  36. exit();
  37. }
  38. $token = $_POST['token'];
  39. $remote = $_POST['remote'];
  40. $owner = $_POST['owner'];
  41. $ownerDisplayName = $_POST['ownerDisplayName'];
  42. $name = $_POST['name'];
  43. $password = $_POST['password'];
  44. // Check for invalid name
  45. if(!\OCP\Util::isValidFileName($name)) {
  46. \OCP\JSON::error(array('data' => array('message' => $l->t('The mountpoint name contains invalid characters.'))));
  47. exit();
  48. }
  49. $currentUser = \OC::$server->getUserSession()->getUser()->getUID();
  50. $currentServer = \OC::$server->getURLGenerator()->getAbsoluteURL('/');
  51. if (\OC\Share\Helper::isSameUserOnSameServer($owner, $remote, $currentUser, $currentServer )) {
  52. \OCP\JSON::error(array('data' => array('message' => $l->t('Not allowed to create a federated share with the same user server'))));
  53. exit();
  54. }
  55. $discoveryManager = new \OCA\FederatedFileSharing\DiscoveryManager(
  56. \OC::$server->getMemCacheFactory(),
  57. \OC::$server->getHTTPClientService()
  58. );
  59. $externalManager = new \OCA\Files_Sharing\External\Manager(
  60. \OC::$server->getDatabaseConnection(),
  61. \OC\Files\Filesystem::getMountManager(),
  62. \OC\Files\Filesystem::getLoader(),
  63. \OC::$server->getHTTPHelper(),
  64. \OC::$server->getNotificationManager(),
  65. $discoveryManager,
  66. \OC::$server->getUserSession()->getUser()->getUID()
  67. );
  68. // check for ssl cert
  69. if (substr($remote, 0, 5) === 'https') {
  70. try {
  71. \OC::$server->getHTTPClientService()->newClient()->get($remote, [
  72. 'timeout' => 10,
  73. 'connect_timeout' => 10,
  74. ])->getBody();
  75. } catch (\Exception $e) {
  76. \OCP\JSON::error(array('data' => array('message' => $l->t('Invalid or untrusted SSL certificate'))));
  77. exit;
  78. }
  79. }
  80. $mount = $externalManager->addShare($remote, $token, $password, $name, $ownerDisplayName, true);
  81. /**
  82. * @var \OCA\Files_Sharing\External\Storage $storage
  83. */
  84. $storage = $mount->getStorage();
  85. try {
  86. // check if storage exists
  87. $storage->checkStorageAvailability();
  88. } catch (\OCP\Files\StorageInvalidException $e) {
  89. // note: checkStorageAvailability will already remove the invalid share
  90. \OCP\Util::writeLog(
  91. 'files_sharing',
  92. 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(),
  93. \OCP\Util::DEBUG
  94. );
  95. \OCP\JSON::error(
  96. array(
  97. 'data' => array(
  98. 'message' => $l->t('Could not authenticate to remote share, password might be wrong')
  99. )
  100. )
  101. );
  102. exit();
  103. } catch (\Exception $e) {
  104. \OCP\Util::writeLog(
  105. 'files_sharing',
  106. 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(),
  107. \OCP\Util::DEBUG
  108. );
  109. $externalManager->removeShare($mount->getMountPoint());
  110. \OCP\JSON::error(array('data' => array('message' => $l->t('Storage not valid'))));
  111. exit();
  112. }
  113. $result = $storage->file_exists('');
  114. if ($result) {
  115. try {
  116. $storage->getScanner()->scanAll();
  117. \OCP\JSON::success();
  118. } catch (\OCP\Files\StorageInvalidException $e) {
  119. \OCP\Util::writeLog(
  120. 'files_sharing',
  121. 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(),
  122. \OCP\Util::DEBUG
  123. );
  124. \OCP\JSON::error(array('data' => array('message' => $l->t('Storage not valid'))));
  125. } catch (\Exception $e) {
  126. \OCP\Util::writeLog(
  127. 'files_sharing',
  128. 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(),
  129. \OCP\Util::DEBUG
  130. );
  131. \OCP\JSON::error(array('data' => array('message' => $l->t('Couldn\'t add remote share'))));
  132. }
  133. } else {
  134. $externalManager->removeShare($mount->getMountPoint());
  135. \OCP\Util::writeLog(
  136. 'files_sharing',
  137. 'Couldn\'t add remote share',
  138. \OCP\Util::DEBUG
  139. );
  140. \OCP\JSON::error(array('data' => array('message' => $l->t('Couldn\'t add remote share'))));
  141. }