PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-super-cache/wp-cache-phase1.php

https://bitbucket.org/derekeder/thoughtyouknew.us
PHP | 444 lines | 371 code | 66 blank | 7 comment | 178 complexity | 74d353e0bc3c5b87a737d62e22e2744a MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0, Apache-2.0, GPL-3.0
  1. <?php
  2. //error_reporting(E_ERROR | E_PARSE); // uncomment to debug this file!
  3. // Pre-2.6 compatibility
  4. if( !defined('WP_CONTENT_DIR') )
  5. define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
  6. if( !include( WP_CONTENT_DIR . '/wp-cache-config.php' ) )
  7. return true;
  8. if( !defined( 'WPCACHEHOME' ) )
  9. define('WPCACHEHOME', dirname(__FILE__).'/');
  10. include( WPCACHEHOME . 'wp-cache-base.php');
  11. if( $blogcacheid != '' ) {
  12. $blog_cache_dir = str_replace( '//', '/', $cache_path . "blogs/" . $blogcacheid . '/' );
  13. } else {
  14. $blog_cache_dir = $cache_path;
  15. }
  16. $mutex_filename = 'wp_cache_mutex.lock';
  17. $new_cache = false;
  18. // Don't change variables behind this point
  19. if( !isset( $wp_cache_plugins_dir ) )
  20. $wp_cache_plugins_dir = WPCACHEHOME . 'plugins';
  21. require_once( WPCACHEHOME . 'wp-cache-phase2.php');
  22. $plugins = glob( $wp_cache_plugins_dir . '/*.php' );
  23. if( is_array( $plugins ) ) {
  24. foreach ( $plugins as $plugin ) {
  25. if( is_file( $plugin ) )
  26. require_once( $plugin );
  27. }
  28. }
  29. if (!$cache_enabled || $_SERVER["REQUEST_METHOD"] == 'POST')
  30. return true;
  31. $file_expired = false;
  32. $cache_filename = '';
  33. $meta_file = '';
  34. $wp_cache_gzip_encoding = '';
  35. $gzipped = 0;
  36. $gzsize = 0;
  37. function gzip_accepted(){
  38. if( ini_get( 'zlib.output_compression' ) ) // don't compress WP-Cache data files when PHP is already doing it
  39. return false;
  40. if (strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false) return false;
  41. return 'gzip';
  42. }
  43. if ($cache_compression) {
  44. $wp_cache_gzip_encoding = gzip_accepted();
  45. }
  46. add_cacheaction( 'wp_cache_get_cookies_values', 'wp_cache_check_mobile' );
  47. $wp_cache_request_uri = $_SERVER[ 'REQUEST_URI' ]; // Cache this in case any plugin modifies it.
  48. if ( $wp_cache_object_cache ) {
  49. if ( ! include_once( WP_CONTENT_DIR . '/object-cache.php' ) )
  50. return;
  51. wp_cache_init(); // Note: wp-settings.php calls wp_cache_init() which clobbers the object made here.
  52. if ( ! is_object( $wp_object_cache ) )
  53. return;
  54. }
  55. if( false == @is_dir( $blog_cache_dir ) ) {
  56. @mkdir( $cache_path . "blogs" );
  57. @mkdir( $blog_cache_dir );
  58. }
  59. if( false == @is_dir( $blog_cache_dir . 'meta' ) )
  60. @mkdir( $blog_cache_dir . 'meta' );
  61. $wp_start_time = microtime();
  62. function get_wp_cache_key( $url = false ) {
  63. global $wp_cache_request_uri, $wp_cache_gzip_encoding;
  64. if ( !$url )
  65. $url = $wp_cache_request_uri;
  66. return do_cacheaction( 'wp_cache_key', $_SERVER['HTTP_HOST'] . intval( $_SERVER[ 'SERVER_PORT' ] ) . preg_replace('/#.*$/', '', str_replace( '/index.php', '/', $url ) ) . $wp_cache_gzip_encoding . wp_cache_get_cookies_values() );
  67. }
  68. function wp_super_cache_init() {
  69. 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;
  70. $wp_cache_key = get_wp_cache_key();
  71. $key = $blogcacheid . md5( $wp_cache_key );
  72. $wp_cache_key = $blogcacheid . $wp_cache_key;
  73. $cache_filename = $file_prefix . $key . '.html';
  74. $meta_file = $file_prefix . $key . '.meta';
  75. $cache_file = realpath( $blog_cache_dir . $cache_filename );
  76. $meta_pathname = realpath( $blog_cache_dir . 'meta/' . $meta_file );
  77. return compact( 'key', 'cache_filename', 'meta_file', 'cache_file', 'meta_pathname' );
  78. }
  79. function wp_cache_serve_cache_file() {
  80. 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;
  81. global $wp_cache_object_cache, $cache_compression, $wp_cache_slash_check;
  82. extract( wp_super_cache_init() );
  83. if ( wp_cache_user_agent_is_rejected() ) {
  84. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "No wp-cache file served as user agent rejected.", 5 );
  85. return false;
  86. }
  87. if ( $wp_cache_object_cache && wp_cache_get_cookies_values() == '' ) {
  88. if ( !empty( $_GET ) ) {
  89. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Non empty GET request. Not serving request from object cache", 1 );
  90. return false;
  91. }
  92. $oc_key = get_oc_key();
  93. $meta_filename = $oc_key . ".meta";
  94. if ( gzip_accepted() ) {
  95. $oc_key .= ".gz";
  96. $meta_filename .= ".gz";
  97. }
  98. $cache = wp_cache_get( $oc_key, 'supercache' );
  99. $meta = unserialize( wp_cache_get( $meta_filename, 'supercache' ) );
  100. if ( is_array( $meta ) == false ) {
  101. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Meta array from object cache corrupt. Ignoring cache.", 1 );
  102. return true;
  103. }
  104. } elseif ( file_exists( $cache_file ) ) {
  105. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "wp-cache file exists: $cache_file", 5 );
  106. if ( !( $meta = unserialize( @file_get_contents( $meta_pathname) ) ) ) {
  107. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "couldn't load wp-cache meta file", 5 );
  108. return true;
  109. }
  110. if ( is_array( $meta ) == false ) {
  111. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "meta array corrupt, deleting $meta_pathname and $cache_file", 1 );
  112. @unlink( $meta_pathname );
  113. @unlink( $cache_file );
  114. return true;
  115. }
  116. } else {
  117. // last chance, check if a supercache file exists. Just in case .htaccess rules don't work on this host
  118. $file = get_current_url_supercache_dir() . "index.html";
  119. if ( ( $wp_cache_request_uri == '/' || ( $wp_cache_slash_check && substr( $wp_cache_request_uri, -1 ) == '/' ) || ( $wp_cache_slash_check == 0 && substr( $wp_cache_request_uri, -1 ) != '/' ) ) && ( wp_cache_get_cookies_values() == '' && empty( $_GET ) && file_exists( $file ) ) ) {
  120. 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
  121. header( "Vary: Accept-Encoding, Cookie" );
  122. header( "Cache-Control: max-age=300, must-revalidate" );
  123. header( "WP-Cache: Served supercache file from PHP" );
  124. readfile( $file );
  125. 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 );
  126. die();
  127. } else {
  128. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "No wp-cache file exists. Must generate a new one.", 5 );
  129. return false;
  130. }
  131. }
  132. $cache_file = do_cacheaction( 'wp_cache_served_cache_file', $cache_file );
  133. // Sometimes the gzip headers are lost. Make sure html returned isn't compressed!
  134. if ( $cache_compression && $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $meta[ 'headers' ] ) ) {
  135. $ungzip = true;
  136. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "GZIP headers not found. Force uncompressed output.", 1 );
  137. } else {
  138. $ungzip = false;
  139. }
  140. foreach ($meta[ 'headers' ] as $t => $header) {
  141. // 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/
  142. if( strpos( $header, 'Last-Modified:' ) === false )
  143. header($header);
  144. }
  145. header( 'WP-Super-Cache: WP-Cache' );
  146. if ( $wp_cache_object_cache ) {
  147. if ( $cache ) {
  148. if ( $ungzip ) {
  149. $uncompressed = gzuncompress( $cache );
  150. if ( $uncompressed ) {
  151. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Uncompressed gzipped cache file from object cache", 1 );
  152. $cache = $uncompressed;
  153. unset( $uncompressed );
  154. }
  155. }
  156. if ( $meta[ 'dynamic' ] ) {
  157. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache dynamic file from object cache", 5 );
  158. eval( '?>' . $cache . '<?php ' );
  159. } else {
  160. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache static file from object cache", 5 );
  161. echo $cache;
  162. }
  163. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "exit request", 5 );
  164. die();
  165. }
  166. } else {
  167. if ( $meta[ 'dynamic' ] ) {
  168. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache dynamic file", 5 );
  169. if ( $ungzip ) {
  170. $cache = file_get_contents( $cache_file );
  171. $uncompressed = gzuncompress( $cache );
  172. if ( $uncompressed ) {
  173. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Uncompressed gzipped cache file from wp-cache", 1 );
  174. unset( $cache );
  175. eval( '?>' . $uncompressed . '<?php ' );
  176. } else {
  177. eval( '?>' . $cache . '<?php ' );
  178. }
  179. } else {
  180. include( $cache_file );
  181. }
  182. } else {
  183. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Serving wp-cache static file", 5 );
  184. if ( $ungzip ) {
  185. $cache = file_get_contents( $cache_file );
  186. $uncompressed = gzuncompress( $cache );
  187. if ( $uncompressed ) {
  188. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Uncompressed gzipped cache file from wp-cache", 1 );
  189. echo $uncompressed;
  190. } else {
  191. echo $cache;
  192. }
  193. } else {
  194. readfile( $cache_file );
  195. }
  196. }
  197. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "exit request", 5 );
  198. die();
  199. }
  200. }
  201. if(defined('DOING_CRON')) {
  202. extract( wp_super_cache_init() );
  203. return true;
  204. }
  205. if ( !isset( $wp_super_cache_late_init ) || ( isset( $wp_super_cache_late_init ) && false == $wp_super_cache_late_init ) ) {
  206. wp_cache_serve_cache_file();
  207. }
  208. function wp_cache_postload() {
  209. global $cache_enabled, $wp_super_cache_late_init, $wp_super_cache_debug;
  210. if ( !$cache_enabled || isset( $_GET[ 'preview' ] ) )
  211. return true;
  212. if ( isset( $wp_super_cache_late_init ) && true == $wp_super_cache_late_init ) {
  213. 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 );
  214. add_action( 'init', 'wp_cache_late_loader', 9999 );
  215. } else {
  216. wp_super_cache_init();
  217. wp_cache_phase2();
  218. }
  219. }
  220. function wp_cache_late_loader() {
  221. global $wp_super_cache_debug;
  222. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug ) wp_cache_debug( "Supercache Late Loader running on init", 3 );
  223. wp_cache_serve_cache_file();
  224. wp_cache_phase2();
  225. }
  226. function wp_cache_get_cookies_values() {
  227. $string = '';
  228. while ($key = key($_COOKIE)) {
  229. if ( preg_match( "/^wp-postpass|^wordpress_logged_in|^comment_author_/", $key ) ) {
  230. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cookie detected: $key", 5 );
  231. $string .= $_COOKIE[ $key ] . ",";
  232. }
  233. next($_COOKIE);
  234. }
  235. reset($_COOKIE);
  236. // If you use this hook, make sure you update your .htaccess rules with the same conditions
  237. $string = do_cacheaction( 'wp_cache_get_cookies_values', $string );
  238. return $string;
  239. }
  240. function add_cacheaction( $action, $func ) {
  241. global $wp_supercache_actions;
  242. $wp_supercache_actions[ $action ][] = $func;
  243. }
  244. function do_cacheaction( $action, $value = '' ) {
  245. global $wp_supercache_actions;
  246. if( array_key_exists($action, $wp_supercache_actions) && is_array( $wp_supercache_actions[ $action ] ) ) {
  247. $actions = $wp_supercache_actions[ $action ];
  248. foreach( $actions as $func ) {
  249. $value = $func( $value );
  250. }
  251. }
  252. return $value;
  253. }
  254. function wp_cache_mobile_group( $user_agent ) {
  255. global $wp_cache_mobile_groups;
  256. foreach( (array)$wp_cache_mobile_groups as $name => $group ) {
  257. foreach( (array)$group as $browser ) {
  258. $browser = trim( strtolower( $browser ) );
  259. if ( $browser != '' && strstr( $user_agent, $browser ) ) {
  260. return $browser;
  261. }
  262. }
  263. }
  264. return "mobile";
  265. }
  266. // From http://wordpress.org/extend/plugins/wordpress-mobile-edition/ by Alex King
  267. function wp_cache_check_mobile( $cache_key ) {
  268. global $wp_cache_mobile_enabled, $wp_cache_mobile_browsers, $wp_cache_mobile_prefixes;
  269. if( !isset( $wp_cache_mobile_enabled ) || false == $wp_cache_mobile_enabled )
  270. return $cache_key;
  271. if (!isset($_SERVER["HTTP_USER_AGENT"])) {
  272. return $cache_key;
  273. }
  274. $browsers = explode( ',', $wp_cache_mobile_browsers );
  275. $user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  276. foreach ($browsers as $browser) {
  277. if ( strstr( $user_agent, trim( strtolower( $browser ) ) ) ) {
  278. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "mobile browser detected: " . $_SERVER[ "HTTP_USER_AGENT" ], 5 );
  279. return $cache_key . '-' . wp_cache_mobile_group( $user_agent );
  280. }
  281. }
  282. if (isset($_SERVER['HTTP_X_WAP_PROFILE']) )
  283. return $cache_key . '-' . $_SERVER['HTTP_X_WAP_PROFILE'];
  284. if (isset($_SERVER['HTTP_PROFILE']) )
  285. return $cache_key . '-' . $_SERVER['HTTP_PROFILE'];
  286. if ( isset( $wp_cache_mobile_prefixes ) ) {
  287. $browsers = explode( ',', $wp_cache_mobile_prefixes );
  288. foreach ($browsers as $browser_prefix) {
  289. if ( substr($user_agent, 0, 4) == $browser_prefix ) {
  290. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "mobile browser (prefix) detected: " . $_SERVER[ "HTTP_USER_AGENT" ], 5 );
  291. return $cache_key . '-' . $browser_prefix;
  292. }
  293. }
  294. }
  295. $accept = strtolower($_SERVER['HTTP_ACCEPT']);
  296. if (strpos($accept, 'wap') !== false) {
  297. return $cache_key . '-' . 'wap';
  298. }
  299. if (isset($_SERVER['ALL_HTTP']) && strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false) {
  300. return $cache_key . '-' . 'operamini';
  301. }
  302. return $cache_key;
  303. }
  304. function wp_cache_debug( $message, $level = 1 ) {
  305. global $wp_cache_debug_level, $wp_cache_debug_log, $wp_cache_debug_email, $cache_path, $wp_cache_debug_ip, $wp_super_cache_debug;
  306. if ( isset( $wp_super_cache_debug ) && $wp_super_cache_debug == false )
  307. return false;
  308. if ( isset( $wp_cache_debug_email ) == false && isset( $wp_cache_debug_log ) == false )
  309. return false;
  310. if ( isset( $wp_cache_debug_level ) == false )
  311. $wp_cache_debug_level = 1;
  312. if ( $wp_cache_debug_level < $level )
  313. return false;
  314. if ( isset( $wp_cache_debug_ip ) && $wp_cache_debug_ip != '' && $wp_cache_debug_ip != $_SERVER[ 'REMOTE_ADDR' ] )
  315. return false;
  316. if ( isset( $wp_cache_debug_log ) && $wp_cache_debug_log != '' ) {
  317. error_log( date( 'H:i:s' ) . " " . $_SERVER[ 'REQUEST_URI' ] . " " . $message . "\n", 3, $cache_path . str_replace( '/', '', str_replace( '..', '', $wp_cache_debug_log ) ) );
  318. }
  319. if ( isset( $wp_cache_debug_email ) && $wp_cache_debug_email != '' ) {
  320. $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";
  321. mail( $wp_cache_debug_email, '[' . addslashes( $_SERVER[ 'HTTP_HOST' ] ) . "] WP Super Cache Debug", $message );
  322. }
  323. }
  324. function wp_cache_user_agent_is_rejected() {
  325. global $cache_rejected_user_agent;
  326. if (!function_exists('apache_request_headers')) return false;
  327. $headers = apache_request_headers();
  328. if (!isset($headers["User-Agent"])) return false;
  329. foreach ($cache_rejected_user_agent as $expr) {
  330. if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr))
  331. return true;
  332. }
  333. return false;
  334. }
  335. function get_current_url_supercache_dir( $post_id = 0 ) {
  336. global $cached_direct_pages, $cache_path, $wp_cache_request_uri;
  337. if ( $post_id != 0 ) {
  338. $uri = str_replace( site_url(), '', get_permalink( $post_id ) );
  339. } else {
  340. $uri = $wp_cache_request_uri;
  341. }
  342. $uri = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '/index.php', '/', str_replace( '..', '', preg_replace("/(\?.*)?$/", '', $uri ) ) ) );
  343. $uri = str_replace( '\\', '', $uri );
  344. $dir = strtolower(preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"])) . $uri; // To avoid XSS attacks
  345. if ( function_exists( "apply_filters" ) )
  346. $dir = apply_filters( 'supercache_dir', $dir );
  347. $dir = $cache_path . 'supercache/' . $dir . '/';
  348. if( is_array( $cached_direct_pages ) && in_array( $_SERVER[ 'REQUEST_URI' ], $cached_direct_pages ) ) {
  349. $dir = ABSPATH . $uri . '/';
  350. }
  351. $dir = str_replace( '//', '/', $dir );
  352. if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "supercache dir: $dir", 5 );
  353. return $dir;
  354. }
  355. function get_oc_version() {
  356. $wp_cache_oc_key = wp_cache_get( "wp_cache_oc_key" );
  357. if ( ! $wp_cache_oc_key ) {
  358. $wp_cache_oc_key[ 'key' ] = reset_oc_version();
  359. } elseif ( $wp_cache_oc_key[ 'ts' ] < time() - 600 )
  360. wp_cache_set( "wp_cache_oc_key", array( 'ts' => time(), 'key' => $wp_cache_oc_key[ 'key' ] ) );
  361. return $wp_cache_oc_key[ 'key' ];
  362. }
  363. function reset_oc_version( $version = 1 ) {
  364. if ( $version == 1 )
  365. $version = mt_rand();
  366. wp_cache_set( "wp_cache_oc_key", array( 'ts' => time(), 'key' => $version ) );
  367. return $version;
  368. }
  369. function get_oc_key( $url = false ) {
  370. global $wp_cache_gzip_encoding;
  371. if ( $url ) {
  372. $key = intval( $_SERVER[ 'SERVER_PORT' ] ) . strtolower( preg_replace( '/:.*$/', '', $_SERVER[ "HTTP_HOST" ] ) ) . $url;
  373. } else {
  374. $key = get_current_url_supercache_dir();
  375. }
  376. return $key . $wp_cache_gzip_encoding . get_oc_version();
  377. }
  378. ?>