'POST', 'callback' => array( __CLASS__, 'sync' ), 'permission_callback' => '__return_true', // Authenticated by HMAC signature below. ) ); register_rest_route( self::NS, '/strings', array( 'methods' => 'GET', 'callback' => array( __CLASS__, 'list_strings' ), 'permission_callback' => array( __CLASS__, 'can_manage' ), 'args' => array( 'lang' => array( 'required' => true, 'validate_callback' => array( Languages::class, 'exists' ), ), 'status' => array( 'default' => '' ), 'search' => array( 'default' => '' ), 'page' => array( 'default' => 1 ), ), ) ); register_rest_route( self::NS, '/strings/(?P\d+)', array( array( 'methods' => 'POST', 'callback' => array( __CLASS__, 'save_string' ), 'permission_callback' => array( __CLASS__, 'can_edit' ), ), array( 'methods' => 'DELETE', 'callback' => array( __CLASS__, 'delete_string' ), 'permission_callback' => array( __CLASS__, 'can_manage' ), ), ) ); } public static function can_manage() { return current_user_can( 'manage_options' ); } public static function can_edit() { return current_user_can( 'manage_options' ); } /** * Verify X-LF-Signature = HMAC_SHA256(push_secret, "{timestamp}.{body}"). */ public static function sync( \WP_REST_Request $request ) { $conn = Settings::connection(); $secret = (string) ( $conn['push_secret'] ?? '' ); $timestamp = (string) $request->get_header( 'x_lf_timestamp' ); $signature = strtolower( (string) $request->get_header( 'x_lf_signature' ) ); $body = (string) $request->get_body(); if ( '' === $secret || '' === $timestamp || '' === $signature || ! ctype_digit( $timestamp ) || abs( time() - (int) $timestamp ) > 300 ) { return new \WP_Error( 'langferry_unauthorized', 'Invalid signature.', array( 'status' => 401 ) ); } $expected = hash_hmac( 'sha256', $timestamp . '.' . $body, $secret ); if ( ! hash_equals( $expected, $signature ) ) { return new \WP_Error( 'langferry_unauthorized', 'Invalid signature.', array( 'status' => 401 ) ); } $payload = json_decode( $body, true ); $event = (string) ( $payload['event'] ?? '' ); if ( 'site.verify' === $event ) { // Proves this plugin, at this address, holds the site's secret (no DNS record needed). $nonce = (string) ( $payload['nonce'] ?? '' ); if ( '' === $nonce || strlen( $nonce ) > 128 ) { return new \WP_Error( 'langferry_bad_request', 'Missing nonce.', array( 'status' => 400 ) ); } return rest_ensure_response( array( 'ok' => true, 'proof' => hash_hmac( 'sha256', 'verify.' . $nonce, $secret ), ) ); } if ( 'site.disconnected' === $event ) { Settings::clear_connection(); return rest_ensure_response( array( 'ok' => true ) ); } // Clear pauses so new languages / quota take effect immediately. delete_transient( 'langferry_quota_exceeded' ); Translator::clear_plan_pauses(); foreach ( Settings::effective()['languages'] as $lang ) { delete_transient( 'langferry_pause_' . $lang ); } $result = Cloud::sync(); if ( is_wp_error( $result ) ) { return new \WP_Error( 'langferry_sync_failed', $result->get_error_message(), array( 'status' => 502 ) ); } return rest_ensure_response( array( 'ok' => true, 'config_version' => Settings::config_version(), ) ); } public static function list_strings( \WP_REST_Request $request ) { $result = Translation_Memory::search( array( 'lang' => $request['lang'], 'status' => (string) $request['status'], 'search' => sanitize_text_field( (string) $request['search'] ), 'page' => (int) $request['page'], 'per_page' => 50, ) ); return rest_ensure_response( $result ); } public static function save_string( \WP_REST_Request $request ) { $row = Translation_Memory::get( (int) $request['id'] ); if ( ! $row ) { return new \WP_Error( 'langferry_not_found', __( 'String not found.', 'langferry' ), array( 'status' => 404 ) ); } $translation = trim( (string) $request->get_param( 'translation' ) ); if ( '' === $translation ) { return new \WP_Error( 'langferry_empty', __( 'Translation cannot be empty.', 'langferry' ), array( 'status' => 422 ) ); } $check = self::validate_placeholders( $row['source'], $translation ); if ( is_wp_error( $check ) ) { return $check; } // Only the placeholder tags from the source are allowed; strip anything else. $translation = wp_kses( $translation, self::allowed_placeholder_tags( $row['source'] ) ); Translation_Memory::save_manual( (int) $row['id'], $translation ); return rest_ensure_response( Translation_Memory::get( (int) $row['id'] ) ); } public static function delete_string( \WP_REST_Request $request ) { Translation_Memory::delete( (int) $request['id'] ); return rest_ensure_response( array( 'deleted' => true ) ); } /** * Every placeholder in the source must appear exactly once. */ public static function validate_placeholders( $source, $translation ) { preg_match_all( '/<([a-z0-9]+)\s+i="(\d+)"/i', $source, $src ); preg_match_all( '/<([a-z0-9]+)\s+i="(\d+)"/i', $translation, $dst ); $a = $src[2]; $b = $dst[2]; sort( $a ); sort( $b ); if ( $a !== $b ) { return new \WP_Error( 'langferry_placeholders', __( 'Keep every tag from the original (like …) exactly once in your translation.', 'langferry' ), array( 'status' => 422 ) ); } return true; } private static function allowed_placeholder_tags( $source ) { $allowed = array(); preg_match_all( '/<([a-z0-9]+)\s+i="\d+"/i', $source, $m ); foreach ( array_unique( array_map( 'strtolower', $m[1] ) ) as $tag ) { $allowed[ $tag ] = array( 'i' => true, 'translate' => true, ); } return $allowed; } }