PluginProbe
The WP Remote WordPress Plugin / 5.72
The WP Remote WordPress Plugin v5.72
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 5.72, at php_error_monitoring/monitoring.php

155 lines 4.8 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 set_error_handler(array(self::class, 'errorHandler'), self::$error_level);
86 register_shutdown_function(array(self::class, 'handleShutdown'));
87 }
88
89 public static function handleShutdown() {
90 $error = error_get_last();
91 if (null !== $error && (($error['type'] & self::$error_level) === $error['type'])) {
92 self::errorHandler($error['type'], $error['message'], $error['file'], $error['line']);
93 }
94 }
95
96 public static function canAddToBVTable() {
97 $bv_table = self::$db->getBVTable(self::ERROR_TABLE);
98
99 if (!self::$db->isTablePresent($bv_table)) {
100 return false;
101 }
102
103 $row_count = self::$db->rowsCount($bv_table);
104 return $row_count < self::$max_table_length;
105 }
106
107 public static function saveError($data) {
108 if (!self::canAddToBVTable()) {
109 return;
110 }
111 $values = array("data" => $data, "time" => time());
112 self::$db->insertIntoBVTable(self::ERROR_TABLE, $values);
113 }
114
115 public static function canCaptureError($md5) {
116 if (in_array($md5, self::$md5s_to_ignore, true)) {
117 return false;
118 }
119 return true;
120 }
121
122 public static function errorHandler($code, $message, $file, $line) {
123 $data = array();
124
125 $serialized_backtrace = '';
126 if (self::$include_backtrace) {
127 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, self::$max_backtrace_frames);
128
129 $keys = array_flip(array('file', 'line', 'function'));
130 foreach ($backtrace as &$frame) {
131 $frame = array_intersect_key($frame, $keys);
132 }
133
134 $serialized_backtrace = maybe_serialize($backtrace);
135 $data["backtrace"] = $serialized_backtrace;
136 }
137
138 $data["md5"] = md5($code . '-' . $message . '-' . $line . '-' . $file . '-' . $serialized_backtrace);
139
140 if (!self::canCaptureError($data['md5'])) {
141 return;
142 }
143
144 $data["error_code"] = $code;
145 $data["error_message"] = $message;
146 $data["error_line"] = $line;
147 $data["error_file"] = $file;
148 $uri = array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : '';
149 $data["request_path"] = parse_url($uri, PHP_URL_PATH);
150 $data["request_id"] = WPRInfo::getRequestID();
151
152 self::saveError(maybe_serialize($data));
153 }
154 }
155 endif;