Merge pull request #7095 from mjwright/online-pledge-payment-fix
[civicrm-core.git] / CRM / Utils / Check.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33 class CRM_Utils_Check {
34 // How often to run checks and notify admins about issues.
35 const CHECK_TIMER = 86400;
36
37 /**
38 * @var array
39 * @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
40 */
41 protected static $severityList = array(
42 \Psr\Log\LogLevel::DEBUG,
43 \Psr\Log\LogLevel::INFO,
44 \Psr\Log\LogLevel::NOTICE,
45 \Psr\Log\LogLevel::WARNING,
46 \Psr\Log\LogLevel::ERROR,
47 \Psr\Log\LogLevel::CRITICAL,
48 \Psr\Log\LogLevel::ALERT,
49 \Psr\Log\LogLevel::EMERGENCY,
50 );
51
52 /**
53 * We only need one instance of this object, so we use the
54 * singleton pattern and cache the instance in this variable
55 *
56 * @var object
57 */
58 static private $_singleton = NULL;
59
60 /**
61 * Provide static instance of CRM_Utils_Check.
62 *
63 * @return CRM_Utils_Check
64 */
65 public static function &singleton() {
66 if (!isset(self::$_singleton)) {
67 self::$_singleton = new CRM_Utils_Check();
68 }
69 return self::$_singleton;
70 }
71
72 /**
73 * @return array
74 */
75 public static function getSeverityList() {
76 return self::$severityList;
77 }
78
79 /**
80 * Display daily system status alerts (admin only).
81 */
82 public function showPeriodicAlerts() {
83 if (CRM_Core_Permission::check('administer CiviCRM')) {
84 $session = CRM_Core_Session::singleton();
85 if ($session->timer('check_' . __CLASS__, self::CHECK_TIMER)) {
86
87 // Best attempt at re-securing folders
88 $config = CRM_Core_Config::singleton();
89 $config->cleanup(0, FALSE);
90
91 $statusMessages = array();
92 $maxSeverity = 0;
93 foreach ($this->checkAll() as $message) {
94 if (!$message->isVisible()) {
95 continue;
96 }
97 if ($message->getLevel() >= 3) {
98 $maxSeverity = max($maxSeverity, $message->getLevel());
99 $statusMessage = $message->getMessage();
100 $statusMessages[] = $statusTitle = $message->getTitle();
101 }
102 }
103
104 if ($statusMessages) {
105 if (count($statusMessages) > 1) {
106 $statusTitle = self::toStatusLabel($maxSeverity);
107 $statusMessage = '<ul><li>' . implode('</li><li>', $statusMessages) . '</li></ul>';
108 }
109
110 $statusMessage .= '<p><a href="' . CRM_Utils_System::url('civicrm/a/#/status') . '">' . ts('View details and manage alerts') . '</a></p>';
111
112 $statusType = $maxSeverity >= 4 ? 'error' : 'alert';
113 CRM_Core_Session::setStatus($statusMessage, $statusTitle, $statusType);
114 }
115 }
116 }
117 }
118
119 /**
120 * Sort messages based upon severity
121 *
122 * @param CRM_Utils_Check_Message $a
123 * @param CRM_Utils_Check_Message $b
124 * @return int
125 */
126 public static function severitySort($a, $b) {
127 $aSeverity = $a->getLevel();
128 $bSeverity = $b->getLevel();
129 if ($aSeverity == $bSeverity) {
130 return strcmp($a->getName(), $b->getName());
131 }
132 // The Message constructor guarantees that these will always be integers.
133 return ($aSeverity < $bSeverity);
134 }
135
136 /**
137 * Get the integer value (useful for thresholds) of the severity.
138 *
139 * @param int|string $severity
140 * the value to look up
141 * @param bool $reverse
142 * whether to find the constant from the integer
143 * @return string|int
144 * @throws \CRM_Core_Exception
145 */
146 public static function severityMap($severity, $reverse = FALSE) {
147 if ($reverse) {
148 if (isset(self::$severityList[$severity])) {
149 return self::$severityList[$severity];
150 }
151 }
152 else {
153 // Lowercase string-based severities
154 $severity = strtolower($severity);
155 if (in_array($severity, self::$severityList)) {
156 return array_search($severity, self::$severityList);
157 }
158 }
159 throw new CRM_Core_Exception('Invalid PSR Severity Level');
160 }
161
162 /**
163 * Throw an exception if any of the checks fail.
164 *
165 * @param array|NULL $messages list of CRM_Utils_Check_Message; or NULL if the default list should be fetched
166 *
167 * @throws Exception
168 */
169 public function assertValid($messages = NULL) {
170 if ($messages === NULL) {
171 $messages = $this->checkAll();
172 }
173 if (!empty($messages)) {
174 $messagesAsArray = array();
175 foreach ($messages as $message) {
176 $messagesAsArray[] = $message->toArray();
177 }
178 throw new Exception('There are configuration problems with this installation: ' . print_r($messagesAsArray, TRUE));
179 }
180 }
181
182 /**
183 * Run all system checks.
184 *
185 * This functon is wrapped by the System.check api.
186 *
187 * Calls hook_civicrm_check() for extensions to add or modify messages.
188 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_check
189 *
190 * @param bool $max
191 * Whether to return just the maximum non-hushed severity
192 *
193 * @return array
194 * Array of CRM_Utils_Check_Message objects
195 */
196 public static function checkAll($max = FALSE) {
197 $checks = array();
198 $checks[] = new CRM_Utils_Check_Security();
199 $checks[] = new CRM_Utils_Check_Env();
200
201 $compInfo = CRM_Core_Component::getEnabledComponents();
202 foreach ($compInfo as $compObj) {
203 switch ($compObj->info['name']) {
204 case 'CiviCase':
205 $checks[] = new CRM_Utils_Check_Case(CRM_Case_XMLRepository::singleton(), CRM_Case_PseudoConstant::caseType('name'));
206 break;
207
208 default:
209 }
210 }
211
212 $messages = array();
213 foreach ($checks as $check) {
214 $messages = array_merge($messages, $check->checkAll());
215 }
216
217 CRM_Utils_Hook::check($messages);
218
219 uasort($messages, array(__CLASS__, 'severitySort'));
220
221 $maxSeverity = 1;
222 foreach ($messages as $message) {
223 if (!$message->isVisible()) {
224 continue;
225 }
226 $maxSeverity = max(1, $message->getLevel());
227 break;
228 }
229
230 Civi::settings()->set('systemStatusCheckResult', $maxSeverity);
231
232 return ($max) ? $maxSeverity : $messages;
233 }
234
235 /**
236 * @param int $level
237 * @return string
238 */
239 public static function toStatusLabel($level) {
240 switch ($level) {
241 case 7:
242 return ts('System Status: Emergency');
243
244 case 6:
245 return ts('System Status: Alert');
246
247 case 5:
248 return ts('System Status: Critical');
249
250 case 4:
251 return ts('System Status: Error');
252
253 case 3:
254 return ts('System Status: Warning');
255
256 case 2:
257 return ts('System Status: Notice');
258
259 default:
260 return ts('System Status: Ok');
261 }
262 }
263
264 }