| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <?php
- class serverLog {
- public static function printLog(string $_log){
- if (serverLog::detectLogsAttempts($_log)) {
- self::printLogAttempts($_log);
- } elseif (serverLog::detectApacheLog($_log)) {
- self::printColoredApacheLog($_log );
- } elseif (stripos($_log, 'error') !== false) {
- echo '<div style="color: salmon;">' . $_log . '</div>';
- } elseif (stripos($_log, 'fatal') !== false) {
- echo '<div style="color: #FF5252;">' . $_log . '</div>';
- } elseif (stripos($_log, 'warn') !== false OR stripos($_log, 'alert') !== false) {
- echo '<div style="color: orange;">' . $_log . '</div>';
- } elseif (stripos($_log, 'notice') !== false) {
- echo '<div style="color: white;">' . $_log . '</div>';
- } else {
- echo '<div style="color: grey;">' . $_log . '</div>';
- }
- }
- public static function filtreLog(string $_log){
- $log = self::hidePassword($_log);
- $log = self::hideEmail($log);
- self::printLog($log);
- }
- private static function detectLogsAttempts(string $str): bool {
- return preg_match(
- '/^\d{1,3}(\.\d{1,3}){3},\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},/',
- $str
- ) === 1;
- }
- public static function printLogAttempts(string $_log){
- $parts = explode(',', $_log, 4);
- if (count($parts) === 4) {
- echo '<div class="notice">'
- . '<span style="color:#4FC3F7;">' . htmlspecialchars($parts[0]) . '</span>, '
- . '<span style="color:#81C784;">' . htmlspecialchars($parts[1]) . '</span>, '
- . '<span style="color:#FFD54F;">' . htmlspecialchars($parts[2]) . '</span>, '
- . '<span style="color:#FF8A65;">' . htmlspecialchars($parts[3]) . '</span>'
- . '</div>';
- return;
- }
- }
- private static function detectApacheLog(string $str): bool {
- return preg_match(
- '/^\[(?<date>[A-Za-z]{3} [A-Za-z]{3} \d{2} [\d:.]+ \d{4})\] \[(?<type>[^\]]+)\] \[pid (?<pid>\d+):tid (?<tid>\d+)\] \[client (?<client>[^\]]+)\] (?<msg>.*?)(?:, referer: (?<referer>.+))?$/',
- $str
- ) === 1;
- }
- public static function printColoredApacheLog(string $line) {
- $regex = '/^\[(?<date>[A-Za-z]{3} [A-Za-z]{3} \d{2} [\d:.]+ \d{4})\] \[(?<type>[^\]]+)\] \[pid (?<pid>\d+):tid (?<tid>\d+)\] \[client (?<client>[^\]]+)\] (?<msg>.*?)(?:, referer: (?<referer>.+))?$/';
- if (preg_match($regex, $line, $matches)) {
- echo '<div class="notice">'
- . '<span style="color:#4FC3F7;">[' . htmlspecialchars($matches['date']) . ']</span> '
- . '<span style="color:#FFD54F;">[' . htmlspecialchars($matches['type']) . ']</span> '
- . '<span style="color:#BDBDBD;">[pid ' . htmlspecialchars($matches['pid']) . ':tid ' . htmlspecialchars($matches['tid']) . ']</span> '
- . '<span style="color:#81C784;">[client ' . htmlspecialchars($matches['client']) . ']</span> '
- . '<span style="color:#FF8A65;">' . htmlspecialchars($matches['msg']) . '</span>';
- if (!empty($matches['referer'])) {
- echo ' <span style="color:#90CAF9;">referer: ' . htmlspecialchars($matches['referer']) . '</span>';
- }
- echo '</div>';
- } else {
- echo '<div style="color:grey;">' . htmlspecialchars($line) . '</div>';
- }
- }
- private static function ifFolderWww(string $_log){
- return (stripos($_log, DOCUMENT_ROOT) !== false) ? TRUE : FALSE;
- }
- private static function ifFolderDomain(string $_log){
- return (stripos($_log, DOMAIN_CMS) !== false) ? TRUE : FALSE;
- }
- private static function ifGeneral(string $_log){
- return (stripos($_log, "/var/www/") == FALSE AND stripos($_log, "https://") == FALSE) ? TRUE : FALSE;
- }
- private static function hidePassword($input) {
- return preg_replace("/('password'\s*=>\s*)'[^']*'/", "$1'##PASSWORD##'", $input);
- }
- private static function hideEmail($input) {
- return preg_replace("/('email'\s*=>\s*)'[^']*'/", "$1'##EMAIL##'", $input);
- }
- public static function consoleApache(){
- if (!is_readable(SERVER_LOGS_APACHE)) {
- echo "Le fichier n'est pas accessible en lecture.";
- exit();
- }
- $lines = file(SERVER_LOGS_APACHE);
- $search = strtolower($_GET['search'] ?? '');
- $limit = strtolower($_GET['limit'] ?? 50); // A défaut les 50 derniers logs
- if ($search) {
- $lines = array_filter($lines, fn($line) => stripos($line, $search) !== false);
- }
- $lines = array_reverse($lines);
- echo '<!DOCTYPE html>
- <html lang="fr">
- <head>
- <meta charset="UTF-8">
- <style>
- body { font-family: monospace; background: black !important; padding: 20px; }
- div { font-size: 0.8em; }
- pre { background: #fff; padding: 10px; border: 1px solid #ccc; overflow-x: auto; }
- </style>
- <link rel="stylesheet" href="' . cache::getFileWithTime("libs/bootstrap/assets/dist/css/bootstrap.min.css") . '">
- <script src="' . cache::getFileWithTime("libs/bootstrap/js/bootstrap.min.js") . '"></script>
- </head>
- <body>';
- foreach (array_slice($lines, 0, $limit) as $line) {
- serverLog::filtreLog($line);
- }
- echo '</body>
- </html>';
- }
- public static function consoleAttempts(){
- if (!is_readable(SERVER_LOGS_BLACKLIST . "ip_attempts.log")) {
- echo "Le fichier n'est pas accessible en lecture.";
- exit();
- }
- $lines = file(SERVER_LOGS_BLACKLIST . "ip_attempts.log");
- $search = strtolower($_GET['search'] ?? '');
- $limit = strtolower($_GET['limit'] ?? 50); // A défaut les 50 derniers logs
- if ($search) {
- $lines = array_filter($lines, fn($line) => stripos($line, $search) !== false);
- }
- $lines = array_reverse($lines);
- echo '<!DOCTYPE html>
- <html lang="fr">
- <head>
- <meta charset="UTF-8">
- <style>
- body { font-family: monospace; background: black !important; padding: 20px; }
- div { font-size: 0.8em; }
- pre { background: #fff; padding: 10px; border: 1px solid #ccc; overflow-x: auto; }
- </style>
- <link rel="stylesheet" href="' . cache::getFileWithTime("libs/bootstrap/assets/dist/css/bootstrap.min.css") . '">
- <script src="' . cache::getFileWithTime("libs/bootstrap/js/bootstrap.min.js") . '"></script>
- </head>
- <body>';
- foreach (array_slice($lines, 0, $limit) as $line) {
- serverLog::filtreLog($line);
- }
- echo '</body>
- </html>';
- }
- public static function consoleIpBlacklist(){
- if (!is_readable(SERVER_LOGS_BLACKLIST . "ip.txt")) {
- echo "Le fichier n'est pas accessible en lecture.";
- exit();
- }
- $lines = file(SERVER_LOGS_BLACKLIST . "ip.txt");
- $search = strtolower($_GET['search'] ?? '');
- $limit = strtolower($_GET['limit'] ?? 50); // A défaut les 50 derniers logs
- if ($search) {
- $lines = array_filter($lines, fn($line) => stripos($line, $search) !== false);
- }
- $lines = array_reverse($lines);
- echo '<!DOCTYPE html>
- <html lang="fr">
- <head>
- <meta charset="UTF-8">
- <style>
- body { font-family: monospace; background: black !important; padding: 20px; }
- div { font-size: 0.8em; }
- pre { background: #fff; padding: 10px; border: 1px solid #ccc; overflow-x: auto; }
- </style>
- <link rel="stylesheet" href="' . cache::getFileWithTime("libs/bootstrap/assets/dist/css/bootstrap.min.css") . '">
- <script src="' . cache::getFileWithTime("libs/bootstrap/js/bootstrap.min.js") . '"></script>
- </head>
- <body>';
- foreach (array_slice($lines, 0, $limit) as $line) {
- serverLog::filtreLog($line);
- }
- echo '</body>
- </html>';
- }
- }
|