| @@ -3,8 +3,10 @@ | ||
| 3 | 3 | if (!defined('ABSPATH')) { |
| 4 | 4 | exit; |
| 5 | 5 | } |
| 6 | 6 | |
| 7 | +require_once __DIR__ . '/privacy.php'; | |
| 8 | + | |
| 7 | 9 | /** |
| 8 | 10 | * Request monitoring |
| 9 | 11 | * |
| 10 | 12 | * */ |
| @@ -67,8 +69,12 @@ | ||
| 67 | 69 | |
| 68 | 70 | # let's monitor outgoing logs |
| 69 | 71 | function api_monitor_outgoing($preempt, $args, $url){ |
| 70 | 72 | |
| 73 | + if (metasync_telemetry_is_disabled()) { | |
| 74 | + return $preempt; | |
| 75 | + } | |
| 76 | + | |
| 71 | 77 | # ignore wp chron requests |
| 72 | 78 | if(strpos($url, 'wp-cron.php') !== false){ |
| 73 | 79 | return $preempt; |
| 74 | 80 | } |
| @@ -82,10 +88,10 @@ | ||
| 82 | 88 | $request = [ |
| 83 | 89 | 'timestamp' => $this->log_timestamp(), |
| 84 | 90 | 'type' => 'outgoing_request', |
| 85 | 91 | 'method' => $args['method'] ?? 'undefined', |
| 86 | - 'request_url' => $url, | |
| 87 | - 'args' => $args | |
| 92 | + 'request_url' => metasync_telemetry_request_path($url), | |
| 93 | + 'args' => $this->sanitize_args($args) | |
| 88 | 94 | ]; |
| 89 | 95 | |
| 90 | 96 | # send the request with telemetry manager |
| 91 | 97 | $this->telemetry_manager->send_message('Outgoing Request', 'info', $request); |
| @@ -98,8 +104,12 @@ | ||
| 98 | 104 | |
| 99 | 105 | # monitor incoming |
| 100 | 106 | function api_monitor_incoming($result, $server, $request){ |
| 101 | 107 | |
| 108 | + if (metasync_telemetry_is_disabled()) { | |
| 109 | + return $result; | |
| 110 | + } | |
| 111 | + | |
| 102 | 112 | # check that the route is permitted for logging |
| 103 | 113 | if ( empty($request->get_route()) || $this->log_uri_permitted($request->get_route()) == False ){ |
| 104 | 114 | return $result; |
| 105 | 115 | } |
| @@ -109,20 +119,11 @@ | ||
| 109 | 119 | $request_data = [ |
| 110 | 120 | 'timestamp' => $this->log_timestamp(), |
| 111 | 121 | 'type' => 'incoming_request', |
| 112 | 122 | 'method' => $request->get_method(), |
| 113 | - 'uri' => $request->get_route(), | |
| 114 | - 'headers' => $request->get_headers(), | |
| 115 | - 'body' => $request->get_params() | |
| 123 | + 'uri' => metasync_telemetry_request_path($request->get_route()) | |
| 116 | 124 | ]; |
| 117 | 125 | |
| 118 | - # if the request is not GET attach get params | |
| 119 | - if($request->get_method() !== 'GET'){ | |
| 120 | - | |
| 121 | - #append request get params | |
| 122 | - $request_data['_GET_params'] = $_GET; | |
| 123 | - } | |
| 124 | - | |
| 125 | 126 | $this->telemetry_manager->send_message('Incoming Request', 'info', $request_data); |
| 126 | 127 | |
| 127 | 128 | |
| 128 | 129 | # Return result to allow the request to continue |
| @@ -135,8 +136,11 @@ | ||
| 135 | 136 | |
| 136 | 137 | # monitor the response to an outgoing API call |
| 137 | 138 | # function monitor_outgoing_response($response, $context, $url, $parsed_args){ |
| 138 | 139 | function monitor_outgoing_response($response, $parsed_args, $url){ |
| 140 | + if (metasync_telemetry_is_disabled()) { | |
| 141 | + return $response; | |
| 142 | + } | |
| 139 | 143 | |
| 140 | 144 | # Skip logging for wp-cron requests |
| 141 | 145 | if (strpos($url, 'wp-cron.php') !== false) { |
| 142 | 146 | return $response; |
| @@ -151,9 +155,9 @@ | ||
| 151 | 155 | $response_data = [ |
| 152 | 156 | 'timestamp' => $this->log_timestamp(), |
| 153 | 157 | 'type' => 'outgoing_response', |
| 154 | 158 | 'status_code' => $response['response']['code'] ?? '', |
| 155 | - 'url' => $url, | |
| 159 | + 'url' => metasync_telemetry_request_path($url), | |
| 156 | 160 | 'response_data' => $this->sanitize_outgoing_response($response), |
| 157 | 161 | 'args' => $this->sanitize_args($parsed_args) |
| 158 | 162 | ]; |
| 159 | 163 | |
| @@ -166,8 +170,11 @@ | ||
| 166 | 170 | } |
| 167 | 171 | |
| 168 | 172 | # monitor the response to an incoming api call |
| 169 | 173 | function monitor_incoming_response($response, $server, $request){ |
| 174 | + if (metasync_telemetry_is_disabled()) { | |
| 175 | + return $response; | |
| 176 | + } | |
| 170 | 177 | |
| 171 | 178 | # check that the route is permitted for logging |
| 172 | 179 | if ( empty($request->get_route()) || $this->log_uri_permitted($request->get_route()) == False ){ |
| 173 | 180 | return $response; |
| @@ -177,9 +184,9 @@ | ||
| 177 | 184 | $response_object = array( |
| 178 | 185 | 'timestamp' => $this->log_timestamp(), |
| 179 | 186 | 'type' => 'incoming_response', |
| 180 | 187 | 'status_code' => $response->get_status() ?? '', |
| 181 | - 'url' => $request->get_route(), | |
| 188 | + 'url' => metasync_telemetry_request_path($request->get_route()), | |
| 182 | 189 | 'method' => $request->get_method(), |
| 183 | 190 | 'response_data' => $this->sanitize_response_data($response), |
| 184 | 191 | 'request_data' => $this->sanitize_request_data($request) |
| 185 | 192 | ); |
| @@ -204,12 +211,9 @@ | ||
| 204 | 211 | return is_array($response) ? $response : array(); |
| 205 | 212 | } |
| 206 | 213 | |
| 207 | 214 | return array( |
| 208 | - 'status' => method_exists($response, 'get_status') ? $response->get_status() : null, | |
| 209 | - 'data' => method_exists($response, 'get_data') ? $response->get_data() : null, | |
| 210 | - 'headers' => method_exists($response, 'get_headers') ? $response->get_headers() : null, | |
| 211 | - 'links' => method_exists($response, 'get_links') ? $response->get_links() : null | |
| 215 | + 'status' => method_exists($response, 'get_status') ? $response->get_status() : null | |
| 212 | 216 | ); |
| 213 | 217 | } |
| 214 | 218 | |
| 215 | 219 | /** |
| @@ -224,12 +228,11 @@ | ||
| 224 | 228 | } |
| 225 | 229 | |
| 226 | 230 | return array( |
| 227 | 231 | 'method' => method_exists($request, 'get_method') ? $request->get_method() : null, |
| 228 | - 'route' => method_exists($request, 'get_route') ? $request->get_route() : null, | |
| 229 | - 'params' => method_exists($request, 'get_params') ? $request->get_params() : null, | |
| 230 | - 'headers' => method_exists($request, 'get_headers') ? $request->get_headers() : null, | |
| 231 | - 'body' => method_exists($request, 'get_body') ? $request->get_body() : null | |
| 232 | + 'route' => method_exists($request, 'get_route') | |
| 233 | + ? metasync_telemetry_request_path($request->get_route()) | |
| 234 | + : null | |
| 232 | 235 | ); |
| 233 | 236 | } |
| 234 | 237 | |
| 235 | 238 | /** |
| @@ -252,20 +255,8 @@ | ||
| 252 | 255 | 'message' => $response['response']['message'] ?? null |
| 253 | 256 | ); |
| 254 | 257 | } |
| 255 | 258 | |
| 256 | - if (isset($response['headers'])) { | |
| 257 | - $sanitized['headers'] = $response['headers']; | |
| 258 | - } | |
| 259 | - | |
| 260 | - if (isset($response['body'])) { | |
| 261 | - $sanitized['body'] = $response['body']; | |
| 262 | - } | |
| 263 | - | |
| 264 | - if (isset($response['cookies'])) { | |
| 265 | - $sanitized['cookies'] = $response['cookies']; | |
| 266 | - } | |
| 267 | - | |
| 268 | 259 | return $sanitized; |
| 269 | 260 | } |
| 270 | 261 | |
| 271 | 262 | /** |
| @@ -289,22 +280,8 @@ | ||
| 289 | 280 | $sanitized[$key] = $args[$key]; |
| 290 | 281 | } |
| 291 | 282 | } |
| 292 | 283 | |
| 293 | - // Sanitize headers if present | |
| 294 | - if (isset($args['headers']) && is_array($args['headers'])) { | |
| 295 | - $sanitized['headers'] = $args['headers']; | |
| 296 | - } | |
| 297 | - | |
| 298 | - // Sanitize body if present (but limit size) | |
| 299 | - if (isset($args['body'])) { | |
| 300 | - $body = $args['body']; | |
| 301 | - if (is_string($body) && strlen($body) > 1000) { | |
| 302 | - $body = substr($body, 0, 1000) . '... [truncated]'; | |
| 303 | - } | |
| 304 | - $sanitized['body'] = $body; | |
| 305 | - } | |
| 306 | - | |
| 307 | 284 | return $sanitized; |
| 308 | 285 | } |
| 309 | 286 | |
| 310 | 287 | /** |
| @@ -324,5 +301,5 @@ | ||
| 324 | 301 | # Monitor incoming request responses |
| 325 | 302 | add_filter('rest_post_dispatch', [$this, 'monitor_incoming_response'], 10, 3); |
| 326 | 303 | |
| 327 | 304 | } |
| 328 | -} | |
| 305 | +} | |