diff --git a/src/purge.cls.php b/src/purge.cls.php index 74133d2ac..27ecdad9f 100644 --- a/src/purge.cls.php +++ b/src/purge.cls.php @@ -393,16 +393,40 @@ private function _purge_all_cssjs($silence = false) * Purge opcode cache * * @since 1.8.2 + * @since 7.3 added test for opcode cache restriction * @access public */ public function purge_all_opcache($silence = false) { if (!Router::opcache_enabled()) { - self::debug('Failed to reset opcode cache due to opcache not enabled'); + self::debug('❌ Failed to reset OPcache due to OPcache not enabled'); if (!$silence) { - $msg = __('Opcode cache is not enabled.', 'litespeed-cache'); - Admin_Display::error($msg); + $msg = __('OPcache is not enabled.', 'litespeed-cache'); + !defined('LITESPEED_PURGE_SILENT') && Admin_Display::error($msg); + } + + return false; + } + + if (Router::opcache_restricted(__FILE__)) { + self::debug('❌ Failed to reset OPcache due to OPcache is restricted. File requesting the clear is not allowed.'); + + if (!$silence) { + $msg = sprintf(__('OPcache is restricted by %s setting.', 'litespeed-cache'), 'restrict_api'); + !defined('LITESPEED_PURGE_SILENT') && Admin_Display::error($msg); + } + + return false; + } + + // Purge opcode cache + if (!opcache_reset()) { + self::debug('❌ Reset OPcache not worked'); + + if (!$silence) { + $msg = __('Reset the OPcache failed.', 'litespeed-cache'); + !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); } return false; @@ -411,12 +435,10 @@ public function purge_all_opcache($silence = false) // Action to run after opcache purge. do_action('litespeed_purged_all_opcache'); - // Purge opcode cache - opcache_reset(); - self::debug('Reset opcode cache'); + self::debug('Reset OPcache'); if (!$silence) { - $msg = __('Reset the entire opcode cache successfully.', 'litespeed-cache'); + $msg = __('Reset the entire OPcache successfully.', 'litespeed-cache'); !defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg); } diff --git a/src/router.cls.php b/src/router.cls.php index d90f124fb..b28d55862 100644 --- a/src/router.cls.php +++ b/src/router.cls.php @@ -755,6 +755,25 @@ public static function opcache_enabled() return function_exists('opcache_reset') && ini_get('opcache.enable'); } + /** + * Check if opcode cache is restricted and file that is requesting. + * https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.restrict-api + * + * @since 7.3 + * @access public + */ + public static function opcache_restricted($file) + { + $restrict_value = ini_get('opcache.restrict_api'); + if ($restrict_value) { + if ( !$file || false === strpos($restrict_value, $file) ) { + return true; + } + } + + return false; + } + /** * Handle static files *