Merge pull request #17943 from jitendrapurohit/core-1906
[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 (empty(Civi::$statics[__FUNCTION__])) {
31 Civi::$statics[__FUNCTION__] = (array) StatusPreference::get(FALSE)
32 ->addWhere('domain_id', '=', 'current_domain')
33 ->execute()->indexBy('name');
34 }
35 return Civi::$statics[__FUNCTION__];
36 }
37
38 /**
39 * Should these checks be run?
40 *
41 * @return bool
42 */
43 public function isEnabled() {
44 return TRUE;
45 }
46
47 /**
48 * Run all checks in this class.
49 *
50 * @return array
51 * [CRM_Utils_Check_Message]
52 *
53 * @throws \API_Exception
54 * @throws \Civi\API\Exception\UnauthorizedException
55 */
56 public function checkAll() {
57 $messages = [];
58 foreach (get_class_methods($this) as $method) {
59 // Note that we should check if the test is disabled BEFORE running it in case it's disabled for performance.
60 if ($method !== 'checkAll' && strpos($method, 'check') === 0 && !$this->isDisabled($method)) {
61 $messages = array_merge($messages, $this->$method());
62 }
63 }
64 return $messages;
65 }
66
67 /**
68 * Is the specified check disabled.
69 *
70 * @param string $method
71 *
72 * @return bool
73 *
74 * @throws \API_Exception
75 * @throws \Civi\API\Exception\UnauthorizedException
76 */
77 public function isDisabled($method) {
78 $checks = $this->getChecksConfig();
79 if (isset($checks[$method]['is_active'])) {
80 return !$checks[$method]['is_active'];
81 }
82 return FALSE;
83 }
84
85 /**
86 * Check if file exists on given URL.
87 *
88 * @param string $url
89 * @param float|bool $timeoutOverride
90 *
91 * @return bool
92 * @throws \GuzzleHttp\Exception\GuzzleException
93 */
94 public function fileExists($url, $timeoutOverride = FALSE) {
95 // Timeout past in maybe 0 in which case we should still permit it (0 is infinite).
96 if (!$timeoutOverride && $timeoutOverride !== 0) {
97 $timeoutOverride = (float) Civi::settings()->get('http_timeout');
98 }
99 $fileExists = FALSE;
100 try {
101 $guzzleClient = new GuzzleHttp\Client();
102 $guzzleResponse = $guzzleClient->request('GET', $url, array(
103 'timeout' => $timeoutOverride,
104 ));
105 $fileExists = ($guzzleResponse->getStatusCode() == 200);
106 }
107 catch (Exception $e) {
108 // At this stage we are not checking for variants of not being able to receive it.
109 // However, we might later enhance this to distinguish forbidden from a 500 error.
110 }
111 return $fileExists;
112 }
113
114 }