size( $from ); if ( ! is_int( $size ) || $size > self::MAX_PACKED ) { throw new \RuntimeException( 'the location data is more than ' . (int) ( self::MAX_PACKED / 1048576 ) . ' MB packed' ); } $packed = Storage::files()->get_contents( $from ); $inflate = is_string( $packed ) ? inflate_init( ZLIB_ENCODING_GZIP ) : false; if ( ! is_string( $packed ) || false === $inflate ) { throw new \RuntimeException( 'could not unpack ' . esc_html( $from ) ); } $hash = hash_init( 'sha1' ); $unpacked = ''; $length = strlen( $packed ); // Small pieces, so one that unpacks a thousandfold, as zeros do, is a few megabytes before it is counted. $step = 4096; for ( $at = 0; $at < $length && ZLIB_STREAM_END !== inflate_get_status( $inflate ); $at += $step ) { // A stream that is not gzip, or is damaged, makes PHP warn as well as answer false. $piece = @inflate_add( $inflate, substr( $packed, $at, $step ) ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- the failure is thrown just below. if ( false === $piece ) { throw new \RuntimeException( 'it is not a gzip file' ); } if ( strlen( $unpacked ) + strlen( $piece ) > $max ) { throw new \RuntimeException( 'the location data is more than ' . (int) ( $max / 1048576 ) . ' MB' ); } hash_update( $hash, $piece ); $unpacked .= $piece; } if ( ZLIB_STREAM_END !== inflate_get_status( $inflate ) ) { throw new \RuntimeException( 'the gzip stream stops before its end' ); } if ( inflate_get_read_len( $inflate ) !== $length ) { throw new \RuntimeException( 'there is more in it after the end of the gzip stream' ); } unset( $packed ); if ( ! Storage::files()->put_contents( $to, $unpacked, self::mode() ) ) { throw new \RuntimeException( 'the unpacked database could not be written' ); } return hash_final( $hash ); } /** * The database in a file. * * @param string $file The file. * @throws \RuntimeException When it cannot be read. */ public function open( string $file ): Mmdb { $bytes = Storage::files()->get_contents( $file ); if ( ! is_string( $bytes ) ) { throw new \RuntimeException( 'Could not read ' . esc_html( $file ) ); } return new Mmdb( $bytes ); } /** * Puts a file in place. * * @param string $from The file. * @param string $to Its new name. * @throws \RuntimeException When it cannot. */ public function move( string $from, string $to ): void { if ( ! Storage::files()->move( $from, $to, true ) ) { throw new \RuntimeException( 'could not move ' . esc_html( $from ) ); } } /** * Deletes a file, when it is there. * * @param string $file The file. */ public function delete( string $file ): void { wp_delete_file( $file ); } }