index-wp-redis.php 4.78 KB
Newer Older
Benjamin Adams's avatar
Benjamin Adams committed
1 2
<?php

Ulrich Block's avatar
Ulrich Block committed
3 4 5
// Start the timer so we can track the page load time
$start = microtime();

6
function get_micro_time($time) {
Erick Hitter's avatar
Erick Hitter committed
7 8
	list($usec, $sec) = explode(" ", $time);
	return ((float) $usec + (float) $sec);
Ulrich Block's avatar
Ulrich Block committed
9 10
}

11
function refresh_has_secret($secret) {
Erick Hitter's avatar
Erick Hitter committed
12
	return isset($_GET['refresh']) && $_GET['refresh'] == $secret;
Benjamin Adams's avatar
merged  
Benjamin Adams committed
13
}
Ulrich Block's avatar
Ulrich Block committed
14

15
function request_has_secret($secret) {
Erick Hitter's avatar
Erick Hitter committed
16
	return strpos($_SERVER['REQUEST_URI'],"refresh=${secret}")!==false;
Benjamin Adams's avatar
merged  
Benjamin Adams committed
17
}
Hendrik Klemp's avatar
Hendrik Klemp committed
18

19
function is_remote_page_load($currentUrl, $websiteIp) {
Erick Hitter's avatar
Erick Hitter committed
20 21 22 23
	return (isset($_SERVER['HTTP_REFERER'])
			&& $_SERVER['HTTP_REFERER']== $currentUrl
			&& $_SERVER['REQUEST_URI'] != '/'
			&& $_SERVER['REMOTE_ADDR'] != $websiteIp);
Benjamin Adams's avatar
merged  
Benjamin Adams committed
24
}
Hendrik Klemp's avatar
Hendrik Klemp committed
25

26
function handle_cdn_remote_addressing() {
Erick Hitter's avatar
Erick Hitter committed
27 28 29 30
	// so we don't confuse the cloudflare server
	if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
		$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
	}
Benjamin Adams's avatar
merged  
Benjamin Adams committed
31
}
32

33
function get_clean_url($secret) {
Erick Hitter's avatar
Erick Hitter committed
34 35 36 37
	$replaceKeys = array("?refresh=${secret}","&refresh=${secret}");
	$url = "http://${_SERVER['HTTP_HOST']}${_SERVER['REQUEST_URI']}";
	$current_url = str_replace($replaceKeys, '', $url);
	return $current_url;
Benjamin Adams's avatar
Benjamin Adams committed
38
}
Benjamin Adams's avatar
merged  
Benjamin Adams committed
39

Erick Hitter's avatar
Erick Hitter committed
40 41 42
$debug		  = true;
$cache		  = true;
$websiteIp	  = '127.0.0.1';
Benjamin Adams's avatar
merged  
Benjamin Adams committed
43 44
$reddis_server  = '127.0.0.1';
$secret_string  = 'changeme';
45
$current_url	= get_clean_url($secret_string);
Erick Hitter's avatar
Erick Hitter committed
46
$redis_key	  = md5($current_url);
Benjamin Adams's avatar
merged  
Benjamin Adams committed
47

48
handle_cdn_remote_addressing();
Hendrik Klemp's avatar
Hendrik Klemp committed
49

Vasili Pascal's avatar
Vasili Pascal committed
50
if(!defined('WP_USE_THEMES')) {
Erick Hitter's avatar
Erick Hitter committed
51
	define('WP_USE_THEMES', true);
Vasili Pascal's avatar
Vasili Pascal committed
52
}
Benjamin Adams's avatar
Benjamin Adams committed
53

