3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
12 use Civi\Api4\StatusPreference
;
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 abstract class CRM_Utils_Check_Component
{
24 public $checksConfig = [];
27 * Get the configured status checks.
31 * @throws \API_Exception
32 * @throws \Civi\API\Exception\UnauthorizedException
34 public function getChecksConfig() {
35 if (empty($this->checksConfig
)) {
36 $this->checksConfig
= Civi
::cache('checks')->get('checksConfig', []);
37 if (empty($this->checksConfig
)) {
38 $this->checksConfig
= StatusPreference
::get()->setCheckPermissions(FALSE)->execute()->indexBy('name');
41 return $this->checksConfig
;
45 * @param array $checksConfig
47 public function setChecksConfig(array $checksConfig) {
48 $this->checksConfig
= $checksConfig;
52 * Should these checks be run?
56 public function isEnabled() {
61 * Run all checks in this class.
64 * [CRM_Utils_Check_Message]
66 * @throws \API_Exception
67 * @throws \Civi\API\Exception\UnauthorizedException
69 public function checkAll() {
71 foreach (get_class_methods($this) as $method) {
72 // Note that we should check if the test is disabled BEFORE running it in case it's disabled for performance.
73 if ($method !== 'checkAll' && strpos($method, 'check') === 0 && !$this->isDisabled($method)) {
74 $messages = array_merge($messages, $this->$method());
81 * Is the specified check disabled.
83 * @param string $method
87 * @throws \API_Exception
88 * @throws \Civi\API\Exception\UnauthorizedException
90 public function isDisabled($method) {
92 $checks = $this->getChecksConfig();
93 if (!empty($checks[$method])) {
94 return (bool) empty($checks[$method]['is_active']);
97 catch (PEAR_Exception
$e) {
98 // if we're hitting this, DB migration to 5.19 probably hasn't run yet, so
99 // is_active doesn't exist. Ignore this error so the status check (which
100 // might warn about missing migrations!) still renders.
101 // TODO: remove at some point after 5.19
108 * Check if file exists on given URL.
111 * @param float|bool $timeoutOverride
114 * @throws \GuzzleHttp\Exception\GuzzleException
116 public function fileExists($url, $timeoutOverride = FALSE) {
117 // Timeout past in maybe 0 in which case we should still permit it (0 is infinite).
118 if (!$timeoutOverride && $timeoutOverride !== 0) {
119 $timeoutOverride = (float) Civi
::settings()->get('http_timeout');
123 $guzzleClient = new GuzzleHttp\
Client();
124 $guzzleResponse = $guzzleClient->request('GET', $url, array(
125 'timeout' => $timeoutOverride,
127 $fileExists = ($guzzleResponse->getStatusCode() == 200);
129 catch (Exception
$e) {
130 // At this stage we are not checking for variants of not being able to receive it.
131 // However, we might later enhance this to distinguish forbidden from a 500 error.