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

146 lines 4.5 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 ABSPATH . 'wp-admin/includes/plugin.php';
25
26 if (is_plugin_active('wpremote/plugin.php')) {
27 include_once dirname(__FILE__) . '/../wp_settings.php';
28 include_once dirname(__FILE__) . '/../wp_db.php';
29 include_once dirname(__FILE__) . '/../info.php';
30 self::$settings = new WPRWPSettings();
31 self::$db = new WPRWPDb();
32 self::$info = new WPRInfo(self::$settings);
33
34 if (self::$info->isServiceActive('php_error_monitoring') && self::$info->hasValidDBVersion()) {
35 self::updateDefaultValues();
36 self::setPhpErrorHandler();
37 self::$initialized = true;
38 }
39 }
40 }
41
42 public static function isValidErrorLevel($error_level) {
43 return is_int($error_level) && ($error_level & E_ALL) === $error_level;
44 }
45
46 public static function updateDefaultValues() {
47 if (!isset(self::$info->config['php_error_monitoring']) ||
48 !is_array(self::$info->config['php_error_monitoring'])) {
49 return;
50 }
51
52 $config = self::$info->config['php_error_monitoring'];
53
54 if (array_key_exists('include_backtrace', $config) && is_bool($config['include_backtrace'])) {
55 self::$include_backtrace = $config['include_backtrace'];
56 }
57
58 if (array_key_exists('max_table_length', $config) && is_int($config['max_table_length'])) {
59 self::$max_table_length = $config['max_table_length'];
60 }
61
62 if (array_key_exists('error_level', $config) && self::isValidErrorLevel($config['error_level'])) {
63 self::$error_level = $config['error_level'];
64 }
65
66 if (array_key_exists('md5s_to_ignore', $config) && is_array($config['md5s_to_ignore'])) {
67 self::$md5s_to_ignore = $config['md5s_to_ignore'];
68 }
69
70 if (array_key_exists('max_backtrace_frames', $config) && is_int($config['max_backtrace_frames'])) {
71 self::$max_backtrace_frames = $config['max_backtrace_frames'];
72 }
73 }
74
75 public static function setPhpErrorHandler() {
76 set_error_handler(array(self::class, 'errorHandler'), self::$error_level);
77 register_shutdown_function(array(self::class, 'handleShutdown'));
78 }
79
80 public static function handleShutdown() {
81 $error = error_get_last();
82 if (null !== $error && (($error['type'] & self::$error_level) === $error['type'])) {
83 self::errorHandler($error['type'], $error['message'], $error['file'], $error['line']);
84 }
85 }
86
87 public static function canAddToBVTable() {
88 $bv_table = self::$db->getBVTable(self::ERROR_TABLE);
89
90 if (!self::$db->isTablePresent($bv_table)) {
91 return false;
92 }
93
94 $row_count = self::$db->rowsCount($bv_table);
95 return $row_count < self::$max_table_length;
96 }
97
98 public static function saveError($data) {
99 if (!self::canAddToBVTable()) {
100 return;
101 }
102 $values = array("data" => $data, "time" => time());
103 self::$db->insertIntoBVTable(self::ERROR_TABLE, $values);
104 }
105
106 public static function canCaptureError($md5) {
107 if (in_array($md5, self::$md5s_to_ignore, true)) {
108 return false;
109 }
110 return true;
111 }
112
113 public static function errorHandler($code, $message, $file, $line) {
114 $data = array();
115
116 $serialized_backtrace = '';
117 if (self::$include_backtrace) {
118 $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, self::$max_backtrace_frames);
119
120 $keys = array_flip(array('file', 'line', 'function'));
121 foreach ($backtrace as &$frame) {
122 $frame = array_intersect_key($frame, $keys);
123 }
124
125 $serialized_backtrace = maybe_serialize($backtrace);
126 $data["backtrace"] = $serialized_backtrace;
127 }
128
129 $data["md5"] = md5($code . '-' . $message . '-' . $line . '-' . $file . '-' . $serialized_backtrace);
130
131 if (!self::canCaptureError($data['md5'])) {
132 return;
133 }
134
135 $data["error_code"] = $code;
136 $data["error_message"] = $message;
137 $data["error_line"] = $line;
138 $data["error_file"] = $file;
139 $uri = array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : '';
140 $data["request_path"] = parse_url($uri, PHP_URL_PATH);
141 $data["request_id"] = WPRInfo::getRequestID();
142
143 self::saveError(maybe_serialize($data));
144 }
145 }
146 endif;