From 715b591d3f7db013a2e4a716aa071c72124aa639 Mon Sep 17 00:00:00 2001 From: JasWSInc Date: Mon, 29 Sep 2014 23:40:57 -0800 Subject: [PATCH] Bug fix. Only cache `GET` requests. See: websharks/quick-cache#279 --- .../includes/advanced-cache.tpl.php | 2 +- quick-cache-pro/includes/share.php | 33 +++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/quick-cache-pro/includes/advanced-cache.tpl.php b/quick-cache-pro/includes/advanced-cache.tpl.php index c8a705b6..20ff4978 100644 --- a/quick-cache-pro/includes/advanced-cache.tpl.php +++ b/quick-cache-pro/includes/advanced-cache.tpl.php @@ -904,7 +904,7 @@ public function maybe_postload_invalidate_when_logged_in() if(!empty($_REQUEST[__NAMESPACE__]['ajax_clear_cache'])) return; // Site owner is clearing cache now. - if($this->is_uncacheable_request_method()) + if($this->is_post_put_delete_request() || $this->is_uncacheable_request_method()) $this->postload['invalidate_when_logged_in'] = TRUE; else if(!QUICK_CACHE_GET_REQUESTS && $this->is_get_request_w_query()) diff --git a/quick-cache-pro/includes/share.php b/quick-cache-pro/includes/share.php index 1cdb26fe..90765290 100644 --- a/quick-cache-pro/includes/share.php +++ b/quick-cache-pro/includes/share.php @@ -494,7 +494,31 @@ public function is_get_request_w_query() return static::$static[__FUNCTION__]; if(!empty($_GET) || isset($_SERVER['QUERY_STRING'][0])) - if(!(isset($_GET['qcABC']) && count($_GET) === 1)) // Ignore this special case. + if(!(isset($_GET['qcABC']) && count($_GET) === 1)) // Special case. + return (static::$static[__FUNCTION__] = TRUE); + + return (static::$static[__FUNCTION__] = FALSE); + } + + /** + * Is the current request method `POST`, `PUT` or `DELETE`? + * + * @since 14xxxx Updating `is_uncacheable_request_method()` and restoring this one. + * + * @return boolean `TRUE` if current request method is `POST`, `PUT` or `DELETE`. + * + * @note The return value of this function is cached to reduce overhead on repeat calls. + */ + public function is_post_put_delete_request() + { + if(isset(static::$static[__FUNCTION__])) + return static::$static[__FUNCTION__]; + + if(!empty($_POST)) // Being thorough. + return (static::$static[__FUNCTION__] = TRUE); + + if(!empty($_SERVER['REQUEST_METHOD'])) + if(in_array(strtoupper($_SERVER['REQUEST_METHOD']), array('POST', 'PUT', 'DELETE'), TRUE)) return (static::$static[__FUNCTION__] = TRUE); return (static::$static[__FUNCTION__] = FALSE); @@ -503,6 +527,7 @@ public function is_get_request_w_query() /** * Is the current request method is uncacheable? * + * @since 14xxxx Reversing logic; only allow `GET` requests to be cached. * @since 140725 Adding HEAD/OPTIONS/TRACE/CONNECT to the list of uncacheables. * * @return boolean `TRUE` if current request method is uncacheable. @@ -514,9 +539,11 @@ public function is_uncacheable_request_method() if(isset(static::$static[__FUNCTION__])) return static::$static[__FUNCTION__]; + if(!empty($_POST)) // Being thorough. + return (static::$static[__FUNCTION__] = TRUE); + if(!empty($_SERVER['REQUEST_METHOD'])) - if(in_array(strtoupper($_SERVER['REQUEST_METHOD']), - array('POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT'), TRUE)) + if(!in_array(strtoupper($_SERVER['REQUEST_METHOD']), array('GET'), TRUE)) return (static::$static[__FUNCTION__] = TRUE); return (static::$static[__FUNCTION__] = FALSE);