phpcs fix
[civicrm-core.git] / CRM / Utils / Check.php
CommitLineData
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 */
35class 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 */
f0f49b45 70 public function showPeriodicAlerts($messages = NULL, $filter = array(__CLASS__, 'isImportantAlert')) {
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 }
84 foreach ($messages as $message) {
f0f49b45
TO
85 if ($filter === TRUE || call_user_func($filter, $message)) {
86 CRM_Core_Session::setStatus($message->getMessage(), $message->getTitle());
87 }
1248c859
TO
88 }
89 }
90 }
91 }
92
f0f49b45
TO
93 /**
94 * Determine if a message is important enough to harass the administrator about.
95 *
96 * @param CRM_Utils_Check_Message $message
97 * @return bool
98 */
99 protected static function isImportantAlert($message) {
100 return in_array($message->getLevel(), array(
101 \Psr\Log\LogLevel::WARNING,
102 \Psr\Log\LogLevel::ALERT,
103 \Psr\Log\LogLevel::CRITICAL,
104 \Psr\Log\LogLevel::EMERGENCY,
105 ));
106 }
107
c0bc3902 108 /**
fe482240 109 * Throw an exception if any of the checks fail.
7c42ee33
TO
110 *
111 * @param array|NULL $messages list of CRM_Utils_Check_Message; or NULL if the default list should be fetched
f4aaa82a
EM
112 *
113 * @throws Exception
c0bc3902 114 */
7c42ee33
TO
115 public function assertValid($messages = NULL) {
116 if ($messages === NULL) {
117 $messages = $this->checkAll();
118 }
c0bc3902
TO
119 if (!empty($messages)) {
120 $messagesAsArray = array();
121 foreach ($messages as $message) {
122 $messagesAsArray[] = $message->toArray();
123 }
124 throw new Exception('There are configuration problems with this installation: ' . print_r($messagesAsArray, TRUE));
125 }
126 }
127
1248c859
TO
128 /**
129 * Run some sanity checks.
130 *
131 * This could become a hook so that CiviCRM can run both built-in
132 * configuration & sanity checks, and modules/extensions can add
133 * their own checks.
134 *
135 * We might even expose the results of these checks on the Wordpress
136 * plugin status page or the Drupal admin/reports/status path.
137 *
a6c01b45 138 * @return array
16b10e64 139 * Array of messages
d3e86119 140 * @link https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_requirements
1248c859
TO
141 */
142 public function checkAll() {
2aa3d7ab
TO
143 $checks = array();
144 $checks[] = new CRM_Utils_Check_Security();
145 $checks[] = new CRM_Utils_Check_Env();
146
147 $compInfo = CRM_Core_Component::getEnabledComponents();
148 foreach ($compInfo as $compObj) {
149 switch ($compObj->info['name']) {
150 case 'CiviCase':
151 $checks[] = new CRM_Utils_Check_Case(CRM_Case_XMLRepository::singleton(), CRM_Case_PseudoConstant::caseType('name'));
152 break;
e7292422 153
2aa3d7ab
TO
154 default:
155 }
156 }
157
158 $messages = array();
159 foreach ($checks as $check) {
160 $messages = array_merge($messages, $check->checkAll());
161 }
260e353b
TO
162
163 CRM_Utils_Hook::check($messages);
164
1248c859
TO
165 return $messages;
166 }
167
f4aaa82a 168}