(int) $max_length || 1 === preg_match( '/[\x00-\x1F\x7F]/', $value ) ) { return null; } return $value; } /** * Reads a configuration file: a first line that stops PHP ("", so requesting the file over * the web shows nothing), then JSON. It is read as text, never included, because PHP's opcode cache * belongs to each process: a file rewritten by WP-CLI or cron could otherwise stay stale in the web * server's cache until PHP restarts. * * @param string|false $raw File contents. * @return array|null */ function gorilla_cache_engine_parse_config( $raw ) { if ( ! is_string( $raw ) || false === strpos( $raw, "\n" ) ) { return null; } $config = json_decode( substr( $raw, strpos( $raw, "\n" ) + 1 ), true ); return is_array( $config ) ? $config : null; } /** * Handles the current request. Exits after a cache hit. * * @param string $config_dir Folder holding one generated ".php" configuration per known host. * @return string "miss" or "bypass". */ function gorilla_cache_engine_boot( $config_dir ) { // Only what the engine uses: nine request values and the cookie names (never their values), each sanitised where // it is read. GorillaCache_Engine_Request then validates them. $server = array(); foreach ( array( 'REQUEST_METHOD', 'HTTPS', 'SERVER_PORT', 'HTTP_HOST', 'REQUEST_URI', 'HTTP_ACCEPT', 'HTTP_USER_AGENT', 'HTTP_X_GORILLA_CACHE_PROBE', 'HTTP_X_GORILLA_CACHE_WARM' ) as $key ) { if ( isset( $_SERVER[ $key ] ) ) { $value = gorilla_cache_engine_sanitize( $_SERVER[ $key ], 'REQUEST_URI' === $key ? 8192 : 2048 ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- presubmit-allow: check-10 (read before WordPress loads: wp_unslash() and sanitize_*() do not exist yet; gorilla_cache_engine_sanitize() on this line refuses control characters and over-long values) if ( null === $value ) { return 'bypass'; } $server[ $key ] = $value; } } $cookies = array(); foreach ( array_keys( $_COOKIE ) as $name ) { $name = gorilla_cache_engine_sanitize( $name, 256 ); if ( null === $name ) { return 'bypass'; } $cookies[ $name ] = ''; } $request = GorillaCache_Engine_Request::from_arrays( $server, $cookies ); if ( '' === $request->host ) { return 'bypass'; } $config_file = rtrim( (string) $config_dir, '/' ) . '/' . str_replace( ':', '_', $request->host ) . '.php'; $config = gorilla_cache_engine_parse_config( is_readable( $config_file ) ? file_get_contents( $config_file ) : false ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- runs before WordPress loads. if ( ! is_array( $config ) ) { return 'bypass'; } // Settings written by another install (a copy of the files on another server, folder, database or // environment): serve nothing, store nothing, until this install has written its own. if ( isset( $config['site'] ) && GorillaCache_Engine_Rules::current_site_hash() !== $config['site'] ) { if ( ! defined( 'GORILLA_CACHE_OTHER_INSTALL' ) ) { define( 'GORILLA_CACHE_OTHER_INSTALL', true ); // The plugin starts fresh in this same request. } if ( ! headers_sent() ) { header( 'X-Gorilla-Cache: BYPASS (other_install)' ); } return 'bypass'; } $result = GorillaCache_Engine::start( $config, $server, $cookies ); if ( 'hit' === $result ) { exit; } return $result; }