$log Bisheriges Protokoll. * @param string $host Host des Aufrufs. * @param bool $blocked Wurde der Aufruf blockiert? * @param int $now Zeitstempel. * @param bool $is_live Gilt der Host als Live-Adresse? * @param int $max Obergrenze der Hosts. * @return array */ public static function record( array $log, $host, $blocked, $now, $is_live = false, $max = self::MAX_HOSTS ) { $host = strtolower( trim( (string) $host ) ); if ( '' === $host || ! preg_match( '/^[a-z0-9._:-]{1,253}$/', $host ) ) { return $log; } $entry = isset( $log[ $host ] ) && is_array( $log[ $host ] ) ? $log[ $host ] : array(); $entry = array( 'blocked' => (int) ( $entry['blocked'] ?? 0 ), 'allowed' => (int) ( $entry['allowed'] ?? 0 ), 'live' => ! empty( $entry['live'] ) || (bool) $is_live, 'last' => (int) $now, ); if ( $blocked ) { ++$entry['blocked']; } else { ++$entry['allowed']; } $log[ $host ] = $entry; $max = max( 1, (int) $max ); while ( count( $log ) > $max ) { $oldest = null; $oldest_time = PHP_INT_MAX; foreach ( $log as $name => $row ) { $last = (int) ( $row['last'] ?? 0 ); if ( $last < $oldest_time && (string) $name !== $host ) { $oldest = $name; $oldest_time = $last; } } if ( null === $oldest ) { break; } unset( $log[ $oldest ] ); } return $log; } /** * Hosts nach letztem Aufruf, neueste zuerst. * * @param array $log Protokoll. * @param int $limit Hoechstzahl der Zeilen. * @return array */ public static function latest( array $log, $limit = 0 ) { $rows = array(); foreach ( $log as $host => $row ) { $rows[] = array( 'host' => (string) $host, 'blocked' => (int) ( $row['blocked'] ?? 0 ), 'allowed' => (int) ( $row['allowed'] ?? 0 ), 'live' => ! empty( $row['live'] ), 'last' => (int) ( $row['last'] ?? 0 ), ); } usort( $rows, static function ( $a, $b ) { return $b['last'] <=> $a['last'] ?: strcmp( $a['host'], $b['host'] ); } ); // 0 = alle Zeilen (das Protokoll ist ohnehin begrenzt). return (int) $limit > 0 ? array_slice( $rows, 0, (int) $limit ) : $rows; } /** * Summen ueber alle Hosts. * * @param array $log Protokoll. * @return array{hosts: int, blocked: int, allowed: int} */ public static function totals( array $log ) { $blocked = 0; $allowed = 0; foreach ( $log as $row ) { $blocked += (int) ( $row['blocked'] ?? 0 ); $allowed += (int) ( $row['allowed'] ?? 0 ); } return array( 'hosts' => count( $log ), 'blocked' => $blocked, 'allowed' => $allowed, ); } }