Commit | Line | Data |
---|---|---|
1248c859 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
1248c859 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
1248c859 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
1248c859 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
1248c859 TO |
32 | */ |
33 | class CRM_Utils_Check { | |
50bfb460 | 34 | // How often to run checks and notify admins about issues. |
7d029160 | 35 | const CHECK_TIMER = 86400; |
1248c859 TO |
36 | |
37 | /** | |
38 | * We only need one instance of this object, so we use the | |
39 | * singleton pattern and cache the instance in this variable | |
40 | * | |
41 | * @var object | |
1248c859 TO |
42 | */ |
43 | static private $_singleton = NULL; | |
44 | ||
45 | /** | |
46 | * Provide static instance of CRM_Utils_Check. | |
47 | * | |
48 | * @return CRM_Utils_Check | |
49 | */ | |
00be9182 | 50 | public static function &singleton() { |
1248c859 TO |
51 | if (!isset(self::$_singleton)) { |
52 | self::$_singleton = new CRM_Utils_Check(); | |
53 | } | |
54 | return self::$_singleton; | |
55 | } | |
56 | ||
57 | /** | |
fe482240 | 58 | * Execute "checkAll". |
7c42ee33 | 59 | * |
f0f49b45 TO |
60 | * @param array|NULL $messages |
61 | * List of CRM_Utils_Check_Message; or NULL if the default list should be fetched. | |
62 | * @param array|string|callable $filter | |
63 | * Restrict messages using a callback filter. | |
64 | * By default, only show warnings and errors. | |
65 | * Set TRUE to show all messages. | |
1248c859 | 66 | */ |
6ea8408d | 67 | public function showPeriodicAlerts($messages = NULL, $filter = array(__CLASS__, 'severityMap')) { |
1248c859 TO |
68 | if (CRM_Core_Permission::check('administer CiviCRM') |
69 | && CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'securityAlert', NULL, TRUE) | |
70 | ) { | |
71 | $session = CRM_Core_Session::singleton(); | |
72 | if ($session->timer('check_' . __CLASS__, self::CHECK_TIMER)) { | |
73 | ||
74 | // Best attempt at re-securing folders | |
75 | $config = CRM_Core_Config::singleton(); | |
76 | $config->cleanup(0, FALSE); | |
77 | ||
7c42ee33 TO |
78 | if ($messages === NULL) { |
79 | $messages = $this->checkAll(); | |
80 | } | |
848577e3 | 81 | $statusMessages = array(); |
77fe66d6 | 82 | $statusType = 'alert'; |
7c42ee33 | 83 | foreach ($messages as $message) { |
4f2714af J |
84 | if ($filter === TRUE || $message->getSeverity() >= 3) { |
85 | $statusType = $message->getSeverity() >= 4 ? 'error' : $statusType; | |
47e2b515 AH |
86 | $statusMessage = $message->getMessage(); |
87 | $statusMessages[] = $statusTitle = $message->getTitle(); | |
f0f49b45 | 88 | } |
1248c859 | 89 | } |
848577e3 AH |
90 | |
91 | if (count($statusMessages)) { | |
92 | if (count($statusMessages) > 1) { | |
93 | $statusTitle = ts('Multiple Alerts'); | |
c1c5b99a | 94 | $statusMessage = ts('Please check your <a href="%1">status page</a> for a full list and further details.', array(1 => CRM_Utils_System::url('civicrm/a/#/status'))) . '<ul><li>' . implode('</li><li>', $statusMessages) . '</li></ul>'; |
848577e3 | 95 | } |
1b366958 | 96 | |
50bfb460 | 97 | // @todo add link to status page |
77fe66d6 | 98 | CRM_Core_Session::setStatus($statusMessage, $statusTitle, $statusType); |
848577e3 | 99 | } |
1248c859 TO |
100 | } |
101 | } | |
102 | } | |
103 | ||
1b366958 AH |
104 | /** |
105 | * Sort messages based upon severity | |
106 | * | |
107 | * @param CRM_Utils_Check_Message $a | |
108 | * @param CRM_Utils_Check_Message $b | |
fd66a333 | 109 | * @return int |
1b366958 | 110 | */ |
7d029160 | 111 | public static function severitySort($a, $b) { |
1b366958 AH |
112 | $aSeverity = $a->getSeverity(); |
113 | $bSeverity = $b->getSeverity(); | |
114 | if ($aSeverity == $bSeverity) { | |
47e2b515 | 115 | return strcmp($a->getName(), $b->getName()); |
1b366958 | 116 | } |
4f2714af J |
117 | // The Message constructor guarantees that these will always be integers. |
118 | return ($aSeverity < $bSeverity); | |
1b366958 AH |
119 | } |
120 | ||
f0f49b45 | 121 | /** |
6ea8408d | 122 | * Get the integer value (useful for thresholds) of the severity. |
f0f49b45 | 123 | * |
fd66a333 | 124 | * @param int|const $severity |
6ea8408d AH |
125 | * the value to look up |
126 | * @param bool $reverse | |
127 | * whether to find the constant from the integer | |
f0f49b45 TO |
128 | * @return bool |
129 | */ | |
6ea8408d | 130 | public static function severityMap($severity, $reverse = FALSE) { |
bc84da84 J |
131 | // Lowercase string-based severities |
132 | if (!$reverse) { | |
133 | $severity = strtolower($severity); | |
134 | } | |
135 | ||
6ea8408d AH |
136 | // See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md |
137 | $levels = array( | |
138 | \Psr\Log\LogLevel::EMERGENCY => 7, | |
139 | \Psr\Log\LogLevel::ALERT => 6, | |
140 | \Psr\Log\LogLevel::CRITICAL => 5, | |
141 | \Psr\Log\LogLevel::ERROR => 4, | |
142 | \Psr\Log\LogLevel::WARNING => 3, | |
143 | \Psr\Log\LogLevel::NOTICE => 2, | |
144 | \Psr\Log\LogLevel::INFO => 1, | |
145 | \Psr\Log\LogLevel::DEBUG => 0, | |
146 | ); | |
147 | return ($reverse) ? array_search($severity, $levels) : $levels[$severity]; | |
f0f49b45 TO |
148 | } |
149 | ||
c0bc3902 | 150 | /** |
fe482240 | 151 | * Throw an exception if any of the checks fail. |
7c42ee33 TO |
152 | * |
153 | * @param array|NULL $messages list of CRM_Utils_Check_Message; or NULL if the default list should be fetched | |
f4aaa82a EM |
154 | * |
155 | * @throws Exception | |
c0bc3902 | 156 | */ |
7c42ee33 TO |
157 | public function assertValid($messages = NULL) { |
158 | if ($messages === NULL) { | |
159 | $messages = $this->checkAll(); | |
160 | } | |
c0bc3902 TO |
161 | if (!empty($messages)) { |
162 | $messagesAsArray = array(); | |
163 | foreach ($messages as $message) { | |
164 | $messagesAsArray[] = $message->toArray(); | |
165 | } | |
166 | throw new Exception('There are configuration problems with this installation: ' . print_r($messagesAsArray, TRUE)); | |
167 | } | |
168 | } | |
169 | ||
1248c859 TO |
170 | /** |
171 | * Run some sanity checks. | |
172 | * | |
173 | * This could become a hook so that CiviCRM can run both built-in | |
174 | * configuration & sanity checks, and modules/extensions can add | |
175 | * their own checks. | |
176 | * | |
177 | * We might even expose the results of these checks on the Wordpress | |
178 | * plugin status page or the Drupal admin/reports/status path. | |
179 | * | |
7d029160 NM |
180 | * @param bool $max |
181 | * Whether to return just the maximum non-hushed severity | |
182 | * | |
a6c01b45 | 183 | * @return array |
16b10e64 | 184 | * Array of messages |
d3e86119 | 185 | * @link https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_requirements |
1248c859 | 186 | */ |
7d029160 | 187 | public static function checkAll($max = FALSE) { |
2aa3d7ab TO |
188 | $checks = array(); |
189 | $checks[] = new CRM_Utils_Check_Security(); | |
190 | $checks[] = new CRM_Utils_Check_Env(); | |
191 | ||
192 | $compInfo = CRM_Core_Component::getEnabledComponents(); | |
193 | foreach ($compInfo as $compObj) { | |
194 | switch ($compObj->info['name']) { | |
195 | case 'CiviCase': | |
196 | $checks[] = new CRM_Utils_Check_Case(CRM_Case_XMLRepository::singleton(), CRM_Case_PseudoConstant::caseType('name')); | |
197 | break; | |
e7292422 | 198 | |
2aa3d7ab TO |
199 | default: |
200 | } | |
201 | } | |
202 | ||
203 | $messages = array(); | |
204 | foreach ($checks as $check) { | |
205 | $messages = array_merge($messages, $check->checkAll()); | |
206 | } | |
260e353b TO |
207 | |
208 | CRM_Utils_Hook::check($messages); | |
209 | ||
46a903fb NM |
210 | foreach ($messages as $key => $message) { |
211 | $hush = self::checkHushSnooze($message); | |
03aa6781 | 212 | $message->setVisible(!$hush); |
a1c17079 | 213 | } |
47e2b515 AH |
214 | uasort($messages, array(__CLASS__, 'severitySort')); |
215 | ||
7d029160 NM |
216 | $maxSeverity = 1; |
217 | foreach ($messages as $message) { | |
218 | if (!$message->isVisible()) { | |
219 | continue; | |
220 | } | |
221 | $maxSeverity = max(1, $message->getLevel()); | |
222 | break; | |
223 | } | |
224 | ||
225 | Civi::cache()->set('systemCheckSeverity', $maxSeverity); | |
226 | $timestamp = time(); | |
227 | Civi::cache()->set('systemCheckDate', $timestamp); | |
228 | ||
229 | return ($max) ? $maxSeverity : $messages; | |
1248c859 TO |
230 | } |
231 | ||
27042a9e J |
232 | /** |
233 | * Evaluate if a system check should be hushed/snoozed. | |
234 | * | |
ea3ddccf | 235 | * @param string $message |
236 | * | |
27042a9e J |
237 | * @return bool |
238 | * TRUE means hush/snooze, FALSE means display. | |
ea3ddccf | 239 | * @throws \CiviCRM_API3_Exception |
27042a9e | 240 | */ |
7d029160 | 241 | public static function checkHushSnooze($message) { |
27042a9e J |
242 | $statusPreferenceParams = array( |
243 | 'name' => $message->getName(), | |
244 | 'domain_id' => CRM_Core_Config::domainID(), | |
245 | ); | |
246 | // Check if there's a StatusPreference matching this name/domain. | |
247 | $statusPreference = civicrm_api3('StatusPreference', 'get', $statusPreferenceParams); | |
77f715c6 J |
248 | $spid = FALSE; |
249 | if (isset($statusPreference['id'])) { | |
27042a9e | 250 | $spid = $statusPreference['id']; |
77f715c6 J |
251 | } |
252 | if ($spid) { | |
253 | // If so, compare severity to StatusPreference->severity. | |
d07e781d | 254 | $severity = $message->getSeverity(); |
27042a9e | 255 | if ($severity <= $statusPreference['values'][$spid]['ignore_severity']) { |
27042a9e | 256 | // A hush or a snooze has been set. Find out which. |
77f715c6 | 257 | if (isset($statusPreference['values'][$spid]['hush_until'])) { |
d47e1e1d | 258 | // Snooze is set. |
27042a9e | 259 | $today = new DateTime(); |
d47e1e1d J |
260 | $snoozeDate = new DateTime($statusPreference['values'][$spid]['hush_until']); |
261 | if ($today > $snoozeDate) { | |
262 | // Snooze is expired. | |
263 | return FALSE; | |
264 | } | |
265 | else { | |
266 | // Snooze is active. | |
267 | return TRUE; | |
268 | } | |
27042a9e J |
269 | } |
270 | else { | |
271 | // Hush. | |
27042a9e J |
272 | return TRUE; |
273 | } | |
274 | } | |
275 | } | |
276 | return FALSE; | |
277 | } | |
d47e1e1d | 278 | |
f4aaa82a | 279 | } |