]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Merge pull request #1601 from ArthurHoaro/feature/psr3
authorArthurHoaro <arthur@hoa.ro>
Sat, 24 Oct 2020 09:37:29 +0000 (11:37 +0200)
committerGitHub <noreply@github.com>
Sat, 24 Oct 2020 09:37:29 +0000 (11:37 +0200)
1  2 
application/Utils.php
index.php

diff --combined application/Utils.php
index 37be9a13fdf89df0e9eeadc03c4b8c0a10f7c043,7a9d264556213e09bd173508ecf6c63c3cc41553..bc1c9f5d6133b67eacc321b3d41da57f60b0f38d
@@@ -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 b6ee8ebc482b585e01bf858bc97ae90184b172c1,ea6e8501f1e39f4f613765433e3ea561fdf5d69e..1b10ee41c8299f196b510bde155d6c09e8bd045b
+++ 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)
 +    ));
 +}