ys = array(); include W3TC_DIR . '/ConfigKeys.php'; if ( is_array( $w3tc_keys ) ) { foreach ( $w3tc_keys as $w3tc_name => $w3tc_descriptor ) { if ( is_array( $w3tc_descriptor ) && isset( $w3tc_descriptor['flags'] ) && is_array( $w3tc_descriptor['flags'] ) && ! empty( $w3tc_descriptor['flags']['directive_string'] ) ) { $set[ $w3tc_name ] = true; } } } } return isset( $set[ $w3tc_key ] ) ? true : null; } /** * Constrains scalar `$w3tc_value` to the enum declared in * {@see ConfigKeys.php} for `$w3tc_key`, when present. * * Schema declares an enum like: * * 'cdn.engine' => array( * 'type' => 'string', * 'default' => '', * 'enum' => array( 'ftp', 's3', ... ), * ), * * Behavior: * * * If the key has no `enum` entry, return `$w3tc_value` unchanged. * * If `$w3tc_value` is in the enum, return it unchanged. * * Otherwise, retain the value already stored under that key * (or the schema `default`) and emit an audit-log entry. The * invalid value never reaches `$this->_data`, so downstream * callers — including the `header()` emitters that * interpolate `cdn.engine` — only ever see an allowlisted * slug. * * Top-level keys only (compound `array( 'extension', 'sub' )` * keys skip enforcement); the schema doesn't currently declare * enums on extension subkeys. * * @since 2.10.0 * * @param string|array $w3tc_key Config key (string for top-level, array for extension subkey). * @param mixed $w3tc_value The candidate value. * @param Config $w3tc_config Config instance, used to look up the prior value as fallback. * * @return mixed The original value if allowed, otherwise the prior stored value (or schema default). */ private static function enforce_enum( $w3tc_key, $w3tc_value, Config $w3tc_config ) { if ( ! \is_string( $w3tc_key ) ) { return $w3tc_value; } $schema = self::config_keys_schema(); if ( ! isset( $schema[ $w3tc_key ]['enum'] ) || ! \is_array( $schema[ $w3tc_key ]['enum'] ) ) { return $w3tc_value; } $enum = $schema[ $w3tc_key ]['enum']; /** * Non-scalar values are an immediate reject path. The earlier * shape coerced them to `''` and then ran the enum match — but * `''` is itself a valid enum member for `cdn.engine`, so an * array / object write (e.g. from a malformed import) would * have been *accepted* by the enum check and overwritten the * prior value. Route non-scalars straight to the fallback * branch below so they never normalise to an allowed slug. */ if ( \is_scalar( $w3tc_value ) ) { $value_string = (string) $w3tc_value; if ( \in_array( $value_string, $enum, true ) ) { /** * Return the normalised string form so the stored * type matches the schema's declared `string` type * even when a caller passes a non-string scalar * (e.g. `true` → `'1'`, `42` → `'42'`) that happens * to string-cast into an allowed enum member. */ return $value_string; } } /** * Reject. Retain whatever was previously stored (or fall * back to the schema default). Audit-log the rejection so * operators can see attempted out-of-enum writes (a CRLF- * bearing value, or a buggy filter widening the surface). */ $fallback = ''; if ( isset( $w3tc_config->_data[ $w3tc_key ] ) ) { $fallback = $w3tc_config->_data[ $w3tc_key ]; } elseif ( isset( $schema[ $w3tc_key ]['default'] ) ) { $fallback = $schema[ $w3tc_key ]['default']; } /** * Allow the autoloader to load `Util_Debug` if it isn't * already in memory (the earlier `class_exists(..., false)` * would skip the audit-log in any request path where * `Util_Debug` hadn't been touched yet — i.e. most enum-write * flows — and silently drop the diagnostic operators rely on). */ if ( \class_exists( __NAMESPACE__ . '\\Util_Debug' ) ) { Util_Debug::log( 'config', \sprintf( 'Rejected out-of-enum write to %s; retained prior value. Allowed: %s', $w3tc_key, \implode( ',', $enum ) ) ); } return $fallback; } /** * Lazy-load the {@see ConfigKeys.php} schema once per request. * * `ConfigKeys.php` populates a local `$w3tc_keys` variable; we * import the file inside an isolated scope and cache the * result so per-write enum lookups don't re-include the file. * * @since 2.10.0 * * @return array> */ private static function config_keys_schema() { static $schema = null; if ( null === $schema ) { $schema = array(); $loader = static function () { $w3tc_keys = array(); include W3TC_DIR . '/ConfigKeys.php'; return $w3tc_keys; }; $schema = (array) $loader(); } return $schema; } /** * Checks if the current configuration is a preview. * * @return bool True if preview mode is enabled, false otherwise. */ public function is_preview() { return $this->_preview; } /** * Checks if the current configuration is the master configuration. * * @return bool True if the configuration is the master, false otherwise. */ public function is_master() { return $this->_is_master; } /** * Checks if the configuration is compiled. * * @return bool True if the configuration is compiled, false otherwise. */ public function is_compiled() { return $this->_compiled; } /** * Sets the default configuration values. * * @return void */ public function set_defaults() { $w3tc_c = new ConfigCompiler( $this->_blog_id, $this->_preview ); $this->_data = $w3tc_c->get_data(); } /** * Saves the current configuration. * * @return void */ public function save() { if ( function_exists( 'do_action' ) ) { do_action( 'w3tc_config_save', $this ); } $w3tc_c = new ConfigCompiler( $this->_blog_id, $this->_preview ); $w3tc_c->apply_data( $this->_data ); $w3tc_c->save(); } /** * Checks if a configuration key is sealed (immutable). * * @param string $w3tc_key The configuration key to check. * * @return bool True if the key is sealed, false otherwise. */ public function is_sealed( $w3tc_key ) { if ( $this->is_master() ) { return false; } // better to use master config data here, but its faster and preciese enough for UI. return ConfigCompiler::child_key_sealed( $w3tc_key, $this->_data, $this->_data ); } /** * Exports the current configuration as a JSON string. * * @return string The configuration data as a JSON string. */ public function export() { $export_data = $this->_data; // Export files are often copied between environments; omit stored credential values. foreach ( \array_keys( self::secret_keys() ) as $w3tc_key ) { if ( isset( $export_data[ $w3tc_key ] ) && '' !== $export_data[ $w3tc_key ] ) { $export_data[ $w3tc_key ] = ''; } } /** * JSON_HEX_TAG / HEX_AMP / HEX_APOS / HEX_QUOT escape `<` `&` `'` * `"` to their `\uXXXX` forms. The export endpoint serves this * body to admins, and even with `Content-Type: application/json` * some clients can render the response as HTML (history walks, * view-source, intermediary proxies that ignore the content- * type). Hex-escaping every HTML-significant character means * the body cannot contain a literal `