|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Warns administrators when the server runs a vulnerable PHP version. |
| 4 | + * |
| 5 | + * @package \paystack\payment_forms |
| 6 | + */ |
| 7 | + |
| 8 | +namespace paystack\payment_forms; |
| 9 | + |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { |
| 11 | + exit; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * PHP version notice class. |
| 16 | + */ |
| 17 | +class PHP_Version_Notice { |
| 18 | + |
| 19 | + /** |
| 20 | + * First patched release per supported minor branch. |
| 21 | + * |
| 22 | + * CVE-2026-109618 / CVE-2026-109619; 8.1 and older are end of life with no fix. |
| 23 | + * |
| 24 | + * @var array |
| 25 | + */ |
| 26 | + const FIXED_PATCH = array( |
| 27 | + '8.2' => 35, |
| 28 | + '8.3' => 36, |
| 29 | + '8.4' => 27, |
| 30 | + '8.5' => 12, |
| 31 | + ); |
| 32 | + |
| 33 | + /** |
| 34 | + * Construct the class. |
| 35 | + */ |
| 36 | + public function __construct() { |
| 37 | + add_filter( 'site_status_tests', array( $this, 'add_site_health_test' ) ); |
| 38 | + add_action( 'admin_notices', array( $this, 'admin_notice' ) ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Check whether a PHP version is affected. |
| 43 | + * |
| 44 | + * @param string $version PHP version string. |
| 45 | + * |
| 46 | + * @return bool |
| 47 | + */ |
| 48 | + public static function is_affected( $version ) { |
| 49 | + if ( ! preg_match( '/^(\d+)\.(\d+)(?:\.(\d+))?/', (string) $version, $matches ) ) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + $major = (int) $matches[1]; |
| 54 | + $minor = (int) $matches[2]; |
| 55 | + $patch = isset( $matches[3] ) ? (int) $matches[3] : 0; |
| 56 | + |
| 57 | + if ( $major < 8 || ( 8 === $major && $minor < 2 ) ) { |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + $key = $major . '.' . $minor; |
| 62 | + |
| 63 | + return isset( self::FIXED_PATCH[ $key ] ) && $patch < self::FIXED_PATCH[ $key ]; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get the running PHP release, without any distro suffix. |
| 68 | + * |
| 69 | + * @return string |
| 70 | + */ |
| 71 | + private static function current_version() { |
| 72 | + return PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Whether this is a distro build whose security fixes may be backported. |
| 77 | + * |
| 78 | + * @return bool |
| 79 | + */ |
| 80 | + private static function is_distro_build() { |
| 81 | + return '' !== PHP_EXTRA_VERSION; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Whether the running version is affected, ignoring distro backports. |
| 86 | + * |
| 87 | + * @return bool |
| 88 | + */ |
| 89 | + private function is_vulnerable() { |
| 90 | + return self::is_affected( self::current_version() ) && apply_filters( 'pff_paystack_php_version_warning', true ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Whether the admin notice should be shown. |
| 95 | + * |
| 96 | + * @return bool |
| 97 | + */ |
| 98 | + private function should_warn() { |
| 99 | + return $this->is_vulnerable() && ! self::is_distro_build(); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Build the warning message. |
| 104 | + * |
| 105 | + * @return string |
| 106 | + */ |
| 107 | + private function message() { |
| 108 | + $fixed = array(); |
| 109 | + foreach ( self::FIXED_PATCH as $branch => $patch ) { |
| 110 | + $fixed[] = $branch . '.' . $patch; |
| 111 | + } |
| 112 | + |
| 113 | + return sprintf( |
| 114 | + /* translators: 1: PHP version, 2: list of fixed PHP versions. */ |
| 115 | + esc_html__( 'Payment Forms for Paystack: this server runs PHP %1$s, which is affected by critical security vulnerabilities (CVE-2026-109618, CVE-2026-109619) that allow remote code execution. Ask your hosting provider to upgrade to PHP %2$s or later. If your host backports security fixes to older PHP builds, this warning may not apply.', 'pff-paystack' ), |
| 116 | + esc_html( PHP_VERSION ), |
| 117 | + esc_html( implode( ', ', $fixed ) ) |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Add the Site Health test. |
| 123 | + * |
| 124 | + * @param array $tests Site Health tests. |
| 125 | + * |
| 126 | + * @return array |
| 127 | + */ |
| 128 | + public function add_site_health_test( $tests ) { |
| 129 | + $tests['direct']['pff_paystack_php_version'] = array( |
| 130 | + 'label' => esc_html__( 'Payment Forms for Paystack PHP version', 'pff-paystack' ), |
| 131 | + 'test' => array( $this, 'site_health_result' ), |
| 132 | + ); |
| 133 | + |
| 134 | + return $tests; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Site Health test callback. |
| 139 | + * |
| 140 | + * @return array |
| 141 | + */ |
| 142 | + public function site_health_result() { |
| 143 | + $affected = $this->is_vulnerable(); |
| 144 | + if ( ! $affected ) { |
| 145 | + $status = 'good'; |
| 146 | + } else { |
| 147 | + $status = self::is_distro_build() ? 'recommended' : 'critical'; |
| 148 | + } |
| 149 | + $colors = array( |
| 150 | + 'good' => 'blue', |
| 151 | + 'recommended' => 'orange', |
| 152 | + 'critical' => 'red', |
| 153 | + ); |
| 154 | + |
| 155 | + return array( |
| 156 | + 'label' => $affected |
| 157 | + ? esc_html__( 'PHP version has known critical vulnerabilities', 'pff-paystack' ) |
| 158 | + : esc_html__( 'PHP version is not affected by CVE-2026-109618 or CVE-2026-109619', 'pff-paystack' ), |
| 159 | + 'status' => $status, |
| 160 | + 'badge' => array( |
| 161 | + 'label' => esc_html__( 'Security', 'pff-paystack' ), |
| 162 | + 'color' => $colors[ $status ], |
| 163 | + ), |
| 164 | + 'description' => '<p>' . ( $affected |
| 165 | + ? $this->message() |
| 166 | + : esc_html__( 'This server\'s PHP version is not affected by CVE-2026-109618 or CVE-2026-109619.', 'pff-paystack' ) ) . '</p>', |
| 167 | + 'actions' => sprintf( |
| 168 | + '<p><a href="%s" target="_blank" rel="noopener noreferrer">%s</a></p>', |
| 169 | + esc_url( 'https://www.php.net/supported-versions.php' ), |
| 170 | + esc_html__( 'View supported PHP versions', 'pff-paystack' ) |
| 171 | + ), |
| 172 | + 'test' => 'pff_paystack_php_version', |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Display the admin notice. |
| 178 | + * |
| 179 | + * @return void |
| 180 | + */ |
| 181 | + public function admin_notice() { |
| 182 | + if ( ! current_user_can( Settings::get_capability() ) || ! $this->should_warn() ) { |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + $screen = get_current_screen(); |
| 187 | + if ( ! $screen || ( 'plugins' !== $screen->id && 'paystack_form' !== $screen->post_type ) ) { |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + echo '<div class="notice notice-warning"><p>' . $this->message() . '</p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped in message(). |
| 192 | + } |
| 193 | +} |
0 commit comments