all repos — wp-ffpc @ 5ca01200859ffffa21c680143697f57796752ac0

advanced-cache.php (view raw)

 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
<?php
/**
 * part of WordPress plugin WP-FFPC
 */

/* check for WP cache enabled*/
if ( !WP_CACHE )
	return false;

/* check for config */
if (!isset($wp_ffpc_config))
	return false;

/* request uri */
$wp_ffpc_uri = $_SERVER['REQUEST_URI'];
/* query string */
$wp_ffpc_qs = stripos($wp_ffpc_uri, '?');

/* no cache for uri with query strings, things usually go bad that way */
if ($wp_ffpc_qs !== false)
	return false;

/* no cache for post request (comments, plugins and so on) */
if ($_SERVER["REQUEST_METHOD"] == 'POST')
	return false;

/**
 * Try to avoid enabling the cache if sessions are managed
 * with request parameters and a session is active
 */
if (defined('SID') && SID != '')
	return false;

/* no cache for pages starting with /wp- like WP admin */
if (stripos($wp_ffpc_uri, '/wp-') !== false)
	return false;

/* no cache for robots.txt */
if ( stripos($wp_ffpc_uri, 'robots.txt') )
	return false;

/* multisite files can be too large for memcached */
if (function_exists('is_multisite') && is_multisite() && stripos($wp_ffpc_uri, '/files/') )
	return false;


/* no cache for logged in users */
if (!$wp_ffpc_config['cache_loggedin']) {
	foreach ($_COOKIE as $n=>$v) {
		// test cookie makes to cache not work!!!
		if ($n == 'wordpress_test_cookie') continue;
		// wp 2.5 and wp 2.3 have different cookie prefix, skip cache if a post password cookie is present, also
		if ( (substr($n, 0, 14) == 'wordpressuser_' || substr($n, 0, 10) == 'wordpress_' || substr($n, 0, 12) == 'wp-postpass_') && !$wp_ffpc_config['cache_loggedin'] ) {
			return false;
		}
	}
}

global $wp_ffpc_backend_status;
$wp_ffpc_backend_status = wp_ffpc_init( );

/* check alive status of backend */
if ( !$wp_ffpc_backend_status )
	return false;

/* use the full accessed URL string as key, same will be generated by nginx as well
   we need a data and a meta key: data is string only with content, meta is not used in nginx */
global $wp_ffpc_data_key_protocol;
$wp_ffpc_data_key_protocol = empty ( $_SERVER['HTTPS'] ) ? 'http://' : 'https://';

global $wp_ffpc_data_key;
$wp_ffpc_data_key = $wp_ffpc_config['prefix_data'] . $wp_ffpc_data_key_protocol . $_SERVER['HTTP_HOST'] . $wp_ffpc_uri;
global $wp_ffpc_meta_key;
$wp_ffpc_meta_key = $wp_ffpc_config['prefix_meta'] . $wp_ffpc_data_key_protocol . $_SERVER['HTTP_HOST'] . $wp_ffpc_uri;

/* search for valid meta entry */
global $wp_ffpc_meta;
$wp_ffpc_meta = wp_ffpc_get ( $wp_ffpc_meta_key );

/* meta is corrupted or empty */
if ( !$wp_ffpc_meta ) {
	wp_ffpc_start();
	return;
}

/* search for valid data entry */
global $wp_ffpc_data;
$uncompress = ( isset($wp_ffpc_meta['compressed']) ) ? $wp_ffpc_meta['compressed'] : false;
$wp_ffpc_data = wp_ffpc_get ( $wp_ffpc_data_key , $uncompress );

/* data is corrupted or empty */
if ( !$wp_ffpc_data ) {
	wp_ffpc_start();
	return;
}

/* 404 status cache */
if ($wp_ffpc_meta['status'] == 404) {
	header("HTTP/1.1 404 Not Found");
	flush();
	die();
}

/* server redirect cache */
if ($wp_ffpc_meta['redirect_location']) {
	header('Location: ' . $wp_ffpc_meta['redirect_location']);
	flush();
	die();
}

/* page is already cached on client side (chrome likes to do this, anyway, it's quite efficient) */
if (array_key_exists("HTTP_IF_MODIFIED_SINCE", $_SERVER) && !empty($wp_ffpc_meta['lastmodified']) ) {
	$if_modified_since = strtotime(preg_replace('/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"]));
	/* check is cache is still valid */
	if ( $if_modified_since >= $wp_ffpc_meta['lastmodified'] ) {
		header("HTTP/1.0 304 Not Modified");
		flush();
		die();
	}
}

/* data found & correct, serve it */
header('Content-Type: ' . $wp_ffpc_meta['mime']);
/* don't allow browser caching of page */
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0');
header('Pragma: no-cache');
/* expire at this very moment */
header('Expires: ' . gmdate("D, d M Y H:i:s", time() ) . " GMT");

/* if shortlinks were set */
if (!empty ( $wp_ffpc_meta['shortlink'] ) )
	header('Link:<'. $wp_ffpc_meta['shortlink'] .'>; rel=shortlink');

/* if last modifications were set (for posts & pages) */
if ( !empty($wp_ffpc_meta['lastmodified']) )
	header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $wp_ffpc_meta['lastmodified'] ). " GMT");

