access = (string) CFSP_Settings::get( 'r2_access_key' );
$this->secret = (string) CFSP_Settings::get( 'r2_secret_key' );
$this->bucket = (string) CFSP_Settings::get( 'r2_bucket' );
$custom = (string) CFSP_Settings::get( 'r2_endpoint' );
$account = (string) CFSP_Settings::get( 'r2_account_id' );
$this->endpoint = untrailingslashit( $custom ? $custom : 'https://' . $account . '.r2.cloudflarestorage.com' );
}
public static function is_configured() {
return CFSP_Settings::get( 'r2_access_key' ) && CFSP_Settings::get( 'r2_secret_key' ) && CFSP_Settings::get( 'r2_bucket' )
&& ( CFSP_Settings::get( 'r2_account_id' ) || CFSP_Settings::get( 'r2_endpoint' ) );
}
/* ------------------------------------------------------------------ *
* Signing (verified against AWS's published SigV4 example)
* ------------------------------------------------------------------ */
private static function uri_encode_path( $path ) {
return implode( '/', array_map( 'rawurlencode', explode( '/', $path ) ) );
}
/**
* Build a signed request.
*
* @return array [ 'url' => ..., 'headers' => [...] ]
*/
private function signed( $method, $key, array $query = array(), $payload_hash = null, array $extra_headers = array() ) {
$host = (string) wp_parse_url( $this->endpoint, PHP_URL_HOST );
$base_path = rtrim( (string) wp_parse_url( $this->endpoint, PHP_URL_PATH ), '/' );
$path = $base_path . '/' . $this->bucket . ( '' !== $key ? '/' . ltrim( $key, '/' ) : '' );
$canon_uri = self::uri_encode_path( $path );
$payload_hash = $payload_hash ? $payload_hash : hash( 'sha256', '' );
$amz_date = gmdate( 'Ymd\THis\Z' );
$date = substr( $amz_date, 0, 8 );
ksort( $query );
$pairs = array();
foreach ( $query as $k => $v ) {
$pairs[] = rawurlencode( $k ) . '=' . rawurlencode( (string) $v );
}
$canon_query = implode( '&', $pairs );
$headers = array();
foreach ( $extra_headers as $name => $value ) {
$headers[ strtolower( $name ) ] = trim( (string) $value );
}
$headers['host'] = $host;
$headers['x-amz-content-sha256'] = $payload_hash;
$headers['x-amz-date'] = $amz_date;
ksort( $headers );
$canon_headers = '';
foreach ( $headers as $name => $value ) {
$canon_headers .= $name . ':' . $value . "\n";
}
$signed_headers = implode( ';', array_keys( $headers ) );
$canonical = implode( "\n", array( $method, $canon_uri, $canon_query, $canon_headers, $signed_headers, $payload_hash ) );
$scope = $date . '/' . self::REGION . '/' . self::SERVICE . '/aws4_request';
$to_sign = implode( "\n", array( 'AWS4-HMAC-SHA256', $amz_date, $scope, hash( 'sha256', $canonical ) ) );
$k = hash_hmac( 'sha256', $date, 'AWS4' . $this->secret, true );
$k = hash_hmac( 'sha256', self::REGION, $k, true );
$k = hash_hmac( 'sha256', self::SERVICE, $k, true );
$k = hash_hmac( 'sha256', 'aws4_request', $k, true );
$signature = hash_hmac( 'sha256', $to_sign, $k );
$headers['authorization'] = sprintf( 'AWS4-HMAC-SHA256 Credential=%s/%s, SignedHeaders=%s, Signature=%s', $this->access, $scope, $signed_headers, $signature );
unset( $headers['host'] ); // Set by the HTTP client from the URL.
$scheme = (string) wp_parse_url( $this->endpoint, PHP_URL_SCHEME );
$port = wp_parse_url( $this->endpoint, PHP_URL_PORT );
return array(
'url' => $scheme . '://' . $host . ( $port ? ':' . $port : '' ) . $canon_uri . ( $canon_query ? '?' . $canon_query : '' ),
'headers' => $headers,
);
}
private static function error_from( $code, $body ) {
$message = 'R2 HTTP ' . $code;
if ( preg_match( '#(.*?)#', (string) $body, $c ) ) {
$message .= ': ' . $c[1];
if ( preg_match( '#(.*?)#', (string) $body, $m ) ) {
$message .= ' (' . $m[1] . ')';
}
}
return new WP_Error( 'cfsp_r2', $message, array( 'status' => $code ) );
}
/* ------------------------------------------------------------------ *
* Operations
* ------------------------------------------------------------------ */
/**
* R2 rejects a PUT without Content-Length (HTTP 411). cURL leaves it out when the body is
* empty (e.g. a 0-byte index.html placeholder), so always send it explicitly.
*/
private static function with_length( array $headers, $body ) {
$headers['Content-Length'] = (string) strlen( (string) $body );
return $headers;
}
/** Upload one object. */
public function put( $key, $body, $content_type ) {
$req = $this->signed( 'PUT', $key, array(), hash( 'sha256', $body ), self::object_headers( $content_type ) );
$response = wp_remote_request(
$req['url'],
array(
'method' => 'PUT',
'headers' => self::with_length( $req['headers'], $body ),
'body' => $body,
'timeout' => 120,
)
);
if ( is_wp_error( $response ) ) {
return $response;
}
$code = (int) wp_remote_retrieve_response_code( $response );
return 200 === $code ? true : self::error_from( $code, wp_remote_retrieve_body( $response ) );
}
public function delete( $key ) {
$req = $this->signed( 'DELETE', $key );
$response = wp_remote_request(
$req['url'],
array(
'method' => 'DELETE',
'headers' => $req['headers'],
'timeout' => 30,
)
);
if ( is_wp_error( $response ) ) {
return $response;
}
$code = (int) wp_remote_retrieve_response_code( $response );
return in_array( $code, array( 200, 204, 404 ), true ) ? true : self::error_from( $code, wp_remote_retrieve_body( $response ) );
}
/** Check the bucket is reachable and readable (lists at most one key). */
public function test_read() {
$req = $this->signed( 'GET', '', array( 'list-type' => '2', 'max-keys' => '1' ) );
$response = wp_remote_get(
$req['url'],
array(
'headers' => $req['headers'],
'timeout' => 20,
)
);
if ( is_wp_error( $response ) ) {
return $response;
}
$code = (int) wp_remote_retrieve_response_code( $response );
return 200 === $code ? true : self::error_from( $code, wp_remote_retrieve_body( $response ) );
}
/**
* One page of object keys (ListObjectsV2), in key order.
*
* Paging uses start-after (the last key seen) instead of a continuation token, so deleting
* objects between pages can never make the listing skip any.
*
* @return array|WP_Error [ 'keys' => string[], 'next' => key to start after, or null when done ]
*/
public function list_objects( $prefix, $after = null, $max = 1000 ) {
$query = array(
'list-type' => '2',
'max-keys' => (string) $max,
'prefix' => $prefix,
);
if ( $after ) {
$query['start-after'] = $after;
}
$req = $this->signed( 'GET', '', $query );
$response = wp_remote_get(
$req['url'],
array(
'headers' => $req['headers'],
'timeout' => 60,
)
);
if ( is_wp_error( $response ) ) {
return $response;
}
$code = (int) wp_remote_retrieve_response_code( $response );
$body = (string) wp_remote_retrieve_body( $response );
if ( 200 !== $code ) {
return self::error_from( $code, $body );
}
preg_match_all( '#(.*?)#s', $body, $m );
$keys = array_map(
function ( $k ) {
return html_entity_decode( $k, ENT_QUOTES | ENT_XML1, 'UTF-8' );
},
$m[1]
);
$more = preg_match( '#true#i', $body ) && $keys;
return array(
'keys' => $keys,
'next' => $more ? end( $keys ) : null,
);
}
/**
* Delete up to 1,000 objects in one request (DeleteObjects).
*
* @return array|WP_Error [ 'deleted' => int, 'errors' => [ key => message ] ]
*/
public function delete_many( array $keys ) {
$keys = array_slice( array_values( $keys ), 0, 1000 );
if ( ! $keys ) {
return array( 'deleted' => 0, 'errors' => array() );
}
$xml = 'true';
foreach ( $keys as $key ) {
$xml .= '';
}
$xml .= '';
$req = $this->signed(
'POST',
'',
array( 'delete' => '' ),
hash( 'sha256', $xml ),
array(
'content-md5' => base64_encode( md5( $xml, true ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions
'content-type' => 'application/xml',
)
);
$response = wp_remote_post(
$req['url'],
array(
'headers' => $req['headers'],
'body' => $xml,
'timeout' => 120,
)
);
if ( is_wp_error( $response ) ) {
return $response;
}
$code = (int) wp_remote_retrieve_response_code( $response );
$body = (string) wp_remote_retrieve_body( $response );
if ( 200 !== $code ) {
return self::error_from( $code, $body );
}
// Quiet mode: the response lists only the keys that failed.
$errors = array();
if ( preg_match_all( '#.*?(.*?).*?(.*?).*?#s', $body, $m, PREG_SET_ORDER ) ) {
foreach ( $m as $e ) {
$errors[ html_entity_decode( $e[1], ENT_QUOTES | ENT_XML1, 'UTF-8' ) ] = $e[2];
}
}
return array(
'deleted' => count( $keys ) - count( $errors ),
'errors' => $errors,
);
}
/** File data held in memory per round of parallel uploads (put_many() reads each file whole). */
const ROUND_BYTES = 67108864; // 64 MiB.
/**
* Upload files in rounds of up to $parallel at once, through WordPress's HTTP library (Requests).
* A round holds at most ROUND_BYTES of file data, so a few large files go in a smaller round.
*
* @param array $files [ id => [ 'key', 'path', 'type', 'size' ] ], uploaded in this order.
* @param int $parallel Uploads per round.
* @param callable $keep_going Called before each round; return false to stop starting more.
* @param callable $on_result Called as fn( $id, true|WP_Error ) when each upload ends.
* @return string[] Ids that were started (each gets exactly one on_result call).
*/
public function put_rolling( array $files, $parallel, callable $keep_going, callable $on_result ) {
$parallel = max( 1, (int) $parallel );
$started = array();
$ids = array_keys( $files );
$next = 0;
while ( $next < count( $ids ) && call_user_func( $keep_going ) ) {
$round = array();
$bytes = 0;
while ( $next < count( $ids ) && count( $round ) < $parallel ) {
$id = $ids[ $next ];
$size = (int) $files[ $id ]['size'];
if ( $round && $bytes + $size > self::ROUND_BYTES ) {
break;
}
$round[ $id ] = $files[ $id ];
$bytes += $size;
$started[] = (string) $id;
++$next;
}
foreach ( $this->put_many( $round ) as $id => $result ) {
call_user_func( $on_result, $id, $result );
}
}
return $started;
}
/**
* Upload several files in parallel.
*
* @param array $files [ id => [ 'key' => ..., 'path' => absolute file path, 'type' => content type ] ]
* @return array [ id => true|WP_Error ]
*/
public function put_many( array $files ) {
$requests = array();
$results = array();
foreach ( $files as $id => $f ) {
$body = @file_get_contents( $f['path'] ); // phpcs:ignore WordPress.PHP.NoSilencedErrors
if ( false === $body ) {
$results[ $id ] = new WP_Error( 'cfsp_r2', 'Could not read ' . $f['path'] );
continue;
}
$req = $this->signed( 'PUT', $f['key'], array(), hash( 'sha256', $body ), self::object_headers( $f['type'] ) );
$requests[ $id ] = array(
'url' => $req['url'],
'headers' => self::with_length( $req['headers'], $body ),
'data' => $body,
'type' => 'PUT',
);
}
if ( ! $requests ) {
return $results;
}
$class = class_exists( '\WpOrg\Requests\Requests' ) ? '\WpOrg\Requests\Requests' : ( class_exists( 'Requests' ) ? 'Requests' : null );
if ( ! $class ) {
// No parallel transport: upload one by one.
foreach ( $requests as $id => $r ) {
$results[ $id ] = $this->put( $files[ $id ]['key'], $r['data'], $files[ $id ]['type'] );
}
return $results;
}
$responses = call_user_func(
array( $class, 'request_multiple' ),
$requests,
array(
'timeout' => 120,
'connect_timeout' => 15,
'verify' => true,
'useragent' => 'Static-Push/' . CFSP_VERSION,
)
);
foreach ( $requests as $id => $unused ) {
$r = $responses[ $id ] ?? null;
if ( $r instanceof \Exception ) {
$results[ $id ] = new WP_Error( 'cfsp_r2', $r->getMessage() );
} elseif ( is_object( $r ) && isset( $r->status_code ) ) {
$results[ $id ] = 200 === (int) $r->status_code ? true : self::error_from( (int) $r->status_code, $r->body ?? '' );
} else {
$results[ $id ] = new WP_Error( 'cfsp_r2', 'No response' );
}
}
return $results;
}
private static function object_headers( $content_type ) {
$headers = array( 'content-type' => $content_type ? $content_type : 'application/octet-stream' );
$cache = trim( (string) CFSP_Settings::get( 'r2_cache_control' ) );
if ( '' !== $cache ) {
$headers['cache-control'] = $cache;
}
return $headers;
}
}