-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-mo-saml-logger.php
325 lines (302 loc) · 10.1 KB
/
class-mo-saml-logger.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
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
<?php
/**
* This File has class which handles all the debug logs file related functions.
*
* @package miniorange-saml-20-single-sign-on
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
require_once 'class-mo-saml-utilities.php';
require_once __DIR__ . '/includes/lib/class-mo-saml-options-enum.php';
require_once 'class-mo-saml-wp-config-editor.php';
/**
* Class includes all the functions like to create log file, to add logs, to get log file, and etc.. .
*
* @category Class
*/
class Mo_SAML_Logger {
const INFO = 'INFO';
const DEBUG = 'DEBUG';
const ERROR = 'ERROR';
const CRITICAL = 'CRITICAL';
/**
* To check if file is writable.
*
* @var boolean
*/
private static $log_file_writable = false;
/**
* Data of logs.
*
* @var array
*/
protected $cached_logs = array();
/**
* Check if log file is writable.
*
* @return bool
*/
public static function mo_saml_is_log_file_writable() {
if ( is_dir( self::mo_saml_get_saml_log_directory() ) ) {
return wp_is_writable( self::mo_saml_get_saml_log_directory() );
}
}
/**
* Initializes directory to write debug logs.
*
* @throws Exception Directory Not created.
*/
public static function mo_saml_init() {
// For setting up debug directory for log files.
$upload_dir = wp_upload_dir( null, false );
$log_dir = self::mo_saml_get_saml_log_directory();
$upload_basedir = str_replace( '\\', '/', $upload_dir['basedir'] );
if ( wp_is_writable( $upload_basedir ) ) {
self::$log_file_writable = true;
if ( ! is_dir( $log_dir ) ) {
global $wp_filesystem;
if ( ! WP_Filesystem() ) {
return;
}
if ( ! $wp_filesystem->mkdir( $log_dir, 0755, true ) && ! is_dir( $log_dir ) ) {
throw new Exception( sprintf( 'Directory "%s" was not created', esc_html( $log_dir ) ) );
}
self::mo_saml_create_files();
}
} else {
add_action( 'admin_notices', array( __CLASS__, 'mo_saml_directory_notice' ) );
}
}
/**
* This function is to get SAML log directory.
*
* @return string SAML log directory path.
*/
public static function mo_saml_get_saml_log_directory() {
$upload_dir = wp_upload_dir( null, false );
$log_dir = $upload_dir['basedir'] . '/mo-saml-logs/';
return str_replace( '\\', '/', $log_dir );
}
/**
* Add a log entry along with the log level.
*
* @param string $log_message log entry message.
* @param string $log_level log entry info.
*/
public static function mo_saml_add_log( $log_message = '', $log_level = self::INFO ) {
if ( ! self::mo_saml_is_debugging_enabled() ) {
return;
}
//phpcs:ignore WordPress.PHP.DevelopmentFunctions.prevent_path_disclosure_error_reporting, WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_error_reporting -- Adding this to write error in error logs.
error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR );
$log_path = self::mo_saml_get_log_file_path( 'mo_saml' );
if ( $log_path ) {
//phpcs:ignore WordPress.PHP.IniSet.display_errors_Disallowed -- Prevent displaying the errors.
ini_set( 'display_errors', 0 );
//phpcs:ignore WordPress.PHP.IniSet.log_errors_Disallowed -- Enable error logging.
ini_set( 'log_errors', 1 );
//phpcs:ignore WordPress.PHP.IniSet.Risky -- To add the error log path.
ini_set( 'error_log', $log_path );
$exception = new Exception();
$trace = $exception->getTrace();
$last_call = $trace[1];
$message = $log_level;
$message .= ' ' . $last_call['function'] . ' : ' . $last_call['line'];
$message = $message . ' ' . str_replace( array( "\r", "\n", "\t" ), '', rtrim( $log_message ) ) . PHP_EOL;
$message = preg_replace( '/[,]/', "\n", $message );
//phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- The error_log is used here for writing content to the log file.
error_log( $message );
}
}
/**
* This function is to Log critical errors.
*/
public static function mo_saml_log_critical_errors() {
$error = error_get_last();
if ( $error && in_array(
$error['type'],
array(
E_ERROR,
E_PARSE,
E_COMPILE_ERROR,
E_USER_ERROR,
E_RECOVERABLE_ERROR,
),
true
) ) {
self::mo_saml_add_log(
/* translators: %1$s: message term %2$s: file term %3$s: line term*/
sprintf( __( '%1$s in %2$s on line %3$s', 'miniorange-saml-20-single-sign-on' ), $error['message'], $error['file'], $error['line'] ) . PHP_EOL,
self::CRITICAL
);
}
}
/**
* This function is to get all log files in the log directory.
*
* @return array
* @since 3.4.0
*/
public static function mo_saml_get_log_files() {
$files = is_dir( self::mo_saml_get_saml_log_directory() ) ? scandir( self::mo_saml_get_saml_log_directory() ) : '';
$result = array();
if ( ! empty( $files ) ) {
foreach ( $files as $key => $value ) {
if ( ! in_array( $value, array( '.', '..' ), true ) ) {
if ( ! is_dir( $value ) && strstr( $value, '.log' ) ) {
$result[ sanitize_title( $value ) ] = $value;
}
}
}
}
return $result;
}
/**
* Deletes all the files in the Log directory older than 7 Days.
*
* @param int $timestamp time.
*/
public static function mo_saml_delete_logs_before_timestamp( $timestamp = 0 ) {
if ( ! $timestamp ) {
return;
}
$log_files = self::mo_saml_get_log_files();
foreach ( $log_files as $log_file ) {
$last_modified = filemtime( trailingslashit( self::mo_saml_get_saml_log_directory() ) . $log_file );
if ( $last_modified < $timestamp ) {
@unlink(trailingslashit(self::mo_saml_get_saml_log_directory()) . $log_file); // @codingStandardsIgnoreLine.
}
}
}
/**
* Get the file path of the current log file used by plugins.
*
* @param string $handle //file path.
* @return string|false The log file path or false on failure.
*/
public static function mo_saml_get_log_file_path( $handle ) {
if ( function_exists( 'wp_hash' ) ) {
$log_file_path = trailingslashit( self::mo_saml_get_saml_log_directory() ) . self::mo_saml_get_log_file_name( $handle );
return str_replace( '\\', '/', $log_file_path );
} else {
return false;
}
}
/**
* To get the log for based on the time.
*
* @param string $handle file path name.
*/
public static function mo_saml_get_log_file_name( $handle ) {
if ( function_exists( 'wp_hash' ) ) {
$date_suffix = gmdate( 'Y-m-d', time() );
$hash_suffix = wp_hash( $handle );
return sanitize_file_name( implode( '-', array( $handle, $date_suffix, $hash_suffix ) ) . '.log' );
} else {
_doing_it_wrong( __METHOD__, esc_html_e( 'This method should not be called before plugins_loaded.', 'miniorange-saml-20-single-sign-on' ), esc_html( Mo_Saml_Options_Plugin_Constants::VERSION ) );
return false;
}
}
/**
* Used to show the UI part of the log feature to user screen.
*/
public static function mo_saml_log_page() {
mo_saml_display_log_page();
}
/**
* Creates files Index.html for directory listing and local .htaccess rule to avoid hotlinking.
*/
private static function mo_saml_create_files() {
$upload_dir = wp_get_upload_dir();
$files = array(
array(
'base' => self::mo_saml_get_saml_log_directory(),
'file' => '.htaccess',
'content' => 'deny from all',
),
array(
'base' => self::mo_saml_get_saml_log_directory(),
'file' => 'index.html',
'content' => '',
),
);
foreach ( $files as $file ) {
if ( wp_mkdir_p( $file['base'] ) && ! file_exists( trailingslashit( $file['base'] ) . $file['file'] ) ) {
global $wp_filesystem;
if ( ! WP_Filesystem() ) {
return;
}
$file_path = trailingslashit( $file['base'] ) . $file['file'];
$wp_filesystem->put_contents( $file_path, $file['content'], FS_CHMOD_FILE );
}
}
}
/**
* Check if a constant is defined if not define a cosnt.
*
* @param string $name name constant.
* @param string $value value of constant.
*/
private static function define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/**
* To check if Debug constant is defined and logs are enabled.
*
* @return bool
*/
public static function mo_saml_is_debugging_enabled() {
if ( ! defined( 'MO_SAML_LOGGING' ) ) {
return false;
} else {
return MO_SAML_LOGGING;
}
}
/**
* This function is to show admin notices.
*
* @return void
*/
public static function mo_saml_admin_notices() {
if ( ! self::mo_saml_is_log_file_writable() && self::mo_saml_is_debugging_enabled() ) {
add_action(
'admin_notices',
function () {
echo wp_kses_post(
sprintf(
/* translators: %1s: search term */
'<div class="error" style=""><p/>' . __( 'To allow logging, make <code>"%1s"</code> directory writable.miniOrange will not be able to log the errors.', 'miniorange-saml-20-single-sign-on' ) . '</div>',
self::mo_saml_get_saml_log_directory()
)
);
}
);
}
if ( self::mo_saml_is_log_file_writable() && self::mo_saml_is_debugging_enabled() && current_user_can( 'manage_options' ) ) {
add_action(
'admin_notices',
function () {
echo wp_kses_post(
sprintf(
/* translators: %s: search term */
'<div class="updated"><p/>' . __( ' miniOrange SAML 2.0 logs are active. Want to turn it off? <a href="%s">Learn more here.', 'miniorange-saml-20-single-sign-on' ) . '</a></div>',
admin_url() . 'admin.php?page=mo_saml_enable_debug_logs'
)
);
}
);
}
}
/**
* This function is to show directory notice.
*
* @return void
*/
public static function mo_saml_directory_notice() {
$msg = esc_html( sprintf( 'Directory %1$s is not writeable, plugin will not able to write the file please update file permission', self::mo_saml_get_saml_log_directory() ) );
echo '<div class="error"> <p>' . esc_html( $msg ) . '</p></div>';
}
}