RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
-<Limit GET POST PUT DELETE OPTIONS>
- <IfModule version_module>
- <IfVersion >= 2.4>
- Require all granted
- </IfVersion>
- <IfVersion < 2.4>
- Allow from all
- Deny from none
- </IfVersion>
- </IfModule>
-
- <IfModule !version_module>
- Require all granted
- </IfModule>
-</Limit>
-
-<LimitExcept GET POST PUT DELETE OPTIONS>
+<LimitExcept GET POST PUT DELETE PATCH OPTIONS>
<IfModule version_module>
<IfVersion >= 2.4>
Require all denied
use Shaarli\Feed\FeedBuilder;
use Shaarli\Formatter\FormatterFactory;
use Shaarli\Front\Controller\Visitor\ErrorController;
+use Shaarli\Front\Controller\Visitor\ErrorNotFoundController;
use Shaarli\History;
use Shaarli\Http\HttpAccess;
use Shaarli\Netscape\NetscapeBookmarkUtils;
);
};
+ $container['notFoundHandler'] = function (ShaarliContainer $container): ErrorNotFoundController {
+ return new ErrorNotFoundController($container);
+ };
$container['errorHandler'] = function (ShaarliContainer $container): ErrorController {
return new ErrorController($container);
};
/**
* Extension of Slim container to document the injected objects.
*
- * @property string $basePath Shaarli's instance base path (e.g. `/shaarli/`)
+ * @property string $basePath Shaarli's instance base path (e.g. `/shaarli/`)
* @property BookmarkServiceInterface $bookmarkService
* @property CookieManager $cookieManager
* @property ConfigManager $conf
- * @property mixed[] $environment $_SERVER automatically injected by Slim
- * @property callable $errorHandler Overrides default Slim exception display
+ * @property mixed[] $environment $_SERVER automatically injected by Slim
+ * @property callable $errorHandler Overrides default Slim exception display
* @property FeedBuilder $feedBuilder
* @property FormatterFactory $formatterFactory
* @property History $history
* @property HttpAccess $httpAccess
* @property LoginManager $loginManager
* @property NetscapeBookmarkUtils $netscapeBookmarkUtils
+ * @property callable $notFoundHandler Overrides default Slim exception display
* @property PageBuilder $pageBuilder
* @property PageCacheManager $pageCacheManager
- * @property callable $phpErrorHandler Overrides default Slim PHP error display
+ * @property callable $phpErrorHandler Overrides default Slim PHP error display
* @property PluginManager $pluginManager
* @property SessionManager $sessionManager
* @property Thumbnailer $thumbnailer
if (isset($parameters['parameters_form'])) {
unset($parameters['parameters_form']);
+ unset($parameters['token']);
foreach ($parameters as $param => $value) {
$this->container->conf->set('plugins.'. $param, escape($value));
}
--- /dev/null
+<?php
+
+declare(strict_types=1);
+
+namespace Shaarli\Front\Controller\Visitor;
+
+use Slim\Http\Request;
+use Slim\Http\Response;
+
+/**
+ * Controller used to render the 404 error page.
+ */
+class ErrorNotFoundController extends ShaarliVisitorController
+{
+ public function __invoke(Request $request, Response $response): Response
+ {
+ // Request from the API
+ if (false !== strpos($request->getRequestTarget(), '/api/v1')) {
+ return $response->withStatus(404);
+ }
+
+ // This is required because the middleware is ignored if the route is not found.
+ $this->container->basePath = rtrim($request->getUri()->getBasePath(), '/');
+
+ $this->assignView('error_message', t('Requested page could not be found.'));
+
+ return $response->withStatus(404)->write($this->render('404'));
+ }
+}
*/
public function executeHooks($hook, &$data, $params = array())
{
- if (!empty($params['target'])) {
- $data['_PAGE_'] = $params['target'];
- }
-
- if (isset($params['loggedin'])) {
- $data['_LOGGEDIN_'] = $params['loggedin'];
- }
-
- if (isset($params['basePath'])) {
- $data['_BASE_PATH_'] = $params['basePath'];
- }
-
- if (isset($params['bookmarkService'])) {
- $data['_BOOKMARK_SERVICE_'] = $params['bookmarkService'];
+ $metadataParameters = [
+ 'target' => '_PAGE_',
+ 'loggedin' => '_LOGGEDIN_',
+ 'basePath' => '_BASE_PATH_',
+ 'bookmarkService' => '_BOOKMARK_SERVICE_',
+ ];
+
+ foreach ($metadataParameters as $parameter => $metaKey) {
+ if (array_key_exists($parameter, $params)) {
+ $data[$metaKey] = $params[$parameter];
+ }
}
foreach ($this->loadedPlugins as $plugin) {
}
}
}
+
+ foreach ($metadataParameters as $metaKey) {
+ unset($data[$metaKey]);
+ }
}
/**
$this->assertTrue(function_exists('hook_test_random'));
- $data = array(0 => 'woot');
+ $data = [0 => 'woot'];
$this->pluginManager->executeHooks('random', $data);
- $this->assertEquals('woot', $data[1]);
- $data = array(0 => 'woot');
+ static::assertCount(2, $data);
+ static::assertSame('woot', $data[1]);
+
+ $data = [0 => 'woot'];
$this->pluginManager->executeHooks('random', $data, array('target' => 'test'));
- $this->assertEquals('page test', $data[1]);
- $data = array(0 => 'woot');
+ static::assertCount(2, $data);
+ static::assertSame('page test', $data[1]);
+
+ $data = [0 => 'woot'];
$this->pluginManager->executeHooks('random', $data, array('loggedin' => true));
- $this->assertEquals('loggedin', $data[1]);
+
+ static::assertCount(2, $data);
+ static::assertEquals('loggedin', $data[1]);
+
+ $data = [0 => 'woot'];
+ $this->pluginManager->executeHooks('random', $data, array('loggedin' => null));
+
+ static::assertCount(3, $data);
+ static::assertEquals('loggedin', $data[1]);
+ static::assertArrayHasKey(2, $data);
+ static::assertNull($data[2]);
}
/**
*/
public function testPluginNotFound(): void
{
- $this->pluginManager->load(array());
- $this->pluginManager->load(array('nope', 'renope'));
+ $this->pluginManager->load([]);
+ $this->pluginManager->load(['nope', 'renope']);
$this->addToAssertionCount(1);
}
public function testGetPluginsMeta(): void
{
PluginManager::$PLUGINS_PATH = self::$pluginPath;
- $this->pluginManager->load(array(self::$pluginName));
+ $this->pluginManager->load([self::$pluginName]);
- $expectedParameters = array(
- 'pop' => array(
+ $expectedParameters = [
+ 'pop' => [
'value' => '',
'desc' => 'pop description',
- ),
- 'hip' => array(
+ ],
+ 'hip' => [
'value' => '',
'desc' => '',
- ),
- );
+ ],
+ ];
$meta = $this->pluginManager->getPluginsMeta();
$this->assertEquals('test plugin', $meta[self::$pluginName]['description']);
$this->assertEquals($expectedParameters, $meta[self::$pluginName]['parameters']);
use Shaarli\Feed\FeedBuilder;
use Shaarli\Formatter\FormatterFactory;
use Shaarli\Front\Controller\Visitor\ErrorController;
+use Shaarli\Front\Controller\Visitor\ErrorNotFoundController;
use Shaarli\History;
use Shaarli\Http\HttpAccess;
use Shaarli\Netscape\NetscapeBookmarkUtils;
static::assertInstanceOf(PageBuilder::class, $container->pageBuilder);
static::assertInstanceOf(PageCacheManager::class, $container->pageCacheManager);
static::assertInstanceOf(ErrorController::class, $container->phpErrorHandler);
+ static::assertInstanceOf(ErrorNotFoundController::class, $container->notFoundHandler);
static::assertInstanceOf(PluginManager::class, $container->pluginManager);
static::assertInstanceOf(SessionManager::class, $container->sessionManager);
static::assertInstanceOf(Thumbnailer::class, $container->thumbnailer);
array_map(function (string $plugin) use ($path) { touch($path . '/' . $plugin); }, static::PLUGIN_NAMES);
}
- public function tearDown()
+ public function tearDown(): void
{
$path = __DIR__ . '/folder';
array_map(function (string $plugin) use ($path) { unlink($path . '/' . $plugin); }, static::PLUGIN_NAMES);
'parameters_form' => true,
'parameter1' => 'blip',
'parameter2' => 'blop',
+ 'token' => 'this parameter should not be saved'
];
$request = $this->createMock(Request::class);
->with('save_plugin_parameters', $parameters)
;
$this->container->conf
- ->expects(static::atLeastOnce())
+ ->expects(static::exactly(2))
->method('set')
->withConsecutive(['plugins.parameter1', 'blip'], ['plugins.parameter2', 'blop'])
;
--- /dev/null
+<?php
+
+declare(strict_types=1);
+
+namespace Shaarli\Front\Controller\Visitor;
+
+use PHPUnit\Framework\TestCase;
+use Slim\Http\Request;
+use Slim\Http\Response;
+use Slim\Http\Uri;
+
+class ErrorNotFoundControllerTest extends TestCase
+{
+ use FrontControllerMockHelper;
+
+ /** @var ErrorNotFoundController */
+ protected $controller;
+
+ public function setUp(): void
+ {
+ $this->createContainer();
+
+ $this->controller = new ErrorNotFoundController($this->container);
+ }
+
+ /**
+ * Test displaying 404 error
+ */
+ public function testDisplayNotFoundError(): void
+ {
+ $request = $this->createMock(Request::class);
+ $request->expects(static::once())->method('getRequestTarget')->willReturn('/');
+ $request->method('getUri')->willReturnCallback(function (): Uri {
+ $uri = $this->createMock(Uri::class);
+ $uri->method('getBasePath')->willReturn('/subfolder');
+
+ return $uri;
+ });
+
+ $response = new Response();
+
+ // Save RainTPL assigned variables
+ $assignedVariables = [];
+ $this->assignTemplateVars($assignedVariables);
+
+ $result = ($this->controller)(
+ $request,
+ $response
+ );
+
+ static::assertSame(404, $result->getStatusCode());
+ static::assertSame('404', (string) $result->getBody());
+ static::assertSame('Requested page could not be found.', $assignedVariables['error_message']);
+ }
+
+ /**
+ * Test displaying 404 error from REST API
+ */
+ public function testDisplayNotFoundErrorFromAPI(): void
+ {
+ $request = $this->createMock(Request::class);
+ $request->expects(static::once())->method('getRequestTarget')->willReturn('/sufolder/api/v1/links');
+ $request->method('getUri')->willReturnCallback(function (): Uri {
+ $uri = $this->createMock(Uri::class);
+ $uri->method('getBasePath')->willReturn('/subfolder');
+
+ return $uri;
+ });
+
+ $response = new Response();
+
+ // Save RainTPL assigned variables
+ $assignedVariables = [];
+ $this->assignTemplateVars($assignedVariables);
+
+ $result = ($this->controller)($request, $response);
+
+ static::assertSame(404, $result->getStatusCode());
+ static::assertSame([], $assignedVariables);
+ }
+}
protected function assignTemplateVars(array &$variables): void
{
$this->container->pageBuilder
- ->expects(static::atLeastOnce())
->method('assign')
->willReturnCallback(function ($key, $value) use (&$variables) {
$variables[$key] = $value;
$data[1] = 'page test';
} elseif (isset($data['_LOGGEDIN_']) && $data['_LOGGEDIN_'] === true) {
$data[1] = 'loggedin';
+ } elseif (array_key_exists('_LOGGEDIN_', $data)) {
+ $data[1] = 'loggedin';
+ $data[2] = $data['_LOGGEDIN_'];
} else {
$data[1] = $data[0];
}