( isset( $request_headers['Accept'] ) ) ? $request_headers['Accept'] : ( ( isset( $_SERVER[ 'HTTP_ACCEPT' ] ) ) ? $_SERVER[ 'HTTP_ACCEPT' ] : '' ), 'Accept-Encoding' => ( isset( $request_headers['Accept-Encoding'] ) ) ? $request_headers['Accept-Encoding'] : ( ( isset( $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] ) ) ? $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] : '' ), 'Host' => ( isset( $request_headers['Host'] ) ) ? $request_headers['Host'] : ( ( isset( $_SERVER[ 'HTTP_HOST' ] ) ) ? $_SERVER[ 'HTTP_HOST' ] : '' ), 'If-Modified-Since' => ( isset( $request_headers['If-Modified-Since'] ) ) ? $request_headers['If-Modified-Since'] : ( ( isset( $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] ) ) ? $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] : '' ), 'User-Agent' => ( isset( $request_headers['User-Agent'] ) ) ? $request_headers['User-Agent'] : ( ( isset( $_SERVER[ 'HTTP_USER_AGENT' ] ) ) ? $_SERVER[ 'HTTP_USER_AGENT' ] : '' ), 'X-Forwarded-Proto' => ( isset( $request_headers['X-Forwarded-Proto'] ) ) ? $request_headers['X-Forwarded-Proto'] : ( ( isset( $_SERVER[ 'HTTP_X_FORWARDED_PROTO' ] ) ) ? $_SERVER[ 'HTTP_X_FORWARDED_PROTO' ] : '' ), 'X-Forwarded-Scheme' => ( isset( $request_headers['X-Forwarded-Scheme'] ) ) ? $request_headers['X-Forwarded-Scheme'] : ( ( isset( $_SERVER[ 'HTTP_X_FORWARDED_SCHEME' ] ) ) ? $_SERVER[ 'HTTP_X_FORWARDED_SCHEME' ] : '' ), ]; return $request_headers; } /** * check if directory index file * * @return boolean true if directory index file, false otherwise */ private static function is_index() { if ( strtolower( basename( $_SERVER['SCRIPT_NAME'] ) ) === 'index.php' ) { return true; } return false; } /** * check if page can be cached * * @param string $page_contents contents of a page from the output buffer * @return boolean true if page contents are cacheable, false otherwise */ private static function is_cacheable($page_contents) { $has_html_tag = ( stripos( $page_contents, '/i', ltrim( $page_contents ) ); $has_xsl_stylesheet = ( stripos( $page_contents, ' $value) { if ( preg_match( $cookies_regex, $key ) ) { return true; } } } return false; } /** * check if search page * * @return boolean true if search page, false otherwise */ private static function is_search() { if ( apply_filters( 'a2opt_cache_exclude_search', is_search() ) ) { return true; } return false; } /** * check if mobile template * * @return boolean true if mobile template, false otherwise */ private static function is_mobile() { return ( strpos( TEMPLATEPATH, 'wptouch' ) || strpos( TEMPLATEPATH, 'carrington' ) || strpos( TEMPLATEPATH, 'jetpack' ) || strpos( TEMPLATEPATH, 'handheld' ) ); } /** * check if cache should be bypassed * * @return boolean true if cache should be bypassed, false otherwise */ private static function bypass_cache() { // bypass cache hook if ( apply_filters( 'a2opt_cache_bypass_cache', false ) ) { return true; } // check request method if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || $_SERVER['REQUEST_METHOD'] !== 'GET' ) { return true; } // check HTTP status code if ( http_response_code() !== 200 ) { return true; } // check DONOTCACHEPAGE constant if ( defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE ) { return true; } // check conditional tags if ( self::is_wrong_permalink_structure() || self::is_excluded() ) { return true; } // check conditional tags when output buffering has ended if ( class_exists( 'WP' ) ) { if ( is_admin() || self::is_search() || is_feed() || is_trackback() || is_robots() || is_preview() || post_password_required() || self::is_mobile() ) { return true; } } return false; } /** * deliver cache * * @return boolean false if cached page was not delivered */ public static function deliver_cache() { $cache_file = A2_Optimized_Cache_Disk::get_cache(); if ( A2_Optimized_Cache_Disk::cache_exists( $cache_file ) && ! A2_Optimized_Cache_Disk::cache_expired( $cache_file ) && ! self::bypass_cache() ) { // set X-Cache-Handler response header header( 'X-Cache-Handler: cache-enabler-engine' ); // return 304 Not Modified with empty body if applicable if ( strtotime( self::$request_headers['If-Modified-Since'] >= filemtime( $cache_file ) ) ) { header( $_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified', true, 304 ); exit; } // set Content-Encoding response header if applicable if ( strpos( basename( $cache_file ), 'gz' ) !== false ) { header( 'Content-Encoding: gzip' ); } // deliver cache readfile( $cache_file ); exit; } return false; } }