Commit | Line | Data |
---|---|---|
1248c859 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | * $Id: $ |
33 | * | |
34 | */ | |
35 | class CRM_Utils_Check { | |
7da04cde | 36 | const |
1248c859 TO |
37 | // How often to run checks and notify admins about issues. |
38 | CHECK_TIMER = 86400; | |
39 | ||
40 | /** | |
41 | * We only need one instance of this object, so we use the | |
42 | * singleton pattern and cache the instance in this variable | |
43 | * | |
44 | * @var object | |
1248c859 TO |
45 | */ |
46 | static private $_singleton = NULL; | |
47 | ||
48 | /** | |
49 | * Provide static instance of CRM_Utils_Check. | |
50 | * | |
51 | * @return CRM_Utils_Check | |
52 | */ | |
00be9182 | 53 | public static function &singleton() { |
1248c859 TO |
54 | if (!isset(self::$_singleton)) { |
55 | self::$_singleton = new CRM_Utils_Check(); | |
56 | } | |
57 | return self::$_singleton; | |
58 | } | |
59 | ||
60 | /** | |
fe482240 | 61 | * Execute "checkAll". |
7c42ee33 | 62 | * |
f0f49b45 TO |
63 | * @param array|NULL $messages |
64 | * List of CRM_Utils_Check_Message; or NULL if the default list should be fetched. | |
65 | * @param array|string|callable $filter | |
66 | * Restrict messages using a callback filter. | |
67 | * By default, only show warnings and errors. | |
68 | * Set TRUE to show all messages. | |
1248c859 | 69 | */ |
6ea8408d | 70 | public function showPeriodicAlerts($messages = NULL, $filter = array(__CLASS__, 'severityMap')) { |
1248c859 TO |
71 | if (CRM_Core_Permission::check('administer CiviCRM') |
72 | && CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'securityAlert', NULL, TRUE) | |
73 | ) { | |
74 | $session = CRM_Core_Session::singleton(); | |
75 | if ($session->timer('check_' . __CLASS__, self::CHECK_TIMER)) { | |
76 | ||
77 | // Best attempt at re-securing folders | |
78 | $config = CRM_Core_Config::singleton(); | |
79 | $config->cleanup(0, FALSE); | |
80 | ||
7c42ee33 TO |
81 | if ($messages === NULL) { |
82 | $messages = $this->checkAll(); | |
83 | } | |
848577e3 | 84 | $statusMessages = array(); |
77fe66d6 AH |
85 | $statusType = 'alert'; |
86 | ||
7c42ee33 | 87 | foreach ($messages as $message) { |
6ea8408d AH |
88 | if ($filter === TRUE || call_user_func($filter, $message->getSeverity()) >= 3) { |
89 | $statusType = (call_user_func($filter, $message->getSeverity()) >= 4) ? 'error' : $statusType; | |
47e2b515 AH |
90 | $statusMessage = $message->getMessage(); |
91 | $statusMessages[] = $statusTitle = $message->getTitle(); | |
f0f49b45 | 92 | } |
1248c859 | 93 | } |
848577e3 AH |
94 | |
95 | if (count($statusMessages)) { | |
96 | if (count($statusMessages) > 1) { | |
97 | $statusTitle = ts('Multiple Alerts'); | |
fd66a333 | 98 | $statusMessage = '<ul><li>' . implode('</li><li>', $statusMessages) . '</li></ul>'; |
848577e3 | 99 | } |
1b366958 | 100 | |
848577e3 | 101 | // TODO: add link to status page |
77fe66d6 | 102 | CRM_Core_Session::setStatus($statusMessage, $statusTitle, $statusType); |
848577e3 | 103 | } |
1248c859 TO |
104 | } |
105 | } | |
106 | } | |
107 | ||
1b366958 AH |
108 | /** |
109 | * Sort messages based upon severity | |
110 | * | |
111 | * @param CRM_Utils_Check_Message $a | |
112 | * @param CRM_Utils_Check_Message $b | |
fd66a333 | 113 | * @return int |
1b366958 AH |
114 | */ |
115 | public function severitySort($a, $b) { | |
116 | $aSeverity = $a->getSeverity(); | |
117 | $bSeverity = $b->getSeverity(); | |
118 | if ($aSeverity == $bSeverity) { | |
47e2b515 | 119 | return strcmp($a->getName(), $b->getName()); |
1b366958 AH |
120 | } |
121 | return (self::severityMap($aSeverity) > self::severityMap($bSeverity)); | |
122 | } | |
123 | ||
f0f49b45 | 124 | /** |
6ea8408d | 125 | * Get the integer value (useful for thresholds) of the severity. |
f0f49b45 | 126 | * |
fd66a333 | 127 | * @param int|const $severity |
6ea8408d AH |
128 | * the value to look up |
129 | * @param bool $reverse | |
130 | * whether to find the constant from the integer | |
f0f49b45 TO |
131 | * @return bool |
132 | */ | |
6ea8408d | 133 | public static function severityMap($severity, $reverse = FALSE) { |
bc84da84 J |
134 | // Lowercase string-based severities |
135 | if (!$reverse) { | |
136 | $severity = strtolower($severity); | |
137 | } | |
138 | ||
6ea8408d AH |
139 | // See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md |
140 | $levels = array( | |
141 | \Psr\Log\LogLevel::EMERGENCY => 7, | |
142 | \Psr\Log\LogLevel::ALERT => 6, | |
143 | \Psr\Log\LogLevel::CRITICAL => 5, | |
144 | \Psr\Log\LogLevel::ERROR => 4, | |
145 | \Psr\Log\LogLevel::WARNING => 3, | |
146 | \Psr\Log\LogLevel::NOTICE => 2, | |
147 | \Psr\Log\LogLevel::INFO => 1, | |
148 | \Psr\Log\LogLevel::DEBUG => 0, | |
149 | ); | |
150 | return ($reverse) ? array_search($severity, $levels) : $levels[$severity]; | |
f0f49b45 TO |
151 | } |
152 | ||
c0bc3902 | 153 | /** |
fe482240 | 154 | * Throw an exception if any of the checks fail. |
7c42ee33 TO |
155 | * |
156 | * @param array|NULL $messages list of CRM_Utils_Check_Message; or NULL if the default list should be fetched | |
f4aaa82a EM |
157 | * |
158 | * @throws Exception | |
c0bc3902 | 159 | */ |
7c42ee33 TO |
160 | public function assertValid($messages = NULL) { |
161 | if ($messages === NULL) { | |
162 | $messages = $this->checkAll(); | |
163 | } | |
c0bc3902 TO |
164 | if (!empty($messages)) { |
165 | $messagesAsArray = array(); | |
166 | foreach ($messages as $message) { | |
167 | $messagesAsArray[] = $message->toArray(); | |
168 | } | |
169 | throw new Exception('There are configuration problems with this installation: ' . print_r($messagesAsArray, TRUE)); | |
170 | } | |
171 | } | |
172 | ||
1248c859 TO |
173 | /** |
174 | * Run some sanity checks. | |
175 | * | |
176 | * This could become a hook so that CiviCRM can run both built-in | |
177 | * configuration & sanity checks, and modules/extensions can add | |
178 | * their own checks. | |
179 | * | |
180 | * We might even expose the results of these checks on the Wordpress | |
181 | * plugin status page or the Drupal admin/reports/status path. | |
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 | */ |
fd66a333 | 187 | public function checkAll($showHushed = 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 | ||
27042a9e J |
210 | if (!$showHushed) { |
211 | foreach ($messages as $key => $message) { | |
212 | $hush = self::checkHushSnooze($message); | |
213 | if ($hush) { | |
214 | unset($messages[$key]); | |
215 | } | |
216 | } | |
a1c17079 | 217 | } |
218 | ||
47e2b515 AH |
219 | uasort($messages, array(__CLASS__, 'severitySort')); |
220 | ||
1248c859 TO |
221 | return $messages; |
222 | } | |
223 | ||
27042a9e J |
224 | /** |
225 | * Evaluate if a system check should be hushed/snoozed. | |
226 | * | |
227 | * @return bool | |
228 | * TRUE means hush/snooze, FALSE means display. | |
229 | */ | |
230 | public function checkHushSnooze($message) { | |
27042a9e J |
231 | $statusPreferenceParams = array( |
232 | 'name' => $message->getName(), | |
233 | 'domain_id' => CRM_Core_Config::domainID(), | |
234 | ); | |
235 | // Check if there's a StatusPreference matching this name/domain. | |
236 | $statusPreference = civicrm_api3('StatusPreference', 'get', $statusPreferenceParams); | |
237 | if ($statusPreference['id']) { | |
238 | // If so, compare severity to StatusPreference->severity. | |
239 | $spid = $statusPreference['id']; | |
240 | $severity = self::severityMap($message->getSeverity()); | |
241 | if ($severity <= $statusPreference['values'][$spid]['ignore_severity']) { | |
27042a9e J |
242 | // A hush or a snooze has been set. Find out which. |
243 | if ($statusPreference['values'][$spid]['hush_until']) { | |
d47e1e1d | 244 | // Snooze is set. |
27042a9e | 245 | $today = new DateTime(); |
d47e1e1d J |
246 | $snoozeDate = new DateTime($statusPreference['values'][$spid]['hush_until']); |
247 | if ($today > $snoozeDate) { | |
248 | // Snooze is expired. | |
249 | return FALSE; | |
250 | } | |
251 | else { | |
252 | // Snooze is active. | |
253 | return TRUE; | |
254 | } | |
27042a9e J |
255 | } |
256 | else { | |
257 | // Hush. | |
27042a9e J |
258 | return TRUE; |
259 | } | |
260 | } | |
261 | } | |
262 | return FALSE; | |
263 | } | |
d47e1e1d | 264 | |
f4aaa82a | 265 | } |