/* only set when not multisite, fallback to HTTP HOST */
$wp_ffpc_pingback_url = (empty( $wp_ffpc_config['url'] )) ? $_SERVER['HTTP_HOST'] : $wp_ffpc_config['url'];

/* pingback additional header */
if ($wp_ffpc_config['pingback_status'])
	header('X-Pingback: ' . $wp_ffpc_pingback_url . '/xmlrpc.php' );

/* for debugging */
if ($wp_ffpc_config['debug'])
	header('X-Cache-Engine: WP-FFPC with ' . $wp_ffpc_config['cache_type']);

/* HTML data */
echo $wp_ffpc_data;
flush();
die();

/**
 *  FUNCTIONS
 */

/**
 * starts caching
 *
 */
function wp_ffpc_start( ) {
	ob_start('wp_ffpc_callback');
}

/**
 * write cache function, called when page generation ended
 */
function wp_ffpc_callback($buffer) {
	global $wp_ffpc_config;
	global $wp_ffpc_data;
	global $wp_ffpc_meta;
	global $wp_ffpc_redirect;
	global $wp_ffpc_meta_key;
	global $wp_ffpc_data_key;

	/* no is_home = error */
	if (!function_exists('is_home'))
		return $buffer;

	/* no <body> close tag = not HTML, don't cache */
	if (stripos($buffer, '</body>') === false)
		return $buffer;

	/* reset meta to solve conflicts */
	$wp_ffpc_meta = array();

	/* WP is sending a redirect */
	if ($wp_ffpc_redirect) {
			$wp_ffpc_meta['redirect_location'] = $wp_ffpc_redirect;
			wp_ffpc_write();
			return $buffer;
	}

	/* trim unneeded whitespace from beginning / ending of buffer */
	$buffer = trim($buffer);

	/* Can be a trackback or other things without a body.
	   We do not cache them, WP needs to get those calls. */
	if (strlen($buffer) == 0)
		return '';

	if ( is_home() )
		$wp_ffpc_meta['type'] = 'home';
	elseif (is_feed() )
		$wp_ffpc_meta['type'] = 'feed';
	elseif ( is_archive() )
		$wp_ffpc_meta['type'] = 'archive';
	elseif ( is_single() )
		$wp_ffpc_meta['type'] = 'single';
	else if ( is_page() )
		$wp_ffpc_meta['type'] = 'page';
	else
		$wp_ffpc_meta['type'] = 'unknown';

	/* check if caching is disabled for page type */
	$nocache_key = 'nocache_'. $wp_ffpc_meta['type'];

	/* don't cache if prevented by rule, also, log it */
	if ( $wp_ffpc_config[$nocache_key] == 1 ) {
		wp_ffpc_log ( "not caching, prevented by settings for no-cache: " . $nocache_key );
		return $buffer;
	}

	if ( is_404() )
		$wp_ffpc_meta['status'] = 404;

	/* feed is xml, all others forced to be HTML */
	if ( is_feed() )
		$wp_ffpc_meta['mime'] = 'text/xml;charset=';
	else
		$wp_ffpc_meta['mime'] = 'text/html;charset=';

	/* set mimetype */
	$wp_ffpc_meta['mime'] = $wp_ffpc_meta['mime'] . $wp_ffpc_config['charset'];

	/* get shortlink, if possible */
	if (function_exists('wp_get_shortlink'))
	{
		$shortlink = wp_get_shortlink( );
		if (!empty ( $shortlink ) )
			$wp_ffpc_meta['shortlink'] = $shortlink;
	}

	/* try if post is available
		if made with archieve, last listed post can make this go bad
	*/
	global $post;
	if ( !empty($post) && ( $wp_ffpc_meta['type'] == 'single' || $wp_ffpc_meta['type'] == 'page' ) && !empty ( $post->post_modified_gmt ) )
	{
		/* get last modification data */
		$wp_ffpc_meta['lastmodified'] = strtotime ( $post->post_modified_gmt );
	}

	/* APC compression */
	$compress = ( ($wp_ffpc_config['cache_type'] == 'apc') && $wp_ffpc_config['apc_compress'] ) ? true : false;
	$wp_ffpc_meta['compressed'] = $compress;

	/* sync all http and https requests if enabled */
	if ( !empty($wp_ffpc_config['sync_protocols']) )
	{
		if ( !empty( $_SERVER['HTTPS'] ) )
		{
			$sync_from = 'http://' . $_SERVER['SERVER_NAME'];
			$sync_to = 'https://' . $_SERVER['SERVER_NAME'];
		}
		else
		{
			$sync_from = 'https://' . $_SERVER['SERVER_NAME'];
			$sync_to = 'http://' . $_SERVER['SERVER_NAME'];
		}

		$buffer = str_replace ( $sync_from, $sync_to, $buffer );
	}

	/* set meta */
	wp_ffpc_set ( $wp_ffpc_meta_key, $wp_ffpc_meta );
	wp_ffpc_set ( $wp_ffpc_data_key, $buffer, $compress );

	/* vital for nginx, make no problem at other places */
	header("HTTP/1.1 200 OK");

	/* echoes HTML out */
	return $buffer;
}

?>