Report abuse

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
<?php
/** Custom-made funky-caching for HB!: save it as wp-content/advanced-cache.php */

/** Where all the static pages will be stored, each site in a sub-folder */
define('HBCACHE_STORAGE', __DIR__ . '/hb-cache');

/** The magic happens here */
class HB_Cache {

	/**
	* Start output buffering
	*/
	Public Static Function process() {
		ob_start(array( __CLASS__, 'ob_callback'));
		}	

	/**
	* Output buffering callback
	*
	* @param string $buffer
	* @return string
	*/
	Public Static Function ob_callback(&$buffer) {

		if ($buffer != '' ) {

			$file = HBCACHE_STORAGE  . '/'
				. $_SERVER['HTTP_HOST'] 
				. $_SERVER['REQUEST_URI']
				. 'hb.html';

			mkdir(dirname($file), 0755, true);
			file_put_contents($file, $buffer 
				. '<!-- HB! Cache ' . date('r') . ' -->');
			}
		
		return $buffer;
		}
	
	// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
	    
	/**
	* Disk cache cleanup
	*
	* @param string $path
	* @param boolean $remove
	*/
	Public Static Function cleanup($path = HBCACHE_STORAGE, $remove = false) {

		if ($dir = @opendir($path)) {
			while (($entry = @readdir($dir)) !== false) {
				if ($entry != '.' && $entry != '..') {
					$full_path = $path . '/' . $entry;
		
					if (@is_dir($full_path)) {
						self::cleanup($full_path, true);
						} else
					if (!self::is_valid($full_path)) {
						echo $full_path, "\r\n";
						@unlink($full_path);
						}
					}
				}
		
			@closedir($dir);
		
			if ($remove) {
				echo $path, "\r\n";
				@rmdir($path);
				}
			}
		}

	/**
	* Check if a file is due to be garbage-collected
	*
	* @param string $file
	* @return boolean
	*/
	Public Static Function is_valid($file) {

		if (file_exists($file)) {
			$ftime = @filemtime($file);
		
			/* cache the pages for 24hrs */
			if ($ftime && $ftime > (time() - 86400)) { 
				return true;
				}
			}
		
		return false;
		}

	////-end-of-class-- 
	}

/////////////////////////////////////////////////////////////////////////////

/**
* Skip caching for some pages
*/
switch (true) {
	case defined('DOING_AJAX'):
	case defined('DOING_CRON'):
	case defined('APP_REQUEST'):
	case defined('XMLRPC_REQUEST'):
	case defined('WP_ADMIN'):
	case (defined('SHORTINIT') && SHORTINIT):
	    return;
	}

/** 
* If included from WordPress will process the buffered output, otherwise 
* if called from console\cron will execute the garbage-collection 
*/
defined('WP_CACHE') 
	? HB_Cache::process() 
	: HB_Cache::cleanup();

/////////////////////////////////////////////////////////////////////////////

/* Include this in your nginx conf:

location / {

        # file exist, push it
        if (-f $request_filename) {
                break;
        	}

        set $hb_file '';

        # compose the path to the static file form the cache
	if ($request_uri ~ ^.+$) {
		set $hb_file /wp-content/hb-cache/$http_host/$request_uri/hb.html;
        	}

        if ($request_method = POST) {
		set $hb_file '';
        	}

        # query string found, do not push then
        if ($query_string) {
		set $hb_file '';
        	}

	# admin, comment troll or whetever -- do not push either
        if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
		set $hb_file '';
        	}

        # only rewrite to the static cache file if it actually exists
        if (-f $document_root$hb_file) {
		rewrite ^(.*)$ $hb_file break;
        	}

        # all other requests go to WordPress
        if (!-e $request_filename) {
                rewrite . /index.php last;
        	}
	}

*/