-
Notifications
You must be signed in to change notification settings - Fork 12
/
cache.php
130 lines (103 loc) · 2.62 KB
/
cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Cache Content
*
* This file is loaded when there's a chance the request content should be
* saved to cache.
*
* @package Surge
*/
namespace Surge;
if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
return;
}
include_once( __DIR__ . '/common.php' );
/**
* The main output buffer callback.
*
* @param string $contents The buffer contents.
*
* @return string Contents.
*/
$ob_callback = function( $contents ) {
$ttl = config( 'ttl' );
if ( $ttl < 1 ) {
header( 'X-Cache: bypass' );
return $contents;
}
$skip = false;
$headers = [];
foreach ( headers_list() as $header ) {
list( $name, $value ) = array_map( 'trim', explode( ':', $header, 2 ) );
// Do not store or vary on these headers.
if ( in_array( strtolower( $name ), ['x-cache', 'x-powered-by'] ) ) {
continue;
}
$headers[ $name ][] = $value;
if ( strtolower( $name ) == 'set-cookie' ) {
$skip = true;
break;
}
if ( strtolower( $name ) == 'cache-control' ) {
if ( stripos( $value, 'no-cache' ) !== false || stripos( $value, 'max-age=0' ) !== false ) {
$skip = true;
break;
}
}
}
if ( ! empty( $_SERVER['HTTP_AUTHORIZATION'] ) ) {
$skip = true;
}
if ( ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), [ 'GET', 'HEAD' ] ) ) {
$skip = true;
}
if ( ! in_array( http_response_code(), [ 200, 301, 302, 404 ] ) ) {
$skip = true;
}
if ( defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE ) {
$skip = true;
}
if ( $skip ) {
header( 'X-Cache: bypass' );
return $contents;
}
$key = key();
$meta = [
'code' => http_response_code(),
'headers' => $headers,
'created' => time(),
'expires' => time() + $ttl,
'flags' => array_unique( flag() ),
'path' => $key['path'],
];
$meta_json = json_encode( $meta );
$cache_key = md5( json_encode( $key ) );
$level = substr( $cache_key, -2 );
if ( ! wp_mkdir_p( CACHE_DIR . "/{$level}/" ) ) {
return $contents;
}
// Open a new cache file.
$hash = wp_generate_password( 6, false );
$f = fopen( CACHE_DIR . "/{$level}/{$cache_key}.{$hash}.php", 'xb' );
// Could not create file.
if ( false === $f ) {
header( 'X-Cache: bypass' );
return $contents;
}
fwrite( $f, '<?php exit; ?>' );
fwrite( $f, pack( 'L', strlen( $meta_json ) ) );
fwrite( $f, $meta_json );
fwrite( $f, $contents );
// Close the file.
fclose( $f );
// Atomic (hopefully) rename.
if ( ! rename( CACHE_DIR . "/{$level}/{$cache_key}.{$hash}.php",
CACHE_DIR . "/{$level}/{$cache_key}.php" )
) {
unlink( CACHE_DIR . "/{$level}/{$cache_key}.{$hash}.php" );
}
event( 'request', [ 'meta' => $meta ] );
return $contents;
};
// Attach to main output buffer.
ob_start( $ob_callback );