Improved session error handling

Added check for the minimum php version
Package update and cleanup
This commit is contained in:
Sergio Brighenti 2019-05-07 00:49:24 +02:00
parent d02bc0afa2
commit 0ce9bde57c
8 changed files with 1261 additions and 705 deletions

View file

@ -3,6 +3,8 @@
namespace App\Web;
use Exception;
class Session
{
@ -10,19 +12,24 @@ class Session
* Session constructor.
* @param string $name
* @param string $path
* @throws \Exception
* @throws Exception
*/
public function __construct(string $name, $path = '')
{
if (session_status() === PHP_SESSION_NONE) {
if (!is_writable($path) && $path !== '') {
throw new \Exception("The given path '{$path}' is not writable.");
throw new Exception("The given path '{$path}' is not writable.");
}
session_start([
$started = @session_start([
'name' => $name,
'save_path' => $path,
'cookie_httponly' => true,
]);
if (!$started) {
throw new Exception("Cannot start the HTTP session. That the session path '{$path}' is writable and your PHP settings.");
}
}
}
@ -98,7 +105,7 @@ class Session
* Retrieve flash alerts
* @return array
*/
public function getAlert()
public function getAlert(): ?array
{
$flash = self::get('_flash');
self::set('_flash', []);

View file

@ -1,6 +1,8 @@
<?php
use App\Database\DB;
use App\Exceptions\MaintenanceException;
use App\Exceptions\UnauthorizedException;
use App\Web\Lang;
use App\Web\Session;
use Monolog\Formatter\LineFormatter;
@ -8,6 +10,12 @@ use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Slim\App;
use Slim\Container;
use Slim\Http\Environment;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\Uri;
use Slim\Views\Twig;
use Twig\TwigFunction;
if (!file_exists('config.php') && is_dir('install/')) {
header('Location: ./install/');
@ -66,7 +74,7 @@ $container['lang'] = function ($container) {
};
$container['view'] = function ($container) use (&$config) {
$view = new \Slim\Views\Twig(BASE_DIR . 'resources/templates', [
$view = new Twig(BASE_DIR . 'resources/templates', [
'cache' => BASE_DIR . 'resources/cache',
'autoescape' => 'html',
'debug' => $config['displayErrorDetails'],
@ -75,7 +83,7 @@ $container['view'] = function ($container) use (&$config) {
// Instantiate and add Slim specific extension
$router = $container->get('router');
$uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER));
$uri = Uri::createFromEnvironment(new Environment($_SERVER));
$view->addExtension(new Slim\Views\TwigExtension($router, $uri));
$view->getEnvironment()->addGlobal('config', $config);
@ -85,29 +93,29 @@ $container['view'] = function ($container) use (&$config) {
$view->getEnvironment()->addGlobal('current_lang', $container->get('lang')->getLang());
$view->getEnvironment()->addGlobal('PLATFORM_VERSION', PLATFORM_VERSION);
$view->getEnvironment()->addFunction(new Twig_Function('route', 'route'));
$view->getEnvironment()->addFunction(new Twig_Function('lang', 'lang'));
$view->getEnvironment()->addFunction(new Twig_Function('urlFor', 'urlFor'));
$view->getEnvironment()->addFunction(new Twig_Function('mime2font', 'mime2font'));
$view->getEnvironment()->addFunction(new Twig_Function('queryParams', 'queryParams'));
$view->getEnvironment()->addFunction(new TwigFunction('route', 'route'));
$view->getEnvironment()->addFunction(new TwigFunction('lang', 'lang'));
$view->getEnvironment()->addFunction(new TwigFunction('urlFor', 'urlFor'));
$view->getEnvironment()->addFunction(new TwigFunction('mime2font', 'mime2font'));
$view->getEnvironment()->addFunction(new TwigFunction('queryParams', 'queryParams'));
return $view;
};
$container['phpErrorHandler'] = function ($container) {
return function (\Slim\Http\Request $request, \Slim\Http\Response $response, \Throwable $error) use (&$container) {
return function (Request $request, Response $response, Throwable $error) use (&$container) {
$container->logger->critical('Fatal runtime error during app execution', [$error, $error->getTraceAsString()]);
return $container->view->render($response->withStatus(500), 'errors/500.twig', ['exception' => $error]);
};
};
$container['errorHandler'] = function ($container) {
return function (\Slim\Http\Request $request, \Slim\Http\Response $response, \Exception $exception) use (&$container) {
return function (Request $request, Response $response, Exception $exception) use (&$container) {
if ($exception instanceof \App\Exceptions\MaintenanceException) {
if ($exception instanceof MaintenanceException) {
return $container->view->render($response->withStatus(503), 'errors/maintenance.twig');
}
if ($exception instanceof \App\Exceptions\UnauthorizedException) {
if ($exception instanceof UnauthorizedException) {
return $container->view->render($response->withStatus(403), 'errors/403.twig');
}
@ -117,13 +125,13 @@ $container['errorHandler'] = function ($container) {
};
$container['notAllowedHandler'] = function ($container) {
return function (\Slim\Http\Request $request, \Slim\Http\Response $response, $methods) use (&$container) {
return function (Request $request, Response $response, $methods) use (&$container) {
return $container->view->render($response->withStatus(405)->withHeader('Allow', implode(', ', $methods)), 'errors/405.twig');
};
};
$container['notFoundHandler'] = function ($container) {
return function (\Slim\Http\Request $request, \Slim\Http\Response $response) use (&$container) {
return function (Request $request, Response $response) use (&$container) {
$response->withStatus(404)->withHeader('Content-Type', 'text/html');
return $container->view->render($response, 'errors/404.twig');
};
@ -132,7 +140,7 @@ $container['notFoundHandler'] = function ($container) {
$app = new App($container);
// Permanently redirect paths with a trailing slash to their non-trailing counterpart
$app->add(function (\Slim\Http\Request $request, \Slim\Http\Response $response, callable $next) {
$app->add(function (Request $request, Response $response, callable $next) {
$uri = $request->getUri();
$path = $uri->getPath();
@ -150,4 +158,6 @@ $app->add(function (\Slim\Http\Request $request, \Slim\Http\Response $response,
});
// Load the application routes
require BASE_DIR . 'app/routes.php';
require BASE_DIR . 'app/routes.php';
return $app;

View file

@ -29,5 +29,8 @@
"blacklist": [],
"update_with_dependencies": 1
}
},
"require-dev": {
"phpstan/phpstan": "^0.11.5"
}
}

1047
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,9 +1,9 @@
<?php
(PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 1) ?: die('Sorry, PHP >=7.1 is required to run XBackBone.');
require __DIR__ . '/vendor/autoload.php';
define('BASE_DIR', __DIR__ . DIRECTORY_SEPARATOR);
define('PLATFORM_VERSION', json_decode(file_get_contents('composer.json'))->version);
require 'bootstrap/app.php';
$app = require_once __DIR__ . '/bootstrap/app.php';
$app->run();

View file

@ -1,12 +1,16 @@
<?php
(PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 1) ?: die('Sorry, PHP >=7.1 is required to run XBackBone.');
require __DIR__ . '/../vendor/autoload.php';
use App\Database\DB;
use App\Web\Session;
use Slim\App;
use Slim\Container;
use Slim\Http\Environment;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Http\Uri;
use Slim\Views\Twig;
define('PLATFORM_VERSION', json_decode(file_get_contents(__DIR__ . '/../composer.json'))->version);
@ -29,7 +33,7 @@ $container['session'] = function ($container) {
};
$container['view'] = function ($container) use (&$config) {
$view = new \Slim\Views\Twig([__DIR__ . '/templates', __DIR__ . '/../resources/templates'], [
$view = new Twig([__DIR__ . '/templates', __DIR__ . '/../resources/templates'], [
'cache' => false,
'autoescape' => 'html',
'debug' => $config['displayErrorDetails'],
@ -38,7 +42,7 @@ $container['view'] = function ($container) use (&$config) {
// Instantiate and add Slim specific extension
$router = $container->get('router');
$uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER));
$uri = Uri::createFromEnvironment(new Environment($_SERVER));
$view->addExtension(new Slim\Views\TwigExtension($router, $uri));
$view->getEnvironment()->addGlobal('config', $config);

831
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,22 +1,22 @@
{
"dependencies": {
"@fortawesome/fontawesome-free": "^5.7.2",
"@fortawesome/fontawesome-free": "^5.8.1",
"bootstrap": "^4.3.1",
"clipboard": "^2.0.4",
"highlightjs": "^9.12.0",
"jquery": "^3.3.1",
"popper.js": "^1.14.7",
"tooltip.js": "^1.3.1",
"video.js": "^7.4.2"
"jquery": "^3.4.1",
"popper.js": "^1.15.0",
"tooltip.js": "^1.3.2",
"video.js": "^7.5.4"
},
"devDependencies": {
"grunt": "^1.0",
"grunt": "^1.0.4",
"grunt-contrib-copy": "^1.0.0",
"grunt-contrib-cssmin": "^3.0.0",
"grunt-contrib-jshint": "^2.0.0",
"grunt-contrib-uglify": "^4.0.0",
"grunt-contrib-jshint": "^2.1.0",
"grunt-contrib-uglify": "^4.0.1",
"grunt-contrib-watch": "^1.1.0",
"grunt-zip": "^0.18.1",
"grunt-zip": "^0.18.2",
"load-grunt-tasks": "^4.0.0"
}
}