-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpuerrorlogs.php
514 lines (445 loc) · 18.1 KB
/
wpuerrorlogs.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php
/*
Plugin Name: WPU Error Logs
Plugin URI: https://github.com/WordPressUtilities/wpuerrorlogs
Update URI: https://github.com/WordPressUtilities/wpuerrorlogs
Description: Make sense of your log files
Version: 0.9.1
Author: Darklg
Author URI: https://github.com/Darklg
Text Domain: wpuerrorlogs
Network: true
Domain Path: /lang
Requires at least: 6.2
Requires PHP: 8.0
License: MIT License
License URI: https://opensource.org/licenses/MIT
*/
if (!defined('ABSPATH')) {
exit();
}
class WPUErrorLogs {
public $settings_update;
private $number_of_days = 10;
private $plugin_version = '0.9.1';
private $plugin_settings = array(
'id' => 'wpuerrorlogs',
'name' => 'WPU Error Logs'
);
private $basetoolbox;
private $adminpages;
private $plugin_description;
public function __construct() {
add_action('init', array(&$this, 'load_translation'));
add_action('init', array(&$this, 'init'));
}
public function load_translation() {
# TRANSLATION
$lang_dir = dirname(plugin_basename(__FILE__)) . '/lang/';
if (strpos(__DIR__, 'mu-plugins') !== false) {
load_muplugin_textdomain('wpuerrorlogs', $lang_dir);
} else {
load_plugin_textdomain('wpuerrorlogs', false, $lang_dir);
}
$this->plugin_description = __('Make sense of your log files', 'wpuerrorlogs');
}
public function init() {
# UPDATE
require_once __DIR__ . '/inc/WPUBaseUpdate/WPUBaseUpdate.php';
$this->settings_update = new \wpuerrorlogs\WPUBaseUpdate(
'WordPressUtilities',
'wpuerrorlogs',
$this->plugin_version);
# TOOLBOX
require_once __DIR__ . '/inc/WPUBaseToolbox/WPUBaseToolbox.php';
$this->basetoolbox = new \wpuerrorlogs\WPUBaseToolbox(array(
'plugin_name' => $this->plugin_settings['name'],
'need_form_js' => false
));
# CUSTOM PAGE
$admin_pages = array(
'main' => array(
'icon_url' => 'dashicons-sos',
'menu_name' => $this->plugin_settings['name'],
'name' => $this->plugin_settings['name'],
'settings_link' => true,
'settings_name' => __('Settings', 'wpuerrorlogs'),
'function_content' => array(&$this,
'page_content__main'
),
'function_action' => array(&$this,
'page_action__main'
)
)
);
$pages_options = array(
'id' => $this->plugin_settings['id'],
'level' => 'manage_options',
'network_page' => (defined('MULTISITE') && MULTISITE),
'basename' => plugin_basename(__FILE__)
);
// Init admin page
require_once __DIR__ . '/inc/WPUBaseAdminPage/WPUBaseAdminPage.php';
$this->adminpages = new \wpuerrorlogs\WPUBaseAdminPage();
$this->adminpages->init($pages_options, $admin_pages);
# HOOKS
$this->number_of_days = apply_filters('wpuerrorlogs__number_of_days', $this->number_of_days);
}
public function page_content__main() {
/* Find debug file */
if (!WP_DEBUG_LOG) {
echo 'Debug logs are not enabled';
return;
}
$number_of_days = $this->number_of_days;
if (isset($_GET['number_of_days']) && is_numeric($_GET['number_of_days']) && $_GET['number_of_days'] <= $this->number_of_days) {
$number_of_days = intval($_GET['number_of_days']);
}
$search = '';
if (isset($_GET['s'])) {
$search = stripslashes(stripslashes($_GET['s']));
}
$errors = $this->get_logs(array(
'number_of_days' => $number_of_days,
'search_string' => $search
));
/* Keep only first five and extract data */
$colnames = array(
'count' => __('Count', 'wpuerrorlogs'),
'date' => __('Date', 'wpuerrorlogs'),
'type' => __('Type', 'wpuerrorlogs'),
'text' => __('Text', 'wpuerrorlogs')
);
echo '<details ' . ($search || isset($_GET['has_action']) ? 'open' : '') . '>';
echo '<summary>' . __('Filter results', 'wpuerrorlogs') . '</summary>';
/* Select number of days */
echo '<p>';
echo '<label for="wpuerrorlogs_switch_day">' . __('Check the last :', 'wpuerrorlogs') . '</label><br />';
echo '<select name="number_of_days" id="wpuerrorlogs_switch_day">';
for ($i = $this->number_of_days; $i > 0; $i--) {
echo '<option value="' . $i . '"' . ($number_of_days == $i ? ' selected' : '') . '>' . ($i < 2 ? __('1 day', 'wpuerrorlogs') : sprintf(__('%s days', 'wpuerrorlogs'), $i)) . '</option>';
}
echo '</select>';
echo '<input type="hidden" name="wpuerrorlogs_search_action" value="1">';
echo '</p>';
/* Search bar */
echo '<p>';
echo '<label>' . __('Search in errors', 'wpuerrorlogs') . '</label><br />';
echo '<input name="wpuerrorlogs_search" type="search" placeholder="' . __('Search', 'wpuerrorlogs') . '" value="' . htmlentities($search) . '" id="wpuerrorlogs_search" />';
echo '</p>';
submit_button(__('Filter results', 'wpuerrorlogs'));
echo '</details>';
/* Top errors */
$top_errors = $this->sort_errors_by_top($errors, 10);
echo '<h2>' . __('Top errors', 'wpuerrorlogs') . '</h2>';
$html_errors = $this->basetoolbox->array_to_html_table($top_errors, array(
'table_classname' => 'widefat striped',
'htmlspecialchars_td' => false,
'colnames' => $colnames
));
echo $html_errors ? $html_errors : '<p>' . __('No errors at the moment.', 'wpuerrorlogs') . '</p>';
/* Latest errors */
$latest_errors = $this->sort_errors_by_latest($errors, 10);
echo '<h2>' . __('Latest errors', 'wpuerrorlogs') . '</h2>';
$html_errors = $this->basetoolbox->array_to_html_table($latest_errors, array(
'table_classname' => 'widefat striped',
'htmlspecialchars_td' => false,
'colnames' => $colnames
));
echo $html_errors ? $html_errors : '<p>' . __('No errors at the moment.', 'wpuerrorlogs') . '</p>';
/* Latest by type */
$fatal_errors = array_filter($errors, function ($item) {
return isset($item['type']) && $item['type'] == 'php-fatal';
});
$latest_fatal_errors = $this->sort_errors_by_latest($fatal_errors, 10);
$html_errors = $this->basetoolbox->array_to_html_table($latest_fatal_errors, array(
'table_classname' => 'widefat striped',
'htmlspecialchars_td' => false,
'colnames' => $colnames
));
if ($html_errors) {
echo '<h2>' . __('Latest fatal errors', 'wpuerrorlogs') . '</h2>';
echo $html_errors;
}
}
public function page_action__main() {
$new_url = $this->adminpages->get_page_url('main');
if (isset($_POST['number_of_days'])) {
$new_url = add_query_arg('number_of_days', urlencode($_POST['number_of_days']), $new_url);
}
if (isset($_POST['wpuerrorlogs_search'])) {
$new_url = add_query_arg('s', urlencode($_POST['wpuerrorlogs_search']), $new_url);
}
if (isset($_POST['wpuerrorlogs_search_action'])) {
$new_url = add_query_arg('has_action', 1, $new_url);
}
wp_redirect($new_url);
die;
}
/* ----------------------------------------------------------
Sort errors
---------------------------------------------------------- */
public function sort_errors_by_top($errors, $max_number = 5) {
$top_errors_raw = [];
foreach ($errors as $error) {
if (!isset($top_errors_raw[$error['text']])) {
$top_errors_raw[$error['text']] = 0;
}
$top_errors_raw[$error['text']]++;
}
arsort($top_errors_raw);
$top_errors_raw = array_slice($top_errors_raw, 0, $max_number, true);
$top_errors = array();
foreach ($top_errors_raw as $text => $count) {
$top_errors[] = array(
'count' => $count,
'text' => $this->expand_error_text($text)
);
}
$top_errors = $this->prepare_errors_for_display($top_errors);
return $top_errors;
}
public function sort_errors_by_latest($errors, $max_number = 5) {
$latest_errors = array_slice($errors, 0, $max_number, true);
foreach ($latest_errors as $i => $error) {
$latest_errors[$i]['text'] = $this->expand_error_text($error['text']);
}
/* Reset keys */
$latest_errors = array_values($latest_errors);
$latest_errors = $this->prepare_errors_for_display($latest_errors);
return $latest_errors;
}
public function prepare_errors_for_display($errors) {
/* Prepare for display */
$errors = array_map(function ($item) {
$item['text'] = $this->display_content_with_toggle($item['text']);
return $item;
}, $errors);
return $errors;
}
/* ----------------------------------------------------------
Extract logs from file
---------------------------------------------------------- */
public function get_logs($args = array()) {
$args = wp_parse_args($args, array(
'number_of_days' => 5,
'search_string' => ''
));
$excluded_strings = array();
$included_strings = array();
$args['search_string'] = str_replace('-"', '"-', $args['search_string']);
$search_parts = explode('"', $args['search_string']);
$search_parts = array_map(function ($a) {
return str_replace('"', '', $a);
}, $search_parts);
if (!empty($search_parts)) {
$search_parts = array_filter($search_parts);
foreach ($search_parts as $search_part) {
if (substr($search_part, 0, 1) == '-') {
$excluded_strings[] = trim(substr($search_part, 1));
} else {
$included_strings[] = trim($search_part);
}
}
}
$previous_files = array();
/* Try to obtain previous files */
$file = ABSPATH . '/wp-content/debug.log';
$debug_dir = dirname(WP_DEBUG_LOG);
if (is_dir($debug_dir)) {
if (is_readable(WP_DEBUG_LOG)) {
$file = WP_DEBUG_LOG;
} else {
/* Find most recent file */
$previous_files = glob($debug_dir . '/*.log');
arsort($previous_files);
if (isset($previous_files[0])) {
$file = array_shift($previous_files);
$previous_files = array_slice($previous_files, 0, $args['number_of_days']);
}
}
}
if (!is_readable($file)) {
return array();
}
$max_date = time() - 86400 * $args['number_of_days'];
/* Parse errors in files */
$errors = $this->get_logs_from_file($file, array(
'max_date' => $max_date,
'included_strings' => $included_strings,
'excluded_strings' => $excluded_strings
));
if (empty($previous_files)) {
$previous_files = $this->find_previous_log_files($file, $args['number_of_days']);
}
foreach ($previous_files as $previous_file) {
$errors_previous = $this->get_logs_from_file($previous_file, array(
'max_date' => $max_date,
'included_strings' => $included_strings,
'excluded_strings' => $excluded_strings
));
foreach ($errors_previous as $error) {
$errors[] = $error;
}
}
return $errors;
}
public function find_previous_log_files($file, $number_of_days = 5) {
$date_formats = array('Ymd', 'dmY');
$previous_files = array();
foreach ($date_formats as $date_format) {
$now_date = date($date_format);
if (strpos($file, $now_date) === false) {
continue;
}
for ($i = 1; $i <= $number_of_days; $i++) {
$previous_date = date($date_format, time() - 86400 * $i);
$previous_file = str_replace($now_date, $previous_date, $file);
if (is_readable($previous_file)) {
$previous_files[] = $previous_file;
}
}
}
return $previous_files;
}
public function get_logs_from_file($file, $args = array()) {
$args = wp_parse_args($args, array(
'max_date' => time(),
'included_strings' => array(),
'excluded_strings' => array()
));
$lines = array_reverse(file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
$errors = [];
$default_error = array(
'date' => 'none',
'type' => 'none',
'text' => array()
);
$currentError = $default_error;
foreach ($lines as $line) {
$is_error_start = substr($line, 0, 1) == '[' && preg_match('/^\[\d{2}-[A-Za-z]{3}-\d{4} \d{2}:\d{2}:\d{2} [A-Za-z\/]+\]/', $line, $matches);
/* Is it a new error */
if ($is_error_start) {
$line_error = $this->get_error_from_line($line);
/* If date is not ok */
if (isset($matches[0])) {
$time = strtotime(str_replace(array('[', ']'), '', $matches[0]));
if ($time < $args['max_date']) {
break;
}
}
if ($currentError['date'] == 'none' && !empty($currentError['text'])) {
$line_error['text'] = array_merge($currentError['text'], $line_error['text']);
$line_error['text'] = $this->minimize_error_text(implode("\n", array_reverse($line_error['text'])));
$line_error = $this->filter_visible_error($line_error, $args);
if ($line_error) {
$errors[] = $line_error;
}
$currentError = $default_error;
continue;
}
$line_error['text'] = implode('', $line_error['text']);
$line_error = $this->filter_visible_error($line_error, $args);
if ($line_error) {
$errors[] = $line_error;
}
} else {
$currentError['text'][] = $line;
}
}
if (!empty($currentError) && is_array($currentError['text']) && !empty($currentError['text'])) {
$currentError['text'] = $this->minimize_error_text(implode("\n", array_reverse($currentError['text'])));
$currentError = $this->filter_visible_error($currentError, $args);
if ($currentError) {
$errors[] = $currentError;
}
}
return $errors;
}
public function filter_visible_error($error, $args) {
if (!empty($args['included_strings'])) {
foreach ($args['included_strings'] as $included_string) {
if (strpos($error['text'], $included_string) === false) {
return false;
}
}
}
if (!empty($args['excluded_strings'])) {
foreach ($args['excluded_strings'] as $excluded_string) {
if (strpos($error['text'], $excluded_string) !== false) {
return false;
}
}
}
return $error;
}
public function get_error_from_line($line) {
/* Extract values */
$date_parts = explode(']', $line);
$date = str_replace('[', '', $date_parts[0]);
$text = trim(substr($line, strlen('[' . $date . ']')));
/* Extract type */
$type = 'none';
$text_parts_type = explode(':', $text);
switch ($text_parts_type[0]) {
case 'PHP Warning':
$type = 'php-warning';
break;
case 'PHP Parse error':
$type = 'php-parse';
break;
case 'PHP Deprecated':
$type = 'php-deprecated';
break;
case 'PHP Fatal error':
$type = 'php-fatal';
break;
default:
}
/* Return value */
return array(
'date' => $date,
'type' => $type,
'text' => array($text)
);
}
/* ----------------------------------------------------------
Helpers
---------------------------------------------------------- */
/* Display content
-------------------------- */
public function display_content_with_toggle($content) {
$content = strip_tags($content);
$content_parts = explode("\n", $content);
$minimized_error = str_replace(ABSPATH, '', $content_parts[0]);
if ($minimized_error == $content) {
return $content;
}
$content = $minimized_error;
$content .= '<details><summary>' . __('Full error', 'wpuerrorlogs') . '</summary><pre style="overflow:auto;white-space: normal;">' . implode("\n", $content_parts) . '</pre></details>';
return $content;
}
/* Minimize text
-------------------------- */
public function minimize_get_correspondances() {
return array(
'abs' => ABSPATH,
'plug' => 'wp-content/plugins/'
);
}
public function expand_error_text($text) {
$correspondances = $this->minimize_get_correspondances();
foreach ($correspondances as $min => $max) {
$text = str_replace('#!' . $min . '!#', $max, $text);
}
return $text;
}
public function minimize_error_text($text) {
$correspondances = $this->minimize_get_correspondances();
foreach ($correspondances as $min => $max) {
$text = str_replace($max, '#!' . $min . '!#', $text);
}
return $text;
}
}
$WPUErrorLogs = new WPUErrorLogs();