forked from Automattic/wp-super-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-cache-phase2.php
3577 lines (3107 loc) · 121 KB
/
wp-cache-phase2.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
* WP Super Cache Phase 2 file.
* This file is included by the files wp-cache.php and wp-cache-phase1.php
* It has all the code for caching and serving requests.
*/
// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_is_writable -- TODO: Fix or determine for sure that these should not be fixed.
// phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_fwrite -- TODO: Fix or determine for sure that these should not be fixed.
function gzip_accepted() {
if ( 1 == ini_get( 'zlib.output_compression' ) || 'on' == strtolower( ini_get( 'zlib.output_compression' ) ) ) { // don't compress WP-Cache data files when PHP is already doing it
return false;
}
if ( ! isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) || ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) && strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) === false ) ) {
return false;
}
return 'gzip';
}
function setup_blog_cache_dir() {
global $blog_cache_dir, $cache_path;
if ( false == @is_dir( $blog_cache_dir ) ) {
@mkdir( $cache_path . 'blogs' );
@mkdir( $blog_cache_dir );
}
if ( false == @is_dir( $blog_cache_dir . 'meta' ) ) {
@mkdir( $blog_cache_dir . 'meta' );
}
}
function get_wp_cache_key( $url = false ) {
global $wp_cache_request_uri, $wp_cache_gzip_encoding, $WPSC_HTTP_HOST;
if ( ! $url ) {
$url = $wp_cache_request_uri;
}
$server_port = isset( $_SERVER['SERVER_PORT'] ) ? intval( $_SERVER['SERVER_PORT'] ) : 0;
// Prepare a tag to include in the cache key if the request is anything other than text/html
$accept = wpsc_get_accept_header();
if ( $accept === 'text/html' ) {
$accept_tag = '';
} else {
$accept_tag = '-' . $accept;
}
return do_cacheaction(
'wp_cache_key',
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
wp_cache_check_mobile( $WPSC_HTTP_HOST . $server_port . preg_replace( '/#.*$/', '', str_replace( '/index.php', '/', $url ) ) . $wp_cache_gzip_encoding . wp_cache_get_cookies_values() . '-' . wpsc_get_accept_header() . $accept_tag )
);
}
/**
* Parse a partial URL (only the path and query components).
*
* @param string $partial_uri - The path and query component of a URI to parse.
*/
function wpsc_parse_partial_url( $partial_uri ) {
global $WPSC_HTTP_HOST;
$scheme = wpsc_is_https() ? 'https://' : 'http://';
return parse_url( $scheme . $WPSC_HTTP_HOST . $partial_uri );
}
function wpsc_remove_tracking_params_from_uri( $uri ) {
global $wpsc_tracking_parameters, $wpsc_ignore_tracking_parameters;
if ( ! isset( $wpsc_ignore_tracking_parameters ) || ! $wpsc_ignore_tracking_parameters ) {
return $uri;
}
if ( ! isset( $wpsc_tracking_parameters ) || empty( $wpsc_tracking_parameters ) ) {
return $uri;
}
$parsed_url = wpsc_parse_partial_url( $uri );
$query = array();
if ( isset( $parsed_url['query'] ) ) {
parse_str( $parsed_url['query'], $query );
foreach ( $wpsc_tracking_parameters as $param_name ) {
unset( $query[ $param_name ] );
unset( $_GET[ $param_name ] );
}
}
$path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : '';
$query = ! empty( $query ) ? '?' . http_build_query( $query ) : '';
if ( empty( $_GET ) ) {
$_SERVER['REQUEST_URI'] = strtok( $_SERVER['REQUEST_URI'], '?' );
}
if ( $uri !== $path . $query ) {
wp_cache_debug( 'Removed tracking parameters from URL. Returning ' . $path . $query );
}
return $path . $query;
}
function wp_super_cache_init() {
global $wp_cache_key, $key, $blogcacheid, $file_prefix, $blog_cache_dir, $meta_file, $cache_file, $cache_filename, $meta_pathname;
$wp_cache_key = get_wp_cache_key();
$key = $blogcacheid . md5( $wp_cache_key );
$wp_cache_key = $blogcacheid . $wp_cache_key;
$cache_filename = $file_prefix . $key . '.php';
$meta_file = $file_prefix . $key . '.php';
$cache_file = wpsc_get_realpath( $blog_cache_dir );
if ( $cache_file ) {
$cache_file .= '/' . $cache_filename;
}
$meta_pathname = wpsc_get_realpath( $blog_cache_dir . 'meta/' );
if ( $meta_pathname ) {
$meta_pathname .= '/' . $meta_file;
}
return compact( 'key', 'cache_filename', 'meta_file', 'cache_file', 'meta_pathname' );
}
function wp_cache_serve_cache_file() {
global $key, $blogcacheid, $wp_cache_request_uri, $file_prefix, $blog_cache_dir, $meta_file, $cache_file, $cache_filename, $meta_pathname, $wp_cache_gzip_encoding, $meta;
global $cache_compression, $wp_cache_slash_check, $wp_supercache_304, $wp_cache_home_path, $wp_cache_no_cache_for_get;
global $wp_cache_disable_utf8, $wp_cache_mfunc_enabled, $wpsc_served_header;
if ( wpsc_is_backend() ) {
wp_cache_debug( 'Not serving wp-admin requests.', 5 );
return false;
}
if ( $wp_cache_no_cache_for_get && wpsc_is_get_query() ) {
wp_cache_debug( 'Non empty GET request. Caching disabled on settings page. ' . wpsc_dump_get_request(), 1 );
return false;
}
if ( defined( 'WPSC_SERVE_DISABLED' ) ) {
wp_cache_debug( 'wp_cache_serve_cache_file: WPSC_SERVE_DISABLED defined. Not serving cached files.' );
return false;
}
if ( wpsc_get_accept_header() !== 'text/html' ) {
wp_cache_debug( 'wp_cache_serve_cache_file: visitor does not accept text/html. Not serving cached file.' );
return false;
}
extract( wp_super_cache_init() ); // $key, $cache_filename, $meta_file, $cache_file, $meta_pathname
// Look for wp-cache file + meta file for the current URL
// If we can't find them, we will look for supercache html files. These files don't have any meta data
// which is why the code below does more work setting up the headers, etc.
if (
! defined( 'WPSC_SUPERCACHE_ONLY' ) &&
(
( $cache_file && file_exists( $cache_file ) ) ||
file_exists( get_current_url_supercache_dir() . 'meta-' . $cache_filename )
)
) {
if ( file_exists( get_current_url_supercache_dir() . 'meta-' . $cache_filename ) ) {
$cache_file = get_current_url_supercache_dir() . $cache_filename;
$meta_pathname = get_current_url_supercache_dir() . 'meta-' . $cache_filename;
} elseif ( ! file_exists( $cache_file ) ) {
wp_cache_debug( 'wp_cache_serve_cache_file: found cache file but then it disappeared!' );
return false;
}
if ( ! $meta_pathname ) {
wp_cache_debug( 'wp_cache_serve_cache_file: meta pathname is empty. Could not load wp-cache meta file.' );
return true;
}
wp_cache_debug( "wp-cache file exists: $cache_file", 5 );
if ( ! ( $meta = json_decode( wp_cache_get_legacy_cache( $meta_pathname ), true ) ) ) {
wp_cache_debug( "couldn't load wp-cache meta file", 5 );
return true;
}
if ( is_array( $meta ) == false ) {
wp_cache_debug( "meta array corrupt, deleting $meta_pathname and $cache_file", 1 );
@unlink( $meta_pathname );
@unlink( $cache_file );
return true;
}
} else { // no wp-cache file, look for a supercache file
global $wpsc_save_headers;
global $cache_max_time;
// last chance, check if a supercache file exists. Just in case .htaccess rules don't work on this host
$file = get_current_url_supercache_dir() . supercache_filename();
if ( false == file_exists( $file ) ) {
wp_cache_debug( "No Super Cache file found for current URL: $file" );
return false;
} elseif ( wpsc_is_get_query() ) {
wp_cache_debug( 'GET array not empty. Cannot serve a supercache file. ' . wpsc_dump_get_request() );
return false;
} elseif ( wp_cache_get_cookies_values() != '' ) {
wp_cache_debug( 'Cookies found. Cannot serve a supercache file. ' . wp_cache_get_cookies_values() );
return false;
} elseif ( wpsc_get_accept_header() !== 'text/html' ) {
wp_cache_debug( 'Accept header is not text/html. Cannot serve supercache file.' . wp_cache_get_cookies_values() );
return false;
} elseif ( isset( $wpsc_save_headers ) && $wpsc_save_headers ) {
wp_cache_debug( 'Saving headers. Cannot serve a supercache file.' );
return false;
} elseif ( $cache_max_time > 0 && ( filemtime( $file ) + $cache_max_time ) < time() ) {
wp_cache_debug( sprintf( 'Cache has expired and is older than %d seconds old.', $cache_max_time ) );
return false;
}
if ( isset( $wp_cache_mfunc_enabled ) == false ) {
$wp_cache_mfunc_enabled = 0;
}
if ( false == isset( $wp_cache_home_path ) ) {
$wp_cache_home_path = '/';
}
// make sure ending slashes are ok
if ( $wp_cache_request_uri == $wp_cache_home_path || ( $wp_cache_slash_check && substr( $wp_cache_request_uri, -1 ) == '/' ) || ( $wp_cache_slash_check == 0 && substr( $wp_cache_request_uri, -1 ) != '/' ) ) {
if ( $wp_cache_mfunc_enabled == 0 ) {
// get data from file
if ( $wp_cache_gzip_encoding ) {
if ( file_exists( $file . '.gz' ) ) {
$cachefiledata = file_get_contents( $file . '.gz' );
wp_cache_debug( "Fetched gzip static page data from supercache file using PHP. File: $file.gz" );
} else {
$cachefiledata = gzencode( file_get_contents( $file ), 6, FORCE_GZIP );
wp_cache_debug( "Fetched static page data from supercache file using PHP and gzipped it. File: $file" );
}
} else {
$cachefiledata = file_get_contents( $file );
wp_cache_debug( "Fetched static page data from supercache file using PHP. File: $file" );
}
} else {
// get dynamic data from filtered file
$cachefiledata = do_cacheaction( 'wpsc_cachedata', file_get_contents( $file ) );
if ( $wp_cache_gzip_encoding ) {
$cachefiledata = gzencode( $cachefiledata, 6, FORCE_GZIP );
wp_cache_debug( "Fetched dynamic page data from supercache file using PHP and gzipped it. File: $file" );
} else {
wp_cache_debug( "Fetched dynamic page data from supercache file using PHP. File: $file" );
}
}
if ( isset( $wp_cache_disable_utf8 ) == false || $wp_cache_disable_utf8 == 0 ) {
header( 'Content-type: text/html; charset=UTF-8' );
}
if ( defined( 'WPSC_VARY_HEADER' ) ) {
if ( WPSC_VARY_HEADER != '' ) {
header( 'Vary: ' . WPSC_VARY_HEADER );
}
} else {
header( 'Vary: Accept-Encoding, Cookie' );
}
if ( defined( 'WPSC_CACHE_CONTROL_HEADER' ) ) {
if ( WPSC_CACHE_CONTROL_HEADER != '' ) {
header( 'Cache-Control: ' . WPSC_CACHE_CONTROL_HEADER );
}
} else {
header( 'Cache-Control: max-age=3, must-revalidate' );
}
$size = ( function_exists( 'mb_strlen' ) && function_exists( 'is_utf8_charset' ) ) ? mb_strlen( $cachefiledata, '8bit' ) : strlen( $cachefiledata );
if ( $wp_cache_gzip_encoding ) {
if ( isset( $wpsc_served_header ) && $wpsc_served_header ) {
header( 'X-WP-Super-Cache: Served supercache gzip file from PHP' );
}
header( 'Content-Encoding: ' . $wp_cache_gzip_encoding );
header( 'Content-Length: ' . $size );
} elseif ( $wp_supercache_304 ) {
if ( isset( $wpsc_served_header ) && $wpsc_served_header ) {
header( 'X-WP-Super-Cache: Served supercache 304 file from PHP' );
}
header( 'Content-Length: ' . $size );
} elseif ( isset( $wpsc_served_header ) && $wpsc_served_header ) {
header( 'X-WP-Super-Cache: Served supercache file from PHP' );
}
// don't try to match modified dates if using dynamic code.
if ( $wp_cache_mfunc_enabled == 0 && $wp_supercache_304 ) {
wp_cache_debug( 'wp_cache_serve_cache_file: checking age of cached vs served files.' );
$headers = apache_request_headers();
$remote_mod_time = isset( $headers['If-Modified-Since'] ) ? $headers['If-Modified-Since'] : null;
if ( $remote_mod_time === null && isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
$remote_mod_time = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
}
$local_mod_time = gmdate( 'D, d M Y H:i:s', filemtime( $file ) ) . ' GMT';
if ( $remote_mod_time !== null && $remote_mod_time == $local_mod_time ) {
wp_cache_debug( 'wp_cache_serve_cache_file: Send 304 Not Modified header.' );
header( $_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified' );
exit();
} else {
wp_cache_debug( 'wp_cache_serve_cache_file: 304 browser caching not possible as timestamps differ.' );
}
header( 'Last-Modified: ' . $local_mod_time );
}
echo $cachefiledata;
exit();
} else {
wp_cache_debug( 'No wp-cache file exists. Must generate a new one.' );
return false;
}
}
$cache_file = do_cacheaction( 'wp_cache_served_cache_file', $cache_file );
// Sometimes the gzip headers are lost. Make sure html returned isn't compressed!
$do_not_serve_gzip_data = true;
if ( $cache_compression && $wp_cache_gzip_encoding ) {
if ( ! in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $meta['headers'], true ) ) {
wp_cache_debug( 'GZIP headers not found. Force uncompressed output.' );
} else {
$do_not_serve_gzip_data = false;
if ( isset( $meta['dynamic'] ) ) {
unset( $meta['headers']['Content-Length'] ); // this is set later after the output data is compressed
}
wp_cache_debug( 'GZIP headers found. Serving compressed output.' );
}
}
foreach ( $meta['headers'] as $t => $header ) {
// godaddy fix, via http://blog.gneu.org/2008/05/wp-supercache-on-godaddy/ and http://www.littleredrails.com/blog/2007/09/08/using-wp-cache-on-godaddy-500-error/
if ( strpos( $header, 'Last-Modified:' ) === false ) {
header( $header );
wp_cache_debug( 'Sending Header: ' . $header );
}
}
if ( isset( $wpsc_served_header ) && $wpsc_served_header ) {
header( 'X-WP-Super-Cache: Served WPCache cache file' );
}
if ( isset( $meta['dynamic'] ) ) {
wp_cache_debug( 'Serving wp-cache dynamic file', 5 );
if ( $do_not_serve_gzip_data ) {
// attempt to uncompress the cached file just in case it's gzipped
$cache = wp_cache_get_legacy_cache( $cache_file );
$uncompressed = @gzuncompress( $cache );
if ( $uncompressed ) {
wp_cache_debug( 'Uncompressed gzipped cache from wp-cache: ' . $cache_file );
$cache = $uncompressed;
unset( $uncompressed );
}
$cache = do_cacheaction( 'wpsc_cachedata', $cache );
} else {
wp_cache_debug( 'Compressed cache data from wp-cache: ' . $cache_file );
$cache = gzencode(
do_cacheaction(
'wpsc_cachedata',
wp_cache_get_legacy_cache( $cache_file )
),
6,
FORCE_GZIP
);
$size = ( function_exists( 'mb_strlen' ) && function_exists( 'is_utf8_charset' ) ) ? mb_strlen( $cache, '8bit' ) : strlen( $cache );
wp_cache_debug( 'Sending Header: Content-Length: ' . $size );
header( 'Content-Length: ' . $size );
}
} elseif ( $do_not_serve_gzip_data ) {
$cache = wp_cache_get_legacy_cache( $cache_file );
$uncompressed = @gzuncompress( $cache ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- there is a small chance the cache isn't gzipped despite being configured to be.
if ( $uncompressed ) {
$cache = $uncompressed;
unset( $uncompressed );
wp_cache_debug( 'Uncompressed gzipped cache data from wp-cache file: ' . $cache_file );
} else {
wp_cache_debug( 'Sending already uncompressed cache file from wp-cache to browser: ' . $cache_file );
}
} else {
wp_cache_debug( 'Sending wp-cache file to browser: ' . $cache_file );
$cache = wp_cache_get_legacy_cache( $cache_file );
}
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- this is the cached version of the current page. It will have been escaped already.
echo $cache;
wp_cache_debug( 'exit request', 5 );
die();
}
function wp_cache_get_legacy_cache( $cache_file ) {
return substr( @file_get_contents( $cache_file ), 15 );
}
function wp_cache_postload() {
global $cache_enabled, $wp_super_cache_late_init;
global $wp_cache_request_uri;
if ( empty( $wp_cache_request_uri ) ) {
wp_cache_debug( 'wp_cache_postload: no request uri configured. Not running.' );
return false;
}
// have to sanitize here because formatting.php is loaded after wp_cache_request_uri is set
$wp_cache_request_uri = esc_url_raw( wp_unslash( $wp_cache_request_uri ) );
if ( ! $cache_enabled ) {
return true;
}
if ( isset( $wp_super_cache_late_init ) && true == $wp_super_cache_late_init ) {
wp_cache_debug( 'Supercache Late Init: add wp_cache_serve_cache_file to init', 3 );
add_action( 'init', 'wp_cache_late_loader', 9999 );
} else {
wp_super_cache_init();
wp_cache_phase2();
}
}
function wp_cache_late_loader() {
wp_cache_debug( 'Supercache Late Loader running on init', 3 );
wp_cache_serve_cache_file();
wp_cache_phase2();
}
function wpsc_get_auth_cookies() {
static $cached_cookies;
if ( isset( $cached_cookies ) && is_array( $cached_cookies ) ) {
return $cached_cookies;
}
$cookies = array_keys( $_COOKIE );
if ( empty( $cookies ) ) {
return array();
}
$auth_cookies = array();
$duplicate_cookies = array();
$wp_cookies = array(
'AUTH_COOKIE' => 'wordpress_',
'SECURE_AUTH_COOKIE' => 'wordpress_sec_',
'LOGGED_IN_COOKIE' => 'wordpress_logged_in_',
);
foreach ( $wp_cookies as $cookie_const => $cookie_prefix ) {
$cookie_key = strtolower( $cookie_const );
if ( defined( $cookie_const ) ) {
if ( in_array( constant( $cookie_const ), $cookies, true ) ) {
$auth_cookies[ $cookie_key ] = constant( $cookie_const );
}
continue;
}
$found_cookies = preg_grep( '`^' . preg_quote( $cookie_prefix, '`' ) . '([0-9a-f]+)$`', $cookies );
if ( count( $found_cookies ) === 1 ) {
$auth_cookies[ $cookie_key ] = reset( $found_cookies );
} elseif ( count( $found_cookies ) > 1 ) {
$duplicate_cookies = array_merge( $duplicate_cookies, $found_cookies );
$auth_cookies[ $cookie_key ] = $found_cookies;
}
}
$cookie_hash = defined( 'COOKIEHASH' ) ? COOKIEHASH : '';
$other_cookies = array(
'comment_cookie' => 'comment_author_',
'postpass_cookie' => 'wp-postpass_',
);
foreach ( $other_cookies as $cookie_key => $cookie_prefix ) {
if ( $cookie_hash ) {
if ( in_array( $cookie_prefix . $cookie_hash, $cookies, true ) ) {
$auth_cookies[ $cookie_key ] = $cookie_prefix . $cookie_hash;
}
continue;
}
$found_cookies = preg_grep( '`^' . preg_quote( $cookie_prefix, '`' ) . '([0-9a-f]+)$`', $cookies );
if ( count( $found_cookies ) === 1 ) {
$auth_cookies[ $cookie_key ] = reset( $found_cookies );
} elseif ( count( $found_cookies ) > 1 ) {
$duplicate_cookies = array_merge( $duplicate_cookies, $found_cookies );
$auth_cookies[ $cookie_key ] = $found_cookies;
}
}
if ( ! $duplicate_cookies ) {
$cached_cookies = $auth_cookies;
}
if ( empty( $auth_cookies ) ) {
wp_cache_debug( 'wpsc_get_auth_cookies: no auth cookies detected', 5 );
} elseif ( $duplicate_cookies ) {
wp_cache_debug( 'wpsc_get_auth_cookies: duplicate cookies detected( ' . implode( ', ', $duplicate_cookies ) . ' )', 5 );
} else {
wp_cache_debug( 'wpsc_get_auth_cookies: cookies detected: ' . implode( ', ', $auth_cookies ), 5 );
}
return $auth_cookies;
}
/**
* Returns a string containing a sanitized version of the Accept header.
* For now, this can only respond with `text/html` or `application/json` -
* used to differentiate between pages which may or may not be cacheable.
*/
function wpsc_get_accept_header() {
static $accept = 'N/A';
if ( $accept === 'N/A' ) {
$accept_headers = apply_filters( 'wpsc_accept_headers', array( 'application/json', 'application/activity+json', 'application/ld+json' ) );
$accept_headers = array_map( 'strtolower', $accept_headers );
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- $accept is checked and set below.
$accept = isset( $_SERVER['HTTP_ACCEPT'] ) ? strtolower( filter_var( $_SERVER['HTTP_ACCEPT'] ) ) : '';
foreach ( $accept_headers as $header ) {
if ( str_contains( $accept, $header ) ) {
$accept = 'application/json';
}
}
if ( $accept !== 'application/json' ) {
$accept = 'text/html';
}
wp_cache_debug( 'ACCEPT: ' . $accept );
}
return $accept;
}
function wp_cache_get_cookies_values() {
global $wpsc_cookies;
static $string = '';
if ( $string != '' ) {
wp_cache_debug( "wp_cache_get_cookies_values: cached: $string" );
return $string;
}
if ( defined( 'COOKIEHASH' ) ) {
$cookiehash = preg_quote( constant( 'COOKIEHASH' ) );
} else {
$cookiehash = '';
}
$regex = "/^wp-postpass_$cookiehash|^comment_author_$cookiehash";
if ( defined( 'LOGGED_IN_COOKIE' ) ) {
$regex .= '|^' . preg_quote( constant( 'LOGGED_IN_COOKIE' ) );
} else {
$regex .= "|^wordpress_logged_in_$cookiehash";
}
$regex .= '/';
while ( $key = key( $_COOKIE ) ) {
if ( preg_match( $regex, $key ) ) {
wp_cache_debug( 'wp_cache_get_cookies_values: Login/postpass cookie detected' );
$string .= $_COOKIE[ $key ] . ',';
}
next( $_COOKIE );
}
reset( $_COOKIE );
// If you use this hook, make sure you update your .htaccess rules with the same conditions
$string = do_cacheaction( 'wp_cache_get_cookies_values', $string );
if (
isset( $wpsc_cookies ) &&
is_array( $wpsc_cookies ) &&
! empty( $wpsc_cookies )
) {
foreach ( $wpsc_cookies as $name ) {
if ( isset( $_COOKIE[ $name ] ) ) {
wp_cache_debug( "wp_cache_get_cookies_values - found extra cookie: $name" );
$string .= $name . '=' . $_COOKIE[ $name ] . ',';
}
}
}
if ( $string != '' ) {
$string = md5( $string );
}
wp_cache_debug( "wp_cache_get_cookies_values: return: $string", 5 );
return $string;
}
function add_cacheaction( $action, $func ) {
global $wp_supercache_actions;
$wp_supercache_actions[ $action ][] = $func;
}
function do_cacheaction( $action, $value = '' ) {
global $wp_supercache_actions;
if ( ! isset( $wp_supercache_actions ) || ! is_array( $wp_supercache_actions ) ) {
return $value;
}
if ( array_key_exists( $action, $wp_supercache_actions ) && is_array( $wp_supercache_actions[ $action ] ) ) {
$actions = $wp_supercache_actions[ $action ];
foreach ( $actions as $func ) {
$value = call_user_func_array( $func, array( $value ) );
}
}
return $value;
}
function wp_cache_mobile_group( $user_agent ) {
global $wp_cache_mobile_groups;
foreach ( (array) $wp_cache_mobile_groups as $name => $group ) {
foreach ( (array) $group as $browser ) {
$browser = trim( strtolower( $browser ) );
if ( $browser != '' && strstr( $user_agent, $browser ) ) {
return $browser;
}
}
}
return 'mobile';
}
// From https://wordpress.org/plugins/wordpress-mobile-edition/ by Alex King
function wp_cache_check_mobile( $cache_key ) {
global $wp_cache_mobile_enabled, $wp_cache_mobile_browsers, $wp_cache_mobile_prefixes;
if ( ! isset( $wp_cache_mobile_enabled ) || false == $wp_cache_mobile_enabled ) {
return $cache_key;
}
// a check of wp_is_mobile() should be added here, but if the Jetpack WPSC plugin
// is enabled, jetpack_is_mobile() is used through the wp_cache_check_mobile action.
wp_cache_debug( "wp_cache_check_mobile: $cache_key" );
// allow plugins to short circuit mobile check. Cookie, extra UA checks?
switch ( do_cacheaction( 'wp_cache_check_mobile', $cache_key ) ) {
case 'normal':
wp_cache_debug( 'wp_cache_check_mobile: desktop user agent detected by wp_cache_check_mobile action' );
return $cache_key;
break;
case 'mobile':
wp_cache_debug( 'wp_cache_check_mobile: mobile user agent detected by wp_cache_check_mobile action' );
return $cache_key . '-mobile';
break;
}
if ( ! isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
return $cache_key;
}
if ( do_cacheaction( 'disable_mobile_check', false ) ) {
wp_cache_debug( 'wp_cache_check_mobile: disable_mobile_check disabled mobile check' );
return $cache_key;
}
$browsers = explode( ',', $wp_cache_mobile_browsers );
$user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
foreach ( $browsers as $browser ) {
if ( strstr( $user_agent, trim( strtolower( $browser ) ) ) ) {
wp_cache_debug( 'mobile browser detected: ' . $browser );
return $cache_key . '-' . wp_cache_mobile_group( $user_agent );
}
}
if ( isset( $_SERVER['HTTP_X_WAP_PROFILE'] ) ) {
return $cache_key . '-' . $_SERVER['HTTP_X_WAP_PROFILE'];
}
if ( isset( $_SERVER['HTTP_PROFILE'] ) ) {
return $cache_key . '-' . $_SERVER['HTTP_PROFILE'];
}
if ( isset( $wp_cache_mobile_prefixes ) ) {
$browsers = explode( ',', $wp_cache_mobile_prefixes );
foreach ( $browsers as $browser_prefix ) {
if ( substr( $user_agent, 0, 4 ) == $browser_prefix ) {
wp_cache_debug( 'mobile browser (prefix) detected: ' . $browser_prefix );
return $cache_key . '-' . $browser_prefix;
}
}
}
$accept = isset( $_SERVER['HTTP_ACCEPT'] ) ? strtolower( $_SERVER['HTTP_ACCEPT'] ) : '';
if ( str_contains( $accept, 'wap' ) ) {
return $cache_key . '-' . 'wap';
}
if ( isset( $_SERVER['ALL_HTTP'] ) && strpos( strtolower( $_SERVER['ALL_HTTP'] ), 'operamini' ) !== false ) {
return $cache_key . '-' . 'operamini';
}
return $cache_key;
}
/**
* Add a log message to the file, if debugging is turned on
*
* @param $message string The message that should be added to the log
* @param $level int
*/
function wp_cache_debug( $message, $level = 1 ) {
global $wp_cache_debug_log, $cache_path, $wp_cache_debug_ip, $wp_super_cache_debug;
static $last_message = '';
if ( $last_message == $message ) {
return false;
}
$last_message = $message;
// If either of the debug or log globals aren't set, then we can stop
if ( ! isset( $wp_super_cache_debug )
|| ! isset( $wp_cache_debug_log ) ) {
return false;
}
// If either the debug or log globals are false or empty, we can stop
if ( $wp_super_cache_debug == false
|| $wp_cache_debug_log === '' ) {
return false;
}
// If the debug_ip has been set, but it doesn't match the ip of the requester
// then we can stop.
if ( isset( $wp_cache_debug_ip )
&& $wp_cache_debug_ip !== ''
&& ( ! isset( $_SERVER['REMOTE_ADDR'] ) || $wp_cache_debug_ip !== $_SERVER['REMOTE_ADDR'] ) ) {
return false;
}
// if cache path is gone, then don't log anything
if ( empty( $cache_path ) || ! is_dir( $cache_path ) ) {
return;
}
// Log message: Date URI Message
$log_message = date( 'H:i:s' ) . ' ' . getmypid() . " {$_SERVER['REQUEST_URI']} {$message}" . PHP_EOL;
// path to the log file in the cache folder
$log_file = $cache_path . str_replace( '/', '', str_replace( '..', '', $wp_cache_debug_log ) );
if ( ! file_exists( $log_file ) && function_exists( 'wpsc_create_debug_log' ) ) {
global $wp_cache_debug_username;
if ( ! isset( $wp_cache_debug_username ) ) {
$wp_cache_debug_username = '';
}
wpsc_create_debug_log( $wp_cache_debug_log, $wp_cache_debug_username );
}
error_log( $log_message, 3, $log_file );
}
function wpsc_dump_get_request() {
static $string;
if ( isset( $string ) ) {
return $string;
}
if ( function_exists( 'wp_json_encode' ) ) {
$string = wp_json_encode( $_GET );
} else {
$string = json_encode( $_GET );
}
return $string;
}
function wpsc_is_backend() {
static $is_backend;
if ( isset( $is_backend ) ) {
return $is_backend;
}
$is_backend = is_admin();
if ( $is_backend ) {
return $is_backend;
}
$script = isset( $_SERVER['PHP_SELF'] ) ? basename( $_SERVER['PHP_SELF'] ) : '';
if ( $script !== 'index.php' ) {
if ( in_array( $script, array( 'wp-login.php', 'xmlrpc.php', 'wp-cron.php' ) ) ) {
$is_backend = true;
} elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) {
$is_backend = true;
} elseif ( PHP_SAPI == 'cli' || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
$is_backend = true;
}
}
return $is_backend;
}
function get_supercache_dir( $blog_id = 0 ) {
global $cache_path;
if ( $blog_id == 0 ) {
$home = get_option( 'home' );
} else {
$home = get_blog_option( $blog_id, 'home' );
}
return trailingslashit( apply_filters( 'wp_super_cache_supercachedir', $cache_path . 'supercache/' . trailingslashit( strtolower( preg_replace( '/:.*$/', '', str_replace( 'http://', '', str_replace( 'https://', '', $home ) ) ) ) ) ) );
}
function get_current_url_supercache_dir( $post_id = 0 ) {
global $cached_direct_pages, $cache_path, $wp_cache_request_uri, $WPSC_HTTP_HOST, $wp_cache_home_path;
static $saved_supercache_dir = array();
if ( isset( $saved_supercache_dir[ $post_id ] ) ) {
return $saved_supercache_dir[ $post_id ];
}
$DONOTREMEMBER = 0;
if ( $post_id != 0 ) {
$site_url = site_url();
$permalink = get_permalink( $post_id );
if ( ! str_contains( $permalink, $site_url ) ) {
/*
* Sometimes site_url doesn't return the siteurl. See https://wordpress.org/support/topic/wp-super-cache-not-refreshing-post-after-comments-made
*/
$DONOTREMEMBER = 1;
wp_cache_debug( "get_current_url_supercache_dir: WARNING! site_url ($site_url) not found in permalink ($permalink).", 1 );
if ( preg_match( '`^(https?:)?//([^/]+)(/.*)?$`i', $permalink, $matches ) ) {
if ( $WPSC_HTTP_HOST != $matches[2] ) {
wp_cache_debug( "get_current_url_supercache_dir: WARNING! SERVER_NAME ({$WPSC_HTTP_HOST}) not found in permalink ($permalink).", 1 );
}
wp_cache_debug( "get_current_url_supercache_dir: Removing SERVER_NAME ({$matches[2]}) from permalink ($permalink). Is the url right?", 1 );
$uri = isset( $matches[3] ) ? $matches[3] : '';
} elseif ( preg_match( '`^/([^/]+)(/.*)?$`i', $permalink, $matches ) ) {
wp_cache_debug( "get_current_url_supercache_dir: WARNING! Permalink ($permalink) looks as absolute path. Is the url right?", 1 );
$uri = $permalink;
} else {
wp_cache_debug( "get_current_url_supercache_dir: WARNING! Permalink ($permalink) could not be understood by parsing url. Using front page.", 1 );
$uri = '';
}
} else {
$uri = str_replace( $site_url, '', $permalink );
if ( ! str_starts_with( $uri, $wp_cache_home_path ) ) {
$uri = rtrim( $wp_cache_home_path, '/' ) . $uri;
}
}
} else {
$uri = strtolower( $wp_cache_request_uri );
$uri = preg_replace_callback(
'/%[a-f0-9]{2}/',
function ( $matches ) {
return strtoupper( $matches[0] );
},
$uri
);
}
$uri = wpsc_deep_replace( array( '..', '\\', 'index.php' ), preg_replace( '/[ <>\'\"\r\n\t\(\)]/', '', preg_replace( '/(\?.*)?(#.*)?$/', '', $uri ) ) );
$hostname = $WPSC_HTTP_HOST;
// Get hostname from wp options for wp-cron, wp-cli and similar requests.
if ( empty( $hostname ) && function_exists( 'get_option' ) ) {
$hostname = (string) parse_url( get_option( 'home' ), PHP_URL_HOST );
}
$dir = preg_replace( '/:.*$/', '', $hostname ) . $uri; // To avoid XSS attacks
if ( function_exists( 'apply_filters' ) ) {
$dir = apply_filters( 'supercache_dir', $dir );
} else {
$dir = do_cacheaction( 'supercache_dir', $dir );
}
$dir = $cache_path . 'supercache/' . $dir . '/';
if ( is_array( $cached_direct_pages ) && in_array( $_SERVER['REQUEST_URI'], $cached_direct_pages ) ) {
$dir = ABSPATH . $uri . '/';
}
$dir = str_replace( '..', '', str_replace( '//', '/', $dir ) );
wp_cache_debug( "supercache dir: $dir", 5 );
if ( $DONOTREMEMBER == 0 ) {
$saved_supercache_dir[ $post_id ] = $dir;
}
return $dir;
}
/*
* Delete (or rebuild) all the files in one directory.
* Checks if it is in the cache directory but doesn't allow files in the following directories to be deleted:
* wp-content/cache/
* wp-content/cache/blogs/
* wp-content/cache/supercache/
*
*/
function wpsc_rebuild_files( $dir ) {
return wpsc_delete_files( $dir, false );
}
// realpath() doesn't always remove the trailing slash
function wpsc_get_realpath( $directory ) {
if ( $directory == '/' ) {
wp_cache_debug( "wpsc_get_realpath: cannot get realpath of '/'" );
return false;
}
$original_dir = $directory;
$directory = realpath( $directory );
if ( ! $directory ) {
wp_cache_debug( "wpsc_get_realpath: directory does not exist - $original_dir" );
return false;
}
if ( substr( $directory, -1 ) == '/' || substr( $directory, -1 ) == '\\' ) {
$directory = substr( $directory, 0, -1 ); // remove trailing slash
}
return $directory;
}
// return true if directory is in the cache directory
function wpsc_is_in_cache_directory( $directory ) {
global $cache_path;
static $rp_cache_path = '';
if ( $directory == '' ) {
wp_cache_debug( 'wpsc_is_in_cache_directory: exiting as directory is blank' );
return false;
}
if ( $cache_path == '' ) {
wp_cache_debug( 'wpsc_is_in_cache_directory: exiting as cache_path is blank' );
return false;
}
if ( $rp_cache_path == '' ) {
$rp_cache_path = wpsc_get_realpath( $cache_path );
}
if ( ! $rp_cache_path ) {
wp_cache_debug( 'wpsc_is_in_cache_directory: exiting as cache_path directory does not exist' );
return false;
}
$directory = wpsc_get_realpath( $directory );
if ( ! $directory ) {
wp_cache_debug( 'wpsc_is_in_cache_directory: directory does not exist' );
return false;
}
if ( substr( $directory, 0, strlen( $rp_cache_path ) ) == $rp_cache_path ) {
return true;
} else {
return false;
}
}
function wpsc_delete_files( $dir, $delete = true ) {
global $cache_path;
static $protected = '';
if ( $dir == '' ) {
wp_cache_debug( 'wpsc_delete_files: directory is blank' );
return false;
}
wp_cache_debug( 'wpsc_delete_files: deleting ' . $dir );
// only do this once, this function will be called many times
if ( $protected == '' ) {
$protected = array( $cache_path, $cache_path . 'blogs/', $cache_path . 'supercache' );
foreach ( $protected as $id => $directory ) {
$protected[ $id ] = trailingslashit( wpsc_get_realpath( $directory ) );
}
}
$orig_dir = $dir;
$dir = wpsc_get_realpath( $dir );
if ( ! $dir ) {
wp_cache_debug( 'wpsc_delete_files: directory does not exist: ' . $orig_dir );
return false;
}
$dir = trailingslashit( $dir );
if ( ! wpsc_is_in_cache_directory( $dir ) ) {
wp_cache_debug( 'wpsc_delete_files: directory is not in cache directory: ' . $dir );
return false;
}
if ( in_array( $dir, $protected ) ) {
wp_cache_debug( 'wpsc_delete_files: directory is protected ' . $dir );
return false;
}
if ( is_dir( $dir ) && $dh = @opendir( $dir ) ) {
while ( ( $file = readdir( $dh ) ) !== false ) {
wp_cache_debug( 'wpsc_delete_files: reading files: ' . $file );
if ( $file != '.' && $file != '..' && $file != '.htaccess' && is_file( $dir . $file ) ) {
if ( $delete ) {
wp_cache_debug( 'wpsc_delete_files: deleting ' . $dir . $file );
@unlink( $dir . $file );
} else {
wp_cache_debug( 'wpsc_delete_files: rebuild or delete ' . $dir . $file );
@wp_cache_rebuild_or_delete( $dir . $file );
}
}
}
closedir( $dh );
if ( $delete ) {
wp_cache_debug( 'wpsc_delete_files: remove directory ' . $dir );
@rmdir( $dir );
}
} else {
wp_cache_debug( 'wpsc_delete_files: could not open directory ' . $dir );
}
return true;