PageRenderTime 41ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wp-cache/wp-cache-phase2.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 328 lines | 288 code | 23 blank | 17 comment | 30 complexity | 47ffedbd2cfcad286c15bb040619b4e8 MD5 | raw file
  1. <?php
  2. /** Diable here because PHP4.3 does not make the global
  3. Serious bug?
  4. $mutex_filename = 'wp_cache_mutex.lock';
  5. $new_cache = false;
  6. */
  7. function wp_cache_phase2() {
  8. global $cache_filename, $cache_acceptable_files, $wp_cache_meta_object;
  9. wp_cache_mutex_init();
  10. if(function_exists('add_action')) {
  11. // Post ID is received
  12. add_action('publish_post', 'wp_cache_post_change', 0);
  13. add_action('edit_post', 'wp_cache_post_change', 0);
  14. add_action('delete_post', 'wp_cache_post_change', 0);
  15. add_action('publish_phone', 'wp_cache_post_change', 0);
  16. // Coment ID is received
  17. add_action('trackback_post', 'wp_cache_get_postid_from_comment', 0);
  18. add_action('pingback_post', 'wp_cache_get_postid_from_comment', 0);
  19. add_action('comment_post', 'wp_cache_get_postid_from_comment', 0);
  20. add_action('edit_comment', 'wp_cache_get_postid_from_comment', 0);
  21. add_action('wp_set_comment_status', 'wp_cache_get_postid_from_comment', 0);
  22. // No post_id is available
  23. add_action('delete_comment', 'wp_cache_no_postid', 0);
  24. add_action('switch_theme', 'wp_cache_no_postid', 0);
  25. }
  26. //$script = basename($_SERVER['SCRIPT_NAME']);
  27. if( $_SERVER["REQUEST_METHOD"] == 'POST' || get_settings('gzipcompression'))
  28. return;
  29. $script = basename($_SERVER['PHP_SELF']);
  30. if (!in_array($script, $cache_acceptable_files) &&
  31. wp_cache_is_rejected($_SERVER["REQUEST_URI"]))
  32. return;
  33. if (wp_cache_user_agent_is_rejected()) return;
  34. $wp_cache_meta_object = new CacheMeta;
  35. ob_start('wp_cache_ob_callback');
  36. register_shutdown_function('wp_cache_shutdown_callback');
  37. }
  38. function wp_cache_get_response_headers() {
  39. if(function_exists('apache_response_headers')) {
  40. $headers = apache_response_headers();
  41. } else if(function_exists('headers_list')) {
  42. $headers = array();
  43. foreach(headers_list() as $hdr) {
  44. list($header_name, $header_value) = explode(': ', $hdr, 2);
  45. $headers[$header_name] = $header_value;
  46. }
  47. } else
  48. $headers = null;
  49. return $headers;
  50. }
  51. function wp_cache_is_rejected($uri) {
  52. global $cache_rejected_uri;
  53. if (strstr($uri, '/wp-admin/'))
  54. return true; //we don't allow cacheing wp-admin for security
  55. foreach ($cache_rejected_uri as $expr) {
  56. if (strlen($expr) > 0 && strstr($uri, $expr))
  57. return true;
  58. }
  59. return false;
  60. }
  61. function wp_cache_user_agent_is_rejected() {
  62. global $cache_rejected_user_agent;
  63. if (!function_exists('apache_request_headers')) return false;
  64. $headers = apache_request_headers();
  65. if (!isset($headers["User-Agent"])) return false;
  66. foreach ($cache_rejected_user_agent as $expr) {
  67. if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr))
  68. return true;
  69. }
  70. return false;
  71. }
  72. function wp_cache_mutex_init() {
  73. global $use_flock, $mutex, $cache_path, $mutex_filename, $sem_id;
  74. if(!is_bool($use_flock)) {
  75. if(function_exists('sem_get'))
  76. $use_flock = false;
  77. else
  78. $use_flock = true;
  79. }
  80. if ($use_flock)
  81. $mutex = fopen($cache_path . $mutex_filename, 'w');
  82. else
  83. $mutex = sem_get($sem_id, 1, 0644 | IPC_CREAT, 1);
  84. }
  85. function wp_cache_writers_entry() {
  86. global $use_flock, $mutex, $cache_path, $mutex_filename;
  87. if ($use_flock)
  88. flock($mutex, LOCK_EX);
  89. else
  90. sem_acquire($mutex);
  91. }
  92. function wp_cache_writers_exit() {
  93. global $use_flock, $mutex, $cache_path, $mutex_filename;
  94. if ($use_flock)
  95. flock($mutex, LOCK_UN);
  96. else
  97. sem_release($mutex);
  98. }
  99. function wp_cache_ob_callback($buffer) {
  100. global $cache_path, $cache_filename, $meta_file, $wp_start_time;
  101. global $new_cache, $wp_cache_meta_object, $file_expired, $blog_id;
  102. /* Mode paranoic, check for closing tags
  103. * we avoid caching incomplete files */
  104. if (is_404() || !preg_match('/(<\/html>|<\/rss>|<\/feed>)/i',$buffer) ) {
  105. $new_cache = false;
  106. return $buffer;
  107. }
  108. $duration = wp_cache_microtime_diff($wp_start_time, microtime());
  109. $duration = sprintf("%0.3f", $duration);
  110. $buffer .= "\n<!-- Dynamic Page Served (once) in $duration seconds -->\n";
  111. wp_cache_writers_entry();
  112. $mtime = @filemtime($cache_path . $cache_filename);
  113. /* Return if:
  114. the file didn't exist before but it does exist now (another connection created)
  115. OR
  116. the file was expired and its mtime is less than 5 seconds
  117. */
  118. if( !((!$file_expired && $mtime) || ($mtime && $file_expired && (time() - $mtime) < 5)) ) {
  119. $fr = fopen($cache_path . $cache_filename, 'w');
  120. if (!$fr)
  121. $buffer = "Couldn't write to: " . $cache_path . $cache_filename . "\n";
  122. if (preg_match('/<!--mclude|<!--mfunc/', $buffer)) { //Dynamic content
  123. $store = preg_replace('|<!--mclude (.*?)-->(.*?)<!--/mclude-->|is',
  124. "<!--mclude-->\n<?php include_once('" . ABSPATH . "$1'); ?>\n<!--/mclude-->", $buffer);
  125. $store = preg_replace('|<!--mfunc (.*?)-->(.*?)<!--/mfunc-->|is',
  126. "<!--mfunc-->\n<?php $1 ;?>\n<!--/mfunc-->", $store);
  127. $wp_cache_meta_object->dynamic = true;
  128. /* Clean function calls in tag */
  129. $buffer = preg_replace('|<!--mclude (.*?)-->|is', '<!--mclude-->', $buffer);
  130. $buffer = preg_replace('|<!--mfunc (.*?)-->|is', '<!--mfunc-->', $buffer);
  131. fputs($fr, $store);
  132. } else {
  133. fputs($fr, $buffer);
  134. }
  135. $new_cache = true;
  136. fclose($fr);
  137. }
  138. wp_cache_writers_exit();
  139. return $buffer;
  140. }
  141. function wp_cache_phase2_clean_cache($file_prefix) {
  142. global $cache_path;
  143. wp_cache_writers_entry();
  144. if ( ($handle = opendir( $cache_path )) ) {
  145. while ( false !== ($file = readdir($handle))) {
  146. if ( preg_match("/^$file_prefix/", $file) ) {
  147. unlink($cache_path . $file);
  148. }
  149. }
  150. closedir($handle);
  151. }
  152. wp_cache_writers_exit();
  153. }
  154. function wp_cache_phase2_clean_expired($file_prefix) {
  155. global $cache_path, $cache_max_time;
  156. clearstatcache();
  157. wp_cache_writers_entry();
  158. $now = time();
  159. if ( ($handle = opendir( $cache_path )) ) {
  160. while ( false !== ($file = readdir($handle))) {
  161. if ( preg_match("/^$file_prefix/", $file) &&
  162. (filemtime($cache_path . $file) + $cache_max_time) <= $now ) {
  163. unlink($cache_path . $file);
  164. }
  165. }
  166. closedir($handle);
  167. }
  168. wp_cache_writers_exit();
  169. }
  170. function wp_cache_shutdown_callback() {
  171. global $cache_path, $cache_max_time, $file_expired, $file_prefix, $meta_file, $new_cache;
  172. global $wp_cache_meta_object, $known_headers, $blog_id;
  173. $wp_cache_meta_object->uri = $_SERVER["SERVER_NAME"].preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', $_SERVER['REQUEST_URI']); // To avoid XSS attacs
  174. $wp_cache_meta_object->blog_id=$blog_id;
  175. $wp_cache_meta_object->post = wp_cache_post_id();
  176. $response = wp_cache_get_response_headers();
  177. $wp_cache_meta_object->headers = array();
  178. foreach ($known_headers as $key) {
  179. if(isset($response{$key})) {
  180. array_push($wp_cache_meta_object->headers, "$key: " . $response{$key});
  181. }
  182. }
  183. /* Not used because it gives problems with some
  184. * PHP installations
  185. if (!$response{'Content-Length'}) {
  186. // WP does not set content size
  187. $content_size = ob_get_length();
  188. @header("Content-Length: $content_size");
  189. array_push($wp_cache_meta_object->headers, "Content-Length: $content_size");
  190. }
  191. */
  192. if (!$response{'Last-Modified'}) {
  193. $value = gmdate('D, d M Y H:i:s') . ' GMT';
  194. /* Dont send this the first time */
  195. /* @header('Last-Modified: ' . $value); */
  196. array_push($wp_cache_meta_object->headers, "Last-Modified: $value");
  197. }
  198. if (!$response{'Content-Type'} && !$response{'Content-type'}) {
  199. $value = "text/html; charset=" . get_settings('blog_charset');
  200. @header("Content-Type: $value");
  201. array_push($wp_cache_meta_object->headers, "Content-Type: $value");
  202. }
  203. ob_end_flush();
  204. if ($new_cache) {
  205. $serial = serialize($wp_cache_meta_object);
  206. wp_cache_writers_entry();
  207. $fr = fopen($cache_path . $meta_file, 'w');
  208. fputs($fr, $serial);
  209. fclose($fr);
  210. wp_cache_writers_exit();
  211. }
  212. if ($file_expired == false) {
  213. return;
  214. }
  215. // we delete expired files
  216. flush(); //Ensure we send data to the client
  217. wp_cache_phase2_clean_expired($file_prefix);
  218. }
  219. function wp_cache_no_postid($id) {
  220. return wp_cache_post_change(wp_cache_post_id());
  221. }
  222. function wp_cache_get_postid_from_comment($comment_id) {
  223. $comment = get_commentdata($comment_id, 1, true);
  224. $postid = $comment['comment_post_ID'];
  225. // Do nothing if comment is not moderated
  226. // http://ocaoimh.ie/2006/12/05/caching-wordpress-with-wp-cache-in-a-spam-filled-world
  227. if( !preg_match('/wp-admin\//', $_SERVER['REQUEST_URI']) && $comment['comment_approved'] != 1 )
  228. return $post_id;
  229. // We must check it up again due to WP bugs calling two different actions
  230. // for delete, for example both wp_set_comment_status and delete_comment
  231. // are called when deleting a comment
  232. if ($postid > 0)
  233. return wp_cache_post_change($postid);
  234. else
  235. return wp_cache_post_change(wp_cache_post_id());
  236. }
  237. function wp_cache_post_change($post_id) {
  238. global $file_prefix;
  239. global $cache_path;
  240. global $blog_id;
  241. static $last_processed = -1;
  242. // Avoid cleaning twice the same pages
  243. if ($post_id == $last_processed) return $post_id;
  244. $last_processed = $post_id;
  245. $meta = new CacheMeta;
  246. $matches = array();
  247. wp_cache_writers_entry();
  248. if ( ($handle = opendir( $cache_path )) ) {
  249. while ( false !== ($file = readdir($handle))) {
  250. if ( preg_match("/^($file_prefix.*)\.meta/", $file, $matches) ) {
  251. $meta_pathname = $cache_path . $file;
  252. $content_pathname = $cache_path . $matches[1] . ".html";
  253. $meta = unserialize(@file_get_contents($meta_pathname));
  254. if ($post_id > 0 && $meta) {
  255. if ($meta->blog_id == $blog_id && (!$meta->post || $meta->post == $post_id) ) {
  256. unlink($meta_pathname);
  257. unlink($content_pathname);
  258. }
  259. } elseif ($meta->blog_id == $blog_id) {
  260. unlink($meta_pathname);
  261. unlink($content_pathname);
  262. }
  263. }
  264. }
  265. closedir($handle);
  266. }
  267. wp_cache_writers_exit();
  268. return $post_id;
  269. }
  270. function wp_cache_microtime_diff($a, $b) {
  271. list($a_dec, $a_sec) = explode(' ', $a);
  272. list($b_dec, $b_sec) = explode(' ', $b);
  273. return $b_sec - $a_sec + $b_dec - $a_dec;
  274. }
  275. function wp_cache_post_id() {
  276. global $posts, $comment_post_ID, $post_ID;
  277. // We try hard all options. More frequent first.
  278. if ($post_ID > 0 ) return $post_ID;
  279. if ($comment_post_ID > 0 ) return $comment_post_ID;
  280. if (is_single() || is_page()) return $posts[0]->ID;
  281. if ($_GET['p'] > 0) return $_GET['p'];
  282. if ($_POST['p'] > 0) return $_POST['p'];
  283. return 0;
  284. }
  285. ?>