Nico Huber | ee52fbc | 2023-06-24 11:52:57 +0000 | [diff] [blame] | 1 | <?php |
| 2 | /* |
| 3 | * SimpleID |
| 4 | * |
| 5 | * Copyright (C) Kelvin Mo 2007-9 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public |
| 18 | * License along with this program; if not, write to the Free |
| 19 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | * |
| 21 | * $Id$ |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * Functions for logging. |
| 26 | * |
| 27 | * @package simpleid |
| 28 | * @filesource |
| 29 | */ |
| 30 | |
| 31 | /** Log level */ |
| 32 | define('SIMPLEID_LOG_DEBUG', 5); |
| 33 | /** Log level */ |
| 34 | define('SIMPLEID_LOG_INFO', 4); |
| 35 | /** Log level */ |
| 36 | define('SIMPLEID_LOG_NOTICE', 3); |
| 37 | /** Log level */ |
| 38 | define('SIMPLEID_LOG_WARN', 2); |
| 39 | /** Log level */ |
| 40 | define('SIMPLEID_LOG_ERROR', 1); |
| 41 | /** Log level */ |
| 42 | define('SIMPLEID_LOG_FATAL', 0); |
| 43 | |
| 44 | /** |
| 45 | * This variable holds the pointer to the currently opened log file. If the |
| 46 | * log file is not open, this variable is NULL. |
| 47 | * |
| 48 | * @global resource $log |
| 49 | */ |
| 50 | $log = NULL; |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Opens the log file. |
| 55 | * |
| 56 | * This function opens a pointed to the log file for later usage. |
| 57 | * |
| 58 | * @return bool true if the log file is opened successfully. |
| 59 | */ |
| 60 | function log_open() { |
| 61 | global $log; |
| 62 | if (!defined('SIMPLEID_LOGFILE') || (SIMPLEID_LOGFILE == '')) return; |
| 63 | $log = fopen(SIMPLEID_LOGFILE, 'a'); |
| 64 | |
| 65 | if ($log === false) { |
| 66 | $log = NULL; |
| 67 | return false; |
| 68 | } else { |
| 69 | return true; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Closes the log file, if it is open. |
| 75 | */ |
| 76 | function log_close() { |
| 77 | if ($log != NULL) { |
| 78 | fflush($log); |
| 79 | fclose($log); |
| 80 | $log = NULL; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Logs a DEBUG message. |
| 86 | * |
| 87 | * @param string $message the message to log |
| 88 | * @see _log_write() |
| 89 | */ |
| 90 | function log_debug($message) { |
| 91 | _log_write($message, SIMPLEID_LOG_DEBUG); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Logs an INFO message. |
| 96 | * |
| 97 | * @param string $message the message to log |
| 98 | * @see _log_write() |
| 99 | */ |
| 100 | function log_info($message) { |
| 101 | _log_write($message, SIMPLEID_LOG_INFO); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Logs a NOTICE message. |
| 106 | * |
| 107 | * @param string $message the message to log |
| 108 | * @see _log_write() |
| 109 | */ |
| 110 | function log_notice($message) { |
| 111 | _log_write($message, SIMPLEID_LOG_NOTICE); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Logs a WARN message. |
| 116 | * |
| 117 | * @param string $message the message to log |
| 118 | * @see _log_write() |
| 119 | */ |
| 120 | function log_warn($message) { |
| 121 | _log_write($message, SIMPLEID_LOG_WARN); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Logs a ERROR message. |
| 126 | * |
| 127 | * @param string $message the message to log |
| 128 | * @see _log_write() |
| 129 | */ |
| 130 | function log_error($message) { |
| 131 | _log_write($message, SIMPLEID_LOG_ERROR); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Logs a FATAL message. |
| 136 | * |
| 137 | * @param string $message the message to log |
| 138 | * @see _log_write() |
| 139 | */ |
| 140 | function log_fatal($message) { |
| 141 | _log_write($message, SIMPLEID_LOG_FATAL); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Converts an array into a string for logging purposes. |
| 146 | * |
| 147 | * @param array $array the array the convert |
| 148 | * @param array $keys an array of keys to include in the converted string. Set |
| 149 | * to false if all the keys in the array should be included |
| 150 | * @return string the converted string. |
| 151 | */ |
| 152 | function log_array($array, $keys = false) { |
| 153 | $output = array(); |
| 154 | |
| 155 | if ($keys == false) $keys = array_keys($array); |
| 156 | |
| 157 | foreach ($keys as $key) { |
| 158 | $output[] = $key . ": " . $array[$key]; |
| 159 | } |
| 160 | |
| 161 | return implode('; ', $output); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Logs a message |
| 166 | * |
| 167 | * @param string $message the message to log |
| 168 | * @param int $level the log level |
| 169 | * @return bool true if the log has been written successfully |
| 170 | */ |
| 171 | function _log_write($message, $level = false) { |
| 172 | global $log; |
| 173 | static $levels; |
| 174 | |
| 175 | if (!$levels) { |
| 176 | $levels = array( |
| 177 | SIMPLEID_LOG_DEBUG => 'DEBUG', |
| 178 | SIMPLEID_LOG_INFO => 'INFO', |
| 179 | SIMPLEID_LOG_NOTICE => 'NOTICE', |
| 180 | SIMPLEID_LOG_WARN => 'WARN', |
| 181 | SIMPLEID_LOG_ERROR => 'ERROR', |
| 182 | SIMPLEID_LOG_FATAL => 'FATAL' |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | /* If a priority hasn't been specified, use the default value. */ |
| 187 | if ($level === false) { |
| 188 | $level = SIMPLEID_LOG_INFO; |
| 189 | } |
| 190 | |
| 191 | /* Abort early if the priority is above the maximum logging level. */ |
| 192 | if ($level > SIMPLEID_LOGLEVEL) { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | /* If the log file isn't already open, open it now. */ |
| 197 | if (($log == NULL) && !log_open()) { |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | /* Build the string containing the complete log line. */ |
| 202 | $line = sprintf('%1$s %2$s [%3$s] %4$s', strftime(SIMPLEID_DATE_TIME_FORMAT), session_id(), $levels[$level], $message) . "\n"; |
| 203 | |
| 204 | /* Write the log line to the log file. */ |
| 205 | $success = (fwrite($log, $line) !== false); |
| 206 | |
| 207 | return $success; |
| 208 | } |
| 209 | |
| 210 | ?> |