[php8-compat] Fix issue with returning bool from uasort by using the spaceship operator
[civicrm-core.git] / CRM / Utils / Check.php
CommitLineData
1248c859
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
1248c859 5 | |
bc77d7c0
TO
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 |
1248c859 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
1248c859
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
1248c859
TO
16 */
17class CRM_Utils_Check {
50bfb460 18 // How often to run checks and notify admins about issues.
7d029160 19 const CHECK_TIMER = 86400;
1248c859 20
e5e4a5b2
CW
21 /**
22 * @var array
23 * @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
24 */
be2fb01f 25 protected static $severityList = [
e5e4a5b2
CW
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,
be2fb01f 34 ];
e5e4a5b2 35
1248c859
TO
36 /**
37 * We only need one instance of this object, so we use the
38 * singleton pattern and cache the instance in this variable
39 *
40 * @var object
1248c859
TO
41 */
42 static private $_singleton = NULL;
43
44 /**
45 * Provide static instance of CRM_Utils_Check.
46 *
47 * @return CRM_Utils_Check
48 */
00be9182 49 public static function &singleton() {
1248c859
TO
50 if (!isset(self::$_singleton)) {
51 self::$_singleton = new CRM_Utils_Check();
52 }
53 return self::$_singleton;
54 }
55
0e1b333f
CW
56 /**
57 * @return array
58 */
59 public static function getSeverityList() {
60 return self::$severityList;
61 }
62
1248c859 63 /**
f608a24a 64 * Display daily system status alerts (admin only).
1248c859 65 */
f608a24a 66 public function showPeriodicAlerts() {
755a1835 67 if (CRM_Core_Permission::check('administer CiviCRM system')) {
1248c859
TO
68 $session = CRM_Core_Session::singleton();
69 if ($session->timer('check_' . __CLASS__, self::CHECK_TIMER)) {
70
71 // Best attempt at re-securing folders
72 $config = CRM_Core_Config::singleton();
73 $config->cleanup(0, FALSE);
74
be2fb01f 75 $statusMessages = [];
f608a24a
CW
76 $maxSeverity = 0;
77 foreach ($this->checkAll() as $message) {
00eef683
AH
78 if (!$message->isVisible()) {
79 continue;
80 }
f608a24a
CW
81 if ($message->getLevel() >= 3) {
82 $maxSeverity = max($maxSeverity, $message->getLevel());
47e2b515
AH
83 $statusMessage = $message->getMessage();
84 $statusMessages[] = $statusTitle = $message->getTitle();
f0f49b45 85 }
1248c859 86 }
848577e3 87
f608a24a 88 if ($statusMessages) {
848577e3 89 if (count($statusMessages) > 1) {
f608a24a
CW
90 $statusTitle = self::toStatusLabel($maxSeverity);
91 $statusMessage = '<ul><li>' . implode('</li><li>', $statusMessages) . '</li></ul>';
848577e3 92 }
1b366958 93
f608a24a
CW
94 $statusMessage .= '<p><a href="' . CRM_Utils_System::url('civicrm/a/#/status') . '">' . ts('View details and manage alerts') . '</a></p>';
95
96 $statusType = $maxSeverity >= 4 ? 'error' : 'alert';
77fe66d6 97 CRM_Core_Session::setStatus($statusMessage, $statusTitle, $statusType);
848577e3 98 }
1248c859
TO
99 }
100 }
101 }
102
1b366958
AH
103 /**
104 * Sort messages based upon severity
105 *
106 * @param CRM_Utils_Check_Message $a
107 * @param CRM_Utils_Check_Message $b
fd66a333 108 * @return int
1b366958 109 */
7d029160 110 public static function severitySort($a, $b) {
fe8bdad8
CW
111 $aSeverity = $a->getLevel();
112 $bSeverity = $b->getLevel();
1b366958 113 if ($aSeverity == $bSeverity) {
47e2b515 114 return strcmp($a->getName(), $b->getName());
1b366958 115 }
4f2714af 116 // The Message constructor guarantees that these will always be integers.
11d593ed 117 return ($aSeverity <=> $bSeverity);
1b366958
AH
118 }
119
f0f49b45 120 /**
6ea8408d 121 * Get the integer value (useful for thresholds) of the severity.
f0f49b45 122 *
3860d465 123 * @param int|string $severity
6ea8408d
AH
124 * the value to look up
125 * @param bool $reverse
126 * whether to find the constant from the integer
3860d465
CW
127 * @return string|int
128 * @throws \CRM_Core_Exception
f0f49b45 129 */
6ea8408d 130 public static function severityMap($severity, $reverse = FALSE) {
3860d465
CW
131 if ($reverse) {
132 if (isset(self::$severityList[$severity])) {
133 return self::$severityList[$severity];
134 }
135 }
136 else {
137 // Lowercase string-based severities
bc84da84 138 $severity = strtolower($severity);
3860d465
CW
139 if (in_array($severity, self::$severityList)) {
140 return array_search($severity, self::$severityList);
141 }
bc84da84 142 }
3860d465 143 throw new CRM_Core_Exception('Invalid PSR Severity Level');
f0f49b45
TO
144 }
145
c0bc3902 146 /**
fe482240 147 * Throw an exception if any of the checks fail.
7c42ee33 148 *
c3850204
CW
149 * @param array|NULL $messages
150 * [CRM_Utils_Check_Message]
151 * @param string $threshold
f4aaa82a 152 *
c3850204
CW
153 * @throws \CRM_Core_Exception
154 * @throws \Exception
c0bc3902 155 */
c3850204 156 public function assertValid($messages = NULL, $threshold = \Psr\Log\LogLevel::ERROR) {
7c42ee33
TO
157 if ($messages === NULL) {
158 $messages = $this->checkAll();
159 }
c3850204 160 $minLevel = self::severityMap($threshold);
be2fb01f 161 $errors = [];
c3850204
CW
162 foreach ($messages as $message) {
163 if ($message->getLevel() >= $minLevel) {
164 $errors[] = $message->toArray();
c0bc3902 165 }
c3850204
CW
166 }
167 if ($errors) {
168 throw new Exception("System $threshold: " . print_r($errors, TRUE));
c0bc3902
TO
169 }
170 }
171
1248c859 172 /**
675e2573 173 * Run all enabled system checks.
1248c859 174 *
5f3c0af5 175 * This functon is wrapped by the System.check api.
1248c859 176 *
5f3c0af5 177 * Calls hook_civicrm_check() for extensions to add or modify messages.
d05f3550 178 * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_check/
1248c859 179 *
7d029160
NM
180 * @param bool $max
181 * Whether to return just the maximum non-hushed severity
182 *
675e2573 183 * @return CRM_Utils_Check_Message[]
1248c859 184 */
7d029160 185 public static function checkAll($max = FALSE) {
675e2573 186 $messages = self::checkStatus();
260e353b 187
be2fb01f 188 uasort($messages, [__CLASS__, 'severitySort']);
47e2b515 189
7d029160
NM
190 $maxSeverity = 1;
191 foreach ($messages as $message) {
192 if (!$message->isVisible()) {
193 continue;
194 }
195 $maxSeverity = max(1, $message->getLevel());
196 break;
197 }
198
b1fc1ab0 199 Civi::cache('checks')->set('systemStatusCheckResult', $maxSeverity);
7d029160
NM
200
201 return ($max) ? $maxSeverity : $messages;
1248c859
TO
202 }
203
675e2573
CW
204 /**
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)
209 *
210 * @return CRM_Utils_Check_Message[]
211 */
212 public static function checkStatus($statusNames = [], $includeDisabled = FALSE) {
213 $messages = [];
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));
221 }
222 if ($statusNames) {
223 // Early return if we have already run (or skipped) all the requested checks.
224 $checksNeeded = array_diff($checksNeeded, $component->getAllChecks());
225 if (!$checksNeeded) {
226 return $messages;
227 }
228 }
229 }
230
231 CRM_Utils_Hook::check($messages, $statusNames, $includeDisabled);
232
233 return $messages;
234 }
235
f608a24a
CW
236 /**
237 * @param int $level
238 * @return string
239 */
240 public static function toStatusLabel($level) {
241 switch ($level) {
242 case 7:
243 return ts('System Status: Emergency');
244
245 case 6:
246 return ts('System Status: Alert');
247
248 case 5:
249 return ts('System Status: Critical');
250
251 case 4:
252 return ts('System Status: Error');
253
254 case 3:
255 return ts('System Status: Warning');
256
257 case 2:
258 return ts('System Status: Notice');
259
260 default:
261 return ts('System Status: Ok');
262 }
263 }
264
f4aaa82a 265}