'POST', 'callback' => array( $api, 'handle_intercept', ), 'permission_callback' => array( $api, 'permission_callback', ), ) ); } public function permission_callback( WP_REST_Request $request ) { // Check for the API key in the header. $provided_auth_hash = $request->get_header( 'X-NSCPLE-API-KEY' ); $helper = new Helper(); $auth_hash = $helper->get_auth_hash(); if ( empty( $provided_auth_hash ) || $provided_auth_hash !== $auth_hash ) { return new WP_Error( 'rest_forbidden', __( 'Invalid Auth', '3rd-party-request-manager' ), array( 'status' => 403 ) ); } return true; } public function handle_intercept( WP_REST_Request $request ) { global $wpdb; $table_name = $wpdb->prefix . 'nsc3ppm_intercepts'; $data = $this->get_interception_data( $request ); $inserted = 0; $batch_domains = array(); foreach ( $data as $event ) { $original_url = isset( $event['originalUrl'] ) ? sanitize_text_field( $event['originalUrl'] ) : ''; // Validate URL format. if ( ! filter_var( $original_url, FILTER_VALIDATE_URL ) ) { continue; } $method = isset( $event['method'] ) ? sanitize_text_field( $event['method'] ) : ''; $type = isset( $event['type'] ) ? sanitize_text_field( $event['type'] ) : ''; $page_url = isset( $event['pageUrl'] ) ? sanitize_text_field( $event['pageUrl'] ) : ''; if ( ! filter_var( $page_url, FILTER_VALIDATE_URL ) ) { continue; } $domain_details = $this->get_domain_details( $original_url ); $hostname = $domain_details['hostname']; $sld = $domain_details['sld']; $batch_domains[] = $hostname; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $result = $wpdb->insert( $table_name, array( 'original_url' => $original_url, 'hostname' => $hostname, 'second_level_domain' => $sld, 'page_url' => $page_url, 'method' => $method, 'type' => $type, 'intercepted_at' => current_time( 'mysql', true ), ), array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', ) ); if ( $result ) { ++$inserted; } }//end foreach // Process new domains. $batch_domains = array_unique( $batch_domains ); $notified = get_option( 'nsc3ppm_notified_hostnames', array() ); $new_domains = array_diff( $batch_domains, $notified ); $frequency = get_option( 'nsc3ppm_notify_frequency', 'do_not_notify' ); if ( ! empty( $new_domains ) && 'do_not_notify' !== $frequency ) { if ( 'immediate' === $frequency ) { $notify_email = get_option( 'nsc3ppm_notify_email', get_option( 'admin_email' ) ); $subject = 'New hostnames detected'; $message = "The following new hostnames have been detected:\n\n" . implode( "\n", $new_domains ); wp_mail( $notify_email, $subject, $message ); $merged = array_unique( array_merge( $notified, $new_domains ) ); update_option( 'nsc3ppm_notified_hostnames', $merged ); } elseif ( 'daily' === $frequency ) { $existing_new = get_option( 'nsc3ppm_new_hostnames', array() ); $updated_new = array_unique( array_merge( $existing_new, $new_domains ) ); update_option( 'nsc3ppm_new_hostnames', $updated_new ); } } return new WP_REST_Response( array( 'success' => true, 'inserted' => $inserted, ), 200 ); } private function get_interception_data( WP_REST_Request $request ) { $data = $request->get_json_params(); if ( is_array( $data ) === false ) { return array( $data ); } return $data; } private function get_domain_details( $url ) { $host = wp_parse_url( $url, PHP_URL_HOST ); if ( ! $host ) { $host = ''; } $parts = explode( '.', $host ); $sld = $host; if ( count( $parts ) >= 2 ) { $sld = $parts[ ( count( $parts ) - 2 ) ] . '.' . $parts[ ( count( $parts ) - 1 ) ]; } return array( 'hostname' => $host, 'sld' => $sld, ); } }