2
0

serverLog.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 consoleLog(string $_logFile){
  82. if (!is_readable($_logFile)) {
  83. echo "Le fichier n'est pas accessible en lecture.";
  84. exit();
  85. }
  86. $lines = file($_logFile);
  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. foreach (array_slice($lines, 0, $limit) as $line) {
  94. serverLog::filtreLog($line);
  95. }
  96. }
  97. }