'<', 'le' => '<=', 'gt' => '>', 'ge' => '>=', 'eq' => '==', ); /** Kuerzel des Dienstes => Stufe, aufsteigend nach Gewicht. */ const SEVERITIES = array( 'n' => 'none', 'l' => 'low', 'm' => 'medium', 'h' => 'high', 'c' => 'critical', ); /** * Bringt den `operator` des Dienstes in eine feste Form. * * @param mixed $operator Wert aus der Antwort. * @return array{min:?string,min_op:?string,max:?string,max_op:?string,unfixed:bool}|null * null, wenn der Bereich nicht lesbar ist (unbekannter Operator). */ public static function normalize( $operator ) { if ( ! is_array( $operator ) ) { return null; } $range = array( 'min' => null, 'min_op' => null, 'max' => null, 'max_op' => null, 'unfixed' => ! empty( $operator['unfixed'] ), ); foreach ( array( 'min', 'max' ) as $side ) { $version = $operator[ $side . '_version' ] ?? null; $op = $operator[ $side . '_operator' ] ?? null; if ( null === $version || '' === $version ) { continue; } if ( ! is_scalar( $version ) || ! is_string( $op ) || ! isset( self::OPERATORS[ $op ] ) ) { return null; } $range[ $side ] = (string) $version; $range[ $side . '_op' ] = $op; } return $range; } /** * Liegt die Version im Bereich? * * @param string $version Installierte Version. * @param array $range Bereich aus {@see self::normalize()}. */ public static function matches( $version, array $range ): bool { $version = (string) $version; if ( '' === $version ) { return false; } foreach ( array( 'min', 'max' ) as $side ) { if ( null === $range[ $side ] ) { continue; } if ( ! version_compare( $version, $range[ $side ], self::OPERATORS[ $range[ $side . '_op' ] ] ) ) { return false; } } return true; } /** * Hat die Version die obere Grenze schon hinter sich? Ein solcher Eintrag * kann ohne Downgrade nie wieder zutreffen und muss nicht aufbewahrt werden. * * @param string $version Installierte Version. * @param array $range Bereich aus {@see self::normalize()}. */ public static function outdated( $version, array $range ): bool { $version = (string) $version; if ( '' === $version || null === $range['max'] || ! in_array( $range['max_op'], array( 'lt', 'le', 'eq' ), true ) ) { return false; } return version_compare( $version, $range['max'], 'lt' === $range['max_op'] ? '>=' : '>' ); } /** * Erste behobene Version, soweit der Bereich sie nennt („kleiner als X“). * * @param array $range Bereich aus {@see self::normalize()}. */ public static function fixed_in( array $range ): string { if ( $range['unfixed'] || 'lt' !== $range['max_op'] ) { return ''; } return (string) $range['max']; } /** * Bereich als kurzer Text, z. B. `>= 5.0, < 5.3.2`. Leer heisst: alle Versionen. * * @param array $range Bereich aus {@see self::normalize()}. */ public static function describe( array $range ): string { $parts = array(); foreach ( array( 'min', 'max' ) as $side ) { if ( null !== $range[ $side ] ) { $parts[] = self::OPERATORS[ $range[ $side . '_op' ] ] . ' ' . $range[ $side ]; } } return implode( ', ', $parts ); } /** * Schweregrad aus `impact`. Der Dienst liefert ihn nicht fuer jeden Eintrag. * * @param mixed $impact Wert aus der Antwort. * @return array{level:string,score:float} level ist leer, wenn nichts geliefert wurde. */ public static function severity( $impact ): array { $cvss = is_array( $impact ) && isset( $impact['cvss'] ) && is_array( $impact['cvss'] ) ? $impact['cvss'] : array(); $short = isset( $cvss['severity'] ) && is_string( $cvss['severity'] ) ? strtolower( $cvss['severity'] ) : ''; return array( 'level' => self::SEVERITIES[ $short ] ?? '', 'score' => isset( $cvss['score'] ) && is_numeric( $cvss['score'] ) ? (float) $cvss['score'] : 0.0, ); } /** * Die schwerere von zwei Stufen. Eine leere Stufe verliert immer. * * @param string $a Stufe. * @param string $b Stufe. */ public static function higher( $a, $b ): string { $rank = array_flip( array_values( self::SEVERITIES ) ); return ( $rank[ $b ] ?? -1 ) > ( $rank[ $a ] ?? -1 ) ? (string) $b : (string) $a; } }