index-wp-redis.php 5.36 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();

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

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

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

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

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

Benjamin Adams's avatar
merged  
Benjamin Adams committed
33 34 35 36 37
function getCleanUrl($secret) {
    $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 40 41 42 43 44 45 46 47 48

$debug          = true;
$cache          = true;
$websiteIp      = '127.0.0.1';
$reddis_server  = '127.0.0.1';
$secret_string  = 'changeme';
$current_url    = getCleanUrl($secret_string);
$redis_key      = md5($current_url);

handleCDNRemoteAddressing();
Hendrik Klemp's avatar
Hendrik Klemp committed
49

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

Ulrich Block's avatar
Ulrich Block committed
54
try {
Ulrich Block's avatar
Ulrich Block committed
55 56
    // check if PECL Extension is available
    if (class_exists('Redis')) {
Benjamin Adams's avatar
merged  
Benjamin Adams committed
57 58 59
        if ($debug) {
            echo "<!-- Redis PECL module found -->\n";
        }
Ulrich Block's avatar
Ulrich Block committed
60
        $redis = new Redis();
Benjamin Adams's avatar
merged  
Benjamin Adams committed
61

Ulrich Block's avatar
Ulrich Block committed
62
        // Sockets can be used as well. Documentation @ https://github.com/nicolasff/phpredis/#connection
Benjamin Adams's avatar
merged  
Benjamin Adams committed
63
        $redis->connect($reddis_server);
Ulrich Block's avatar
Ulrich Block committed
64
        
Benjamin Adams's avatar
merged  
Benjamin Adams committed
65 66 67 68 69
    } else { // Fallback to predis5.2.php

        if ($debug) {
            echo "<!-- using predis as a backup -->\n";
        }
70
        include_once("wp-content/plugins/wp-redis-cache/predis5.2.php"); //we need this to use Redis inside of PHP
Ulrich Block's avatar
Ulrich Block committed
71 72
        $redis = new Predis_Client();
    }
73
    
Ulrich Block's avatar
Ulrich Block committed
74
    //Either manual refresh cache by adding ?refresh=secret_string after the URL or somebody posting a comment
Benjamin Adams's avatar
merged  
Benjamin Adams committed
75 76 77 78
    if (refreshHasSecret($secret_string) || requestHasSecret($secret_string) || isRemotePageLoad($current_url, $websiteIp)) {
        if ($debug) {
            echo "<!-- manual refresh was required -->\n";
        }
Ulrich Block's avatar
Ulrich Block committed
79
        $redis->del($redis_key);
80
        require('./wp-blog-header.php');
Benjamin Adams's avatar
merged  
Benjamin Adams committed
81 82 83
        
        $unlimited = get_option('wp-redis-cache-debug',false);
        $seconds_cache_redis = get_option('wp-redis-cache-seconds',43200);
Ulrich Block's avatar
Ulrich Block committed
84 85
    // This page is cached, lets display it
    } else if ($redis->exists($redis_key)) {
Benjamin Adams's avatar
merged  
Benjamin Adams committed
86 87 88 89
        if ($debug) {
            echo "<!-- serving page from cache: key: $redis_key -->\n";
        }
        $cache  = true;
Ulrich Block's avatar
Ulrich Block committed
90
        $html_of_page = $redis->get($redis_key);
Benjamin Adams's avatar
merged  
Benjamin Adams committed
91 92
        echo $html_of_page;

Ulrich Block's avatar
Ulrich Block committed
93
     // If the cache does not exist lets display the user the normal page without cache, and then fetch a new cache page
Benjamin Adams's avatar
merged  
Benjamin Adams committed
94 95 96 97
    } else if ($_SERVER['REMOTE_ADDR'] != $websiteIp && strstr($current_url, 'preview=true') == false) {
        if ($debug) {
            echo "<!-- displaying page without cache -->\n";
        }
Ulrich Block's avatar
Ulrich Block committed
98 99
        
        $isPOST = ($_SERVER['REQUEST_METHOD'] === 'POST') ? 1 : 0;
100
        
Benjamin Adams's avatar
merged  
Benjamin Adams committed
101 102
        $loggedIn = preg_match("/wordpress_logged_in/", var_export($_COOKIE, true));
        if (!$isPOST && !$loggedIn) {
Ulrich Block's avatar
Ulrich Block committed
103 104 105 106
            ob_start();
            require('./wp-blog-header.php');
            $html_of_page = ob_get_contents();
            ob_end_clean();
Benjamin Adams's avatar
merged  
Benjamin Adams committed
107 108 109 110 111 112 113 114
            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
Hendrik Klemp's avatar
Hendrik Klemp committed
115
            if ((!is_404()) and (!is_search()))  {
Hendrik Klemp's avatar
Hendrik Klemp committed
116
                if ($unlimited) {
Benjamin Adams's avatar
merged  
Benjamin Adams committed
117 118 119
                    $redis->set($redis_key, $html_of_page);
                } else {
                    $redis->setex($redis_key, $seconds_cache_redis, $html_of_page);
Hendrik Klemp's avatar
Hendrik Klemp committed
120 121
                }

Ulrich Block's avatar
Ulrich Block committed
122
            }
Benjamin Adams's avatar
merged  
Benjamin Adams committed
123
        } else { //either the user is logged in, or is posting a comment, show them uncached
Ulrich Block's avatar
Ulrich Block committed
124
            require('./wp-blog-header.php');
125
        }
Ulrich Block's avatar
Ulrich Block committed
126
        
Benjamin Adams's avatar
merged  
Benjamin Adams committed
127
    } else if ($_SERVER['REMOTE_ADDR'] != $websiteIp && strstr($current_url, 'preview=true') == true) {
128 129
        require('./wp-blog-header.php');
    }
Benjamin Adams's avatar
merged  
Benjamin Adams committed
130
     // else {   // This is what your server should get if no cache exists  //deprecated, as the ob_start() is cleaner
Ulrich Block's avatar
Ulrich Block committed
131 132 133
        // require('./wp-blog-header.php');
    // }
} catch (Exception $e) {
Benjamin Adams's avatar
merged  
Benjamin Adams 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 139 140
$end  = microtime();
$time = (@getMicroTime($end) - @getMicroTime($start));
if ($debug) {
Benjamin Adams's avatar
merged  
Benjamin Adams 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
}