-
Notifications
You must be signed in to change notification settings - Fork 0
/
BBKK_Logger.class.php
277 lines (241 loc) · 8.91 KB
/
BBKK_Logger.class.php
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
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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/*
Copyright (c) 2008-2014 - Andrea Ferroni [[email protected]]
This file is part of BBKK-dbdatalib.
BBKK-dbdatalib is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* This file contains BBKK_Logger class implementation and is part of
* BBKK-dbdatalib project... but can be used independently ;)
* The project can be followed and downloaded at
* https://github.com/bubbakk/BBKK-dbdatalib
*/
/*
* Class: BBKK_Logger
* This class implements simple logging setup and methods.
*
* Details:
* Actually the class only logs messages into a file.
*
* To set successfully the log filename, this class needs that a base-path is
* set.
* The <BBKK_Logger.__construct> method sets a default value to application
* base path (see <BBKK_Logger.get_application_base_path>. It is NOT POSSIBLE
* to set this value at will for security reasons.
* Generally speaking, the application directory is frequently a good
* candidate for logging purposes.
* If the log file located in the application directory is not writeable for
* any reason, the user home directory will be used (option valid only for
* CLI execution).
*
* In order to use this class, *call the constructor* or *set the (magic)
* <BBKK_Logger.$log_filename>* attribute . Please *remember* that if this
* class is inherited, the constructor is not automatically called (how
* much strange is PHP!).
*
* TODO:
* - add an extra parameter to configure the class behaviour on no writeable
* log file (return a FALSE or trigger an error)
*
* NOTES:
* it is suggested to turn following methods into functions:
* - <BBKK_Logger.get_application_base_path>
* - <BBKK_Logger.get_application_base_directory>
* - <BBKK_Logger.get_user_home_directory>
*/
class BBKK_Logger
{
/*
* Attribute: $base_path
*/
private $base_path = '';
/*
* Attribute: $log_filename
*/
private $log_filename = '';
/*
* Attribute: $abspath_log_filename
*/
private $abspath_log_filename = '';
/*
* Constant: DEFAULT_LOG_FILE_NAME
*/
const DEFAULT_LOG_FILE_NAME = 'error.log';
/*
* Method: __construct
* Set and check the log file to its default value or to passed parameter
*
* Parameters:
* $_log_filename {string} - desired log file name. Pass empty string or
* nothing to set the file name to default value
* <BBKK_Logger.DEFAULT_LOG_FILE_NAME>
* (default: '')
*
* See:
* <BBKK_Logger.set_log_filename>
*/
public function __construct($_log_filename = '')
{
if ( $_log_filename === '' )
$_log_filename = BBKK_Logger::DEFAULT_LOG_FILE_NAME;
$this->set_log_filename($_log_filename);
}
/*
* Method: __set
* magic setter method (not a real method)
*
* Details:
* parameters that can be set are:
* - *log_filename*: the log file name (is it enough clear?)
*
* Parameters:
* $attr_name {string} - one of class' known attribute name
* $value {mixed} - value to be set
*/
public function __set($attr_name, $value)
{
switch ( $attr_name )
{
case 'log_filename':
$this->set_log_filename($value);
break;
default:
trigger_error();
break;
}
}
/*
* Method: set_log_filename
* set the log file name and its absolute-path "version".
*
* Details:
* before setting the new full-path filename for the log file, a
* sanitization to passed file name is performed to ensure correct
* assignement (is always risky to let users/programmers set file names!)
*
* The base path (used to build <BBKK_Logger.$abspath_log_filename) is
* alse initialized if still not set.
*
* Parameters:
* $_log_filename {string} - the name to be set as log file (default = '')
*
* NOTE:
* if a good log file is set, only the file name can be changed, not
* the base path
*
* Returns:
* TRUE if everything's fine, FALSE if passed file name is not a suitable
* name for a file (see <BBKK_Logger.filename_check> sanitization method
* for further details) or if can't be set a writeable log file
*/
private function set_log_filename($_log_filename = '')
{
// check parameter passed
if ( !$this->filename_check($_log_filename) )
return false;
$this->log_filename = $_log_filename;
$logfile_writable = false; // used in the next do-while
if ( $this->base_path === '' ) $base_path_set = false;
else $base_path_set = true;
// used to launch base path functions
$base_paths_available = ['application', 'userhome'];
$base_paths_guesses = count($base_paths_available);
$path_guess_no = 0;
do
{
// set base path if not set
if ( !$base_path_set )
{
switch ($base_paths[$i])
{
case 'application':
$this->base_path = $this->get_application_base_directory();
$this->base_path .= '/'; // append trailing '/'
break;
case 'userhome':
$this->base_path = $this->get_user_home_directory();
$this->base_path .= '/'; // append trailing '/'
break;
}
}
// set filename attribute with absolute path
$this->abspath_log_filename = $this->base_path;
$this->abspath_log_filename .= $this->log_filename;
// check if log file set is writeable
$logfile_writable = is_writable($this->abspath_log_filename);
}
while ( !$logfile_writable && $path_guess_no < $base_paths_guesses )
// if all path-file couples are tested and no one is writeable...
if ( !$logfile_writable ) {
// ..reset attributes
$this->abspath_log_filename = '';
$this->base_path = '';
}
return $logfile_writable;
}
/*
* Method: filename_check
* check if passed parameter is a good string for a file name
*
* Details:
* parameter checks are made againts being a string, trailing
* white-spaces ("\t\n\r\0\x0B") and sanitization from special characters
* like tilde, colon, dot-dot-slash and so on (this is the
* regex: ([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})) ).
*
* Parameters:
* $filename {string} - string that will be checked as a valid file name
* (default: '')
*
* Returns:
* {bool} TRUE if the parameter passes each single check, FALSE if not
*/
private function filename_check($filename = '')
{
$whitespaces = " \t\n\r\0\x0B";
$filename_sanit_regex = "([^\w\s\d\-_~,;:\[\]\(\].]|[\.]{2,})";
if ( !is_string($ilename) ) return false;
// trim and sanitize
$filename_trimmed = trim($filename, $whitespaces);
$filename_sanitized = preg_replace($filename_sanit_regex,
'',
$filename_trimmed);
if ( $filename_sanitized !== $filename ) return false;
return true;
}
/*
* Method: get_application_base_path
*/
private function get_application_base_directory()
{
if ( PHP_SAPI == 'cli' ) $retval = $_SERVER["PWD"] );
else $retval = $_SERVER["DOCUMENT_ROOT"];
return $retval;
}
/*
* Method: set_user_home_directory
* get user's home directory
*
* Details:
* This method return user's home only if the PHP application/script is
* launched from CLI
*
* Returns:
* {string|bool} user's home directory or FALSE if the script is not a CLI
* application (eg: run in web server environment)
*/
private function get_user_home_directory()
{
if ( PHP_SAPI == 'cli' ) return $_SERVER["HOME"];
else return false;
}
}