diff --git a/dist/oribi-tech-sync.zip b/dist/oribi-tech-sync.zip index 75a0610..d11c66a 100644 Binary files a/dist/oribi-tech-sync.zip and b/dist/oribi-tech-sync.zip differ diff --git a/includes/api-client.php b/includes/api-client.php index e604946..ff3c009 100644 --- a/includes/api-client.php +++ b/includes/api-client.php @@ -341,14 +341,15 @@ function oribi_sync_fetch_file( string $api_base, string $branch, string $file_p break; case 'gitea': - $url = $api_base . '/raw/' . $encoded_path . '?ref=' . rawurlencode( $branch ); - $accept = 'text/plain'; + // Use /contents/ endpoint which returns base64-encoded content (more reliable) + $url = $api_base . '/contents/' . $encoded_path . '?ref=' . rawurlencode( $branch ); + $accept = 'application/json'; break; case 'github': default: $url = $api_base . '/contents/' . $encoded_path . '?ref=' . rawurlencode( $branch ); - $accept = 'application/vnd.github.raw+json'; + $accept = 'application/vnd.github.raw'; break; } @@ -377,7 +378,28 @@ function oribi_sync_fetch_file( string $api_base, string $branch, string $file_p ); } - return wp_remote_retrieve_body( $response ); + $body = wp_remote_retrieve_body( $response ); + + // For Gitea, the /contents/ endpoint returns base64-encoded content in JSON + if ( $provider === 'gitea' ) { + $decoded = json_decode( $body, true ); + if ( isset( $decoded['content'] ) && is_string( $decoded['content'] ) ) { + $body = base64_decode( $decoded['content'], true ); + if ( $body === false ) { + return new WP_Error( 'oribi_sync_decode_error', 'Failed to decode base64 content from Gitea' ); + } + } + } + + // Validate and fix encoding if necessary (handles non-UTF-8 sources) + if ( ! empty( $body ) ) { + if ( ! mb_check_encoding( $body, 'UTF-8' ) ) { + // Try to convert from common encodings to UTF-8 + $body = mb_convert_encoding( $body, 'UTF-8', 'UTF-8, ISO-8859-1, Windows-1252' ); + } + } + + return $body; } // ─── Tree filtering ─────────────────────────────────────────────────────────── diff --git a/includes/push-client.php b/includes/push-client.php index 6207692..fc644a1 100644 --- a/includes/push-client.php +++ b/includes/push-client.php @@ -87,17 +87,24 @@ function oribi_sync_api_request( string $method, string $url, array $body, strin $headers = array_merge( oribi_sync_auth_headers( $provider, $pat ), [ - 'Content-Type' => 'application/json', + 'Content-Type' => 'application/json; charset=utf-8', 'Accept' => 'application/json', 'User-Agent' => 'Oribi-Sync-WP/' . ORIBI_SYNC_VERSION, ] ); + // Ensure UTF-8 encoding of all body content + array_walk_recursive( $body, function ( &$item ) { + if ( is_string( $item ) && ! mb_check_encoding( $item, 'UTF-8' ) ) { + $item = mb_convert_encoding( $item, 'UTF-8' ); + } + }); + $args = [ 'method' => $method, 'timeout' => 30, 'headers' => $headers, - 'body' => wp_json_encode( $body ), + 'body' => wp_json_encode( $body, JSON_UNESCAPED_UNICODE ), ]; $response = wp_remote_request( $url, $args );