Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
require_once LIB_DIR . '/libbashcolor.inc.php';
/**
* Logging class
*
* @author croydon
*/
class ISPConfigLog {
const PRIO_DEBUG = 0;
const PRIO_INFO = 1;
const PRIO_WARN = 2;
const PRIO_ERROR = 3;
private static $priority = self::PRIO_INFO;
private static $logfile = 'ispconfig.log';
const PRIORITY_TEXT = array(
0 => 'DEBUG',
1 => 'INFO',
2 => 'WARN',
3 => 'ERROR'
);
public static function setLogPriority($priority) {
if(array_key_exists($priority, self::PRIORITY_TEXT)) {
self::$priority = $priority;
} else {
throw new ISPConfigLogException('Invalid logging priority: ' . $priority);
}
}
public static function setLogFile($filename) {
if(strpos($filename, '/') !== false || strpos($filename, '..') !== false) {
throw new ISPConfigLogException('Insecure log filename: ' . $filename);
}
if(!preg_match('/^[a-zA-Z0-9_\-\.]+$/', $filename)) {
throw new ISPConfigLogException('Invalid log filename: ' . $filename);
}
if(!$filename) {
self::$logfile = 'ispconfig.log';
} else {
self::$logfile = $filename;
}
return true;
}
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
public static function print($message) {
$lines = explode("\n", $message);
$colw = trim(exec('tput cols'));
if(!$colw) {
$colw = 80;
}
$line_prefix = '';
$match = array();
foreach($lines as $line) {
if(substr($line, 0, 4) === '{FW}') {
$rep = substr($line, 4, 1);
if($rep) {
print str_repeat($rep, $colw);
}
continue;
}
while(preg_match('/^(.*?)\t(.*)$/', $line, $match)) {
$sub = mb_strlen($match[1]) % 4;
$line = $match[1] . str_repeat(' ', 4 - $sub) . $match[2];
}
$line_len = mb_strlen($line);
if(substr($line, 0, 2) !== '->') {
$line_prefix = '';
} else {
$line = substr($line, 2);
}
if($line_prefix == '' && preg_match('/^(\s+-[a-z0-9A-Z\-]+\s+|\s+)->(\S.*?)$/', $line, $match)) {
$line_prefix = str_repeat(' ', mb_strlen($match[1]));
$line = $match[1] . $match[2];
} elseif($line_prefix == '' && $line_len > $colw) {
preg_match('/^(\s*)\S/', $line, $match);
if(isset($match[1])) {
$line_prefix = $match[1];
}
} else {
if(mb_strlen($line_prefix) >= $colw) {
$line_prefix = ' ';
}
$line = $line_prefix . $line;
}
if(mb_strlen($line_prefix) >= $colw) {
$line_prefix = ' ';
}
while(mb_strlen($line) > $colw) {
$tmp = explode("{###}", wordwrap($line, $colw, "{###}", true), 2);
$tmp = array_shift($tmp);
print $tmp . "\n";
$line = $line_prefix . trim(mb_substr($line, mb_strlen($tmp)));
unset($tmp);
if(trim($line) === '') {
break;
}
}
print $line ."\n";
}
}
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* @param string $message
* @param int $priority
* @param boolean $output
* @throws ISPConfigLogException
*/
private static function log($message, $priority = ISPConfigLog::PRIO_INFO, $output = false) {
if(self::$priority > $priority) {
return;
}
if(!@is_dir(LOG_DIR)) {
if(!@mkdir(LOG_DIR, 0777, true)) {
throw new ISPConfigLogException('Log path ' . LOG_DIR . ' could not be created.');
}
}
if(!isset(self::PRIORITY_TEXT[$priority])) {
throw new ISPConfigLogException('Invalid logging priority ' . $priority . ' provided.');
}
$log_file = LOG_DIR . '/' . self::$logfile;
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
array_shift($caller);
if(count($caller) > 0) {
$caller = array_shift($caller);
}
if(substr($caller['file'], 0, strlen(APP_DIR)) === APP_DIR) {
$caller['file'] = substr($caller['file'], strlen(APP_DIR));
}
$fp = @fopen($log_file, 'a');
if(!$fp) {
throw new ISPConfigLogException('Could not open logfile ' . $log_file . '.');
}
if(!@fwrite($fp, strftime("%d.%m.%Y-%H:%M:%S", time()) . ' - ' . $caller['file'] . ':' . $caller['line'] . ': [' . self::PRIORITY_TEXT[$priority] . '] ' . $message . "\n")) {
throw new ISPConfigLogException('Could not write to logfile ' . $log_file . '.');
}
fclose($fp);
if($output === true) {
$pre = '[' . self::PRIORITY_TEXT[$priority] . ']';
if($priority === self::PRIO_ERROR) {
$pre = '<red>' . $pre . '</red>';
} elseif($priority === self::PRIO_WARN) {
$pre = '<lightred>' . $pre . '</lightred>';
}
$add_message = '';
if($priority >= self::PRIO_WARN) {
$add_message .= ' <em>(' . $caller['file'] . ':' . $caller['line'] . ')</em>';
}
print PXBashColor::getString($pre . ' ' . $message . $add_message . "\n");
}
}
/**
* @param string $message
* @param boolean $output
*/
public static function debug($message, $output = false) {
self::log($message, ISPConfigLog::PRIO_DEBUG, $output);
}
/**
* @param string $message
* @param boolean $output
*/
public static function info($message, $output = false) {
self::log($message, ISPConfigLog::PRIO_INFO, $output);
}
/**
* @param string $message
* @param boolean $output
*/
public static function warn($message, $output = false) {
self::log($message, ISPConfigLog::PRIO_WARN, $output);
}
/**
* @param string $message
* @param boolean $output
*/
public static function error($message, $output = false) {
self::log($message, ISPConfigLog::PRIO_ERROR, $output);
}
}