Merge pull request #15923 from eileenmcnaughton/act_order
[civicrm-core.git] / CRM / Utils / Check / Component.php
CommitLineData
3a0d0bbd
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
3a0d0bbd 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
3a0d0bbd
CW
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
b4628ea6 28use Civi\Api4\StatusPreference;
4094935a 29
3a0d0bbd
CW
30/**
31 *
32 * @package CRM
6b83d5bd 33 * @copyright CiviCRM LLC (c) 2004-2019
3a0d0bbd
CW
34 */
35abstract class CRM_Utils_Check_Component {
36
b4628ea6 37 /**
38 * @var array
39 */
40 public $checksConfig = [];
41
42 /**
43 * Get the configured status checks.
44 *
45 * @return array
46 *
47 * @throws \API_Exception
48 * @throws \Civi\API\Exception\UnauthorizedException
49 */
50 public function getChecksConfig() {
51 if (empty($this->checksConfig)) {
52 $this->checksConfig = Civi::cache('checks')->get('checksConfig', []);
53 if (empty($this->checksConfig)) {
54 $this->checksConfig = StatusPreference::get()->setCheckPermissions(FALSE)->execute()->indexBy('name');
55 }
56 }
57 return $this->checksConfig;
58 }
59
60 /**
61 * @param array $checksConfig
62 */
63 public function setChecksConfig(array $checksConfig) {
64 $this->checksConfig = $checksConfig;
65 }
66
3a0d0bbd
CW
67 /**
68 * Should these checks be run?
69 *
70 * @return bool
71 */
72 public function isEnabled() {
73 return TRUE;
74 }
75
76 /**
77 * Run all checks in this class.
78 *
79 * @return array
80 * [CRM_Utils_Check_Message]
b4628ea6 81 *
82 * @throws \API_Exception
83 * @throws \Civi\API\Exception\UnauthorizedException
3a0d0bbd
CW
84 */
85 public function checkAll() {
be2fb01f 86 $messages = [];
3a0d0bbd 87 foreach (get_class_methods($this) as $method) {
b4628ea6 88 // Note that we should check if the test is disabled BEFORE running it in case it's disabled for performance.
89 if ($method !== 'checkAll' && strpos($method, 'check') === 0 && !$this->isDisabled($method)) {
3a0d0bbd
CW
90 $messages = array_merge($messages, $this->$method());
91 }
92 }
93 return $messages;
94 }
d631cdc8 95
b4628ea6 96 /**
97 * Is the specified check disabled.
98 *
99 * @param string $method
100 *
101 * @return bool
102 *
103 * @throws \API_Exception
104 * @throws \Civi\API\Exception\UnauthorizedException
105 */
106 public function isDisabled($method) {
5a74bbd5
PF
107 try {
108 $checks = $this->getChecksConfig();
109 if (!empty($checks[$method])) {
110 return (bool) empty($checks[$method]['is_active']);
111 }
b4628ea6 112 }
5a74bbd5
PF
113 catch (PEAR_Exception $e) {
114 // if we're hitting this, DB migration to 5.19 probably hasn't run yet, so
115 // is_active doesn't exist. Ignore this error so the status check (which
116 // might warn about missing migrations!) still renders.
117 // TODO: remove at some point after 5.19
118 }
119
b4628ea6 120 return FALSE;
121 }
122
4094935a
AP
123 /**
124 * Check if file exists on given URL.
125 *
6714d8d2 126 * @param string $url
9679fb15 127 * @param float|bool $timeoutOverride
32e1f6cc 128 *
4094935a 129 * @return bool
b4628ea6 130 * @throws \GuzzleHttp\Exception\GuzzleException
4094935a 131 */
9679fb15
SL
132 public function fileExists($url, $timeoutOverride = FALSE) {
133 // Timeout past in maybe 0 in which case we should still permit it (0 is infinite).
134 if (!$timeoutOverride && $timeoutOverride !== 0) {
135 $timeoutOverride = (float) Civi::settings()->get('http_timeout');
136 }
4094935a
AP
137 $fileExists = FALSE;
138 try {
139 $guzzleClient = new GuzzleHttp\Client();
140 $guzzleResponse = $guzzleClient->request('GET', $url, array(
9679fb15 141 'timeout' => $timeoutOverride,
4094935a
AP
142 ));
143 $fileExists = ($guzzleResponse->getStatusCode() == 200);
144 }
145 catch (Exception $e) {
32e1f6cc 146 // At this stage we are not checking for variants of not being able to receive it.
147 // However, we might later enhance this to distinguish forbidden from a 500 error.
4094935a
AP
148 }
149 return $fileExists;
150 }
151
137d9cf0 152}