3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 use Civi\Api4\StatusPreference
;
33 * @copyright CiviCRM LLC (c) 2004-2019
35 abstract class CRM_Utils_Check_Component
{
40 public $checksConfig = [];
43 * Get the configured status checks.
47 * @throws \API_Exception
48 * @throws \Civi\API\Exception\UnauthorizedException
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');
57 return $this->checksConfig
;
61 * @param array $checksConfig
63 public function setChecksConfig(array $checksConfig) {
64 $this->checksConfig
= $checksConfig;
68 * Should these checks be run?
72 public function isEnabled() {
77 * Run all checks in this class.
80 * [CRM_Utils_Check_Message]
82 * @throws \API_Exception
83 * @throws \Civi\API\Exception\UnauthorizedException
85 public function checkAll() {
87 foreach (get_class_methods($this) as $method) {
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)) {
90 $messages = array_merge($messages, $this->$method());
97 * Is the specified check disabled.
99 * @param string $method
103 * @throws \API_Exception
104 * @throws \Civi\API\Exception\UnauthorizedException
106 public function isDisabled($method) {
108 $checks = $this->getChecksConfig();
109 if (!empty($checks[$method])) {
110 return (bool) empty($checks[$method]['is_active']);
113 catch (PEAR_Exception
$e) {
114 // if we're hitting this, DB migration to 5.19 probably hasn't run yet, so
115 // is_active doesn't exist. Ignore this error so the status check (which
116 // might warn about missing migrations!) still renders.
117 // TODO: remove at some point after 5.19
124 * Check if file exists on given URL.
127 * @param float|bool $timeoutOverride
130 * @throws \GuzzleHttp\Exception\GuzzleException
132 public function fileExists($url, $timeoutOverride = FALSE) {
133 // Timeout past in maybe 0 in which case we should still permit it (0 is infinite).
134 if (!$timeoutOverride && $timeoutOverride !== 0) {
135 $timeoutOverride = (float) Civi
::settings()->get('http_timeout');
139 $guzzleClient = new GuzzleHttp\
Client();
140 $guzzleResponse = $guzzleClient->request('GET', $url, array(
141 'timeout' => $timeoutOverride,
143 $fileExists = ($guzzleResponse->getStatusCode() == 200);
145 catch (Exception
$e) {
146 // At this stage we are not checking for variants of not being able to receive it.
147 // However, we might later enhance this to distinguish forbidden from a 500 error.