Issues (141)

src/Service/Loggit.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace CILogon\Service;
4
5
use CILogon\Service\Util;
6
use Log;
7
8
/**
9
 * Loggit
10
 */
11
class Loggit
12
{
13
    /**
14
     * @var Log $logger The Log object to write log info with.
15
     */
16
    protected $logger;
17
18
    /**
19
     * __construct
20
     *
21
     * Default constructor. This functions creates a new internal
22
     * $logger object to be utilized to write out log messages to the
23
     * intended destination (the first parameter) using the PHP Pear Log
24
     * module. You can log to the console (i.e., STDOUT), the system
25
     * syslog, or a file. The default is DEFAULT_LOGTYPE (defined in the
26
     * top-level config.php file) If logtype is 'file', the second
27
     * parameter is the file name. If running in a browser session,
28
     * the SERVER_NAME and REQUEST_URI are includedin all log events.
29
     *
30
     * Example usage:
31
     *     // Log info message to syslog
32
     *     $sysloggit = new Loggit('syslog');
33
     *     $sysloggit->info('This is an info message.');
34
     *
35
     * @param string $logtype (Optional) The log type, can be one of
36
     *        'console', 'syslog', or 'file'.
37
     * @param string $name (Optional) The name of the log file.
38
     */
39
    public function __construct($logtype = DEFAULT_LOGTYPE, $logname = DEFAULT_LOGNAME)
0 ignored issues
show
The constant CILogon\Service\DEFAULT_LOGTYPE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
The constant CILogon\Service\DEFAULT_LOGNAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
40
    {
41
        $ident = Util::getServerVar('SERVER_NAME') .
42
                 Util::getServerVar('REQUEST_URI');
43
44
        if (($logtype == 'syslog') && (strlen($logname) == 0)) {
45
            $logname = 'LOG_SYSLOG';
46
        }
47
        $this->logger = Log::singleton($logtype, $logname, $ident);
48
    }
49
50
    /**
51
     * info
52
     *
53
     * This function writes a message to a "log" using the PHP Pear Log
54
     * module. Several server variables and cookies (if they are set)
55
     * are automatically appended to the message to be logged.  These
56
     * are found in the $envs and $cookies array in the code below.
57
     * Also, all PHP session variables are logged.
58
     *
59
     * @param string $message The message string to be logged.
60
     * @param bool $missing (Optional) If true, print some missing user
61
     *        session variables. Defaults to false.
62
     * @param int $level (Optional) The PHP Pear-Log level for the message.
63
     *        Defaults to PEAR_LOG_INFO.
64
     */
65
    public function info($message, $missing = false, $level = PEAR_LOG_INFO)
66
    {
67
        // Don't log messages from monit/nagios hosts, the
68
        // same ones configured in /etc/httpd/conf.httpd.conf
69
        $dontlog = array('141.142.148.8',   // nagios-sec
70
                         '141.142.148.108', // nagios2-sec
71
                         '141.142.234.38',  // falco
72
                         '192.249.7.62'     // fozzie
73
                        );
74
        if (
75
            (isset($_SERVER['REMOTE_ADDR'])) &&
76
            (in_array($_SERVER['REMOTE_ADDR'], $dontlog))
77
        ) {
78
            return;
79
        }
80
81
        // Always print out certain HTTP headers, if available
82
        $envs    = array('REMOTE_ADDR',
83
                         'REMOTE_USER',
84
                         'HTTP_SHIB_IDENTITY_PROVIDER',
85
                         'HTTP_SHIB_SESSION_ID'
86
                        );
87
        // Always print out certain cookies, if available
88
        $cookies = array('providerId',
89
                        );
90
91
        $envstr = ' ';
92
        foreach ($envs as $value) {
93
            if ((isset($_SERVER[$value])) && (strlen($_SERVER[$value]) > 0)) {
94
                $envstr .= $value . '="' . $_SERVER[$value] . '" ';
95
            }
96
        }
97
98
        foreach ($cookies as $value) {
99
            if ((isset($_COOKIE[$value])) && (strlen($_COOKIE[$value]) > 0)) {
100
                $envstr .= $value . '="' . $_COOKIE[$value] . '" ';
101
            }
102
        }
103
104
        /* NEED TO CHANGE THIS WHEN USING HTTP_Session2 */
105
        if (session_id() != '') {
106
            foreach ($_SESSION as $key => $value) {
107
                $envstr .= $key . '="' .
108
                    (is_array($value) ? 'Array' : $value) . '" ';
109
            }
110
        }
111
112
        if ($missing) { // Output any important missing user session vars
113
            foreach (DBService::$user_attrs as $value) {
114
                if (!isset($_SESSION[$value])) {
115
                    $envstr .= $value . '="MISSING" ';
116
                }
117
            }
118
        }
119
120
        $this->logger->log($message . ' ' . $envstr, $level);
121
    }
122
123
    /**
124
     * warn
125
     *
126
     * This function writes a warning message message to the log.
127
     *
128
     */
129
    public function warn($message, $missing = false)
130
    {
131
        $this->info($message, $missing, PEAR_LOG_WARNING);
132
    }
133
134
    /**
135
     * Function  : error
136
     *
137
     * This function writes an error message message to the log.
138
     *
139
     * @param string $message The message string to be logged.
140
     * @param bool $missing (Optional) If true, print some missing user
141
     *        session variables. Defaults to false.
142
     */
143
    public function error($message, $missing = false)
144
    {
145
        $this->info($message, $missing, PEAR_LOG_ERR);
146
    }
147
148
    /**
149
     * alert
150
     *
151
     * This function writes an alert message message to the log.
152
     *
153
     * @param string $message The message string to be logged.
154
     * @param bool $missing (Optional) If true, print some missing user
155
     *        session variables. Defaults to false.
156
     */
157
    public function alert($message, $missing = false)
158
    {
159
        $this->info($message, $missing, PEAR_LOG_ALERT);
160
    }
161
}
162