Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/WP_SQLite_Driver_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -11363,6 +11363,42 @@ public function testVersionFunction(): void {
$this->assertSame( '8.0.38', $result[0]->{'VERSION()'} );
}

public function testFromBase64Function(): void {
// Basic decoding.
$result = $this->assertQuery( "SELECT FROM_BASE64('SGVsbG8gV29ybGQ=') AS decoded" );
$this->assertSame( 'Hello World', $result[0]->decoded );

// Empty string.
$result = $this->assertQuery( "SELECT FROM_BASE64('') AS decoded" );
$this->assertSame( '', $result[0]->decoded );

// NULL input returns NULL.
$result = $this->assertQuery( 'SELECT FROM_BASE64(NULL) AS decoded' );
$this->assertNull( $result[0]->decoded );

// Binary data round-trip.
$result = $this->assertQuery( "SELECT FROM_BASE64(TO_BASE64('binary\\0data')) AS decoded" );
$this->assertSame( "binary\0data", $result[0]->decoded );
}

public function testToBase64Function(): void {
// Basic encoding.
$result = $this->assertQuery( "SELECT TO_BASE64('Hello World') AS encoded" );
$this->assertSame( 'SGVsbG8gV29ybGQ=', $result[0]->encoded );

// Empty string.
$result = $this->assertQuery( "SELECT TO_BASE64('') AS encoded" );
$this->assertSame( '', $result[0]->encoded );

// NULL input returns NULL.
$result = $this->assertQuery( 'SELECT TO_BASE64(NULL) AS encoded' );
$this->assertNull( $result[0]->encoded );

// Round-trip: TO_BASE64(FROM_BASE64(x)) = x.
$result = $this->assertQuery( "SELECT TO_BASE64(FROM_BASE64('dGVzdA==')) AS encoded" );
$this->assertSame( 'dGVzdA==', $result[0]->encoded );
}

public function testSubstringFunction(): void {
$result = $this->assertQuery( "SELECT SUBSTRING('abcdef', 1, 3) AS s" );
$this->assertSame( 'abc', $result[0]->s );
Expand Down
40 changes: 40 additions & 0 deletions wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public static function register_for( $pdo ): self {
'ucase' => 'ucase',
'lcase' => 'lcase',
'unhex' => 'unhex',
'from_base64' => 'from_base64',
'to_base64' => 'to_base64',
'inet_ntoa' => 'inet_ntoa',
'inet_aton' => 'inet_aton',
'datediff' => 'datediff',
Expand Down Expand Up @@ -667,6 +669,44 @@ public function unhex( $number ) {
return pack( 'H*', $number );
}

/**
* Method to emulate MySQL FROM_BASE64() function.
*
* Takes a base64-encoded string and returns the decoded result as a binary
* string. Returns NULL if the argument is NULL or is not a valid base64 string.
*
* @param string|null $str The base64-encoded string.
*
* @return string|null Decoded binary string, or NULL.
*/
public function from_base64( $str ) {
if ( null === $str ) {
return null;
}
$decoded = base64_decode( $str, true );
if ( false === $decoded ) {
return null;
}
return $decoded;
}

/**
* Method to emulate MySQL TO_BASE64() function.
*
* Takes a string and returns a base64-encoded result.
* Returns NULL if the argument is NULL.
*
* @param string|null $str The string to encode.
*
* @return string|null Base64-encoded string, or NULL.
*/
public function to_base64( $str ) {
if ( null === $str ) {
return null;
}
return base64_encode( $str );
}

/**
* Method to emulate MySQL INET_NTOA() function.
*
Expand Down
Loading