$value ) { $line = "define( '" . $key . "', '" . $value . "' );"; $pattern = "/^[ \t]*define\s*\(\s*['\"]" . $key . "['\"]\s*,\s*(?:'(?:[^'\\\\]|\\\\.)*'|\"(?:[^\"\\\\]|\\\\.)*\")\s*\)\s*;[^\n]*$/m"; // A callback, so a "$1" or "\1" inside a random key is never read as a back-reference. $content = preg_replace_callback( $pattern, function () use ( $line ) { return $line; }, $content, 1, $count ); if ( ! $count ) { $missing[] = $line; } } if ( $missing ) { $block = "\n/** AI GuardWall: security keys */\n" . implode( "\n", $missing ) . "\n"; $insert = function ( $m ) use ( $block ) { return $block . $m[0]; }; $content = preg_replace_callback( '/^\s*\$table_prefix\s*=/m', $insert, $content, 1, $count ); if ( ! $count ) { $content = preg_replace_callback( '/\/\*\s*That\'s all, stop editing/', $insert, $content, 1, $count ); } if ( ! $count ) { return new WP_Error( 'svabg_config_shape', __( 'wp-config.php has an unexpected layout; add the keys by hand.', 'ai-guardwall' ) ); } } // Every new key must be in the text exactly as made. $all_in = true; foreach ( $fresh as $key => $value ) { $all_in = $all_in && false !== strpos( $content, "define( '" . $key . "', '" . $value . "' );" ); } if ( ! $all_in || false === strpos( $content, 'wp-settings.php' ) || false === strpos( $content, '$table_prefix' ) ) { return new WP_Error( 'svabg_config_shape', __( 'The rewritten wp-config.php failed a sanity check and was not saved.', 'ai-guardwall' ) ); } $written = self::write( $content, $original ); if ( is_wp_error( $written ) ) { return $written; } Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'tool', __( 'Security keys and salts were rotated; all sessions were ended.', 'ai-guardwall' ) ); return true; } /** * Save new wp-config.php text in place. No temporary file or copy of it is written anywhere: the file is * rewritten under a lock, read back, and put back as it was when the text does not match. * * @since 1.5.1 * @param string $content New text. * @param string $original The text it replaces. * @return true|WP_Error */ public static function write( $content, $original ) { $path = self::path(); if ( '' === $path || ! wp_is_writable( $path ) ) { return new WP_Error( 'svabg_write', __( 'wp-config.php could not be written.', 'ai-guardwall' ) ); } clearstatcache( true, $path ); if ( (string) @file_get_contents( $path ) !== (string) $original ) { return new WP_Error( 'svabg_write', __( 'wp-config.php changed while this was running; nothing was written. Try again.', 'ai-guardwall' ) ); } $free = function_exists( 'disk_free_space' ) ? @disk_free_space( dirname( $path ) ) : false; if ( false !== $free && $free < strlen( $content ) * 4 + 1048576 ) { return new WP_Error( 'svabg_write', __( 'The disk is almost full, so wp-config.php was not changed.', 'ai-guardwall' ) ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- wp-config.php sits outside the folders WP_Filesystem is set up for on many hosts. $ok = @file_put_contents( $path, $content, LOCK_EX ); clearstatcache( true, $path ); if ( false === $ok || (string) @file_get_contents( $path ) !== (string) $content ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents -- puts the original text back. @file_put_contents( $path, $original, LOCK_EX ); return new WP_Error( 'svabg_write', __( 'wp-config.php could not be written, so it was left as it was.', 'ai-guardwall' ) ); } if ( function_exists( 'opcache_invalidate' ) ) { @opcache_invalidate( $path, true ); } return true; } /** * Whether a constant is defined in the file text (not just at runtime). */ public static function defines( $name ) { $path = self::path(); if ( '' === $path || ! is_readable( $path ) ) { return false; } return (bool) preg_match( "/define\s*\(\s*['\"]" . preg_quote( $name, '/' ) . "['\"]/", (string) file_get_contents( $path ) ); } }