| Server IP : 185.252.147.100 / Your IP : 216.73.216.196 Web Server : nginx/1.27.3 System : Linux mitrofanov.ru 6.1.0-37-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.140-1 (2025-05-22) x86_64 User : mitr ( 1000) PHP Version : 8.2.29 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /www/html/tour.mitrofanov.ru/public_04_11/wp-content/plugins/ |
Upload File : |
<?php
/*
Plugin Name: nginx fastcgi Cache Purge
Plugin URI: http://mattgadient.com/flushing-the-nginx-fastcgi-cache-via-php-and-or-wordpress/
Description: WARNING: verify cache location in nginx-fastcgi-cache-purge.php BEFORE activating. Purges cache automatically after posting/editing, and manually via settings page.
Version: 0.0.2
Author: Matt Gadient
Author URI: http://mattgadient.com/
Copyright (C)2013 Matt Gadient
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
if (!defined('ABSPATH')) {
die();
}
//
// You MUST verify that the cache location below is correct before
// enabling the plugin, and change it if not.
//
// Also a good idea to ensure that permissions are correct.
//
// YOU HAVE BEEN WARNED!
//
$nginx_cache_location = "/var/cache/nginx/tour.mitrofanov.ru/";
///var/cache/nginx/tpktrade
//ini_set( 'error_reporting', E_ALL );
if ( ! function_exists( 'plugin_log' ) ) {
function plugin_log( $entry, $mode = 'a', $file = 'plugin' ) {
// Get WordPress uploads directory.
$upload_dir = wp_upload_dir();
$upload_dir = $upload_dir['basedir'];
// If the entry is array, json_encode.
if ( is_array( $entry ) ) {
$entry = json_encode( $entry );
}
// Write the log file.
$file = $upload_dir . '/' . $file . '.log';
$file = fopen( $file, $mode );
$bytes = fwrite( $file, current_time( 'mysql' ) . "::" . $entry . "\n" );
fclose( $file );
return $bytes;
}
}
//
// The actual filters that will be checked against to auto-purge the entire cache.
// If you do not want the plugin to auto-purge, comment them out.
//
add_filter ('switch_theme', 'deleteCache', 100);
add_filter ('publish_post', 'deleteCache', 100);
add_filter ('edit_post', 'deleteCache', 100);
add_filter ('delete_post', 'deleteCache', 100);
add_filter ('save_post', 'deleteCache', 100);
add_filter ('add_category', 'deleteCache', 100);
add_filter ('delete_category', 'deleteCache', 100);
//add_action( 'save_post', 'cs_twitter_save_meta', 100, 3 );
function cs_twitter_save_meta( $post_id, $post, $update ) {
global $nginx_cache_location;
// // Check the logged in user has permission to edit this post
// if ( ! current_user_can( 'manage_options' ) ) {
// return $post_id;
// }
// // Only want to set if this is a new post!
// if ( $update ){
// return;
// }
// // If this is just a revision, don't send the email.
if ( wp_is_post_revision( $post_id ) ) {
return;
}
// API request (not in gutenberg)
//if (REST_REQUEST) return;
// autosave
if (wp_is_post_autosave($post_id)) return;
// if ( get_post_type( $post_id ) !== 'event' ) return;
// // Only set for post_type = post!
// if ( 'post' !== $post->post_type ) {
// return;
// }
$url = parse_url( get_the_permalink($post_id) );
//plugin_log($url);
if ($url) {
$scheme = $url['scheme'];
$host = $url['host'];
$requesturi = $url['path'];
$hash = md5($scheme.'GET'.$host.$requesturi);
$file = $nginx_cache_location . substr($hash, -1) . '/' . substr($hash,-3,2) . '/' . $hash;
plugin_log($file);
if (file_exists($file)) {
//plugin_log(var_dump(unlink($file)));
$file_success = unlink($file);
plugin_log( $file_success ? "The file was deleted" : "The file was not deleted" );
//$file_success = unlink($file);
// if ($file_success) {
// plugin_log('Successfully removed');
// //echo ('<p style="color:green;font-size:14px;">Successfully removed '
// //. $_POST['purge_single']
// //. ' from the cache!</p>');
// } else {
// plugin_log('Unable to remove cached file!');
// //echo ('<p style="color:orange;font-size:14px;">Unable to remove cached file!<br />
// //It is possible that nginx cleared it just before you did, although if you always
// //get this message, chances are that something is not configured correctly.</p>');
// }
}
//plugin_log('$file_success - '.$file_success);
//$file_success = unlink($nginx_cache_location . substr($hash, -1) . '/' . substr($hash,-3,2) . '/' . $hash);
//plugin_log($nginx_cache_location . substr($hash, -1) . '/' . substr($hash,-3,2) . '/' . $hash);
// if ($file_success) {
// plugin_log('Successfully removed');
// //echo ('<p style="color:green;font-size:14px;">Successfully removed '
// //. $_POST['purge_single']
// //. ' from the cache!</p>');
// } else {
// plugin_log('Unable to remove cached file!');
// //echo ('<p style="color:orange;font-size:14px;">Unable to remove cached file!<br />
// //It is possible that nginx cleared it just before you did, although if you always
// //get this message, chances are that something is not configured correctly.</p>');
// }
}
}
//
// The function that deletes the entire cache when called.
//
function deleteCache() {
echo '';
global $nginx_cache_location;
if ((is_dir($nginx_cache_location)) && (is_writable($nginx_cache_location))) {
array_map('unlink', glob($nginx_cache_location . "*/*/*"));
array_map('rmdir', glob($nginx_cache_location . "*/*"));
array_map('rmdir', glob($nginx_cache_location . "*"));
} else {
echo ('<p style="color:red;font-size:14px;">Warning! ' . $nginx_cache_location
. ' was not found, or not accessible! Doing nothing.</p>');
}
}
//
// Creates the menu and adds the option page to it.
//
add_action('admin_menu', 'add_plugin_nginx_cache_menu');
function add_plugin_nginx_cache_menu() {
add_options_page('nginx cache settings', 'nginx Cache Purge', 'manage_options', 'nginx_cache_purge', 'nginx_cache_page');
}
//
// The actual option page. Contains most of the logic (a bit messy).
//
function nginx_cache_page() {
global $wpdb;
global $nginx_cache_location;
// If manually purging all is selected, this happens.
if (isset($_POST['purge'])) {
echo ('<p style="color:green;font-size:14px;">Attempted to purge all cached files from ' . $nginx_cache_location
. '...<br />check file counts below to see if it was successful (should show 0).</p>');
deleteCache();
}
// If manually purging a single post/page, this happens.
if (isset($_POST['purge_single']))
{
$url = parse_url($_POST['purge_single']);
if (!$url) {
echo '<p style="color:red;font-size:14px;">There was a problem parsing the URL, so we are
<b>not</b> attempting to clear it. Possible misconfiguration.</p>';
} else {
$scheme = $url['scheme'];
$host = $url['host'];
$requesturi = $url['path'];
$hash = md5($scheme.'GET'.$host.$requesturi);
$file_success = unlink($nginx_cache_location . substr($hash, -1) . '/' . substr($hash,-3,2) . '/' . $hash);
if ($file_success) {
echo ('<p style="color:green;font-size:14px;">Successfully removed '
. $_POST['purge_single']
. ' from the cache!</p>');
} else {
echo ('<p style="color:orange;font-size:14px;">Unable to remove cached file!<br />
It is possible that nginx cleared it just before you did, although if you always
get this message, chances are that something is not configured correctly.</p>');
}
}
}
//
// This collects a list of page/post url's from the wordpress database, and
// checks for the existance of each in the cache. We end up with:
// -a total number of pages/posts that are cached.
// -a string we can print later listing them (with a purge button on each)
//
$url_list = "";
$counted_urls = 0;
$postID = $wpdb->get_col("
SELECT ID FROM $wpdb->posts
WHERE ((post_type = 'post') OR (post_type = 'page'))
AND (post_status = 'publish')
");
echo '<ul id="post_links">';
foreach($postID as $post_link) {
$url = parse_url(get_permalink($post_link));
if(!$url) {
echo '<p style="color:red;font-size:14px;"><b>WARNING: Unable to determine the URLs from
the wordpress database. Recommend you do NOT continue.</b></p>';
} else {
$scheme = $url['scheme'];
$host = $url['host'];
$requesturi = $url['path'];
$hash = md5($scheme.'GET'.$host.$requesturi);
if (file_exists($nginx_cache_location . substr($hash, -1) . '/' . substr($hash,-3,2) . '/' . $hash)) {
$counted_urls++;
$url_list .= ('<li><button type="submit" name="purge_single" value="'
. get_permalink($post_link)
. '" ><i>(purge from cache)</i></button> <b>'
. get_the_title($post_link) . '</b> ' . get_permalink($post_link)
. '</li>');
}
}
}
//
// Finally, the main option page output.
//
?>
<p>The path currently hardwired into the plugin is <b><?php echo $nginx_cache_location ?></b>
and it also assumes you have used <b>levels=1:2</b> <i>(suggest you verify both before using this plugin).</i></p>
<p>A quick check was done to see if <b><?php echo $nginx_cache_location; ?></b> existed and was writable, and...
<?php if ((is_dir($nginx_cache_location)) && (is_writable($nginx_cache_location))) {
echo '<span style="color:green;">It appears to be writable!</span>';
} else {
echo '<span style="color:red;">It does not appear to be writable!!! You should deactivate the plugin immediately'
. ' and verify that the value hardcoded in the plugin matches the setting in your nginx config!</span>';
}
?>
</p>
<p> </p>
<p>------------------------------</p>
<p> </p>
<p>Saving/editing a post, adding/deleting a category, approving comments, and changing the theme
should all automatically clear the entire cache unless the check above failed.</p>
<p> </p>
<p>------------------------------</p>
<p> </p>
<p style="font-size:14px;">Currently there appear to be <b><?php echo sizeof(glob($nginx_cache_location . "*/*/*")); ?></b> items cached by nginx in total.<br />
<b><?php echo $counted_urls; ?></b> were matched to your posts/pages, and are listed below.</p>
<form method="post" action="">
<?php wp_nonce_field(); ?>
<p class="submit">
<input class="button" type="submit" name="purge" value="Purge all items from cache" />
</p>
<p>Clicking the button above will attempt to purge the cache of all files. You can selectively
target files below if you would prefer.</p>
<?php
echo $url_list;
}
?>