s['path'] = $p[0]; $this->_request_url_fragments['querystring'] = ( empty( $p[1] ) ? '' : '?' . $p[1] ); $this->_request_url_fragments = $this->_normalize_url_fragments( $this->_request_url_fragments ); } private function _normalize_url_fragments( $fragments ) { $fragments = w3tc_apply_filters( 'pagecache_normalize_url_fragments', $fragments ); $fragments['querystring'] = $this->_normalize_querystring( $fragments['querystring'] ); return $fragments; } private function _normalize_querystring( $querystring ) { $ignore_qs = $this->_config->get_array( 'pgcache.accept.qs' ); $ignore_qs = w3tc_apply_filters( 'pagecache_extract_accept_qs', $ignore_qs ); Util_Rule::array_trim( $ignore_qs ); if ( empty( $ignore_qs ) || empty( $querystring ) ) { return $querystring; } $querystring_naked = substr( $querystring, 1 ); foreach ( $ignore_qs as $qs ) { $m = null; if ( strpos( $qs, '=' ) === false ) { $regexp = Util_Environment::preg_quote( str_replace( '+', ' ', $qs ) ); if ( @preg_match( "~^(.*?&|)$regexp(=[^&]*)?(&.*|)$~i", $querystring_naked, $m ) ) { $querystring_naked = $m[1] . $m[3]; } } else { $regexp = Util_Environment::preg_quote( str_replace( '+', ' ', $qs ) ); if ( @preg_match( "~^(.*?&|)$regexp(&.*|)$~i", $querystring_naked, $m ) ) { $querystring_naked = $m[1] . $m[2]; } } } $querystring_naked = preg_replace( '~[&]+~', '&', $querystring_naked ); $querystring_naked = trim( $querystring_naked, '&' ); return empty( $querystring_naked ) ? '' : '?' . $querystring_naked; } /** * */ public function delayed_cache_print() { if ( $this->_late_caching && $this->_caching ) { $this->_cached_data = $this->_extract_cached_page( true ); if ( $this->_cached_data ) { global $w3_late_caching_succeeded; $w3_late_caching_succeeded = true; $this->process_status = 'hit'; $this->process_cached_page_and_exit( $this->_cached_data ); // if is passes here - exit is not possible now and // will happen on init return; } } if ( $this->_late_init && $this->_caching ) { $this->process_status = 'hit'; $this->process_cached_page_and_exit( $this->_cached_data ); // if is passes here - exit is not possible now and // will happen on init return; } } private function _maybe_save_cached_result( $buffer, $response_headers, $has_dynamic ) { $mobile_group = $this->_page_key_extension['useragent']; $referrer_group = $this->_page_key_extension['referrer']; $encryption = $this->_page_key_extension['encryption']; $compression_header = $this->_page_key_extension['compression']; $compressions_to_store = $this->_get_compressions(); /** * Don't compress here for debug mode or dynamic tags * because we need to modify buffer before send it to client */ if ( $this->_debug || $has_dynamic ) { $compressions_to_store = array( false ); } // right now dont return compressed buffer if we are dynamic, // that will happen on shutdown after processing dynamic stuff $compression_of_returned_content = ( $has_dynamic ? false : $compression_header ); $headers = $this->_get_cached_headers( $response_headers['plain'] ); if ( !empty( $headers['Status-Code'] ) ) { $is_404 = ( $headers['Status-Code'] == 404 ); } elseif ( function_exists( 'is_404' ) ) { $is_404 = is_404(); } else { $is_404 = false; } if ( $this->_enhanced_mode ) { // redirect issued, if we have some old cache entries // they will be turned into fresh files and catch further requests if ( isset( $response_headers['kv']['Location'] ) ) { $cache = $this->_get_cache( $this->_page_key_extension['group'] ); foreach ( $compressions_to_store as $_compression ) { $_page_key = $this->_get_page_key( array_merge( $this->_page_key_extension, array( 'compression' => $_compression ) ) ); $cache->hard_delete( $_page_key ); } return $buffer; } } $content_type = ''; if ( $this->_enhanced_mode && !$this->_late_init ) { register_shutdown_function( array( $this, '_check_rules_present' ) ); if ( isset( $response_headers['kv']['Content-Type'] ) ) { $content_type = $response_headers['kv']['Content-Type']; } } $time = time(); $cache = $this->_get_cache( $this->_page_key_extension['group'] ); /** * Store different versions of cache */ $buffers = array(); $something_was_set = false; foreach ( $compressions_to_store as $_compression ) { $this->_set_extract_page_key( array_merge( $this->_page_key_extension, array( 'compression' => $_compression, 'content_type' => $content_type ) ), true ); if ( empty( $this->_page_key ) ) continue; // Compress content $buffers[$_compression] = $this->_compress( $buffer, $_compression ); // Store cache data $_data = array( '404' => $is_404, 'headers' => $headers, 'time' => $time, 'content' => $buffers[$_compression] ); if ( !empty( $_compression ) ) { $_data['c'] = $_compression; } if ( $has_dynamic ) $_data['has_dynamic'] = true; $_data = apply_filters( 'w3tc_pagecache_set', $_data, $this->_page_key, $this->_page_group ); if ( !empty( $_data ) ) { $cache->set( $this->_page_key, $_data, $this->_lifetime, $this->_page_group ); $something_was_set = true; } } if ( $something_was_set ) { $this->process_status = 'miss_fill'; } else { $this->process_status = 'miss_third_party'; } // Change buffer if using compression if ( defined( 'W3TC_PAGECACHE_OUTPUT_COMPRESSION_OFF' ) ) { $compression_header = false; } elseif ( $compression_of_returned_content && isset( $buffers[$compression_of_returned_content] ) ) { $buffer = $buffers[$compression_of_returned_content]; } // Calculate content etag $etag = md5( $buffer ); // Send headers $this->_send_headers( $is_404, $time, $etag, $compression_header, $headers ); return $buffer; } public function w3tc_usage_statistics_of_request( $storage ) { global $w3tc_start_microtime; $time_ms = 0; if ( !empty( $w3tc_start_microtime ) ) { $time_ms = (int)( ( microtime( true ) - $w3tc_start_microtime ) * 1000 ); $storage->counter_add( 'pagecache_requests_time_10ms', (int)( $time_ms / 10 ) ); } if ( !empty( $this->process_status ) ) { // see registered keys in PgCache_Plugin.w3tc_usage_statistics_metrics $storage->counter_add( 'php_requests_pagecache_' . $this->process_status, 1 ); if ( $this->_debug ) { self::log( 'finished in ' . $time_ms . ' size ' . $this->output_size . ' with process status ' . $this->process_status . ' reason ' . $this->cache_reject_reason); } } } /** * Log. */ static protected function log( $msg ) { $data = sprintf( "[%s] [%s] [%s] %s\n", date( 'r' ), isset( $_SERVER['REQUEST_URI'] ) ? filter_var( stripslashes( $_SERVER['REQUEST_URI'] ), FILTER_SANITIZE_URL ) : '', ! empty( $_SERVER['HTTP_REFERER'] ) ? htmlspecialchars( $_SERVER['HTTP_REFERER'] ) : '-', $msg ); $data = strtr( $data, '<>', '..' ); $filename = Util_Debug::log_filename( 'pagecache' ); return @file_put_contents( $filename, $data, FILE_APPEND ); } }