#!/bin/bash
# Are this host's PHP packages at the versions patched for the 2026-11-19 PHP security release (INC-491)?
# Read-only: it only asks rpm what is installed. Works for every cohort; a host just reports what it has.
#   OK       at or above the patched version
#   OLD      below it: update pending
#   PENDING  no patched build has been released for this package yet
#   PARTIAL  a vendor build that fixes most but not all of the issues
#   OTHER    a build this check knows nothing about, not judged
# Our own rebuilds, Remi's own packages and cPanel's own packages are all recognised.
# Exit status 1 if anything is OLD.
# Options:  --output json   one JSON document on stdout instead of the text lines (for Ansible and the like)
#           --output text   the default
#           --color=auto|always|never   colour the status words (auto: only on a terminal and only when --output
#                           is not given; --no-color = never). JSON output is never coloured.
# Through a pipe, options go after "bash -s --":  curl -fsSL <url> | bash -s -- --output json
out=text; color=auto; out_given=0
while [ $# -gt 0 ]; do
  case "$1" in
    --output) out_given=1; out=$2; shift 2 || { echo "php-inc491-check: --output needs a value (text or json)" >&2; exit 2; };;
    --output=*) out_given=1; out=${1#--output=}; shift;;
    --json) out_given=1; out=json; shift;;
    --color=*) color=${1#--color=}; shift;;
    --no-color) color=never; shift;;
    -h|--help) echo "usage: php-inc491-check.sh [--output text|json] [--color=auto|always|never]"; exit 0;;
    *) echo "php-inc491-check: unknown option: $1" >&2; exit 2;;
  esac
done
case "$color" in auto|always|never) ;; *) echo "php-inc491-check: --color must be auto, always or never, not: $color" >&2; exit 2;; esac
# Naming an output format means something is reading it: no colour then unless --color=always asks. JSON never has any.
[ "$color" = auto ] && [ $out_given = 1 ] && color=never
[ "$out" = json ] && color=never
if [ "$color" = auto ]; then if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-dumb}" != dumb ]; then color=always; else color=never; fi; fi
paint() {  # status word, pad width -> the word padded, coloured when colour is on
  if [ "$color" != always ]; then printf "%-$2s" "$1"; return; fi
  case "$1" in OK) c='1;32';; OLD|FAIL) c='1;31';; PARTIAL|PENDING|WARN) c='1;33';; OTHER|INFO) c='36';; *) c=0;; esac
  printf "\033[%sm%-$2s\033[0m" "$c" "$1"
}
case "$out" in text|json) ;; *) echo "php-inc491-check: --output must be text or json, not: $out" >&2; exit 2;; esac
el=$(rpm -E '%{?rhel}' 2>/dev/null); [ -n "$el" ] || el=$(sed -E 's/[^0-9]*([0-9]+).*/\1/' /etc/redhat-release 2>/dev/null)

