From 1248c859b5179926424c88232e0eeee14b8ca8c2 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 3 Apr 2014 21:36:48 -0700 Subject: [PATCH] CRM-14423 - Extract class CRM_Utils_Check from CRM_Utils_Check_Security --- CRM/Core/Page.php | 2 +- CRM/Utils/Check.php | 104 +++++++++++++++++++++++++++++++++++ CRM/Utils/Check/Message.php | 16 +++++- CRM/Utils/Check/Security.php | 72 ++++-------------------- api/v3/System.php | 2 +- 5 files changed, 131 insertions(+), 65 deletions(-) create mode 100644 CRM/Utils/Check.php diff --git a/CRM/Core/Page.php b/CRM/Core/Page.php index 520a6f4feb..522b3cae73 100644 --- a/CRM/Core/Page.php +++ b/CRM/Core/Page.php @@ -195,7 +195,7 @@ class CRM_Core_Page { if (empty($_GET['snippet'])) { // Version check and intermittent alert to admins CRM_Utils_VersionCheck::singleton()->versionAlert(); - CRM_Utils_Check_Security::singleton()->showPeriodicAlerts(); + CRM_Utils_Check::singleton()->showPeriodicAlerts(); // Debug msg once per hour if ($config->debug && CRM_Core_Permission::check('administer CiviCRM') && CRM_Core_Session::singleton()->timer('debug_alert', 3600)) { diff --git a/CRM/Utils/Check.php b/CRM/Utils/Check.php new file mode 100644 index 0000000000..65a4ff331d --- /dev/null +++ b/CRM/Utils/Check.php @@ -0,0 +1,104 @@ +timer('check_' . __CLASS__, self::CHECK_TIMER)) { + + // Best attempt at re-securing folders + $config = CRM_Core_Config::singleton(); + $config->cleanup(0, FALSE); + + foreach ($this->checkAll() as $message) { + CRM_Core_Session::setStatus($message->getMessage(), $message->getTitle()); + } + } + } + } + + /** + * Run some sanity checks. + * + * This could become a hook so that CiviCRM can run both built-in + * configuration & sanity checks, and modules/extensions can add + * their own checks. + * + * We might even expose the results of these checks on the Wordpress + * plugin status page or the Drupal admin/reports/status path. + * + * @return array of messages + * @see Drupal's hook_requirements() - + * https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_requirements + */ + public function checkAll() { + $security = new CRM_Utils_Check_Security(); + $messages = array_merge( + $security->checkAll() + ); + return $messages; + } + +} \ No newline at end of file diff --git a/CRM/Utils/Check/Message.php b/CRM/Utils/Check/Message.php index 3f3cd2e0a3..825d6065d8 100644 --- a/CRM/Utils/Check/Message.php +++ b/CRM/Utils/Check/Message.php @@ -43,9 +43,15 @@ class CRM_Utils_Check_Message { */ private $message; - function __construct($name, $message) { + /** + * @var string + */ + private $title; + + function __construct($name, $message, $title) { $this->name = $name; $this->message = $message; + $this->title = $title; } /** @@ -62,6 +68,13 @@ class CRM_Utils_Check_Message { return $this->message; } + /** + * @return string + */ + public function getTitle() { + return $this->title; + } + /** * @return array */ @@ -69,6 +82,7 @@ class CRM_Utils_Check_Message { return array( 'name' => $this->name, 'message' => $this->message, + 'title' => $this->title, ); } } diff --git a/CRM/Utils/Check/Security.php b/CRM/Utils/Check/Security.php index c6cd89ac2c..d5ca50dd98 100644 --- a/CRM/Utils/Check/Security.php +++ b/CRM/Utils/Check/Security.php @@ -34,31 +34,6 @@ */ class CRM_Utils_Check_Security { - CONST - // How often to run checks and notify admins about issues. - CHECK_TIMER = 86400; - - /** - * We only need one instance of this object, so we use the - * singleton pattern and cache the instance in this variable - * - * @var object - * @static - */ - static private $_singleton = NULL; - - /** - * Provide static instance of CRM_Utils_Check_Security. - * - * @return CRM_Utils_Check_Security - */ - static function &singleton() { - if (!isset(self::$_singleton)) { - self::$_singleton = new CRM_Utils_Check_Security(); - } - return self::$_singleton; - } - /** * CMS have a different pattern to their default file path and URL. * @@ -75,46 +50,16 @@ class CRM_Utils_Check_Security { } } - /** - * Execute "checkAll" - */ - public function showPeriodicAlerts() { - if (CRM_Core_Permission::check('administer CiviCRM') - && CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'securityAlert', NULL, TRUE) - ) { - $session = CRM_Core_Session::singleton(); - if ($session->timer('check_' . __CLASS__, self::CHECK_TIMER)) { - - // Best attempt at re-securing folders - $config = CRM_Core_Config::singleton(); - $config->cleanup(0, FALSE); - - foreach ($this->checkAll() as $message) { - CRM_Core_Session::setStatus($message->getMessage(), ts('Security Warning')); - } - } - } - } - /** * Run some sanity checks. * - * This could become a hook so that CiviCRM can run both built-in - * configuration & sanity checks, and modules/extensions can add - * their own checks. - * - * We might even expose the results of these checks on the Wordpress - * plugin status page or the Drupal admin/reports/status path. - * - * @return array of messages - * @see Drupal's hook_requirements() - - * https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_requirements + * @return array */ public function checkAll() { $messages = array_merge( - CRM_Utils_Check_Security::singleton()->checkLogFileIsNotAccessible(), - CRM_Utils_Check_Security::singleton()->checkUploadsAreNotAccessible(), - CRM_Utils_Check_Security::singleton()->checkDirectoriesAreNotBrowseable() + $this->checkLogFileIsNotAccessible(), + $this->checkUploadsAreNotAccessible(), + $this->checkDirectoriesAreNotBrowseable() ); return $messages; } @@ -162,7 +107,8 @@ class CRM_Utils_Check_Security { 'Read more about this warning'; $messages[] = new CRM_Utils_Check_Message( 'checkLogFileIsNotAccessible', - ts($msg, array(1 => $log_url, 2 => $docs_url)) + ts($msg, array(1 => $log_url, 2 => $docs_url)), + ts('Security Warning') ); } } @@ -206,7 +152,8 @@ class CRM_Utils_Check_Security { $docs_url = $this->createDocUrl('checkUploadsAreNotAccessible'); $messages[] = new CRM_Utils_Check_Message( 'checkUploadsAreNotAccessible', - ts($msg, array(1 => $docs_url)) + ts($msg, array(1 => $docs_url)), + ts('Security Warning') ); } } @@ -253,7 +200,8 @@ class CRM_Utils_Check_Security { $docs_url = $this->createDocUrl('checkDirectoriesAreNotBrowseable'); $messages[] = new CRM_Utils_Check_Message( 'checkDirectoriesAreNotBrowseable', - ts($msg, array(1 => $publicDir, 2 => $publicDir, 3 => $docs_url)) + ts($msg, array(1 => $publicDir, 2 => $publicDir, 3 => $docs_url)), + ts('Security Warning') ); } } diff --git a/api/v3/System.php b/api/v3/System.php index 9a8d092e83..99bec9d1ca 100644 --- a/api/v3/System.php +++ b/api/v3/System.php @@ -92,7 +92,7 @@ function _civicrm_api3_system_check_spec(&$spec) { */ function civicrm_api3_system_check($params) { $returnValues = array(); - foreach (CRM_Utils_Check_Security::singleton()->checkAll() as $message) { + foreach (CRM_Utils_Check::singleton()->checkAll() as $message) { $returnValues[] = $message->toArray(); } -- 2.25.1