PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/brandonburke/WordPress-Plugin-Baseline
PHP | 264 lines | 236 code | 26 blank | 2 comment | 26 complexity | 2ba5f688b63cae9b630e330452d3ee87 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( false == @is_dir( $blog_cache_dir ) ) {
  48. @mkdir( $cache_path . "blogs" );
  49. @mkdir( $blog_cache_dir );
  50. }
  51. if( false == @is_dir( $blog_cache_dir . 'meta' ) )
  52. @mkdir( $blog_cache_dir . 'meta' );
  53. $wp_start_time = microtime();
  54. function wp_super_cache_init() {
  55. 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;
  56. $wp_cache_key = do_cacheaction( 'wp_cache_key', $_SERVER['HTTP_HOST'].preg_replace('/#.*$/', '', str_replace( '/index.php', '/', $wp_cache_request_uri ) ).$wp_cache_gzip_encoding.wp_cache_get_cookies_values() );
  57. $key = $blogcacheid . md5( $wp_cache_key );
  58. $wp_cache_key = $blogcacheid . $wp_cache_key;
  59. $cache_filename = $file_prefix . $key . '.html';
  60. $meta_file = $file_prefix . $key . '.meta';
  61. $cache_file = realpath( $blog_cache_dir . $cache_filename );
  62. $meta_pathname = realpath( $blog_cache_dir . 'meta/' . $meta_file );
  63. return compact( 'key', 'cache_filename', 'meta_file', 'cache_file', 'meta_pathname' );
  64. }
  65. function wp_cache_serve_cache_file() {
  66. 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;
  67. extract( wp_super_cache_init() );
  68. if ( file_exists( $cache_file ) && !wp_cache_user_agent_is_rejected() ) {
  69. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "wp-cache file exists", 5 );
  70. if ( !( $meta = unserialize( @file_get_contents( $meta_pathname) ) ) ) {
  71. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "couldn't load wp-cache meta file", 5 );
  72. return true;
  73. }
  74. if ( is_array( $meta ) == false ) {
  75. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "meta array corrupt, deleting $meta_pathname and $cache_file", 1 );
  76. @unlink( $meta_pathname );
  77. @unlink( $cache_file );
  78. return true;
  79. }
  80. $cache_file = do_cacheaction( 'wp_cache_served_cache_file', $cache_file );
  81. // Sometimes the gzip headers are lost. If this is a gzip capable client, send those headers.
  82. if( $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $meta[ 'headers' ] ) ) {
  83. $meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
  84. $meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
  85. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "added gzip headers before serving.", 1 );
  86. }
  87. foreach ($meta[ 'headers' ] as $t => $header) {
  88. // 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/
  89. if( strpos( $header, 'Last-Modified:' ) === false )
  90. header($header);
  91. }
  92. header( 'WP-Super-Cache: WP-Cache' );
  93. if ( $meta[ 'dynamic' ] ) {
  94. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache dynamic file", 5 );
  95. include($cache_file);
  96. } else {
  97. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache static file", 5 );
  98. readfile( $cache_file );
  99. }
  100. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "exit request", 5 );
  101. die();
  102. } else {
  103. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "No wp-cache file exists or user agent rejected.", 5 );
  104. }
  105. }
  106. if(defined('DOING_CRON')) {
  107. extract( wp_super_cache_init() );
  108. return true;
  109. }
  110. if ( !isset( $wp_super_cache_late_init ) || ( isset( $wp_super_cache_late_init ) && false == $wp_super_cache_late_init ) ) {
  111. wp_cache_serve_cache_file();
  112. }
  113. function wp_cache_postload() {
  114. global $cache_enabled, $wp_super_cache_late_init, $wp_super_cache_debug;
  115. if ( !$cache_enabled || isset( $_GET[ 'preview' ] ) )
  116. return true;
  117. if ( isset( $wp_super_cache_late_init ) && true == $wp_super_cache_late_init ) {
  118. 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 );
  119. add_action( 'init', 'wp_cache_late_loader', 9999 );
  120. } else {
  121. wp_super_cache_init();
  122. wp_cache_phase2();
  123. }
  124. }
  125. function wp_cache_late_loader() {
  126. global $wp_super_cache_debug;
  127. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Supercache Late Loader running on init", 3 );
  128. wp_cache_serve_cache_file();
  129. wp_cache_phase2();
  130. }
  131. function wp_cache_get_cookies_values() {
  132. $string = '';
  133. while ($key = key($_COOKIE)) {
  134. if ( preg_match( "/^wp-postpass|^wordpress|^comment_author_/", $key ) ) {
  135. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cookie detected: $key", 5 );
  136. $string .= $_COOKIE[ $key ] . ",";
  137. }
  138. next($_COOKIE);
  139. }
  140. reset($_COOKIE);
  141. // If you use this hook, make sure you update your .htaccess rules with the same conditions
  142. $string = do_cacheaction( 'wp_cache_get_cookies_values', $string );
  143. return $string;
  144. }
  145. function add_cacheaction( $action, $func ) {
  146. global $wp_supercache_actions;
  147. $wp_supercache_actions[ $action ][] = $func;
  148. }
  149. function do_cacheaction( $action, $value = '' ) {
  150. global $wp_supercache_actions;
  151. if( is_array( $wp_supercache_actions[ $action ] ) ) {
  152. $actions = $wp_supercache_actions[ $action ];
  153. foreach( $actions as $func ) {
  154. $value = $func( $value );
  155. }
  156. }
  157. return $value;
  158. }
  159. // From http://wordpress.org/extend/plugins/wordpress-mobile-edition/ by Alex King
  160. function wp_cache_check_mobile( $cache_key ) {
  161. global $wp_cache_mobile_enabled, $wp_cache_mobile_browsers, $wp_cache_mobile_whitelist;
  162. if( !isset( $wp_cache_mobile_enabled ) || false == $wp_cache_mobile_enabled )
  163. return $cache_key;
  164. if (!isset($_SERVER["HTTP_USER_AGENT"])) {
  165. return $cache_key;
  166. }
  167. $whitelist = explode( ',', $wp_cache_mobile_whitelist );
  168. foreach ($whitelist as $browser) {
  169. if (strstr($_SERVER["HTTP_USER_AGENT"], trim($browser))) {
  170. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "whitelst mobile browser detected: " . $_SERVER[ "HTTP_USER_AGENT" ], 5 );
  171. return $cache_key;
  172. }
  173. }
  174. $browsers = explode( ',', $wp_cache_mobile_browsers );
  175. foreach ($browsers as $browser) {
  176. if (strstr($_SERVER["HTTP_USER_AGENT"], trim( $browser ))) {
  177. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "mobile browser detected: " . $_SERVER[ "HTTP_USER_AGENT" ], 5 );
  178. return $cache_key . $browser;
  179. }
  180. }
  181. return $cache_key;
  182. }
  183. function wp_cache_debug( $message, $level = 1 ) {
  184. global $wp_cache_debug_level, $wp_cache_debug_log, $wp_cache_debug_email, $cache_path, $wp_cache_debug_ip, $wp_super_cache_debug;
  185. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug == false )
  186. return false;
  187. if ( isset( $wp_cache_debug_email ) == false && isset( $wp_cache_debug_log ) == false )
  188. return false;
  189. if ( isset( $wp_cache_debug_level ) == false )
  190. $wp_cache_debug_level = 1;
  191. if ( $wp_cache_debug_level < $level )
  192. return false;
  193. if ( isset( $wp_cache_debug_ip ) && $wp_cache_debug_ip != '' && $wp_cache_debug_ip != $_SERVER[ 'REMOTE_ADDR' ] )
  194. return false;
  195. if ( isset( $wp_cache_debug_log ) && $wp_cache_debug_log != '' ) {
  196. error_log( date( 'H:i:s' ) . " " . $_SERVER[ 'REQUEST_URI' ] . " " . $message . "\n", 3, $cache_path . str_replace( '/', '', str_replace( '..', '', $wp_cache_debug_log ) ) );
  197. }
  198. if ( isset( $wp_cache_debug_email ) && $wp_cache_debug_email != '' ) {
  199. $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";
  200. mail( $wp_cache_debug_email, '[' . addslashes( $_SERVER[ 'HTTP_HOST' ] ) . "] WP Super Cache Debug", $message );
  201. }
  202. }
  203. function wp_cache_user_agent_is_rejected() {
  204. global $cache_rejected_user_agent;
  205. if (!function_exists('apache_request_headers')) return false;
  206. $headers = apache_request_headers();
  207. if (!isset($headers["User-Agent"])) return false;
  208. foreach ($cache_rejected_user_agent as $expr) {
  209. if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr))
  210. return true;
  211. }
  212. return false;
  213. }
  214. ?>