Logging.php
8.94 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/**
* Class Logging
*
* @package ContentControl\Vendor\TrustedLogin\Client
*
* @copyright 2021 Katz Web Services, Inc.
*
* @license GPL-2.0-or-later
* Modified by code-atlantic on 21-June-2024 using {@see https://github.com/BrianHenryIE/strauss}.
*/
namespace ContentControl\Vendor\TrustedLogin;
class Logging {
/**
* Path to logging directory (inside the WP Uploads base dir)
*/
const DIRECTORY_PATH = 'trustedlogin-logs/';
/**
* @var string Namespace for the vendor
*/
private $ns;
/**
* @var Config
*/
private $config = null;
/**
* @var bool $logging_enabled
*/
private $logging_enabled = false;
/**
* @var Logger|null|false Null: not instantiated; False: failed to instantiate.
*/
private $klogger = null;
/**
* Logger constructor.
*/
public function __construct( Config $config ) {
$this->config = $config;
$this->ns = $config->ns();
$this->logging_enabled = $config->get_setting( 'logging/enabled', false );
}
/**
* Attempts to initialize KLogger logging
*
* @param Config $config
*
* @return false|Logger
*/
private function setup_klogger( $config ) {
$logging_directory = null;
$configured_logging_dir = $config->get_setting( 'logging/directory', '' );
if( ! empty( $configured_logging_dir ) ) {
$logging_directory = $this->check_directory( $configured_logging_dir );
if ( ! $logging_directory ) {
return false;
}
}
if ( ! $logging_directory ) {
$logging_directory = $this->maybe_make_logging_directory();
}
// Directory cannot be found or created. Cannot log.
if( ! $logging_directory ) {
return false;
}
// Directory cannot be written to
if( ! $this->check_directory( $logging_directory ) ) {
return false;
}
try {
$DateTime = new \DateTime( '@' . time() );
// Filename hash changes every day, make it harder to guess
$filename_hash_data = $this->ns . home_url( '/' ) . $DateTime->format( 'z' );
$default_options = array(
'extension' => 'log',
'dateFormat' => 'Y-m-d G:i:s.u',
'filename' => sprintf( 'client-%s-%s-%s', $this->ns, $DateTime->format( 'Y-m-d' ), \hash( 'sha256', $filename_hash_data ) ),
'flushFrequency' => false,
'logFormat' => false,
'appendContext' => true,
);
$settings_options = $config->get_setting( 'logging/options', $default_options );
$options = wp_parse_args( $settings_options, $default_options );
$klogger = new Logger (
$logging_directory,
$config->get_setting( 'logging/threshold', 'notice' ),
$options
);
} catch ( \RuntimeException $exception ) {
$this->log( 'Could not initialize KLogger: ' . $exception->getMessage(), __METHOD__, 'error' );
return false;
} catch ( \Exception $exception ) {
$this->log( 'DateTime could not be created: ' . $exception->getMessage(), __METHOD__, 'error' );
return false;
}
return $klogger;
}
/**
* Returns the full path to the log file
*
* @since 1.5.0
*
* @return null|string Path to log file, if exists; null if not instantiated.
*/
public function get_log_file_path() {
if ( ! $this->klogger instanceof Logger ) {
return null;
}
return $this->klogger->getLogFilePath();
}
/**
* Checks whether a path exists and is writable
*
* @param string $dirpath Path to directory
*
* @return bool|string If exists and writable, returns original string. Otherwise, returns false.
*/
private function check_directory( $dirpath ) {
$dirpath = (string) $dirpath;
$file_exists = file_exists( $dirpath );
$is_writable = wp_is_writable( $dirpath );
// If the configured setting path exists and is writeable, use it.
if( $file_exists && $is_writable ) {
return $dirpath;
}
// Otherwise, try and log default errors
if( ! $file_exists ) {
$this->log( 'The defined logging directory does not exist: ' . $dirpath, __METHOD__, 'error' );
}
if( ! $is_writable ) {
$this->log( 'The defined logging directory exists but could not be written to: ' . $dirpath, __METHOD__, 'error' );
}
return false;
}
/**
* Returns the directory to use for logging if not defined by Config. Creates one if it doesn't exist.
*
* Note: Created directories are protected by an index.html file to prevent browsing.
*
* @return false|string Directory path, if exists; False if failure.
*/
private function maybe_make_logging_directory() {
$upload_dir = wp_upload_dir();
$log_dir = trailingslashit( $upload_dir['basedir'] ) . self::DIRECTORY_PATH;
// Directory exists; return early
if( file_exists( $log_dir ) ) {
$this->prevent_directory_browsing( $log_dir );
return $log_dir;
}
// Create the folder using wp_mkdir_p() instead of relying on KLogger
$folder_created = wp_mkdir_p( $log_dir );
// Something went wrong mapping the directory
if( ! $folder_created ) {
$this->log( 'The log directory could not be created: ' . $log_dir, __METHOD__, 'error' );
return false;
}
// Protect directory from being browsed by adding index.html
$this->prevent_directory_browsing( $log_dir );
// Make sure the new log directory can be written to
return $log_dir;
}
/**
* Prevent browsing a directory by adding an index.html file to it
*
* Code inspired by @see wp_privacy_generate_personal_data_export_file()
*
* @param string $dirpath Path to directory to protect (in this case, logging)
*
* @return bool True: File exists or was created; False: file could not be created.
*/
private function prevent_directory_browsing( $dirpath ) {
// Protect export folder from browsing.
$index_pathname = $dirpath . 'index.html';
if ( file_exists( $index_pathname ) ) {
return true;
}
$file = fopen( $index_pathname, 'w' );
if ( false === $file ) {
$this->log( 'Unable to protect directory from browsing.', __METHOD__, 'error' );
return false;
}
fwrite( $file, '<!-- Silence is golden. TrustedLogin is also pretty great. Learn more: https://www.trustedlogin.com/about/easy-and-safe/ -->' );
fclose( $file );
return true;
}
/**
* Returns whether logging is enabled
*
* @return bool
*/
public function is_enabled() {
$is_enabled = ! empty( $this->logging_enabled );
/**
* Filter: Whether debug logging is enabled in TrustedLogin Client
*
* @since 1.0.0
*
* @param bool $debug_mode Default: false
*/
$is_enabled = apply_filters( 'trustedlogin/' . $this->ns . '/logging/enabled', $is_enabled );
return (bool) $is_enabled;
}
/**
* @see https://github.com/php-fig/log/blob/master/Psr/Log/LogLevel.php for log levels
*
* @param string|\WP_Error $message Message or error to log. If a WP_Error is passed, $data is ignored.
* @param string $method Method where the log was called
* @param string $level PSR-3 log level
* @param \WP_Error|\Exception|mixed $data Optional. Error data. Ignored if $message is WP_Error.
*
*/
public function log( $message = '', $method = '', $level = 'debug', $data = array() ) {
if ( ! $this->is_enabled() ) {
return;
}
$levels = array( 'emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug' );
if ( ! in_array( $level, $levels ) ) {
$this->log( sprintf( 'Invalid level passed by %s method: %s', $method, $level ), __METHOD__, 'error' );
$level = 'debug'; // Continue processing original log
}
$log_message = $message;
if ( is_wp_error( $log_message ) ) {
$data = $log_message; // Store WP_Error as extra data.
$log_message = ''; // The message will be constructed below.
}
if ( ! is_string( $log_message ) ) {
$log_message = print_r( $log_message, true );
}
if ( is_wp_error( $data ) ) {
$log_message .= sprintf( '[%s] %s', $data->get_error_code(), $data->get_error_message() );
}
if ( $data instanceof \Exception ) {
$log_message .= sprintf( '[%s] %s', $data->getCode(), $data->getMessage() );
}
// Keep PSR-4 compatible
if ( $data && ! is_array( $data ) ) {
$data = array( $data );
}
do_action( 'trustedlogin/' . $this->ns . '/logging/log', $message, $method, $level, $data );
do_action( 'trustedlogin/' . $this->ns . '/logging/log_' . $level, $message, $method, $data );
// If logging is in place, don't use the error_log
if ( has_action( 'trustedlogin/' . $this->ns . '/logging/log' ) || has_action( 'trustedlogin/' . $this->ns . '/logging/log_' . $level ) ) {
return;
}
// Set up klogger, creating the logging file/directory if it doesn't already exist.
$this->klogger = $this->setup_klogger( $this->config );
// The logger class didn't load. Rely on WordPress logging, if enabled.
if ( ! $this->klogger ) {
$wp_debug = defined( 'WP_DEBUG' ) && WP_DEBUG;
$wp_debug_log = defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG;
// If WP_DEBUG and WP_DEBUG_LOG are enabled, log errors to that file.
if ( $wp_debug && $wp_debug_log ) {
if ( ! empty( $data ) ) {
$log_message .= ' Error data: ' . print_r( $data, true );
}
error_log( $method . ' (' . $level . '): ' . $log_message );
}
return;
}
$this->klogger->{$level}( $log_message, (array) $data );
}
}