Merge pull request #7024 from colemanw/CRM-13823
[civicrm-core.git] / CRM / Utils / Check / Env.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 */
33 class CRM_Utils_Check_Env {
34
35 /**
36 * Run some sanity checks.
37 *
38 * @return array<CRM_Utils_Check_Message>
39 */
40 public function checkAll() {
41 $messages = array_merge(
42 $this->checkPhpVersion(),
43 $this->checkMysqlTime(),
44 $this->checkDebug(),
45 $this->checkOutboundMail(),
46 $this->checkDomainNameEmail(),
47 $this->checkDefaultMailbox(),
48 $this->checkLastCron(),
49 $this->checkVersion(),
50 $this->checkExtensions(),
51 $this->checkExtensionUpgrades(),
52 $this->checkDbVersion(),
53 $this->checkDbEngine()
54 );
55 return $messages;
56 }
57
58 /**
59 * @return array
60 */
61 public function checkPhpVersion() {
62 $messages = array();
63
64 if (version_compare(phpversion(), CRM_Upgrade_Incremental_General::MIN_RECOMMENDED_PHP_VER) < 0) {
65 $messages[] = new CRM_Utils_Check_Message(
66 'checkPhpVersion',
67 ts('This system uses PHP version %1. While this meets the minimum requirements for CiviCRM to function, upgrading to PHP version %2 or newer is recommended for maximum compatibility.',
68 array(
69 1 => phpversion(),
70 2 => CRM_Upgrade_Incremental_General::MIN_RECOMMENDED_PHP_VER,
71 )),
72 ts('PHP Out-of-Date'),
73 \Psr\Log\LogLevel::NOTICE
74 );
75 }
76 else {
77 $messages[] = new CRM_Utils_Check_Message(
78 'checkPhpVersion',
79 ts('This system uses PHP version %1 which meets or exceeds the minimum recommendation of %2.',
80 array(
81 1 => phpversion(),
82 2 => CRM_Upgrade_Incremental_General::MIN_RECOMMENDED_PHP_VER,
83 )),
84 ts('PHP Up-to-Date'),
85 \Psr\Log\LogLevel::INFO
86 );
87 }
88
89 return $messages;
90 }
91
92 /**
93 * Check that the MySQL time settings match the PHP time settings.
94 *
95 * @return array<CRM_Utils_Check_Message> an empty array, or a list of warnings
96 */
97 public function checkMysqlTime() {
98 $messages = array();
99
100 $phpNow = date('Y-m-d H:i');
101 $sqlNow = CRM_Core_DAO::singleValueQuery("SELECT date_format(now(), '%Y-%m-%d %H:%i')");
102 if (!CRM_Utils_Time::isEqual($phpNow, $sqlNow, 2.5 * 60)) {
103 $messages[] = new CRM_Utils_Check_Message(
104 'checkMysqlTime',
105 ts('Timestamps reported by MySQL (eg "%2") and PHP (eg "%3" ) are mismatched.<br /><a href="%1">Read more about this warning</a>', array(
106 1 => CRM_Utils_System::getWikiBaseURL() . 'checkMysqlTime',
107 2 => $sqlNow,
108 3 => $phpNow,
109 )),
110 ts('Timestamp Mismatch'),
111 \Psr\Log\LogLevel::ERROR
112 );
113 }
114
115 return $messages;
116 }
117
118 /**
119 * @return array
120 */
121 public function checkDebug() {
122 $messages = array();
123
124 $config = CRM_Core_Config::singleton();
125 if ($config->debug) {
126 $messages[] = new CRM_Utils_Check_Message(
127 'checkDebug',
128 ts('Warning: Debug is enabled in <a href="%1">system settings</a>. This should not be enabled on production servers.',
129 array(1 => CRM_Utils_System::url('civicrm/admin/setting/debug', 'reset=1'))),
130 ts('Debug Mode Enabled'),
131 \Psr\Log\LogLevel::WARNING
132 );
133 }
134
135 return $messages;
136 }
137
138 /**
139 * @return array
140 */
141 public function checkOutboundMail() {
142 $messages = array();
143
144 $mailingInfo = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'mailing_backend');
145 if (($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB
146 || (defined('CIVICRM_MAIL_LOG') && CIVICRM_MAIL_LOG)
147 || $mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED
148 || $mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MOCK)
149 ) {
150 $messages[] = new CRM_Utils_Check_Message(
151 'checkOutboundMail',
152 ts('Warning: Outbound email is disabled in <a href="%1">system settings</a>. Proper settings should be enabled on production servers.',
153 array(1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1'))),
154 ts('Outbound Email Disabled'),
155 \Psr\Log\LogLevel::WARNING
156 );
157 }
158
159 return $messages;
160 }
161
162 /**
163 * Check that domain email and org name are set
164 * @return array
165 */
166 public function checkDomainNameEmail() {
167 $messages = array();
168
169 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail(TRUE);
170 $domain = CRM_Core_BAO_Domain::getDomain();
171 $domainName = $domain->name;
172 $fixEmailUrl = CRM_Utils_System::url("civicrm/admin/domain", "action=update&reset=1");
173
174 if (!$domainEmailAddress || $domainEmailAddress == 'info@EXAMPLE.ORG') {
175 if (!$domainName || $domainName == 'Default Domain Name') {
176 $msg = ts("Please enter your organization's <a href=\"%1\">name, primary address, and default FROM Email Address</a> (for system-generated emails).",
177 array(1 => $fixEmailUrl));
178 }
179 else {
180 $msg = ts('Please enter a <a href="%1">default FROM Email Address</a> (for system-generated emails).',
181 array(1 => $fixEmailUrl));
182 }
183 }
184 elseif (!$domainName || $domainName == 'Default Domain Name') {
185 $msg = ts("Please enter your organization's <a href=\"%1\">name and primary address</a>.",
186 array(1 => $fixEmailUrl));
187 }
188
189 if (!empty($msg)) {
190 $messages[] = new CRM_Utils_Check_Message(
191 'checkDomainNameEmail',
192 $msg,
193 ts('Complete Setup'),
194 \Psr\Log\LogLevel::WARNING
195 );
196 }
197
198 return $messages;
199 }
200
201 /**
202 * Checks if a default bounce handling mailbox is set up
203 * @return array
204 */
205 public function checkDefaultMailbox() {
206 $messages = array();
207 $config = CRM_Core_Config::singleton();
208
209 if (in_array('CiviMail', $config->enableComponents) &&
210 CRM_Core_BAO_MailSettings::defaultDomain() == "EXAMPLE.ORG"
211 ) {
212 $message = new CRM_Utils_Check_Message(
213 'checkDefaultMailbox',
214 ts('Please configure a <a href="%1">default mailbox</a> for CiviMail.',
215 array(1 => CRM_Utils_System::url('civicrm/admin/mailSettings', "reset=1"))),
216 ts('Configure Default Mailbox'),
217 \Psr\Log\LogLevel::WARNING
218 );
219 $message->addHelp(ts('Learn more in the <a href="%1">user guide</a>', array(1 => 'http://book.civicrm.org/user/advanced-configuration/email-system-configuration/')));
220 $messages[] = $message;
221 }
222
223 return $messages;
224 }
225
226 /**
227 * Checks if cron has run in a reasonable amount of time
228 * @return array
229 */
230 public function checkLastCron() {
231 $messages = array();
232
233 $statusPreference = new CRM_Core_DAO_StatusPreference();
234 $statusPreference->domain_id = CRM_Core_Config::domainID();
235 $statusPreference->name = 'checkLastCron';
236
237 if ($statusPreference->find(TRUE)) {
238 $lastCron = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StatusPreference', $statusPreference->id, 'check_info');
239 $msg = ts('Last cron run at %1.', array(1 => CRM_Utils_Date::customFormat(date('c', $lastCron))));
240 }
241 else {
242 $lastCron = 0;
243 $msg = ts('No cron runs have been recorded.');
244 }
245
246 if ($lastCron > gmdate('U') - 3600) {
247 $messages[] = new CRM_Utils_Check_Message(
248 'checkLastCron',
249 $msg,
250 ts('Cron Running OK'),
251 \Psr\Log\LogLevel::INFO
252 );
253 }
254 elseif ($lastCron > gmdate('U') - 86400) {
255 $message = new CRM_Utils_Check_Message(
256 'checkLastCron',
257 $msg,
258 ts('Cron Not Running'),
259 \Psr\Log\LogLevel::WARNING
260 );
261 $message->addHelp(ts('Learn more in the <a href="%1">Administrator\'s Guide supplement</a>', array(1 => 'http://wiki.civicrm.org/confluence/display/CRMDOC/Managing+Scheduled+Jobs')));
262 $messages[] = $message;
263 }
264 else {
265 $message = new CRM_Utils_Check_Message(
266 'checkLastCron',
267 $msg,
268 ts('Cron Not Running'),
269 \Psr\Log\LogLevel::ERROR
270 );
271 $message->addHelp(ts('Learn more in the <a href="%1">Administrator\'s Guide supplement</a>', array(1 => 'http://wiki.civicrm.org/confluence/display/CRMDOC/Managing+Scheduled+Jobs')));
272 $messages[] = $message;
273 }
274
275 return $messages;
276 }
277
278 /**
279 * Checks if new versions are available
280 * @return array
281 */
282 public function checkVersion() {
283 $messages = array();
284
285 if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'versionAlert', NULL, 1)) {
286 $vc = CRM_Utils_VersionCheck::singleton();
287 $newerVersion = $vc->isNewerVersionAvailable();
288
289 if ($newerVersion['version']) {
290 $vInfo = array(
291 1 => $newerVersion['version'],
292 2 => $vc->localVersion,
293 );
294 if ($newerVersion['status'] == 'lts') {
295 $vInfo[1] .= ' ' . ts('(long-term support)'); // LTS = long-term support version
296 }
297
298 if ($newerVersion['upgrade'] == 'security') {
299 // For most new versions, just make them notice
300 $severity = \Psr\Log\LogLevel::CRITICAL;
301 $message = ts('New security release %1 is available. The site is currently running %2.', $vInfo);
302 }
303 elseif ($newerVersion['status'] == 'eol') {
304 // Warn about EOL
305 $severity = \Psr\Log\LogLevel::WARNING;
306 $message = ts('New version %1 is available. The site is currently running %2, which has reached its end of life.', $vInfo);
307 }
308 else {
309 // For most new versions, just make them notice
310 $severity = \Psr\Log\LogLevel::NOTICE;
311 $message = ts('New version %1 is available. The site is currently running %2.', $vInfo);
312 }
313 }
314 else {
315 $vNum = $vc->localVersion;
316 if ($newerVersion['status'] == 'lts') {
317 $vNum .= ' ' . ts('(long-term support)'); // LTS = long-term support version
318 }
319
320 $severity = \Psr\Log\LogLevel::INFO;
321 $message = ts('Version %1 is up-to-date.', array(1 => $vNum));
322 }
323
324 $messages[] = new CRM_Utils_Check_Message(
325 'checkVersion',
326 $message,
327 ts('Update Status'),
328 $severity
329 );
330 }
331 else {
332 $messages[] = new CRM_Utils_Check_Message(
333 'checkVersion',
334 ts('The check for new versions of CiviCRM has been disabled.'),
335 ts('Update Check Disabled'),
336 \Psr\Log\LogLevel::NOTICE
337 );
338 }
339
340 return $messages;
341 }
342
343 /**
344 * Checks if extensions are set up properly
345 * @return array
346 */
347 public function checkExtensions() {
348 $messages = array();
349 $extensionSystem = CRM_Extension_System::singleton();
350 $mapper = $extensionSystem->getMapper();
351 $manager = $extensionSystem->getManager();
352
353 if ($extensionSystem->getDefaultContainer()) {
354 $basedir = $extensionSystem->getDefaultContainer()->baseDir;
355 }
356
357 if (empty($basedir)) {
358 // no extension directory
359 $messages[] = new CRM_Utils_Check_Message(
360 'checkExtensions',
361 ts('Your extensions directory is not set. Click <a href="%1">here</a> to set the extensions directory.',
362 array(1 => CRM_Utils_System::url('civicrm/admin/setting/path', 'reset=1'))),
363 ts('Extensions directory not writable'),
364 \Psr\Log\LogLevel::NOTICE
365 );
366 return $messages;
367 }
368
369 if (!is_dir($basedir)) {
370 $messages[] = new CRM_Utils_Check_Message(
371 'checkExtensions',
372 ts('Your extensions directory path points to %1, which is not a directory. Please check your file system.',
373 array(1 => $basedir)),
374 ts('Extensions directory incorrect'),
375 \Psr\Log\LogLevel::ERROR
376 );
377 return $messages;
378 }
379 elseif (!is_writable($basedir)) {
380 $messages[] = new CRM_Utils_Check_Message(
381 'checkExtensions',
382 ts('Your extensions directory, %1, is not writable. Please change your file permissions.',
383 array(1 => $basedir)),
384 ts('Extensions directory not writable'),
385 \Psr\Log\LogLevel::ERROR
386 );
387 return $messages;
388 }
389
390 if (empty($extensionSystem->getDefaultContainer()->baseUrl)) {
391 $messages[] = new CRM_Utils_Check_Message(
392 'checkExtensions',
393 ts('The extensions URL is not properly set. Please go to the <a href="%1">URL setting page</a> and correct it.',
394 array(1 => CRM_Utils_System::url('civicrm/admin/setting/url', 'reset=1'))),
395 ts('Extensions directory not writable'),
396 \Psr\Log\LogLevel::ERROR
397 );
398 return $messages;
399 }
400
401 if (!$extensionSystem->getBrowser()->isEnabled()) {
402 $messages[] = new CRM_Utils_Check_Message(
403 'checkExtensions',
404 ts('Not checking remote URL for extensions since ext_repo_url is set to false.'),
405 ts('Extensions check disabled'),
406 \Psr\Log\LogLevel::NOTICE
407 );
408 return $messages;
409 }
410
411 try {
412 $remotes = $extensionSystem->getBrowser()->getExtensions();
413 }
414 catch (CRM_Extension_Exception $e) {
415 $messages[] = new CRM_Utils_Check_Message(
416 'checkExtensions',
417 $e->getMessage(),
418 ts('Extension download error'),
419 \Psr\Log\LogLevel::ERROR
420 );
421 return $messages;
422 }
423
424 if (!$remotes) {
425 // CRM-13141 There may not be any compatible extensions available for the requested CiviCRM version + CMS. If so, $extdir is empty so just return a notice.
426 $messages[] = new CRM_Utils_Check_Message(
427 'checkExtensions',
428 ts('There are currently no extensions on the CiviCRM public extension directory which are compatible with version %1. If you want to install an extension which is not marked as compatible, you may be able to <a %2>download and install extensions manually</a> (depending on access to your web server).', array(
429 1 => CRM_Utils_System::majorVersion(),
430 2 => 'href="http://wiki.civicrm.org/confluence/display/CRMDOC/Extensions"',
431 )),
432 ts('No Extensions Available for this Version'),
433 \Psr\Log\LogLevel::NOTICE
434 );
435 return $messages;
436 }
437
438 $keys = array_keys($manager->getStatuses());
439 sort($keys);
440 $severity = 1;
441 $msgArray = $okextensions = array();
442 foreach ($keys as $key) {
443 try {
444 $obj = $mapper->keyToInfo($key);
445 }
446 catch (CRM_Extension_Exception $ex) {
447 $severity = 4;
448 $msgArray[] = ts('Failed to read extension (%1). Please refresh the extension list.', array(1 => $key));
449 continue;
450 }
451 $row = CRM_Admin_Page_Extensions::createExtendedInfo($obj);
452 switch ($row['status']) {
453 case CRM_Extension_Manager::STATUS_INSTALLED_MISSING:
454 $severity = 4;
455 $msgArray[] = ts('%1 extension (%2) is installed but missing files.', array(1 => CRM_Utils_Array::value('label', $row), 2 => $key));
456 break;
457
458 case CRM_Extension_Manager::STATUS_INSTALLED:
459 if (CRM_Utils_Array::value($key, $remotes)) {
460 if (version_compare($row['version'], $remotes[$key]->version, '<')) {
461 $severity = ($severity < 3) ? 3 : $severity;
462 $msgArray[] = ts('%1 extension (%2) is upgradeable to version %3.', array(1 => CRM_Utils_Array::value('label', $row), 2 => $key, 3 => $remotes[$key]->version));
463 }
464 else {
465 $okextensions[] = CRM_Utils_Array::value('label', $row) ? "{$row['label']} ($key)" : $key;
466 }
467 }
468 else {
469 $okextensions[] = CRM_Utils_Array::value('label', $row) ? "{$row['label']} ($key)" : $key;
470 }
471 break;
472
473 case CRM_Extension_Manager::STATUS_UNINSTALLED:
474 case CRM_Extension_Manager::STATUS_DISABLED:
475 case CRM_Extension_Manager::STATUS_DISABLED_MISSING:
476 default:
477 }
478 }
479 $msg = implode(' ', $msgArray);
480 if (empty($msgArray)) {
481 $msg = (empty($okextensions)) ? ts('No extensions installed.') : ts('Extensions are up-to-date:') . ' ' . implode(', ', $okextensions);
482 }
483 elseif (!empty($okextensions)) {
484 $msg .= ' ' . ts('Other extensions are up-to-date:') . ' ' . implode(', ', $okextensions);
485 }
486
487 // OK, return several data rows
488 $messages[] = new CRM_Utils_Check_Message(
489 'checkExtensions',
490 $msg,
491 ts('Extension Updates'),
492 CRM_Utils_Check::severityMap($severity, TRUE)
493 );
494
495 return $messages;
496 }
497
498
499 /**
500 * Checks if extensions are set up properly
501 * @return array
502 */
503 public function checkExtensionUpgrades() {
504 $messages = array();
505
506 if (CRM_Extension_Upgrades::hasPending()) {
507 $messages[] = new CRM_Utils_Check_Message(
508 'checkExtensionUpgrades',
509 ts('Extension upgrades are pending. Please visit <a href="%1">the upgrade page</a> to run them.',
510 array(1 => CRM_Utils_System::url('civicrm/admin/extensions/upgrade', 'reset=1'))),
511 ts('Run Extension Upgrades'),
512 \Psr\Log\LogLevel::ERROR
513 );
514 }
515 return $messages;
516 }
517
518 /**
519 * Checks if CiviCRM database version is up-to-date
520 * @return array
521 */
522 public function checkDbVersion() {
523 $messages = array();
524 $dbVersion = CRM_Core_BAO_Domain::version();
525 $upgradeUrl = CRM_Utils_System::url("civicrm/upgrade", "reset=1");
526
527 if (!$dbVersion) {
528 // if db.ver missing
529 $messages[] = new CRM_Utils_Check_Message(
530 'checkDbVersion',
531 ts('Version information found to be missing in database. You will need to determine the correct version corresponding to your current database state.'),
532 ts('Database Version Missing'),
533 \Psr\Log\LogLevel::ERROR
534 );
535 }
536 elseif (!CRM_Utils_System::isVersionFormatValid($dbVersion)) {
537 $messages[] = new CRM_Utils_Check_Message(
538 'checkDbVersion',
539 ts('Database is marked with invalid version format. You may want to investigate this before you proceed further.'),
540 ts('Database Version Invalid'),
541 \Psr\Log\LogLevel::ERROR
542 );
543 }
544 elseif (stripos($dbVersion, 'upgrade')) {
545 // if db.ver indicates a partially upgraded db
546 $messages[] = new CRM_Utils_Check_Message(
547 'checkDbVersion',
548 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.', array(1 => $upgradeUrl)),
549 ts('Database Partially Upgraded'),
550 \Psr\Log\LogLevel::ALERT
551 );
552 }
553 else {
554 $codeVersion = CRM_Utils_System::version();
555
556 // if db.ver < code.ver, time to upgrade
557 if (version_compare($dbVersion, $codeVersion) < 0) {
558 $messages[] = new CRM_Utils_Check_Message(
559 'checkDbVersion',
560 ts('New codebase version detected. You must visit <a href=\'%1\'>upgrade screen</a> to upgrade the database.', array(1 => $upgradeUrl)),
561 ts('Database Upgrade Required'),
562 \Psr\Log\LogLevel::ALERT
563 );
564 }
565
566 // if db.ver > code.ver, sth really wrong
567 if (version_compare($dbVersion, $codeVersion) > 0) {
568 $messages[] = new CRM_Utils_Check_Message(
569 'checkDbVersion',
570 ts('Your database is marked with an unexpected version number: %1. The v%2 codebase may not be compatible with your database state.
571 You will need to determine the correct version corresponding to your current database state. You may want to revert to the codebase
572 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.',
573 array(1 => $dbVersion, 2 => $codeVersion)
574 ),
575 ts('Database In Unexpected Version'),
576 \Psr\Log\LogLevel::ERROR
577 );
578 }
579 }
580
581 return $messages;
582 }
583
584 /**
585 * ensure that all CiviCRM tables are InnoDB
586 * @return array
587 */
588 public function checkDbEngine() {
589 $messages = array();
590
591 if (CRM_Core_DAO::isDBMyISAM(150)) {
592 $messages[] = new CRM_Utils_Check_Message(
593 'checkDbEngine',
594 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.'),
595 ts('MyISAM Database Engine'),
596 \Psr\Log\LogLevel::ERROR
597 );
598 }
599 return $messages;
600 }
601
602 }