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 | * @var array | |
23 | */ | |
24 | public $checksConfig = []; | |
25 | ||
26 | /** | |
27 | * Get the configured status checks. | |
28 | * | |
29 | * @return array | |
30 | * | |
31 | * @throws \API_Exception | |
32 | * @throws \Civi\API\Exception\UnauthorizedException | |
33 | */ | |
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'); | |
39 | } | |
40 | } | |
41 | return $this->checksConfig; | |
42 | } | |
43 | ||
44 | /** | |
45 | * @param array $checksConfig | |
46 | */ | |
47 | public function setChecksConfig(array $checksConfig) { | |
48 | $this->checksConfig = $checksConfig; | |
49 | } | |
50 | ||
3a0d0bbd CW |
51 | /** |
52 | * Should these checks be run? | |
53 | * | |
54 | * @return bool | |
55 | */ | |
56 | public function isEnabled() { | |
57 | return TRUE; | |
58 | } | |
59 | ||
60 | /** | |
61 | * Run all checks in this class. | |
62 | * | |
63 | * @return array | |
64 | * [CRM_Utils_Check_Message] | |
b4628ea6 | 65 | * |
66 | * @throws \API_Exception | |
67 | * @throws \Civi\API\Exception\UnauthorizedException | |
3a0d0bbd CW |
68 | */ |
69 | public function checkAll() { | |
be2fb01f | 70 | $messages = []; |
3a0d0bbd | 71 | foreach (get_class_methods($this) as $method) { |
b4628ea6 | 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)) { | |
3a0d0bbd CW |
74 | $messages = array_merge($messages, $this->$method()); |
75 | } | |
76 | } | |
77 | return $messages; | |
78 | } | |
d631cdc8 | 79 | |
b4628ea6 | 80 | /** |
81 | * Is the specified check disabled. | |
82 | * | |
83 | * @param string $method | |
84 | * | |
85 | * @return bool | |
86 | * | |
87 | * @throws \API_Exception | |
88 | * @throws \Civi\API\Exception\UnauthorizedException | |
89 | */ | |
90 | public function isDisabled($method) { | |
5a74bbd5 PF |
91 | try { |
92 | $checks = $this->getChecksConfig(); | |
93 | if (!empty($checks[$method])) { | |
94 | return (bool) empty($checks[$method]['is_active']); | |
95 | } | |
b4628ea6 | 96 | } |
5a74bbd5 PF |
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 | |
102 | } | |
103 | ||
b4628ea6 | 104 | return FALSE; |
105 | } | |
106 | ||
4094935a AP |
107 | /** |
108 | * Check if file exists on given URL. | |
109 | * | |
6714d8d2 | 110 | * @param string $url |
9679fb15 | 111 | * @param float|bool $timeoutOverride |
32e1f6cc | 112 | * |
4094935a | 113 | * @return bool |
b4628ea6 | 114 | * @throws \GuzzleHttp\Exception\GuzzleException |
4094935a | 115 | */ |
9679fb15 SL |
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'); | |
120 | } | |
4094935a AP |
121 | $fileExists = FALSE; |
122 | try { | |
123 | $guzzleClient = new GuzzleHttp\Client(); | |
124 | $guzzleResponse = $guzzleClient->request('GET', $url, array( | |
9679fb15 | 125 | 'timeout' => $timeoutOverride, |
4094935a AP |
126 | )); |
127 | $fileExists = ($guzzleResponse->getStatusCode() == 200); | |
128 | } | |
129 | catch (Exception $e) { | |
32e1f6cc | 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. | |
4094935a AP |
132 | } |
133 | return $fileExists; | |
134 | } | |
135 | ||
137d9cf0 | 136 | } |