value), wp_version, core_update * (array|null), plugins (inventory|null), themes (inventory|null), constants (array name=>value), * multisite, ssl, next_scheduled (int|false), now (UTC 'Y-m-d H:i:s'), advisories (array), schedule_enabled, * feed_enabled (bool), feed_manifest (array|null), feed_records (array type => slug => Record[]). * * Inventories: array{checked_at:int, items:array}. core_update: array{checked_at:int, latest:string, response:string}. * * @package GSFP */ namespace GSFP\Scanner; if ( ! defined( 'ABSPATH' ) ) { exit; } final class Context { /** @var array */ private $o; /** @param array $overrides */ public function __construct( array $overrides = array() ) { $this->o = $overrides; } private function has( $key ) { return array_key_exists( $key, $this->o ); } public function php_version() { return $this->has( 'php_version' ) ? (string) $this->o['php_version'] : PHP_VERSION; } public function php_sapi() { return $this->has( 'php_sapi' ) ? (string) $this->o['php_sapi'] : PHP_SAPI; } /** @return string|null null when unavailable. */ public function ini( $name ) { if ( $this->has( 'ini' ) ) { return isset( $this->o['ini'][ $name ] ) ? (string) $this->o['ini'][ $name ] : null; } $v = function_exists( 'ini_get' ) ? ini_get( $name ) : false; return false === $v ? null : (string) $v; } public function wp_version() { if ( $this->has( 'wp_version' ) ) { return (string) $this->o['wp_version']; } return function_exists( 'get_bloginfo' ) ? (string) get_bloginfo( 'version' ) : ''; } /** @return array{checked_at:int,latest:string,response:string}|null null when update data is unavailable. */ public function core_update() { if ( $this->has( 'core_update' ) ) { return $this->o['core_update']; } $this->load_admin( 'update' ); $t = function_exists( 'get_site_transient' ) ? get_site_transient( 'update_core' ) : false; if ( ! is_object( $t ) || empty( $t->last_checked ) ) { return null; } $latest = ''; $response = 'latest'; if ( function_exists( 'get_core_updates' ) ) { $updates = get_core_updates(); if ( is_array( $updates ) && ! empty( $updates[0] ) && is_object( $updates[0] ) ) { $response = isset( $updates[0]->response ) ? (string) $updates[0]->response : 'latest'; $latest = isset( $updates[0]->current ) ? (string) $updates[0]->current : ''; } } return array( 'checked_at' => (int) $t->last_checked, 'latest' => $latest, 'response' => $response, ); } /** * @return array{checked_at:int,items:array>}|null * items: file, slug, name, version, active (bool), update (new version or ''). */ public function plugins() { if ( $this->has( 'plugins' ) ) { return $this->o['plugins']; } $this->load_admin( 'plugin' ); if ( ! function_exists( 'get_plugins' ) ) { return null; } $t = get_site_transient( 'update_plugins' ); $checked = is_object( $t ) && ! empty( $t->last_checked ) ? (int) $t->last_checked : 0; $active = (array) get_option( 'active_plugins', array() ); if ( function_exists( 'is_multisite' ) && is_multisite() ) { $active = array_merge( $active, array_keys( (array) get_site_option( 'active_sitewide_plugins', array() ) ) ); } $items = array(); foreach ( (array) get_plugins() as $file => $data ) { $update = ''; if ( is_object( $t ) && isset( $t->response[ $file ]->new_version ) ) { $update = (string) $t->response[ $file ]->new_version; } $slug = false !== strpos( $file, '/' ) ? strstr( $file, '/', true ) : basename( $file, '.php' ); $items[] = array( 'file' => $file, 'slug' => $slug, 'name' => isset( $data['Name'] ) ? (string) $data['Name'] : $slug, 'version' => isset( $data['Version'] ) ? (string) $data['Version'] : '', 'active' => in_array( $file, $active, true ), 'update' => $update, ); } return array( 'checked_at' => $checked, 'items' => $items, ); } /** * @return array{checked_at:int,items:array>}|null * items: slug, name, version, active (bool), parent_of_active (bool), update. */ public function themes() { if ( $this->has( 'themes' ) ) { return $this->o['themes']; } $this->load_admin( 'theme' ); if ( ! function_exists( 'wp_get_themes' ) ) { return null; } $t = get_site_transient( 'update_themes' ); $checked = is_object( $t ) && ! empty( $t->last_checked ) ? (int) $t->last_checked : 0; $active = (string) get_stylesheet(); $template = (string) get_template(); $items = array(); foreach ( wp_get_themes() as $slug => $theme ) { $update = ''; if ( is_object( $t ) && isset( $t->response[ $slug ]['new_version'] ) ) { $update = (string) $t->response[ $slug ]['new_version']; } $items[] = array( 'slug' => (string) $slug, 'name' => (string) $theme->get( 'Name' ), 'version' => (string) $theme->get( 'Version' ), 'active' => (string) $slug === $active, 'parent_of_active' => (string) $slug === $template && $template !== $active, 'update' => $update, ); } return array( 'checked_at' => $checked, 'items' => $items, ); } public function has_constant( $name ) { if ( $this->has( 'constants' ) ) { return array_key_exists( $name, $this->o['constants'] ); } return defined( $name ); } /** @return mixed null when not defined. */ public function constant_value( $name ) { if ( $this->has( 'constants' ) ) { return array_key_exists( $name, $this->o['constants'] ) ? $this->o['constants'][ $name ] : null; } return defined( $name ) ? constant( $name ) : null; } public function is_multisite() { if ( $this->has( 'multisite' ) ) { return (bool) $this->o['multisite']; } return function_exists( 'is_multisite' ) && is_multisite(); } public function is_ssl() { if ( $this->has( 'ssl' ) ) { return (bool) $this->o['ssl']; } return function_exists( 'is_ssl' ) && is_ssl(); } /** @return int|false Unix timestamp of next gsfp_daily_scan, or false. */ public function next_scheduled_scan() { if ( $this->has( 'next_scheduled' ) ) { return $this->o['next_scheduled']; } return function_exists( 'wp_next_scheduled' ) ? wp_next_scheduled( 'gsfp_daily_scan' ) : false; } public function schedule_enabled() { if ( $this->has( 'schedule_enabled' ) ) { return (bool) $this->o['schedule_enabled']; } return class_exists( '\GSFP\Core\Settings' ) && function_exists( 'get_option' ) && (bool) \GSFP\Core\Settings::get( 'schedule_enabled' ); } /** UTC 'Y-m-d H:i:s'. */ public function now() { return $this->has( 'now' ) ? (string) $this->o['now'] : gmdate( 'Y-m-d H:i:s' ); } public function now_ts() { $ts = strtotime( $this->now() . ' UTC' ); return false === $ts ? time() : $ts; } /** * PHP advisory data: the live feed when it is newer than the bundled copy, otherwise the bundled data. * * @return array Empty array if unreadable. */ public function advisories() { if ( $this->has( 'advisories' ) ) { return (array) $this->o['advisories']; } return \GSFP\Feed\Data::pick_advisories( self::load_advisories(), $this->feed_manifest() ); } /** Is the live vulnerability feed switched on? */ public function feed_enabled() { if ( $this->has( 'feed_enabled' ) ) { return (bool) $this->o['feed_enabled']; } return class_exists( '\GSFP\Core\Settings' ) && function_exists( 'get_option' ) && (bool) \GSFP\Core\Settings::get( 'feed_enabled' ); } /** @return array|null Stored feed manifest; null when the feed is off or nothing was fetched yet. */ public function feed_manifest() { if ( $this->has( 'feed_manifest' ) ) { return is_array( $this->o['feed_manifest'] ) ? $this->o['feed_manifest'] : null; } if ( ! function_exists( 'get_option' ) || ! $this->feed_enabled() ) { return null; } return \GSFP\Feed\FeedStore::manifest(); } /** * Stored vulnerability records for an installed plugin or theme. * * @param string $type 'plugin'|'theme'. * @param string $slug Plugin or theme folder name. * @return array> */ public function feed_records( $type, $slug ) { if ( $this->has( 'feed_records' ) ) { $r = $this->o['feed_records']; return isset( $r[ $type ][ strtolower( (string) $slug ) ] ) ? (array) $r[ $type ][ strtolower( (string) $slug ) ] : array(); } if ( ! function_exists( 'get_option' ) || ! $this->feed_enabled() ) { return array(); } return \GSFP\Feed\FeedStore::records( $type, $slug ); } /** @return array */ public static function load_advisories() { $file = ( defined( 'GSFP_DIR' ) ? GSFP_DIR : dirname( __DIR__, 2 ) . '/' ) . 'data/advisories.json'; $json = is_readable( $file ) ? file_get_contents( $file ) : false; // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $data = false === $json ? null : json_decode( $json, true ); return is_array( $data ) ? $data : array(); } private function load_admin( $what ) { $fn = array( 'update' => 'get_core_updates', 'plugin' => 'get_plugins', 'theme' => 'wp_get_themes', ); if ( function_exists( $fn[ $what ] ) || ! defined( 'ABSPATH' ) || ! defined( 'WPINC' ) ) { return; } $file = ABSPATH . 'wp-admin/includes/' . $what . '.php'; if ( is_readable( $file ) ) { require_once $file; } } }