Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve wp-cache config functions #487

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions wp-cache-config-sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
See wp-cache.php for author details.
*/

if ( ! defined('WPCACHEHOME') )
if ( ! defined( 'WPCACHEHOME' ) ) {
define( 'WPCACHEHOME', WP_PLUGIN_DIR . '/wp-super-cache/' );
}

$cache_compression = 0; // Super cache compression
$cache_enabled = false;
$super_cache_enabled = true;
$cache_max_time = 3600; //in seconds
$cache_max_time = 3600; // in seconds
//$use_flock = true; // Set it true or false if you know what to use
$cache_path = WP_CONTENT_DIR . '/cache/';
$file_prefix = 'wp-cache-';
Expand All @@ -20,8 +21,8 @@
// Array of files that have 'wp-' but should still be cached
$cache_acceptable_files = array( 'wp-comments-popup.php', 'wp-links-opml.php', 'wp-locations.php' );

$cache_rejected_uri = array('wp-.*\\.php', 'index\\.php');
$cache_rejected_user_agent = array ( 0 => 'bot', 1 => 'ia_archive', 2 => 'slurp', 3 => 'crawl', 4 => 'spider', 5 => 'Yandex' );
$cache_rejected_uri = array( 'wp-.*\\.php', 'index\\.php' );
$cache_rejected_user_agent = array( 'bot', 'ia_archive', 'slurp', 'crawl', 'spider', 'Yandex' );

$cache_rebuild_files = 1;

Expand All @@ -33,7 +34,7 @@
// Just modify it if you have conflicts with semaphores
$sem_id = 5419;

