/") removed. * * @param string $file Zip file. * @param string $slug Expected top folder. * @return array path => [md5]. * @throws \RuntimeException When the package cannot be read. */ public static function hashes( $file, $slug ) { if ( ! is_readable( $file ) || filesize( $file ) > self::MAX_BYTES ) { throw new \RuntimeException( esc_html__( 'The theme package is missing or too large.', 'safetybis-security' ) ); } if ( class_exists( '\ZipArchive' ) ) { return self::with_ziparchive( $file, $slug ); } return self::with_pclzip( $file, $slug ); } /** * ZipArchive: stream each entry into md5. * * @param string $file Zip file. * @param string $slug Top folder. * @return array * @throws \RuntimeException When the package cannot be read. */ private static function with_ziparchive( $file, $slug ) { $zip = new \ZipArchive(); if ( true !== $zip->open( $file ) ) { throw new \RuntimeException( esc_html__( 'The theme package could not be opened.', 'safetybis-security' ) ); } $out = array(); for ( $i = 0; $i < $zip->numFiles; $i++ ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase $name = $zip->getNameIndex( $i ); $rel = self::relative( (string) $name, $slug ); if ( null === $rel ) { continue; } $stat = $zip->statIndex( $i ); if ( is_array( $stat ) && $stat['size'] <= self::MAX_ENTRY ) { $out[ $rel ] = self::entry_hashes( (string) $zip->getFromIndex( $i ) ); continue; } $stream = $zip->getStream( $name ); if ( ! $stream ) { continue; } $ctx = hash_init( 'md5' ); while ( ! feof( $stream ) ) { hash_update( $ctx, (string) fread( $stream, 65536 ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fread } fclose( $stream ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose $out[ $rel ] = array( hash_final( $ctx ) ); } $zip->close(); return $out; } /** * PclZip (bundled with WordPress): one entry at a time as a string. * * @param string $file Zip file. * @param string $slug Top folder. * @return array * @throws \RuntimeException When the package cannot be read. */ private static function with_pclzip( $file, $slug ) { require_once ABSPATH . 'wp-admin/includes/class-pclzip.php'; $zip = new \PclZip( $file ); $list = $zip->listContent(); if ( ! is_array( $list ) ) { throw new \RuntimeException( esc_html__( 'The theme package could not be opened.', 'safetybis-security' ) ); } $out = array(); foreach ( $list as $entry ) { if ( ! empty( $entry['folder'] ) ) { continue; } $rel = self::relative( (string) $entry['filename'], $slug ); if ( null === $rel ) { continue; } $data = $zip->extractByIndex( $entry['index'], PCLZIP_OPT_EXTRACT_AS_STRING ); if ( is_array( $data ) && isset( $data[0]['content'] ) ) { $out[ $rel ] = self::entry_hashes( (string) $data[0]['content'] ); } } return $out; } /** * md5 of an entry as it is, plus md5 with CRLF turned into LF when it differs: * WordPress.org theme packages of the Twenty* themes use CRLF, the copies shipped with * WordPress use LF (research.md, 19.11.2026). * * @param string $data Entry content. * @return string[] */ private static function entry_hashes( $data ) { $hashes = array( md5( $data ) ); if ( false !== strpos( $data, "\r\n" ) ) { $hashes[] = md5( str_replace( "\r\n", "\n", $data ) ); } return $hashes; } /** * Path inside the top folder, or null for folders and other entries. * * @param string $name Entry name. * @param string $slug Top folder. * @return string|null */ private static function relative( $name, $slug ) { if ( '' === $name || '/' === substr( $name, -1 ) ) { return null; } $prefix = $slug . '/'; if ( 0 !== strpos( $name, $prefix ) ) { return null; } $rel = substr( $name, strlen( $prefix ) ); return ( '' === $rel || false !== strpos( $rel, '..' ) ) ? null : $rel; } }