namespace, '/webhooks/postshiba', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'receive' ), // Public on purpose: the sending service calls this, not a person. The handler checks the request signature and rejects anything unsigned. 'permission_callback' => '__return_true', ) ); } /** * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function receive( $request ) { $secret = Mailpuff_PostShiba::webhook_secret(); if ( ! Mailpuff_PostShiba::connected() || '' === $secret ) { return new WP_Error( 'mailpuff_not_connected', __( 'PostShiba is not connected to this site.', 'mailpuff' ), array( 'status' => 410 ) ); } $body = (string) $request->get_body(); if ( strlen( $body ) > self::MAX_BODY ) { return new WP_Error( 'mailpuff_too_large', __( 'Too large.', 'mailpuff' ), array( 'status' => 413 ) ); } if ( ! self::verify( $body, (string) $request->get_header( 'x_capsule_timestamp' ), (string) $request->get_header( 'x_capsule_signature' ), $secret ) ) { return new WP_Error( 'mailpuff_bad_signature', __( 'Bad signature.', 'mailpuff' ), array( 'status' => 401 ) ); } $events = json_decode( $body, true ); if ( ! is_array( $events ) ) { return new WP_Error( 'mailpuff_bad_body', __( 'Expected JSON.', 'mailpuff' ), array( 'status' => 400 ) ); } if ( isset( $events['event'] ) ) { $events = array( $events ); } $site = Mailpuff_PostShiba::site_key(); $handled = 0; foreach ( array_slice( array_values( $events ), 0, self::MAX_EVENTS ) as $event ) { if ( ! is_array( $event ) || '' === $site || ! hash_equals( $site, self::site_of( $event ) ) ) { continue; } // PostShiba retries and replays: act on each event once. $id = isset( $event['sg_event_id'] ) && is_scalar( $event['sg_event_id'] ) ? (string) $event['sg_event_id'] : wp_json_encode( $event ); $key = 'mailpuff_wh_' . md5( (string) $id ); if ( get_transient( $key ) ) { continue; } set_transient( $key, 1, DAY_IN_SECONDS ); if ( self::apply( $event ) ) { ++$handled; } } return rest_ensure_response( array( 'ok' => true, 'handled' => $handled, ) ); } /** * Whether a request is PostShiba's: a fresh timestamp and a matching signature. * * @param string $body Raw body. * @param string $timestamp X-Capsule-Timestamp. * @param string $signature X-Capsule-Signature. * @param string $secret The endpoint's secret. * @return bool */ public static function verify( $body, $timestamp, $signature, $secret ) { $timestamp = trim( $timestamp ); if ( '' === $secret || ! ctype_digit( $timestamp ) || abs( time() - (int) $timestamp ) > self::TOLERANCE ) { return false; } $given = strtolower( trim( $signature ) ); $given = 0 === strpos( $given, 'sha256=' ) ? substr( $given, 7 ) : $given; $expected = hash_hmac( 'sha256', $timestamp . '.' . $body, $secret ); return '' !== $given && hash_equals( $expected, $given ); } /** * The site key an event carries, in `unique_args` or flattened beside it. * * @param array $event Event. * @return string */ private static function site_of( array $event ) { $args = $event['unique_args'] ?? array(); if ( is_string( $args ) ) { $args = json_decode( $args, true ); } if ( is_array( $args ) && isset( $args['mailpuff_site'] ) && is_scalar( $args['mailpuff_site'] ) ) { return (string) $args['mailpuff_site']; } return isset( $event['mailpuff_site'] ) && is_scalar( $event['mailpuff_site'] ) ? (string) $event['mailpuff_site'] : ''; } /** * Act on one event. * * @param array $event Event. * @return bool Whether it changed anything. */ private static function apply( array $event ) { $email = sanitize_email( (string) ( $event['email'] ?? '' ) ); if ( '' === $email ) { return false; } $name = strtolower( (string) ( $event['event'] ?? '' ) ); $reason = (string) ( $event['reason'] ?? ( $event['response'] ?? '' ) ); switch ( $name ) { case 'bounce': // 'blocked' is this message refused, not the address gone: soft. // Never pass 'block', which the bounce handler treats as hard. $type = 'blocked' === strtolower( (string) ( $event['type'] ?? '' ) ) ? 'soft' : 'hard'; do_action( 'mailpuff_bounce', $email, $type, $reason, 'postshiba' ); return true; case 'spamreport': do_action( 'mailpuff_bounce', $email, 'complaint', $reason, 'postshiba' ); return true; case 'unsubscribe': Mailpuff_Subscription::unsubscribe_address( $email ); return true; case 'dropped': $why = strtolower( $reason ); if ( false !== strpos( $why, 'unsubscrib' ) ) { Mailpuff_Subscription::unsubscribe_address( $email ); return true; } if ( false !== strpos( $why, 'spam' ) ) { do_action( 'mailpuff_bounce', $email, 'complaint', $reason, 'postshiba' ); return true; } if ( false !== strpos( $why, 'bounce' ) ) { do_action( 'mailpuff_bounce', $email, 'hard', $reason, 'postshiba' ); return true; } return false; } return false; } }