if ( '/' != substr($cache_path, -1)) {
if ( '/' != substr( $cache_path, -1 ) ) {
$cache_path .= '/';
}

Expand Down Expand Up @@ -63,16 +64,16 @@
$wp_cache_debug_ip = '';
$wp_cache_debug_log = '';
$wp_cache_debug_email = '';
$wp_cache_pages[ "search" ] = 0;
$wp_cache_pages[ "feed" ] = 0;
$wp_cache_pages[ "category" ] = 0;
$wp_cache_pages[ "home" ] = 0;
$wp_cache_pages[ "frontpage" ] = 0;
$wp_cache_pages[ "tag" ] = 0;
$wp_cache_pages[ "archives" ] = 0;
$wp_cache_pages[ "pages" ] = 0;
$wp_cache_pages[ "single" ] = 0;
$wp_cache_pages[ "author" ] = 0;
$wp_cache_pages['search'] = 0;
$wp_cache_pages['feed'] = 0;
$wp_cache_pages['category'] = 0;
$wp_cache_pages['home'] = 0;
$wp_cache_pages['frontpage'] = 0;
$wp_cache_pages['tag'] = 0;
$wp_cache_pages['archives'] = 0;
$wp_cache_pages['pages'] = 0;
$wp_cache_pages['single'] = 0;
$wp_cache_pages['author'] = 0;
$wp_cache_hide_donation = 0;
$wp_cache_not_logged_in = 0;
$wp_cache_clear_on_post_edit = 0;
Expand Down Expand Up @@ -102,4 +103,3 @@
$wpsc_save_headers = 0;
$cache_schedule_interval = 'daily';
$wp_super_cache_comments = 1;
?>
58 changes: 42 additions & 16 deletions wp-cache-phase2.php
Original file line number Diff line number Diff line change
Expand Up @@ -1019,18 +1019,28 @@ function is_writeable_ACLSafe( $path ) {
return true;
}

function wpsc_var_export( $var ) {

if ( is_string( $var ) ) {
return "'" . addslashes($var) . "'";
}
elseif ( is_numeric( $var ) ) {
return strval( $var );
}
elseif ( is_array( $var ) ) {
$str = implode( ', ', array_map( 'wpsc_var_export', $var ) );
return empty( $str ) ? 'array()' : 'array( ' . $str . ' )';
}

return str_replace( PHP_EOL, ' ', var_export( $var, true ) );
}

function wp_cache_setting( $field, $value ) {
global $wp_cache_config_file;

$GLOBALS[ $field ] = $value;
if ( is_numeric( $value ) ) {
wp_cache_replace_line( '^ *\$' . $field, "\$$field = $value;", $wp_cache_config_file );
} elseif ( is_object( $value ) || is_array( $value ) ) {
$text = var_export( $value, true );
$text = preg_replace( '/[\s]+/', ' ', $text );
wp_cache_replace_line( '^ *\$' . $field, "\$$field = $text;", $wp_cache_config_file );
} else {
wp_cache_replace_line( '^ *\$' . $field, "\$$field = '$value';", $wp_cache_config_file );

if ( $value !== $GLOBALS[ $field ] ) {
$new_value = '$' . $field . ' = ' . wpsc_var_export( $value ) . ';';
wp_cache_replace_line( '^\s*\$' . $field . '\s*=', $new_value , $GLOBALS['wp_cache_config_file'] );
$GLOBALS[ $field ] = $value;
}
}

Expand Down Expand Up @@ -1081,15 +1091,31 @@ function wp_cache_replace_line( $old, $new, $my_file ) {
}
}
} else {
$done = false;
$reg_exps = array( '/^\s*define\s*\(/', '/^\s*\$/' );
foreach( $reg_exps as $reg_exp ) {
if ( preg_match( $reg_exp, $new ) ) {
break;
}
}

$done = false;
$state = false;
foreach( (array) $lines as $line ) {
if ( $done || ! preg_match( '/^(if\ \(\ \!\ )?define|\$|\?>/', $line ) ) {
fputs($fd, $line);
} else {

if ( false !== strpos( $line, '{' ) ) {
$state = true;
}

// Insert new line before first variable/constant.
if ( ! $done && ! $state && preg_match( $reg_exp, $line ) ) {
fputs($fd, "$new\n");
fputs($fd, $line);
$done = true;
}
fputs($fd, $line);

if ( false !== strpos( $line, '}' ) ) {
$state = false;
}
}
}
fclose( $fd );
Expand Down
81 changes: 44 additions & 37 deletions wp-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,7 @@ function wp_update_lock_down() {

if ( isset( $_POST[ 'wp_lock_down' ] ) && $valid_nonce ) {
$wp_lock_down = $_POST[ 'wp_lock_down' ] == '1' ? '1' : '0';
wp_cache_replace_line( '^.*WPLOCKDOWN', "if ( ! defined( 'WPLOCKDOWN' ) ) define( 'WPLOCKDOWN', '$wp_lock_down' );", $wp_cache_config_file );
wp_cache_replace_line( '^.*WPLOCKDOWN', "defined( 'WPLOCKDOWN' ) && define( 'WPLOCKDOWN', '$wp_lock_down' );", $wp_cache_config_file );
if ( false == defined( 'WPLOCKDOWN' ) )
define( 'WPLOCKDOWN', $wp_lock_down );
if ( $wp_lock_down == '0' && function_exists( 'prune_super_cache' ) )
Expand Down Expand Up @@ -1707,7 +1707,7 @@ function wp_cache_time_update() {
}

function wp_cache_edit_max_time() {
global $cache_max_time, $wp_cache_config_file, $valid_nonce, $super_cache_enabled, $cache_schedule_type, $cache_scheduled_time, $cache_schedule_interval, $cache_time_interval, $cache_gc_email_me, $wp_cache_preload_on;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$valid_nonce needs to be a global variable in the functions that update settings because the REST API in rest/class.wp-super-cache-rest-update-settings.php sets it to true so it can use these update functions.

If we want to verify the nonce in each update function the REST API update code has to be modified to generate a valid POST nonce too. Probably best to keep that for a different PR?

global $cache_max_time, $super_cache_enabled, $cache_schedule_type, $cache_scheduled_time, $cache_schedule_interval, $cache_time_interval, $cache_gc_email_me, $wp_cache_preload_on;

$timezone_format = _x('Y-m-d G:i:s', 'timezone date format');

Expand Down Expand Up @@ -1787,6 +1787,11 @@ function wp_cache_edit_max_time() {
}

function wp_cache_sanitize_value($text, & $array) {

if ( function_exists( '_deprecated_function' ) ) {
_deprecated_function( __FUNCTION__, 'WP Super Cache 1.5.9' );
}

$text = esc_html(strip_tags($text));
$array = preg_split("/[\s,]+/", chop($text));
$text = var_export($array, true);
Expand All @@ -1810,23 +1815,19 @@ function apache_request_headers() {
}

function wp_cache_update_rejected_ua() {
global $cache_rejected_user_agent, $wp_cache_config_file, $valid_nonce;

if ( !function_exists( 'apache_request_headers' ) ) return;

if ( isset( $_POST[ 'wp_rejected_user_agent' ] ) && $valid_nonce ) {
$_POST[ 'wp_rejected_user_agent' ] = str_replace( ' ', '___', $_POST[ 'wp_rejected_user_agent' ] );
$text = str_replace( '___', ' ', wp_cache_sanitize_value( $_POST[ 'wp_rejected_user_agent' ], $cache_rejected_user_agent ) );
wp_cache_replace_line( '^ *\$cache_rejected_user_agent', "\$cache_rejected_user_agent = $text;", $wp_cache_config_file );
foreach( $cache_rejected_user_agent as $k => $ua ) {
$cache_rejected_user_agent[ $k ] = str_replace( '___', ' ', $ua );
}
reset( $cache_rejected_user_agent );
if ( isset( $_POST['wp_rejected_user_agent'], $_POST['_wpnonce'] )
&& wp_verify_nonce( $_POST['_wpnonce'], 'wp-cache' )
) {
$array = preg_split( "/[\r\n,]+/", stripslashes( $_POST['wp_rejected_user_agent'] ) );
$array = array_map( 'trim', array_filter( $array ) );
wp_cache_setting( 'cache_rejected_user_agent', $array );
}
}

function wp_cache_edit_rejected_ua() {
global $cache_rejected_user_agent, $wp_cache_config_file, $valid_nonce;

if ( !function_exists( 'apache_request_headers' ) ) return;

Expand All @@ -1836,8 +1837,8 @@ function wp_cache_edit_rejected_ua() {
echo "<p>" . __( 'Strings in the HTTP &#8217;User Agent&#8217; header that prevent WP-Cache from caching bot, spiders, and crawlers&#8217; requests. Note that super cached files are still sent to these agents if they already exists.', 'wp-super-cache' ) . "</p>\n";
echo '<form name="wp_edit_rejected_user_agent" action="#useragents" method="post">';
echo '<textarea name="wp_rejected_user_agent" cols="40" rows="4" style="width: 50%; font-size: 12px;" class="code">';
foreach( $cache_rejected_user_agent as $ua ) {
echo esc_html( $ua ) . "\n";
foreach( $GLOBALS['cache_rejected_user_agent'] as $ua ) {
echo esc_attr( $ua ) . "\n";
}
echo '</textarea> ';
echo '<div class="submit"><input class="button-primary" type="submit" ' . SUBMITDISABLED . 'value="' . __( 'Save UA Strings', 'wp-super-cache' ) . '" /></div>';
Expand All @@ -1847,24 +1848,29 @@ function wp_cache_edit_rejected_ua() {
}

function wp_cache_update_rejected_pages() {
global $wp_cache_config_file, $valid_nonce, $wp_cache_pages;

if ( isset( $_POST[ 'wp_edit_rejected_pages' ] ) && $valid_nonce ) {
if ( isset( $_POST['wp_edit_rejected_pages'], $_POST['_wpnonce'] )
&& wp_verify_nonce( $_POST['_wpnonce'], 'wp-cache' )
) {
$pages = array( 'single', 'pages', 'archives', 'tag', 'frontpage', 'home', 'category', 'feed', 'author', 'search' );
foreach( $pages as $page ) {
if ( isset( $_POST[ 'wp_cache_pages' ][ $page ] ) ) {
$value = 1;
} else {
$value = 0;

$value = empty( $_POST['wp_cache_pages'][ $page ] ) ? 0 : 1;
if ( $value === $GLOBALS['wp_cache_pages'][ $page ] ) {
continue;
}
wp_cache_replace_line('^ *\$wp_cache_pages\[ "' . $page . '" \]', "\$wp_cache_pages[ \"{$page}\" ] = $value;", $wp_cache_config_file);
$wp_cache_pages[ $page ] = $value;

$page_regexp = '\s*(' . preg_quote( "'" . $page . "'" ) . '|' . preg_quote( '"' . $page . '"' ) . ')\s*';
$new_value = '$wp_cache_pages[' . wpsc_var_export( $page ) . '] = ' . wpsc_var_export( $value ) . ';';
wp_cache_replace_line( '^\s*\$wp_cache_pages\[' . $page_regexp . '\]', $new_value, $GLOBALS['wp_cache_config_file'] );

$GLOBALS['wp_cache_pages'][ $page ] = $value;
}
}
}

function wp_cache_edit_rejected_pages() {
global $wp_cache_config_file, $valid_nonce, $wp_cache_pages;
global $wp_cache_pages;

wp_cache_update_rejected_pages();

Expand All @@ -1890,26 +1896,26 @@ function wp_cache_edit_rejected_pages() {
}

function wp_cache_update_rejected_strings() {
global $cache_rejected_uri, $wp_cache_config_file, $valid_nonce;

if ( isset($_REQUEST['wp_rejected_uri']) && $valid_nonce ) {
$text = wp_cache_sanitize_value( str_replace( '\\\\', '\\', $_REQUEST['wp_rejected_uri'] ), $cache_rejected_uri );
wp_cache_replace_line('^ *\$cache_rejected_uri', "\$cache_rejected_uri = $text;", $wp_cache_config_file);
if ( isset( $_POST['wp_rejected_uri'], $_POST['_wpnonce'] )
&& wp_verify_nonce( $_POST['_wpnonce'], 'wp-cache' )
) {
$array = array_filter( preg_split( '/[\s,]+/', stripslashes( $_POST['wp_rejected_uri'] ) ) );
wp_cache_setting( 'cache_rejected_uri', $array );
}

}

function wp_cache_edit_rejected() {
global $cache_rejected_uri;

wp_cache_update_rejected_strings();

echo '<a name="rejecturi"></a>';
echo '<form name="wp_edit_rejected" action="#rejecturi" method="post">';
echo "<p>" . __( 'Add here strings (not a filename) that forces a page not to be cached. For example, if your URLs include year and you dont want to cache last year posts, it&#8217;s enough to specify the year, i.e. &#8217;/2004/&#8217;. WP-Cache will search if that string is part of the URI and if so, it will not cache that page.', 'wp-super-cache' ) . "</p>\n";
echo '<textarea name="wp_rejected_uri" cols="40" rows="4" style="width: 50%; font-size: 12px;" class="code">';
foreach ($cache_rejected_uri as $file) {
echo esc_html( $file ) . "\n";
foreach ( $GLOBALS['cache_rejected_uri'] as $file) {
echo esc_attr( $file ) . "\n";
}
echo '</textarea> ';
echo '<div class="submit"><input class="button-primary" type="submit" ' . SUBMITDISABLED . 'value="' . __( 'Save Strings', 'wp-super-cache' ) . '" /></div>';
Expand All @@ -1918,11 +1924,12 @@ function wp_cache_edit_rejected() {
}

function wp_cache_update_accepted_strings() {
global $cache_acceptable_files, $wp_cache_config_file, $valid_nonce;

if ( isset( $_REQUEST[ 'wp_accepted_files' ] ) && $valid_nonce ) {
$text = wp_cache_sanitize_value( $_REQUEST[ 'wp_accepted_files' ], $cache_acceptable_files );
wp_cache_replace_line( '^ *\$cache_acceptable_files', "\$cache_acceptable_files = $text;", $wp_cache_config_file );
if ( isset( $_POST['wp_accepted_files'], $_POST['_wpnonce'] )
&& wp_verify_nonce( $_POST['_wpnonce'], 'wp-cache' )
) {
$array = array_filter( preg_split( '/[\s,]+/', stripslashes( $_POST['wp_accepted_files'] ) ) );
wp_cache_setting( 'cache_acceptable_files', $array );
}
}

Expand All @@ -1936,7 +1943,7 @@ function wp_cache_edit_accepted() {
echo "<p>" . __( 'Add here those filenames that can be cached, even if they match one of the rejected substring specified above.', 'wp-super-cache' ) . "</p>\n";
echo '<textarea name="wp_accepted_files" cols="40" rows="8" style="width: 50%; font-size: 12px;" class="code">';
foreach ($cache_acceptable_files as $file) {
echo esc_html($file) . "\n";
echo esc_attr($file) . "\n";
}
echo '</textarea> ';
echo '<div class="submit"><input class="button-primary" type="submit" ' . SUBMITDISABLED . 'value="' . __( 'Save Files', 'wp-super-cache' ) . '" /></div>';
Expand All @@ -1945,7 +1952,7 @@ function wp_cache_edit_accepted() {
}

function wpsc_update_debug_settings() {
global $wp_super_cache_debug, $wp_cache_debug_log, $wp_cache_debug_ip, $cache_path, $valid_nonce, $wp_cache_config_file, $wp_super_cache_comments;
global $wp_super_cache_debug, $wp_cache_debug_log, $wp_cache_debug_ip, $cache_path, $valid_nonce, $wp_super_cache_comments;
global $wp_super_cache_front_page_check, $wp_super_cache_front_page_clear, $wp_super_cache_front_page_text, $wp_super_cache_front_page_notification, $wp_super_cache_advanced_debug;
global $wp_cache_debug_username;

Expand Down Expand Up @@ -2018,7 +2025,7 @@ function wpsc_update_debug_settings() {
}

function wp_cache_debug_settings() {
global $wp_super_cache_debug, $wp_cache_debug_log, $wp_cache_debug_ip, $cache_path, $valid_nonce, $wp_cache_config_file, $wp_super_cache_comments;
global $wp_super_cache_debug, $wp_cache_debug_log, $wp_cache_debug_ip, $cache_path, $wp_super_cache_comments;
global $wp_super_cache_front_page_check, $wp_super_cache_front_page_clear, $wp_super_cache_front_page_text, $wp_super_cache_front_page_notification, $wp_super_cache_advanced_debug;
global $wp_cache_debug_username;

Expand Down