From b7760d30e4dbf601690c8d7351542330e7e8f193 Mon Sep 17 00:00:00 2001 From: Bradley Taylor Date: Sun, 20 Feb 2022 16:21:55 +0000 Subject: [PATCH] Improve Permission denied handling in WordPress --- CRM/Core/Error.php | 14 ++++++++++---- CRM/Utils/System.php | 1 + CRM/Utils/System/Base.php | 9 +++++++++ CRM/Utils/System/WordPress.php | 19 +++++++++++++++++++ 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/CRM/Core/Error.php b/CRM/Core/Error.php index 763a2605d3..7687bb641a 100644 --- a/CRM/Core/Error.php +++ b/CRM/Core/Error.php @@ -204,9 +204,11 @@ class CRM_Core_Error extends PEAR_ErrorStack { CRM_Core_Error::debug_var('Fatal Error Details', $error, TRUE, TRUE, '', PEAR_LOG_ERR); CRM_Core_Error::backtrace('backTrace', TRUE); + $exit = TRUE; if ($config->initialized) { $content = $template->fetch('CRM/common/fatal.tpl'); echo CRM_Utils_System::theme($content); + $exit = CRM_Utils_System::exitAfterFatal(); } else { echo "Sorry. A non-recoverable error has occurred. The error trace below might help to resolve the issue

"; @@ -217,7 +219,7 @@ class CRM_Core_Error extends PEAR_ErrorStack { exit; } $runOnce = TRUE; - self::abend(CRM_Core_Error::FATAL_ERROR); + self::abend(CRM_Core_Error::FATAL_ERROR, $exit); } /** @@ -442,9 +444,10 @@ class CRM_Core_Error extends PEAR_ErrorStack { } echo CRM_Utils_System::theme($content); + $exit = CRM_Utils_System::exitAfterFatal(); // fin - self::abend(CRM_Core_Error::FATAL_ERROR); + self::abend(CRM_Core_Error::FATAL_ERROR, $exit); } /** @@ -999,12 +1002,15 @@ class CRM_Core_Error extends PEAR_ErrorStack { * Terminate execution abnormally. * * @param string $code + * @param bool $exit */ - protected static function abend($code) { + protected static function abend($code, $exit = TRUE) { // do a hard rollback of any pending transactions // if we've come here, its because of some unexpected PEAR errors CRM_Core_Transaction::forceRollbackIfEnabled(); - CRM_Utils_System::civiExit($code); + if ($exit) { + CRM_Utils_System::civiExit($code); + } } /** diff --git a/CRM/Utils/System.php b/CRM/Utils/System.php index addc25b46b..4514547628 100644 --- a/CRM/Utils/System.php +++ b/CRM/Utils/System.php @@ -38,6 +38,7 @@ * @method static array synchronizeUsers() Create CRM contacts for all existing CMS users. * @method static void appendCoreResources(\Civi\Core\Event\GenericHookEvent $e) Callback for hook_civicrm_coreResourceList. * @method static void alterAssetUrl(\Civi\Core\Event\GenericHookEvent $e) Callback for hook_civicrm_getAssetUrl. + * @method static exitAfterFatal() Should the current execution exit after a fatal error? */ class CRM_Utils_System { diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php index a4660834c5..e52c33cbe1 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -1099,4 +1099,13 @@ abstract class CRM_Utils_System_Base { return []; } + /** + * Should the current execution exit after a fatal error? + * This is the appropriate functionality in most cases. + * @return bool + */ + public function exitAfterFatal() { + return TRUE; + } + } diff --git a/CRM/Utils/System/WordPress.php b/CRM/Utils/System/WordPress.php index 241eeaca6c..4f2bf719b5 100644 --- a/CRM/Utils/System/WordPress.php +++ b/CRM/Utils/System/WordPress.php @@ -602,6 +602,8 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base { */ public function permissionDenied() { status_header(403); + global $civicrm_wp_title; + $civicrm_wp_title = ts('You do not have permission to access this page.'); throw new CRM_Core_Exception(ts('You do not have permission to access this page.')); } @@ -1473,4 +1475,21 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base { return !$this->isUserRegistrationPermitted(); } + /** + * Should the current execution exit after a fatal error? + * + * In WordPress, it is not usually possible to trigger theming outside of the WordPress theme process, + * meaning that in order to render an error inside the theme we cannot exit on error. + * + * @return bool + */ + public function exitAfterFatal() { + $ret = TRUE; + if (!is_admin() && !wp_doing_ajax()) { + $ret = FALSE; + } + + return apply_filters('civicrm_exit_after_fatal', $ret); + } + } -- 2.25.1