want() {  # package name, installed version, installed release -> patched version-release, or "-" if none yet
  case "$1" in
    php54-php-common) echo 5.4.45-19.3;; php55-php-common) echo 5.5.38-13.3;; php56-php-common) echo 5.6.40-47.5;;
    php70-php-common) echo 7.0.33-47.5;; php71-php-common) echo 7.1.33-34.5;;
    php72-php-common) case $el in 8) echo 7.2.34-32.1;; *) echo 7.2.34-31.3;; esac;;
    php73-php-common) case $el in 8) echo 7.3.33-25.1;; *) echo 7.3.33-24.3;; esac;;
    php74-php-common) echo 7.4.33-32.1;; php80-php-common) echo 8.0.30-22.2;; php81-php-common) echo 8.1.34-7.1;;
    php82-php-common) echo 8.2.36-1.1;; php83-php-common) echo 8.3.37-1.1;;
    php-common) case "$2" in
        5.4.*) case "$3" in 4[0-9].*) echo 5.4.45-49.2;; *) echo 5.4.45-19.4;; esac;;
        5.5.*) echo 5.5.38-13.4;; 5.6.*) echo 5.6.40-41.4;; 7.0.*) echo 7.0.33-42.4;; 7.1.*) echo 7.1.33-29.4;;
        7.2.*) case $el in 8) echo 7.2.34-32.1;; *) echo 7.2.34-31.3;; esac;;
        7.3.*) echo 7.3.33-24.3;; 7.4.*) echo 7.4.33-32.1;; 8.0.*) echo 8.0.30-22.2;; 8.1.*) echo 8.1.34-7.1;;
        8.2.*) echo 8.2.36-1.1;; 8.3.*) echo 8.3.37-1.1;; esac;;
    ea-php54-php-common) case $el in 6) echo 5.4.45-82.83.1.4;; *) echo 5.4.45-82.83.33.5;; esac;;
    ea-php55-php-common) case $el in 6) echo 5.5.38-65.66.1.4;; *) echo 5.5.38-65.66.31.5;; esac;;
    ea-php56-php-common) case $el in 6) echo 5.6.40-25.27.1.4;; *) echo 5.6.40-25.27.33.5;; esac;;
    ea-php70-php-common) case $el in 6) echo 7.0.33-29.30.1.4;; *) echo 7.0.33-29.30.32.5;; esac;;
    ea-php71-php-common) case $el in 6) echo 7.1.33-20.21.1.4;; *) echo 7.1.33-20.21.33.5;; esac;;
    ea-php72-php-common) case $el in 6) echo 7.2.34-12.14.1.4;; 8) echo 7.2.34-12.14.19.5;; *) echo 7.2.34-12.14.32.5;; esac;;
    ea-php73-php-common) case $el in 6) echo 7.3.33-15.20.1.4;; 8) echo 7.3.33-15.20.18.5;; *) echo 7.3.33-15.20.31.5;; esac;;
    ea-php74-php-common) case $el in 6) echo "-";; *) echo 7.4.34-2.2.1.2;; esac;;
    ea-php80-php-common) echo 8.0.31-2.2.1.2;; ea-php81-php-common) echo 8.1.35-2.2.1.2;;
    php53u-common) echo 5.3.29-5.ius;; php54-common) echo 5.4.45-5.ius;; php54u-php-common) echo 5.4.45-5.ius;;
    php55u-php-common) echo 5.5.38-5.ius;; php56u-php-common) echo 5.6.40-18.ius;;
    php70u-php-common) echo 7.0.33-6;; php71u-php-common) echo 7.1.33-6;; php72u-php-common) echo 7.2.34-5;; php73u-php-common) echo 7.3.31-5;;
    nexkit-php) case "$2" in 5.6.*) echo 5.6.40-2;; 8.3.*) echo 8.3.37-1;; esac;;
    iworxphp72-php-common) echo 7.2.34-130;; iworxphp82-php-common) echo 8.2.36-8;;
  esac
}

want_remi() {  # installed version -> Remi's own fixed version-release, empty if he has none
  case "$1" in
    7.2.*) echo 7.2.34-32;; 7.3.*) echo 7.3.33-25;; 7.4.*) echo 7.4.33-32;; 8.0.*) echo 8.0.30-22;; 8.1.*) echo 8.1.34-7;;
    8.2.*) echo 8.2.36-1;; 8.3.*) echo 8.3.37-1;; 8.4.*) echo 8.4.28-1;; 8.5.*) echo 8.5.13-1;; 8.6.*) echo 8.6.0-1;;
  esac
}
want_cpanel() {  # package -> cPanel's own fixed version-release ("partial <v>" if it lacks some fixes), empty if none
  case "$1" in
    ea-php74-php-common) echo "partial 7.4.34-3";;
    ea-php82-php-common) echo 8.2.36-1;; ea-php83-php-common) echo 8.3.37-1;;
    ea-php84-php-common) echo 8.4.28-1;; ea-php85-php-common) echo 8.5.13-1;;
  esac
}
atleast() { [ "$(printf '%s\n%s\n' "$1" "$2" | sort -V | head -1)" = "$1" ]; }  # want, have

esc() { printf '%s' "$1" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g'; }   # JSON string escape (no control characters occur in these values)
items=; n_ok=0; n_old=0; n_pending=0; n_partial=0; n_other=0
emit() {  # status, package, version, release, origin (ours|remi|cpanel|unknown), patched version-release or "", text after the version
  found=1
  case "$1" in OK) n_ok=$((n_ok+1));; OLD) n_old=$((n_old+1)); old=1;; PENDING) n_pending=$((n_pending+1));; PARTIAL) n_partial=$((n_partial+1));; OTHER) n_other=$((n_other+1));; esac
  if [ "$out" = json ]; then
    note=${7# }; note=${note#(}; note=${note%)}
    items="$items${items:+,}
    {\"name\":\"$(esc "$2")\",\"version\":\"$(esc "$3")\",\"release\":\"$(esc "$4")\",\"epoch\":$(rpm -q --qf '%{EPOCHNUM}\n' "$2" 2>/dev/null | head -1 | grep -E '^[0-9]+$' || echo 0),\"installed\":\"$(esc "$3-$4")\",\"status\":\"$1\",\"origin\":\"$5\",\"patched\":$( [ -n "$6" ] && echo "\"$(esc "$6")\"" || echo null ),\"note\":\"$(esc "$note")\"}"
  else
    printf '%s %s %s%s\n' "$(paint "$1" 8)" "$2" "$3-$4" "$7"
  fi
}

