3 namespace Shaarli\Front
;
5 use Shaarli\Container\ShaarliContainer
;
6 use Shaarli\Front\Exception\UnauthorizedException
;
8 use Slim\Http\Response
;
11 * Class ShaarliMiddleware
13 * This will be called before accessing any Shaarli controller.
15 class ShaarliMiddleware
17 /** @var ShaarliContainer contains all Shaarli DI */
20 public function __construct(ShaarliContainer
$container)
22 $this->container
= $container;
26 * Middleware execution:
28 * - if not logged in open shaarli, redirect to login
29 * - execute the controller
30 * - return the response
32 * In case of error, the error template will be displayed with the exception message.
34 * @param Request $request Slim request
35 * @param Response $response Slim response
36 * @param callable $next Next action
38 * @return Response response.
40 public function __invoke(Request
$request, Response
$response, callable
$next): Response
42 $this->initBasePath($request);
46 !is_file($this->container
->conf
->getConfigFileExt())
47 && !in_array($next->getName(), ['displayInstall', 'saveInstall'], true)
49 return $response->withRedirect($this->container
->basePath
. '/install');
53 $this->checkOpenShaarli($request, $response, $next);
55 return $next($request, $response);
56 } catch (UnauthorizedException
$e) {
57 $returnUrl = urlencode($this->container
->environment
['REQUEST_URI']);
59 return $response->withRedirect($this->container
->basePath
. '/login?returnurl=' . $returnUrl);
61 // Other exceptions are handled by ErrorController
65 * Run the updater for every requests processed while logged in.
67 protected function runUpdates(): void
69 if ($this->container
->loginManager
->isLoggedIn() !== true) {
73 $this->container
->updater
->setBasePath($this->container
->basePath
);
74 $newUpdates = $this->container
->updater
->update();
75 if (!empty($newUpdates)) {
76 $this->container
->updater
->writeUpdates(
77 $this->container
->conf
->get('resource.updates'),
78 $this->container
->updater
->getDoneUpdates()
81 $this->container
->pageCacheManager
->invalidateCaches();
86 * Access is denied to most pages with `hide_public_links` + `force_login` settings.
88 protected function checkOpenShaarli(Request
$request, Response
$response, callable
$next): bool
91 // if the user isn't logged in
92 !$this->container
->loginManager
->isLoggedIn()
93 // and Shaarli doesn't have public content...
94 && $this->container
->conf
->get('privacy.hide_public_links')
95 // and is configured to enforce the login
96 && $this->container
->conf
->get('privacy.force_login')
97 // and the current page isn't already the login page
98 // and the user is not requesting a feed (which would lead to a different content-type as expected)
99 && !in_array($next->getName(), ['login', 'processLogin', 'atom', 'rss'], true)
101 throw new UnauthorizedException();
108 * Initialize the URL base path if it hasn't been defined yet.
110 protected function initBasePath(Request
$request): void
112 if (null === $this->container
->basePath
) {
113 $this->container
->basePath
= rtrim($request->getUri()->getBasePath(), '/');