2018-05-18 13:28:52 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace OC\AppFramework\Middleware\PublicShare;
|
|
|
|
|
|
|
|
use OC\AppFramework\Middleware\PublicShare\Exceptions\NeedAuthenticationException;
|
|
|
|
use OCP\AppFramework\AuthPublicShareController;
|
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
|
|
|
use OCP\AppFramework\Http\Response;
|
|
|
|
use OCP\AppFramework\Middleware;
|
|
|
|
use OCP\AppFramework\PublicShareController;
|
|
|
|
use OCP\Files\NotFoundException;
|
2018-06-11 15:19:15 +03:00
|
|
|
use OCP\IConfig;
|
2018-05-18 13:28:52 +03:00
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\ISession;
|
|
|
|
|
|
|
|
class PublicShareMiddleware extends Middleware {
|
|
|
|
/** @var IRequest */
|
|
|
|
private $request;
|
|
|
|
|
|
|
|
/** @var ISession */
|
|
|
|
private $session;
|
|
|
|
|
2018-06-11 15:19:15 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
public function __construct(IRequest $request, ISession $session, IConfig $config) {
|
2018-05-18 13:28:52 +03:00
|
|
|
$this->request = $request;
|
|
|
|
$this->session = $session;
|
2018-06-11 15:19:15 +03:00
|
|
|
$this->config = $config;
|
2018-05-18 13:28:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function beforeController($controller, $methodName) {
|
|
|
|
if (!($controller instanceof PublicShareController)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-11 15:19:15 +03:00
|
|
|
if (!$this->isLinkSharingEnabled()) {
|
|
|
|
throw new NotFoundException('Link sharing is disabled');
|
|
|
|
}
|
|
|
|
|
2018-05-18 13:28:52 +03:00
|
|
|
// We require the token parameter to be set
|
|
|
|
$token = $this->request->getParam('token');
|
|
|
|
if ($token === null) {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the token
|
|
|
|
$controller->setToken($token);
|
|
|
|
|
|
|
|
if (!$controller->isValidToken()) {
|
|
|
|
$controller->shareNotFound();
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
// No need to check for authentication when we try to authenticate
|
|
|
|
if ($methodName === 'authenticate' || $methodName === 'showAuthenticate') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If authentication succeeds just continue
|
2018-06-14 12:14:55 +03:00
|
|
|
if ($controller->isAuthenticated()) {
|
2018-05-18 13:28:52 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we can authenticate to this controller do it else we throw a 404 to not leak any info
|
|
|
|
if ($controller instanceof AuthPublicShareController) {
|
|
|
|
$this->session->set('public_link_authenticate_redirect', json_encode($this->request->getParams()));
|
|
|
|
throw new NeedAuthenticationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function afterException($controller, $methodName, \Exception $exception) {
|
|
|
|
if (!($controller instanceof PublicShareController)) {
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($exception instanceof NotFoundException) {
|
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($controller instanceof AuthPublicShareController && $exception instanceof NeedAuthenticationException) {
|
|
|
|
return $controller->getAuthenticationRedirect($this->getFunctionForRoute($this->request->getParam('_route')));
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getFunctionForRoute(string $route): string {
|
2018-05-23 11:50:44 +03:00
|
|
|
$tmp = explode('.', $route);
|
|
|
|
return array_pop($tmp);
|
2018-05-18 13:28:52 +03:00
|
|
|
}
|
2018-06-11 15:19:15 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if link sharing is allowed
|
|
|
|
*/
|
|
|
|
private function isLinkSharingEnabled(): bool {
|
|
|
|
// Check if the shareAPI is enabled
|
|
|
|
if ($this->config->getAppValue('core', 'shareapi_enabled', 'yes') !== 'yes') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether public sharing is enabled
|
|
|
|
if($this->config->getAppValue('core', 'shareapi_allow_links', 'yes') !== 'yes') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-18 13:28:52 +03:00
|
|
|
}
|