0 ) { $merged[ $n - 1 ][1] = $r[1]; } continue; } $merged[] = $r; } $data = ''; foreach ( $merged as $r ) { $data .= $r[0] . $r[1] . $r[2]; } return self::put( $name . '-v' . (int) $family, $data, count( $merged ) ); } /** * Store raw, already sorted record bytes. */ public static function put( $file, $data, $count ) { $dir = self::dir(); if ( '' === $dir || ! is_dir( $dir ) ) { self::$dir = Softvolt_Ai_Bodyguard_Helpers::work_dir( 'ranges' ); } $tmp = self::path( $file ) . '.tmp'; if ( false === @file_put_contents( $tmp, $data ) ) { return new WP_Error( 'svabg_ranges', __( 'The uploads folder is not writable, so the list could not be saved.', 'ai-guardwall' ) ); } wp_delete_file( self::path( $file ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename -- an atomic move on the same disk (quarantine, backups, temporary files). if ( ! @rename( $tmp, self::path( $file ) ) ) { wp_delete_file( $tmp ); return new WP_Error( 'svabg_ranges', __( 'The list file could not be replaced.', 'ai-guardwall' ) ); } return (int) $count; } /** * Find the record holding a key in a range file. * * @param string $file File name without .bin (e.g. "geo-v4"). * @param string $key Key bytes (4 or 8). * @param int $payload Payload length. * @return string|false The payload ('' for block lists) or false when not found. */ public static function find( $file, $key, $payload = 0 ) { $path = self::path( $file ); $klen = strlen( $key ); $rlen = $klen * 2 + $payload; $size = is_file( $path ) ? (int) filesize( $path ) : 0; if ( ! $size || $size < $rlen ) { return false; } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- large files are streamed in chunks; WP_Filesystem cannot stream. $fh = @fopen( $path, 'rb' ); if ( ! $fh ) { return false; } $lo = 0; $hi = intdiv( $size, $rlen ) - 1; $found = false; while ( $lo <= $hi ) { $mid = ( $lo + $hi ) >> 1; fseek( $fh, $mid * $rlen ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fread -- large files are streamed in chunks; WP_Filesystem cannot stream. $rec = (string) fread( $fh, $rlen ); if ( strlen( $rec ) < $rlen ) { break; } $start = substr( $rec, 0, $klen ); if ( strcmp( $start, $key ) <= 0 ) { $found = $rec; $lo = $mid + 1; } else { $hi = $mid - 1; } } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- large files are streamed in chunks; WP_Filesystem cannot stream. fclose( $fh ); if ( false === $found ) { return false; } $end = substr( $found, $klen, $klen ); if ( strcmp( $key, $end ) > 0 ) { return false; } return $payload ? substr( $found, $klen * 2, $payload ) : ''; } /** * Look an IP up in the v4/v6 pair of a list. * * @param string $name List name (e.g. "geo", "threat"). * @param string $ip IP. * @param int $payload Payload length. * @return string|false */ public static function lookup( $name, $ip, $payload = 0 ) { $key = self::key( $ip ); if ( ! $key ) { return false; } return self::find( $name . '-v' . $key[0], $key[1], $payload ); } /** * Records in a list file (0 when missing). */ public static function count( $file, $family, $payload = 0 ) { $size = is_file( self::path( $file ) ) ? (int) filesize( self::path( $file ) ) : 0; $rlen = ( 4 === (int) $family ? 8 : 16 ) + $payload; return $size ? intdiv( (int) $size, $rlen ) : 0; } public static function delete( $name ) { foreach ( array( 4, 6 ) as $family ) { wp_delete_file( self::path( $name . '-v' . $family ) ); } } /** * The bytes + 1 (used to merge touching ranges). */ private static function inc( $bytes ) { $i = strlen( $bytes ) - 1; while ( $i >= 0 ) { $o = ord( $bytes[ $i ] ); if ( $o < 255 ) { $bytes[ $i ] = chr( $o + 1 ); return $bytes; } $bytes[ $i ] = "\0"; $i--; } return str_repeat( "\xff", strlen( $bytes ) ); // Overflow: the end of the address space. } }