Skip to content

Commit

Permalink
Completing work on ignored GET vars. See: wpsharks/comet-cache#639
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswsinc committed Nov 10, 2016
1 parent 89cf91d commit 08fd140
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 172 deletions.
14 changes: 7 additions & 7 deletions src/includes/classes/AbsBase.php
Expand Up @@ -11,35 +11,35 @@
abstract class AbsBase implements Interfaces\Shared\NcDebugConsts, Interfaces\Shared\CachePathConsts
{
/**
* @type null|plugin Plugin reference.
* @var null|plugin Plugin reference.
*
* @since 150422 Rewrite.
*/
protected $plugin;

/**
* @type array Instance cache.
* @var array Instance cache.
*
* @since 150422 Rewrite.
*/
protected $cache = [];

/**
* @type array Global static cache ref.
* @var array Global static cache ref.
*
* @since 150422 Rewrite.
*/
protected $static = [];

/**
* @type array Global static cache.
* @var array Global static cache.
*
* @since 150422 Rewrite.
*/
protected static $global_static = [];

/**
* @type \stdClass Overload properties.
* @var \stdClass Overload properties.
*
* @since 150422 Rewrite.
*/
Expand Down Expand Up @@ -185,7 +185,7 @@ public function &cacheKey($function, $args = [], $___prop = 'cache')

switch (gettype($_arg)) {
case 'integer':
$_key = (integer) $_arg;
$_key = (int) $_arg;
break; // Break switch handler.

case 'double':
Expand All @@ -194,7 +194,7 @@ public function &cacheKey($function, $args = [], $___prop = 'cache')
break; // Break switch handler.

case 'boolean':
$_key = (integer) $_arg;
$_key = (int) $_arg;
break; // Break switch handler.

case 'array':
Expand Down
1 change: 1 addition & 0 deletions src/includes/classes/AbsBaseAp.php
Expand Up @@ -11,6 +11,7 @@
abstract class AbsBaseAp extends AbsBase
{
/*[.build.php-auto-generate-use-Traits]*/
use Traits\Shared\ArrayUtils;
use Traits\Shared\BlogUtils;
use Traits\Shared\CacheDirUtils;
use Traits\Shared\CacheLockUtils;
Expand Down
29 changes: 13 additions & 16 deletions src/includes/traits/Ac/ClientSideUtils.php
Expand Up @@ -6,37 +6,34 @@
trait ClientSideUtils
{
/**
* Sends no-cache headers (if applicable).
* Sends no-cache headers.
*
* @since 150422 Rewrite. Enhanced/altered 151220.
* @since 150422 Rewrite.
* @since 151220 Enhancing.
* @since 16xxxx Enhancing.
*/
public function maybeStopBrowserCaching()
{
if (!defined('COMET_CACHE_ALLOW_CLIENT_SIDE_CACHE')) {
return $this->sendNoCacheHeaders(); // Upgrading from <= v160521, before we renamed this constant. Return default.
}

switch ((bool) COMET_CACHE_ALLOW_CLIENT_SIDE_CACHE) {
$short_name_lc = mb_strtolower(SHORT_NAME); // Needed below.

case true: // If global config allows, check exclusions.
switch (defined('COMET_CACHE_ALLOW_CLIENT_SIDE_CACHE') ? (bool) COMET_CACHE_ALLOW_CLIENT_SIDE_CACHE : false) {
case true: // If global config allows; check exclusions.

if (isset($_GET[mb_strtolower(SHORT_NAME).'ABC'])) {
if (!filter_var($_GET[mb_strtolower(SHORT_NAME).'ABC'], FILTER_VALIDATE_BOOLEAN)) {
if (isset($_GET[$short_name_lc.'ABC'])) {
if (!filter_var($_GET[$short_name_lc.'ABC'], FILTER_VALIDATE_BOOLEAN)) {
return $this->sendNoCacheHeaders(); // Disallow.
} // Else, allow client-side caching; because `ABC` is a true-ish value.
// ↑ Note that exclusion patterns are ignored in this case, in favor of `ABC`.
} // Else, allow client-side caching because `ABC` is a true-ish value.
} elseif (COMET_CACHE_EXCLUDE_CLIENT_SIDE_URIS && (empty($_SERVER['REQUEST_URI']) || preg_match(COMET_CACHE_EXCLUDE_CLIENT_SIDE_URIS, $_SERVER['REQUEST_URI']))) {
return $this->sendNoCacheHeaders(); // Disallow.
}
return; // Allow client-side caching; default behavior in this mode.

case false: // Global config disallows; check inclusions.

if (isset($_GET[mb_strtolower(SHORT_NAME).'ABC'])) {
if (filter_var($_GET[mb_strtolower(SHORT_NAME).'ABC'], FILTER_VALIDATE_BOOLEAN)) {
if (isset($_GET[$short_name_lc.'ABC'])) {
if (filter_var($_GET[$short_name_lc.'ABC'], FILTER_VALIDATE_BOOLEAN)) {
return; // Allow, because `ABC` is a false-ish value.
} // Else, disallow client-side caching; because `ABC` is a true-ish value.
// ↑ Note that inclusion patterns are ignored in this case, in favor of `ABC`.
} // Else, disallow client-side caching because `ABC` is a true-ish value.
}
return $this->sendNoCacheHeaders(); // Disallow; default behavior in this mode.
}
Expand Down
14 changes: 7 additions & 7 deletions src/includes/traits/Ac/ObUtils.php
Expand Up @@ -120,7 +120,7 @@ public function maybeStartOutputBuffering()
if (isset($_SERVER['COMET_CACHE_ALLOWED']) && !$_SERVER['COMET_CACHE_ALLOWED']) {
return $this->maybeSetDebugInfo($this::NC_DEBUG_COMET_CACHE_ALLOWED_SERVER_VAR);
}
if (defined('DONOTCACHEPAGE')) {
if (defined('DONOTCACHEPAGE')) { // Common to most WP cache plugins.
return $this->maybeSetDebugInfo($this::NC_DEBUG_DONOTCACHEPAGE_CONSTANT);
}
if (isset($_SERVER['DONOTCACHEPAGE'])) {
Expand All @@ -135,7 +135,7 @@ public function maybeStartOutputBuffering()
if (isset($_SERVER['SERVER_ADDR']) && $this->currentIp() === $_SERVER['SERVER_ADDR']) {
if ((!IS_PRO || !$this->isAutoCacheEngine()) && !$this->isLocalhost()) {
return $this->maybeSetDebugInfo($this::NC_DEBUG_SELF_SERVE_REQUEST);
}
} // Don't trip on requests by the auto-cache engine.
}
if (!COMET_CACHE_FEEDS_ENABLE && $this->isFeed()) {
return $this->maybeSetDebugInfo($this::NC_DEBUG_FEED_REQUEST);
Expand All @@ -155,7 +155,7 @@ public function maybeStartOutputBuffering()
if (!COMET_CACHE_GET_REQUESTS && $this->requestContainsUncacheableQueryVars()) {
return $this->maybeSetDebugInfo($this::NC_DEBUG_GET_REQUEST_QUERIES);
}
if (!empty($_REQUEST['preview'])) {
if (!empty($_REQUEST['preview'])) { // Don't cache previews under any circumstance.
return $this->maybeSetDebugInfo($this::NC_DEBUG_PREVIEW);
}
if (COMET_CACHE_EXCLUDE_HOSTS && preg_match(COMET_CACHE_EXCLUDE_HOSTS, $_SERVER['HTTP_HOST'])) {
Expand All @@ -167,21 +167,21 @@ public function maybeStartOutputBuffering()
if (COMET_CACHE_EXCLUDE_AGENTS && !empty($_SERVER['HTTP_USER_AGENT']) && (!IS_PRO || !$this->isAutoCacheEngine())) {
if (preg_match(COMET_CACHE_EXCLUDE_AGENTS, $_SERVER['HTTP_USER_AGENT'])) {
return $this->maybeSetDebugInfo($this::NC_DEBUG_EXCLUDED_AGENTS);
}
} // Don't trip on requests by the auto-cache engine.
}
if (COMET_CACHE_EXCLUDE_REFS && !empty($_REQUEST['_wp_http_referer'])) {
if (preg_match(COMET_CACHE_EXCLUDE_REFS, stripslashes($_REQUEST['_wp_http_referer']))) {
return $this->maybeSetDebugInfo($this::NC_DEBUG_EXCLUDED_REFS);
}
} // This variable is set by WordPress core in some cases.
}
if (COMET_CACHE_EXCLUDE_REFS && !empty($_SERVER['HTTP_REFERER'])) {
if (preg_match(COMET_CACHE_EXCLUDE_REFS, $_SERVER['HTTP_REFERER'])) {
return $this->maybeSetDebugInfo($this::NC_DEBUG_EXCLUDED_REFS);
}
} // Based on the HTTP referrer in this case.
}
$this->protocol = $this->isSsl() ? 'https://' : 'http://';
$this->host_token = $this->hostToken();
$this->host_base_dir_tokens = $this->hostBaseDirTokens();
$this->protocol = $this->isSsl() ? 'https://' : 'http://';

$this->version_salt = ''; // Initialize the version salt.
/*[pro strip-from="lite"]*/ // Fill the version salt in pro version.
Expand Down
57 changes: 35 additions & 22 deletions src/includes/traits/Shared/CachePathUtils.php
Expand Up @@ -6,26 +6,38 @@
trait CachePathUtils
{
/**
* Filter GET vars; e.g., remove those we ignore.
* Filter query vars; e.g., remove those we ignore.
*
* @since 16xxxx Adding support for ignored GET vars.
* @since 16xxxx Adding support for sorted query vars.
*
* @param array $get_vars GET vars to filter.
* @param array $_vars Query vars to filter.
*
* @return array Filtered GET vars.
* @return array Filtered query vars.
*/
public function filterGetVars($get_vars)
public function filterQueryVars($_vars)
{
$get_vars = (array) $get_vars;
$_vars = (array) $_vars; // Force array.
$cache_key = $_vars === $_GET ? md5(serialize($_vars)) : '';

if (COMET_CACHE_IGNORE_GET_REQUEST_VARS) {
foreach ($get_vars as $_key => $_value) {
if (is_string($_key) && preg_match(COMET_CACHE_IGNORE_GET_REQUEST_VARS, $_key)) {
unset($get_vars[$_key]); // Ignore.
}
} // unset($_key, $_value); // Housekeeping.
if ($cache_key && ($vars = &$this->staticKey(__FUNCTION__, $cache_key)) !== null) {
return $vars; // Already cached this.
}
return $this->ksortDeep($get_vars);
$vars = $_vars; // Copy.
$short_name_lc = mb_strtolower(SHORT_NAME);
$ignore_get_request_vars_regex = defined('COMET_CACHE_IGNORE_GET_REQUEST_VARS') ? COMET_CACHE_IGNORE_GET_REQUEST_VARS : '';

foreach ($vars as $_key => $_value) {
if (!is_string($_key)) {
continue; // Not applicable.
} elseif ($_key === $short_name_lc.'AC' || $_key === $short_name_lc.'ABC') {
unset($vars[$_key]);
} elseif ($ignore_get_request_vars_regex && preg_match($ignore_get_request_vars_regex, $_key)) {
unset($vars[$_key]);
}
} // unset($_key, $_value); // Housekeeping.

return $vars = $vars ? $this->ksortDeep($vars) : [];
}

/**
Expand Down Expand Up @@ -116,11 +128,8 @@ public function buildCachePath($url, $with_user_token = '', $with_version_salt =
$is_url_domain_mapped = $is_multisite && $can_consider_domain_mapping && $this->domainMappingBlogId($url);
$host_base_dir_tokens = $this->hostBaseDirTokens(false, $is_url_domain_mapped, !empty($url_parts['path']) ? $url_parts['path'] : '/');

$is_a_multisite_base_dir = $is_multisite && $host_base_dir_tokens && $host_base_dir_tokens !== '/' // Check?
&& mb_stripos(!empty($url_parts['path']) ? rtrim($url_parts['path'], '/').'/' : '/', $host_base_dir_tokens) === 0;

$is_a_multisite_base_dir_root = $is_multisite && $is_a_multisite_base_dir // Save time by using the previous check here.
&& strcasecmp(trim($host_base_dir_tokens, '/'), trim(!empty($url_parts['path']) ? $url_parts['path'] : '/', '/')) === 0;
$is_a_multisite_base_dir = $is_multisite && $host_base_dir_tokens && $host_base_dir_tokens !== '/' && mb_stripos(!empty($url_parts['path']) ? rtrim($url_parts['path'], '/').'/' : '/', $host_base_dir_tokens) === 0;
$is_a_multisite_base_dir_root = $is_multisite && $is_a_multisite_base_dir && strcasecmp(trim($host_base_dir_tokens, '/'), trim(!empty($url_parts['path']) ? $url_parts['path'] : '/', '/')) === 0;

# Build and return the cache path.

Expand All @@ -129,7 +138,6 @@ public function buildCachePath($url, $with_user_token = '', $with_version_salt =
}
if (!($flags & $this::CACHE_PATH_NO_HOST)) {
$cache_path .= $url_parts['host'].'/';

// Put multisite sub-roots into a host directory of their own.
// e.g., `example-com[[-base]-child1]` instead of `example-com`.
if ($is_a_multisite_base_dir && $host_base_dir_tokens && $host_base_dir_tokens !== '/') {
Expand Down Expand Up @@ -168,15 +176,20 @@ public function buildCachePath($url, $with_user_token = '', $with_version_salt =
$cache_path .= 'index/';
}
}
if ($this->isExtensionLoaded('mbstring') && mb_check_encoding($cache_path, 'UTF-8')) {
$cache_path = mb_strtolower($cache_path, 'UTF-8');
}
$cache_path = str_replace('.', '-', mb_strtolower($cache_path));

if (!($flags & $this::CACHE_PATH_NO_QUV)) {
if (!($flags & $this::CACHE_PATH_NO_QUERY)) {
if (isset($url_parts['query']) && $url_parts['query'] !== '') {
$cache_path = rtrim($cache_path, '/').'.q/'.md5($url_parts['query']).'/';

// Support for ignored GET vars.
parse_str($url_parts['query'], $_query_vars);
$_query_vars = $this->filterQueryVars($_query_vars);
// ↑ Also sorts query vars for smarter caching.

if ($_query_vars) { // If we have cacheable query vars.
$cache_path = rtrim($cache_path, '/').'.q/'.md5(serialize($_query_vars)).'/';
} // unset($_query_vars); // Housekeeping.
}
}
if (!($flags & $this::CACHE_PATH_NO_USER)) {
Expand Down

0 comments on commit 08fd140

Please sign in to comment.