Ticket #517: patch-headers.diff

File patch-headers.diff, 4.0 KB (added by boredzo, 5 years ago)

Patch implementing a replacement for apache_response_headers, renaming the shutdown callback, and relocating the creation of $meta_object.

  • wp-cache-phase2.php

     
    3636                return; 
    3737        if (wp_cache_user_agent_is_rejected()) return; 
    3838        ob_start('wp_cache_ob_callback');  
    39         register_shutdown_function('wp_cache_ob_end'); 
     39        register_shutdown_function('wp_cache_shutdown_callback'); 
    4040} 
    4141 
     42function wp_cache_get_response_headers() { 
     43        if(function_exists('apache_response_headers')) { 
     44                $headers = apache_response_headers(); 
     45        } else if(function_exists('headers_list')) { 
     46                $headers = array(); 
     47                foreach(headers_list() as $hdr) { 
     48                        list($header_name, $header_value) = explode(': ', $hdr, 2); 
     49                        $headers[$header_name] = $header_value; 
     50                } 
     51        } else 
     52                $headers = null; 
     53 
     54        return $headers; 
     55} 
     56 
    4257function wp_cache_is_rejected($uri) { 
    4358        global $cache_rejected_uri; 
    4459 
     
    110125                return $buffer; 
    111126        } 
    112127 
    113         $meta_object = new CacheMeta; 
    114         $meta_object->uri = $_SERVER["SERVER_NAME"].preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', $_SERVER['REQUEST_URI']); // To avoid XSS attacs 
    115         $meta_object->blog_id=$blog_id; 
    116         $meta_object->post = wp_cache_post_id(); 
    117128        $duration = wp_cache_microtime_diff($wp_start_time, microtime()); 
    118129        $duration = sprintf("%0.3f", $duration); 
    119130        $buffer .= "\n<!-- Dynamic Page Served (once) in $duration seconds -->\n"; 
     
    183194        wp_cache_writers_exit(); 
    184195} 
    185196 
    186 function wp_cache_ob_end() { 
     197function wp_cache_shutdown_callback() { 
    187198        global $cache_path, $cache_max_time, $file_expired, $file_prefix, $meta_file, $new_cache; 
    188199        global $meta_object, $known_headers; 
    189200 
    190         /* Preparing... with PHP5 is straightforward, use headers_list() */ 
    191         if(function_exists('apache_response_headers') ) { 
    192                 $response = apache_response_headers(); 
    193                 $meta_object->headers = array(); 
    194                 foreach ($known_headers as $key) { 
    195                         if(isset($response{$key})) { 
    196                                 array_push($meta_object->headers, "$key: " . $response{$key}); 
    197                         } 
     201        $meta_object = new CacheMeta; 
     202        $meta_object->uri = $_SERVER["SERVER_NAME"].preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', $_SERVER['REQUEST_URI']); // To avoid XSS attacs 
     203        $meta_object->blog_id=$blog_id; 
     204        $meta_object->post = wp_cache_post_id(); 
     205 
     206        $response = wp_cache_get_response_headers(); 
     207        $meta_object->headers = array(); 
     208        foreach ($known_headers as $key) { 
     209                if(isset($response{$key})) { 
     210                        array_push($meta_object->headers, "$key: " . $response{$key}); 
    198211                } 
    199                 /* Not use because it gives problems with some 
    200                  * PHP installations 
    201                 if (!$response{'Content-Length'}) { 
    202                 // WP does not set content size 
    203                         $content_size = ob_get_length(); 
    204                         @header("Content-Length: $content_size"); 
    205                         array_push($meta_object->headers, "Content-Length: $content_size"); 
    206                 } 
    207                 */ 
    208                 if (!$response{'Last-Modified'}) { 
    209                         $value = gmdate('D, d M Y H:i:s') . ' GMT'; 
    210                         /* Dont send this the first time */ 
    211                         /* @header('Last-Modified: ' . $value); */ 
    212                         array_push($meta_object->headers, "Last-Modified: $value"); 
    213                 } 
    214                 if (!$response{'Content-Type'}) { 
    215                         $value =  "text/html; charset=\"" . get_settings('blog_charset')  . "\""; 
    216                         @header("Content-Type: $value"); 
    217                         array_push($meta_object->headers, "Content-Type: $value"); 
    218                 } 
    219212        } 
     213        /* Not use because it gives problems with some 
     214         * PHP installations 
     215        if (!$response{'Content-Length'}) { 
     216        // WP does not set content size 
     217                $content_size = ob_get_length(); 
     218                @header("Content-Length: $content_size"); 
     219                array_push($meta_object->headers, "Content-Length: $content_size"); 
     220        } 
     221        */ 
     222        if (!$response{'Last-Modified'}) { 
     223                $value = gmdate('D, d M Y H:i:s') . ' GMT'; 
     224                /* Dont send this the first time */ 
     225                /* @header('Last-Modified: ' . $value); */ 
     226                array_push($meta_object->headers, "Last-Modified: $value"); 
     227        } 
     228        if (!$response{'Content-Type'}) { 
     229                $value =  "text/html; charset=\"" . get_settings('blog_charset')  . "\""; 
     230                @header("Content-Type: $value"); 
     231                array_push($meta_object->headers, "Content-Type: $value"); 
     232        } 
     233 
    220234        ob_end_clean(); 
    221235        if ($new_cache) { 
    222236                $serial = serialize($meta_object);