.
", keyed with the SHA-256 of * the secret (hex). Refused when the timestamp is more than five minutes off, * or when the same signature was seen before (replay). * * @package aiprofile-inside */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** Option with the secret and the registration state. Not autoloaded. */ define( 'AIPROFILE_INSIDE_SITE_OPTION', 'aiprofile_inside_site' ); /** How far a ping's timestamp may be from our clock, in seconds. */ define( 'AIPROFILE_INSIDE_PING_WINDOW', 300 ); /** * The registration state. * * @return array { secret, key, registered_at, verified_at, error, bust, last_ping } */ function aiprofile_inside_site() { $s = get_option( AIPROFILE_INSIDE_SITE_OPTION, array() ); return is_array( $s ) ? $s : array(); } /** * Change the registration state. * * @param array $changes Keys to set; null removes a key. */ function aiprofile_inside_site_update( $changes ) { // Read it fresh: the hello ping is handled by another request while the // registration request still waits for AiProfile's answer. wp_cache_delete( AIPROFILE_INSIDE_SITE_OPTION, 'options' ); $s = aiprofile_inside_site(); foreach ( $changes as $k => $v ) { if ( null === $v ) { unset( $s[ $k ] ); } else { $s[ $k ] = $v; } } update_option( AIPROFILE_INSIDE_SITE_OPTION, $s, false ); } /** * The cache-busting value of the last ping, or 0. * * @return int */ function aiprofile_inside_bust() { $s = aiprofile_inside_site(); return isset( $s['bust'] ) ? absint( $s['bust'] ) : 0; } /** Does the site need the link: instant updates or the question form on? */ function aiprofile_inside_wants_link() { return (bool) aiprofile_inside_option( 'instant' ) || (bool) aiprofile_inside_option( 'questions' ); } /** Is the link confirmed by AiProfile's hello ping? */ function aiprofile_inside_link_verified() { $s = aiprofile_inside_site(); return ! empty( $s['secret'] ) && ! empty( $s['verified_at'] ); } /** The address of the ping route. */ function aiprofile_inside_ping_url() { return rest_url( 'aiprofile-inside/v1/ping' ); } /** * Register (or register again) with AiProfile. Called from the settings * screen only, after the owner switched instant updates or the question form * on, or connected another profile while one of them is on. * * @return true|string True, or a short reason it failed. */ function aiprofile_inside_register_site() { $settings = aiprofile_inside_settings(); $origin = aiprofile_inside_origin(); if ( empty( $settings['key'] ) || '' === $origin ) { return 'not connected'; } $site = aiprofile_inside_site(); $secret = ! empty( $site['secret'] ) ? (string) $site['secret'] : wp_generate_password( 64, false, false ); // Stored before the call: AiProfile's hello ping can arrive while we wait. aiprofile_inside_site_update( array( 'secret' => $secret, 'key' => $settings['key'], 'verified_at' => null, 'error' => null, ) ); $response = wp_safe_remote_post( $origin . '/api/v1/embed/site', aiprofile_inside_http_args( array( 'timeout' => 10, 'headers' => array( 'Content-Type' => 'application/json', 'Referer' => home_url( '/' ), ), 'body' => wp_json_encode( array( 'key' => $settings['key'], 'siteUrl' => home_url( '/' ), 'pingUrl' => aiprofile_inside_ping_url(), 'secret' => $secret, 'language' => aiprofile_inside_lang( '', '' ), 'pluginVersion' => AIPROFILE_INSIDE_VERSION, ) ), ) ) ); if ( is_wp_error( $response ) ) { aiprofile_inside_site_update( array( 'error' => substr( $response->get_error_message(), 0, 200 ) ) ); return $response->get_error_message(); } $code = (int) wp_remote_retrieve_response_code( $response ); if ( 200 !== $code ) { aiprofile_inside_site_update( array( 'error' => 'HTTP ' . $code ) ); return 'HTTP ' . $code; } aiprofile_inside_site_update( array( 'registered_at' => time() ) ); return true; } /** * Forget the registration, here and at AiProfile. Called when the owner * switches both instant updates and the question form off, and on uninstall. */ function aiprofile_inside_unregister_site() { $site = aiprofile_inside_site(); $origin = aiprofile_inside_origin(); if ( ! empty( $site['secret'] ) && '' !== $origin ) { wp_safe_remote_request( $origin . '/api/v1/embed/site', aiprofile_inside_http_args( array( 'method' => 'DELETE', 'timeout' => 5, 'headers' => array( 'Authorization' => 'Bearer ' . $site['secret'] ), ) ) ); } delete_option( AIPROFILE_INSIDE_SITE_OPTION ); } /** The ping route. Public by design: the signature is the check. */ function aiprofile_inside_register_rest_route() { register_rest_route( 'aiprofile-inside/v1', '/ping', array( 'methods' => 'POST', 'callback' => 'aiprofile_inside_handle_ping', 'permission_callback' => '__return_true', ) ); } add_action( 'rest_api_init', 'aiprofile_inside_register_rest_route' ); /** * Check a ping's signature, time and freshness. * * @param string $secret Our secret. * @param string $timestamp X-AiProfile-Timestamp. * @param string $signature X-AiProfile-Signature ("sha256=