Merge pull request #16759 from eileenmcnaughton/fatal2
[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()->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
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 try {
92 $checks = $this->getChecksConfig();
93 if (!empty($checks[$method])) {
94 return (bool) empty($checks[$method]['is_active']);
95 }
96 }
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
104 return FALSE;
105 }
106
107 /**
108 * Check if file exists on given URL.
109 *
110 * @param string $url
111 * @param float|bool $timeoutOverride
112 *
113 * @return bool
114 * @throws \GuzzleHttp\Exception\GuzzleException
115 */
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 }
121 $fileExists = FALSE;
122 try {
123 $guzzleClient = new GuzzleHttp\Client();
124 $guzzleResponse = $guzzleClient->request('GET', $url, array(
125 'timeout' => $timeoutOverride,
126 ));
127 $fileExists = ($guzzleResponse->getStatusCode() == 200);
128 }
129 catch (Exception $e) {
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.
132 }
133 return $fileExists;
134 }
135
136 }