Commit | Line | Data |
---|---|---|
3a0d0bbd CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
3a0d0bbd | 5 | | | |
bc77d7c0 TO |
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 | | |
3a0d0bbd CW |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
b4628ea6 | 12 | use Civi\Api4\StatusPreference; |
4094935a | 13 | |
3a0d0bbd CW |
14 | /** |
15 | * | |
16 | * @package CRM | |
ca5cec67 | 17 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
3a0d0bbd CW |
18 | */ |
19 | abstract class CRM_Utils_Check_Component { | |
20 | ||
b4628ea6 | 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() { | |
1416a3c4 CW |
30 | if (empty(Civi::$statics[__FUNCTION__])) { |
31 | Civi::$statics[__FUNCTION__] = (array) StatusPreference::get(FALSE) | |
32 | ->addWhere('domain_id', '=', 'current_domain') | |
33 | ->execute()->indexBy('name'); | |
b4628ea6 | 34 | } |
1416a3c4 | 35 | return Civi::$statics[__FUNCTION__]; |
b4628ea6 | 36 | } |
37 | ||
3a0d0bbd CW |
38 | /** |
39 | * Should these checks be run? | |
40 | * | |
41 | * @return bool | |
42 | */ | |
43 | public function isEnabled() { | |
44 | return TRUE; | |
45 | } | |
46 | ||
675e2573 CW |
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 | ||
3a0d0bbd CW |
58 | /** |
59 | * Run all checks in this class. | |
60 | * | |
675e2573 CW |
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) | |
b4628ea6 | 65 | * |
675e2573 CW |
66 | * @return CRM_Utils_Check_Message[] |
67 | * | |
68 | * @throws API_Exception | |
b4628ea6 | 69 | * @throws \Civi\API\Exception\UnauthorizedException |
3a0d0bbd | 70 | */ |
675e2573 | 71 | public function checkAll($requestedChecks = [], $includeDisabled = FALSE) { |
be2fb01f | 72 | $messages = []; |
675e2573 | 73 | foreach ($this->getAllChecks() as $method) { |
b4628ea6 | 74 | // Note that we should check if the test is disabled BEFORE running it in case it's disabled for performance. |
675e2573 CW |
75 | if ($this->isRequested($method, $requestedChecks) && ($includeDisabled || !$this->isDisabled($method))) { |
76 | $messages = array_merge($messages, $this->$method($includeDisabled)); | |
3a0d0bbd CW |
77 | } |
78 | } | |
79 | return $messages; | |
80 | } | |
d631cdc8 | 81 | |
675e2573 CW |
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 | ||
b4628ea6 | 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) { | |
185a0768 CW |
112 | $checks = $this->getChecksConfig(); |
113 | if (isset($checks[$method]['is_active'])) { | |
114 | return !$checks[$method]['is_active']; | |
b4628ea6 | 115 | } |
116 | return FALSE; | |
117 | } | |
118 | ||
4094935a AP |
119 | /** |
120 | * Check if file exists on given URL. | |
121 | * | |
6714d8d2 | 122 | * @param string $url |
9679fb15 | 123 | * @param float|bool $timeoutOverride |
32e1f6cc | 124 | * |
4094935a | 125 | * @return bool |
b4628ea6 | 126 | * @throws \GuzzleHttp\Exception\GuzzleException |
4094935a | 127 | */ |
9679fb15 SL |
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 | } | |
4094935a AP |
133 | $fileExists = FALSE; |
134 | try { | |
135 | $guzzleClient = new GuzzleHttp\Client(); | |
136 | $guzzleResponse = $guzzleClient->request('GET', $url, array( | |
9679fb15 | 137 | 'timeout' => $timeoutOverride, |
4094935a AP |
138 | )); |
139 | $fileExists = ($guzzleResponse->getStatusCode() == 200); | |
140 | } | |
141 | catch (Exception $e) { | |
32e1f6cc | 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. | |
4094935a AP |
144 | } |
145 | return $fileExists; | |
146 | } | |
147 | ||
137d9cf0 | 148 | } |