Commit | Line | Data |
---|---|---|
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 | */ |
17 | class 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() { |
f55dd135 | 67 | if (CRM_Core_Permission::check('administer CiviCRM')) { |
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 J |
116 | // The Message constructor guarantees that these will always be integers. |
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 | /** |
5f3c0af5 | 173 | * Run all system checks. |
1248c859 | 174 | * |
5f3c0af5 | 175 | * This functon is wrapped by the System.check api. |
1248c859 | 176 | * |
5f3c0af5 CW |
177 | * Calls hook_civicrm_check() for extensions to add or modify messages. |
178 | * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_check | |
1248c859 | 179 | * |
7d029160 NM |
180 | * @param bool $max |
181 | * Whether to return just the maximum non-hushed severity | |
182 | * | |
a6c01b45 | 183 | * @return array |
5f3c0af5 | 184 | * Array of CRM_Utils_Check_Message objects |
1248c859 | 185 | */ |
7d029160 | 186 | public static function checkAll($max = FALSE) { |
be2fb01f | 187 | $messages = []; |
3a0d0bbd CW |
188 | foreach (glob(__DIR__ . '/Check/Component/*.php') as $filePath) { |
189 | $className = 'CRM_Utils_Check_Component_' . basename($filePath, '.php'); | |
190 | /* @var CRM_Utils_Check_Component $check */ | |
191 | $check = new $className(); | |
192 | if ($check->isEnabled()) { | |
193 | $messages = array_merge($messages, $check->checkAll()); | |
194 | } | |
2aa3d7ab | 195 | } |
260e353b TO |
196 | |
197 | CRM_Utils_Hook::check($messages); | |
198 | ||
be2fb01f | 199 | uasort($messages, [__CLASS__, 'severitySort']); |
47e2b515 | 200 | |
7d029160 NM |
201 | $maxSeverity = 1; |
202 | foreach ($messages as $message) { | |
203 | if (!$message->isVisible()) { | |
204 | continue; | |
205 | } | |
206 | $maxSeverity = max(1, $message->getLevel()); | |
207 | break; | |
208 | } | |
209 | ||
b1fc1ab0 | 210 | Civi::cache('checks')->set('systemStatusCheckResult', $maxSeverity); |
7d029160 NM |
211 | |
212 | return ($max) ? $maxSeverity : $messages; | |
1248c859 TO |
213 | } |
214 | ||
f608a24a CW |
215 | /** |
216 | * @param int $level | |
217 | * @return string | |
218 | */ | |
219 | public static function toStatusLabel($level) { | |
220 | switch ($level) { | |
221 | case 7: | |
222 | return ts('System Status: Emergency'); | |
223 | ||
224 | case 6: | |
225 | return ts('System Status: Alert'); | |
226 | ||
227 | case 5: | |
228 | return ts('System Status: Critical'); | |
229 | ||
230 | case 4: | |
231 | return ts('System Status: Error'); | |
232 | ||
233 | case 3: | |
234 | return ts('System Status: Warning'); | |
235 | ||
236 | case 2: | |
237 | return ts('System Status: Notice'); | |
238 | ||
239 | default: | |
240 | return ts('System Status: Ok'); | |
241 | } | |
242 | } | |
243 | ||
f4aaa82a | 244 | } |