Merge pull request #23459 from darrick/dev/core#2300
[civicrm-core.git] / CRM / Utils / Check / Component.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 use Civi\Api4\StatusPreference;
13
14 /**
15 *
16 * @package CRM
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 */
19 abstract class CRM_Utils_Check_Component {
20
21 /**
22 * Get the configured status checks.
23 *
24 * @return array
25 *
26 * @throws \API_Exception
27 * @throws \Civi\API\Exception\UnauthorizedException
28 */
29 public function getChecksConfig() {
30 if (!isset(Civi::$statics[__FUNCTION__])) {
31 Civi::$statics[__FUNCTION__] = (array) StatusPreference::get(FALSE)
32 ->addWhere('domain_id', '=', 'current_domain')
33 ->execute()->indexBy('name');
34 }
35 return Civi::$statics[__FUNCTION__];
36 }
37
38 /**
39 * Should these checks be run?
40 *
41 * @return bool
42 */
43 public function isEnabled() {
44 return TRUE;
45 }
46
47 /**
48 * Get the names of all check functions in this class
49 *
50 * @return string[]
51 */
52 public function getAllChecks() {
53 return array_filter(get_class_methods($this), function($method) {
54 return $method !== 'checkAll' && strpos($method, 'check') === 0;
55 });
56 }
57
58 /**
59 * Run all checks in this class.
60 *
61 * @param array $requestedChecks
62 * Optionally specify the names of specific checks requested, or leave empty to run all
63 * @param bool $includeDisabled
64 * Run checks that have been explicitly disabled (default false)
65 *
66 * @return CRM_Utils_Check_Message[]
67 *
68 * @throws API_Exception
69 * @throws \Civi\API\Exception\UnauthorizedException
70 */
71 public function checkAll($requestedChecks = [], $includeDisabled = FALSE) {
72 $messages = [];
73 foreach ($this->getAllChecks() as $method) {
74 // Note that we should check if the test is disabled BEFORE running it in case it's disabled for performance.
75 if ($this->isRequested($method, $requestedChecks) && ($includeDisabled || !$this->isDisabled($method))) {
76 $messages = array_merge($messages, $this->$method($includeDisabled));
77 }
78 }
79 return $messages;
80 }
81
82 /**
83 * Is this check one of those requested
84 *
85 * @param string $method
86 * @param array $requestedChecks
87 * @return bool
88 */
89 private function isRequested($method, $requestedChecks) {
90 if (!$requestedChecks) {
91 return TRUE;
92 }
93 foreach ($requestedChecks as $name) {
94 if (strpos($name, $method) === 0) {
95 return TRUE;
96 }
97 }
98 return FALSE;
99 }
100
101 /**
102 * Is the specified check disabled.
103 *
104 * @param string $method
105 *
106 * @return bool
107 *
108 * @throws \API_Exception
109 * @throws \Civi\API\Exception\UnauthorizedException
110 */
111 public function isDisabled($method) {
112 $checks = $this->getChecksConfig();
113 if (isset($checks[$method]['is_active'])) {
114 return !$checks[$method]['is_active'];
115 }
116 return FALSE;
117 }
118
119 /**
120 * Check if file exists on given URL.
121 *
122 * @param string $url
123 * @param float|bool $timeoutOverride
124 *
125 * @return bool
126 * @throws \GuzzleHttp\Exception\GuzzleException
127 */
128 public function fileExists($url, $timeoutOverride = FALSE) {
129 // Timeout past in maybe 0 in which case we should still permit it (0 is infinite).
130 if (!$timeoutOverride && $timeoutOverride !== 0) {
131 $timeoutOverride = (float) Civi::settings()->get('http_timeout');
132 }
133 $fileExists = FALSE;
134 try {
135 $guzzleClient = new GuzzleHttp\Client();
136 $guzzleResponse = $guzzleClient->request('GET', $url, array(
137 'timeout' => $timeoutOverride,
138 ));
139 $fileExists = ($guzzleResponse->getStatusCode() == 200);
140 }
141 catch (Exception $e) {
142 // At this stage we are not checking for variants of not being able to receive it.
143 // However, we might later enhance this to distinguish forbidden from a 500 error.
144 }
145 return $fileExists;
146 }
147
148 }