3 namespace Civi\Install
;
7 * @package Civi\Install
12 * Requirement severity -- Requirement successfully met.
14 const REQUIREMENT_OK
= 0;
17 * Requirement severity -- Warning condition; proceed but flag warning.
19 const REQUIREMENT_WARNING
= 1;
22 * Requirement severity -- Error condition; abort installation.
24 const REQUIREMENT_ERROR
= 2;
29 protected $system_checks = [
31 'checkMysqlConnectExists',
32 'checkJsonEncodeExists',
33 'checkMultibyteExists',
36 protected $system_checks_web = [
37 'checkServerVariables',
40 protected $database_checks = [
41 'checkMysqlConnection',
44 'checkMysqlTempTables',
45 'checkMySQLAutoIncrementIncrementOne',
47 'checkMysqlThreadStack',
48 'checkMysqlLockTables',
53 * Run all requirements tests.
55 * @param array $config
56 * An array with two keys:
61 * An array of check summaries. Each array contains the keys 'title', 'severity', and 'details'.
63 public function checkAll(array $config) {
64 if (!class_exists('\CRM_Utils_SQL_TempTable')) {
65 require_once dirname(__FILE__
) . '/../../CRM/Utils/SQL/TempTable.php';
67 return array_merge($this->checkSystem($config['file_paths']), $this->checkDatabase($config['db_config']));
71 * Check system requirements are met, such as sufficient memory,
72 * necessary file paths are writable and required php extensions
75 * @param array $file_paths
76 * An array of file paths that will be checked to confirm they
81 public function checkSystem(array $file_paths) {
84 $errors[] = $this->checkFilepathIsWritable($file_paths);
85 foreach ($this->system_checks
as $check) {
86 $errors[] = $this->$check();
89 if (PHP_SAPI
!== 'cli') {
90 foreach ($this->system_checks_web
as $check) {
91 $errors[] = $this->$check();
99 * Check database connection, database version and other
100 * database requirements are met.
102 * @param array $db_config
103 * An array with keys:
104 * - host (with optional port specified eg. localhost:12345)
105 * - database (name of database to select)
111 public function checkDatabase(array $db_config) {
114 foreach ($this->database_checks
as $check) {
115 $errors[] = $this->$check($db_config);
122 * Generates a mysql connection
124 * @param $db_config array
125 * @return object mysqli connection
127 protected function connect($db_config) {
129 if (!empty($db_config['host'])) {
130 $host = $db_config['host'];
132 elseif (!empty($db_config['server'])) {
133 $host = $db_config['server'];
135 if (empty($db_config['ssl_params'])) {
136 $conn = @mysqli_connect
($host, $db_config['username'], $db_config['password'], $db_config['database'], !empty($db_config['port']) ?
$db_config['port'] : NULL);
140 $init = mysqli_init();
143 $db_config['ssl_params']['key'] ??
NULL,
144 $db_config['ssl_params']['cert'] ??
NULL,
145 $db_config['ssl_params']['ca'] ??
NULL,
146 $db_config['ssl_params']['capath'] ??
NULL,
147 $db_config['ssl_params']['cipher'] ??
NULL
149 if (@mysqli_real_connect
($init, $host, $db_config['username'], $db_config['password'], $db_config['database'], (!empty($db_config['port']) ?
$db_config['port'] : NULL), NULL, MYSQLI_CLIENT_SSL
)) {
157 * Check configured php Memory.
160 public function checkMemory() {
161 $min = 1024 * 1024 * 32;
162 $recommended = 1024 * 1024 * 64;
164 $mem = $this->getPHPMemory();
165 $mem_string = ini_get('memory_limit');
168 'title' => 'CiviCRM memory check',
169 'severity' => $this::REQUIREMENT_OK
,
170 'details' => "You have $mem_string allocated (minimum 32Mb, recommended 64Mb)",
173 if ($mem < $min && $mem > 0) {
174 $results['severity'] = $this::REQUIREMENT_ERROR
;
176 elseif ($mem < $recommended && $mem != 0) {
177 $results['severity'] = $this::REQUIREMENT_WARNING
;
180 $results['details'] = "Cannot determine PHP memory allocation. Install only if you're sure you've allocated at least 32 MB.";
181 $results['severity'] = $this::REQUIREMENT_WARNING
;
188 * Get Configured PHP memory.
191 protected function getPHPMemory() {
192 $memString = ini_get("memory_limit");
194 switch (strtolower(substr($memString, -1))) {
196 return round(substr($memString, 0, -1) * 1024);
199 return round(substr($memString, 0, -1) * 1024 * 1024);
202 return round(substr($memString, 0, -1) * 1024 * 1024 * 1024);
205 return round($memString);
212 public function checkServerVariables() {
214 'title' => 'CiviCRM PHP server variables',
215 'severity' => $this::REQUIREMENT_OK
,
216 'details' => 'The required $_SERVER variables are set',
219 $required_variables = ['SCRIPT_NAME', 'HTTP_HOST', 'SCRIPT_FILENAME'];
222 foreach ($required_variables as $required_variable) {
223 if (empty($_SERVER[$required_variable])) {
224 $missing[] = '$_SERVER[' . $required_variable . ']';
229 $results['severity'] = $this::REQUIREMENT_ERROR
;
230 $results['details'] = 'The following PHP variables are not set: ' . implode(', ', $missing);
239 public function checkJsonEncodeExists() {
241 'title' => 'CiviCRM JSON encoding support',
242 'severity' => $this::REQUIREMENT_OK
,
243 'details' => 'Function json_encode() found',
245 if (!function_exists('json_encode')) {
246 $results['severity'] = $this::REQUIREMENT_ERROR
;
247 $results['details'] = 'Function json_encode() does not exist';
254 * CHeck that PHP Multibyte functions are enabled.
257 public function checkMultibyteExists() {
259 'title' => 'CiviCRM MultiByte encoding support',
260 'severity' => $this::REQUIREMENT_OK
,
261 'details' => 'PHP Multibyte etension found',
263 if (!function_exists('mb_substr')) {
264 $results['severity'] = $this::REQUIREMENT_ERROR
;
265 $results['details'] = 'PHP Multibyte extension has not been installed and enabled';
274 public function checkMysqlConnectExists() {
276 'title' => 'CiviCRM MySQL check',
277 'severity' => $this::REQUIREMENT_OK
,
278 'details' => 'Function mysqli_connect() found',
280 if (!function_exists('mysqli_connect')) {
281 $results['severity'] = $this::REQUIREMENT_ERROR
;
282 $results['details'] = 'Function mysqli_connect() does not exist';
289 * @param array $db_config
293 public function checkMysqlConnection(array $db_config) {
295 'title' => 'CiviCRM MySQL connection',
296 'severity' => $this::REQUIREMENT_OK
,
297 'details' => "Connected",
300 $conn = $this->connect($db_config);
303 $results['details'] = mysqli_connect_error();
304 $results['severity'] = $this::REQUIREMENT_ERROR
;
308 if (!@mysqli_select_db
($conn, $db_config['database'])) {
309 $results['details'] = mysqli_error($conn);
310 $results['severity'] = $this::REQUIREMENT_ERROR
;
318 * @param array $db_config
322 public function checkMysqlVersion(array $db_config) {
323 if (!class_exists('\CRM_Upgrade_Incremental_General')) {
324 require_once dirname(__FILE__
) . '/../../CRM/Upgrade/Incremental/General.php';
326 $min = \CRM_Upgrade_Incremental_General
::MIN_INSTALL_MYSQL_VER
;
328 'title' => 'CiviCRM MySQL Version',
329 'severity' => $this::REQUIREMENT_OK
,
332 $conn = $this->connect($db_config);
333 if (!$conn ||
!($info = mysqli_get_server_info($conn))) {
334 $results['severity'] = $this::REQUIREMENT_WARNING
;
335 $results['details'] = "Cannot determine the version of MySQL installed. Please ensure at least version {$min} is installed.";
339 $versionDetails = mysqli_query($conn, 'SELECT version() as version')->fetch_assoc();
340 if (version_compare($versionDetails['version'], $min) == -1) {
341 $results['severity'] = $this::REQUIREMENT_ERROR
;
342 $results['details'] = "MySQL version is {$info}; minimum required is {$min}";
346 $results['details'] = "MySQL version is {$info}";
351 * @param array $db_config
355 public function checkMysqlInnodb(array $db_config) {
357 'title' => 'CiviCRM InnoDB support',
358 'severity' => $this::REQUIREMENT_ERROR
,
359 'details' => 'Could not determine if MySQL has InnoDB support. Assuming none.',
362 $conn = $this->connect($db_config);
367 $innodb_support = FALSE;
368 $result = mysqli_query($conn, "SHOW ENGINES");
369 while ($values = mysqli_fetch_array($result)) {
370 if ($values['Engine'] == 'InnoDB') {
371 if (strtolower($values['Support']) == 'yes' ||
strtolower($values['Support']) == 'default') {
372 $innodb_support = TRUE;
378 if ($innodb_support) {
379 $results['severity'] = $this::REQUIREMENT_OK
;
380 $results['details'] = 'MySQL supports InnoDB';
386 * @param array $db_config
390 public function checkMysqlTempTables(array $db_config) {
392 'title' => 'CiviCRM MySQL Temp Tables',
393 'severity' => $this::REQUIREMENT_OK
,
394 'details' => 'MySQL server supports temporary tables',
397 $conn = $this->connect($db_config);
399 $results['severity'] = $this::REQUIREMENT_ERROR
;
400 $results['details'] = "Could not connect to database";
404 if (!@mysqli_select_db
($conn, $db_config['database'])) {
405 $results['severity'] = $this::REQUIREMENT_ERROR
;
406 $results['details'] = "Could not select the database";
409 $temporaryTableName = \CRM_Utils_SQL_TempTable
::build()->setCategory('install')->getName();
410 $r = mysqli_query($conn, 'CREATE TEMPORARY TABLE ' . $temporaryTableName . ' (test text)');
412 $results['severity'] = $this::REQUIREMENT_ERROR
;
413 $results['details'] = "Database does not support creation of temporary tables";
417 mysqli_query($conn, 'DROP TEMPORARY TABLE ' . $temporaryTableName);
426 public function checkMysqlTrigger($db_config) {
428 'title' => 'CiviCRM MySQL Trigger',
429 'severity' => $this::REQUIREMENT_OK
,
430 'details' => 'Database supports MySQL triggers',
433 $conn = $this->connect($db_config);
435 $results['severity'] = $this::REQUIREMENT_ERROR
;
436 $results['details'] = 'Could not connect to database';
440 if (!@mysqli_select_db
($conn, $db_config['database'])) {
441 $results['severity'] = $this::REQUIREMENT_ERROR
;
442 $results['details'] = "Could not select the database";
446 $r = mysqli_query($conn, 'CREATE TABLE civicrm_install_temp_table_test (test text)');
448 $results['severity'] = $this::REQUIREMENT_ERROR
;
449 $results['details'] = 'Could not create a table to run test';
453 $r = mysqli_query($conn, 'CREATE TRIGGER civicrm_install_temp_table_test_trigger BEFORE INSERT ON civicrm_install_temp_table_test FOR EACH ROW BEGIN END');
455 $results['severity'] = $this::REQUIREMENT_ERROR
;
456 $results['details'] = 'Database does not support creation of triggers';
459 mysqli_query($conn, 'DROP TRIGGER civicrm_install_temp_table_test_trigger');
462 mysqli_query($conn, 'DROP TABLE civicrm_install_temp_table_test');
467 * @param array $db_config
471 public function checkMySQLAutoIncrementIncrementOne(array $db_config) {
473 'title' => 'CiviCRM MySQL AutoIncrementIncrement',
474 'severity' => $this::REQUIREMENT_OK
,
475 'details' => 'MySQL server auto_increment_increment is 1',
478 $conn = $this->connect($db_config);
480 $results['severity'] = $this::REQUIREMENT_ERROR
;
481 $results['details'] = 'Could not connect to database';
485 $r = mysqli_query($conn, "SHOW variables like 'auto_increment_increment'");
487 $results['severity'] = $this::REQUIREMENT_ERROR
;
488 $results['details'] = 'Could not query database server variables';
492 $values = mysqli_fetch_row($r);
493 if ($values[1] != 1) {
494 $results['severity'] = $this::REQUIREMENT_ERROR
;
495 $results['details'] = 'MySQL server auto_increment_increment is not 1';
505 public function checkMysqlThreadStack($db_config) {
506 $min_thread_stack = 192;
509 'title' => 'CiviCRM Mysql thread stack',
510 'severity' => $this::REQUIREMENT_OK
,
511 'details' => 'MySQL thread_stack is OK',
514 $conn = $this->connect($db_config);
516 $results['severity'] = $this::REQUIREMENT_ERROR
;
517 $results['details'] = 'Could not connect to database';
521 if (!@mysqli_select_db
($conn, $db_config['database'])) {
522 $results['severity'] = $this::REQUIREMENT_ERROR
;
523 $results['details'] = 'Could not select the database';
528 $r = mysqli_query($conn, "SHOW VARIABLES LIKE 'thread_stack'");
530 $results['severity'] = $this::REQUIREMENT_ERROR
;
531 $results['details'] = 'Could not query thread_stack value';
534 $values = mysqli_fetch_row($r);
535 if ($values[1] < (1024 * $min_thread_stack)) {
536 $results['severity'] = $this::REQUIREMENT_ERROR
;
537 $results['details'] = 'MySQL thread_stack is ' . ($values[1] / 1024) . "kb (minimum required is {$min_thread_stack} kb";
549 public function checkMysqlLockTables($db_config) {
551 'title' => 'CiviCRM MySQL Lock Tables',
552 'severity' => $this::REQUIREMENT_OK
,
553 'details' => 'Can successfully lock and unlock tables',
556 $conn = $this->connect($db_config);
558 $results['severity'] = $this::REQUIREMENT_ERROR
;
559 $results['details'] = 'Could not connect to database';
563 if (!@mysqli_select_db
($conn, $db_config['database'])) {
564 $results['severity'] = $this::REQUIREMENT_ERROR
;
565 $results['details'] = 'Could not select the database';
570 $r = mysqli_query($conn, 'CREATE TEMPORARY TABLE civicrm_install_temp_table_test (test text)');
572 $results['severity'] = $this::REQUIREMENT_ERROR
;
573 $results['details'] = 'Could not create a table';
578 $r = mysqli_query($conn, 'LOCK TABLES civicrm_install_temp_table_test WRITE');
580 $results['severity'] = $this::REQUIREMENT_ERROR
;
581 $results['details'] = 'Could not obtain a write lock';
586 $r = mysqli_query($conn, 'UNLOCK TABLES');
588 $results['severity'] = $this::REQUIREMENT_ERROR
;
589 $results['details'] = 'Could not release table lock';
601 public function checkFilepathIsWritable($file_paths) {
603 'title' => 'CiviCRM directories are writable',
604 'severity' => $this::REQUIREMENT_OK
,
605 'details' => 'All required directories are writable: ' . implode(', ', $file_paths),
608 $unwritable_dirs = [];
609 foreach ($file_paths as $path) {
610 if (!is_writable($path)) {
611 $unwritable_dirs[] = $path;
615 if ($unwritable_dirs) {
616 $results['severity'] = $this::REQUIREMENT_ERROR
;
617 $results['details'] = "The following directories need to be made writable by the webserver: " . implode(', ', $unwritable_dirs);
628 public function checkMysqlUtf8mb4($db_config) {
630 'title' => 'CiviCRM MySQL utf8mb4 Support',
631 'severity' => $this::REQUIREMENT_OK
,
632 'details' => 'Your system supports the MySQL utf8mb4 character set.',
635 $conn = $this->connect($db_config);
637 $results['severity'] = $this::REQUIREMENT_ERROR
;
638 $results['details'] = 'Could not connect to database';
642 if (!@mysqli_select_db
($conn, $db_config['database'])) {
643 $results['severity'] = $this::REQUIREMENT_ERROR
;
644 $results['details'] = 'Could not select the database';
649 mysqli_query($conn, 'DROP TABLE IF EXISTS civicrm_utf8mb4_test');
650 $r = mysqli_query($conn, 'CREATE TABLE civicrm_utf8mb4_test (id VARCHAR(255), PRIMARY KEY(id(255))) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC ENGINE=INNODB');
652 $results['severity'] = $this::REQUIREMENT_WARNING
;
653 $results['details'] = 'It is recommended, though not yet required, to configure your MySQL server for utf8mb4 support. You will need the following MySQL server configuration: innodb_large_prefix=true innodb_file_format=barracuda innodb_file_per_table=true';
657 mysqli_query($conn, 'DROP TABLE civicrm_utf8mb4_test');
659 // Ensure that the MySQL driver supports utf8mb4 encoding.
660 $version = mysqli_get_client_info();
661 if (strpos($version, 'mysqlnd') !== FALSE) {
662 // The mysqlnd driver supports utf8mb4 starting at version 5.0.9.
663 $version = preg_replace('/^\D+([\d.]+).*/', '$1', $version);
664 if (version_compare($version, '5.0.9', '<')) {
665 $results['severity'] = $this::REQUIREMENT_WARNING
;
666 $results['details'] = 'It is recommended, though not yet required, to upgrade your PHP MySQL driver (mysqlnd) to >= 5.0.9 for utf8mb4 support.';
672 // The libmysqlclient driver supports utf8mb4 starting at version 5.5.3.
673 if (version_compare($version, '5.5.3', '<')) {
674 $results['severity'] = $this::REQUIREMENT_WARNING
;
675 $results['details'] = 'It is recommended, though not yet required, to upgrade your PHP MySQL driver (libmysqlclient) to >= 5.5.3 for utf8mb4 support.';