diff --git a/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php b/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php index d62c4e48..9e11b5c8 100644 --- a/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php +++ b/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php @@ -7,6 +7,7 @@ use function EE\Site\Utils\get_container_file_contents; use function EE\Site\Utils\put_container_file_contents; use function EE\Site\Utils\site_php_conf_dir; +use function EE\Site\Utils\strip_session_sid_settings; /** * Removes session.sid_length and session.sid_bits_per_character, deprecated as of PHP 8.4, from the php.ini of PHP 8.4 and 8.5 sites. @@ -21,45 +22,6 @@ class DropDeprecatedSessionSidSettings extends Base { /** `latest` sites run the php8.4 image. */ const PHP_VERSIONS = [ '8.4', '8.5', 'latest' ]; - /** Values EasyEngine shipped; any other value was set by the user. */ - const SHIPPED_VALUES = [ - 'session.sid_length' => '26', - 'session.sid_bits_per_character' => '5', - ]; - - /** Comments shipped right above each directive, removed together with it. */ - const SHIPPED_COMMENTS = [ - 'session.sid_length' => [ - '; Set session ID character length. This value could be between 22 to 256.', - '; Shorter length than default is supported only for compatibility reason.', - '; Users should use 32 or more chars.', - '; http://php.net/session.sid-length', - '; Default Value: 32', - '; Development Value: 26', - '; Production Value: 26', - ], - 'session.sid_bits_per_character' => [ - '; Define how many bits are stored in each character when converting', - '; the binary hash data to something readable.', - '; Possible values:', - '; 4 (4 bits: 0-9, a-f)', - '; 5 (5 bits: 0-9, a-v)', - '; 6 (6 bits: 0-9, a-z, A-Z, "-", ",")', - '; Default Value: 4', - '; Development Value: 5', - '; Production Value: 5', - '; http://php.net/session.hash-bits-per-character', - ], - ]; - - /** Entry of the quick reference at the top of the shipped php.ini. */ - const SHIPPED_QUICK_REFERENCE = [ - '; session.sid_bits_per_character', - '; Default Value: 4', - '; Development Value: 5', - '; Production Value: 5', - ]; - private $sites; /** @var array Files changed by up(), restored by down(). */ @@ -151,7 +113,7 @@ private function migrate_site( $site ) { $dir = site_php_conf_dir( $site->site_fs_path, 'php' ); $content = get_container_file_contents( $ini, $dir, self::MAX_SIZE ); - list( $new_content, $removed ) = self::strip_settings( $content ); + list( $new_content, $removed ) = strip_session_sid_settings( $content ); if ( $new_content === $content ) { EE::debug( "$ini has no deprecated session ID settings." ); @@ -201,88 +163,6 @@ private function remove_backup( $backup ) { @rmdir( dirname( $backup, 2 ) ); } - /** - * Removes the two directives, wherever they are set, and the shipped comments that document them. - * Every other line is kept byte for byte. - * - * @param string $content php.ini content. - * - * @return array New content, and the removed directive lines => whether the value is the shipped one. - * - * @throws \Exception When the edit would change how PHP reads any other setting, or the file doesn't parse. - */ - public static function strip_settings( $content ) { - - // After every LF, and after a CR not followed by LF, so a CR-only file isn't read as one line. - $lines = preg_split( '/(?<=\n)|(?<=\r)(?!\n)/', $content, -1, PREG_SPLIT_NO_EMPTY ); - $norm = array_map( - function ( $line ) { - return rtrim( $line, " \t\r\n" ); - }, - $lines - ); - $drop = []; - $removed = []; - - $quick_count = count( self::SHIPPED_QUICK_REFERENCE ); - foreach ( array_keys( $norm ) as $i ) { - if ( array_slice( $norm, $i, $quick_count ) === self::SHIPPED_QUICK_REFERENCE ) { - $drop += array_fill( $i, $quick_count, true ); - if ( isset( $norm[ $i + $quick_count ] ) && '' === $norm[ $i + $quick_count ] ) { - $drop[ $i + $quick_count ] = true; - } - } - } - - foreach ( $norm as $i => $line ) { - if ( ! preg_match( '/^\s*(session\.sid_length|session\.sid_bits_per_character)\s*=(.*)$/', $line, $matches ) ) { - continue; - } - $name = $matches[1]; - $drop[ $i ] = true; - - // Inline comment and quotes don't change the value. - $value = trim( preg_replace( '/;.*$/', '', $matches[2] ), " \t\"'" ); - $removed[ trim( $line ) ] = self::SHIPPED_VALUES[ $name ] === $value; - - $comments = self::SHIPPED_COMMENTS[ $name ]; - $count = count( $comments ); - if ( $i >= $count && array_slice( $norm, $i - $count, $count ) === $comments ) { - $drop += array_fill( $i - $count, $count, true ); - if ( isset( $norm[ $i + 1 ] ) && '' === $norm[ $i + 1 ] ) { - $drop[ $i + 1 ] = true; - } - } - } - - $new_content = implode( '', array_diff_key( $lines, $drop ) ); - - // A quoted value can span lines, so PHP must read the result as the original minus the two directives. - if ( $new_content !== $content ) { - // PHP-FPM resolves ${...} from the settings read so far, which parse_ini_string() can't see; it trims spaces around the name and takes a :- fallback. - if ( preg_match( '/\$\{[ \t]*session\.sid_(length|bits_per_character)[ \t]*(\}|:-)/', $new_content ) ) { - throw new \Exception( 'another setting refers to them as ${...}, which PHP would then read as empty or as its fallback, so the file was left as it is; remove session.sid_length and session.sid_bits_per_character by hand.' ); - } - // Flat too: a repeated section header hides the earlier section from the sectioned parse, but PHP still reads it. - foreach ( [ true, false ] as $sections ) { - $expected = @parse_ini_string( $content, $sections, INI_SCANNER_NORMAL ); - if ( is_array( $expected ) ) { - foreach ( $expected as $key => $value ) { - if ( is_array( $value ) ) { - $expected[ $key ] = array_diff_key( $value, self::SHIPPED_VALUES ); - } - } - $expected = array_diff_key( $expected, self::SHIPPED_VALUES ); - } - if ( ! is_array( $expected ) || @parse_ini_string( $new_content, $sections, INI_SCANNER_NORMAL ) !== $expected ) { - throw new \Exception( 'the file does not parse, or removing them line by line would change other settings (a quoted value spanning lines), so it was left as it is; remove session.sid_length and session.sid_bits_per_character by hand.' ); - } - } - } - - return [ $new_content, $removed ]; - } - /** * PHP-FPM reads php.ini only when it starts; a graceful reload (as `ee site reload --php` does) applies it. * Stopped containers read it when they start. diff --git a/src/helper/Site_Backup_Restore.php b/src/helper/Site_Backup_Restore.php index 2845fe78..cf5e3a91 100644 --- a/src/helper/Site_Backup_Restore.php +++ b/src/helper/Site_Backup_Restore.php @@ -112,6 +112,9 @@ class Site_Backup_Restore { // metadata.json of the backup being restored. private $restore_metadata = []; + // [ is_warning, message ] of drop_deprecated_session_sid_settings(), printed once the restore is committed. + private $sid_settings_notice = null; + private $restore_in_progress = false; // WordPress root the running backup excludes core from ('' = core is archived). @@ -1947,6 +1950,16 @@ private function apply_restore_transaction( $backup_dir ) { $this->commit_restore_transaction(); + if ( $this->sid_settings_notice ) { + list( $is_warning, $message ) = $this->sid_settings_notice; + $this->sid_settings_notice = null; + if ( $is_warning ) { + EE::warning( $message ); + } else { + EE::log( $message ); + } + } + if ( 'wp' === $this->site_data['site_type'] ) { $flush = EE::launch( $this->get_ee_shell_command( 'wp cache flush --skip-plugins --skip-themes' ) ); if ( $flush->return_code ) { @@ -3590,6 +3603,10 @@ private function restore_php_conf_files( $source, array $entries ) { if ( realpath( $dir ) !== $dir ) { throw new \Exception( "config/php/$parent is not a directory, or a directory above it is a symlink" ); } + // Not when it is skipped below. + if ( 'php/php.ini' === $path && ! $is_dir && ( $current || 'Darwin' !== PHP_OS_FAMILY ) ) { + $content = $this->drop_deprecated_session_sid_settings( $content, $source, $entries ); + } if ( ! $current ) { // On macOS config/php isn't mounted (only custom.ini is), so an entry missing there has no effect, as before. if ( 'Darwin' === PHP_OS_FAMILY ) { @@ -3628,6 +3645,69 @@ private function restore_php_conf_files( $source, array $entries ) { } } + /** + * A backup taken before v4.14.0 brings back session.sid_length and session.sid_bits_per_character, which PHP 8.4+ + * reports as deprecated, and the migration that removed them doesn't run again; remove them the same way. + * + * @param string $content php.ini from the backup. + * @param string $source Extracted `php` directory of the backup. + * @param array $entries Path => lstat() from list_php_conf_backup(). + * + * @return string The content to restore. + */ + private function drop_deprecated_session_sid_settings( $content, $source, array $entries ) { + $php_version = (string) ( $this->site_data['php_version'] ?? '' ); + if ( ! \EE\Site\Utils\php_deprecates_session_sid_settings( $php_version ) ) { + return $content; + } + + $custom = 'php/conf.d/custom.ini'; + try { + list( $new_content, $removed ) = \EE\Site\Utils\strip_session_sid_settings( $content ); + // Only the shipped quick reference: nothing to remove. + if ( ! $removed ) { + return $content; + } + // PHP reads conf.d/custom.ini after php.ini and resolves its ${...} from there. + if ( isset( $entries[ $custom ] ) && 0100000 === ( $entries[ $custom ]['mode'] & 0170000 ) + && \EE\Site\Utils\references_session_sid_settings( \EE\Site\Utils\get_container_file_contents( "$source/$custom", realpath( $source ) . '/php/conf.d' ) ) ) { + throw new \Exception( 'the restored custom.ini refers to them as ${...}, which PHP would then read as empty or as its fallback, so the file was left as it is; remove session.sid_length and session.sid_bits_per_character by hand.' ); + } + + // Next to the migration's copies: `ee site backup` and `ee site restore` empty EE_BACKUP_DIR//. + $root = EE_BACKUP_DIR . '/session-sid-settings-' . date( 'Ymd-His' ); + $file = '/' . $this->site_data['site_url'] . '/php.ini'; + if ( file_exists( $root . $file ) ) { + $root .= '-' . getmypid(); + } + try { + $this->fs->mkdir( dirname( $root . $file ), 0700 ); + $this->fs->dumpFile( $root . $file, $content ); + } catch ( \Exception $e ) { + // rmdir() refuses a dir that holds other sites' copies. + @rmdir( dirname( $root . $file ) ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- best effort. + @rmdir( $root ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- best effort. + throw new \Exception( "could not save a copy of it ({$e->getMessage()}), so it was left as it is; remove session.sid_length and session.sid_bits_per_character by hand." ); + } + } catch ( \Exception $e ) { + EE::warning( 'Could not remove the session ID settings deprecated as of PHP 8.4 from the restored php.ini: ' . $e->getMessage() ); + + return $content; + } + + $lines = array_map( + function ( $line ) { + return "`$line`"; + }, + array_keys( $removed ) + ); + $message = sprintf( 'Removed %s from the restored php.ini: deprecated as of PHP 8.4, so PHP now uses its default. The php.ini from the backup is saved as %s.', implode( ', ', $lines ), $root . $file ); + // Only a value the user set is worth a warning, as in the migration. Printed after the commit: a rollback puts the previous php.ini back. + $this->sid_settings_notice = [ in_array( false, $removed, true ), $message ]; + + return $new_content; + } + /** * @param string $site_dir Site root. * @param string $path Path below config/php. diff --git a/src/helper/site-utils.php b/src/helper/site-utils.php index 2ae2d5c7..5e33e0c4 100644 --- a/src/helper/site-utils.php +++ b/src/helper/site-utils.php @@ -2017,6 +2017,162 @@ function ini_global_part( string $content ) { return $content; } +/** + * Whether a site's PHP version deprecates session.sid_length and session.sid_bits_per_character (PHP 8.4 and later). + * + * @param string $php_version Site PHP version; `latest` sites run the php8.4 image. + * + * @return bool + */ +function php_deprecates_session_sid_settings( $php_version ) { + + $php_version = (string) $php_version; + + return 'latest' === $php_version || ( preg_match( '/^\d+\.\d+$/', $php_version ) && version_compare( $php_version, '8.4', '>=' ) ); +} + +/** + * Remove session.sid_length and session.sid_bits_per_character, wherever they are set, and the shipped comments that document them. + * Every other line is kept byte for byte. + * + * @param string $content php.ini content. + * + * @return array New content, and the removed directive lines => whether the value is the one EasyEngine shipped. + * + * @throws \Exception When the edit would change how PHP reads any other setting, or the file doesn't parse. + */ +function strip_session_sid_settings( string $content ) { + + // Values EasyEngine shipped; any other value was set by the user. + $shipped_values = [ + 'session.sid_length' => '26', + 'session.sid_bits_per_character' => '5', + ]; + // Comments shipped right above each directive, removed together with it. + $shipped_comments = [ + 'session.sid_length' => [ + '; Set session ID character length. This value could be between 22 to 256.', + '; Shorter length than default is supported only for compatibility reason.', + '; Users should use 32 or more chars.', + '; http://php.net/session.sid-length', + '; Default Value: 32', + '; Development Value: 26', + '; Production Value: 26', + ], + 'session.sid_bits_per_character' => [ + '; Define how many bits are stored in each character when converting', + '; the binary hash data to something readable.', + '; Possible values:', + '; 4 (4 bits: 0-9, a-f)', + '; 5 (5 bits: 0-9, a-v)', + '; 6 (6 bits: 0-9, a-z, A-Z, "-", ",")', + '; Default Value: 4', + '; Development Value: 5', + '; Production Value: 5', + '; http://php.net/session.hash-bits-per-character', + ], + ]; + // Entry of the quick reference at the top of the shipped php.ini. + $shipped_quick_reference = [ + '; session.sid_bits_per_character', + '; Default Value: 4', + '; Development Value: 5', + '; Production Value: 5', + ]; + + // After every LF, and after a CR not followed by LF, so a CR-only file isn't read as one line. + $lines = preg_split( '/(?<=\n)|(?<=\r)(?!\n)/', $content, -1, PREG_SPLIT_NO_EMPTY ); + $norm = array_map( + function ( $line ) { + return rtrim( $line, " \t\r\n" ); + }, + $lines + ); + $drop = []; + $removed = []; + + $quick_count = count( $shipped_quick_reference ); + foreach ( array_keys( $norm ) as $i ) { + if ( array_slice( $norm, $i, $quick_count ) === $shipped_quick_reference ) { + $drop += array_fill( $i, $quick_count, true ); + if ( isset( $norm[ $i + $quick_count ] ) && '' === $norm[ $i + $quick_count ] ) { + $drop[ $i + $quick_count ] = true; + } + } + } + + foreach ( $norm as $i => $line ) { + if ( ! preg_match( '/^\s*(session\.sid_length|session\.sid_bits_per_character)\s*=(.*)$/', $line, $matches ) ) { + continue; + } + $name = $matches[1]; + $drop[ $i ] = true; + + // Inline comment and quotes don't change the value. + $value = trim( preg_replace( '/;.*$/', '', $matches[2] ), " \t\"'" ); + $removed[ trim( $line ) ] = $shipped_values[ $name ] === $value; + + $comments = $shipped_comments[ $name ]; + $count = count( $comments ); + if ( $i >= $count && array_slice( $norm, $i - $count, $count ) === $comments ) { + $drop += array_fill( $i - $count, $count, true ); + if ( isset( $norm[ $i + 1 ] ) && '' === $norm[ $i + 1 ] ) { + $drop[ $i + 1 ] = true; + } + } + } + + $new_content = implode( '', array_diff_key( $lines, $drop ) ); + + // A quoted value can span lines, so PHP must read the result as the original minus the two directives. + if ( $new_content !== $content ) { + if ( references_session_sid_settings( $new_content ) ) { + throw new \Exception( 'another setting refers to them as ${...}, which PHP would then read as empty or as its fallback, so the file was left as it is; remove session.sid_length and session.sid_bits_per_character by hand.' ); + } + // Flat too: a repeated section header hides the earlier section from the sectioned parse, but PHP still reads it. + foreach ( [ true, false ] as $sections ) { + $expected = @parse_ini_string( $content, $sections, INI_SCANNER_NORMAL ); + if ( is_array( $expected ) ) { + foreach ( $expected as $key => $value ) { + if ( is_array( $value ) ) { + $expected[ $key ] = array_diff_key( $value, $shipped_values ); + } + } + $expected = array_diff_key( $expected, $shipped_values ); + } + if ( ! is_array( $expected ) || @parse_ini_string( $new_content, $sections, INI_SCANNER_NORMAL ) !== $expected ) { + throw new \Exception( 'the file does not parse, or removing them line by line would change other settings (a quoted value spanning lines), so it was left as it is; remove session.sid_length and session.sid_bits_per_character by hand.' ); + } + } + // A repeated section header can hide a removed line inside a quoted value from the parses above; such a line has an edge where the file so far doesn't parse. + $edges = []; + foreach ( array_keys( $drop ) as $i ) { + $edges[ $i ] = true; + $edges[ $i + 1 ] = true; + } + foreach ( array_keys( $edges ) as $edge ) { + if ( ! is_array( @parse_ini_string( implode( '', array_slice( $lines, 0, $edge ) ), false, INI_SCANNER_NORMAL ) ) ) { + throw new \Exception( 'the file does not parse, or removing them line by line would change other settings (a quoted value spanning lines), so it was left as it is; remove session.sid_length and session.sid_bits_per_character by hand.' ); + } + } + } + + return [ $new_content, $removed ]; +} + +/** + * Whether INI content refers to session.sid_length or session.sid_bits_per_character as ${...}. + * PHP resolves ${...} from the settings read so far, which parse_ini_string() can't see; it trims spaces around the name and takes a :- fallback. + * + * @param string $content INI content. + * + * @return bool + */ +function references_session_sid_settings( string $content ) { + + return (bool) preg_match( '/\$\{[ \t]*session\.sid_(length|bits_per_character)[ \t]*(\}|:-)/', $content ); +} + /** * Real path of a directory in a site's php config. config/php is EasyEngine's own link to the site's config volume; everything below it belongs to the site's php container. *