' . __( 'Warning', 'wp-super-cache' ) . ': ' . __( 'GZIP compression is enabled in WordPress, wp-cache will be bypassed until you disable gzip compression.', 'wp-super-cache' );
return false;
}
$lines = file( $wp_cache_config_file );
foreach ( $lines as $line ) {
if ( preg_match( '/^\s*\$cache_enabled\s*=\s*true\s*;/', $line ) ) {
return true;
}
}
return false;
}
function wp_cache_remove_index() {
global $cache_path;
if ( empty( $cache_path ) ) {
return;
}
@unlink( $cache_path . "index.html" );
@unlink( $cache_path . "supercache/index.html" );
@unlink( $cache_path . "blogs/index.html" );
if ( is_dir( $cache_path . "blogs" ) ) {
$dir = new DirectoryIterator( $cache_path . "blogs" );
foreach( $dir as $fileinfo ) {
if ( $fileinfo->isDot() ) {
continue;
}
if ( $fileinfo->isDir() ) {
$directory = $cache_path . "blogs/" . $fileinfo->getFilename();
if ( is_file( $directory . "/index.html" ) ) {
unlink( $directory . "/index.html" );
}
if ( is_dir( $directory . "/meta" ) ) {
if ( is_file( $directory . "/meta/index.html" ) ) {
unlink( $directory . "/meta/index.html" );
}
}
}
}
}
}
function wp_cache_logout_all() {
global $current_user;
if ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'wpsclogout' && wp_verify_nonce( $_GET[ '_wpnonce' ], 'wpsc_logout' ) ) {
$user_id = $current_user->ID;
WP_Session_Tokens::destroy_all_for_all_users();
wp_set_auth_cookie( $user_id, false, is_ssl() );
update_site_option( 'wp_super_cache_index_detected', 2 );
wp_redirect( admin_url() );
}
}
if ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == 'wpsclogout' )
add_action( 'admin_init', 'wp_cache_logout_all' );
function wp_cache_add_index_protection() {
global $cache_path, $blog_cache_dir;
if ( is_dir( $cache_path ) && false == is_file( "$cache_path/index.html" ) ) {
$page = wp_remote_get( home_url( "/wp-content/cache/" ) );
if ( false == is_wp_error( $page ) ) {
if ( false == get_site_option( 'wp_super_cache_index_detected' )
&& $page[ 'response' ][ 'code' ] == 200
&& stripos( $page[ 'body' ], 'index of' ) ) {
add_site_option( 'wp_super_cache_index_detected', 1 ); // only show this once
}
}
if ( ! function_exists( 'insert_with_markers' ) ) {
include_once( ABSPATH . 'wp-admin/includes/misc.php' );
}
insert_with_markers( $cache_path . '.htaccess', "INDEX", array( 'Options -Indexes' ) );
}
$directories = array( $cache_path, $cache_path . '/supercache/', $cache_path . '/blogs/', $blog_cache_dir, $blog_cache_dir . "/meta" );
foreach( $directories as $dir ) {
if ( false == is_dir( $dir ) )
@mkdir( $dir );
if ( is_dir( $dir ) && false == is_file( "$dir/index.html" ) ) {
$fp = @fopen( "$dir/index.html", 'w' );
if ( $fp )
fclose( $fp );
}
}
}
function wp_cache_add_site_cache_index() {
global $cache_path;
wp_cache_add_index_protection(); // root and supercache
if ( is_dir( $cache_path . "blogs" ) ) {
$dir = new DirectoryIterator( $cache_path . "blogs" );
foreach( $dir as $fileinfo ) {
if ( $fileinfo->isDot() ) {
continue;
}
if ( $fileinfo->isDir() ) {
$directory = $cache_path . "blogs/" . $fileinfo->getFilename();
if ( false == is_file( $directory . "/index.html" ) ) {
$fp = @fopen( $directory . "/index.html", 'w' );
if ( $fp )
fclose( $fp );
}
if ( is_dir( $directory . "/meta" ) ) {
if ( false == is_file( $directory . "/meta/index.html" ) ) {
$fp = @fopen( $directory . "/meta/index.html", 'w' );
if ( $fp )
fclose( $fp );
}
}
}
}
}
}
function wp_cache_verify_cache_dir() {
global $cache_path, $blog_cache_dir;
$dir = dirname($cache_path);
if ( !file_exists($cache_path) ) {
if ( !is_writeable_ACLSafe( $dir ) || !($dir = mkdir( $cache_path ) ) ) {
echo "" . __( 'Error', 'wp-super-cache' ) . ": " . sprintf( __( 'Your cache directory (%1$s) did not exist and couldn’t be created by the web server. Check %1$s permissions.', 'wp-super-cache' ), $dir );
return false;
}
}
if ( !is_writeable_ACLSafe($cache_path)) {
echo "" . __( 'Error', 'wp-super-cache' ) . ": " . sprintf( __( 'Your cache directory (%1$s) or %2$s need to be writable for this plugin to work. Double-check it.', 'wp-super-cache' ), $cache_path, $dir );
return false;
}
if ( '/' != substr($cache_path, -1)) {
$cache_path .= '/';
}
if( false == is_dir( $blog_cache_dir ) ) {
@mkdir( $cache_path . "blogs" );
if( $blog_cache_dir != $cache_path . "blogs/" )
@mkdir( $blog_cache_dir );
}
if( false == is_dir( $blog_cache_dir . 'meta' ) )
@mkdir( $blog_cache_dir . 'meta' );
wp_cache_add_index_protection();
return true;
}
function wp_cache_verify_config_file() {
global $wp_cache_config_file, $wp_cache_config_file_sample, $sem_id, $cache_path;
global $WPSC_HTTP_HOST;
$new = false;
$dir = dirname($wp_cache_config_file);
if ( file_exists($wp_cache_config_file) ) {
$lines = implode( ' ', file( $wp_cache_config_file ) );
if ( ! str_contains( $lines, 'WPCACHEHOME' ) ) {
if( is_writeable_ACLSafe( $wp_cache_config_file ) ) {
@unlink( $wp_cache_config_file );
} else {
echo "" . __( 'Error', 'wp-super-cache' ) . ": " . sprintf( __( 'Your WP-Cache config file (%s) is out of date and not writable by the Web server. Please delete it and refresh this page.', 'wp-super-cache' ), $wp_cache_config_file );
return false;
}
}
} elseif( !is_writeable_ACLSafe($dir)) {
echo "" . __( 'Error', 'wp-super-cache' ) . ": " . sprintf( __( 'Configuration file missing and %1$s directory (%2$s) is not writable by the web server. Check its permissions.', 'wp-super-cache' ), WP_CONTENT_DIR, $dir );
return false;
}
if ( !file_exists($wp_cache_config_file) ) {
if ( !file_exists($wp_cache_config_file_sample) ) {
echo "" . __( 'Error', 'wp-super-cache' ) . ": " . sprintf( __( 'Sample WP-Cache config file (%s) does not exist. Verify your installation.', 'wp-super-cache' ), $wp_cache_config_file_sample );
return false;
}
copy($wp_cache_config_file_sample, $wp_cache_config_file);
$dir = str_replace( str_replace( '\\', '/', WP_CONTENT_DIR ), '', str_replace( '\\', '/', dirname( __DIR__ ) ) );
if ( is_file( dirname( __DIR__ ) . '/wp-cache-config-sample.php' ) ) {
wp_cache_replace_line('define\(\ \'WPCACHEHOME', "\tdefine( 'WPCACHEHOME', WP_CONTENT_DIR . \"{$dir}/\" );", $wp_cache_config_file);
} elseif ( is_file( dirname( __DIR__ ) . '/wp-super-cache/wp-cache-config-sample.php' ) ) {
wp_cache_replace_line('define\(\ \'WPCACHEHOME', "\tdefine( 'WPCACHEHOME', WP_CONTENT_DIR . \"{$dir}/wp-super-cache/\" );", $wp_cache_config_file);
}
$new = true;
}
if ( $sem_id == 5419 && $cache_path != '' && $WPSC_HTTP_HOST != '' ) {
$sem_id = crc32( $WPSC_HTTP_HOST . $cache_path ) & 0x7fffffff;
wp_cache_replace_line('sem_id', '$sem_id = ' . $sem_id . ';', $wp_cache_config_file);
}
if ( $new ) {
require($wp_cache_config_file);
wpsc_set_default_gc( true );
}
return true;
}
function wp_cache_create_advanced_cache() {
global $wpsc_advanced_cache_filename, $wpsc_advanced_cache_dist_filename;
if ( file_exists( ABSPATH . 'wp-config.php') ) {
$global_config_file = ABSPATH . 'wp-config.php';
} elseif ( file_exists( dirname( ABSPATH ) . '/wp-config.php' ) ) {
$global_config_file = dirname( ABSPATH ) . '/wp-config.php';
} elseif ( defined( 'DEBIAN_FILE' ) && file_exists( DEBIAN_FILE ) ) {
$global_config_file = DEBIAN_FILE;
} else {
die('Cannot locate wp-config.php');
}
$line = 'define( \'WPCACHEHOME\', \'' . dirname( __DIR__ ) . '/\' );';
if ( ! apply_filters( 'wpsc_enable_wp_config_edit', true ) ) {
echo '
' . __( 'Warning', 'wp-super-cache' ) . "! " . sprintf( __( 'Not allowed to edit %s per configuration.', 'wp-super-cache' ), $global_config_file ) . "
";
return false;
}
if (
! strpos( file_get_contents( $global_config_file ), "WPCACHEHOME" ) ||
(
defined( 'WPCACHEHOME' ) &&
(
constant( 'WPCACHEHOME' ) == '' ||
(
constant( 'WPCACHEHOME' ) != '' &&
! file_exists( constant( 'WPCACHEHOME' ) . '/wp-cache.php' )
)
)
)
) {
if (
! is_writeable_ACLSafe( $global_config_file ) ||
! wp_cache_replace_line( 'define *\( *\'WPCACHEHOME\'', $line, $global_config_file )
) {
echo '' . __( 'Warning', 'wp-super-cache' ) . "! " . sprintf( __( 'Could not update %s! WPCACHEHOME must be set in config file.', 'wp-super-cache' ), $global_config_file ) . "
";
return false;
}
}
$ret = true;
if ( file_exists( $wpsc_advanced_cache_filename ) ) {
$file = file_get_contents( $wpsc_advanced_cache_filename );
if (
! strpos( $file, "WP SUPER CACHE 0.8.9.1" ) &&
! strpos( $file, "WP SUPER CACHE 1.2" )
) {
return false;
}
}
$file = file_get_contents( $wpsc_advanced_cache_dist_filename );
$fp = @fopen( $wpsc_advanced_cache_filename, 'w' );
if( $fp ) {
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite
fwrite( $fp, $file );
fclose( $fp );
do_action( 'wpsc_created_advanced_cache' );
} else {
$ret = false;
}
return $ret;
}
/**
* Identify the advanced cache plugin used
*
* @return string The name of the advanced cache plugin, BOOST, WPSC or OTHER.
*/
function wpsc_identify_advanced_cache() {
global $wpsc_advanced_cache_filename;
if ( ! file_exists( $wpsc_advanced_cache_filename ) ) {
return 'NONE';
}
$contents = file_get_contents( $wpsc_advanced_cache_filename ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
if ( false !== str_contains( $contents, 'Boost Cache Plugin' ) ) {
return 'BOOST';
}
if ( str_contains( $contents, 'WP SUPER CACHE 0.8.9.1' ) || str_contains( $contents, 'WP SUPER CACHE 1.2' ) ) {
return 'WPSC';
}
return 'OTHER';
}
function wpsc_check_advanced_cache() {
global $wpsc_advanced_cache_filename;
$ret = false;
$other_advanced_cache = false;
if ( file_exists( $wpsc_advanced_cache_filename ) ) {
$cache_type = wpsc_identify_advanced_cache();
switch ( $cache_type ) {
case 'WPSC':
return true;
case 'BOOST':
$other_advanced_cache = 'BOOST';
break;
default:
$other_advanced_cache = true;
break;
}
} else {
$ret = wp_cache_create_advanced_cache();
}
if ( false == $ret ) {
if ( $other_advanced_cache === 'BOOST' ) {
wpsc_deactivate_boost_cache_notice();
} elseif ( $other_advanced_cache ) {
echo '' . __( 'Warning! You may not be allowed to use this plugin on your site.', 'wp-super-cache' ) . "
";
echo '
' .
sprintf(
__( 'The file %s was created by another plugin or by your system administrator. Please examine the file carefully by FTP or SSH and consult your hosting documentation. ', 'wp-super-cache' ),
$wpsc_advanced_cache_filename
) .
'
';
echo '
' .
__( 'If it was created by another caching plugin please uninstall that plugin first before activating WP Super Cache. If the file is not removed by that action you should delete the file manually.', 'wp-super-cache' ),
'
';
echo '
' .
__( 'If you need support for this problem contact your hosting provider.', 'wp-super-cache' ),
'
';
echo '
';
} elseif ( ! is_writeable_ACLSafe( $wpsc_advanced_cache_filename ) ) {
echo '' . __( 'Warning', 'wp-super-cache' ) . "! " . sprintf( __( '%s/advanced-cache.php cannot be updated.', 'wp-super-cache' ), WP_CONTENT_DIR ) . "
";
echo '
';
echo "- " .
sprintf(
__( 'Make %1$s writable using the chmod command through your ftp or server software. (chmod 777 %1$s) and refresh this page. This is only a temporary measure and you’ll have to make it read only afterwards again. (Change 777 to 755 in the previous command)', 'wp-super-cache' ),
WP_CONTENT_DIR
) .
"
";
echo "- " . sprintf( __( 'Refresh this page to update %s/advanced-cache.php', 'wp-super-cache' ), WP_CONTENT_DIR ) . "
";
echo sprintf( __( 'If that doesn’t work, make sure the file
%s/advanced-cache.php doesn’t exist:', 'wp-super-cache' ), WP_CONTENT_DIR ) . "
";
echo "
";
echo '
';
}
return false;
}
return true;
}
function wp_cache_check_global_config() {
global $wp_cache_check_wp_config;
if ( !isset( $wp_cache_check_wp_config ) )
return true;
if ( file_exists( ABSPATH . 'wp-config.php') ) {
$global_config_file = ABSPATH . 'wp-config.php';
} else {
$global_config_file = dirname( ABSPATH ) . '/wp-config.php';
}
if ( preg_match( '#^\s*(define\s*\(\s*[\'"]WP_CACHE[\'"]|const\s+WP_CACHE\s*=)#m', file_get_contents( $global_config_file ) ) === 1 ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
if ( defined( 'WP_CACHE' ) && ! constant( 'WP_CACHE' ) ) {
?>
define('WP_CACHE', true);
' . __( "WP_CACHE constant set to false
The WP_CACHE constant is used by WordPress to load the code that serves cached pages. Unfortunately, it is set to false. Please edit your wp-config.php and add or edit the following line above the final require_once command:
define('WP_CACHE', true);
", 'wp-super-cache' ) . "";
} else {
echo '' . __( "Error: WP_CACHE is not enabled in your wp-config.php file and I couldn’t modify it.", 'wp-super-cache' ) . "
";
echo "
" . sprintf( __( "Edit %s and add the following line:
define('WP_CACHE', true);
Otherwise, WP-Cache will not be executed by WordPress core. ", 'wp-super-cache' ), $global_config_file ) . "
";
}
return false;
} else {
echo "" . __( '
WP_CACHE constant added to wp-config.php
If you continue to see this warning message please see point 5 of the Troubleshooting Guide. The WP_CACHE line must be moved up.', 'wp-super-cache' ) . "
";
}
return true;
}
function wp_cache_disable_plugin( $delete_config_file = true ) {
global $wp_rewrite;
if ( file_exists( ABSPATH . 'wp-config.php') ) {
$global_config_file = ABSPATH . 'wp-config.php';
} else {
$global_config_file = dirname(ABSPATH) . '/wp-config.php';
}
if ( apply_filters( 'wpsc_enable_wp_config_edit', true ) ) {
$line = 'define(\'WP_CACHE\', true);';
if (
strpos( file_get_contents( $global_config_file ), $line ) &&
(
! is_writeable_ACLSafe( $global_config_file ) ||
! wp_cache_replace_line( 'define*\(*\'WP_CACHE\'', '', $global_config_file )
)
) {
wp_die( "Could not remove WP_CACHE define from $global_config_file. Please edit that file and remove the line containing the text 'WP_CACHE'. Then refresh this page." );
}
$line = 'define( \'WPCACHEHOME\',';
if (
strpos( file_get_contents( $global_config_file ), $line ) &&
(
! is_writeable_ACLSafe( $global_config_file ) ||
! wp_cache_replace_line( 'define *\( *\'WPCACHEHOME\'', '', $global_config_file )
)
) {
wp_die( "Could not remove WPCACHEHOME define from $global_config_file. Please edit that file and remove the line containing the text 'WPCACHEHOME'. Then refresh this page." );
}
} elseif ( function_exists( 'wp_cache_debug' ) ) {
wp_cache_debug( 'wp_cache_disable_plugin: not allowed to edit wp-config.php per configuration.' );
}
uninstall_supercache( WP_CONTENT_DIR . '/cache' );
$file_not_deleted = array();
wpsc_remove_advanced_cache();
if ( @file_exists( WP_CONTENT_DIR . "/advanced-cache.php" ) ) {
$file_not_deleted[] = WP_CONTENT_DIR . '/advanced-cache.php';
}
if ( $delete_config_file && @file_exists( WPCACHECONFIGPATH . "/wp-cache-config.php" ) ) {
if ( false == unlink( WPCACHECONFIGPATH . "/wp-cache-config.php" ) )
$file_not_deleted[] = WPCACHECONFIGPATH . '/wp-cache-config.php';
}
if ( ! empty( $file_not_deleted ) ) {
$msg = __( "Dear User,\n\nWP Super Cache was removed from your blog or deactivated but some files could\nnot be deleted.\n\n", 'wp-super-cache' );
foreach ( $file_not_deleted as $path ) {
$msg .= "{$path}\n";
}
$msg .= "\n";
$msg .= sprintf( __( "You should delete these files manually.\nYou may need to change the permissions of the files or parent directory.\nYou can read more about this in the Codex at\n%s\n\nThank you.", 'wp-super-cache' ), 'https://codex.wordpress.org/Changing_File_Permissions#About_Chmod' );
if ( apply_filters( 'wpsc_send_uninstall_errors', 1 ) ) {
wp_mail( get_option( 'admin_email' ), __( 'WP Super Cache: could not delete files', 'wp-super-cache' ), $msg );
}
}
extract( wpsc_get_htaccess_info() ); // $document_root, $apache_root, $home_path, $home_root, $home_root_lc, $inst_root, $wprules, $scrules, $condition_rules, $rules, $gziprules
// @phan-suppress-next-line PhanTypeSuspiciousStringExpression -- $home_path is set via extract()
if ( $scrules != '' && insert_with_markers( $home_path.'.htaccess', 'WPSuperCache', array() ) ) {
$wp_rewrite->flush_rules();
} elseif( $scrules != '' ) {
wp_mail( get_option( 'admin_email' ), __( 'Supercache Uninstall Problems', 'wp-super-cache' ), sprintf( __( "Dear User,\n\nWP Super Cache was removed from your blog but the mod_rewrite rules\nin your .htaccess were not.\n\nPlease edit the following file and remove the code\nbetween 'BEGIN WPSuperCache' and 'END WPSuperCache'. Please backup the file first!\n\n%s\n\nRegards,\nWP Super Cache Plugin\nhttps://wordpress.org/plugins/wp-super-cache/", 'wp-super-cache' ), ABSPATH . '/.htaccess' ) );
}
}
function uninstall_supercache( $folderPath ) { // from http://www.php.net/manual/en/function.rmdir.php
if ( trailingslashit( constant( 'ABSPATH' ) ) == trailingslashit( $folderPath ) )
return false;
if ( @is_dir ( $folderPath ) ) {
$dh = @opendir($folderPath);
while( false !== ( $value = @readdir( $dh ) ) ) {
if ( $value != "." && $value != ".." ) {
$value = $folderPath . "/" . $value;
if ( @is_dir ( $value ) ) {
uninstall_supercache( $value );
} else {
@unlink( $value );
}
}
}
return @rmdir( $folderPath );
} else {
return false;
}
}
function wpsc_set_default_gc( $force = false ) {
global $cache_path, $wp_cache_shutdown_gc, $cache_schedule_type;
if ( isset( $wp_cache_shutdown_gc ) && $wp_cache_shutdown_gc == 1 ) {
return false;
}
if ( $force ) {
unset( $cache_schedule_type );
$timestamp = wp_next_scheduled( 'wp_cache_gc' );
if ( $timestamp ) {
wp_unschedule_event( $timestamp, 'wp_cache_gc' );
}
}
// set up garbage collection with some default settings
if ( false == isset( $cache_schedule_type ) && false == wp_next_scheduled( 'wp_cache_gc' ) ) {
$cache_schedule_type = 'interval';
$cache_time_interval = 600;
$cache_max_time = 1800;
$cache_schedule_interval = 'hourly';
$cache_gc_email_me = 0;
wp_cache_setting( 'cache_schedule_type', $cache_schedule_type );
wp_cache_setting( 'cache_time_interval', $cache_time_interval );
wp_cache_setting( 'cache_max_time', $cache_max_time );
wp_cache_setting( 'cache_schedule_interval', $cache_schedule_interval );
wp_cache_setting( 'cache_gc_email_me', $cache_gc_email_me );
wp_schedule_single_event( time() + 600, 'wp_cache_gc' );
}
return true;
}
function wpsc_update_check() {
global $wpsc_version;
if (
! isset( $wpsc_version ) ||
$wpsc_version != 169
) {
wp_cache_setting( 'wpsc_version', 169 );
global $wp_cache_debug_log, $cache_path;
$log_file = $cache_path . str_replace('/', '', str_replace('..', '', $wp_cache_debug_log));
if ( ! file_exists( $log_file ) ) {
return false;
}
@unlink( $log_file );
wp_cache_debug( 'wpsc_update_check: Deleted old log file on plugin update.' );
}
}
add_action( 'admin_init', 'wpsc_update_check' );