Merge pull request #15790 from civicrm/5.20
[civicrm-core.git] / CRM / Utils / Check / Component.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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 use Civi\Api4\StatusPreference;
29
30 /**
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2020
34 */
35 abstract class CRM_Utils_Check_Component {
36
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
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]
81 *
82 * @throws \API_Exception
83 * @throws \Civi\API\Exception\UnauthorizedException
84 */
85 public function checkAll() {
86 $messages = [];
87 foreach (get_class_methods($this) as $method) {
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)) {
90 $messages = array_merge($messages, $this->$method());
91 }
92 }
93 return $messages;
94 }
95
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) {
107 try {
108 $checks = $this->getChecksConfig();
109 if (!empty($checks[$method])) {
110 return (bool) empty($checks[$method]['is_active']);
111 }
112 }
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
120 return FALSE;
121 }
122
123 /**
124 * Check if file exists on given URL.
125 *
126 * @param string $url
127 * @param float|bool $timeoutOverride
128 *
129 * @return bool
130 * @throws \GuzzleHttp\Exception\GuzzleException
131 */
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 }
137 $fileExists = FALSE;
138 try {
139 $guzzleClient = new GuzzleHttp\Client();
140 $guzzleResponse = $guzzleClient->request('GET', $url, array(
141 'timeout' => $timeoutOverride,
142 ));
143 $fileExists = ($guzzleResponse->getStatusCode() == 200);
144 }
145 catch (Exception $e) {
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.
148 }
149 return $fileExists;
150 }
151
152 }