Merge pull request #18139 from totten/master-region-bits
[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 // TODO: Remove this check when MINIMUM_UPGRADABLE_VERSION goes to 4.7.
32 if (CRM_Utils_System::version() !== CRM_Core_BAO_Domain::version() && !CRM_Core_DAO::checkTableExists('civicrm_status_pref')) {
33 Civi::$statics[__FUNCTION__] = [];
34 }
35 else {
36 Civi::$statics[__FUNCTION__] = (array) StatusPreference::get(FALSE)
37 ->addWhere('domain_id', '=', 'current_domain')
38 ->execute()->indexBy('name');
39 }
40 }
41 return Civi::$statics[__FUNCTION__];
42 }
43
44 /**
45 * Should these checks be run?
46 *
47 * @return bool
48 */
49 public function isEnabled() {
50 return TRUE;
51 }
52
53 /**
54 * Get the names of all check functions in this class
55 *
56 * @return string[]
57 */
58 public function getAllChecks() {
59 return array_filter(get_class_methods($this), function($method) {
60 return $method !== 'checkAll' && strpos($method, 'check') === 0;
61 });
62 }
63
64 /**
65 * Run all checks in this class.
66 *
67 * @param array $requestedChecks
68 * Optionally specify the names of specific checks requested, or leave empty to run all
69 * @param bool $includeDisabled
70 * Run checks that have been explicitly disabled (default false)
71 *
72 * @return CRM_Utils_Check_Message[]
73 *
74 * @throws API_Exception
75 * @throws \Civi\API\Exception\UnauthorizedException
76 */
77 public function checkAll($requestedChecks = [], $includeDisabled = FALSE) {
78 $messages = [];
79 foreach ($this->getAllChecks() as $method) {
80 // Note that we should check if the test is disabled BEFORE running it in case it's disabled for performance.
81 if ($this->isRequested($method, $requestedChecks) && ($includeDisabled || !$this->isDisabled($method))) {
82 $messages = array_merge($messages, $this->$method($includeDisabled));
83 }
84 }
85 return $messages;
86 }
87
88 /**
89 * Is this check one of those requested
90 *
91 * @param string $method
92 * @param array $requestedChecks
93 * @return bool
94 */
95 private function isRequested($method, $requestedChecks) {
96 if (!$requestedChecks) {
97 return TRUE;
98 }
99 foreach ($requestedChecks as $name) {
100 if (strpos($name, $method) === 0) {
101 return TRUE;
102 }
103 }
104 return FALSE;
105 }
106
107 /**
108 * Is the specified check disabled.
109 *
110 * @param string $method
111 *
112 * @return bool
113 *
114 * @throws \API_Exception
115 * @throws \Civi\API\Exception\UnauthorizedException
116 */
117 public function isDisabled($method) {
118 $checks = $this->getChecksConfig();
119 if (isset($checks[$method]['is_active'])) {
120 return !$checks[$method]['is_active'];
121 }
122 return FALSE;
123 }
124
125 /**
126 * Check if file exists on given URL.
127 *
128 * @param string $url
129 * @param float|bool $timeoutOverride
130 *
131 * @return bool
132 * @throws \GuzzleHttp\Exception\GuzzleException
133 */
134 public function fileExists($url, $timeoutOverride = FALSE) {
135 // Timeout past in maybe 0 in which case we should still permit it (0 is infinite).
136 if (!$timeoutOverride && $timeoutOverride !== 0) {
137 $timeoutOverride = (float) Civi::settings()->get('http_timeout');
138 }
139 $fileExists = FALSE;
140 try {
141 $guzzleClient = new GuzzleHttp\Client();
142 $guzzleResponse = $guzzleClient->request('GET', $url, array(
143 'timeout' => $timeoutOverride,
144 ));
145 $fileExists = ($guzzleResponse->getStatusCode() == 200);
146 }
147 catch (Exception $e) {
148 // At this stage we are not checking for variants of not being able to receive it.
149 // However, we might later enhance this to distinguish forbidden from a 500 error.
150 }
151 return $fileExists;
152 }
153
154 }