external.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Vincent Petry <pvince81@owncloud.com>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. OCP\JSON::callCheck();
  26. OCP\JSON::checkLoggedIn();
  27. OCP\JSON::checkAppEnabled('files_sharing');
  28. $l = \OC::$server->getL10N('files_sharing');
  29. // check if server admin allows to mount public links from other servers
  30. if (OCA\Files_Sharing\Helper::isIncomingServer2serverShareEnabled() === false) {
  31. \OCP\JSON::error(array('data' => array('message' => $l->t('Server to server sharing is not enabled on this server'))));
  32. exit();
  33. }
  34. $token = $_POST['token'];
  35. $remote = $_POST['remote'];
  36. $owner = $_POST['owner'];
  37. $name = $_POST['name'];
  38. $password = $_POST['password'];
  39. // Check for invalid name
  40. if(!\OCP\Util::isValidFileName($name)) {
  41. \OCP\JSON::error(array('data' => array('message' => $l->t('The mountpoint name contains invalid characters.'))));
  42. exit();
  43. }
  44. $externalManager = new \OCA\Files_Sharing\External\Manager(
  45. \OC::$server->getDatabaseConnection(),
  46. \OC\Files\Filesystem::getMountManager(),
  47. \OC\Files\Filesystem::getLoader(),
  48. \OC::$server->getHTTPHelper(),
  49. \OC::$server->getUserSession()->getUser()->getUID()
  50. );
  51. // check for ssl cert
  52. if (substr($remote, 0, 5) === 'https') {
  53. try {
  54. \OC::$server->getHTTPClientService()->newClient()->get($remote)->getBody();
  55. } catch (\Exception $e) {
  56. \OCP\JSON::error(array('data' => array('message' => $l->t('Invalid or untrusted SSL certificate'))));
  57. exit;
  58. }
  59. }
  60. $mount = $externalManager->addShare($remote, $token, $password, $name, $owner, true);
  61. /**
  62. * @var \OCA\Files_Sharing\External\Storage $storage
  63. */
  64. $storage = $mount->getStorage();
  65. try {
  66. // check if storage exists
  67. $storage->checkStorageAvailability();
  68. } catch (\OCP\Files\StorageInvalidException $e) {
  69. // note: checkStorageAvailability will already remove the invalid share
  70. \OCP\Util::writeLog(
  71. 'files_sharing',
  72. 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(),
  73. \OCP\Util::DEBUG
  74. );
  75. \OCP\JSON::error(
  76. array(
  77. 'data' => array(
  78. 'message' => $l->t('Could not authenticate to remote share, password might be wrong')
  79. )
  80. )
  81. );
  82. exit();
  83. } catch (\Exception $e) {
  84. \OCP\Util::writeLog(
  85. 'files_sharing',
  86. 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(),
  87. \OCP\Util::DEBUG
  88. );
  89. $externalManager->removeShare($mount->getMountPoint());
  90. \OCP\JSON::error(array('data' => array('message' => $l->t('Storage not valid'))));
  91. exit();
  92. }
  93. $result = $storage->file_exists('');
  94. if ($result) {
  95. try {
  96. $storage->getScanner()->scanAll();
  97. \OCP\JSON::success();
  98. } catch (\OCP\Files\StorageInvalidException $e) {
  99. \OCP\Util::writeLog(
  100. 'files_sharing',
  101. 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(),
  102. \OCP\Util::DEBUG
  103. );
  104. \OCP\JSON::error(array('data' => array('message' => $l->t('Storage not valid'))));
  105. } catch (\Exception $e) {
  106. \OCP\Util::writeLog(
  107. 'files_sharing',
  108. 'Invalid remote storage: ' . get_class($e) . ': ' . $e->getMessage(),
  109. \OCP\Util::DEBUG
  110. );
  111. \OCP\JSON::error(array('data' => array('message' => $l->t('Couldn\'t add remote share'))));
  112. }
  113. } else {
  114. $externalManager->removeShare($mount->getMountPoint());
  115. \OCP\Util::writeLog(
  116. 'files_sharing',
  117. 'Couldn\'t add remote share',
  118. \OCP\Util::DEBUG
  119. );
  120. \OCP\JSON::error(array('data' => array('message' => $l->t('Couldn\'t add remote share'))));
  121. }