From: ArthurHoaro Date: Sat, 24 Oct 2020 09:37:29 +0000 (+0200) Subject: Merge pull request #1601 from ArthurHoaro/feature/psr3 X-Git-Tag: v0.12.1^2~28 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=820cae27cfcc94af552818f3f1e5342e00478f6c;hp=-c;p=github%2Fshaarli%2FShaarli.git Merge pull request #1601 from ArthurHoaro/feature/psr3 --- 820cae27cfcc94af552818f3f1e5342e00478f6c diff --combined application/Utils.php index 37be9a13,7a9d2645..bc1c9f5d --- a/application/Utils.php +++ b/application/Utils.php @@@ -4,21 -4,23 +4,23 @@@ */ /** - * Logs a message to a text file + * Format log using provided data. * - * The log format is compatible with fail2ban. + * @param string $message the message to log + * @param string|null $clientIp the client's remote IPv4/IPv6 address * - * @param string $logFile where to write the logs - * @param string $clientIp the client's remote IPv4/IPv6 address - * @param string $message the message to log + * @return string Formatted message to log */ - function logm($logFile, $clientIp, $message) + function format_log(string $message, string $clientIp = null): string { - file_put_contents( - $logFile, - date('Y/m/d H:i:s').' - '.$clientIp.' - '.strval($message).PHP_EOL, - FILE_APPEND - ); + $out = $message; + + if (!empty($clientIp)) { + // Note: we keep the first dash to avoid breaking fail2ban configs + $out = '- ' . $clientIp . ' - ' . $out; + } + + return $out; } /** @@@ -463,12 -465,3 +465,12 @@@ function t($text, $nText = '', $nb = 1 { return dn__($domain, $text, $nText, $nb); } + +/** + * Converts an exception into a printable stack trace string. + */ +function exception2text(Throwable $e): string +{ + return $e->getMessage() . PHP_EOL . $e->getFile() . $e->getLine() . PHP_EOL . $e->getTraceAsString(); +} + diff --combined index.php index b6ee8ebc,ea6e8501..1b10ee41 --- a/index.php +++ b/index.php @@@ -25,9 -25,12 +25,12 @@@ require_once 'application/Utils.php' require_once __DIR__ . '/init.php'; + use Katzgrau\KLogger\Logger; + use Psr\Log\LogLevel; use Shaarli\Config\ConfigManager; use Shaarli\Container\ContainerBuilder; use Shaarli\Languages; + use Shaarli\Security\BanManager; use Shaarli\Security\CookieManager; use Shaarli\Security\LoginManager; use Shaarli\Security\SessionManager; @@@ -48,10 -51,22 +51,22 @@@ if ($conf->get('dev.debug', false)) }); } + $logger = new Logger( + dirname($conf->get('resource.log')), + !$conf->get('dev.debug') ? LogLevel::INFO : LogLevel::DEBUG, + ['filename' => basename($conf->get('resource.log'))] + ); $sessionManager = new SessionManager($_SESSION, $conf, session_save_path()); $sessionManager->initialize(); $cookieManager = new CookieManager($_COOKIE); - $loginManager = new LoginManager($conf, $sessionManager, $cookieManager); + $banManager = new BanManager( + $conf->get('security.trusted_proxies', []), + $conf->get('security.ban_after'), + $conf->get('security.ban_duration'), + $conf->get('resource.ban_file', 'data/ipbans.php'), + $logger + ); + $loginManager = new LoginManager($conf, $sessionManager, $cookieManager, $banManager, $logger); $loginManager->generateStaySignedInToken($_SERVER['REMOTE_ADDR']); // Sniff browser language and set date format accordingly. @@@ -71,7 -86,7 +86,7 @@@ date_default_timezone_set($conf->get('g $loginManager->checkLoginState(client_ip_id($_SERVER)); - $containerBuilder = new ContainerBuilder($conf, $sessionManager, $cookieManager, $loginManager); + $containerBuilder = new ContainerBuilder($conf, $sessionManager, $cookieManager, $loginManager, $logger); $container = $containerBuilder->build(); $app = new App($container); @@@ -151,12 -166,6 +166,12 @@@ $app->group('/api/v1', function () $this->get('/history', '\Shaarli\Api\Controllers\HistoryController:getHistory')->setName('getHistory'); })->add('\Shaarli\Api\ApiMiddleware'); -$response = $app->run(true); - -$app->respond($response); +try { + $response = $app->run(true); + $app->respond($response); +} catch (Throwable $e) { + die(nl2br( + 'An unexpected error happened, and the error template could not be displayed.' . PHP_EOL . PHP_EOL . + exception2text($e) + )); +}