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
{
22 * Get the configured status checks.
26 * @throws \API_Exception
27 * @throws \Civi\API\Exception\UnauthorizedException
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__
] = [];
36 Civi
::$statics[__FUNCTION__
] = (array) StatusPreference
::get(FALSE)
37 ->addWhere('domain_id', '=', 'current_domain')
38 ->execute()->indexBy('name');
41 return Civi
::$statics[__FUNCTION__
];
45 * Should these checks be run?
49 public function isEnabled() {
54 * Get the names of all check functions in this class
58 public function getAllChecks() {
59 return array_filter(get_class_methods($this), function($method) {
60 return $method !== 'checkAll' && strpos($method, 'check') === 0;
65 * Run all checks in this class.
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)
72 * @return CRM_Utils_Check_Message[]
74 * @throws API_Exception
75 * @throws \Civi\API\Exception\UnauthorizedException
77 public function checkAll($requestedChecks = [], $includeDisabled = FALSE) {
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));
89 * Is this check one of those requested
91 * @param string $method
92 * @param array $requestedChecks
95 private function isRequested($method, $requestedChecks) {
96 if (!$requestedChecks) {
99 foreach ($requestedChecks as $name) {
100 if (strpos($name, $method) === 0) {
108 * Is the specified check disabled.
110 * @param string $method
114 * @throws \API_Exception
115 * @throws \Civi\API\Exception\UnauthorizedException
117 public function isDisabled($method) {
118 $checks = $this->getChecksConfig();
119 if (isset($checks[$method]['is_active'])) {
120 return !$checks[$method]['is_active'];
126 * Check if file exists on given URL.
129 * @param float|bool $timeoutOverride
132 * @throws \GuzzleHttp\Exception\GuzzleException
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');
141 $guzzleClient = new GuzzleHttp\
Client();
142 $guzzleResponse = $guzzleClient->request('GET', $url, array(
143 'timeout' => $timeoutOverride,
145 $fileExists = ($guzzleResponse->getStatusCode() == 200);
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.