Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-08-19-00-06-22
[civicrm-core.git] / CRM / Utils / Check.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id: $
33 *
34 */
35 class CRM_Utils_Check {
36 CONST
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
45 * @static
46 */
47 static private $_singleton = NULL;
48
49 /**
50 * Provide static instance of CRM_Utils_Check.
51 *
52 * @return CRM_Utils_Check
53 */
54 static function &singleton() {
55 if (!isset(self::$_singleton)) {
56 self::$_singleton = new CRM_Utils_Check();
57 }
58 return self::$_singleton;
59 }
60
61 /**
62 * Execute "checkAll"
63 *
64 * @param array|NULL $messages list of CRM_Utils_Check_Message; or NULL if the default list should be fetched
65 */
66 public function showPeriodicAlerts($messages = NULL) {
67 if (CRM_Core_Permission::check('administer CiviCRM')
68 && CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'securityAlert', NULL, TRUE)
69 ) {
70 $session = CRM_Core_Session::singleton();
71 if ($session->timer('check_' . __CLASS__, self::CHECK_TIMER)) {
72
73 // Best attempt at re-securing folders
74 $config = CRM_Core_Config::singleton();
75 $config->cleanup(0, FALSE);
76
77 if ($messages === NULL) {
78 $messages = $this->checkAll();
79 }
80 foreach ($messages as $message) {
81 CRM_Core_Session::setStatus($message->getMessage(), $message->getTitle());
82 }
83 }
84 }
85 }
86
87 /**
88 * Throw an exception if any of the checks fail
89 *
90 * @param array|NULL $messages list of CRM_Utils_Check_Message; or NULL if the default list should be fetched
91 *
92 * @throws Exception
93 */
94 public function assertValid($messages = NULL) {
95 if ($messages === NULL) {
96 $messages = $this->checkAll();
97 }
98 if (!empty($messages)) {
99 $messagesAsArray = array();
100 foreach ($messages as $message) {
101 $messagesAsArray[] = $message->toArray();
102 }
103 throw new Exception('There are configuration problems with this installation: ' . print_r($messagesAsArray, TRUE));
104 }
105 }
106
107 /**
108 * Run some sanity checks.
109 *
110 * This could become a hook so that CiviCRM can run both built-in
111 * configuration & sanity checks, and modules/extensions can add
112 * their own checks.
113 *
114 * We might even expose the results of these checks on the Wordpress
115 * plugin status page or the Drupal admin/reports/status path.
116 *
117 * @return array of messages
118 * @see Drupal's hook_requirements() -
119 * https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_requirements
120 */
121 public function checkAll() {
122 $checks = array();
123 $checks[] = new CRM_Utils_Check_Security();
124 $checks[] = new CRM_Utils_Check_Env();
125
126 $compInfo = CRM_Core_Component::getEnabledComponents();
127 foreach ($compInfo as $compObj) {
128 switch ($compObj->info['name']) {
129 case 'CiviCase':
130 $checks[] = new CRM_Utils_Check_Case(CRM_Case_XMLRepository::singleton(), CRM_Case_PseudoConstant::caseType('name'));
131 break;
132 default:
133 }
134 }
135
136 $messages = array();
137 foreach ($checks as $check) {
138 $messages = array_merge($messages, $check->checkAll());
139 }
140 return $messages;
141 }
142
143 }