PluginProbe
The WP Remote WordPress Plugin / 6.65
The WP Remote WordPress Plugin v6.65
6.72 6.69 6.65 6.62 6.48 6.47 4.87 4.97 5.05 5.09 5.16 5.22 5.24 5.25 5.38 5.41 5.42 5.45 5.47 5.53 5.56 5.65 5.68 5.72 5.73 All 53 releases
wpremote / php_error_monitoring / monitoring.php

monitoring.php in The WP Remote WordPress Plugin 6.65, at php_error_monitoring/monitoring.php

162 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH') && !defined("PHP_ERR_MONIT_PATH")) exit;
3
4 if (!class_exists('WPRWPPHPErrorMonitoring')) :
5 class WPRWPPHPErrorMonitoring {
6 private static $db;
7 private static $settings;
8 private static $info;
9 private static $initialized = false;
10 private static $config = array();
11
12 const ERROR_TABLE = 'php_error_store';
13
14 private static $include_backtrace = false;
15 private static $max_table_length = 10000;
16 private static $error_level = E_ALL;
17 private static $max_backtrace_frames = 10;
18 private static $md5s_to_ignore = array();
19
20 public static function init() {
21 if (self::$initialized) {
22 return;
23 }
24 include_once dirname(__FILE__) . '/../wp_settings.php';
25 include_once dirname(__FILE__) . '/../wp_db.php';
26 include_once dirname(__FILE__) . '/../info.php';
27
28 self::$settings = new WPRWPSettings();
29 self::$db = new WPRWPDb();
30 self::$info = new WPRInfo(self::$settings);
31
32 if (self::$info->isServiceActive('php_error_monitoring')) {
33 add_action('wpr_clear_php_error_config', array('WPRWPPHPErrorMonitoring', 'clearConfig'));
34 }
35
36 include_once ABSPATH . 'wp-admin/includes/plugin.php';
37
38 if (is_plugin_active('wpremote/plugin.php')) {
39 if (self::$info->isServiceActive('php_error_monitoring') && self::$info->hasValidDBVersion()) {
40 self::updateDefaultValues();
41 self::setPhpErrorHandler();
42 self::$initialized = true;
43 }
44 }
45 }
46
47 public static function clearConfig() {
48 self::$db->dropBVTable(self::ERROR_TABLE);
49 }
50
51 public static function isValidErrorLevel($error_level) {
52 return is_int($error_level) && ($error_level & E_ALL) === $error_level;
53 }
54
55 public static function updateDefaultValues() {
56 if (!isset(self::$info->config['php_error_monitoring']) ||
57 !is_array(self::$info->config['php_error_monitoring'])) {
58 return;
59 }
60
61 $config = self::$info->config['php_error_monitoring'];
62
63 if (array_key_exists('include_backtrace', $config) && is_bool($config['include_backtrace'])) {
64 self::$include_backtrace = $config['include_backtrace'];
65 }
66
67 if (array_key_exists('max_table_length', $config) && is_int($config['max_table_length'])) {
68 self::$max_table_length = $config['max_table_length'];
69 }
70
71 if (array_key_exists('error_level', $config) && self::isValidErrorLevel($config['error_level'])) {
72 self::$error_level = $config['error_level'];
73 }
74
75 if (array_key_exists('md5s_to_ignore', $config) && is_array($config['md5s_to_ignore'])) {
76 self::$md5s_to_ignore = $config['md5s_to_ignore'];
77 }
78
79 if (array_key_exists('max_backtrace_frames', $config) && is_int($config['max_backtrace_frames'])) {
80 self::$max_backtrace_frames = $config['max_backtrace_frames'];
81 }
82 }
83
84 public static function setPhpErrorHandler() {
85 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler -- Custom error handling
86 set_error_handler(array(self::class, 'errorHandler'), self::$error_level);
87 register_shutdown_function(array(self::class, 'handleShutdown'));
88 }
89
90 public static function handleShutdown() {
91 $error = error_get_last();
92 if (null !== $error && (($error['type'] & self::$error_level) === $error['type'])) {
93 self::errorHandler($error['type'], $error['message'], $error['file'], $error['line']);
94 }
95 }
96
97 public static function canAddToBVTable() {
98 $bv_table = self::$db->getBVTable(self::ERROR_TABLE);
99
100 if (!self::$db->isTablePresent($bv_table)) {
101 return false;
102 }
103
104 $row_count = self::$db->rowsCount($bv_table);
105 return $row_count < self::$max_table_length;
106 }
107
108 public static function saveError($data) {
109 if (!self::canAddToBVTable()) {
110 return;
111 }
112 $values = array("data" => $data, "time" => time());
113 self::$db->insertIntoBVTable(self::ERROR_TABLE, $values);
114 }
115
116 public static function canCaptureError($md5) {
117 if (in_array($md5, self::$md5s_to_ignore, true)) {
118 return false;
119 }
120 return true;
121 }
122
123 public static function errorHandler($code, $message, $file, $line) {
124 $data = array();
125
126 $serialized_backtrace = '';
127 if (self::$include_backtrace) {
128 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace -- Needed for stack trace functionality
129 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, self::$max_backtrace_frames);
130
131 $keys = array_flip(array('file', 'line', 'function'));
132 foreach ($backtrace as &$frame) {
133 $frame = array_intersect_key($frame, $keys);
134 }
135
136 $serialized_backtrace = maybe_serialize($backtrace);
137 $data["backtrace"] = $serialized_backtrace;
138 }
139
140 $data["md5"] = md5($code . '-' . $message . '-' . $line . '-' . $file . '-' . $serialized_backtrace);
141
142 if (!self::canCaptureError($data['md5'])) {
143 return false;
144 }
145
146 $data["error_code"] = $code;
147 $data["error_message"] = $message;
148 $data["error_line"] = $line;
149 $data["error_file"] = $file;
150 $uri = WPRHelper::getRawParam('SERVER', 'REQUEST_URI');
151 if (!isset($uri)) {
152 $uri = "";
153 }
154 $data["request_path"] = parse_url($uri, PHP_URL_PATH);
155 $data["request_id"] = WPRInfo::getRequestID();
156
157 self::saveError(maybe_serialize($data));
158 return false;
159 }
160 }
161 endif;
162