Merge pull request #19628 from vakeesan26/master
[civicrm-core.git] / CRM / Utils / Check / Component / Env.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 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Utils_Check_Component_Env extends CRM_Utils_Check_Component {
18
19 /**
20 * @return CRM_Utils_Check_Message[]
21 */
22 public function checkPhpVersion() {
23 $messages = [];
24 $phpVersion = phpversion();
25
26 if (version_compare($phpVersion, CRM_Upgrade_Incremental_General::RECOMMENDED_PHP_VER) >= 0) {
27 $messages[] = new CRM_Utils_Check_Message(
28 __FUNCTION__,
29 ts('This system uses PHP version %1 which meets or exceeds the recommendation of %2.',
30 [
31 1 => $phpVersion,
32 2 => preg_replace(';^(\d+\.\d+(?:\.[1-9]\d*)?).*$;', '\1', CRM_Upgrade_Incremental_General::RECOMMENDED_PHP_VER),
33 ]),
34 ts('PHP Up-to-Date'),
35 \Psr\Log\LogLevel::INFO,
36 'fa-server'
37 );
38 }
39 elseif (version_compare($phpVersion, CRM_Upgrade_Incremental_General::MIN_RECOMMENDED_PHP_VER) >= 0) {
40 $messages[] = new CRM_Utils_Check_Message(
41 __FUNCTION__,
42 ts('This system uses PHP version %1. This meets the minimum recommendations and you do not need to upgrade immediately, but the preferred version is %2.',
43 [
44 1 => $phpVersion,
45 2 => CRM_Upgrade_Incremental_General::RECOMMENDED_PHP_VER,
46 ]),
47 ts('PHP Out-of-Date'),
48 \Psr\Log\LogLevel::NOTICE,
49 'fa-server'
50 );
51 }
52 elseif (version_compare($phpVersion, CRM_Upgrade_Incremental_General::MIN_INSTALL_PHP_VER) >= 0) {
53 $messages[] = new CRM_Utils_Check_Message(
54 __FUNCTION__,
55 ts('This system uses PHP version %1. This meets the minimum requirements for CiviCRM to function but is not recommended. At least PHP version %2 is recommended; the preferred version is %3.',
56 [
57 1 => $phpVersion,
58 2 => CRM_Upgrade_Incremental_General::MIN_RECOMMENDED_PHP_VER,
59 3 => preg_replace(';^(\d+\.\d+(?:\.[1-9]\d*)?).*$;', '\1', CRM_Upgrade_Incremental_General::RECOMMENDED_PHP_VER),
60 ]),
61 ts('PHP Out-of-Date'),
62 \Psr\Log\LogLevel::WARNING,
63 'fa-server'
64 );
65 }
66 else {
67 $messages[] = new CRM_Utils_Check_Message(
68 __FUNCTION__,
69 ts('This system uses PHP version %1. To ensure the continued operation of CiviCRM, upgrade your server now. At least PHP version %2 is recommended; the preferred version is %3.',
70 [
71 1 => $phpVersion,
72 2 => CRM_Upgrade_Incremental_General::MIN_RECOMMENDED_PHP_VER,
73 3 => preg_replace(';^(\d+\.\d+(?:\.[1-9]\d*)?).*$;', '\1', CRM_Upgrade_Incremental_General::RECOMMENDED_PHP_VER),
74 ]),
75 ts('PHP Out-of-Date'),
76 \Psr\Log\LogLevel::ERROR,
77 'fa-server'
78 );
79 }
80
81 return $messages;
82 }
83
84 /**
85 * @return CRM_Utils_Check_Message[]
86 */
87 public function checkPhpMysqli() {
88 $messages = [];
89
90 if (!extension_loaded('mysqli')) {
91 $messages[] = new CRM_Utils_Check_Message(
92 __FUNCTION__,
93 ts('Future versions of CiviCRM may require the PHP extension "%2". To ensure that your system will be compatible, please install it in advance. For more explanation, see <a href="%1">the announcement</a>.',
94 [
95 1 => 'https://civicrm.org/blog/totten/psa-please-verify-php-extension-mysqli',
96 2 => 'mysqli',
97 ]),
98 ts('Forward Compatibility: Enable "mysqli"'),
99 \Psr\Log\LogLevel::WARNING,
100 'fa-server'
101 );
102 }
103
104 return $messages;
105 }
106
107 /**
108 * Check that the MySQL time settings match the PHP time settings.
109 *
110 * @return CRM_Utils_Check_Message[]
111 */
112 public function checkMysqlTime() {
113 $messages = [];
114
115 $phpNow = date('Y-m-d H:i');
116 $sqlNow = CRM_Core_DAO::singleValueQuery("SELECT date_format(now(), '%Y-%m-%d %H:%i')");
117 if (!CRM_Utils_Time::isEqual($phpNow, $sqlNow, 2.5 * 60)) {
118 $messages[] = new CRM_Utils_Check_Message(
119 __FUNCTION__,
120 ts('Timestamps reported by MySQL (eg "%1") and PHP (eg "%2" ) are mismatched.', [
121 1 => $sqlNow,
122 2 => $phpNow,
123 ]) . '<br />' . CRM_Utils_System::docURL2('sysadmin/requirements/#mysql-time'),
124 ts('Timestamp Mismatch'),
125 \Psr\Log\LogLevel::ERROR,
126 'fa-server'
127 );
128 }
129
130 return $messages;
131 }
132
133 /**
134 * @return CRM_Utils_Check_Message[]
135 */
136 public function checkDebug() {
137 $config = CRM_Core_Config::singleton();
138 if ($config->debug) {
139 $message = new CRM_Utils_Check_Message(
140 __FUNCTION__,
141 ts('Warning: Debug is enabled in <a href="%1">system settings</a>. This should not be enabled on production servers.',
142 [1 => CRM_Utils_System::url('civicrm/admin/setting/debug', 'reset=1')]),
143 ts('Debug Mode Enabled'),
144 CRM_Core_Config::environment() == 'Production' ? \Psr\Log\LogLevel::WARNING : \Psr\Log\LogLevel::INFO,
145 'fa-bug'
146 );
147 $message->addAction(
148 ts('Disable Debug Mode'),
149 ts('Disable debug mode now?'),
150 'api3',
151 ['Setting', 'create', ['debug_enabled' => 0]]
152 );
153 return [$message];
154 }
155
156 return [];
157 }
158
159 /**
160 * @param bool $force
161 * @return CRM_Utils_Check_Message[]
162 */
163 public function checkOutboundMail($force = FALSE) {
164 $messages = [];
165
166 // CiviMail doesn't work in non-production environments; skip.
167 if (!$force && CRM_Core_Config::environment() != 'Production') {
168 return $messages;
169 }
170
171 $mailingInfo = Civi::settings()->get('mailing_backend');
172 if (($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB
173 || (defined('CIVICRM_MAIL_LOG') && CIVICRM_MAIL_LOG)
174 || $mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED
175 || $mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MOCK)
176 ) {
177 $messages[] = new CRM_Utils_Check_Message(
178 __FUNCTION__,
179 ts('Warning: Outbound email is disabled in <a href="%1">system settings</a>. Proper settings should be enabled on production servers.',
180 [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')]),
181 ts('Outbound Email Disabled'),
182 \Psr\Log\LogLevel::WARNING,
183 'fa-envelope'
184 );
185 }
186
187 return $messages;
188 }
189
190 /**
191 * Check that domain email and org name are set
192 * @param bool $force
193 * @return CRM_Utils_Check_Message[]
194 */
195 public function checkDomainNameEmail($force = FALSE) {
196 $messages = [];
197
198 // CiviMail doesn't work in non-production environments; skip.
199 if (!$force && CRM_Core_Config::environment() != 'Production') {
200 return $messages;
201 }
202
203 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail(TRUE);
204 $domain = CRM_Core_BAO_Domain::getDomain();
205 $domainName = $domain->name;
206 $fixEmailUrl = CRM_Utils_System::url("civicrm/admin/options/from_email_address", "&reset=1");
207 $fixDomainName = CRM_Utils_System::url("civicrm/admin/domain", "action=update&reset=1");
208
209 if (!$domainEmailAddress || $domainEmailAddress == 'info@EXAMPLE.ORG') {
210 if (!$domainName || $domainName == 'Default Domain Name') {
211 $msg = ts("Please enter your organization's <a href=\"%1\">name, primary address </a> and <a href=\"%2\">default FROM Email Address </a> (for system-generated emails).",
212 [
213 1 => $fixDomainName,
214 2 => $fixEmailUrl,
215 ]
216 );
217 }
218 else {
219 $msg = ts('Please enter a <a href="%1">default FROM Email Address</a> (for system-generated emails).',
220 [1 => $fixEmailUrl]);
221 }
222 }
223 elseif (!$domainName || $domainName == 'Default Domain Name') {
224 $msg = ts("Please enter your organization's <a href=\"%1\">name and primary address</a>.",
225 [1 => $fixDomainName]);
226 }
227
228 if (!empty($msg)) {
229 $messages[] = new CRM_Utils_Check_Message(
230 __FUNCTION__,
231 $msg,
232 ts('Organization Setup'),
233 \Psr\Log\LogLevel::WARNING,
234 'fa-check-square-o'
235 );
236 }
237
238 return $messages;
239 }
240
241 /**
242 * Checks if a default bounce handling mailbox is set up
243 * @param bool $force
244 * @return CRM_Utils_Check_Message[]
245 */
246 public function checkDefaultMailbox($force = FALSE) {
247 $messages = [];
248
249 // CiviMail doesn't work in non-production environments; skip.
250 if (!$force && CRM_Core_Config::environment() != 'Production') {
251 return $messages;
252 }
253
254 $config = CRM_Core_Config::singleton();
255
256 if (in_array('CiviMail', $config->enableComponents) &&
257 CRM_Core_BAO_MailSettings::defaultDomain() == "EXAMPLE.ORG"
258 ) {
259 $message = new CRM_Utils_Check_Message(
260 __FUNCTION__,
261 ts('Please configure a <a href="%1">default mailbox</a> for CiviMail.',
262 [1 => CRM_Utils_System::url('civicrm/admin/mailSettings', "reset=1")]),
263 ts('Configure Default Mailbox'),
264 \Psr\Log\LogLevel::WARNING,
265 'fa-envelope'
266 );
267 $message->addHelp(
268 ts('A default mailbox must be configured for email bounce processing.') . '<br />' .
269 CRM_Utils_System::docURL2('user/advanced-configuration/email-system-configuration/')
270 );
271 $messages[] = $message;
272 }
273
274 return $messages;
275 }
276
277 /**
278 * Checks if cron has run in the past hour (3600 seconds)
279 * @param bool $force
280 * @return CRM_Utils_Check_Message[]
281 * @throws CRM_Core_Exception
282 */
283 public function checkLastCron($force = FALSE) {
284 // TODO: Remove this check when MINIMUM_UPGRADABLE_VERSION goes to 4.7.
285 if (CRM_Utils_System::version() !== CRM_Core_BAO_Domain::version()) {
286 return [];
287 }
288
289 $messages = [];
290
291 // Cron doesn't work in non-production environments; skip.
292 if (!$force && CRM_Core_Config::environment() != 'Production') {
293 return $messages;
294 }
295
296 $statusPreference = new CRM_Core_DAO_StatusPreference();
297 $statusPreference->domain_id = CRM_Core_Config::domainID();
298 $statusPreference->name = __FUNCTION__;
299
300 $level = \Psr\Log\LogLevel::INFO;
301 $now = gmdate('U');
302
303 // Get timestamp of last cron run
304 if ($statusPreference->find(TRUE) && !empty($statusPreference->check_info)) {
305 $msg = ts('Last cron run at %1.', [1 => CRM_Utils_Date::customFormat(date('c', $statusPreference->check_info))]);
306 }
307 // If cron record doesn't exist, this is a new install. Make a placeholder record (prefs='new').
308 else {
309 $statusPreference = CRM_Core_BAO_StatusPreference::create([
310 'name' => __FUNCTION__,
311 'check_info' => $now,
312 'prefs' => 'new',
313 ]);
314 }
315 $lastCron = $statusPreference->check_info;
316
317 if ($statusPreference->prefs !== 'new' && $lastCron > $now - 3600) {
318 $title = ts('Cron Running OK');
319 }
320 else {
321 // If placeholder record found, give one day "grace period" for admin to set-up cron
322 if ($statusPreference->prefs === 'new') {
323 $title = ts('Set-up Cron');
324 $msg = ts('No cron runs have been recorded.');
325 // After 1 day (86400 seconds) increase the error level
326 $level = ($lastCron > $now - 86400) ? \Psr\Log\LogLevel::NOTICE : \Psr\Log\LogLevel::WARNING;
327 }
328 else {
329 $title = ts('Cron Not Running');
330 // After 1 day (86400 seconds) increase the error level
331 $level = ($lastCron > $now - 86400) ? \Psr\Log\LogLevel::WARNING : \Psr\Log\LogLevel::ERROR;
332 }
333 $msg .= '<p>' . ts('A cron job is required to execute scheduled jobs automatically.') .
334 '<br />' . CRM_Utils_System::docURL2('sysadmin/setup/jobs/') . '</p>';
335 }
336
337 $messages[] = new CRM_Utils_Check_Message(
338 __FUNCTION__,
339 $msg,
340 $title,
341 $level,
342 'fa-clock-o'
343 );
344 return $messages;
345 }
346
347 /**
348 * Recommend that sites use path-variables for their directories and URLs.
349 * @return CRM_Utils_Check_Message[]
350 */
351 public function checkUrlVariables() {
352 $messages = [];
353 $hasOldStyle = FALSE;
354 $settingNames = [
355 'userFrameworkResourceURL',
356 'imageUploadURL',
357 'customCSSURL',
358 'extensionsURL',
359 ];
360
361 foreach ($settingNames as $settingName) {
362 $settingValue = Civi::settings()->get($settingName);
363 if (!empty($settingValue) && $settingValue[0] != '[') {
364 $hasOldStyle = TRUE;
365 break;
366 }
367 }
368
369 if ($hasOldStyle) {
370 $message = new CRM_Utils_Check_Message(
371 __FUNCTION__,
372 ts('<a href="%1">Resource URLs</a> may use absolute paths, relative paths, or variables. Absolute paths are more difficult to maintain. To maximize portability, consider using a variable in each URL (eg "<tt>[cms.root]</tt>" or "<tt>[civicrm.files]</tt>").',
373 [1 => CRM_Utils_System::url('civicrm/admin/setting/url', "reset=1")]),
374 ts('Resource URLs: Make them portable'),
375 \Psr\Log\LogLevel::NOTICE,
376 'fa-server'
377 );
378 $messages[] = $message;
379 }
380
381 return $messages;
382 }
383
384 /**
385 * Recommend that sites use path-variables for their directories and URLs.
386 * @return CRM_Utils_Check_Message[]
387 */
388 public function checkDirVariables() {
389 $messages = [];
390 $hasOldStyle = FALSE;
391 $settingNames = [
392 'uploadDir',
393 'imageUploadDir',
394 'customFileUploadDir',
395 'customTemplateDir',
396 'customPHPPathDir',
397 'extensionsDir',
398 ];
399
400 foreach ($settingNames as $settingName) {
401 $settingValue = Civi::settings()->get($settingName);
402 if (!empty($settingValue) && $settingValue[0] != '[') {
403 $hasOldStyle = TRUE;
404 break;
405 }
406 }
407
408 if ($hasOldStyle) {
409 $message = new CRM_Utils_Check_Message(
410 __FUNCTION__,
411 ts('<a href="%1">Directories</a> may use absolute paths, relative paths, or variables. Absolute paths are more difficult to maintain. To maximize portability, consider using a variable in each directory (eg "<tt>[cms.root]</tt>" or "<tt>[civicrm.files]</tt>").',
412 [1 => CRM_Utils_System::url('civicrm/admin/setting/path', "reset=1")]),
413 ts('Directory Paths: Make them portable'),
414 \Psr\Log\LogLevel::NOTICE,
415 'fa-server'
416 );
417 $messages[] = $message;
418 }
419
420 return $messages;
421 }
422
423 /**
424 * Check that important directories are writable.
425 *
426 * @return CRM_Utils_Check_Message[]
427 */
428 public function checkDirsWritable() {
429 $notWritable = [];
430
431 $config = CRM_Core_Config::singleton();
432 $directories = [
433 'uploadDir' => ts('Temporary Files Directory'),
434 'imageUploadDir' => ts('Images Directory'),
435 'customFileUploadDir' => ts('Custom Files Directory'),
436 ];
437
438 foreach ($directories as $directory => $label) {
439 $file = CRM_Utils_File::createFakeFile($config->$directory);
440
441 if ($file === FALSE) {
442 $notWritable[] = "$label ({$config->$directory})";
443 }
444 else {
445 $dirWithSlash = CRM_Utils_File::addTrailingSlash($config->$directory);
446 unlink($dirWithSlash . $file);
447 }
448 }
449
450 $messages = [];
451
452 if (!empty($notWritable)) {
453 $messages[] = new CRM_Utils_Check_Message(
454 __FUNCTION__,
455 ts('The %1 is not writable. Please check your file permissions.', [
456 1 => implode(', ', $notWritable),
457 'count' => count($notWritable),
458 'plural' => 'The following directories are not writable: %1. Please check your file permissions.',
459 ]),
460 ts('Directory not writable', [
461 'count' => count($notWritable),
462 'plural' => 'Directories not writable',
463 ]),
464 \Psr\Log\LogLevel::ERROR,
465 'fa-ban'
466 );
467 }
468
469 return $messages;
470 }
471
472 /**
473 * Checks if new versions are available
474 * @param bool $force
475 * @return CRM_Utils_Check_Message[]
476 * @throws CRM_Core_Exception
477 */
478 public function checkVersion($force = FALSE) {
479 $messages = [];
480 try {
481 $vc = new CRM_Utils_VersionCheck();
482 $vc->initialize($force);
483 }
484 catch (Exception $e) {
485 $messages[] = new CRM_Utils_Check_Message(
486 'checkVersionError',
487 ts('Directory %1 is not writable. Please change your file permissions.',
488 [1 => dirname($vc->cacheFile)]),
489 ts('Directory not writable'),
490 \Psr\Log\LogLevel::ERROR,
491 'fa-times-circle-o'
492 );
493 return $messages;
494 }
495
496 // Show a notice if the version_check job is disabled
497 if (!$force && empty($vc->cronJob['is_active'])) {
498 $args = empty($vc->cronJob['id']) ? ['reset' => 1] : ['reset' => 1, 'action' => 'update', 'id' => $vc->cronJob['id']];
499 $messages[] = new CRM_Utils_Check_Message(
500 'checkVersionDisabled',
501 ts('The check for new versions of CiviCRM has been disabled. <a %1>Re-enable the scheduled job</a> to receive important security update notifications.', [1 => 'href="' . CRM_Utils_System::url('civicrm/admin/job', $args) . '"']),
502 ts('Update Check Disabled'),
503 \Psr\Log\LogLevel::NOTICE,
504 'fa-times-circle-o'
505 );
506 }
507
508 if ($vc->isInfoAvailable) {
509 foreach ($vc->getVersionMessages() ?? [] as $msg) {
510 $messages[] = new CRM_Utils_Check_Message(__FUNCTION__ . '_' . $msg['name'],
511 $msg['message'], $msg['title'], $msg['severity'], 'fa-cloud-upload');
512 }
513 }
514
515 return $messages;
516 }
517
518 /**
519 * Checks if extensions are set up properly
520 * @return CRM_Utils_Check_Message[]
521 */
522 public function checkExtensions() {
523 $messages = [];
524 $extensionSystem = CRM_Extension_System::singleton();
525 $mapper = $extensionSystem->getMapper();
526 $manager = $extensionSystem->getManager();
527
528 if ($extensionSystem->getDefaultContainer()) {
529 $basedir = $extensionSystem->getDefaultContainer()->baseDir;
530 }
531
532 if (empty($basedir)) {
533 // no extension directory
534 $messages[] = new CRM_Utils_Check_Message(
535 __FUNCTION__,
536 ts('Your extensions directory is not set. Click <a href="%1">here</a> to set the extensions directory.',
537 [1 => CRM_Utils_System::url('civicrm/admin/setting/path', 'reset=1')]),
538 ts('Directory not writable'),
539 \Psr\Log\LogLevel::NOTICE,
540 'fa-plug'
541 );
542 return $messages;
543 }
544
545 if (!is_dir($basedir)) {
546 $messages[] = new CRM_Utils_Check_Message(
547 __FUNCTION__,
548 ts('Your extensions directory path points to %1, which is not a directory. Please check your file system.',
549 [1 => $basedir]),
550 ts('Extensions directory incorrect'),
551 \Psr\Log\LogLevel::ERROR,
552 'fa-plug'
553 );
554 return $messages;
555 }
556 elseif (!is_writable($basedir)) {
557 $messages[] = new CRM_Utils_Check_Message(
558 __FUNCTION__ . 'Writable',
559 ts('Your extensions directory (%1) is read-only. If you would like to perform downloads or upgrades, then change the file permissions.',
560 [1 => $basedir]),
561 ts('Read-Only Extensions'),
562 \Psr\Log\LogLevel::NOTICE,
563 'fa-plug'
564 );
565 }
566
567 if (empty($extensionSystem->getDefaultContainer()->baseUrl)) {
568 $messages[] = new CRM_Utils_Check_Message(
569 __FUNCTION__ . 'URL',
570 ts('The extensions URL is not properly set. Please go to the <a href="%1">URL setting page</a> and correct it.',
571 [1 => CRM_Utils_System::url('civicrm/admin/setting/url', 'reset=1')]),
572 ts('Extensions url missing'),
573 \Psr\Log\LogLevel::ERROR,
574 'fa-plug'
575 );
576 }
577
578 if (!$extensionSystem->getBrowser()->isEnabled()) {
579 $messages[] = new CRM_Utils_Check_Message(
580 __FUNCTION__,
581 ts('Not checking remote URL for extensions since ext_repo_url is set to false.'),
582 ts('Extensions check disabled'),
583 \Psr\Log\LogLevel::NOTICE,
584 'fa-plug'
585 );
586 return $messages;
587 }
588
589 try {
590 $remotes = $extensionSystem->getBrowser()->getExtensions();
591 }
592 catch (CRM_Extension_Exception $e) {
593 $messages[] = new CRM_Utils_Check_Message(
594 __FUNCTION__,
595 $e->getMessage(),
596 ts('Extension download error'),
597 \Psr\Log\LogLevel::ERROR,
598 'fa-plug'
599 );
600 return $messages;
601 }
602
603 $keys = array_keys($manager->getStatuses());
604 sort($keys);
605 $updates = $errors = $okextensions = [];
606
607 foreach ($keys as $key) {
608 try {
609 $obj = $mapper->keyToInfo($key);
610 }
611 catch (CRM_Extension_Exception $ex) {
612 $errors[] = ts('Failed to read extension (%1). Please refresh the extension list.', [1 => $key]);
613 continue;
614 }
615 $row = CRM_Admin_Page_Extensions::createExtendedInfo($obj);
616 switch ($row['status']) {
617 case CRM_Extension_Manager::STATUS_INSTALLED_MISSING:
618 $errors[] = ts('%1 extension (%2) is installed but missing files.', [1 => $row['label'] ?? NULL, 2 => $key]);
619 break;
620
621 case CRM_Extension_Manager::STATUS_INSTALLED:
622 if (!empty($remotes[$key]) && version_compare($row['version'], $remotes[$key]->version, '<')) {
623 $updates[] = $row['label'] . ': ' . $mapper->getUpgradeLink($remotes[$key], $row);
624 }
625 else {
626 if (empty($row['label'])) {
627 $okextensions[] = $key;
628 }
629 else {
630 $okextensions[] = ts('%1: Version %2', [
631 1 => $row['label'],
632 2 => $row['version'],
633 ]);
634 }
635 }
636 break;
637 }
638 }
639
640 if (!$okextensions && !$updates && !$errors) {
641 $messages[] = new CRM_Utils_Check_Message(
642 __FUNCTION__ . 'Ok',
643 ts('No extensions installed. <a %1>Browse available extensions</a>.', [
644 1 => 'href="' . CRM_Utils_System::url('civicrm/admin/extensions', 'reset=1') . '"',
645 ]),
646 ts('Extensions'),
647 \Psr\Log\LogLevel::INFO,
648 'fa-plug'
649 );
650 }
651
652 if ($errors) {
653 $messages[] = new CRM_Utils_Check_Message(
654 __FUNCTION__ . 'Error',
655 '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>',
656 ts('Extension Error'),
657 \Psr\Log\LogLevel::ERROR,
658 'fa-plug'
659 );
660 }
661
662 if ($updates) {
663 $messages[] = new CRM_Utils_Check_Message(
664 __FUNCTION__ . 'Updates',
665 '<ul><li>' . implode('</li><li>', $updates) . '</li></ul>',
666 ts('Extension Update Available', ['plural' => '%count Extension Updates Available', 'count' => count($updates)]),
667 \Psr\Log\LogLevel::WARNING,
668 'fa-plug'
669 );
670 }
671
672 if ($okextensions) {
673 if ($updates || $errors) {
674 $message = ts('1 extension is up-to-date:', ['plural' => '%count extensions are up-to-date:', 'count' => count($okextensions)]);
675 }
676 else {
677 $message = ts('All extensions are up-to-date:');
678 }
679 $messages[] = new CRM_Utils_Check_Message(
680 __FUNCTION__ . 'Ok',
681 $message . '<ul><li>' . implode('</li><li>', $okextensions) . '</li></ul>',
682 ts('Extensions'),
683 \Psr\Log\LogLevel::INFO,
684 'fa-plug'
685 );
686 }
687
688 return $messages;
689 }
690
691 /**
692 * Checks if there are pending extension upgrades.
693 *
694 * @return CRM_Utils_Check_Message[]
695 */
696 public function checkExtensionUpgrades() {
697 if (CRM_Extension_Upgrades::hasPending()) {
698 $message = new CRM_Utils_Check_Message(
699 __FUNCTION__,
700 ts('Extension upgrades should be run as soon as possible.'),
701 ts('Extension Upgrades Pending'),
702 \Psr\Log\LogLevel::ERROR,
703 'fa-plug'
704 );
705 $message->addAction(
706 ts('Run Upgrades'),
707 ts('Run extension upgrades now?'),
708 'href',
709 ['path' => 'civicrm/admin/extensions/upgrade', 'query' => ['reset' => 1, 'destination' => CRM_Utils_System::url('civicrm/a/#/status')]]
710 );
711 return [$message];
712 }
713 return [];
714 }
715
716 /**
717 * Checks if CiviCRM database version is up-to-date
718 * @return CRM_Utils_Check_Message[]
719 */
720 public function checkDbVersion() {
721 $messages = [];
722 $dbVersion = CRM_Core_BAO_Domain::version();
723 $upgradeUrl = CRM_Utils_System::url("civicrm/upgrade", "reset=1");
724
725 if (!$dbVersion) {
726 // if db.ver missing
727 $messages[] = new CRM_Utils_Check_Message(
728 __FUNCTION__,
729 ts('Version information found to be missing in database. You will need to determine the correct version corresponding to your current database state.'),
730 ts('Database Version Missing'),
731 \Psr\Log\LogLevel::ERROR,
732 'fa-database'
733 );
734 }
735 elseif (!CRM_Utils_System::isVersionFormatValid($dbVersion)) {
736 $messages[] = new CRM_Utils_Check_Message(
737 __FUNCTION__,
738 ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.'),
739 ts('Database Version Invalid'),
740 \Psr\Log\LogLevel::ERROR,
741 'fa-database'
742 );
743 }
744 elseif (stripos($dbVersion, 'upgrade')) {
745 // if db.ver indicates a partially upgraded db
746 $messages[] = new CRM_Utils_Check_Message(
747 __FUNCTION__,
748 ts('Database check failed - the database looks to have been partially upgraded. You must reload the database with the backup and try the <a href=\'%1\'>upgrade process</a> again.', [1 => $upgradeUrl]),
749 ts('Database Partially Upgraded'),
750 \Psr\Log\LogLevel::ALERT,
751 'fa-database'
752 );
753 }
754 else {
755 // if db.ver < code.ver, time to upgrade
756 if (CRM_Core_BAO_Domain::isDBUpdateRequired()) {
757 $messages[] = new CRM_Utils_Check_Message(
758 __FUNCTION__,
759 ts('New codebase version detected. You must visit <a href=\'%1\'>upgrade screen</a> to upgrade the database.', [1 => $upgradeUrl]),
760 ts('Database Upgrade Required'),
761 \Psr\Log\LogLevel::ALERT,
762 'fa-database'
763 );
764 }
765
766 // if db.ver > code.ver, sth really wrong
767 $codeVersion = CRM_Utils_System::version();
768 if (version_compare($dbVersion, $codeVersion) > 0) {
769 $messages[] = new CRM_Utils_Check_Message(
770 __FUNCTION__,
771 ts('Your database is marked with an unexpected version number: %1. The v%2 codebase may not be compatible with your database state.
772 You will need to determine the correct version corresponding to your current database state. You may want to revert to the codebase
773 you were using until you resolve this problem.<br/>OR if this is a manual install from git, you might want to fix civicrm-version.php file.',
774 [1 => $dbVersion, 2 => $codeVersion]
775 ),
776 ts('Database In Unexpected Version'),
777 \Psr\Log\LogLevel::ERROR,
778 'fa-database'
779 );
780 }
781 }
782
783 return $messages;
784 }
785
786 /**
787 * Ensure that all CiviCRM tables are InnoDB
788 * @return CRM_Utils_Check_Message[]
789 */
790 public function checkDbEngine() {
791 $messages = [];
792
793 if (CRM_Core_DAO::isDBMyISAM(150)) {
794 $messages[] = new CRM_Utils_Check_Message(
795 __FUNCTION__,
796 ts('Your database is configured to use the MyISAM database engine. CiviCRM requires InnoDB. You will need to convert any MyISAM tables in your database to InnoDB. Using MyISAM tables will result in data integrity issues.'),
797 ts('MyISAM Database Engine'),
798 \Psr\Log\LogLevel::ERROR,
799 'fa-database'
800 );
801 }
802 return $messages;
803 }
804
805 /**
806 * Ensure reply id is set to any default value
807 * @param bool $force
808 * @return CRM_Utils_Check_Message[]
809 */
810 public function checkReplyIdForMailing($force = FALSE) {
811 $messages = [];
812
813 // CiviMail doesn't work in non-production environments; skip.
814 if (!$force && CRM_Core_Config::environment() != 'Production') {
815 return $messages;
816 }
817
818 if (!CRM_Mailing_PseudoConstant::defaultComponent('Reply', '')) {
819 $messages[] = new CRM_Utils_Check_Message(
820 __FUNCTION__,
821 ts('Reply Auto Responder is not set to any default value in <a %1>Headers, Footers, and Automated Messages</a>. This will disable the submit operation on any mailing created from CiviMail.', [1 => 'href="' . CRM_Utils_System::url('civicrm/admin/component', 'reset=1') . '"']),
822 ts('No Default value for Auto Responder.'),
823 \Psr\Log\LogLevel::WARNING,
824 'fa-reply'
825 );
826 }
827 return $messages;
828 }
829
830 /**
831 * Check for required mbstring extension
832 * @return CRM_Utils_Check_Message[]
833 */
834 public function checkMbstring() {
835 $messages = [];
836
837 if (!function_exists('mb_substr')) {
838 $messages[] = new CRM_Utils_Check_Message(
839 __FUNCTION__,
840 ts('The PHP Multibyte String extension is needed for CiviCRM to correctly handle user input among other functionality. Ask your system administrator to install it.'),
841 ts('Missing mbstring Extension'),
842 \Psr\Log\LogLevel::WARNING,
843 'fa-server'
844 );
845 }
846 return $messages;
847 }
848
849 /**
850 * Check if environment is Production.
851 * @return CRM_Utils_Check_Message[]
852 */
853 public function checkEnvironment() {
854 $messages = [];
855
856 $environment = CRM_Core_Config::environment();
857 if ($environment != 'Production') {
858 $messages[] = new CRM_Utils_Check_Message(
859 __FUNCTION__,
860 ts('The environment of this CiviCRM instance is set to \'%1\'. Certain functionality like scheduled jobs has been disabled.', [1 => $environment]),
861 ts('Non-Production Environment'),
862 \Psr\Log\LogLevel::NOTICE,
863 'fa-bug'
864 );
865 }
866 return $messages;
867 }
868
869 /**
870 * Check for utf8mb4 support by MySQL.
871 *
872 * @return CRM_Utils_Check_Message[]
873 */
874 public function checkMysqlUtf8mb4() {
875 $messages = [];
876
877 if (CRM_Core_DAO::getConnection()->phptype != 'mysqli') {
878 return $messages;
879 }
880
881 // Use mysqli_query() to avoid logging an error message.
882 $mb4testTableName = CRM_Utils_SQL_TempTable::build()->setCategory('utf8mb4test')->getName();
883 if (mysqli_query(CRM_Core_DAO::getConnection()->connection, 'CREATE TEMPORARY TABLE ' . $mb4testTableName . ' (id VARCHAR(255), PRIMARY KEY(id(255))) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC ENGINE=INNODB')) {
884 CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE ' . $mb4testTableName);
885 }
886 else {
887 $messages[] = new CRM_Utils_Check_Message(
888 __FUNCTION__,
889 ts("Future versions of CiviCRM may require MySQL to support utf8mb4 encoding. It is recommended, though not yet required. Please discuss with your server administrator about configuring your MySQL server for utf8mb4. CiviCRM's recommended configurations are in the System Administrator Guide") . '<br />' . CRM_Utils_System::docURL2('sysadmin/requirements/#mysql-configuration'),
890 ts('MySQL Emoji Support (utf8mb4)'),
891 \Psr\Log\LogLevel::WARNING,
892 'fa-database'
893 );
894 }
895 // Ensure that the MySQL driver supports utf8mb4 encoding.
896 $version = mysqli_get_client_info();
897 if (strpos($version, 'mysqlnd') !== FALSE) {
898 // The mysqlnd driver supports utf8mb4 starting at version 5.0.9.
899 $version = preg_replace('/^\D+([\d.]+).*/', '$1', $version);
900 if (version_compare($version, '5.0.9', '<')) {
901 $messages[] = new CRM_Utils_Check_Message(
902 __FUNCTION__ . 'mysqlnd',
903 ts('It is recommended, though not yet required, to upgrade your PHP MySQL driver (mysqlnd) to >= 5.0.9 for utf8mb4 support.'),
904 ts('PHP MySQL Driver (mysqlnd)'),
905 \Psr\Log\LogLevel::WARNING,
906 'fa-server'
907 );
908 }
909 }
910 else {
911 // The libmysqlclient driver supports utf8mb4 starting at version 5.5.3.
912 if (version_compare($version, '5.5.3', '<')) {
913 $messages[] = new CRM_Utils_Check_Message(
914 __FUNCTION__ . 'libmysqlclient',
915 ts('It is recommended, though not yet required, to upgrade your PHP MySQL driver (libmysqlclient) to >= 5.5.3 for utf8mb4 support.'),
916 ts('PHP MySQL Driver (libmysqlclient)'),
917 \Psr\Log\LogLevel::WARNING,
918 'fa-server'
919 );
920 }
921 }
922
923 return $messages;
924 }
925
926 public function checkMysqlVersion() {
927 $messages = [];
928 $version = CRM_Utils_SQL::getDatabaseVersion();
929 $minRecommendedVersion = CRM_Upgrade_Incremental_General::MIN_RECOMMENDED_MYSQL_VER;
930 $mariaDbRecommendedVersion = '10.1';
931 $upcomingCiviChangeVersion = '5.34';
932 if (version_compare(CRM_Utils_SQL::getDatabaseVersion(), $minRecommendedVersion, '<')) {
933 $messages[] = new CRM_Utils_Check_Message(
934 __FUNCTION__,
935 ts('To prepare for CiviCRM v%4, please upgrade MySQL. The recommended version will be MySQL v%2 or MariaDB v%3.', [
936 1 => $version,
937 2 => $minRecommendedVersion . '+',
938 3 => $mariaDbRecommendedVersion . '+',
939 4 => $upcomingCiviChangeVersion . '+',
940 ]),
941 ts('MySQL Out-of-Date'),
942 \Psr\Log\LogLevel::NOTICE,
943 'fa-server'
944 );
945 }
946 return $messages;
947 }
948
949 public function checkPHPIntlExists() {
950 $messages = [];
951 if (!extension_loaded('intl')) {
952 $messages[] = new CRM_Utils_Check_Message(
953 __FUNCTION__,
954 ts('This system currently does not have the PHP-Intl extension enabled. Please contact your system administrator about getting the extension enabled.'),
955 ts('Missing PHP Extension: INTL'),
956 \Psr\Log\LogLevel::WARNING,
957 'fa-server'
958 );
959 }
960 return $messages;
961 }
962
963 }