Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit 96e0208

Browse files
committed
Move database directory creation outside of the driver, revamp it
1 parent e4df6b9 commit 96e0208

File tree

2 files changed

+48
-49
lines changed

2 files changed

+48
-49
lines changed

wp-includes/sqlite-ast/class-wp-sqlite-driver.php

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,6 @@ public function __construct( array $options ) {
349349
}
350350
$path = $options['path'];
351351

352-
if ( ':memory:' !== $path && ! is_file( $path ) ) {
353-
$this->prepare_directory();
354-
}
355-
356352
try {
357353
$this->pdo = new PDO( 'sqlite:' . $path );
358354
} catch ( PDOException $e ) {
@@ -2321,51 +2317,6 @@ private function quote_mysql_identifier( string $unquoted_identifier ): string {
23212317
return '`' . str_replace( '`', '``', $unquoted_identifier ) . '`';
23222318
}
23232319

2324-
/**
2325-
* This method makes database directory and .htaccess file.
2326-
*
2327-
* It is executed only once when the installation begins.
2328-
*/
2329-
private function prepare_directory() {
2330-
$u = umask( 0000 );
2331-
if ( ! is_dir( FQDBDIR ) ) {
2332-
if ( ! @mkdir( FQDBDIR, 0704, true ) ) {
2333-
umask( $u );
2334-
wp_die( 'Unable to create the required directory! Please check your server settings.', 'Error!' );
2335-
}
2336-
}
2337-
if ( ! is_writable( FQDBDIR ) ) {
2338-
umask( $u );
2339-
$message = 'Unable to create a file in the directory! Please check your server settings.';
2340-
wp_die( $message, 'Error!' );
2341-
}
2342-
if ( ! is_file( FQDBDIR . '.htaccess' ) ) {
2343-
$fh = fopen( FQDBDIR . '.htaccess', 'w' );
2344-
if ( ! $fh ) {
2345-
umask( $u );
2346-
echo 'Unable to create a file in the directory! Please check your server settings.';
2347-
2348-
return false;
2349-
}
2350-
fwrite( $fh, 'DENY FROM ALL' );
2351-
fclose( $fh );
2352-
}
2353-
if ( ! is_file( FQDBDIR . 'index.php' ) ) {
2354-
$fh = fopen( FQDBDIR . 'index.php', 'w' );
2355-
if ( ! $fh ) {
2356-
umask( $u );
2357-
echo 'Unable to create a file in the directory! Please check your server settings.';
2358-
2359-
return false;
2360-
}
2361-
fwrite( $fh, '<?php // Silence is gold. ?>' );
2362-
fclose( $fh );
2363-
}
2364-
umask( $u );
2365-
2366-
return true;
2367-
}
2368-
23692320
/**
23702321
* Method to clear previous data.
23712322
*/

wp-includes/sqlite/class-wp-sqlite-db.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ public function db_connect( $allow_bail = true ) {
240240
require_once __DIR__ . '/../../wp-includes/sqlite-ast/class-wp-sqlite-driver.php';
241241
require_once __DIR__ . '/../../wp-includes/sqlite-ast/class-wp-sqlite-driver-exception.php';
242242
require_once __DIR__ . '/../../wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php';
243+
$this->ensure_database_directory( FQDB );
243244
$this->dbh = new WP_SQLite_Driver(
244245
array(
245246
'connection' => $pdo,
@@ -442,4 +443,51 @@ public function db_version() {
442443
public function db_server_info() {
443444
return $this->dbh->get_sqlite_version();
444445
}
446+
447+
/**
448+
* Make sure the SQLite database directory exists and is writable.
449+
* Create .htaccess and index.php files to prevent direct access.
450+
*
451+
* @param string $database_path The path to the SQLite database file.
452+
*/
453+
private function ensure_database_directory( string $database_path ) {
454+
$dir = dirname( $database_path );
455+
456+
// Set the umask to 0000 to apply permissions exactly as specified.
457+
// A non-zero umask affects new file and directory permissions.
458+
$umask = umask( 0 );
459+
460+
// Ensure database directory.
461+
if ( ! is_dir( $dir ) ) {
462+
if ( ! @mkdir( $dir, 0700, true ) ) {
463+
wp_die( sprintf( 'Failed to create database directory: %s', $dir ), 'Error!' );
464+
}
465+
}
466+
if ( ! is_writable( $dir ) ) {
467+
wp_die( sprintf( 'Database directory is not writable: %s', $dir ), 'Error!' );
468+
}
469+
470+
// Ensure .htaccess file to prevent direct access.
471+
$path = $dir . DIRECTORY_SEPARATOR . '.htaccess';
472+
if ( ! is_file( $path ) ) {
473+
$result = file_put_contents( $path, 'DENY FROM ALL', LOCK_EX );
474+
if ( false === $result ) {
475+
wp_die( sprintf( 'Failed to create file: %s', $path ), 'Error!' );
476+
}
477+
chmod( $path, 0600 );
478+
}
479+
480+
// Ensure index.php file to prevent direct access.
481+
$path = $dir . DIRECTORY_SEPARATOR . 'index.php';
482+
if ( ! is_file( $path ) ) {
483+
$result = file_put_contents( $path, '<?php // Silence is gold. ?>', LOCK_EX );
484+
if ( false === $result ) {
485+
wp_die( sprintf( 'Failed to create file: %s', $path ), 'Error!' );
486+
}
487+
chmod( $path, 0600 );
488+
}
489+
490+
// Restore the original umask value.
491+
umask( $umask );
492+
}
445493
}

0 commit comments

Comments
 (0)