Ulrich Block's avatar
Ulrich Block committed
54
try {
Erick Hitter's avatar
Erick Hitter committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
	// check if PECL Extension is available
	if (class_exists('Redis')) {
		if ($debug) {
			echo "<!-- Redis PECL module found -->\n";
		}
		$redis = new Redis();

		// Sockets can be used as well. Documentation @ https://github.com/nicolasff/phpredis/#connection
		$redis->connect($reddis_server);

	} else { // Fallback to predis5.2.php

		if ($debug) {
			echo "<!-- using predis as a backup -->\n";
		}
		include_once("wp-content/plugins/wp-redis-cache/predis5.2.php"); //we need this to use Redis inside of PHP
		$redis = new Predis_Client();
	}

	//Either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment
75
	if (refresh_has_secret($secret_string) || request_has_secret($secret_string) || is_remote_page_load($current_url, $websiteIp)) {
Erick Hitter's avatar
Erick Hitter committed
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
		if ($debug) {
			echo "<!-- manual refresh was required -->\n";
		}
		$redis->del($redis_key);
		require('./wp-blog-header.php');

		$unlimited = get_option('wp-redis-cache-debug',false);
		$seconds_cache_redis = get_option('wp-redis-cache-seconds',43200);
	// This page is cached, lets display it
	} else if ($redis->exists($redis_key)) {
		if ($debug) {
			echo "<!-- serving page from cache: key: $redis_key -->\n";
		}
		$cache  = true;
		$html_of_page = $redis->get($redis_key);
		echo $html_of_page;

	 // If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page
	} else if ($_SERVER['REMOTE_ADDR'] != $websiteIp && strstr($current_url, 'preview=true') == false) {
		if ($debug) {
			echo "<!-- displaying page without cache -->\n";
		}

		$isPOST = ($_SERVER['REQUEST_METHOD'] === 'POST') ? 1 : 0;

		$loggedIn = preg_match("/wordpress_logged_in/", var_export($_COOKIE, true));
		if (!$isPOST && !$loggedIn) {
			ob_start();
			require('./wp-blog-header.php');
			$html_of_page = ob_get_contents();
			ob_end_clean();
			echo $html_of_page;

			if (!is_numeric($seconds_cache_redis)) {
				$seconds_cache_redis = 43200;
			}

			// When a page displays after an "HTTP 404: Not Found" error occurs, do not cache
			// When the search was used, do not cache
			if ((!is_404()) and (!is_search()))  {
				if ($unlimited) {
					$redis->set($redis_key, $html_of_page);
				} else {
					$redis->setex($redis_key, $seconds_cache_redis, $html_of_page);
				}

			}
		} else { //either the user is logged in, or is posting a comment, show them uncached
			require('./wp-blog-header.php');
		}

	} else if ($_SERVER['REMOTE_ADDR'] != $websiteIp && strstr($current_url, 'preview=true') == true) {
		require('./wp-blog-header.php');
	}
	 // else {   // This is what your server should get if no cache exists  //deprecated, as the ob_start() is cleaner
		// require('./wp-blog-header.php');
	// }
Ulrich Block's avatar
Ulrich Block committed
133
} catch (Exception $e) {
Erick Hitter's avatar
Erick Hitter committed
134 135
	//require('./wp-blog-header.php');
	echo "something went wrong";
Benjamin Adams's avatar
Benjamin Adams committed
136 137
}

Hendrik Klemp's avatar
Hendrik Klemp committed
138
$end  = microtime();
139
$time = (@get_micro_time($end) - @get_micro_time($start));
Hendrik Klemp's avatar
Hendrik Klemp committed
140
if ($debug) {
Erick Hitter's avatar
Erick Hitter committed
141 142 143 144 145 146 147 148 149 150 151
	echo "<!-- Cache system by Benjamin Adams. Page generated in " . round($time, 5) . " seconds. -->\n";
	echo "<!-- Site was cached  = " . $cache . " -->\n";
	if (isset($seconds_cache_redis)) {
		echo "<!-- wp-redis-cache-seconds  = " . $seconds_cache_redis . " -->\n";
	}
	echo "<!-- wp-redis-cache-secret  = " . $secret_string . "-->\n";
	echo "<!-- wp-redis-cache-ip  = " . $websiteIp . "-->\n";
	if (isset($unlimited)) {
		echo "<!-- wp-redis-cache-unlimited = " . $unlimited . "-->\n";
	}
	echo "<!-- wp-redis-cache-debug  = " . $debug . "-->\n";
Benjamin Adams's avatar
Benjamin Adams committed
152
}