Merge pull request #7252 from jitendrapurohit/CRM-17507
[civicrm-core.git] / CRM / Utils / Check.php
CommitLineData
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 */
33class CRM_Utils_Check {
50bfb460 34 // How often to run checks and notify admins about issues.
7d029160 35 const CHECK_TIMER = 86400;
1248c859 36
e5e4a5b2
CW
37 /**
38 * @var array
39 * @link https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md
40 */
0e1b333f 41 protected static $severityList = array(
e5e4a5b2
CW
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
1248c859
TO
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
1248c859
TO
57 */
58 static private $_singleton = NULL;
59
60 /**
61 * Provide static instance of CRM_Utils_Check.
62 *
63 * @return CRM_Utils_Check
64 */
00be9182 65 public static function &singleton() {
1248c859
TO
66 if (!isset(self::$_singleton)) {
67 self::$_singleton = new CRM_Utils_Check();
68 }
69 return self::$_singleton;
70 }
71
0e1b333f
CW
72 /**
73 * @return array
74 */
75 public static function getSeverityList() {
76 return self::$severityList;
77 }
78
1248c859 79 /**
f608a24a 80 * Display daily system status alerts (admin only).
1248c859 81 */
f608a24a 82 public function showPeriodicAlerts() {
f55dd135 83 if (CRM_Core_Permission::check('administer CiviCRM')) {
1248c859
TO
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
848577e3 91 $statusMessages = array();
f608a24a
CW
92 $maxSeverity = 0;
93 foreach ($this->checkAll() as $message) {
00eef683
AH
94 if (!$message->isVisible()) {
95 continue;
96 }
f608a24a
CW
97 if ($message->getLevel() >= 3) {
98 $maxSeverity = max($maxSeverity, $message->getLevel());
47e2b515
AH
99 $statusMessage = $message->getMessage();
100 $statusMessages[] = $statusTitle = $message->getTitle();
f0f49b45 101 }
1248c859 102 }
848577e3 103
f608a24a 104 if ($statusMessages) {
848577e3 105 if (count($statusMessages) > 1) {
f608a24a
CW
106 $statusTitle = self::toStatusLabel($maxSeverity);
107 $statusMessage = '<ul><li>' . implode('</li><li>', $statusMessages) . '</li></ul>';
848577e3 108 }
1b366958 109
f608a24a
CW
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';
77fe66d6 113 CRM_Core_Session::setStatus($statusMessage, $statusTitle, $statusType);
848577e3 114 }
1248c859
TO
115 }
116 }
117 }
118
1b366958
AH
119 /**
120 * Sort messages based upon severity
121 *
122 * @param CRM_Utils_Check_Message $a
123 * @param CRM_Utils_Check_Message $b
fd66a333 124 * @return int
1b366958 125 */
7d029160 126 public static function severitySort($a, $b) {
fe8bdad8
CW
127 $aSeverity = $a->getLevel();
128 $bSeverity = $b->getLevel();
1b366958 129 if ($aSeverity == $bSeverity) {
47e2b515 130 return strcmp($a->getName(), $b->getName());
1b366958 131 }
4f2714af
J
132 // The Message constructor guarantees that these will always be integers.
133 return ($aSeverity < $bSeverity);
1b366958
AH
134 }
135
f0f49b45 136 /**
6ea8408d 137 * Get the integer value (useful for thresholds) of the severity.
f0f49b45 138 *
3860d465 139 * @param int|string $severity
6ea8408d
AH
140 * the value to look up
141 * @param bool $reverse
142 * whether to find the constant from the integer
3860d465
CW
143 * @return string|int
144 * @throws \CRM_Core_Exception
f0f49b45 145 */
6ea8408d 146 public static function severityMap($severity, $reverse = FALSE) {
3860d465
CW
147 if ($reverse) {
148 if (isset(self::$severityList[$severity])) {
149 return self::$severityList[$severity];
150 }
151 }
152 else {
153 // Lowercase string-based severities
bc84da84 154 $severity = strtolower($severity);
3860d465
CW
155 if (in_array($severity, self::$severityList)) {
156 return array_search($severity, self::$severityList);
157 }
bc84da84 158 }
3860d465 159 throw new CRM_Core_Exception('Invalid PSR Severity Level');
f0f49b45
TO
160 }
161
c0bc3902 162 /**
fe482240 163 * Throw an exception if any of the checks fail.
7c42ee33
TO
164 *
165 * @param array|NULL $messages list of CRM_Utils_Check_Message; or NULL if the default list should be fetched
f4aaa82a
EM
166 *
167 * @throws Exception
c0bc3902 168 */
7c42ee33
TO
169 public function assertValid($messages = NULL) {
170 if ($messages === NULL) {
171 $messages = $this->checkAll();
172 }
c0bc3902
TO
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
1248c859 182 /**
5f3c0af5 183 * Run all system checks.
1248c859 184 *
5f3c0af5 185 * This functon is wrapped by the System.check api.
1248c859 186 *
5f3c0af5
CW
187 * Calls hook_civicrm_check() for extensions to add or modify messages.
188 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_check
1248c859 189 *
7d029160
NM
190 * @param bool $max
191 * Whether to return just the maximum non-hushed severity
192 *
a6c01b45 193 * @return array
5f3c0af5 194 * Array of CRM_Utils_Check_Message objects
1248c859 195 */
7d029160 196 public static function checkAll($max = FALSE) {
2aa3d7ab
TO
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;
e7292422 207
2aa3d7ab
TO
208 default:
209 }
210 }
211
212 $messages = array();
213 foreach ($checks as $check) {
214 $messages = array_merge($messages, $check->checkAll());
215 }
260e353b
TO
216
217 CRM_Utils_Hook::check($messages);
218
47e2b515
AH
219 uasort($messages, array(__CLASS__, 'severitySort'));
220
7d029160
NM
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
f55dd135 230 Civi::settings()->set('systemStatusCheckResult', $maxSeverity);
7d029160
NM
231
232 return ($max) ? $maxSeverity : $messages;
1248c859
TO
233 }
234
f608a24a
CW
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
f4aaa82a 264}