namespace, '/track/open/(?P[a-zA-Z0-9]+)', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'track_open' ), // Public on purpose: an open pixel and a click link load from a reader's mail app, signed in or not. The token names one send; nothing else is read or changed. 'permission_callback' => '__return_true', ) ); register_rest_route( $this->namespace, '/track/click/(?P[a-zA-Z0-9]+)', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'track_click' ), // Public on purpose: an open pixel and a click link load from a reader's mail app, signed in or not. The token names one send; nothing else is read or changed. 'permission_callback' => '__return_true', ) ); } /** * GET /track/open/:token — record an email open and return a 1x1 transparent GIF. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function track_open( $request ) { global $wpdb; $token = sanitize_text_field( $request->get_param( 'token' ) ); if ( empty( $token ) ) { return $this->serve_pixel(); } $tracking_table = Mailpuff_Schema::table( Mailpuff_Schema::TRACKING ); $batch_id = absint( $request->get_param( 'bid' ) ); $contact_id = Mailpuff_Tracking::contact_for_token( $token, $batch_id ); if ( ! $contact_id ) { return $this->serve_pixel(); } $contact = (object) array( 'id' => $contact_id ); if ( $batch_id ) { // Scope the open to a real send: the send job creates the tracking // row for (batch, contact) only when the email actually went out. // We therefore only UPDATE an existing row — never insert one from // this public endpoint — so a leaked token + guessed bid can't // fabricate opens for pairs that were never mailed. $tracking = $wpdb->get_row( $wpdb->prepare( 'SELECT id, opened_at FROM %i WHERE batch_id = %d AND contact_id = %d', $tracking_table, $batch_id, $contact->id ) ); if ( $tracking ) { // Set opened_at only on first open. if ( null === $tracking->opened_at ) { $wpdb->query( $wpdb->prepare( 'UPDATE %i SET opened_at = %s, open_count = open_count + 1 WHERE id = %d', $tracking_table, current_time( 'mysql' ), $tracking->id ) ); } else { $wpdb->query( $wpdb->prepare( 'UPDATE %i SET open_count = open_count + 1 WHERE id = %d', $tracking_table, $tracking->id ) ); } /** * A recipient opened an email (the pixel loaded). * * @param int $batch_id Batch ID. * @param int $contact_id Contact ID. * @param bool $first True for the first open of this batch. */ do_action( 'mailpuff_email_opened', $batch_id, (int) $contact->id, null === $tracking->opened_at ); } } return $this->serve_pixel(); } /** * GET /track/click/:token — record a link click and 302 to the destination. * * The destination is carried in `u` and HMAC-signed in `s` (see * Mailpuff_Tracking::click_sig). We refuse to redirect unless the signature * verifies, so the endpoint can't be turned into an open redirect. The click is * only counted for a real (batch, contact) send — mirroring the open pixel. * * @param WP_REST_Request $request Request object. * @return void */ public function track_click( $request ) { $url = $this->resolve_click( (string) $request->get_param( 'u' ), absint( $request->get_param( 'bid' ) ), sanitize_text_field( $request->get_param( 'token' ) ), sanitize_text_field( (string) $request->get_param( 's' ) ) ); // Unsigned, tampered, or non-http(s) → never forward; bounce home instead. if ( '' === $url ) { wp_safe_redirect( home_url( '/' ) ); exit; } // Signature verified the URL is one we generated, so an external redirect is safe. wp_redirect( esc_url_raw( $url ) ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect exit; } /** * Verify a click link and count the click. * * @param string $url The `u` parameter. * @param int $bid Batch ID. * @param string $token Recipient token. * @param string $sig The `s` parameter. * @return string The URL to forward to, or '' when the link does not verify. */ public function resolve_click( $url, $bid, $token, $sig ) { global $wpdb; $url = self::verified_click_url( $url, $bid, $token, $sig ); if ( '' === $url ) { return ''; } // Count the click only for a recipient who was actually mailed in this batch. if ( $bid && '' !== $token ) { $contact_id = Mailpuff_Tracking::contact_for_token( $token, $bid ); $contact = $contact_id ? (object) array( 'id' => $contact_id ) : null; if ( $contact && Mailpuff_Tracking::find_by_batch_contact( $bid, $contact->id ) ) { Mailpuff_Tracking::record_click( $bid, $contact->id ); Mailpuff_Tracking::record_link_click( $bid, $contact->id, $url ); /** * A recipient clicked a tracked link. * * @param int $batch_id Batch ID. * @param int $contact_id Contact ID. * @param string $url The destination. */ do_action( 'mailpuff_link_clicked', $bid, (int) $contact->id, $url ); } } return $url; } /** * The destination a click link may forward to, or '' when it must not. * * New links carry the URL percent-encoded once (`rawurlencode`), which the * REST layer has already decoded, so the signature is checked against the * value as received. Links sent before that change carried the raw URL; the * same check covers them. A value that was encoded twice on the way (a proxy * or a mail client) is tried once more decoded. * * @param string $url The `u` parameter. * @param int $bid Batch ID. * @param string $token Recipient token. * @param string $sig The `s` parameter. * @return string The URL to forward to, or ''. */ public static function verified_click_url( $url, $bid, $token, $sig ) { $candidates = array( (string) $url, rawurldecode( (string) $url ) ); foreach ( $candidates as $candidate ) { if ( '' !== $candidate && preg_match( '#^https?://#i', $candidate ) && hash_equals( Mailpuff_Tracking::click_sig( $bid, $token, $candidate ), (string) $sig ) ) { return $candidate; } } return ''; } /** * Return a 1x1 transparent GIF as a REST response. * * @return WP_REST_Response */ private function serve_pixel() { // A transparent 1x1 GIF, byte for byte: the header "GIF89a", a 1x1 // screen, a two-colour table, a transparency extension and one pixel. $gif = "\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\xff\xff\xff\x21\xf9\x04\x01\x00\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x01\x44\x00\x3b"; header( 'Content-Type: image/gif' ); header( 'Content-Length: ' . strlen( $gif ) ); header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' ); header( 'Pragma: no-cache' ); header( 'Expires: Mon, 01 Jan 1990 00:00:00 GMT' ); echo $gif; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped exit; } }