Translated using Weblate (Portuguese)
[phpmyadmin.git] / src / Logging.php
blobafcb3497dee9a104f1ae440ea9e20fd9f461a694
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin;
7 use function closelog;
8 use function date;
9 use function error_log;
10 use function function_exists;
11 use function openlog;
12 use function syslog;
14 use const LOG_AUTHPRIV;
15 use const LOG_NDELAY;
16 use const LOG_PID;
17 use const LOG_WARNING;
19 /**
20 * Logging functionality for webserver.
22 * This includes web server specific code to log some information.
24 class Logging
26 /**
27 * Get authentication logging destination
29 public static function getLogDestination(string $authLog): string
31 $logFile = $authLog;
33 /* Autodetect */
34 if ($logFile === 'auto') {
35 if (function_exists('syslog')) {
36 $logFile = 'syslog';
37 } elseif (function_exists('error_log')) {
38 $logFile = 'php';
39 } else {
40 $logFile = '';
44 return $logFile;
47 /**
48 * Generate log message for authentication logging
50 * @param string $user user name
51 * @param string $status status message
53 public static function getLogMessage(string $user, string $status): string
55 if ($status === 'ok') {
56 return 'user authenticated: ' . $user . ' from ' . Core::getIp();
59 return 'user denied: ' . $user . ' (' . $status . ') from ' . Core::getIp();
62 /**
63 * Logs user information to webserver logs.
65 * @param string $user user name
66 * @param string $status status message
68 public static function logUser(Config $config, string $user, string $status = 'ok'): void
70 if (function_exists('apache_note')) {
71 /** @psalm-suppress UnusedFunctionCall */
72 apache_note('userID', $user);
73 /** @psalm-suppress UnusedFunctionCall */
74 apache_note('userStatus', $status);
77 $settings = $config->getSettings();
79 /* Do not log successful authentications */
80 if (! $settings->authLogSuccess && $status === 'ok') {
81 return;
84 $logFile = self::getLogDestination($settings->authLog);
85 if ($logFile === '') {
86 return;
89 $message = self::getLogMessage($user, $status);
90 if ($logFile === 'syslog') {
91 if (function_exists('syslog')) {
92 @openlog('phpMyAdmin', LOG_NDELAY | LOG_PID, LOG_AUTHPRIV);
93 @syslog(LOG_WARNING, $message);
94 closelog();
96 } elseif ($logFile === 'php') {
97 @error_log($message);
98 } elseif ($logFile === 'sapi') {
99 @error_log($message, 4);
100 } else {
101 @error_log(
102 date('M d H:i:s') . ' phpmyadmin: ' . $message . "\n",
104 $logFile,