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

OPcache - handle opcache.restrict_api setting #812

Open
wants to merge 1 commit into
base: dev
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
29 changes: 25 additions & 4 deletions src/purge.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,34 @@ private function _purge_all_cssjs($silence = false)
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 opcode cache due to opcache not enabled');

if (!$silence) {
$msg = __('Opcode cache is not enabled.', 'litespeed-cache');
Admin_Display::error($msg);
!defined('LITESPEED_PURGE_SILENT') && Admin_Display::error($msg);
}

return false;
}

if (Router::opcache_restricted(__FILE__)) {
self::debug('❌ Failed to reset opcode cache due to OPcache is restricted. File requesting the clear is not allowed.');

if (!$silence) {
$msg = __('OPcache is restricted by "restrict_api".', 'litespeed-cache');
!defined('LITESPEED_PURGE_SILENT') && Admin_Display::error($msg);
}

return false;
}

// Purge opcode cache
if (!opcache_reset()) {
self::debug('❌ Reset opcode not worked');

if (!$silence) {
$msg = __('Reset the opcode cache was not successfully.', 'litespeed-cache');
!defined('LITESPEED_PURGE_SILENT') && Admin_Display::success($msg);
}

return false;
Expand All @@ -411,8 +434,6 @@ 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');

if (!$silence) {
Expand Down
22 changes: 22 additions & 0 deletions src/router.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,28 @@ 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.0
* @access public
*/
public static function opcache_restricted($file)
{
$restrict_value = ini_get('opcache.restrict_api');
if ($restrict_value) {
if ($file) {
// if file if not in path, it will show warning.
return strpos($file, $restrict_value) === false;
}

return true;
}

return false;
}

/**
* Handle static files
*
Expand Down