old=0; found=0
for p in $(rpm -qa --qf '%{NAME}\n' 'php*-common' 'ea-php*-php-common' 'iworxphp*-php-common' nexkit-php 2>/dev/null | sort -u); do
  ver=$(rpm -q --qf '%{VERSION}\n' "$p" | head -1); rel=$(rpm -q --qf '%{RELEASE}\n' "$p" | head -1); have="$ver-$rel"
  w=$(want "$p" "$ver" "$rel")
  case "$p $rel" in
    *.remi*)                                             # Remi's own package, from his repositories
      r=$(want_remi "$ver")
      if [ -z "$r" ]; then emit OLD "$p" "$ver" "$rel" remi "${w#- }" " (Remi's build; he has no fixed build of this version${w:+, ours is ${w#- }})"
      elif atleast "$r" "$have"; then emit OK "$p" "$ver" "$rel" remi "$r" " (Remi's build)"
      else emit OLD "$p" "$ver" "$rel" remi "$r" " (Remi's build; patched: $r)"; fi
      continue;;
    ea-php*cpanel*)                                      # cPanel's own builds end in .cpanel too: ours carry epoch 1 on el7 and up
      if [ "$el" != 6 ] && [ "$(rpm -q --qf '%{EPOCHNUM}\n' "$p" | head -1)" != 1 ]; then
        c=$(want_cpanel "$p")
        if [ -z "$c" ]; then emit OLD "$p" "$ver" "$rel" cpanel "${w:+1:$w}" " (cPanel's build; cPanel has no fixed build${w:+, ours is 1:$w})"
        elif ! atleast "${c#partial }" "$have"; then emit OLD "$p" "$ver" "$rel" cpanel "${c#partial }" " (cPanel's build; patched: ${c#partial })"
        elif [ "$c" != "${c#partial }" ]; then emit PARTIAL "$p" "$ver" "$rel" cpanel "${w:+1:$w}" " (cPanel's build; its changelog does not list CVE-2026-115560 or CVE-2026-127960${w:+, ours is 1:$w})"
        else emit OK "$p" "$ver" "$rel" cpanel "${c#partial }" " (cPanel's build)"; fi
        continue
      fi;;
    php*.iworx*|php*ius*|php7[0-3]u*|nexkit-php*|iworxphp*) ;;
    *) [ -n "$w" ] && emit OTHER "$p" "$ver" "$rel" unknown "" " (not a build this check knows)"; continue;;
  esac
  [ -n "$w" ] || continue
  case "$w" in
    "-"*) emit PENDING "$p" "$ver" "$rel" ours "" "$(echo " (no patched build released yet${w#-})" | sed 's/yet /yet; it will be /')"; continue;;
  esac
  if atleast "$w" "$have"; then emit OK "$p" "$ver" "$rel" ours "$w" ""
  else emit OLD "$p" "$ver" "$rel" ours "$w" " (patched: $w)"; fi
done
if [ "$out" = json ]; then
  if [ $found = 0 ]; then overall=none; elif [ $old = 1 ]; then overall=old; else overall=ok; fi
  printf '{"check":"php-inc491","schema":1,"generated":"%s","hostname":"%s","el":"%s","status":"%s",\n "summary":{"ok":%d,"old":%d,"pending":%d,"partial":%d,"other":%d},\n "packages":[%s%s]}\n' \
    "$(date -u '+%Y-%m-%dT%H:%M:%SZ')" "$(esc "$(hostname -f 2>/dev/null || hostname)")" "$(esc "$el")" "$overall" $n_ok $n_old $n_pending $n_partial $n_other "$items" "${items:+
 }"
else
  [ $found = 1 ] || echo "no PHP packages covered by this check are installed"
fi
exit $old
