Skip to content

Commit 4767d47

Browse files
authored
S3 Storage, fix using tmpfile()
1 parent 9305e9d commit 4767d47

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

dt-core/dt-storage.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -368,28 +368,46 @@ public static function validate_connection_settings(): array {
368368
return $result;
369369
}
370370

371+
$temp_path = tempnam( sys_get_temp_dir(), 'dt-storage-validate' );
372+
if ( empty( $temp_path ) ) {
373+
$result['error_message'] = 'Unable to create temporary file for validation.';
374+
return $result;
375+
}
376+
377+
$write_ok = file_put_contents( $temp_path, self::generate_random_string( 24 ) );
378+
if ( $write_ok === false ) {
379+
@unlink( $temp_path );
380+
$result['error_message'] = 'Unable to write to temporary file for validation.';
381+
return $result;
382+
}
383+
384+
$body_handle = fopen( $temp_path, 'r' );
385+
if ( $body_handle === false ) {
386+
@unlink( $temp_path );
387+
$result['error_message'] = 'Unable to read temporary file for validation.';
388+
return $result;
389+
}
390+
371391
try {
372392
// Attempt to upload an empty dummy file.
373393
$key = 'validated';
374-
$dummy_file = tmpfile();
375-
fwrite( $dummy_file, self::generate_random_string( 24 ) );
376-
rewind( $dummy_file );
377-
$dummy_file_metadata = stream_get_meta_data( $dummy_file );
378-
379394
$client->putObject([
380395
'Bucket' => $bucket,
381396
'Key' => $key,
382-
'Body' => fopen( $dummy_file_metadata['uri'], 'r' ),
397+
'Body' => $body_handle,
383398
'ContentType' => 'text/plain'
384399
]);
385400

386-
fclose( $dummy_file );
387-
388401
$result['valid'] = true;
389402
$result['error_message'] = '';
390403

391404
} catch ( Throwable $e ) {
392405
$result['error_message'] = $e->getMessage();
406+
} finally {
407+
if ( is_resource( $body_handle ) ) {
408+
fclose( $body_handle );
409+
}
410+
@unlink( $temp_path );
393411
}
394412

395413
return $result;

0 commit comments

Comments
 (0)