Commit | Line | Data |
---|---|---|
3a0d0bbd CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
3a0d0bbd | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
3a0d0bbd CW |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
b4628ea6 | 28 | use Civi\Api4\StatusPreference; |
4094935a | 29 | |
3a0d0bbd CW |
30 | /** |
31 | * | |
32 | * @package CRM | |
6b83d5bd | 33 | * @copyright CiviCRM LLC (c) 2004-2019 |
3a0d0bbd CW |
34 | */ |
35 | abstract class CRM_Utils_Check_Component { | |
36 | ||
b4628ea6 | 37 | /** |
38 | * @var array | |
39 | */ | |
40 | public $checksConfig = []; | |
41 | ||
42 | /** | |
43 | * Get the configured status checks. | |
44 | * | |
45 | * @return array | |
46 | * | |
47 | * @throws \API_Exception | |
48 | * @throws \Civi\API\Exception\UnauthorizedException | |
49 | */ | |
50 | public function getChecksConfig() { | |
51 | if (empty($this->checksConfig)) { | |
52 | $this->checksConfig = Civi::cache('checks')->get('checksConfig', []); | |
53 | if (empty($this->checksConfig)) { | |
54 | $this->checksConfig = StatusPreference::get()->setCheckPermissions(FALSE)->execute()->indexBy('name'); | |
55 | } | |
56 | } | |
57 | return $this->checksConfig; | |
58 | } | |
59 | ||
60 | /** | |
61 | * @param array $checksConfig | |
62 | */ | |
63 | public function setChecksConfig(array $checksConfig) { | |
64 | $this->checksConfig = $checksConfig; | |
65 | } | |
66 | ||
3a0d0bbd CW |
67 | /** |
68 | * Should these checks be run? | |
69 | * | |
70 | * @return bool | |
71 | */ | |
72 | public function isEnabled() { | |
73 | return TRUE; | |
74 | } | |
75 | ||
76 | /** | |
77 | * Run all checks in this class. | |
78 | * | |
79 | * @return array | |
80 | * [CRM_Utils_Check_Message] | |
b4628ea6 | 81 | * |
82 | * @throws \API_Exception | |
83 | * @throws \Civi\API\Exception\UnauthorizedException | |
3a0d0bbd CW |
84 | */ |
85 | public function checkAll() { | |
be2fb01f | 86 | $messages = []; |
3a0d0bbd | 87 | foreach (get_class_methods($this) as $method) { |
b4628ea6 | 88 | // Note that we should check if the test is disabled BEFORE running it in case it's disabled for performance. |
89 | if ($method !== 'checkAll' && strpos($method, 'check') === 0 && !$this->isDisabled($method)) { | |
3a0d0bbd CW |
90 | $messages = array_merge($messages, $this->$method()); |
91 | } | |
92 | } | |
93 | return $messages; | |
94 | } | |
d631cdc8 | 95 | |
b4628ea6 | 96 | /** |
97 | * Is the specified check disabled. | |
98 | * | |
99 | * @param string $method | |
100 | * | |
101 | * @return bool | |
102 | * | |
103 | * @throws \API_Exception | |
104 | * @throws \Civi\API\Exception\UnauthorizedException | |
105 | */ | |
106 | public function isDisabled($method) { | |
107 | ||
108 | $checks = $this->getChecksConfig(); | |
109 | if (!empty($checks[$method])) { | |
110 | return (bool) empty($checks[$method]['is_active']); | |
111 | } | |
112 | return FALSE; | |
113 | } | |
114 | ||
4094935a AP |
115 | /** |
116 | * Check if file exists on given URL. | |
117 | * | |
6714d8d2 | 118 | * @param string $url |
9679fb15 | 119 | * @param float|bool $timeoutOverride |
32e1f6cc | 120 | * |
4094935a | 121 | * @return bool |
b4628ea6 | 122 | * @throws \GuzzleHttp\Exception\GuzzleException |
4094935a | 123 | */ |
9679fb15 SL |
124 | public function fileExists($url, $timeoutOverride = FALSE) { |
125 | // Timeout past in maybe 0 in which case we should still permit it (0 is infinite). | |
126 | if (!$timeoutOverride && $timeoutOverride !== 0) { | |
127 | $timeoutOverride = (float) Civi::settings()->get('http_timeout'); | |
128 | } | |
4094935a AP |
129 | $fileExists = FALSE; |
130 | try { | |
131 | $guzzleClient = new GuzzleHttp\Client(); | |
132 | $guzzleResponse = $guzzleClient->request('GET', $url, array( | |
9679fb15 | 133 | 'timeout' => $timeoutOverride, |
4094935a AP |
134 | )); |
135 | $fileExists = ($guzzleResponse->getStatusCode() == 200); | |
136 | } | |
137 | catch (Exception $e) { | |
32e1f6cc | 138 | // At this stage we are not checking for variants of not being able to receive it. |
139 | // However, we might later enhance this to distinguish forbidden from a 500 error. | |
4094935a AP |
140 | } |
141 | return $fileExists; | |
142 | } | |
143 | ||
137d9cf0 | 144 | } |