serverLog.class.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. class serverLog {
  3. public static function printLog(string $_log){
  4. if (serverLog::detectLogsAttempts($_log)) {
  5. self::printLogAttempts($_log);
  6. } elseif (serverLog::detectApacheLog($_log)) {
  7. self::printColoredApacheLog($_log );
  8. } elseif (stripos($_log, 'error') !== false) {
  9. echo '<div style="color: salmon;">' . $_log . '</div>';
  10. } elseif (stripos($_log, 'fatal') !== false) {
  11. echo '<div style="color: #FF5252;">' . $_log . '</div>';
  12. } elseif (stripos($_log, 'warn') !== false OR stripos($_log, 'alert') !== false) {
  13. echo '<div style="color: orange;">' . $_log . '</div>';
  14. } elseif (stripos($_log, 'notice') !== false) {
  15. echo '<div style="color: white;">' . $_log . '</div>';
  16. } else {
  17. echo '<div style="color: grey;">' . $_log . '</div>';
  18. }
  19. }
  20. public static function filtreLog(string $_log){
  21. $log = self::hidePassword($_log);
  22. $log = self::hideEmail($log);
  23. self::printLog($log);
  24. }
  25. private static function detectLogsAttempts(string $str): bool {
  26. return preg_match(
  27. '/^\d{1,3}(\.\d{1,3}){3},\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},/',
  28. $str
  29. ) === 1;
  30. }
  31. public static function printLogAttempts(string $_log){
  32. $parts = explode(',', $_log, 4);
  33. if (count($parts) === 4) {
  34. echo '<div class="notice">'
  35. . '<span style="color:#4FC3F7;">' . htmlspecialchars($parts[0]) . '</span>, '
  36. . '<span style="color:#81C784;">' . htmlspecialchars($parts[1]) . '</span>, '
  37. . '<span style="color:#FFD54F;">' . htmlspecialchars($parts[2]) . '</span>, '
  38. . '<span style="color:#FF8A65;">' . htmlspecialchars($parts[3]) . '</span>'
  39. . '</div>';
  40. return;
  41. }
  42. }
  43. private static function detectApacheLog(string $str): bool {
  44. return preg_match(
  45. '/^\[(?<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>.+))?$/',
  46. $str
  47. ) === 1;
  48. }
  49. public static function printColoredApacheLog(string $line) {
  50. $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>.+))?$/';
  51. if (preg_match($regex, $line, $matches)) {
  52. echo '<div class="notice">'
  53. . '<span style="color:#4FC3F7;">[' . htmlspecialchars($matches['date']) . ']</span> '
  54. . '<span style="color:#FFD54F;">[' . htmlspecialchars($matches['type']) . ']</span> '
  55. . '<span style="color:#BDBDBD;">[pid ' . htmlspecialchars($matches['pid']) . ':tid ' . htmlspecialchars($matches['tid']) . ']</span> '
  56. . '<span style="color:#81C784;">[client ' . htmlspecialchars($matches['client']) . ']</span> '
  57. . '<span style="color:#FF8A65;">' . htmlspecialchars($matches['msg']) . '</span>';
  58. if (!empty($matches['referer'])) {
  59. echo ' <span style="color:#90CAF9;">referer: ' . htmlspecialchars($matches['referer']) . '</span>';
  60. }
  61. echo '</div>';
  62. } else {
  63. echo '<div style="color:grey;">' . htmlspecialchars($line) . '</div>';
  64. }
  65. }
  66. private static function ifFolderWww(string $_log){
  67. return (stripos($_log, DOCUMENT_ROOT) !== false) ? TRUE : FALSE;
  68. }
  69. private static function ifFolderDomain(string $_log){
  70. return (stripos($_log, DOMAIN_CMS) !== false) ? TRUE : FALSE;
  71. }
  72. private static function ifGeneral(string $_log){
  73. return (stripos($_log, "/var/www/") == FALSE AND stripos($_log, "https://") == FALSE) ? TRUE : FALSE;
  74. }
  75. private static function hidePassword($input) {
  76. return preg_replace("/('password'\s*=>\s*)'[^']*'/", "$1'##PASSWORD##'", $input);
  77. }
  78. private static function hideEmail($input) {
  79. return preg_replace("/('email'\s*=>\s*)'[^']*'/", "$1'##EMAIL##'", $input);
  80. }
  81. public static function consoleApache(){
  82. if (!is_readable(SERVER_LOGS_APACHE)) {
  83. echo "Le fichier n'est pas accessible en lecture.";
  84. exit();
  85. }
  86. $lines = file(SERVER_LOGS_APACHE);
  87. $search = strtolower($_GET['search'] ?? '');
  88. $limit = strtolower($_GET['limit'] ?? 50); // A défaut les 50 derniers logs
  89. if ($search) {
  90. $lines = array_filter($lines, fn($line) => stripos($line, $search) !== false);
  91. }
  92. $lines = array_reverse($lines);
  93. echo '<!DOCTYPE html>
  94. <html lang="fr">
  95. <head>
  96. <meta charset="UTF-8">
  97. <style>
  98. body { font-family: monospace; background: black !important; padding: 20px; }
  99. div { font-size: 0.8em; }
  100. pre { background: #fff; padding: 10px; border: 1px solid #ccc; overflow-x: auto; }
  101. </style>
  102. <link rel="stylesheet" href="' . cache::printFileWithTime("libs/bootstrap/assets/dist/css/bootstrap.min.css") . '">
  103. <script src="' . cache::printFileWithTime("libs/bootstrap/js/bootstrap.min.js") . '"></script>
  104. </head>
  105. <body>';
  106. foreach (array_slice($lines, 0, $limit) as $line) {
  107. serverLog::filtreLog($line);
  108. }
  109. echo '</body>
  110. </html>';
  111. }
  112. public static function consoleAttempts(){ echo SERVER_LOGS_BLACKLIST . "/ip_attempts.log";
  113. if (!is_readable(SERVER_LOGS_BLACKLIST . "/ip_attempts.log")) {
  114. echo "Le fichier n'est pas accessible en lecture.";
  115. exit();
  116. }
  117. $lines = file(SERVER_LOGS_BLACKLIST . "/ip_attempts.log");
  118. $search = strtolower($_GET['search'] ?? '');
  119. $limit = strtolower($_GET['limit'] ?? 50); // A défaut les 50 derniers logs
  120. if ($search) {
  121. $lines = array_filter($lines, fn($line) => stripos($line, $search) !== false);
  122. }
  123. $lines = array_reverse($lines);
  124. echo '<!DOCTYPE html>
  125. <html lang="fr">
  126. <head>
  127. <meta charset="UTF-8">
  128. <style>
  129. body { font-family: monospace; background: black !important; padding: 20px; }
  130. div { font-size: 0.8em; }
  131. pre { background: #fff; padding: 10px; border: 1px solid #ccc; overflow-x: auto; }
  132. </style>
  133. <link rel="stylesheet" href="' . cache::printFileWithTime("libs/bootstrap/assets/dist/css/bootstrap.min.css") . '">
  134. <script src="' . cache::printFileWithTime("libs/bootstrap/js/bootstrap.min.js") . '"></script>
  135. </head>
  136. <body>';
  137. foreach (array_slice($lines, 0, $limit) as $line) {
  138. serverLog::filtreLog($line);
  139. }
  140. echo '</body>
  141. </html>';
  142. }
  143. public static function consoleIpBlacklist(){
  144. if (!is_readable(SERVER_LOGS_BLACKLIST . "/ip.txt")) {
  145. echo "Le fichier n'est pas accessible en lecture.";
  146. exit();
  147. }
  148. $lines = file(SERVER_LOGS_BLACKLIST . "/ip.txt");
  149. $search = strtolower($_GET['search'] ?? '');
  150. $limit = strtolower($_GET['limit'] ?? 50); // A défaut les 50 derniers logs
  151. if ($search) {
  152. $lines = array_filter($lines, fn($line) => stripos($line, $search) !== false);
  153. }
  154. $lines = array_reverse($lines);
  155. echo '<!DOCTYPE html>
  156. <html lang="fr">
  157. <head>
  158. <meta charset="UTF-8">
  159. <style>
  160. body { font-family: monospace; background: black !important; padding: 20px; }
  161. div { font-size: 0.8em; }
  162. pre { background: #fff; padding: 10px; border: 1px solid #ccc; overflow-x: auto; }
  163. </style>
  164. <link rel="stylesheet" href="' . cache::printFileWithTime("libs/bootstrap/assets/dist/css/bootstrap.min.css") . '">
  165. <script src="' . cache::printFileWithTime("libs/bootstrap/js/bootstrap.min.js") . '"></script>
  166. </head>
  167. <body>';
  168. foreach (array_slice($lines, 0, $limit) as $line) {
  169. serverLog::filtreLog($line);
  170. }
  171. echo '</body>
  172. </html>';
  173. }
  174. }