Logging.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Logging functionality for webserver.
  5. *
  6. * This includes web server specific code to log some information.
  7. *
  8. * @package PhpMyAdmin
  9. */
  10. namespace PhpMyAdmin;
  11. use PhpMyAdmin\Core;
  12. /**
  13. * Misc logging functions
  14. *
  15. * @package PhpMyAdmin
  16. */
  17. class Logging
  18. {
  19. /**
  20. * Get authentication logging destination
  21. *
  22. * @return string
  23. */
  24. public static function getLogDestination()
  25. {
  26. $log_file = $GLOBALS['PMA_Config']->get('AuthLog');
  27. /* Autodetect */
  28. if ($log_file == 'auto') {
  29. if (function_exists('syslog')) {
  30. $log_file = 'syslog';
  31. } elseif (function_exists('error_log')) {
  32. $log_file = 'php';
  33. } else {
  34. $log_file = '';
  35. }
  36. }
  37. return $log_file;
  38. }
  39. /**
  40. * Generate log message for authentication logging
  41. *
  42. * @param string $user user name
  43. * @param string $status status message
  44. *
  45. * @return void
  46. */
  47. public static function getLogMessage($user, $status)
  48. {
  49. if ($status == 'ok') {
  50. return 'user authenticated: ' . $user . ' from ' . Core::getIp();
  51. }
  52. return 'user denied: ' . $user . ' (' . $status . ') from ' . Core::getIp();
  53. }
  54. /**
  55. * Logs user information to webserver logs.
  56. *
  57. * @param string $user user name
  58. * @param string $status status message
  59. *
  60. * @return void
  61. */
  62. public static function logUser($user, $status = 'ok')
  63. {
  64. if (function_exists('apache_note')) {
  65. apache_note('userID', $user);
  66. apache_note('userStatus', $status);
  67. }
  68. /* Do not log successful authentications */
  69. if (! $GLOBALS['PMA_Config']->get('AuthLogSuccess') && $status == 'ok') {
  70. return;
  71. }
  72. $log_file = self::getLogDestination();
  73. if (empty($log_file)) {
  74. return;
  75. }
  76. $message = self::getLogMessage($user, $status);
  77. if ($log_file == 'syslog') {
  78. if (function_exists('syslog')) {
  79. @openlog('phpMyAdmin', LOG_NDELAY | LOG_PID, LOG_AUTHPRIV);
  80. @syslog(LOG_WARNING, $message);
  81. closelog();
  82. }
  83. } elseif ($log_file == 'php') {
  84. @error_log($message);
  85. } elseif ($log_file == 'sapi') {
  86. @error_log($message, 4);
  87. } else {
  88. @error_log(
  89. date('M d H:i:s') . ' phpmyadmin: ' . $message . "\n",
  90. 3, $log_file
  91. );
  92. }
  93. }
  94. }