"8.2.10"). * * @param string $version Raw version. * @return string */ public static function clean_version( $version ) { $version = trim( (string) $version, " \t\n\r\0\x0B" ); if ( preg_match( '/(\d+(?:\.\d+)*)/', $version, $m ) ) { return $m[1]; } return ''; } /** * "8.2.10" => "8.2". * * @param string $version Version. * @return string */ public static function branch( $version ) { $parts = explode( '.', self::clean_version( $version ) ); return isset( $parts[1] ) ? $parts[0] . '.' . $parts[1] : $parts[0]; } /** * Is $version inside a range? Any bound may be empty / '*' meaning open-ended. * * @param string $version Installed version. * @param string $from Lower bound. * @param bool $from_incl Lower bound inclusive. * @param string $to Upper bound. * @param bool $to_incl Upper bound inclusive. * @return bool */ public static function in_range( $version, $from, $from_incl, $to, $to_incl ) { $version = self::clean_version( $version ); if ( '' === $version ) { return false; } $from = ( '*' === $from || null === $from ) ? '' : self::clean_version( $from ); $to = ( '*' === $to || null === $to ) ? '' : self::clean_version( $to ); if ( '' === $from && '' === $to ) { // Fully open range: treat as "all versions" only when explicitly '*'. return true; } if ( '' !== $from && ! version_compare( $version, $from, $from_incl ? '>=' : '>' ) ) { return false; } if ( '' !== $to && ! version_compare( $version, $to, $to_incl ? '<=' : '<' ) ) { return false; } return true; } /** * Map a CVSS score to a severity label. * * @param float|null $score Score. * @return string */ public static function severity_from_score( $score ) { if ( null === $score || '' === $score ) { return 'medium'; } $score = (float) $score; if ( $score >= 9.0 ) { return 'critical'; } if ( $score >= 7.0 ) { return 'high'; } if ( $score >= 4.0 ) { return 'medium'; } if ( $score > 0 ) { return 'low'; } return 'info'; } /** * Normalise free-form severity labels ("H", "High", "c", "CRITICAL"). * * @param string $label Label. * @param float|null $score Fallback score. * @return string */ public static function normalise_severity( $label, $score = null ) { $l = strtolower( substr( trim( (string) $label, " \t\n\r\0\x0B" ), 0, 1 ) ); $map = array( 'c' => 'critical', 'h' => 'high', 'm' => 'medium', 'l' => 'low', 'n' => 'info', 'i' => 'info', ); if ( isset( $map[ $l ] ) ) { return $map[ $l ]; } return self::severity_from_score( $score ); } /** * Severity rank (0 = most severe). * * @param string $severity Severity. * @return int */ public static function severity_rank( $severity ) { $i = array_search( $severity, self::SEVERITIES, true ); return false === $i ? 99 : (int) $i; } /** * Whole days from $now until a Y-m-d date (negative = in the past). * * @param string $date Date (Y-m-d). * @param int|null $now Timestamp. * @return int|null */ public static function days_until( $date, $now = null ) { if ( empty( $date ) || ! is_string( $date ) ) { return null; } $ts = strtotime( $date . ' 23:59:59 UTC' ); if ( false === $ts ) { return null; } $now = null === $now ? time() : (int) $now; return (int) floor( ( $ts - $now ) / 86400 ); } /** * Smallest version from a list that is greater than $installed. * * @param array $candidates Versions. * @param string $installed Installed. * @return string */ public static function lowest_fix( $candidates, $installed ) { $best = ''; foreach ( (array) $candidates as $c ) { $c = self::clean_version( $c ); if ( '' === $c || version_compare( $c, self::clean_version( $installed ), '<=' ) ) { continue; } if ( '' === $best || version_compare( $c, $best, '<' ) ) { $best = $c; } } return $best; } /** * Compute the 0–100 health score + letter grade from findings. * * @param array $findings Findings. * @return array{score:int,grade:string} */ public static function score( $findings ) { $weights = array( 'critical' => 25, 'high' => 15, 'medium' => 7, 'low' => 2, 'info' => 0, ); $penalty = 0; $routine = 0; foreach ( $findings as $f ) { $sev = isset( $f['severity'] ) ? $f['severity'] : 'info'; if ( isset( $f['category'] ) && 'update' === $f['category'] && empty( $f['security_update'] ) ) { // Routine updates: small, capped penalty so 30 pending updates don't read as "hacked". $routine += 1; continue; } $penalty += isset( $weights[ $sev ] ) ? $weights[ $sev ] : 0; } $penalty += min( 10, $routine ); $score = max( 0, 100 - $penalty ); if ( $score >= 90 ) { $grade = 'A'; } elseif ( $score >= 80 ) { $grade = 'B'; } elseif ( $score >= 65 ) { $grade = 'C'; } elseif ( $score >= 50 ) { $grade = 'D'; } else { $grade = 'F'; } return array( 'score' => (int) $score, 'grade' => $grade, ); } /** * Sort findings: severity, then category priority. * * @param array $findings Findings. * @return array */ public static function sort_findings( $findings ) { $cat = array( 'vulnerability' => 0, 'lifecycle' => 1, 'abandoned' => 2, 'update' => 3, 'readiness' => 4, 'config' => 5, ); usort( $findings, function ( $a, $b ) use ( $cat ) { $s = self::severity_rank( $a['severity'] ) - self::severity_rank( $b['severity'] ); if ( 0 !== $s ) { return $s; } $ca = isset( $cat[ $a['category'] ] ) ? $cat[ $a['category'] ] : 9; $cb = isset( $cat[ $b['category'] ] ) ? $cat[ $b['category'] ] : 9; return $ca - $cb; } ); return $findings; } }