Merge pull request #17926 from civicrm/5.28
[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 * @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(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
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]
65 *
66 * @throws \API_Exception
67 * @throws \Civi\API\Exception\UnauthorizedException
68 */
69 public function checkAll() {
70 $messages = [];
71 foreach (get_class_methods($this) as $method) {
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)) {
74 $messages = array_merge($messages, $this->$method());
75 }
76 }
77 return $messages;
78 }
79
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) {
91 $checks = $this->getChecksConfig();
92 if (isset($checks[$method]['is_active'])) {
93 return !$checks[$method]['is_active'];
94 }
95 return FALSE;
96 }
97
98 /**
99 * Check if file exists on given URL.
100 *
101 * @param string $url
102 * @param float|bool $timeoutOverride
103 *
104 * @return bool
105 * @throws \GuzzleHttp\Exception\GuzzleException
106 */
107 public function fileExists($url, $timeoutOverride = FALSE) {
108 // Timeout past in maybe 0 in which case we should still permit it (0 is infinite).
109 if (!$timeoutOverride && $timeoutOverride !== 0) {
110 $timeoutOverride = (float) Civi::settings()->get('http_timeout');
111 }
112 $fileExists = FALSE;
113 try {
114 $guzzleClient = new GuzzleHttp\Client();
115 $guzzleResponse = $guzzleClient->request('GET', $url, array(
116 'timeout' => $timeoutOverride,
117 ));
118 $fileExists = ($guzzleResponse->getStatusCode() == 200);
119 }
120 catch (Exception $e) {
121 // At this stage we are not checking for variants of not being able to receive it.
122 // However, we might later enhance this to distinguish forbidden from a 500 error.
123 }
124 return $fileExists;
125 }
126
127 }