@@ -33,151 +33,151 @@ |
||
| 33 | 33 | |
| 34 | 34 | class RemoteController extends OCSController { |
| 35 | 35 | |
| 36 | - /** @var Manager */ |
|
| 37 | - private $externalManager; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * @NoAdminRequired |
|
| 41 | - * |
|
| 42 | - * Remote constructor. |
|
| 43 | - * |
|
| 44 | - * @param string $appName |
|
| 45 | - * @param IRequest $request |
|
| 46 | - * @param Manager $externalManager |
|
| 47 | - */ |
|
| 48 | - public function __construct($appName, |
|
| 49 | - IRequest $request, |
|
| 50 | - Manager $externalManager) { |
|
| 51 | - parent::__construct($appName, $request); |
|
| 52 | - |
|
| 53 | - $this->externalManager = $externalManager; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @NoAdminRequired |
|
| 58 | - * |
|
| 59 | - * Get list of pending remote shares |
|
| 60 | - * |
|
| 61 | - * @return DataResponse |
|
| 62 | - */ |
|
| 63 | - public function getOpenShares() { |
|
| 64 | - return new DataResponse($this->externalManager->getOpenShares()); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @NoAdminRequired |
|
| 69 | - * |
|
| 70 | - * Accept a remote share |
|
| 71 | - * |
|
| 72 | - * @param int $id |
|
| 73 | - * @return DataResponse |
|
| 74 | - * @throws OCSNotFoundException |
|
| 75 | - */ |
|
| 76 | - public function acceptShare($id) { |
|
| 77 | - if ($this->externalManager->acceptShare($id)) { |
|
| 78 | - return new DataResponse(); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - // Make sure the user has no notification for something that does not exist anymore. |
|
| 82 | - $this->externalManager->processNotification($id); |
|
| 83 | - |
|
| 84 | - throw new OCSNotFoundException('wrong share ID, share doesn\'t exist.'); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @NoAdminRequired |
|
| 89 | - * |
|
| 90 | - * Decline a remote share |
|
| 91 | - * |
|
| 92 | - * @param int $id |
|
| 93 | - * @return DataResponse |
|
| 94 | - * @throws OCSNotFoundException |
|
| 95 | - */ |
|
| 96 | - public function declineShare($id) { |
|
| 97 | - if ($this->externalManager->declineShare($id)) { |
|
| 98 | - return new DataResponse(); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // Make sure the user has no notification for something that does not exist anymore. |
|
| 102 | - $this->externalManager->processNotification($id); |
|
| 103 | - |
|
| 104 | - throw new OCSNotFoundException('wrong share ID, share doesn\'t exist.'); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @param array $share Share with info from the share_external table |
|
| 109 | - * @return array enriched share info with data from the filecache |
|
| 110 | - */ |
|
| 111 | - private static function extendShareInfo($share) { |
|
| 112 | - $view = new \OC\Files\View('/' . \OC_User::getUser() . '/files/'); |
|
| 113 | - $info = $view->getFileInfo($share['mountpoint']); |
|
| 114 | - |
|
| 115 | - $share['mimetype'] = $info->getMimetype(); |
|
| 116 | - $share['mtime'] = $info->getMTime(); |
|
| 117 | - $share['permissions'] = $info->getPermissions(); |
|
| 118 | - $share['type'] = $info->getType(); |
|
| 119 | - $share['file_id'] = $info->getId(); |
|
| 120 | - |
|
| 121 | - return $share; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @NoAdminRequired |
|
| 126 | - * |
|
| 127 | - * List accepted remote shares |
|
| 128 | - * |
|
| 129 | - * @return DataResponse |
|
| 130 | - */ |
|
| 131 | - public function getShares() { |
|
| 132 | - $shares = $this->externalManager->getAcceptedShares(); |
|
| 133 | - $shares = array_map('self::extendShareInfo', $shares); |
|
| 134 | - |
|
| 135 | - return new DataResponse($shares); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @NoAdminRequired |
|
| 140 | - * |
|
| 141 | - * Get info of a remote share |
|
| 142 | - * |
|
| 143 | - * @param int $id |
|
| 144 | - * @return DataResponse |
|
| 145 | - * @throws OCSNotFoundException |
|
| 146 | - */ |
|
| 147 | - public function getShare($id) { |
|
| 148 | - $shareInfo = $this->externalManager->getShare($id); |
|
| 149 | - |
|
| 150 | - if ($shareInfo === false) { |
|
| 151 | - throw new OCSNotFoundException('share does not exist'); |
|
| 152 | - } else { |
|
| 153 | - $shareInfo = self::extendShareInfo($shareInfo); |
|
| 154 | - return new DataResponse($shareInfo); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * @NoAdminRequired |
|
| 160 | - * |
|
| 161 | - * Unshare a remote share |
|
| 162 | - * |
|
| 163 | - * @param int $id |
|
| 164 | - * @return DataResponse |
|
| 165 | - * @throws OCSNotFoundException |
|
| 166 | - * @throws OCSForbiddenException |
|
| 167 | - */ |
|
| 168 | - public function unshare($id) { |
|
| 169 | - $shareInfo = $this->externalManager->getShare($id); |
|
| 170 | - |
|
| 171 | - if ($shareInfo === false) { |
|
| 172 | - throw new OCSNotFoundException('Share does not exist'); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - $mountPoint = '/' . \OC_User::getUser() . '/files' . $shareInfo['mountpoint']; |
|
| 176 | - |
|
| 177 | - if ($this->externalManager->removeShare($mountPoint) === true) { |
|
| 178 | - return new DataResponse(); |
|
| 179 | - } else { |
|
| 180 | - throw new OCSForbiddenException('Could not unshare'); |
|
| 181 | - } |
|
| 182 | - } |
|
| 36 | + /** @var Manager */ |
|
| 37 | + private $externalManager; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * @NoAdminRequired |
|
| 41 | + * |
|
| 42 | + * Remote constructor. |
|
| 43 | + * |
|
| 44 | + * @param string $appName |
|
| 45 | + * @param IRequest $request |
|
| 46 | + * @param Manager $externalManager |
|
| 47 | + */ |
|
| 48 | + public function __construct($appName, |
|
| 49 | + IRequest $request, |
|
| 50 | + Manager $externalManager) { |
|
| 51 | + parent::__construct($appName, $request); |
|
| 52 | + |
|
| 53 | + $this->externalManager = $externalManager; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @NoAdminRequired |
|
| 58 | + * |
|
| 59 | + * Get list of pending remote shares |
|
| 60 | + * |
|
| 61 | + * @return DataResponse |
|
| 62 | + */ |
|
| 63 | + public function getOpenShares() { |
|
| 64 | + return new DataResponse($this->externalManager->getOpenShares()); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @NoAdminRequired |
|
| 69 | + * |
|
| 70 | + * Accept a remote share |
|
| 71 | + * |
|
| 72 | + * @param int $id |
|
| 73 | + * @return DataResponse |
|
| 74 | + * @throws OCSNotFoundException |
|
| 75 | + */ |
|
| 76 | + public function acceptShare($id) { |
|
| 77 | + if ($this->externalManager->acceptShare($id)) { |
|
| 78 | + return new DataResponse(); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + // Make sure the user has no notification for something that does not exist anymore. |
|
| 82 | + $this->externalManager->processNotification($id); |
|
| 83 | + |
|
| 84 | + throw new OCSNotFoundException('wrong share ID, share doesn\'t exist.'); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @NoAdminRequired |
|
| 89 | + * |
|
| 90 | + * Decline a remote share |
|
| 91 | + * |
|
| 92 | + * @param int $id |
|
| 93 | + * @return DataResponse |
|
| 94 | + * @throws OCSNotFoundException |
|
| 95 | + */ |
|
| 96 | + public function declineShare($id) { |
|
| 97 | + if ($this->externalManager->declineShare($id)) { |
|
| 98 | + return new DataResponse(); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + // Make sure the user has no notification for something that does not exist anymore. |
|
| 102 | + $this->externalManager->processNotification($id); |
|
| 103 | + |
|
| 104 | + throw new OCSNotFoundException('wrong share ID, share doesn\'t exist.'); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @param array $share Share with info from the share_external table |
|
| 109 | + * @return array enriched share info with data from the filecache |
|
| 110 | + */ |
|
| 111 | + private static function extendShareInfo($share) { |
|
| 112 | + $view = new \OC\Files\View('/' . \OC_User::getUser() . '/files/'); |
|
| 113 | + $info = $view->getFileInfo($share['mountpoint']); |
|
| 114 | + |
|
| 115 | + $share['mimetype'] = $info->getMimetype(); |
|
| 116 | + $share['mtime'] = $info->getMTime(); |
|
| 117 | + $share['permissions'] = $info->getPermissions(); |
|
| 118 | + $share['type'] = $info->getType(); |
|
| 119 | + $share['file_id'] = $info->getId(); |
|
| 120 | + |
|
| 121 | + return $share; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @NoAdminRequired |
|
| 126 | + * |
|
| 127 | + * List accepted remote shares |
|
| 128 | + * |
|
| 129 | + * @return DataResponse |
|
| 130 | + */ |
|
| 131 | + public function getShares() { |
|
| 132 | + $shares = $this->externalManager->getAcceptedShares(); |
|
| 133 | + $shares = array_map('self::extendShareInfo', $shares); |
|
| 134 | + |
|
| 135 | + return new DataResponse($shares); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @NoAdminRequired |
|
| 140 | + * |
|
| 141 | + * Get info of a remote share |
|
| 142 | + * |
|
| 143 | + * @param int $id |
|
| 144 | + * @return DataResponse |
|
| 145 | + * @throws OCSNotFoundException |
|
| 146 | + */ |
|
| 147 | + public function getShare($id) { |
|
| 148 | + $shareInfo = $this->externalManager->getShare($id); |
|
| 149 | + |
|
| 150 | + if ($shareInfo === false) { |
|
| 151 | + throw new OCSNotFoundException('share does not exist'); |
|
| 152 | + } else { |
|
| 153 | + $shareInfo = self::extendShareInfo($shareInfo); |
|
| 154 | + return new DataResponse($shareInfo); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * @NoAdminRequired |
|
| 160 | + * |
|
| 161 | + * Unshare a remote share |
|
| 162 | + * |
|
| 163 | + * @param int $id |
|
| 164 | + * @return DataResponse |
|
| 165 | + * @throws OCSNotFoundException |
|
| 166 | + * @throws OCSForbiddenException |
|
| 167 | + */ |
|
| 168 | + public function unshare($id) { |
|
| 169 | + $shareInfo = $this->externalManager->getShare($id); |
|
| 170 | + |
|
| 171 | + if ($shareInfo === false) { |
|
| 172 | + throw new OCSNotFoundException('Share does not exist'); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + $mountPoint = '/' . \OC_User::getUser() . '/files' . $shareInfo['mountpoint']; |
|
| 176 | + |
|
| 177 | + if ($this->externalManager->removeShare($mountPoint) === true) { |
|
| 178 | + return new DataResponse(); |
|
| 179 | + } else { |
|
| 180 | + throw new OCSForbiddenException('Could not unshare'); |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | 183 | } |
@@ -64,558 +64,558 @@ |
||
| 64 | 64 | */ |
| 65 | 65 | class ShareController extends Controller { |
| 66 | 66 | |
| 67 | - /** @var IConfig */ |
|
| 68 | - protected $config; |
|
| 69 | - /** @var IURLGenerator */ |
|
| 70 | - protected $urlGenerator; |
|
| 71 | - /** @var IUserManager */ |
|
| 72 | - protected $userManager; |
|
| 73 | - /** @var ILogger */ |
|
| 74 | - protected $logger; |
|
| 75 | - /** @var \OCP\Activity\IManager */ |
|
| 76 | - protected $activityManager; |
|
| 77 | - /** @var \OCP\Share\IManager */ |
|
| 78 | - protected $shareManager; |
|
| 79 | - /** @var ISession */ |
|
| 80 | - protected $session; |
|
| 81 | - /** @var IPreview */ |
|
| 82 | - protected $previewManager; |
|
| 83 | - /** @var IRootFolder */ |
|
| 84 | - protected $rootFolder; |
|
| 85 | - /** @var FederatedShareProvider */ |
|
| 86 | - protected $federatedShareProvider; |
|
| 87 | - /** @var EventDispatcherInterface */ |
|
| 88 | - protected $eventDispatcher; |
|
| 89 | - /** @var IL10N */ |
|
| 90 | - protected $l10n; |
|
| 91 | - /** @var Defaults */ |
|
| 92 | - protected $defaults; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @param string $appName |
|
| 96 | - * @param IRequest $request |
|
| 97 | - * @param IConfig $config |
|
| 98 | - * @param IURLGenerator $urlGenerator |
|
| 99 | - * @param IUserManager $userManager |
|
| 100 | - * @param ILogger $logger |
|
| 101 | - * @param \OCP\Activity\IManager $activityManager |
|
| 102 | - * @param \OCP\Share\IManager $shareManager |
|
| 103 | - * @param ISession $session |
|
| 104 | - * @param IPreview $previewManager |
|
| 105 | - * @param IRootFolder $rootFolder |
|
| 106 | - * @param FederatedShareProvider $federatedShareProvider |
|
| 107 | - * @param EventDispatcherInterface $eventDispatcher |
|
| 108 | - * @param IL10N $l10n |
|
| 109 | - * @param \OC_Defaults $defaults |
|
| 110 | - */ |
|
| 111 | - public function __construct($appName, |
|
| 112 | - IRequest $request, |
|
| 113 | - IConfig $config, |
|
| 114 | - IURLGenerator $urlGenerator, |
|
| 115 | - IUserManager $userManager, |
|
| 116 | - ILogger $logger, |
|
| 117 | - \OCP\Activity\IManager $activityManager, |
|
| 118 | - \OCP\Share\IManager $shareManager, |
|
| 119 | - ISession $session, |
|
| 120 | - IPreview $previewManager, |
|
| 121 | - IRootFolder $rootFolder, |
|
| 122 | - FederatedShareProvider $federatedShareProvider, |
|
| 123 | - EventDispatcherInterface $eventDispatcher, |
|
| 124 | - IL10N $l10n, |
|
| 125 | - \OC_Defaults $defaults) { |
|
| 126 | - parent::__construct($appName, $request); |
|
| 127 | - |
|
| 128 | - $this->config = $config; |
|
| 129 | - $this->urlGenerator = $urlGenerator; |
|
| 130 | - $this->userManager = $userManager; |
|
| 131 | - $this->logger = $logger; |
|
| 132 | - $this->activityManager = $activityManager; |
|
| 133 | - $this->shareManager = $shareManager; |
|
| 134 | - $this->session = $session; |
|
| 135 | - $this->previewManager = $previewManager; |
|
| 136 | - $this->rootFolder = $rootFolder; |
|
| 137 | - $this->federatedShareProvider = $federatedShareProvider; |
|
| 138 | - $this->eventDispatcher = $eventDispatcher; |
|
| 139 | - $this->l10n = $l10n; |
|
| 140 | - $this->defaults = $defaults; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * @PublicPage |
|
| 145 | - * @NoCSRFRequired |
|
| 146 | - * |
|
| 147 | - * @param string $token |
|
| 148 | - * @return TemplateResponse|RedirectResponse |
|
| 149 | - */ |
|
| 150 | - public function showAuthenticate($token) { |
|
| 151 | - $share = $this->shareManager->getShareByToken($token); |
|
| 152 | - |
|
| 153 | - if($this->linkShareAuth($share)) { |
|
| 154 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return new TemplateResponse($this->appName, 'authenticate', array(), 'guest'); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * @PublicPage |
|
| 162 | - * @UseSession |
|
| 163 | - * @BruteForceProtection publicLinkAuth |
|
| 164 | - * |
|
| 165 | - * Authenticates against password-protected shares |
|
| 166 | - * @param string $token |
|
| 167 | - * @param string $password |
|
| 168 | - * @return RedirectResponse|TemplateResponse|NotFoundResponse |
|
| 169 | - */ |
|
| 170 | - public function authenticate($token, $password = '') { |
|
| 171 | - |
|
| 172 | - // Check whether share exists |
|
| 173 | - try { |
|
| 174 | - $share = $this->shareManager->getShareByToken($token); |
|
| 175 | - } catch (ShareNotFound $e) { |
|
| 176 | - return new NotFoundResponse(); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - $authenticate = $this->linkShareAuth($share, $password); |
|
| 180 | - |
|
| 181 | - if($authenticate === true) { |
|
| 182 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - return new TemplateResponse($this->appName, 'authenticate', array('wrongpw' => true), 'guest'); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Authenticate a link item with the given password. |
|
| 190 | - * Or use the session if no password is provided. |
|
| 191 | - * |
|
| 192 | - * This is a modified version of Helper::authenticate |
|
| 193 | - * TODO: Try to merge back eventually with Helper::authenticate |
|
| 194 | - * |
|
| 195 | - * @param \OCP\Share\IShare $share |
|
| 196 | - * @param string|null $password |
|
| 197 | - * @return bool |
|
| 198 | - */ |
|
| 199 | - private function linkShareAuth(\OCP\Share\IShare $share, $password = null) { |
|
| 200 | - if ($password !== null) { |
|
| 201 | - if ($this->shareManager->checkPassword($share, $password)) { |
|
| 202 | - $this->session->set('public_link_authenticated', (string)$share->getId()); |
|
| 203 | - } else { |
|
| 204 | - $this->emitAccessShareHook($share, 403, 'Wrong password'); |
|
| 205 | - return false; |
|
| 206 | - } |
|
| 207 | - } else { |
|
| 208 | - // not authenticated ? |
|
| 209 | - if ( ! $this->session->exists('public_link_authenticated') |
|
| 210 | - || $this->session->get('public_link_authenticated') !== (string)$share->getId()) { |
|
| 211 | - return false; |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - return true; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * throws hooks when a share is attempted to be accessed |
|
| 219 | - * |
|
| 220 | - * @param \OCP\Share\IShare|string $share the Share instance if available, |
|
| 221 | - * otherwise token |
|
| 222 | - * @param int $errorCode |
|
| 223 | - * @param string $errorMessage |
|
| 224 | - * @throws \OC\HintException |
|
| 225 | - * @throws \OC\ServerNotAvailableException |
|
| 226 | - */ |
|
| 227 | - protected function emitAccessShareHook($share, $errorCode = 200, $errorMessage = '') { |
|
| 228 | - $itemType = $itemSource = $uidOwner = ''; |
|
| 229 | - $token = $share; |
|
| 230 | - $exception = null; |
|
| 231 | - if($share instanceof \OCP\Share\IShare) { |
|
| 232 | - try { |
|
| 233 | - $token = $share->getToken(); |
|
| 234 | - $uidOwner = $share->getSharedBy(); |
|
| 235 | - $itemType = $share->getNodeType(); |
|
| 236 | - $itemSource = $share->getNodeId(); |
|
| 237 | - } catch (\Exception $e) { |
|
| 238 | - // we log what we know and pass on the exception afterwards |
|
| 239 | - $exception = $e; |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - \OC_Hook::emit('OCP\Share', 'share_link_access', [ |
|
| 243 | - 'itemType' => $itemType, |
|
| 244 | - 'itemSource' => $itemSource, |
|
| 245 | - 'uidOwner' => $uidOwner, |
|
| 246 | - 'token' => $token, |
|
| 247 | - 'errorCode' => $errorCode, |
|
| 248 | - 'errorMessage' => $errorMessage, |
|
| 249 | - ]); |
|
| 250 | - if(!is_null($exception)) { |
|
| 251 | - throw $exception; |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Validate the permissions of the share |
|
| 257 | - * |
|
| 258 | - * @param Share\IShare $share |
|
| 259 | - * @return bool |
|
| 260 | - */ |
|
| 261 | - private function validateShare(\OCP\Share\IShare $share) { |
|
| 262 | - return $share->getNode()->isReadable() && $share->getNode()->isShareable(); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * @PublicPage |
|
| 267 | - * @NoCSRFRequired |
|
| 268 | - * |
|
| 269 | - * @param string $token |
|
| 270 | - * @param string $path |
|
| 271 | - * @return TemplateResponse|RedirectResponse|NotFoundResponse |
|
| 272 | - * @throws NotFoundException |
|
| 273 | - * @throws \Exception |
|
| 274 | - */ |
|
| 275 | - public function showShare($token, $path = '') { |
|
| 276 | - \OC_User::setIncognitoMode(true); |
|
| 277 | - |
|
| 278 | - // Check whether share exists |
|
| 279 | - try { |
|
| 280 | - $share = $this->shareManager->getShareByToken($token); |
|
| 281 | - } catch (ShareNotFound $e) { |
|
| 282 | - $this->emitAccessShareHook($token, 404, 'Share not found'); |
|
| 283 | - return new NotFoundResponse(); |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - // Share is password protected - check whether the user is permitted to access the share |
|
| 287 | - if ($share->getPassword() !== null && !$this->linkShareAuth($share)) { |
|
| 288 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', |
|
| 289 | - array('token' => $token))); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - if (!$this->validateShare($share)) { |
|
| 293 | - throw new NotFoundException(); |
|
| 294 | - } |
|
| 295 | - // We can't get the path of a file share |
|
| 296 | - try { |
|
| 297 | - if ($share->getNode() instanceof \OCP\Files\File && $path !== '') { |
|
| 298 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 299 | - throw new NotFoundException(); |
|
| 300 | - } |
|
| 301 | - } catch (\Exception $e) { |
|
| 302 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 303 | - throw $e; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - $shareTmpl = []; |
|
| 307 | - $shareTmpl['displayName'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 308 | - $shareTmpl['owner'] = $share->getShareOwner(); |
|
| 309 | - $shareTmpl['filename'] = $share->getNode()->getName(); |
|
| 310 | - $shareTmpl['directory_path'] = $share->getTarget(); |
|
| 311 | - $shareTmpl['mimetype'] = $share->getNode()->getMimetype(); |
|
| 312 | - $shareTmpl['previewSupported'] = $this->previewManager->isMimeSupported($share->getNode()->getMimetype()); |
|
| 313 | - $shareTmpl['dirToken'] = $token; |
|
| 314 | - $shareTmpl['sharingToken'] = $token; |
|
| 315 | - $shareTmpl['server2serversharing'] = $this->federatedShareProvider->isOutgoingServer2serverShareEnabled(); |
|
| 316 | - $shareTmpl['protected'] = $share->getPassword() !== null ? 'true' : 'false'; |
|
| 317 | - $shareTmpl['dir'] = ''; |
|
| 318 | - $shareTmpl['nonHumanFileSize'] = $share->getNode()->getSize(); |
|
| 319 | - $shareTmpl['fileSize'] = \OCP\Util::humanFileSize($share->getNode()->getSize()); |
|
| 320 | - |
|
| 321 | - // Show file list |
|
| 322 | - $hideFileList = false; |
|
| 323 | - if ($share->getNode() instanceof \OCP\Files\Folder) { |
|
| 324 | - /** @var \OCP\Files\Folder $rootFolder */ |
|
| 325 | - $rootFolder = $share->getNode(); |
|
| 326 | - |
|
| 327 | - try { |
|
| 328 | - $folderNode = $rootFolder->get($path); |
|
| 329 | - } catch (\OCP\Files\NotFoundException $e) { |
|
| 330 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 331 | - throw new NotFoundException(); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - $shareTmpl['dir'] = $rootFolder->getRelativePath($folderNode->getPath()); |
|
| 335 | - |
|
| 336 | - /* |
|
| 67 | + /** @var IConfig */ |
|
| 68 | + protected $config; |
|
| 69 | + /** @var IURLGenerator */ |
|
| 70 | + protected $urlGenerator; |
|
| 71 | + /** @var IUserManager */ |
|
| 72 | + protected $userManager; |
|
| 73 | + /** @var ILogger */ |
|
| 74 | + protected $logger; |
|
| 75 | + /** @var \OCP\Activity\IManager */ |
|
| 76 | + protected $activityManager; |
|
| 77 | + /** @var \OCP\Share\IManager */ |
|
| 78 | + protected $shareManager; |
|
| 79 | + /** @var ISession */ |
|
| 80 | + protected $session; |
|
| 81 | + /** @var IPreview */ |
|
| 82 | + protected $previewManager; |
|
| 83 | + /** @var IRootFolder */ |
|
| 84 | + protected $rootFolder; |
|
| 85 | + /** @var FederatedShareProvider */ |
|
| 86 | + protected $federatedShareProvider; |
|
| 87 | + /** @var EventDispatcherInterface */ |
|
| 88 | + protected $eventDispatcher; |
|
| 89 | + /** @var IL10N */ |
|
| 90 | + protected $l10n; |
|
| 91 | + /** @var Defaults */ |
|
| 92 | + protected $defaults; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @param string $appName |
|
| 96 | + * @param IRequest $request |
|
| 97 | + * @param IConfig $config |
|
| 98 | + * @param IURLGenerator $urlGenerator |
|
| 99 | + * @param IUserManager $userManager |
|
| 100 | + * @param ILogger $logger |
|
| 101 | + * @param \OCP\Activity\IManager $activityManager |
|
| 102 | + * @param \OCP\Share\IManager $shareManager |
|
| 103 | + * @param ISession $session |
|
| 104 | + * @param IPreview $previewManager |
|
| 105 | + * @param IRootFolder $rootFolder |
|
| 106 | + * @param FederatedShareProvider $federatedShareProvider |
|
| 107 | + * @param EventDispatcherInterface $eventDispatcher |
|
| 108 | + * @param IL10N $l10n |
|
| 109 | + * @param \OC_Defaults $defaults |
|
| 110 | + */ |
|
| 111 | + public function __construct($appName, |
|
| 112 | + IRequest $request, |
|
| 113 | + IConfig $config, |
|
| 114 | + IURLGenerator $urlGenerator, |
|
| 115 | + IUserManager $userManager, |
|
| 116 | + ILogger $logger, |
|
| 117 | + \OCP\Activity\IManager $activityManager, |
|
| 118 | + \OCP\Share\IManager $shareManager, |
|
| 119 | + ISession $session, |
|
| 120 | + IPreview $previewManager, |
|
| 121 | + IRootFolder $rootFolder, |
|
| 122 | + FederatedShareProvider $federatedShareProvider, |
|
| 123 | + EventDispatcherInterface $eventDispatcher, |
|
| 124 | + IL10N $l10n, |
|
| 125 | + \OC_Defaults $defaults) { |
|
| 126 | + parent::__construct($appName, $request); |
|
| 127 | + |
|
| 128 | + $this->config = $config; |
|
| 129 | + $this->urlGenerator = $urlGenerator; |
|
| 130 | + $this->userManager = $userManager; |
|
| 131 | + $this->logger = $logger; |
|
| 132 | + $this->activityManager = $activityManager; |
|
| 133 | + $this->shareManager = $shareManager; |
|
| 134 | + $this->session = $session; |
|
| 135 | + $this->previewManager = $previewManager; |
|
| 136 | + $this->rootFolder = $rootFolder; |
|
| 137 | + $this->federatedShareProvider = $federatedShareProvider; |
|
| 138 | + $this->eventDispatcher = $eventDispatcher; |
|
| 139 | + $this->l10n = $l10n; |
|
| 140 | + $this->defaults = $defaults; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * @PublicPage |
|
| 145 | + * @NoCSRFRequired |
|
| 146 | + * |
|
| 147 | + * @param string $token |
|
| 148 | + * @return TemplateResponse|RedirectResponse |
|
| 149 | + */ |
|
| 150 | + public function showAuthenticate($token) { |
|
| 151 | + $share = $this->shareManager->getShareByToken($token); |
|
| 152 | + |
|
| 153 | + if($this->linkShareAuth($share)) { |
|
| 154 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return new TemplateResponse($this->appName, 'authenticate', array(), 'guest'); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * @PublicPage |
|
| 162 | + * @UseSession |
|
| 163 | + * @BruteForceProtection publicLinkAuth |
|
| 164 | + * |
|
| 165 | + * Authenticates against password-protected shares |
|
| 166 | + * @param string $token |
|
| 167 | + * @param string $password |
|
| 168 | + * @return RedirectResponse|TemplateResponse|NotFoundResponse |
|
| 169 | + */ |
|
| 170 | + public function authenticate($token, $password = '') { |
|
| 171 | + |
|
| 172 | + // Check whether share exists |
|
| 173 | + try { |
|
| 174 | + $share = $this->shareManager->getShareByToken($token); |
|
| 175 | + } catch (ShareNotFound $e) { |
|
| 176 | + return new NotFoundResponse(); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + $authenticate = $this->linkShareAuth($share, $password); |
|
| 180 | + |
|
| 181 | + if($authenticate === true) { |
|
| 182 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.showShare', array('token' => $token))); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + return new TemplateResponse($this->appName, 'authenticate', array('wrongpw' => true), 'guest'); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Authenticate a link item with the given password. |
|
| 190 | + * Or use the session if no password is provided. |
|
| 191 | + * |
|
| 192 | + * This is a modified version of Helper::authenticate |
|
| 193 | + * TODO: Try to merge back eventually with Helper::authenticate |
|
| 194 | + * |
|
| 195 | + * @param \OCP\Share\IShare $share |
|
| 196 | + * @param string|null $password |
|
| 197 | + * @return bool |
|
| 198 | + */ |
|
| 199 | + private function linkShareAuth(\OCP\Share\IShare $share, $password = null) { |
|
| 200 | + if ($password !== null) { |
|
| 201 | + if ($this->shareManager->checkPassword($share, $password)) { |
|
| 202 | + $this->session->set('public_link_authenticated', (string)$share->getId()); |
|
| 203 | + } else { |
|
| 204 | + $this->emitAccessShareHook($share, 403, 'Wrong password'); |
|
| 205 | + return false; |
|
| 206 | + } |
|
| 207 | + } else { |
|
| 208 | + // not authenticated ? |
|
| 209 | + if ( ! $this->session->exists('public_link_authenticated') |
|
| 210 | + || $this->session->get('public_link_authenticated') !== (string)$share->getId()) { |
|
| 211 | + return false; |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + return true; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * throws hooks when a share is attempted to be accessed |
|
| 219 | + * |
|
| 220 | + * @param \OCP\Share\IShare|string $share the Share instance if available, |
|
| 221 | + * otherwise token |
|
| 222 | + * @param int $errorCode |
|
| 223 | + * @param string $errorMessage |
|
| 224 | + * @throws \OC\HintException |
|
| 225 | + * @throws \OC\ServerNotAvailableException |
|
| 226 | + */ |
|
| 227 | + protected function emitAccessShareHook($share, $errorCode = 200, $errorMessage = '') { |
|
| 228 | + $itemType = $itemSource = $uidOwner = ''; |
|
| 229 | + $token = $share; |
|
| 230 | + $exception = null; |
|
| 231 | + if($share instanceof \OCP\Share\IShare) { |
|
| 232 | + try { |
|
| 233 | + $token = $share->getToken(); |
|
| 234 | + $uidOwner = $share->getSharedBy(); |
|
| 235 | + $itemType = $share->getNodeType(); |
|
| 236 | + $itemSource = $share->getNodeId(); |
|
| 237 | + } catch (\Exception $e) { |
|
| 238 | + // we log what we know and pass on the exception afterwards |
|
| 239 | + $exception = $e; |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | + \OC_Hook::emit('OCP\Share', 'share_link_access', [ |
|
| 243 | + 'itemType' => $itemType, |
|
| 244 | + 'itemSource' => $itemSource, |
|
| 245 | + 'uidOwner' => $uidOwner, |
|
| 246 | + 'token' => $token, |
|
| 247 | + 'errorCode' => $errorCode, |
|
| 248 | + 'errorMessage' => $errorMessage, |
|
| 249 | + ]); |
|
| 250 | + if(!is_null($exception)) { |
|
| 251 | + throw $exception; |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Validate the permissions of the share |
|
| 257 | + * |
|
| 258 | + * @param Share\IShare $share |
|
| 259 | + * @return bool |
|
| 260 | + */ |
|
| 261 | + private function validateShare(\OCP\Share\IShare $share) { |
|
| 262 | + return $share->getNode()->isReadable() && $share->getNode()->isShareable(); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * @PublicPage |
|
| 267 | + * @NoCSRFRequired |
|
| 268 | + * |
|
| 269 | + * @param string $token |
|
| 270 | + * @param string $path |
|
| 271 | + * @return TemplateResponse|RedirectResponse|NotFoundResponse |
|
| 272 | + * @throws NotFoundException |
|
| 273 | + * @throws \Exception |
|
| 274 | + */ |
|
| 275 | + public function showShare($token, $path = '') { |
|
| 276 | + \OC_User::setIncognitoMode(true); |
|
| 277 | + |
|
| 278 | + // Check whether share exists |
|
| 279 | + try { |
|
| 280 | + $share = $this->shareManager->getShareByToken($token); |
|
| 281 | + } catch (ShareNotFound $e) { |
|
| 282 | + $this->emitAccessShareHook($token, 404, 'Share not found'); |
|
| 283 | + return new NotFoundResponse(); |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + // Share is password protected - check whether the user is permitted to access the share |
|
| 287 | + if ($share->getPassword() !== null && !$this->linkShareAuth($share)) { |
|
| 288 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', |
|
| 289 | + array('token' => $token))); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + if (!$this->validateShare($share)) { |
|
| 293 | + throw new NotFoundException(); |
|
| 294 | + } |
|
| 295 | + // We can't get the path of a file share |
|
| 296 | + try { |
|
| 297 | + if ($share->getNode() instanceof \OCP\Files\File && $path !== '') { |
|
| 298 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 299 | + throw new NotFoundException(); |
|
| 300 | + } |
|
| 301 | + } catch (\Exception $e) { |
|
| 302 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 303 | + throw $e; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + $shareTmpl = []; |
|
| 307 | + $shareTmpl['displayName'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 308 | + $shareTmpl['owner'] = $share->getShareOwner(); |
|
| 309 | + $shareTmpl['filename'] = $share->getNode()->getName(); |
|
| 310 | + $shareTmpl['directory_path'] = $share->getTarget(); |
|
| 311 | + $shareTmpl['mimetype'] = $share->getNode()->getMimetype(); |
|
| 312 | + $shareTmpl['previewSupported'] = $this->previewManager->isMimeSupported($share->getNode()->getMimetype()); |
|
| 313 | + $shareTmpl['dirToken'] = $token; |
|
| 314 | + $shareTmpl['sharingToken'] = $token; |
|
| 315 | + $shareTmpl['server2serversharing'] = $this->federatedShareProvider->isOutgoingServer2serverShareEnabled(); |
|
| 316 | + $shareTmpl['protected'] = $share->getPassword() !== null ? 'true' : 'false'; |
|
| 317 | + $shareTmpl['dir'] = ''; |
|
| 318 | + $shareTmpl['nonHumanFileSize'] = $share->getNode()->getSize(); |
|
| 319 | + $shareTmpl['fileSize'] = \OCP\Util::humanFileSize($share->getNode()->getSize()); |
|
| 320 | + |
|
| 321 | + // Show file list |
|
| 322 | + $hideFileList = false; |
|
| 323 | + if ($share->getNode() instanceof \OCP\Files\Folder) { |
|
| 324 | + /** @var \OCP\Files\Folder $rootFolder */ |
|
| 325 | + $rootFolder = $share->getNode(); |
|
| 326 | + |
|
| 327 | + try { |
|
| 328 | + $folderNode = $rootFolder->get($path); |
|
| 329 | + } catch (\OCP\Files\NotFoundException $e) { |
|
| 330 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 331 | + throw new NotFoundException(); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + $shareTmpl['dir'] = $rootFolder->getRelativePath($folderNode->getPath()); |
|
| 335 | + |
|
| 336 | + /* |
|
| 337 | 337 | * The OC_Util methods require a view. This just uses the node API |
| 338 | 338 | */ |
| 339 | - $freeSpace = $share->getNode()->getStorage()->free_space($share->getNode()->getInternalPath()); |
|
| 340 | - if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 341 | - $freeSpace = max($freeSpace, 0); |
|
| 342 | - } else { |
|
| 343 | - $freeSpace = (INF > 0) ? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - $hideFileList = $share->getPermissions() & \OCP\Constants::PERMISSION_READ ? false : true; |
|
| 347 | - $maxUploadFilesize = $freeSpace; |
|
| 348 | - |
|
| 349 | - $folder = new Template('files', 'list', ''); |
|
| 350 | - $folder->assign('dir', $rootFolder->getRelativePath($folderNode->getPath())); |
|
| 351 | - $folder->assign('dirToken', $token); |
|
| 352 | - $folder->assign('permissions', \OCP\Constants::PERMISSION_READ); |
|
| 353 | - $folder->assign('isPublic', true); |
|
| 354 | - $folder->assign('hideFileList', $hideFileList); |
|
| 355 | - $folder->assign('publicUploadEnabled', 'no'); |
|
| 356 | - $folder->assign('uploadMaxFilesize', $maxUploadFilesize); |
|
| 357 | - $folder->assign('uploadMaxHumanFilesize', \OCP\Util::humanFileSize($maxUploadFilesize)); |
|
| 358 | - $folder->assign('freeSpace', $freeSpace); |
|
| 359 | - $folder->assign('usedSpacePercent', 0); |
|
| 360 | - $folder->assign('trash', false); |
|
| 361 | - $shareTmpl['folder'] = $folder->fetchPage(); |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - $shareTmpl['hideFileList'] = $hideFileList; |
|
| 365 | - $shareTmpl['shareOwner'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 366 | - $shareTmpl['downloadURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', ['token' => $token]); |
|
| 367 | - $shareTmpl['shareUrl'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]); |
|
| 368 | - $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10); |
|
| 369 | - $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true); |
|
| 370 | - $shareTmpl['previewMaxX'] = $this->config->getSystemValue('preview_max_x', 1024); |
|
| 371 | - $shareTmpl['previewMaxY'] = $this->config->getSystemValue('preview_max_y', 1024); |
|
| 372 | - $shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null); |
|
| 373 | - if ($shareTmpl['previewSupported']) { |
|
| 374 | - $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute( 'files_sharing.PublicPreview.getPreview', |
|
| 375 | - ['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 't' => $shareTmpl['dirToken']]); |
|
| 376 | - } else { |
|
| 377 | - $shareTmpl['previewImage'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png')); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - // Load files we need |
|
| 381 | - \OCP\Util::addScript('files', 'file-upload'); |
|
| 382 | - \OCP\Util::addStyle('files_sharing', 'public'); |
|
| 383 | - \OCP\Util::addStyle('files_sharing', 'mobile'); |
|
| 384 | - \OCP\Util::addScript('files_sharing', 'public'); |
|
| 385 | - \OCP\Util::addScript('files', 'fileactions'); |
|
| 386 | - \OCP\Util::addScript('files', 'fileactionsmenu'); |
|
| 387 | - \OCP\Util::addScript('files', 'jquery.fileupload'); |
|
| 388 | - \OCP\Util::addScript('files_sharing', 'files_drop'); |
|
| 389 | - |
|
| 390 | - if (isset($shareTmpl['folder'])) { |
|
| 391 | - // JS required for folders |
|
| 392 | - \OCP\Util::addStyle('files', 'files'); |
|
| 393 | - \OCP\Util::addStyle('files', 'upload'); |
|
| 394 | - \OCP\Util::addScript('files', 'filesummary'); |
|
| 395 | - \OCP\Util::addScript('files', 'breadcrumb'); |
|
| 396 | - \OCP\Util::addScript('files', 'fileinfomodel'); |
|
| 397 | - \OCP\Util::addScript('files', 'newfilemenu'); |
|
| 398 | - \OCP\Util::addScript('files', 'files'); |
|
| 399 | - \OCP\Util::addScript('files', 'filelist'); |
|
| 400 | - \OCP\Util::addScript('files', 'keyboardshortcuts'); |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - // OpenGraph Support: http://ogp.me/ |
|
| 404 | - \OCP\Util::addHeader('meta', ['property' => "og:title", 'content' => $this->defaults->getName() . ' - ' . $this->defaults->getSlogan()]); |
|
| 405 | - \OCP\Util::addHeader('meta', ['property' => "og:description", 'content' => $this->l10n->t('%s is publicly shared', [$shareTmpl['filename']])]); |
|
| 406 | - \OCP\Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]); |
|
| 407 | - \OCP\Util::addHeader('meta', ['property' => "og:url", 'content' => $shareTmpl['shareUrl']]); |
|
| 408 | - \OCP\Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]); |
|
| 409 | - \OCP\Util::addHeader('meta', ['property' => "og:image", 'content' => $shareTmpl['previewImage']]); |
|
| 410 | - |
|
| 411 | - $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts'); |
|
| 412 | - |
|
| 413 | - $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy(); |
|
| 414 | - $csp->addAllowedFrameDomain('\'self\''); |
|
| 415 | - $response = new TemplateResponse($this->appName, 'public', $shareTmpl, 'base'); |
|
| 416 | - $response->setContentSecurityPolicy($csp); |
|
| 417 | - |
|
| 418 | - $this->emitAccessShareHook($share); |
|
| 419 | - |
|
| 420 | - return $response; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * @PublicPage |
|
| 425 | - * @NoCSRFRequired |
|
| 426 | - * |
|
| 427 | - * @param string $token |
|
| 428 | - * @param string $files |
|
| 429 | - * @param string $path |
|
| 430 | - * @param string $downloadStartSecret |
|
| 431 | - * @return void|\OCP\AppFramework\Http\Response |
|
| 432 | - * @throws NotFoundException |
|
| 433 | - */ |
|
| 434 | - public function downloadShare($token, $files = null, $path = '', $downloadStartSecret = '') { |
|
| 435 | - \OC_User::setIncognitoMode(true); |
|
| 436 | - |
|
| 437 | - $share = $this->shareManager->getShareByToken($token); |
|
| 438 | - |
|
| 439 | - if(!($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) { |
|
| 440 | - return new \OCP\AppFramework\Http\DataResponse('Share is read-only'); |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - // Share is password protected - check whether the user is permitted to access the share |
|
| 444 | - if ($share->getPassword() !== null && !$this->linkShareAuth($share)) { |
|
| 445 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', |
|
| 446 | - ['token' => $token])); |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - $files_list = null; |
|
| 450 | - if (!is_null($files)) { // download selected files |
|
| 451 | - $files_list = json_decode($files); |
|
| 452 | - // in case we get only a single file |
|
| 453 | - if ($files_list === null) { |
|
| 454 | - $files_list = [$files]; |
|
| 455 | - } |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 459 | - $originalSharePath = $userFolder->getRelativePath($share->getNode()->getPath()); |
|
| 460 | - |
|
| 461 | - if (!$this->validateShare($share)) { |
|
| 462 | - throw new NotFoundException(); |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - // Single file share |
|
| 466 | - if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 467 | - // Single file download |
|
| 468 | - $this->singleFileDownloaded($share, $share->getNode()); |
|
| 469 | - } |
|
| 470 | - // Directory share |
|
| 471 | - else { |
|
| 472 | - /** @var \OCP\Files\Folder $node */ |
|
| 473 | - $node = $share->getNode(); |
|
| 474 | - |
|
| 475 | - // Try to get the path |
|
| 476 | - if ($path !== '') { |
|
| 477 | - try { |
|
| 478 | - $node = $node->get($path); |
|
| 479 | - } catch (NotFoundException $e) { |
|
| 480 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 481 | - return new NotFoundResponse(); |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - $originalSharePath = $userFolder->getRelativePath($node->getPath()); |
|
| 486 | - |
|
| 487 | - if ($node instanceof \OCP\Files\File) { |
|
| 488 | - // Single file download |
|
| 489 | - $this->singleFileDownloaded($share, $share->getNode()); |
|
| 490 | - } else if (!empty($files_list)) { |
|
| 491 | - $this->fileListDownloaded($share, $files_list, $node); |
|
| 492 | - } else { |
|
| 493 | - // The folder is downloaded |
|
| 494 | - $this->singleFileDownloaded($share, $share->getNode()); |
|
| 495 | - } |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - /* FIXME: We should do this all nicely in OCP */ |
|
| 499 | - OC_Util::tearDownFS(); |
|
| 500 | - OC_Util::setupFS($share->getShareOwner()); |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * this sets a cookie to be able to recognize the start of the download |
|
| 504 | - * the content must not be longer than 32 characters and must only contain |
|
| 505 | - * alphanumeric characters |
|
| 506 | - */ |
|
| 507 | - if (!empty($downloadStartSecret) |
|
| 508 | - && !isset($downloadStartSecret[32]) |
|
| 509 | - && preg_match('!^[a-zA-Z0-9]+$!', $downloadStartSecret) === 1) { |
|
| 510 | - |
|
| 511 | - // FIXME: set on the response once we use an actual app framework response |
|
| 512 | - setcookie('ocDownloadStarted', $downloadStartSecret, time() + 20, '/'); |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - $this->emitAccessShareHook($share); |
|
| 516 | - |
|
| 517 | - $server_params = array( 'head' => $this->request->getMethod() == 'HEAD' ); |
|
| 518 | - |
|
| 519 | - /** |
|
| 520 | - * Http range requests support |
|
| 521 | - */ |
|
| 522 | - if (isset($_SERVER['HTTP_RANGE'])) { |
|
| 523 | - $server_params['range'] = $this->request->getHeader('Range'); |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - // download selected files |
|
| 527 | - if (!is_null($files) && $files !== '') { |
|
| 528 | - // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 529 | - // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 530 | - OC_Files::get($originalSharePath, $files_list, $server_params); |
|
| 531 | - exit(); |
|
| 532 | - } else { |
|
| 533 | - // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 534 | - // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 535 | - OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $server_params); |
|
| 536 | - exit(); |
|
| 537 | - } |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * create activity for every downloaded file |
|
| 542 | - * |
|
| 543 | - * @param Share\IShare $share |
|
| 544 | - * @param array $files_list |
|
| 545 | - * @param \OCP\Files\Folder $node |
|
| 546 | - */ |
|
| 547 | - protected function fileListDownloaded(Share\IShare $share, array $files_list, \OCP\Files\Folder $node) { |
|
| 548 | - foreach ($files_list as $file) { |
|
| 549 | - $subNode = $node->get($file); |
|
| 550 | - $this->singleFileDownloaded($share, $subNode); |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - /** |
|
| 556 | - * create activity if a single file was downloaded from a link share |
|
| 557 | - * |
|
| 558 | - * @param Share\IShare $share |
|
| 559 | - */ |
|
| 560 | - protected function singleFileDownloaded(Share\IShare $share, \OCP\Files\Node $node) { |
|
| 561 | - |
|
| 562 | - $fileId = $node->getId(); |
|
| 563 | - |
|
| 564 | - $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 565 | - $userNodeList = $userFolder->getById($fileId); |
|
| 566 | - $userNode = $userNodeList[0]; |
|
| 567 | - $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 568 | - $userPath = $userFolder->getRelativePath($userNode->getPath()); |
|
| 569 | - $ownerPath = $ownerFolder->getRelativePath($node->getPath()); |
|
| 570 | - |
|
| 571 | - $parameters = [$userPath]; |
|
| 572 | - |
|
| 573 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 574 | - if ($node instanceof \OCP\Files\File) { |
|
| 575 | - $subject = Downloads::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED; |
|
| 576 | - } else { |
|
| 577 | - $subject = Downloads::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED; |
|
| 578 | - } |
|
| 579 | - $parameters[] = $share->getSharedWith(); |
|
| 580 | - } else { |
|
| 581 | - if ($node instanceof \OCP\Files\File) { |
|
| 582 | - $subject = Downloads::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED; |
|
| 583 | - } else { |
|
| 584 | - $subject = Downloads::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED; |
|
| 585 | - } |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - $this->publishActivity($subject, $parameters, $share->getSharedBy(), $fileId, $userPath); |
|
| 589 | - |
|
| 590 | - if ($share->getShareOwner() !== $share->getSharedBy()) { |
|
| 591 | - $parameters[0] = $ownerPath; |
|
| 592 | - $this->publishActivity($subject, $parameters, $share->getShareOwner(), $fileId, $ownerPath); |
|
| 593 | - } |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - /** |
|
| 597 | - * publish activity |
|
| 598 | - * |
|
| 599 | - * @param string $subject |
|
| 600 | - * @param array $parameters |
|
| 601 | - * @param string $affectedUser |
|
| 602 | - * @param int $fileId |
|
| 603 | - * @param string $filePath |
|
| 604 | - */ |
|
| 605 | - protected function publishActivity($subject, |
|
| 606 | - array $parameters, |
|
| 607 | - $affectedUser, |
|
| 608 | - $fileId, |
|
| 609 | - $filePath) { |
|
| 610 | - |
|
| 611 | - $event = $this->activityManager->generateEvent(); |
|
| 612 | - $event->setApp('files_sharing') |
|
| 613 | - ->setType('public_links') |
|
| 614 | - ->setSubject($subject, $parameters) |
|
| 615 | - ->setAffectedUser($affectedUser) |
|
| 616 | - ->setObject('files', $fileId, $filePath); |
|
| 617 | - $this->activityManager->publish($event); |
|
| 618 | - } |
|
| 339 | + $freeSpace = $share->getNode()->getStorage()->free_space($share->getNode()->getInternalPath()); |
|
| 340 | + if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 341 | + $freeSpace = max($freeSpace, 0); |
|
| 342 | + } else { |
|
| 343 | + $freeSpace = (INF > 0) ? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + $hideFileList = $share->getPermissions() & \OCP\Constants::PERMISSION_READ ? false : true; |
|
| 347 | + $maxUploadFilesize = $freeSpace; |
|
| 348 | + |
|
| 349 | + $folder = new Template('files', 'list', ''); |
|
| 350 | + $folder->assign('dir', $rootFolder->getRelativePath($folderNode->getPath())); |
|
| 351 | + $folder->assign('dirToken', $token); |
|
| 352 | + $folder->assign('permissions', \OCP\Constants::PERMISSION_READ); |
|
| 353 | + $folder->assign('isPublic', true); |
|
| 354 | + $folder->assign('hideFileList', $hideFileList); |
|
| 355 | + $folder->assign('publicUploadEnabled', 'no'); |
|
| 356 | + $folder->assign('uploadMaxFilesize', $maxUploadFilesize); |
|
| 357 | + $folder->assign('uploadMaxHumanFilesize', \OCP\Util::humanFileSize($maxUploadFilesize)); |
|
| 358 | + $folder->assign('freeSpace', $freeSpace); |
|
| 359 | + $folder->assign('usedSpacePercent', 0); |
|
| 360 | + $folder->assign('trash', false); |
|
| 361 | + $shareTmpl['folder'] = $folder->fetchPage(); |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + $shareTmpl['hideFileList'] = $hideFileList; |
|
| 365 | + $shareTmpl['shareOwner'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 366 | + $shareTmpl['downloadURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', ['token' => $token]); |
|
| 367 | + $shareTmpl['shareUrl'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]); |
|
| 368 | + $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10); |
|
| 369 | + $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true); |
|
| 370 | + $shareTmpl['previewMaxX'] = $this->config->getSystemValue('preview_max_x', 1024); |
|
| 371 | + $shareTmpl['previewMaxY'] = $this->config->getSystemValue('preview_max_y', 1024); |
|
| 372 | + $shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null); |
|
| 373 | + if ($shareTmpl['previewSupported']) { |
|
| 374 | + $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute( 'files_sharing.PublicPreview.getPreview', |
|
| 375 | + ['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 't' => $shareTmpl['dirToken']]); |
|
| 376 | + } else { |
|
| 377 | + $shareTmpl['previewImage'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png')); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + // Load files we need |
|
| 381 | + \OCP\Util::addScript('files', 'file-upload'); |
|
| 382 | + \OCP\Util::addStyle('files_sharing', 'public'); |
|
| 383 | + \OCP\Util::addStyle('files_sharing', 'mobile'); |
|
| 384 | + \OCP\Util::addScript('files_sharing', 'public'); |
|
| 385 | + \OCP\Util::addScript('files', 'fileactions'); |
|
| 386 | + \OCP\Util::addScript('files', 'fileactionsmenu'); |
|
| 387 | + \OCP\Util::addScript('files', 'jquery.fileupload'); |
|
| 388 | + \OCP\Util::addScript('files_sharing', 'files_drop'); |
|
| 389 | + |
|
| 390 | + if (isset($shareTmpl['folder'])) { |
|
| 391 | + // JS required for folders |
|
| 392 | + \OCP\Util::addStyle('files', 'files'); |
|
| 393 | + \OCP\Util::addStyle('files', 'upload'); |
|
| 394 | + \OCP\Util::addScript('files', 'filesummary'); |
|
| 395 | + \OCP\Util::addScript('files', 'breadcrumb'); |
|
| 396 | + \OCP\Util::addScript('files', 'fileinfomodel'); |
|
| 397 | + \OCP\Util::addScript('files', 'newfilemenu'); |
|
| 398 | + \OCP\Util::addScript('files', 'files'); |
|
| 399 | + \OCP\Util::addScript('files', 'filelist'); |
|
| 400 | + \OCP\Util::addScript('files', 'keyboardshortcuts'); |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + // OpenGraph Support: http://ogp.me/ |
|
| 404 | + \OCP\Util::addHeader('meta', ['property' => "og:title", 'content' => $this->defaults->getName() . ' - ' . $this->defaults->getSlogan()]); |
|
| 405 | + \OCP\Util::addHeader('meta', ['property' => "og:description", 'content' => $this->l10n->t('%s is publicly shared', [$shareTmpl['filename']])]); |
|
| 406 | + \OCP\Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]); |
|
| 407 | + \OCP\Util::addHeader('meta', ['property' => "og:url", 'content' => $shareTmpl['shareUrl']]); |
|
| 408 | + \OCP\Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]); |
|
| 409 | + \OCP\Util::addHeader('meta', ['property' => "og:image", 'content' => $shareTmpl['previewImage']]); |
|
| 410 | + |
|
| 411 | + $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts'); |
|
| 412 | + |
|
| 413 | + $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy(); |
|
| 414 | + $csp->addAllowedFrameDomain('\'self\''); |
|
| 415 | + $response = new TemplateResponse($this->appName, 'public', $shareTmpl, 'base'); |
|
| 416 | + $response->setContentSecurityPolicy($csp); |
|
| 417 | + |
|
| 418 | + $this->emitAccessShareHook($share); |
|
| 419 | + |
|
| 420 | + return $response; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * @PublicPage |
|
| 425 | + * @NoCSRFRequired |
|
| 426 | + * |
|
| 427 | + * @param string $token |
|
| 428 | + * @param string $files |
|
| 429 | + * @param string $path |
|
| 430 | + * @param string $downloadStartSecret |
|
| 431 | + * @return void|\OCP\AppFramework\Http\Response |
|
| 432 | + * @throws NotFoundException |
|
| 433 | + */ |
|
| 434 | + public function downloadShare($token, $files = null, $path = '', $downloadStartSecret = '') { |
|
| 435 | + \OC_User::setIncognitoMode(true); |
|
| 436 | + |
|
| 437 | + $share = $this->shareManager->getShareByToken($token); |
|
| 438 | + |
|
| 439 | + if(!($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) { |
|
| 440 | + return new \OCP\AppFramework\Http\DataResponse('Share is read-only'); |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + // Share is password protected - check whether the user is permitted to access the share |
|
| 444 | + if ($share->getPassword() !== null && !$this->linkShareAuth($share)) { |
|
| 445 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files_sharing.sharecontroller.authenticate', |
|
| 446 | + ['token' => $token])); |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + $files_list = null; |
|
| 450 | + if (!is_null($files)) { // download selected files |
|
| 451 | + $files_list = json_decode($files); |
|
| 452 | + // in case we get only a single file |
|
| 453 | + if ($files_list === null) { |
|
| 454 | + $files_list = [$files]; |
|
| 455 | + } |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 459 | + $originalSharePath = $userFolder->getRelativePath($share->getNode()->getPath()); |
|
| 460 | + |
|
| 461 | + if (!$this->validateShare($share)) { |
|
| 462 | + throw new NotFoundException(); |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + // Single file share |
|
| 466 | + if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 467 | + // Single file download |
|
| 468 | + $this->singleFileDownloaded($share, $share->getNode()); |
|
| 469 | + } |
|
| 470 | + // Directory share |
|
| 471 | + else { |
|
| 472 | + /** @var \OCP\Files\Folder $node */ |
|
| 473 | + $node = $share->getNode(); |
|
| 474 | + |
|
| 475 | + // Try to get the path |
|
| 476 | + if ($path !== '') { |
|
| 477 | + try { |
|
| 478 | + $node = $node->get($path); |
|
| 479 | + } catch (NotFoundException $e) { |
|
| 480 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 481 | + return new NotFoundResponse(); |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + $originalSharePath = $userFolder->getRelativePath($node->getPath()); |
|
| 486 | + |
|
| 487 | + if ($node instanceof \OCP\Files\File) { |
|
| 488 | + // Single file download |
|
| 489 | + $this->singleFileDownloaded($share, $share->getNode()); |
|
| 490 | + } else if (!empty($files_list)) { |
|
| 491 | + $this->fileListDownloaded($share, $files_list, $node); |
|
| 492 | + } else { |
|
| 493 | + // The folder is downloaded |
|
| 494 | + $this->singleFileDownloaded($share, $share->getNode()); |
|
| 495 | + } |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + /* FIXME: We should do this all nicely in OCP */ |
|
| 499 | + OC_Util::tearDownFS(); |
|
| 500 | + OC_Util::setupFS($share->getShareOwner()); |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * this sets a cookie to be able to recognize the start of the download |
|
| 504 | + * the content must not be longer than 32 characters and must only contain |
|
| 505 | + * alphanumeric characters |
|
| 506 | + */ |
|
| 507 | + if (!empty($downloadStartSecret) |
|
| 508 | + && !isset($downloadStartSecret[32]) |
|
| 509 | + && preg_match('!^[a-zA-Z0-9]+$!', $downloadStartSecret) === 1) { |
|
| 510 | + |
|
| 511 | + // FIXME: set on the response once we use an actual app framework response |
|
| 512 | + setcookie('ocDownloadStarted', $downloadStartSecret, time() + 20, '/'); |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + $this->emitAccessShareHook($share); |
|
| 516 | + |
|
| 517 | + $server_params = array( 'head' => $this->request->getMethod() == 'HEAD' ); |
|
| 518 | + |
|
| 519 | + /** |
|
| 520 | + * Http range requests support |
|
| 521 | + */ |
|
| 522 | + if (isset($_SERVER['HTTP_RANGE'])) { |
|
| 523 | + $server_params['range'] = $this->request->getHeader('Range'); |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + // download selected files |
|
| 527 | + if (!is_null($files) && $files !== '') { |
|
| 528 | + // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 529 | + // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 530 | + OC_Files::get($originalSharePath, $files_list, $server_params); |
|
| 531 | + exit(); |
|
| 532 | + } else { |
|
| 533 | + // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 534 | + // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 535 | + OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $server_params); |
|
| 536 | + exit(); |
|
| 537 | + } |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * create activity for every downloaded file |
|
| 542 | + * |
|
| 543 | + * @param Share\IShare $share |
|
| 544 | + * @param array $files_list |
|
| 545 | + * @param \OCP\Files\Folder $node |
|
| 546 | + */ |
|
| 547 | + protected function fileListDownloaded(Share\IShare $share, array $files_list, \OCP\Files\Folder $node) { |
|
| 548 | + foreach ($files_list as $file) { |
|
| 549 | + $subNode = $node->get($file); |
|
| 550 | + $this->singleFileDownloaded($share, $subNode); |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + /** |
|
| 556 | + * create activity if a single file was downloaded from a link share |
|
| 557 | + * |
|
| 558 | + * @param Share\IShare $share |
|
| 559 | + */ |
|
| 560 | + protected function singleFileDownloaded(Share\IShare $share, \OCP\Files\Node $node) { |
|
| 561 | + |
|
| 562 | + $fileId = $node->getId(); |
|
| 563 | + |
|
| 564 | + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 565 | + $userNodeList = $userFolder->getById($fileId); |
|
| 566 | + $userNode = $userNodeList[0]; |
|
| 567 | + $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 568 | + $userPath = $userFolder->getRelativePath($userNode->getPath()); |
|
| 569 | + $ownerPath = $ownerFolder->getRelativePath($node->getPath()); |
|
| 570 | + |
|
| 571 | + $parameters = [$userPath]; |
|
| 572 | + |
|
| 573 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 574 | + if ($node instanceof \OCP\Files\File) { |
|
| 575 | + $subject = Downloads::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED; |
|
| 576 | + } else { |
|
| 577 | + $subject = Downloads::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED; |
|
| 578 | + } |
|
| 579 | + $parameters[] = $share->getSharedWith(); |
|
| 580 | + } else { |
|
| 581 | + if ($node instanceof \OCP\Files\File) { |
|
| 582 | + $subject = Downloads::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED; |
|
| 583 | + } else { |
|
| 584 | + $subject = Downloads::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED; |
|
| 585 | + } |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + $this->publishActivity($subject, $parameters, $share->getSharedBy(), $fileId, $userPath); |
|
| 589 | + |
|
| 590 | + if ($share->getShareOwner() !== $share->getSharedBy()) { |
|
| 591 | + $parameters[0] = $ownerPath; |
|
| 592 | + $this->publishActivity($subject, $parameters, $share->getShareOwner(), $fileId, $ownerPath); |
|
| 593 | + } |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + /** |
|
| 597 | + * publish activity |
|
| 598 | + * |
|
| 599 | + * @param string $subject |
|
| 600 | + * @param array $parameters |
|
| 601 | + * @param string $affectedUser |
|
| 602 | + * @param int $fileId |
|
| 603 | + * @param string $filePath |
|
| 604 | + */ |
|
| 605 | + protected function publishActivity($subject, |
|
| 606 | + array $parameters, |
|
| 607 | + $affectedUser, |
|
| 608 | + $fileId, |
|
| 609 | + $filePath) { |
|
| 610 | + |
|
| 611 | + $event = $this->activityManager->generateEvent(); |
|
| 612 | + $event->setApp('files_sharing') |
|
| 613 | + ->setType('public_links') |
|
| 614 | + ->setSubject($subject, $parameters) |
|
| 615 | + ->setAffectedUser($affectedUser) |
|
| 616 | + ->setObject('files', $fileId, $filePath); |
|
| 617 | + $this->activityManager->publish($event); |
|
| 618 | + } |
|
| 619 | 619 | |
| 620 | 620 | |
| 621 | 621 | } |
@@ -27,7 +27,6 @@ |
||
| 27 | 27 | * Expected path with a different root |
| 28 | 28 | * Possible Error Codes: |
| 29 | 29 | * 10 - Path not relative to data/ and point to the users file directory |
| 30 | - |
|
| 31 | 30 | */ |
| 32 | 31 | class BrokenPath extends \Exception { |
| 33 | 32 | } |
@@ -40,131 +40,131 @@ |
||
| 40 | 40 | use OCP\IServerContainer; |
| 41 | 41 | |
| 42 | 42 | class Application extends App { |
| 43 | - public function __construct(array $urlParams = array()) { |
|
| 44 | - parent::__construct('files_sharing', $urlParams); |
|
| 43 | + public function __construct(array $urlParams = array()) { |
|
| 44 | + parent::__construct('files_sharing', $urlParams); |
|
| 45 | 45 | |
| 46 | - $container = $this->getContainer(); |
|
| 47 | - /** @var IServerContainer $server */ |
|
| 48 | - $server = $container->getServer(); |
|
| 46 | + $container = $this->getContainer(); |
|
| 47 | + /** @var IServerContainer $server */ |
|
| 48 | + $server = $container->getServer(); |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Controllers |
|
| 52 | - */ |
|
| 53 | - $container->registerService('ShareController', function (SimpleContainer $c) use ($server) { |
|
| 54 | - $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
| 55 | - return new ShareController( |
|
| 56 | - $c->query('AppName'), |
|
| 57 | - $c->query('Request'), |
|
| 58 | - $server->getConfig(), |
|
| 59 | - $server->getURLGenerator(), |
|
| 60 | - $server->getUserManager(), |
|
| 61 | - $server->getLogger(), |
|
| 62 | - $server->getActivityManager(), |
|
| 63 | - $server->getShareManager(), |
|
| 64 | - $server->getSession(), |
|
| 65 | - $server->getPreviewManager(), |
|
| 66 | - $server->getRootFolder(), |
|
| 67 | - $federatedSharingApp->getFederatedShareProvider(), |
|
| 68 | - $server->getEventDispatcher(), |
|
| 69 | - $server->getL10N($c->query('AppName')), |
|
| 70 | - $server->getThemingDefaults() |
|
| 71 | - ); |
|
| 72 | - }); |
|
| 73 | - $container->registerService('ExternalSharesController', function (SimpleContainer $c) { |
|
| 74 | - return new ExternalSharesController( |
|
| 75 | - $c->query('AppName'), |
|
| 76 | - $c->query('Request'), |
|
| 77 | - $c->query('ExternalManager'), |
|
| 78 | - $c->query('HttpClientService') |
|
| 79 | - ); |
|
| 80 | - }); |
|
| 50 | + /** |
|
| 51 | + * Controllers |
|
| 52 | + */ |
|
| 53 | + $container->registerService('ShareController', function (SimpleContainer $c) use ($server) { |
|
| 54 | + $federatedSharingApp = new \OCA\FederatedFileSharing\AppInfo\Application(); |
|
| 55 | + return new ShareController( |
|
| 56 | + $c->query('AppName'), |
|
| 57 | + $c->query('Request'), |
|
| 58 | + $server->getConfig(), |
|
| 59 | + $server->getURLGenerator(), |
|
| 60 | + $server->getUserManager(), |
|
| 61 | + $server->getLogger(), |
|
| 62 | + $server->getActivityManager(), |
|
| 63 | + $server->getShareManager(), |
|
| 64 | + $server->getSession(), |
|
| 65 | + $server->getPreviewManager(), |
|
| 66 | + $server->getRootFolder(), |
|
| 67 | + $federatedSharingApp->getFederatedShareProvider(), |
|
| 68 | + $server->getEventDispatcher(), |
|
| 69 | + $server->getL10N($c->query('AppName')), |
|
| 70 | + $server->getThemingDefaults() |
|
| 71 | + ); |
|
| 72 | + }); |
|
| 73 | + $container->registerService('ExternalSharesController', function (SimpleContainer $c) { |
|
| 74 | + return new ExternalSharesController( |
|
| 75 | + $c->query('AppName'), |
|
| 76 | + $c->query('Request'), |
|
| 77 | + $c->query('ExternalManager'), |
|
| 78 | + $c->query('HttpClientService') |
|
| 79 | + ); |
|
| 80 | + }); |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * Core class wrappers |
|
| 84 | - */ |
|
| 85 | - $container->registerService('HttpClientService', function (SimpleContainer $c) use ($server) { |
|
| 86 | - return $server->getHTTPClientService(); |
|
| 87 | - }); |
|
| 88 | - $container->registerService(ICloudIdManager::class, function (SimpleContainer $c) use ($server) { |
|
| 89 | - return $server->getCloudIdManager(); |
|
| 90 | - }); |
|
| 91 | - $container->registerService('ExternalManager', function (SimpleContainer $c) use ($server) { |
|
| 92 | - $user = $server->getUserSession()->getUser(); |
|
| 93 | - $uid = $user ? $user->getUID() : null; |
|
| 94 | - $discoveryManager = new DiscoveryManager( |
|
| 95 | - \OC::$server->getMemCacheFactory(), |
|
| 96 | - \OC::$server->getHTTPClientService() |
|
| 97 | - ); |
|
| 98 | - return new \OCA\Files_Sharing\External\Manager( |
|
| 99 | - $server->getDatabaseConnection(), |
|
| 100 | - \OC\Files\Filesystem::getMountManager(), |
|
| 101 | - \OC\Files\Filesystem::getLoader(), |
|
| 102 | - $server->getHTTPClientService(), |
|
| 103 | - $server->getNotificationManager(), |
|
| 104 | - $discoveryManager, |
|
| 105 | - $uid |
|
| 106 | - ); |
|
| 107 | - }); |
|
| 108 | - $container->registerAlias('OCA\Files_Sharing\External\Manager', 'ExternalManager'); |
|
| 82 | + /** |
|
| 83 | + * Core class wrappers |
|
| 84 | + */ |
|
| 85 | + $container->registerService('HttpClientService', function (SimpleContainer $c) use ($server) { |
|
| 86 | + return $server->getHTTPClientService(); |
|
| 87 | + }); |
|
| 88 | + $container->registerService(ICloudIdManager::class, function (SimpleContainer $c) use ($server) { |
|
| 89 | + return $server->getCloudIdManager(); |
|
| 90 | + }); |
|
| 91 | + $container->registerService('ExternalManager', function (SimpleContainer $c) use ($server) { |
|
| 92 | + $user = $server->getUserSession()->getUser(); |
|
| 93 | + $uid = $user ? $user->getUID() : null; |
|
| 94 | + $discoveryManager = new DiscoveryManager( |
|
| 95 | + \OC::$server->getMemCacheFactory(), |
|
| 96 | + \OC::$server->getHTTPClientService() |
|
| 97 | + ); |
|
| 98 | + return new \OCA\Files_Sharing\External\Manager( |
|
| 99 | + $server->getDatabaseConnection(), |
|
| 100 | + \OC\Files\Filesystem::getMountManager(), |
|
| 101 | + \OC\Files\Filesystem::getLoader(), |
|
| 102 | + $server->getHTTPClientService(), |
|
| 103 | + $server->getNotificationManager(), |
|
| 104 | + $discoveryManager, |
|
| 105 | + $uid |
|
| 106 | + ); |
|
| 107 | + }); |
|
| 108 | + $container->registerAlias('OCA\Files_Sharing\External\Manager', 'ExternalManager'); |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Middleware |
|
| 112 | - */ |
|
| 113 | - $container->registerService('SharingCheckMiddleware', function (SimpleContainer $c) use ($server) { |
|
| 114 | - return new SharingCheckMiddleware( |
|
| 115 | - $c->query('AppName'), |
|
| 116 | - $server->getConfig(), |
|
| 117 | - $server->getAppManager(), |
|
| 118 | - $c['ControllerMethodReflector'], |
|
| 119 | - $server->getShareManager(), |
|
| 120 | - $server->getRequest() |
|
| 121 | - ); |
|
| 122 | - }); |
|
| 110 | + /** |
|
| 111 | + * Middleware |
|
| 112 | + */ |
|
| 113 | + $container->registerService('SharingCheckMiddleware', function (SimpleContainer $c) use ($server) { |
|
| 114 | + return new SharingCheckMiddleware( |
|
| 115 | + $c->query('AppName'), |
|
| 116 | + $server->getConfig(), |
|
| 117 | + $server->getAppManager(), |
|
| 118 | + $c['ControllerMethodReflector'], |
|
| 119 | + $server->getShareManager(), |
|
| 120 | + $server->getRequest() |
|
| 121 | + ); |
|
| 122 | + }); |
|
| 123 | 123 | |
| 124 | - $container->registerService('OCSShareAPIMiddleware', function (SimpleContainer $c) use ($server) { |
|
| 125 | - return new OCSShareAPIMiddleware( |
|
| 126 | - $server->getShareManager(), |
|
| 127 | - $server->getL10N($c->query('AppName')) |
|
| 128 | - ); |
|
| 129 | - }); |
|
| 124 | + $container->registerService('OCSShareAPIMiddleware', function (SimpleContainer $c) use ($server) { |
|
| 125 | + return new OCSShareAPIMiddleware( |
|
| 126 | + $server->getShareManager(), |
|
| 127 | + $server->getL10N($c->query('AppName')) |
|
| 128 | + ); |
|
| 129 | + }); |
|
| 130 | 130 | |
| 131 | - // Execute middlewares |
|
| 132 | - $container->registerMiddleWare('SharingCheckMiddleware'); |
|
| 133 | - $container->registerMiddleWare('OCSShareAPIMiddleware'); |
|
| 131 | + // Execute middlewares |
|
| 132 | + $container->registerMiddleWare('SharingCheckMiddleware'); |
|
| 133 | + $container->registerMiddleWare('OCSShareAPIMiddleware'); |
|
| 134 | 134 | |
| 135 | - $container->registerService('MountProvider', function (IContainer $c) { |
|
| 136 | - /** @var \OCP\IServerContainer $server */ |
|
| 137 | - $server = $c->query('ServerContainer'); |
|
| 138 | - return new MountProvider( |
|
| 139 | - $server->getConfig(), |
|
| 140 | - $server->getShareManager(), |
|
| 141 | - $server->getLogger() |
|
| 142 | - ); |
|
| 143 | - }); |
|
| 135 | + $container->registerService('MountProvider', function (IContainer $c) { |
|
| 136 | + /** @var \OCP\IServerContainer $server */ |
|
| 137 | + $server = $c->query('ServerContainer'); |
|
| 138 | + return new MountProvider( |
|
| 139 | + $server->getConfig(), |
|
| 140 | + $server->getShareManager(), |
|
| 141 | + $server->getLogger() |
|
| 142 | + ); |
|
| 143 | + }); |
|
| 144 | 144 | |
| 145 | - $container->registerService('ExternalMountProvider', function (IContainer $c) { |
|
| 146 | - /** @var \OCP\IServerContainer $server */ |
|
| 147 | - $server = $c->query('ServerContainer'); |
|
| 148 | - return new \OCA\Files_Sharing\External\MountProvider( |
|
| 149 | - $server->getDatabaseConnection(), |
|
| 150 | - function() use ($c) { |
|
| 151 | - return $c->query('ExternalManager'); |
|
| 152 | - }, |
|
| 153 | - $server->getCloudIdManager() |
|
| 154 | - ); |
|
| 155 | - }); |
|
| 145 | + $container->registerService('ExternalMountProvider', function (IContainer $c) { |
|
| 146 | + /** @var \OCP\IServerContainer $server */ |
|
| 147 | + $server = $c->query('ServerContainer'); |
|
| 148 | + return new \OCA\Files_Sharing\External\MountProvider( |
|
| 149 | + $server->getDatabaseConnection(), |
|
| 150 | + function() use ($c) { |
|
| 151 | + return $c->query('ExternalManager'); |
|
| 152 | + }, |
|
| 153 | + $server->getCloudIdManager() |
|
| 154 | + ); |
|
| 155 | + }); |
|
| 156 | 156 | |
| 157 | - /* |
|
| 157 | + /* |
|
| 158 | 158 | * Register capabilities |
| 159 | 159 | */ |
| 160 | - $container->registerCapability('OCA\Files_Sharing\Capabilities'); |
|
| 161 | - } |
|
| 160 | + $container->registerCapability('OCA\Files_Sharing\Capabilities'); |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - public function registerMountProviders() { |
|
| 164 | - /** @var \OCP\IServerContainer $server */ |
|
| 165 | - $server = $this->getContainer()->query('ServerContainer'); |
|
| 166 | - $mountProviderCollection = $server->getMountProviderCollection(); |
|
| 167 | - $mountProviderCollection->registerProvider($this->getContainer()->query('MountProvider')); |
|
| 168 | - $mountProviderCollection->registerProvider($this->getContainer()->query('ExternalMountProvider')); |
|
| 169 | - } |
|
| 163 | + public function registerMountProviders() { |
|
| 164 | + /** @var \OCP\IServerContainer $server */ |
|
| 165 | + $server = $this->getContainer()->query('ServerContainer'); |
|
| 166 | + $mountProviderCollection = $server->getMountProviderCollection(); |
|
| 167 | + $mountProviderCollection->registerProvider($this->getContainer()->query('MountProvider')); |
|
| 168 | + $mountProviderCollection->registerProvider($this->getContainer()->query('ExternalMountProvider')); |
|
| 169 | + } |
|
| 170 | 170 | } |
@@ -32,97 +32,97 @@ |
||
| 32 | 32 | use OCP\Files\StorageNotAvailableException; |
| 33 | 33 | |
| 34 | 34 | class Scanner extends \OC\Files\Cache\Scanner { |
| 35 | - /** @var \OCA\Files_Sharing\External\Storage */ |
|
| 36 | - protected $storage; |
|
| 35 | + /** @var \OCA\Files_Sharing\External\Storage */ |
|
| 36 | + protected $storage; |
|
| 37 | 37 | |
| 38 | - /** {@inheritDoc} */ |
|
| 39 | - public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) { |
|
| 40 | - if(!$this->storage->remoteIsOwnCloud()) { |
|
| 41 | - return parent::scan($path, $recursive, $recursive, $lock); |
|
| 42 | - } |
|
| 38 | + /** {@inheritDoc} */ |
|
| 39 | + public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1, $lock = true) { |
|
| 40 | + if(!$this->storage->remoteIsOwnCloud()) { |
|
| 41 | + return parent::scan($path, $recursive, $recursive, $lock); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - $this->scanAll(); |
|
| 45 | - } |
|
| 44 | + $this->scanAll(); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Scan a single file and store it in the cache. |
|
| 49 | - * If an exception happened while accessing the external storage, |
|
| 50 | - * the storage will be checked for availability and removed |
|
| 51 | - * if it is not available any more. |
|
| 52 | - * |
|
| 53 | - * @param string $file file to scan |
|
| 54 | - * @param int $reuseExisting |
|
| 55 | - * @param int $parentId |
|
| 56 | - * @param array | null $cacheData existing data in the cache for the file to be scanned |
|
| 57 | - * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
| 58 | - * @return array an array of metadata of the scanned file |
|
| 59 | - */ |
|
| 60 | - public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true) { |
|
| 61 | - try { |
|
| 62 | - return parent::scanFile($file, $reuseExisting); |
|
| 63 | - } catch (ForbiddenException $e) { |
|
| 64 | - $this->storage->checkStorageAvailability(); |
|
| 65 | - } catch (NotFoundException $e) { |
|
| 66 | - // if the storage isn't found, the call to |
|
| 67 | - // checkStorageAvailable() will verify it and remove it |
|
| 68 | - // if appropriate |
|
| 69 | - $this->storage->checkStorageAvailability(); |
|
| 70 | - } catch (StorageInvalidException $e) { |
|
| 71 | - $this->storage->checkStorageAvailability(); |
|
| 72 | - } catch (StorageNotAvailableException $e) { |
|
| 73 | - $this->storage->checkStorageAvailability(); |
|
| 74 | - } |
|
| 75 | - } |
|
| 47 | + /** |
|
| 48 | + * Scan a single file and store it in the cache. |
|
| 49 | + * If an exception happened while accessing the external storage, |
|
| 50 | + * the storage will be checked for availability and removed |
|
| 51 | + * if it is not available any more. |
|
| 52 | + * |
|
| 53 | + * @param string $file file to scan |
|
| 54 | + * @param int $reuseExisting |
|
| 55 | + * @param int $parentId |
|
| 56 | + * @param array | null $cacheData existing data in the cache for the file to be scanned |
|
| 57 | + * @param bool $lock set to false to disable getting an additional read lock during scanning |
|
| 58 | + * @return array an array of metadata of the scanned file |
|
| 59 | + */ |
|
| 60 | + public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData = null, $lock = true) { |
|
| 61 | + try { |
|
| 62 | + return parent::scanFile($file, $reuseExisting); |
|
| 63 | + } catch (ForbiddenException $e) { |
|
| 64 | + $this->storage->checkStorageAvailability(); |
|
| 65 | + } catch (NotFoundException $e) { |
|
| 66 | + // if the storage isn't found, the call to |
|
| 67 | + // checkStorageAvailable() will verify it and remove it |
|
| 68 | + // if appropriate |
|
| 69 | + $this->storage->checkStorageAvailability(); |
|
| 70 | + } catch (StorageInvalidException $e) { |
|
| 71 | + $this->storage->checkStorageAvailability(); |
|
| 72 | + } catch (StorageNotAvailableException $e) { |
|
| 73 | + $this->storage->checkStorageAvailability(); |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * Checks the remote share for changes. |
|
| 79 | - * If changes are available, scan them and update |
|
| 80 | - * the cache. |
|
| 81 | - * @throws NotFoundException |
|
| 82 | - * @throws StorageInvalidException |
|
| 83 | - * @throws \Exception |
|
| 84 | - */ |
|
| 85 | - public function scanAll() { |
|
| 86 | - try { |
|
| 87 | - $data = $this->storage->getShareInfo(); |
|
| 88 | - } catch (\Exception $e) { |
|
| 89 | - $this->storage->checkStorageAvailability(); |
|
| 90 | - throw new \Exception( |
|
| 91 | - 'Error while scanning remote share: "' . |
|
| 92 | - $this->storage->getRemote() . '" ' . |
|
| 93 | - $e->getMessage() |
|
| 94 | - ); |
|
| 95 | - } |
|
| 96 | - if ($data['status'] === 'success') { |
|
| 97 | - $this->addResult($data['data'], ''); |
|
| 98 | - } else { |
|
| 99 | - throw new \Exception( |
|
| 100 | - 'Error while scanning remote share: "' . |
|
| 101 | - $this->storage->getRemote() . '"' |
|
| 102 | - ); |
|
| 103 | - } |
|
| 104 | - } |
|
| 77 | + /** |
|
| 78 | + * Checks the remote share for changes. |
|
| 79 | + * If changes are available, scan them and update |
|
| 80 | + * the cache. |
|
| 81 | + * @throws NotFoundException |
|
| 82 | + * @throws StorageInvalidException |
|
| 83 | + * @throws \Exception |
|
| 84 | + */ |
|
| 85 | + public function scanAll() { |
|
| 86 | + try { |
|
| 87 | + $data = $this->storage->getShareInfo(); |
|
| 88 | + } catch (\Exception $e) { |
|
| 89 | + $this->storage->checkStorageAvailability(); |
|
| 90 | + throw new \Exception( |
|
| 91 | + 'Error while scanning remote share: "' . |
|
| 92 | + $this->storage->getRemote() . '" ' . |
|
| 93 | + $e->getMessage() |
|
| 94 | + ); |
|
| 95 | + } |
|
| 96 | + if ($data['status'] === 'success') { |
|
| 97 | + $this->addResult($data['data'], ''); |
|
| 98 | + } else { |
|
| 99 | + throw new \Exception( |
|
| 100 | + 'Error while scanning remote share: "' . |
|
| 101 | + $this->storage->getRemote() . '"' |
|
| 102 | + ); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * @param array $data |
|
| 108 | - * @param string $path |
|
| 109 | - */ |
|
| 110 | - private function addResult($data, $path) { |
|
| 111 | - $id = $this->cache->put($path, $data); |
|
| 112 | - if (isset($data['children'])) { |
|
| 113 | - $children = []; |
|
| 114 | - foreach ($data['children'] as $child) { |
|
| 115 | - $children[$child['name']] = true; |
|
| 116 | - $this->addResult($child, ltrim($path . '/' . $child['name'], '/')); |
|
| 117 | - } |
|
| 106 | + /** |
|
| 107 | + * @param array $data |
|
| 108 | + * @param string $path |
|
| 109 | + */ |
|
| 110 | + private function addResult($data, $path) { |
|
| 111 | + $id = $this->cache->put($path, $data); |
|
| 112 | + if (isset($data['children'])) { |
|
| 113 | + $children = []; |
|
| 114 | + foreach ($data['children'] as $child) { |
|
| 115 | + $children[$child['name']] = true; |
|
| 116 | + $this->addResult($child, ltrim($path . '/' . $child['name'], '/')); |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - $existingCache = $this->cache->getFolderContentsById($id); |
|
| 120 | - foreach ($existingCache as $existingChild) { |
|
| 121 | - // if an existing child is not in the new data, remove it |
|
| 122 | - if (!isset($children[$existingChild['name']])) { |
|
| 123 | - $this->cache->remove(ltrim($path . '/' . $existingChild['name'], '/')); |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - } |
|
| 119 | + $existingCache = $this->cache->getFolderContentsById($id); |
|
| 120 | + foreach ($existingCache as $existingChild) { |
|
| 121 | + // if an existing child is not in the new data, remove it |
|
| 122 | + if (!isset($children[$existingChild['name']])) { |
|
| 123 | + $this->cache->remove(ltrim($path . '/' . $existingChild['name'], '/')); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | 128 | } |
@@ -27,44 +27,44 @@ |
||
| 27 | 27 | use OCP\Federation\ICloudId; |
| 28 | 28 | |
| 29 | 29 | class Cache extends \OC\Files\Cache\Cache { |
| 30 | - /** @var ICloudId */ |
|
| 31 | - private $cloudId; |
|
| 32 | - private $remote; |
|
| 33 | - private $remoteUser; |
|
| 34 | - private $storage; |
|
| 30 | + /** @var ICloudId */ |
|
| 31 | + private $cloudId; |
|
| 32 | + private $remote; |
|
| 33 | + private $remoteUser; |
|
| 34 | + private $storage; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @param \OCA\Files_Sharing\External\Storage $storage |
|
| 38 | - * @param ICloudId $cloudId |
|
| 39 | - */ |
|
| 40 | - public function __construct($storage, ICloudId $cloudId) { |
|
| 41 | - $this->cloudId = $cloudId; |
|
| 42 | - $this->storage = $storage; |
|
| 43 | - list(, $remote) = explode('://', $cloudId->getRemote(), 2); |
|
| 44 | - $this->remote = $remote; |
|
| 45 | - $this->remoteUser = $cloudId->getUser(); |
|
| 46 | - parent::__construct($storage); |
|
| 47 | - } |
|
| 36 | + /** |
|
| 37 | + * @param \OCA\Files_Sharing\External\Storage $storage |
|
| 38 | + * @param ICloudId $cloudId |
|
| 39 | + */ |
|
| 40 | + public function __construct($storage, ICloudId $cloudId) { |
|
| 41 | + $this->cloudId = $cloudId; |
|
| 42 | + $this->storage = $storage; |
|
| 43 | + list(, $remote) = explode('://', $cloudId->getRemote(), 2); |
|
| 44 | + $this->remote = $remote; |
|
| 45 | + $this->remoteUser = $cloudId->getUser(); |
|
| 46 | + parent::__construct($storage); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - public function get($file) { |
|
| 50 | - $result = parent::get($file); |
|
| 51 | - if (!$result) { |
|
| 52 | - return false; |
|
| 53 | - } |
|
| 54 | - $result['displayname_owner'] = $this->cloudId->getDisplayId(); |
|
| 55 | - if (!$file || $file === '') { |
|
| 56 | - $result['is_share_mount_point'] = true; |
|
| 57 | - $mountPoint = rtrim($this->storage->getMountPoint()); |
|
| 58 | - $result['name'] = basename($mountPoint); |
|
| 59 | - } |
|
| 60 | - return $result; |
|
| 61 | - } |
|
| 49 | + public function get($file) { |
|
| 50 | + $result = parent::get($file); |
|
| 51 | + if (!$result) { |
|
| 52 | + return false; |
|
| 53 | + } |
|
| 54 | + $result['displayname_owner'] = $this->cloudId->getDisplayId(); |
|
| 55 | + if (!$file || $file === '') { |
|
| 56 | + $result['is_share_mount_point'] = true; |
|
| 57 | + $mountPoint = rtrim($this->storage->getMountPoint()); |
|
| 58 | + $result['name'] = basename($mountPoint); |
|
| 59 | + } |
|
| 60 | + return $result; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - public function getFolderContentsById($id) { |
|
| 64 | - $results = parent::getFolderContentsById($id); |
|
| 65 | - foreach ($results as &$file) { |
|
| 66 | - $file['displayname_owner'] = $this->cloudId->getDisplayId(); |
|
| 67 | - } |
|
| 68 | - return $results; |
|
| 69 | - } |
|
| 63 | + public function getFolderContentsById($id) { |
|
| 64 | + $results = parent::getFolderContentsById($id); |
|
| 65 | + foreach ($results as &$file) { |
|
| 66 | + $file['displayname_owner'] = $this->cloudId->getDisplayId(); |
|
| 67 | + } |
|
| 68 | + return $results; |
|
| 69 | + } |
|
| 70 | 70 | } |
@@ -41,330 +41,330 @@ |
||
| 41 | 41 | use OCP\Files\StorageNotAvailableException; |
| 42 | 42 | |
| 43 | 43 | class Storage extends DAV implements ISharedStorage { |
| 44 | - /** @var ICloudId */ |
|
| 45 | - private $cloudId; |
|
| 46 | - /** @var string */ |
|
| 47 | - private $mountPoint; |
|
| 48 | - /** @var string */ |
|
| 49 | - private $token; |
|
| 50 | - /** @var \OCP\ICacheFactory */ |
|
| 51 | - private $memcacheFactory; |
|
| 52 | - /** @var \OCP\Http\Client\IClientService */ |
|
| 53 | - private $httpClient; |
|
| 54 | - /** @var \OCP\ICertificateManager */ |
|
| 55 | - private $certificateManager; |
|
| 56 | - /** @var bool */ |
|
| 57 | - private $updateChecked = false; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @var \OCA\Files_Sharing\External\Manager |
|
| 61 | - */ |
|
| 62 | - private $manager; |
|
| 63 | - |
|
| 64 | - public function __construct($options) { |
|
| 65 | - $this->memcacheFactory = \OC::$server->getMemCacheFactory(); |
|
| 66 | - $this->httpClient = $options['HttpClientService']; |
|
| 67 | - $discoveryManager = new DiscoveryManager( |
|
| 68 | - $this->memcacheFactory, |
|
| 69 | - $this->httpClient |
|
| 70 | - ); |
|
| 71 | - |
|
| 72 | - $this->manager = $options['manager']; |
|
| 73 | - $this->certificateManager = $options['certificateManager']; |
|
| 74 | - $this->cloudId = $options['cloudId']; |
|
| 75 | - list($protocol, $remote) = explode('://', $this->cloudId->getRemote()); |
|
| 76 | - if (strpos($remote, '/')) { |
|
| 77 | - list($host, $root) = explode('/', $remote, 2); |
|
| 78 | - } else { |
|
| 79 | - $host = $remote; |
|
| 80 | - $root = ''; |
|
| 81 | - } |
|
| 82 | - $secure = $protocol === 'https'; |
|
| 83 | - $root = rtrim($root, '/') . $discoveryManager->getWebDavEndpoint($this->cloudId->getRemote()); |
|
| 84 | - $this->mountPoint = $options['mountpoint']; |
|
| 85 | - $this->token = $options['token']; |
|
| 86 | - parent::__construct(array( |
|
| 87 | - 'secure' => $secure, |
|
| 88 | - 'host' => $host, |
|
| 89 | - 'root' => $root, |
|
| 90 | - 'user' => $options['token'], |
|
| 91 | - 'password' => (string)$options['password'] |
|
| 92 | - )); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public function getWatcher($path = '', $storage = null) { |
|
| 96 | - if (!$storage) { |
|
| 97 | - $storage = $this; |
|
| 98 | - } |
|
| 99 | - if (!isset($this->watcher)) { |
|
| 100 | - $this->watcher = new Watcher($storage); |
|
| 101 | - $this->watcher->setPolicy(\OC\Files\Cache\Watcher::CHECK_ONCE); |
|
| 102 | - } |
|
| 103 | - return $this->watcher; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - public function getRemoteUser() { |
|
| 107 | - return $this->cloudId->getUser(); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - public function getRemote() { |
|
| 111 | - return $this->cloudId->getRemote(); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - public function getMountPoint() { |
|
| 115 | - return $this->mountPoint; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - public function getToken() { |
|
| 119 | - return $this->token; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - public function getPassword() { |
|
| 123 | - return $this->password; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @brief get id of the mount point |
|
| 128 | - * @return string |
|
| 129 | - */ |
|
| 130 | - public function getId() { |
|
| 131 | - return 'shared::' . md5($this->token . '@' . $this->getRemote()); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - public function getCache($path = '', $storage = null) { |
|
| 135 | - if (is_null($this->cache)) { |
|
| 136 | - $this->cache = new Cache($this, $this->cloudId); |
|
| 137 | - } |
|
| 138 | - return $this->cache; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @param string $path |
|
| 143 | - * @param \OC\Files\Storage\Storage $storage |
|
| 144 | - * @return \OCA\Files_Sharing\External\Scanner |
|
| 145 | - */ |
|
| 146 | - public function getScanner($path = '', $storage = null) { |
|
| 147 | - if (!$storage) { |
|
| 148 | - $storage = $this; |
|
| 149 | - } |
|
| 150 | - if (!isset($this->scanner)) { |
|
| 151 | - $this->scanner = new Scanner($storage); |
|
| 152 | - } |
|
| 153 | - return $this->scanner; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * check if a file or folder has been updated since $time |
|
| 158 | - * |
|
| 159 | - * @param string $path |
|
| 160 | - * @param int $time |
|
| 161 | - * @throws \OCP\Files\StorageNotAvailableException |
|
| 162 | - * @throws \OCP\Files\StorageInvalidException |
|
| 163 | - * @return bool |
|
| 164 | - */ |
|
| 165 | - public function hasUpdated($path, $time) { |
|
| 166 | - // since for owncloud webdav servers we can rely on etag propagation we only need to check the root of the storage |
|
| 167 | - // because of that we only do one check for the entire storage per request |
|
| 168 | - if ($this->updateChecked) { |
|
| 169 | - return false; |
|
| 170 | - } |
|
| 171 | - $this->updateChecked = true; |
|
| 172 | - try { |
|
| 173 | - return parent::hasUpdated('', $time); |
|
| 174 | - } catch (StorageInvalidException $e) { |
|
| 175 | - // check if it needs to be removed |
|
| 176 | - $this->checkStorageAvailability(); |
|
| 177 | - throw $e; |
|
| 178 | - } catch (StorageNotAvailableException $e) { |
|
| 179 | - // check if it needs to be removed or just temp unavailable |
|
| 180 | - $this->checkStorageAvailability(); |
|
| 181 | - throw $e; |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - public function test() { |
|
| 186 | - try { |
|
| 187 | - return parent::test(); |
|
| 188 | - } catch (StorageInvalidException $e) { |
|
| 189 | - // check if it needs to be removed |
|
| 190 | - $this->checkStorageAvailability(); |
|
| 191 | - throw $e; |
|
| 192 | - } catch (StorageNotAvailableException $e) { |
|
| 193 | - // check if it needs to be removed or just temp unavailable |
|
| 194 | - $this->checkStorageAvailability(); |
|
| 195 | - throw $e; |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Check whether this storage is permanently or temporarily |
|
| 201 | - * unavailable |
|
| 202 | - * |
|
| 203 | - * @throws \OCP\Files\StorageNotAvailableException |
|
| 204 | - * @throws \OCP\Files\StorageInvalidException |
|
| 205 | - */ |
|
| 206 | - public function checkStorageAvailability() { |
|
| 207 | - // see if we can find out why the share is unavailable |
|
| 208 | - try { |
|
| 209 | - $this->getShareInfo(); |
|
| 210 | - } catch (NotFoundException $e) { |
|
| 211 | - // a 404 can either mean that the share no longer exists or there is no ownCloud on the remote |
|
| 212 | - if ($this->testRemote()) { |
|
| 213 | - // valid ownCloud instance means that the public share no longer exists |
|
| 214 | - // since this is permanent (re-sharing the file will create a new token) |
|
| 215 | - // we remove the invalid storage |
|
| 216 | - $this->manager->removeShare($this->mountPoint); |
|
| 217 | - $this->manager->getMountManager()->removeMount($this->mountPoint); |
|
| 218 | - throw new StorageInvalidException(); |
|
| 219 | - } else { |
|
| 220 | - // ownCloud instance is gone, likely to be a temporary server configuration error |
|
| 221 | - throw new StorageNotAvailableException(); |
|
| 222 | - } |
|
| 223 | - } catch (ForbiddenException $e) { |
|
| 224 | - // auth error, remove share for now (provide a dialog in the future) |
|
| 225 | - $this->manager->removeShare($this->mountPoint); |
|
| 226 | - $this->manager->getMountManager()->removeMount($this->mountPoint); |
|
| 227 | - throw new StorageInvalidException(); |
|
| 228 | - } catch (\GuzzleHttp\Exception\ConnectException $e) { |
|
| 229 | - throw new StorageNotAvailableException(); |
|
| 230 | - } catch (\GuzzleHttp\Exception\RequestException $e) { |
|
| 231 | - throw new StorageNotAvailableException(); |
|
| 232 | - } catch (\Exception $e) { |
|
| 233 | - throw $e; |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - public function file_exists($path) { |
|
| 238 | - if ($path === '') { |
|
| 239 | - return true; |
|
| 240 | - } else { |
|
| 241 | - return parent::file_exists($path); |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * check if the configured remote is a valid federated share provider |
|
| 247 | - * |
|
| 248 | - * @return bool |
|
| 249 | - */ |
|
| 250 | - protected function testRemote() { |
|
| 251 | - try { |
|
| 252 | - return $this->testRemoteUrl($this->getRemote() . '/ocs-provider/index.php') |
|
| 253 | - || $this->testRemoteUrl($this->getRemote() . '/ocs-provider/') |
|
| 254 | - || $this->testRemoteUrl($this->getRemote() . '/status.php'); |
|
| 255 | - } catch (\Exception $e) { |
|
| 256 | - return false; |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * @param string $url |
|
| 262 | - * @return bool |
|
| 263 | - */ |
|
| 264 | - private function testRemoteUrl($url) { |
|
| 265 | - $cache = $this->memcacheFactory->create('files_sharing_remote_url'); |
|
| 266 | - if($cache->hasKey($url)) { |
|
| 267 | - return (bool)$cache->get($url); |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - $client = $this->httpClient->newClient(); |
|
| 271 | - try { |
|
| 272 | - $result = $client->get($url, [ |
|
| 273 | - 'timeout' => 10, |
|
| 274 | - 'connect_timeout' => 10, |
|
| 275 | - ])->getBody(); |
|
| 276 | - $data = json_decode($result); |
|
| 277 | - $returnValue = (is_object($data) && !empty($data->version)); |
|
| 278 | - } catch (ConnectException $e) { |
|
| 279 | - $returnValue = false; |
|
| 280 | - } catch (ClientException $e) { |
|
| 281 | - $returnValue = false; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - $cache->set($url, $returnValue); |
|
| 285 | - return $returnValue; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Whether the remote is an ownCloud, used since some sharing features are not |
|
| 290 | - * standardized. Let's use this to detect whether to use it. |
|
| 291 | - * |
|
| 292 | - * @return bool |
|
| 293 | - */ |
|
| 294 | - public function remoteIsOwnCloud() { |
|
| 295 | - if(defined('PHPUNIT_RUN') || !$this->testRemoteUrl($this->getRemote() . '/status.php')) { |
|
| 296 | - return false; |
|
| 297 | - } |
|
| 298 | - return true; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * @return mixed |
|
| 303 | - * @throws ForbiddenException |
|
| 304 | - * @throws NotFoundException |
|
| 305 | - * @throws \Exception |
|
| 306 | - */ |
|
| 307 | - public function getShareInfo() { |
|
| 308 | - $remote = $this->getRemote(); |
|
| 309 | - $token = $this->getToken(); |
|
| 310 | - $password = $this->getPassword(); |
|
| 311 | - |
|
| 312 | - // If remote is not an ownCloud do not try to get any share info |
|
| 313 | - if(!$this->remoteIsOwnCloud()) { |
|
| 314 | - return ['status' => 'unsupported']; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - $url = rtrim($remote, '/') . '/index.php/apps/files_sharing/shareinfo?t=' . $token; |
|
| 318 | - |
|
| 319 | - // TODO: DI |
|
| 320 | - $client = \OC::$server->getHTTPClientService()->newClient(); |
|
| 321 | - try { |
|
| 322 | - $response = $client->post($url, [ |
|
| 323 | - 'body' => ['password' => $password], |
|
| 324 | - 'timeout' => 10, |
|
| 325 | - 'connect_timeout' => 10, |
|
| 326 | - ]); |
|
| 327 | - } catch (\GuzzleHttp\Exception\RequestException $e) { |
|
| 328 | - if ($e->getCode() === Http::STATUS_UNAUTHORIZED || $e->getCode() === Http::STATUS_FORBIDDEN) { |
|
| 329 | - throw new ForbiddenException(); |
|
| 330 | - } |
|
| 331 | - if ($e->getCode() === Http::STATUS_NOT_FOUND) { |
|
| 332 | - throw new NotFoundException(); |
|
| 333 | - } |
|
| 334 | - // throw this to be on the safe side: the share will still be visible |
|
| 335 | - // in the UI in case the failure is intermittent, and the user will |
|
| 336 | - // be able to decide whether to remove it if it's really gone |
|
| 337 | - throw new StorageNotAvailableException(); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - return json_decode($response->getBody(), true); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - public function getOwner($path) { |
|
| 344 | - return $this->cloudId->getDisplayId(); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - public function isSharable($path) { |
|
| 348 | - if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) { |
|
| 349 | - return false; |
|
| 350 | - } |
|
| 351 | - return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE); |
|
| 352 | - } |
|
| 44 | + /** @var ICloudId */ |
|
| 45 | + private $cloudId; |
|
| 46 | + /** @var string */ |
|
| 47 | + private $mountPoint; |
|
| 48 | + /** @var string */ |
|
| 49 | + private $token; |
|
| 50 | + /** @var \OCP\ICacheFactory */ |
|
| 51 | + private $memcacheFactory; |
|
| 52 | + /** @var \OCP\Http\Client\IClientService */ |
|
| 53 | + private $httpClient; |
|
| 54 | + /** @var \OCP\ICertificateManager */ |
|
| 55 | + private $certificateManager; |
|
| 56 | + /** @var bool */ |
|
| 57 | + private $updateChecked = false; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @var \OCA\Files_Sharing\External\Manager |
|
| 61 | + */ |
|
| 62 | + private $manager; |
|
| 63 | + |
|
| 64 | + public function __construct($options) { |
|
| 65 | + $this->memcacheFactory = \OC::$server->getMemCacheFactory(); |
|
| 66 | + $this->httpClient = $options['HttpClientService']; |
|
| 67 | + $discoveryManager = new DiscoveryManager( |
|
| 68 | + $this->memcacheFactory, |
|
| 69 | + $this->httpClient |
|
| 70 | + ); |
|
| 71 | + |
|
| 72 | + $this->manager = $options['manager']; |
|
| 73 | + $this->certificateManager = $options['certificateManager']; |
|
| 74 | + $this->cloudId = $options['cloudId']; |
|
| 75 | + list($protocol, $remote) = explode('://', $this->cloudId->getRemote()); |
|
| 76 | + if (strpos($remote, '/')) { |
|
| 77 | + list($host, $root) = explode('/', $remote, 2); |
|
| 78 | + } else { |
|
| 79 | + $host = $remote; |
|
| 80 | + $root = ''; |
|
| 81 | + } |
|
| 82 | + $secure = $protocol === 'https'; |
|
| 83 | + $root = rtrim($root, '/') . $discoveryManager->getWebDavEndpoint($this->cloudId->getRemote()); |
|
| 84 | + $this->mountPoint = $options['mountpoint']; |
|
| 85 | + $this->token = $options['token']; |
|
| 86 | + parent::__construct(array( |
|
| 87 | + 'secure' => $secure, |
|
| 88 | + 'host' => $host, |
|
| 89 | + 'root' => $root, |
|
| 90 | + 'user' => $options['token'], |
|
| 91 | + 'password' => (string)$options['password'] |
|
| 92 | + )); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public function getWatcher($path = '', $storage = null) { |
|
| 96 | + if (!$storage) { |
|
| 97 | + $storage = $this; |
|
| 98 | + } |
|
| 99 | + if (!isset($this->watcher)) { |
|
| 100 | + $this->watcher = new Watcher($storage); |
|
| 101 | + $this->watcher->setPolicy(\OC\Files\Cache\Watcher::CHECK_ONCE); |
|
| 102 | + } |
|
| 103 | + return $this->watcher; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + public function getRemoteUser() { |
|
| 107 | + return $this->cloudId->getUser(); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + public function getRemote() { |
|
| 111 | + return $this->cloudId->getRemote(); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + public function getMountPoint() { |
|
| 115 | + return $this->mountPoint; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + public function getToken() { |
|
| 119 | + return $this->token; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + public function getPassword() { |
|
| 123 | + return $this->password; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @brief get id of the mount point |
|
| 128 | + * @return string |
|
| 129 | + */ |
|
| 130 | + public function getId() { |
|
| 131 | + return 'shared::' . md5($this->token . '@' . $this->getRemote()); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + public function getCache($path = '', $storage = null) { |
|
| 135 | + if (is_null($this->cache)) { |
|
| 136 | + $this->cache = new Cache($this, $this->cloudId); |
|
| 137 | + } |
|
| 138 | + return $this->cache; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @param string $path |
|
| 143 | + * @param \OC\Files\Storage\Storage $storage |
|
| 144 | + * @return \OCA\Files_Sharing\External\Scanner |
|
| 145 | + */ |
|
| 146 | + public function getScanner($path = '', $storage = null) { |
|
| 147 | + if (!$storage) { |
|
| 148 | + $storage = $this; |
|
| 149 | + } |
|
| 150 | + if (!isset($this->scanner)) { |
|
| 151 | + $this->scanner = new Scanner($storage); |
|
| 152 | + } |
|
| 153 | + return $this->scanner; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * check if a file or folder has been updated since $time |
|
| 158 | + * |
|
| 159 | + * @param string $path |
|
| 160 | + * @param int $time |
|
| 161 | + * @throws \OCP\Files\StorageNotAvailableException |
|
| 162 | + * @throws \OCP\Files\StorageInvalidException |
|
| 163 | + * @return bool |
|
| 164 | + */ |
|
| 165 | + public function hasUpdated($path, $time) { |
|
| 166 | + // since for owncloud webdav servers we can rely on etag propagation we only need to check the root of the storage |
|
| 167 | + // because of that we only do one check for the entire storage per request |
|
| 168 | + if ($this->updateChecked) { |
|
| 169 | + return false; |
|
| 170 | + } |
|
| 171 | + $this->updateChecked = true; |
|
| 172 | + try { |
|
| 173 | + return parent::hasUpdated('', $time); |
|
| 174 | + } catch (StorageInvalidException $e) { |
|
| 175 | + // check if it needs to be removed |
|
| 176 | + $this->checkStorageAvailability(); |
|
| 177 | + throw $e; |
|
| 178 | + } catch (StorageNotAvailableException $e) { |
|
| 179 | + // check if it needs to be removed or just temp unavailable |
|
| 180 | + $this->checkStorageAvailability(); |
|
| 181 | + throw $e; |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + public function test() { |
|
| 186 | + try { |
|
| 187 | + return parent::test(); |
|
| 188 | + } catch (StorageInvalidException $e) { |
|
| 189 | + // check if it needs to be removed |
|
| 190 | + $this->checkStorageAvailability(); |
|
| 191 | + throw $e; |
|
| 192 | + } catch (StorageNotAvailableException $e) { |
|
| 193 | + // check if it needs to be removed or just temp unavailable |
|
| 194 | + $this->checkStorageAvailability(); |
|
| 195 | + throw $e; |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Check whether this storage is permanently or temporarily |
|
| 201 | + * unavailable |
|
| 202 | + * |
|
| 203 | + * @throws \OCP\Files\StorageNotAvailableException |
|
| 204 | + * @throws \OCP\Files\StorageInvalidException |
|
| 205 | + */ |
|
| 206 | + public function checkStorageAvailability() { |
|
| 207 | + // see if we can find out why the share is unavailable |
|
| 208 | + try { |
|
| 209 | + $this->getShareInfo(); |
|
| 210 | + } catch (NotFoundException $e) { |
|
| 211 | + // a 404 can either mean that the share no longer exists or there is no ownCloud on the remote |
|
| 212 | + if ($this->testRemote()) { |
|
| 213 | + // valid ownCloud instance means that the public share no longer exists |
|
| 214 | + // since this is permanent (re-sharing the file will create a new token) |
|
| 215 | + // we remove the invalid storage |
|
| 216 | + $this->manager->removeShare($this->mountPoint); |
|
| 217 | + $this->manager->getMountManager()->removeMount($this->mountPoint); |
|
| 218 | + throw new StorageInvalidException(); |
|
| 219 | + } else { |
|
| 220 | + // ownCloud instance is gone, likely to be a temporary server configuration error |
|
| 221 | + throw new StorageNotAvailableException(); |
|
| 222 | + } |
|
| 223 | + } catch (ForbiddenException $e) { |
|
| 224 | + // auth error, remove share for now (provide a dialog in the future) |
|
| 225 | + $this->manager->removeShare($this->mountPoint); |
|
| 226 | + $this->manager->getMountManager()->removeMount($this->mountPoint); |
|
| 227 | + throw new StorageInvalidException(); |
|
| 228 | + } catch (\GuzzleHttp\Exception\ConnectException $e) { |
|
| 229 | + throw new StorageNotAvailableException(); |
|
| 230 | + } catch (\GuzzleHttp\Exception\RequestException $e) { |
|
| 231 | + throw new StorageNotAvailableException(); |
|
| 232 | + } catch (\Exception $e) { |
|
| 233 | + throw $e; |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + public function file_exists($path) { |
|
| 238 | + if ($path === '') { |
|
| 239 | + return true; |
|
| 240 | + } else { |
|
| 241 | + return parent::file_exists($path); |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * check if the configured remote is a valid federated share provider |
|
| 247 | + * |
|
| 248 | + * @return bool |
|
| 249 | + */ |
|
| 250 | + protected function testRemote() { |
|
| 251 | + try { |
|
| 252 | + return $this->testRemoteUrl($this->getRemote() . '/ocs-provider/index.php') |
|
| 253 | + || $this->testRemoteUrl($this->getRemote() . '/ocs-provider/') |
|
| 254 | + || $this->testRemoteUrl($this->getRemote() . '/status.php'); |
|
| 255 | + } catch (\Exception $e) { |
|
| 256 | + return false; |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * @param string $url |
|
| 262 | + * @return bool |
|
| 263 | + */ |
|
| 264 | + private function testRemoteUrl($url) { |
|
| 265 | + $cache = $this->memcacheFactory->create('files_sharing_remote_url'); |
|
| 266 | + if($cache->hasKey($url)) { |
|
| 267 | + return (bool)$cache->get($url); |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + $client = $this->httpClient->newClient(); |
|
| 271 | + try { |
|
| 272 | + $result = $client->get($url, [ |
|
| 273 | + 'timeout' => 10, |
|
| 274 | + 'connect_timeout' => 10, |
|
| 275 | + ])->getBody(); |
|
| 276 | + $data = json_decode($result); |
|
| 277 | + $returnValue = (is_object($data) && !empty($data->version)); |
|
| 278 | + } catch (ConnectException $e) { |
|
| 279 | + $returnValue = false; |
|
| 280 | + } catch (ClientException $e) { |
|
| 281 | + $returnValue = false; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + $cache->set($url, $returnValue); |
|
| 285 | + return $returnValue; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Whether the remote is an ownCloud, used since some sharing features are not |
|
| 290 | + * standardized. Let's use this to detect whether to use it. |
|
| 291 | + * |
|
| 292 | + * @return bool |
|
| 293 | + */ |
|
| 294 | + public function remoteIsOwnCloud() { |
|
| 295 | + if(defined('PHPUNIT_RUN') || !$this->testRemoteUrl($this->getRemote() . '/status.php')) { |
|
| 296 | + return false; |
|
| 297 | + } |
|
| 298 | + return true; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * @return mixed |
|
| 303 | + * @throws ForbiddenException |
|
| 304 | + * @throws NotFoundException |
|
| 305 | + * @throws \Exception |
|
| 306 | + */ |
|
| 307 | + public function getShareInfo() { |
|
| 308 | + $remote = $this->getRemote(); |
|
| 309 | + $token = $this->getToken(); |
|
| 310 | + $password = $this->getPassword(); |
|
| 311 | + |
|
| 312 | + // If remote is not an ownCloud do not try to get any share info |
|
| 313 | + if(!$this->remoteIsOwnCloud()) { |
|
| 314 | + return ['status' => 'unsupported']; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + $url = rtrim($remote, '/') . '/index.php/apps/files_sharing/shareinfo?t=' . $token; |
|
| 318 | + |
|
| 319 | + // TODO: DI |
|
| 320 | + $client = \OC::$server->getHTTPClientService()->newClient(); |
|
| 321 | + try { |
|
| 322 | + $response = $client->post($url, [ |
|
| 323 | + 'body' => ['password' => $password], |
|
| 324 | + 'timeout' => 10, |
|
| 325 | + 'connect_timeout' => 10, |
|
| 326 | + ]); |
|
| 327 | + } catch (\GuzzleHttp\Exception\RequestException $e) { |
|
| 328 | + if ($e->getCode() === Http::STATUS_UNAUTHORIZED || $e->getCode() === Http::STATUS_FORBIDDEN) { |
|
| 329 | + throw new ForbiddenException(); |
|
| 330 | + } |
|
| 331 | + if ($e->getCode() === Http::STATUS_NOT_FOUND) { |
|
| 332 | + throw new NotFoundException(); |
|
| 333 | + } |
|
| 334 | + // throw this to be on the safe side: the share will still be visible |
|
| 335 | + // in the UI in case the failure is intermittent, and the user will |
|
| 336 | + // be able to decide whether to remove it if it's really gone |
|
| 337 | + throw new StorageNotAvailableException(); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + return json_decode($response->getBody(), true); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + public function getOwner($path) { |
|
| 344 | + return $this->cloudId->getDisplayId(); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + public function isSharable($path) { |
|
| 348 | + if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) { |
|
| 349 | + return false; |
|
| 350 | + } |
|
| 351 | + return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE); |
|
| 352 | + } |
|
| 353 | 353 | |
| 354 | - public function getPermissions($path) { |
|
| 355 | - $response = $this->propfind($path); |
|
| 356 | - if (isset($response['{http://open-collaboration-services.org/ns}share-permissions'])) { |
|
| 357 | - $permissions = $response['{http://open-collaboration-services.org/ns}share-permissions']; |
|
| 358 | - } else { |
|
| 359 | - // use default permission if remote server doesn't provide the share permissions |
|
| 360 | - if ($this->is_dir($path)) { |
|
| 361 | - $permissions = \OCP\Constants::PERMISSION_ALL; |
|
| 362 | - } else { |
|
| 363 | - $permissions = \OCP\Constants::PERMISSION_ALL & ~\OCP\Constants::PERMISSION_CREATE; |
|
| 364 | - } |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - return $permissions; |
|
| 368 | - } |
|
| 354 | + public function getPermissions($path) { |
|
| 355 | + $response = $this->propfind($path); |
|
| 356 | + if (isset($response['{http://open-collaboration-services.org/ns}share-permissions'])) { |
|
| 357 | + $permissions = $response['{http://open-collaboration-services.org/ns}share-permissions']; |
|
| 358 | + } else { |
|
| 359 | + // use default permission if remote server doesn't provide the share permissions |
|
| 360 | + if ($this->is_dir($path)) { |
|
| 361 | + $permissions = \OCP\Constants::PERMISSION_ALL; |
|
| 362 | + } else { |
|
| 363 | + $permissions = \OCP\Constants::PERMISSION_ALL & ~\OCP\Constants::PERMISSION_CREATE; |
|
| 364 | + } |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + return $permissions; |
|
| 368 | + } |
|
| 369 | 369 | |
| 370 | 370 | } |
@@ -29,59 +29,59 @@ |
||
| 29 | 29 | use OCP\IUser; |
| 30 | 30 | |
| 31 | 31 | class MountProvider implements IMountProvider { |
| 32 | - const STORAGE = '\OCA\Files_Sharing\External\Storage'; |
|
| 32 | + const STORAGE = '\OCA\Files_Sharing\External\Storage'; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @var \OCP\IDBConnection |
|
| 36 | - */ |
|
| 37 | - private $connection; |
|
| 34 | + /** |
|
| 35 | + * @var \OCP\IDBConnection |
|
| 36 | + */ |
|
| 37 | + private $connection; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @var callable |
|
| 41 | - */ |
|
| 42 | - private $managerProvider; |
|
| 39 | + /** |
|
| 40 | + * @var callable |
|
| 41 | + */ |
|
| 42 | + private $managerProvider; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @var ICloudIdManager |
|
| 46 | - */ |
|
| 47 | - private $cloudIdManager; |
|
| 44 | + /** |
|
| 45 | + * @var ICloudIdManager |
|
| 46 | + */ |
|
| 47 | + private $cloudIdManager; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @param \OCP\IDBConnection $connection |
|
| 51 | - * @param callable $managerProvider due to setup order we need a callable that return the manager instead of the manager itself |
|
| 52 | - * @param ICloudIdManager $cloudIdManager |
|
| 53 | - */ |
|
| 54 | - public function __construct(IDBConnection $connection, callable $managerProvider, ICloudIdManager $cloudIdManager) { |
|
| 55 | - $this->connection = $connection; |
|
| 56 | - $this->managerProvider = $managerProvider; |
|
| 57 | - $this->cloudIdManager = $cloudIdManager; |
|
| 58 | - } |
|
| 49 | + /** |
|
| 50 | + * @param \OCP\IDBConnection $connection |
|
| 51 | + * @param callable $managerProvider due to setup order we need a callable that return the manager instead of the manager itself |
|
| 52 | + * @param ICloudIdManager $cloudIdManager |
|
| 53 | + */ |
|
| 54 | + public function __construct(IDBConnection $connection, callable $managerProvider, ICloudIdManager $cloudIdManager) { |
|
| 55 | + $this->connection = $connection; |
|
| 56 | + $this->managerProvider = $managerProvider; |
|
| 57 | + $this->cloudIdManager = $cloudIdManager; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - public function getMount(IUser $user, $data, IStorageFactory $storageFactory) { |
|
| 61 | - $managerProvider = $this->managerProvider; |
|
| 62 | - $manager = $managerProvider(); |
|
| 63 | - $data['manager'] = $manager; |
|
| 64 | - $mountPoint = '/' . $user->getUID() . '/files/' . ltrim($data['mountpoint'], '/'); |
|
| 65 | - $data['mountpoint'] = $mountPoint; |
|
| 66 | - $data['cloudId'] = $this->cloudIdManager->getCloudId($data['owner'], $data['remote']); |
|
| 67 | - $data['certificateManager'] = \OC::$server->getCertificateManager($user->getUID()); |
|
| 68 | - $data['HttpClientService'] = \OC::$server->getHTTPClientService(); |
|
| 69 | - return new Mount(self::STORAGE, $mountPoint, $data, $manager, $storageFactory); |
|
| 70 | - } |
|
| 60 | + public function getMount(IUser $user, $data, IStorageFactory $storageFactory) { |
|
| 61 | + $managerProvider = $this->managerProvider; |
|
| 62 | + $manager = $managerProvider(); |
|
| 63 | + $data['manager'] = $manager; |
|
| 64 | + $mountPoint = '/' . $user->getUID() . '/files/' . ltrim($data['mountpoint'], '/'); |
|
| 65 | + $data['mountpoint'] = $mountPoint; |
|
| 66 | + $data['cloudId'] = $this->cloudIdManager->getCloudId($data['owner'], $data['remote']); |
|
| 67 | + $data['certificateManager'] = \OC::$server->getCertificateManager($user->getUID()); |
|
| 68 | + $data['HttpClientService'] = \OC::$server->getHTTPClientService(); |
|
| 69 | + return new Mount(self::STORAGE, $mountPoint, $data, $manager, $storageFactory); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - public function getMountsForUser(IUser $user, IStorageFactory $loader) { |
|
| 73 | - $query = $this->connection->prepare(' |
|
| 72 | + public function getMountsForUser(IUser $user, IStorageFactory $loader) { |
|
| 73 | + $query = $this->connection->prepare(' |
|
| 74 | 74 | SELECT `remote`, `share_token`, `password`, `mountpoint`, `owner` |
| 75 | 75 | FROM `*PREFIX*share_external` |
| 76 | 76 | WHERE `user` = ? AND `accepted` = ? |
| 77 | 77 | '); |
| 78 | - $query->execute([$user->getUID(), 1]); |
|
| 79 | - $mounts = []; |
|
| 80 | - while ($row = $query->fetch()) { |
|
| 81 | - $row['manager'] = $this; |
|
| 82 | - $row['token'] = $row['share_token']; |
|
| 83 | - $mounts[] = $this->getMount($user, $row, $loader); |
|
| 84 | - } |
|
| 85 | - return $mounts; |
|
| 86 | - } |
|
| 78 | + $query->execute([$user->getUID(), 1]); |
|
| 79 | + $mounts = []; |
|
| 80 | + while ($row = $query->fetch()) { |
|
| 81 | + $row['manager'] = $this; |
|
| 82 | + $row['token'] = $row['share_token']; |
|
| 83 | + $mounts[] = $this->getMount($user, $row, $loader); |
|
| 84 | + } |
|
| 85 | + return $mounts; |
|
| 86 | + } |
|
| 87 | 87 | } |
@@ -23,12 +23,12 @@ |
||
| 23 | 23 | namespace OCA\Files_Sharing\External; |
| 24 | 24 | |
| 25 | 25 | class Watcher extends \OC\Files\Cache\Watcher { |
| 26 | - /** |
|
| 27 | - * remove deleted files in $path from the cache |
|
| 28 | - * |
|
| 29 | - * @param string $path |
|
| 30 | - */ |
|
| 31 | - public function cleanFolder($path) { |
|
| 32 | - // not needed, the scanner takes care of this |
|
| 33 | - } |
|
| 26 | + /** |
|
| 27 | + * remove deleted files in $path from the cache |
|
| 28 | + * |
|
| 29 | + * @param string $path |
|
| 30 | + */ |
|
| 31 | + public function cleanFolder($path) { |
|
| 32 | + // not needed, the scanner takes care of this |
|
| 33 | + } |
|
| 34 | 34 | } |