-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitc.php
More file actions
380 lines (294 loc) · 11.9 KB
/
Copy pathitc.php
File metadata and controls
380 lines (294 loc) · 11.9 KB
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
<?php
/**
* Plugin Name: ITC - is this cached?
* Description: Detects caching layers for any WordPress content.
* Version: 0.1.0
* Author: oilerart
* Author URI: https://oiler.art.br
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
define( 'ITC_VERSION', '0.1.0');
define( 'ITC_DIR', plugin_dir_path( __FILE__) );
define( 'ITC_URL', plugin_dir_url( __FILE__ ) );
register_activation_hook( __FILE__, 'itc_activate' );
register_deactivation_hook( __FILE__, 'itc_deactivate' );
function itc_activate() {
}
function itc_deactivate() {
}
interface ITC_Detector {
public function detect( $response );
}
class ITC_HTTP_Detector implements ITC_Detector {
public function detect( $response ) {
$headers = wp_remote_retrieve_headers( $response );
$cache_control = $headers['cache-control'] ?? '';
$expires = $headers['expires'] ?? '';
$etag = $headers['etag'] ?? '';
$last_modified = $headers['last-modified'] ?? '';
$age = $headers['age'] ?? '';
$verdict = '';
if ( $age !== '' && (int) $age > 0 ) {
$verdict = 'Currently served from cache';
} elseif ( str_contains( $cache_control, 'no-store' ) ) {
$verdict = 'Not cacheable (no-store)';
} elseif ( str_contains( $cache_control, 'public' ) || str_contains( $cache_control, 'max-age' ) || $expires !== '' ) {
$verdict = 'Cacheable, but not currently cached, or just unknown if cached at all';
} else {
$verdict = 'No cache headers found';
}
return array(
'cache_control' => $cache_control,
'expires' => $expires,
'etag' => $etag,
'last_modified' => $last_modified,
'age' => $age,
'verdict' => $verdict,
);
}
}
class ITC_CDN_Detector implements ITC_Detector {
public function detect( $response ) {
$headers = wp_remote_retrieve_headers( $response );
$cf_cache_status = $headers['cf-cache-status'] ?? '';
$cf_ray = $headers['cf-ray'] ?? '';
$x_cache = $headers['x-cache'] ?? '';
$via = $headers['via'] ?? '';
if ( $cf_ray !== '' ) {
$cdn = 'Cloudflare';
if ( $cf_cache_status !== '' ) {
$cdn .= ' (' . $cf_cache_status . ')';
}
} elseif ( str_contains( strtolower( $x_cache ), 'cloudfront' ) ) {
$cdn = 'AWS CloudFront (' . $x_cache . ')';
} elseif ( $x_cache !== '' ) {
$cdn = 'CDN/proxy detected (x-cache: ' . $x_cache . ')';
} elseif ( $via !== '' ) {
$cdn = 'Proxy detected (via: ' . $via . ')';
} else {
$cdn = 'No CDN detected';
}
return array( 'cdn' => $cdn );
}
}
class ITC_Plugin_Detector implements ITC_Detector {
public function detect( $response ) {
$known = array(
'LiteSpeed Cache' => 'LSCWP_V',
'W3 Total Cache' => 'W3TC',
'WP Super Cache' => 'WPCACHEHOME',
'Hummingbird' => 'WPHB_VERSION',
'WP Fastest Cache' => 'WpFastestCache',
);
$detected = array();
foreach ( $known as $plugin => $signal ) {
if ( defined( $signal ) || class_exists( $signal ) ) {
$detected[] = $plugin;
}
}
if ( empty( $detected ) ) {
$cache_plugin = 'No cache plugin detected';
} else {
$cache_plugin = implode( ', ' , $detected );
}
$body = wp_remote_retrieve_body( $response );
$footprints = array(
'WP Super Cache' => 'WP-Super-Cache',
'W3 Total Cache' => 'Performance optimized by W3 Total Cache',
'LiteSpeed Cache' => 'LiteSpeed Cache',
'WP Fastest Cache' => 'WP Fastest Cache',
);
$matches = array();
foreach ( $footprints as $plugin => $string ) {
if ( str_contains( $body, $string ) ) {
$matches[] = $plugin;
}
}
if ( empty( $matches ) ) {
$cache_plugin_footprint = 'No cache plugin detected';
} else {
$cache_plugin_footprint = implode( ', ', $matches );
}
return array(
'cache_plugin' => $cache_plugin,
'cache_plugin_footprint' => $cache_plugin_footprint,
);
}
}
class ITC_Server_Detector implements ITC_Detector {
public function detect( $response ) {
$headers = wp_remote_retrieve_headers( $response );
$server = $headers['server'] ?? '';
$x_varnish = $headers['x-varnish'] ?? '';
$via = $headers['via'] ?? '';
$varnish_verdict = '';
$kinsta = $headers['x-kinsta-cache'] ?? '';
$wpengine = $headers['x-powered-by'] ?? '';
$siteground = $headers['x-proxy-cache'] ?? '';
$host = '';
if ( str_contains( $x_varnish, ' ' ) ) {
$varnish_verdict = 'Varnish HIT';
} elseif ( $x_varnish !== '' ) {
$varnish_verdict = 'Varnish detected (miss)';
} elseif ( str_contains( strtolower( $via ), 'varnish' ) ) {
$varnish_verdict = 'Varnish detected (via)';
} else {
$varnish_verdict = 'No varnish detected';
}
if ( $kinsta !== '' ) {
$host = 'Kinsta';
} elseif ( str_contains( strtolower( $wpengine ), 'wp engine' ) ) {
$host = 'WP Engine';
} elseif ( $siteground !== '') {
$host = 'SiteGround (or Nginx proxy)';
} else {
$host = 'Host not defined';
}
return array(
'server' => $server,
'varnish_verdict' => $varnish_verdict,
'host' => $host,
);
}
}
class ITC_Object_Cache_Detector implements ITC_Detector {
public function detect( $response ) {
if ( wp_using_ext_object_cache() ) {
$object_cache = 'Persistent object cache active';
} else {
$object_cache = 'No persistent object cache';
}
return array(
'object_cache' => $object_cache,
);
}
}
class ITC_OPcache_Detector implements ITC_Detector {
public function detect ( $response ) {
if ( function_exists( 'opcache_get_status' ) && opcache_get_status( false ) ) {
$opcache = 'OPcache enabled';
} else {
$opcache = 'OPcache not enabled';
}
return array(
'opcache' => $opcache,
);
}
}
class ITC {
public function __construct() {
add_action( 'admin_menu' , array( $this, 'register_menu') );
}
public static function init() {
new self();
}
public function register_menu() {
add_options_page(
'ITC - is this cached?',
'ITC',
'manage_options',
'itc',
array ( $this, 'render_page' ),
);
}
public function scan( $url, $cookies = array() ) {
$result = array(
'cache_control' => '',
'expires' => '',
'etag' => '',
'last_modified' => '',
'age' => '',
'verdict' => '',
'cdn' => '',
'cache_plugin' => '',
'cache_plugin_footprint' => '',
'server' => '',
'varnish_verdict' => '',
'host' => '',
'object_cache' => '',
'opcache' => '',
'error' => '',
);
$response = wp_remote_get( $url, array( 'cookies' => $cookies ) );
if ( is_wp_error( $response ) ) {
$result['error'] = $response->get_error_message();
return $result;
}
$http_detector = new ITC_HTTP_Detector;
$http_result = $http_detector->detect( $response );
$cdn_detector = new ITC_CDN_Detector;
$cdn_result = $cdn_detector->detect( $response );
$cache_plugin_detector = new ITC_Plugin_Detector;
$cache_plugin_result = $cache_plugin_detector->detect( $response );
$server_detector = new ITC_Server_Detector;
$server_result = $server_detector->detect( $response );
$object_cache_detector = new ITC_Object_Cache_Detector;
$object_cache_result = $object_cache_detector->detect( $response );
$opcache_detector = new ITC_OPcache_Detector;
$opcache_result = $opcache_detector->detect( $response );
$result = array_merge( $result, $http_result, $cdn_result, $cache_plugin_result, $server_result, $object_cache_result, $opcache_result );
return $result;
}
public function render_page() {
$default = array(
'cache_control' => '',
'expires' => '',
'etag' => '',
'last_modified' => '',
'age' => '',
'verdict' => '',
'cdn' => '',
'cache_plugin' => '',
'cache_plugin_footprint' => '',
'server' => '',
'varnish_verdict' => '',
'host' => '',
'object_cache' => '',
'opcache' => '',
'error' => '',
);
$result_anon = $default;
$result_auth = $default;
$scanned_url = '';
if ( isset( $_POST['itc_nonce'] ) && wp_verify_nonce( $_POST['itc_nonce'], 'itc_scan' ) ) {
$scanned_url = esc_url_raw( $_POST['itc_url'] ?? '' );
if ( $scanned_url ) {
$result_anon = $this->scan( $scanned_url );
$result_auth = $this->scan( $scanned_url, $_COOKIE );
}
}
?>
<div class="wrap">
<h1>Is this cached?</h1>
<form method="post">
<?php wp_nonce_field( 'itc_scan', 'itc_nonce' ); ?>
<input type="url" name="itc_url" placeholder="https://example.com" class="regular-text">
<input type="submit" class="button button-primary" value="Scan">
</form>
<?php if ( $result_anon['error'] ) : ?>
<p>Error: <?php echo esc_html( $result_anon['error'] ); ?></p>
<?php elseif ( $scanned_url ) : ?>
<p>Scanning: <?php echo esc_url( $scanned_url ); ?></p>
<p>Cache control: <?php echo esc_html( $result_anon['cache_control'] ); ?></p>
<p>Expires: <?php echo esc_html( $result_anon['expires'] ); ?></p>
<p>Etag: <?php echo esc_html( $result_anon['etag'] ); ?></p>
<p>Last modified: <?php echo esc_html( $result_anon['last_modified'] ); ?></p>
<p>Age: <?php echo esc_html( $result_anon['age'] ); ?></p>
<p>Verdict (logged out): <?php echo esc_html( $result_anon['verdict'] ); ?></p>
<p>Verdict (logged in): <?php if ( $result_auth['error'] ) { echo 'scan unavailable'; } else { echo esc_html( $result_auth['verdict'] ); } ?></p>
<p>CDN: <?php echo esc_html( $result_anon['cdn'] ); ?></p>
<p>Cache plugin: <?php echo esc_html( $result_anon['cache_plugin'] ); ?></p>
<p>Cache plugin footprint: <?php echo esc_html( $result_anon['cache_plugin_footprint'] ); ?></p>
<p>Server: <?php echo esc_html( $result_anon['server'] ); ?> </p>
<p>Varnish verdict: <?php echo esc_html( $result_anon['varnish_verdict'] ); ?> </p>
<p>Host: <?php echo esc_html( $result_anon['host'] ); ?></p>
<p>Object cache: <?php echo esc_html( $result_anon['object_cache'] ); ?></p>
<p>OPcache: <?php echo esc_html( $result_anon['opcache'] ); ?></p>
<?php endif; ?>
</div>
<?php
}
}
add_action( 'plugins_loaded', array( 'ITC', 'init' ) );