serverLog.class.php 7.6 KB

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