2020-01-18 17:50:11 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Shaarli\Front;
|
|
|
|
|
|
|
|
use Shaarli\Container\ShaarliContainer;
|
2020-05-22 13:20:31 +02:00
|
|
|
use Shaarli\Front\Exception\ShaarliFrontException;
|
|
|
|
use Shaarli\Front\Exception\UnauthorizedException;
|
2020-01-18 17:50:11 +01:00
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ShaarliMiddleware
|
|
|
|
*
|
|
|
|
* This will be called before accessing any Shaarli controller.
|
|
|
|
*/
|
|
|
|
class ShaarliMiddleware
|
|
|
|
{
|
|
|
|
/** @var ShaarliContainer contains all Shaarli DI */
|
|
|
|
protected $container;
|
|
|
|
|
|
|
|
public function __construct(ShaarliContainer $container)
|
|
|
|
{
|
|
|
|
$this->container = $container;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware execution:
|
2020-07-06 08:04:35 +02:00
|
|
|
* - run updates
|
|
|
|
* - if not logged in open shaarli, redirect to login
|
2020-01-18 17:50:11 +01:00
|
|
|
* - execute the controller
|
|
|
|
* - return the response
|
|
|
|
*
|
|
|
|
* In case of error, the error template will be displayed with the exception message.
|
|
|
|
*
|
|
|
|
* @param Request $request Slim request
|
|
|
|
* @param Response $response Slim response
|
|
|
|
* @param callable $next Next action
|
|
|
|
*
|
|
|
|
* @return Response response.
|
|
|
|
*/
|
2020-07-06 08:04:35 +02:00
|
|
|
public function __invoke(Request $request, Response $response, callable $next): Response
|
2020-01-18 17:50:11 +01:00
|
|
|
{
|
2020-06-13 13:08:01 +02:00
|
|
|
$this->container->basePath = rtrim($request->getUri()->getBasePath(), '/');
|
2020-06-13 11:22:14 +02:00
|
|
|
|
2020-06-13 13:08:01 +02:00
|
|
|
try {
|
2020-07-06 08:04:35 +02:00
|
|
|
$this->runUpdates();
|
|
|
|
$this->checkOpenShaarli($request, $response, $next);
|
|
|
|
|
|
|
|
return $next($request, $response);
|
2020-05-22 13:20:31 +02:00
|
|
|
} catch (ShaarliFrontException $e) {
|
2020-07-06 08:04:35 +02:00
|
|
|
// Possible functional error
|
|
|
|
$this->container->pageBuilder->reset();
|
2020-01-18 17:50:11 +01:00
|
|
|
$this->container->pageBuilder->assign('message', $e->getMessage());
|
2020-07-06 08:04:35 +02:00
|
|
|
|
|
|
|
$response = $response->withStatus($e->getCode());
|
|
|
|
|
|
|
|
return $response->write($this->container->pageBuilder->render('error'));
|
|
|
|
} catch (UnauthorizedException $e) {
|
|
|
|
return $response->withRedirect($this->container->basePath . '/login');
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
// Unknown error encountered
|
|
|
|
$this->container->pageBuilder->reset();
|
2020-01-18 17:50:11 +01:00
|
|
|
if ($this->container->conf->get('dev.debug', false)) {
|
2020-07-06 08:04:35 +02:00
|
|
|
$this->container->pageBuilder->assign('message', $e->getMessage());
|
2020-01-18 17:50:11 +01:00
|
|
|
$this->container->pageBuilder->assign(
|
|
|
|
'stacktrace',
|
2020-07-06 08:04:35 +02:00
|
|
|
nl2br(get_class($e) .': '. PHP_EOL . $e->getTraceAsString())
|
2020-01-18 17:50:11 +01:00
|
|
|
);
|
2020-07-06 08:04:35 +02:00
|
|
|
} else {
|
|
|
|
$this->container->pageBuilder->assign('message', t('An unexpected error occurred.'));
|
2020-01-18 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 08:04:35 +02:00
|
|
|
$response = $response->withStatus(500);
|
|
|
|
|
|
|
|
return $response->write($this->container->pageBuilder->render('error'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the updater for every requests processed while logged in.
|
|
|
|
*/
|
|
|
|
protected function runUpdates(): void
|
|
|
|
{
|
|
|
|
if ($this->container->loginManager->isLoggedIn() !== true) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$newUpdates = $this->container->updater->update();
|
|
|
|
if (!empty($newUpdates)) {
|
|
|
|
$this->container->updater->writeUpdates(
|
|
|
|
$this->container->conf->get('resource.updates'),
|
|
|
|
$this->container->updater->getDoneUpdates()
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->container->pageCacheManager->invalidateCaches();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Access is denied to most pages with `hide_public_links` + `force_login` settings.
|
|
|
|
*/
|
|
|
|
protected function checkOpenShaarli(Request $request, Response $response, callable $next): bool
|
|
|
|
{
|
|
|
|
if (// if the user isn't logged in
|
|
|
|
!$this->container->loginManager->isLoggedIn()
|
|
|
|
// and Shaarli doesn't have public content...
|
|
|
|
&& $this->container->conf->get('privacy.hide_public_links')
|
|
|
|
// and is configured to enforce the login
|
|
|
|
&& $this->container->conf->get('privacy.force_login')
|
|
|
|
// and the current page isn't already the login page
|
|
|
|
// and the user is not requesting a feed (which would lead to a different content-type as expected)
|
|
|
|
&& !in_array($next->getName(), ['login', 'atom', 'rss'], true)
|
|
|
|
) {
|
|
|
|
throw new UnauthorizedException();
|
2020-01-18 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 08:04:35 +02:00
|
|
|
return true;
|
2020-01-18 17:50:11 +01:00
|
|
|
}
|
|
|
|
}
|