PageRenderTime 120ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-super-cache/tags/0.9.9/wp-cache-phase1.php

https://github.com/brandonburke/WordPress-Plugin-Baseline
PHP | 369 lines | 301 code | 62 blank | 6 comment | 134 complexity | 209ec67f5c984abb68ada6356c59c60e MD5 | raw file
  1. <?php
  2. // Pre-2.6 compatibility
  3. if( !defined('WP_CONTENT_DIR') )
  4. define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
  5. if( !include( WP_CONTENT_DIR . '/wp-cache-config.php' ) )
  6. return true;
  7. if( !defined( 'WPCACHEHOME' ) )
  8. define('WPCACHEHOME', dirname(__FILE__).'/');
  9. include( WPCACHEHOME . 'wp-cache-base.php');
  10. if( $blogcacheid != '' ) {
  11. $blog_cache_dir = str_replace( '//', '/', $cache_path . "blogs/" . $blogcacheid . '/' );
  12. } else {
  13. $blog_cache_dir = $cache_path;
  14. }
  15. $mutex_filename = 'wp_cache_mutex.lock';
  16. $new_cache = false;
  17. // Don't change variables behind this point
  18. if( !isset( $wp_cache_plugins_dir ) )
  19. $wp_cache_plugins_dir = WPCACHEHOME . 'plugins';
  20. require_once( WPCACHEHOME . 'wp-cache-phase2.php');
  21. $plugins = glob( $wp_cache_plugins_dir . '/*.php' );
  22. if( is_array( $plugins ) ) {
  23. foreach ( $plugins as $plugin ) {
  24. if( is_file( $plugin ) )
  25. require_once( $plugin );
  26. }
  27. }
  28. if (!$cache_enabled || $_SERVER["REQUEST_METHOD"] == 'POST')
  29. return true;
  30. $file_expired = false;
  31. $cache_filename = '';
  32. $meta_file = '';
  33. $wp_cache_gzip_encoding = '';
  34. $gzipped = 0;
  35. $gzsize = 0;
  36. function gzip_accepted(){
  37. if( ini_get( 'zlib.output_compression' ) ) // don't compress WP-Cache data files when PHP is already doing it
  38. return false;
  39. if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false) return false;
  40. return 'gzip';
  41. }
  42. if ($cache_compression) {
  43. $wp_cache_gzip_encoding = gzip_accepted();
  44. }
  45. add_cacheaction( 'wp_cache_get_cookies_values', 'wp_cache_check_mobile' );
  46. $wp_cache_request_uri = $_SERVER[ 'REQUEST_URI' ]; // Cache this in case any plugin modifies it.
  47. if ( $wp_cache_object_cache ) {
  48. if ( ! include_once( WP_CONTENT_DIR . '/object-cache.php' ) )
  49. return;
  50. wp_cache_init(); // Note: wp-settings.php calls wp_cache_init() which clobbers the object made here.
  51. if ( ! is_object( $wp_object_cache ) )
  52. return;
  53. }
  54. if( false == @is_dir( $blog_cache_dir ) ) {
  55. @mkdir( $cache_path . "blogs" );
  56. @mkdir( $blog_cache_dir );
  57. }
  58. if( false == @is_dir( $blog_cache_dir . 'meta' ) )
  59. @mkdir( $blog_cache_dir . 'meta' );
  60. $wp_start_time = microtime();
  61. function get_wp_cache_key( $url = false ) {
  62. global $wp_cache_request_uri, $wp_cache_gzip_encoding;
  63. if ( !$url )
  64. $url = $wp_cache_request_uri;
  65. return do_cacheaction( 'wp_cache_key', $_SERVER['HTTP_HOST'].preg_replace('/#.*$/', '', str_replace( '/index.php', '/', $url ) ) . $wp_cache_gzip_encoding . wp_cache_get_cookies_values() );
  66. }
  67. function wp_super_cache_init() {
  68. global $wp_cache_key, $key, $blogcacheid, $wp_cache_request_uri, $file_prefix, $blog_cache_dir, $meta_file, $cache_file, $cache_filename, $wp_super_cache_debug, $meta_pathname, $wp_cache_gzip_encoding, $meta;
  69. $wp_cache_key = get_wp_cache_key();
  70. $key = $blogcacheid . md5( $wp_cache_key );
  71. $wp_cache_key = $blogcacheid . $wp_cache_key;
  72. $cache_filename = $file_prefix . $key . '.html';
  73. $meta_file = $file_prefix . $key . '.meta';
  74. $cache_file = realpath( $blog_cache_dir . $cache_filename );
  75. $meta_pathname = realpath( $blog_cache_dir . 'meta/' . $meta_file );
  76. return compact( 'key', 'cache_filename', 'meta_file', 'cache_file', 'meta_pathname' );
  77. }
  78. function wp_cache_serve_cache_file() {
  79. global $key, $blogcacheid, $wp_cache_request_uri, $file_prefix, $blog_cache_dir, $meta_file, $cache_file, $cache_filename, $wp_super_cache_debug, $meta_pathname, $wp_cache_gzip_encoding, $meta;
  80. global $wp_cache_object_cache;
  81. extract( wp_super_cache_init() );
  82. if ( wp_cache_user_agent_is_rejected() ) {
  83. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "No wp-cache file served as user agent rejected.", 5 );
  84. return false;
  85. }
  86. if ( $wp_cache_object_cache && wp_cache_get_cookies_values() == '' ) {
  87. if ( !empty( $_GET ) ) {
  88. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Non empty GET request. Not serving request from object cache", 1 );
  89. return false;
  90. }
  91. $oc_key = get_oc_key();
  92. $meta_filename = $oc_key . ".meta";
  93. if ( gzip_accepted() ) {
  94. $oc_key .= ".gz";
  95. $meta_filename .= ".gz";
  96. }
  97. $cache = wp_cache_get( $oc_key, 'supercache' );
  98. $meta = unserialize( wp_cache_get( $meta_filename, 'supercache' ) );
  99. if ( is_array( $meta ) == false ) {
  100. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Meta array from object cache corrupt. Ignoring cache.", 1 );
  101. return true;
  102. }
  103. } elseif ( file_exists( $cache_file ) ) {
  104. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "wp-cache file exists", 5 );
  105. if ( !( $meta = unserialize( @file_get_contents( $meta_pathname) ) ) ) {
  106. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "couldn't load wp-cache meta file", 5 );
  107. return true;
  108. }
  109. if ( is_array( $meta ) == false ) {
  110. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "meta array corrupt, deleting $meta_pathname and $cache_file", 1 );
  111. @unlink( $meta_pathname );
  112. @unlink( $cache_file );
  113. return true;
  114. }
  115. } else {
  116. // last chance, check if a supercache file exists. Just in case .htaccess rules don't work on this host
  117. $file = get_current_url_supercache_dir() . "index.html";
  118. if ( wp_cache_get_cookies_values() == '' && empty( $_GET ) && file_exists( $file ) ) {
  119. header( "Content-type: text/html; charset=UTF-8" ); // UTF-8 hard coded is bad but we don't know what it is this early in the process
  120. header( "Vary: Accept-Encoding, Cookie" );
  121. header( "Cache-Control: max-age=300, must-revalidate" );
  122. header( "WP-Cache: Served supercache file from PHP" );
  123. readfile( $file );
  124. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Served page from supercache file. Mod rewrite rules may be broken or missing.", 5 );
  125. die();
  126. } else {
  127. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "No wp-cache file exists. Must generate a new one.", 5 );
  128. return false;
  129. }
  130. }
  131. $cache_file = do_cacheaction( 'wp_cache_served_cache_file', $cache_file );
  132. // Sometimes the gzip headers are lost. If this is a gzip capable client, send those headers.
  133. if( $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $meta[ 'headers' ] ) ) {
  134. $meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
  135. $meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
  136. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "added gzip headers before serving.", 1 );
  137. }
  138. foreach ($meta[ 'headers' ] as $t => $header) {
  139. // godaddy fix, via http://blog.gneu.org/2008/05/wp-supercache-on-godaddy/ and http://www.littleredrails.com/blog/2007/09/08/using-wp-cache-on-godaddy-500-error/
  140. if( strpos( $header, 'Last-Modified:' ) === false )
  141. header($header);
  142. }
  143. header( 'WP-Super-Cache: WP-Cache' );
  144. if ( $wp_cache_object_cache ) {
  145. if ( $cache ) {
  146. if ( $meta[ 'dynamic' ] ) {
  147. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache dynamic file from object cache", 5 );
  148. eval( $cache );
  149. } else {
  150. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache static file from object cache", 5 );
  151. echo $cache;
  152. }
  153. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "exit request", 5 );
  154. die();
  155. }
  156. } else {
  157. if ( $meta[ 'dynamic' ] ) {
  158. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache dynamic file", 5 );
  159. include($cache_file);
  160. } else {
  161. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache static file", 5 );
  162. readfile( $cache_file );
  163. }
  164. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "exit request", 5 );
  165. die();
  166. }
  167. }
  168. if(defined('DOING_CRON')) {
  169. extract( wp_super_cache_init() );
  170. return true;
  171. }
  172. if ( !isset( $wp_super_cache_late_init ) || ( isset( $wp_super_cache_late_init ) && false == $wp_super_cache_late_init ) ) {
  173. wp_cache_serve_cache_file();
  174. }
  175. function wp_cache_postload() {
  176. global $cache_enabled, $wp_super_cache_late_init, $wp_super_cache_debug;
  177. if ( !$cache_enabled || isset( $_GET[ 'preview' ] ) )
  178. return true;
  179. if ( isset( $wp_super_cache_late_init ) && true == $wp_super_cache_late_init ) {
  180. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Supercache Late Init: add wp_cache_serve_cache_file to init", 3 );
  181. add_action( 'init', 'wp_cache_late_loader', 9999 );
  182. } else {
  183. wp_super_cache_init();
  184. wp_cache_phase2();
  185. }
  186. }
  187. function wp_cache_late_loader() {
  188. global $wp_super_cache_debug;
  189. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Supercache Late Loader running on init", 3 );
  190. wp_cache_serve_cache_file();
  191. wp_cache_phase2();
  192. }
  193. function wp_cache_get_cookies_values() {
  194. $string = '';
  195. while ($key = key($_COOKIE)) {
  196. if ( preg_match( "/^wp-postpass|^wordpress|^comment_author_/", $key ) ) {
  197. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cookie detected: $key", 5 );
  198. $string .= $_COOKIE[ $key ] . ",";
  199. }
  200. next($_COOKIE);
  201. }
  202. reset($_COOKIE);
  203. // If you use this hook, make sure you update your .htaccess rules with the same conditions
  204. $string = do_cacheaction( 'wp_cache_get_cookies_values', $string );
  205. return $string;
  206. }
  207. function add_cacheaction( $action, $func ) {
  208. global $wp_supercache_actions;
  209. $wp_supercache_actions[ $action ][] = $func;
  210. }
  211. function do_cacheaction( $action, $value = '' ) {
  212. global $wp_supercache_actions;
  213. if( is_array( $wp_supercache_actions[ $action ] ) ) {
  214. $actions = $wp_supercache_actions[ $action ];
  215. foreach( $actions as $func ) {
  216. $value = $func( $value );
  217. }
  218. }
  219. return $value;
  220. }
  221. // From http://wordpress.org/extend/plugins/wordpress-mobile-edition/ by Alex King
  222. function wp_cache_check_mobile( $cache_key ) {
  223. global $wp_cache_mobile_enabled, $wp_cache_mobile_browsers;
  224. if( !isset( $wp_cache_mobile_enabled ) || false == $wp_cache_mobile_enabled )
  225. return $cache_key;
  226. if (!isset($_SERVER["HTTP_USER_AGENT"])) {
  227. return $cache_key;
  228. }
  229. $browsers = explode( ',', $wp_cache_mobile_browsers );
  230. foreach ($browsers as $browser) {
  231. if (strstr($_SERVER["HTTP_USER_AGENT"], trim( $browser ))) {
  232. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "mobile browser detected: " . $_SERVER[ "HTTP_USER_AGENT" ], 5 );
  233. return $cache_key . $browser;
  234. }
  235. }
  236. return $cache_key;
  237. }
  238. function wp_cache_debug( $message, $level = 1 ) {
  239. global $wp_cache_debug_level, $wp_cache_debug_log, $wp_cache_debug_email, $cache_path, $wp_cache_debug_ip, $wp_super_cache_debug;
  240. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug == false )
  241. return false;
  242. if ( isset( $wp_cache_debug_email ) == false && isset( $wp_cache_debug_log ) == false )
  243. return false;
  244. if ( isset( $wp_cache_debug_level ) == false )
  245. $wp_cache_debug_level = 1;
  246. if ( $wp_cache_debug_level < $level )
  247. return false;
  248. if ( isset( $wp_cache_debug_ip ) && $wp_cache_debug_ip != '' && $wp_cache_debug_ip != $_SERVER[ 'REMOTE_ADDR' ] )
  249. return false;
  250. if ( isset( $wp_cache_debug_log ) && $wp_cache_debug_log != '' ) {
  251. error_log( date( 'H:i:s' ) . " " . $_SERVER[ 'REQUEST_URI' ] . " " . $message . "\n", 3, $cache_path . str_replace( '/', '', str_replace( '..', '', $wp_cache_debug_log ) ) );
  252. }
  253. if ( isset( $wp_cache_debug_email ) && $wp_cache_debug_email != '' ) {
  254. $message .= "\n\nDisable these emails by commenting out or deleting the line containing\n\$wp_cache_debug_email in wp-content/wp-cache-config.php on your server.\n";
  255. mail( $wp_cache_debug_email, '[' . addslashes( $_SERVER[ 'HTTP_HOST' ] ) . "] WP Super Cache Debug", $message );
  256. }
  257. }
  258. function wp_cache_user_agent_is_rejected() {
  259. global $cache_rejected_user_agent;
  260. if (!function_exists('apache_request_headers')) return false;
  261. $headers = apache_request_headers();
  262. if (!isset($headers["User-Agent"])) return false;
  263. foreach ($cache_rejected_user_agent as $expr) {
  264. if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr))
  265. return true;
  266. }
  267. return false;
  268. }
  269. function get_current_url_supercache_dir() {
  270. global $cached_direct_pages, $cache_path, $wp_cache_request_uri;
  271. $uri = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '/index.php', '/', str_replace( '..', '', preg_replace("/(\?.*)?$/", '', $wp_cache_request_uri ) ) ) );
  272. $uri = str_replace( '\\', '', $uri );
  273. $dir = strtolower(preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"])) . $uri; // To avoid XSS attacks
  274. if ( function_exists( "apply_filters" ) )
  275. $dir = apply_filters( 'supercache_dir', $dir );
  276. $dir = $cache_path . 'supercache/' . $dir . '/';
  277. if( is_array( $cached_direct_pages ) && in_array( $_SERVER[ 'REQUEST_URI' ], $cached_direct_pages ) ) {
  278. $dir = ABSPATH . $uri . '/';
  279. }
  280. $dir = str_replace( '//', '/', $dir );
  281. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "supercache dir: $dir", 5 );
  282. return $dir;
  283. }
  284. function get_oc_version() {
  285. $wp_cache_oc_key = wp_cache_get( "wp_cache_oc_key" );
  286. if ( ! $wp_cache_oc_key ) {
  287. $wp_cache_oc_key[ 'key' ] = reset_oc_version();
  288. } elseif ( $wp_cache_oc_key[ 'ts' ] < time() - 600 )
  289. wp_cache_set( "wp_cache_oc_key", array( 'ts' => time(), 'key' => $wp_cache_oc_key[ 'key' ] ) );
  290. return $wp_cache_oc_key[ 'key' ];
  291. }
  292. function reset_oc_version( $version = 1 ) {
  293. if ( $version == 1 )
  294. $version = mt_rand();
  295. wp_cache_set( "wp_cache_oc_key", array( 'ts' => time(), 'key' => $version ) );
  296. return $version;
  297. }
  298. function get_oc_key( $url = false ) {
  299. global $wp_cache_gzip_encoding;
  300. if ( $url ) {
  301. $key = strtolower( preg_replace( '/:.*$/', '', $_SERVER[ "HTTP_HOST" ] ) ) . $url;
  302. } else {
  303. $key = get_current_url_supercache_dir();
  304. }
  305. return $key . $wp_cache_gzip_encoding . get_oc_version();
  306. }
  307. ?>