3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Utils_Check
{
18 // How often to run checks and notify admins about issues.
19 const CHECK_TIMER
= 86400;
23 * @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
25 protected static $severityList = [
26 \Psr\Log\LogLevel
::DEBUG
,
27 \Psr\Log\LogLevel
::INFO
,
28 \Psr\Log\LogLevel
::NOTICE
,
29 \Psr\Log\LogLevel
::WARNING
,
30 \Psr\Log\LogLevel
::ERROR
,
31 \Psr\Log\LogLevel
::CRITICAL
,
32 \Psr\Log\LogLevel
::ALERT
,
33 \Psr\Log\LogLevel
::EMERGENCY
,
37 * We only need one instance of this object, so we use the
38 * singleton pattern and cache the instance in this variable
42 static private $_singleton = NULL;
45 * Provide static instance of CRM_Utils_Check.
47 * @return CRM_Utils_Check
49 public static function &singleton() {
50 if (!isset(self
::$_singleton)) {
51 self
::$_singleton = new CRM_Utils_Check();
53 return self
::$_singleton;
59 public static function getSeverityList() {
60 return self
::$severityList;
64 * Display daily system status alerts (admin only).
66 public function showPeriodicAlerts() {
67 if (CRM_Core_Permission
::check('administer CiviCRM system')) {
68 $session = CRM_Core_Session
::singleton();
69 if ($session->timer('check_' . __CLASS__
, self
::CHECK_TIMER
)) {
71 // Best attempt at re-securing folders
72 $config = CRM_Core_Config
::singleton();
73 $config->cleanup(0, FALSE);
77 foreach ($this->checkAll() as $message) {
78 if (!$message->isVisible()) {
81 if ($message->getLevel() >= 3) {
82 $maxSeverity = max($maxSeverity, $message->getLevel());
83 $statusMessage = $message->getMessage();
84 $statusMessages[] = $statusTitle = $message->getTitle();
88 if ($statusMessages) {
89 if (count($statusMessages) > 1) {
90 $statusTitle = self
::toStatusLabel($maxSeverity);
91 $statusMessage = '<ul><li>' . implode('</li><li>', $statusMessages) . '</li></ul>';
94 $statusMessage .= '<p><a href="' . CRM_Utils_System
::url('civicrm/a/#/status') . '">' . ts('View details and manage alerts') . '</a></p>';
96 $statusType = $maxSeverity >= 4 ?
'error' : 'alert';
97 CRM_Core_Session
::setStatus($statusMessage, $statusTitle, $statusType);
104 * Sort messages based upon severity
106 * @param CRM_Utils_Check_Message $a
107 * @param CRM_Utils_Check_Message $b
110 public static function severitySort($a, $b) {
111 $aSeverity = $a->getLevel();
112 $bSeverity = $b->getLevel();
113 if ($aSeverity == $bSeverity) {
114 return strcmp($a->getName(), $b->getName());
116 // The Message constructor guarantees that these will always be integers.
117 return ($aSeverity <=> $bSeverity);
121 * Get the integer value (useful for thresholds) of the severity.
123 * @param int|string $severity
124 * the value to look up
125 * @param bool $reverse
126 * whether to find the constant from the integer
128 * @throws \CRM_Core_Exception
130 public static function severityMap($severity, $reverse = FALSE) {
132 if (isset(self
::$severityList[$severity])) {
133 return self
::$severityList[$severity];
137 // Lowercase string-based severities
138 $severity = strtolower($severity);
139 if (in_array($severity, self
::$severityList)) {
140 return array_search($severity, self
::$severityList);
143 throw new CRM_Core_Exception('Invalid PSR Severity Level');
147 * Throw an exception if any of the checks fail.
149 * @param array|NULL $messages
150 * [CRM_Utils_Check_Message]
151 * @param string $threshold
153 * @throws \CRM_Core_Exception
156 public function assertValid($messages = NULL, $threshold = \Psr\Log\LogLevel
::ERROR
) {
157 if ($messages === NULL) {
158 $messages = $this->checkAll();
160 $minLevel = self
::severityMap($threshold);
162 foreach ($messages as $message) {
163 if ($message->getLevel() >= $minLevel) {
164 $errors[] = $message->toArray();
168 throw new Exception("System $threshold: " . print_r($errors, TRUE));
173 * Run all enabled system checks.
175 * This functon is wrapped by the System.check api.
177 * Calls hook_civicrm_check() for extensions to add or modify messages.
178 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_check/
181 * Whether to return just the maximum non-hushed severity
183 * @return CRM_Utils_Check_Message[]
185 public static function checkAll($max = FALSE) {
186 $messages = self
::checkStatus();
188 uasort($messages, [__CLASS__
, 'severitySort']);
191 foreach ($messages as $message) {
192 if (!$message->isVisible()) {
195 $maxSeverity = max(1, $message->getLevel());
199 Civi
::cache('checks')->set('systemStatusCheckResult', $maxSeverity);
201 return ($max) ?
$maxSeverity : $messages;
205 * @param array $statusNames
206 * Optionally specify the names of specific checks to run, or leave empty to run all
207 * @param bool $includeDisabled
208 * Run checks that have been explicitly disabled (default false)
210 * @return CRM_Utils_Check_Message[]
212 public static function checkStatus($statusNames = [], $includeDisabled = FALSE) {
214 $checksNeeded = $statusNames;
215 foreach (glob(__DIR__
. '/Check/Component/*.php') as $filePath) {
216 $className = 'CRM_Utils_Check_Component_' . basename($filePath, '.php');
217 /* @var CRM_Utils_Check_Component $component */
218 $component = new $className();
219 if ($includeDisabled ||
$component->isEnabled()) {
220 $messages = array_merge($messages, $component->checkAll($statusNames, $includeDisabled));
223 // Early return if we have already run (or skipped) all the requested checks.
224 $checksNeeded = array_diff($checksNeeded, $component->getAllChecks());
225 if (!$checksNeeded) {
231 CRM_Utils_Hook
::check($messages, $statusNames, $includeDisabled);
240 public static function toStatusLabel($level) {
243 return ts('System Status: Emergency');
246 return ts('System Status: Alert');
249 return ts('System Status: Critical');
252 return ts('System Status: Error');
255 return ts('System Status: Warning');
258 return ts('System Status: Notice');
261 return ts('System Status: Ok');