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 $conn = @mysqli_connect
($host, $db_config['username'], $db_config['password'], $db_config['database'], !empty($db_config['port']) ?
$db_config['port'] : NULL);
140 * Check configured php Memory.
143 public function checkMemory() {
144 $min = 1024 * 1024 * 32;
145 $recommended = 1024 * 1024 * 64;
147 $mem = $this->getPHPMemory();
148 $mem_string = ini_get('memory_limit');
151 'title' => 'CiviCRM memory check',
152 'severity' => $this::REQUIREMENT_OK
,
153 'details' => "You have $mem_string allocated (minimum 32Mb, recommended 64Mb)",
156 if ($mem < $min && $mem > 0) {
157 $results['severity'] = $this::REQUIREMENT_ERROR
;
159 elseif ($mem < $recommended && $mem != 0) {
160 $results['severity'] = $this::REQUIREMENT_WARNING
;
163 $results['details'] = "Cannot determine PHP memory allocation. Install only if you're sure you've allocated at least 32 MB.";
164 $results['severity'] = $this::REQUIREMENT_WARNING
;
171 * Get Configured PHP memory.
174 protected function getPHPMemory() {
175 $memString = ini_get("memory_limit");
177 switch (strtolower(substr($memString, -1))) {
179 return round(substr($memString, 0, -1) * 1024);
182 return round(substr($memString, 0, -1) * 1024 * 1024);
185 return round(substr($memString, 0, -1) * 1024 * 1024 * 1024);
188 return round($memString);
195 public function checkServerVariables() {
197 'title' => 'CiviCRM PHP server variables',
198 'severity' => $this::REQUIREMENT_OK
,
199 'details' => 'The required $_SERVER variables are set',
202 $required_variables = ['SCRIPT_NAME', 'HTTP_HOST', 'SCRIPT_FILENAME'];
205 foreach ($required_variables as $required_variable) {
206 if (empty($_SERVER[$required_variable])) {
207 $missing[] = '$_SERVER[' . $required_variable . ']';
212 $results['severity'] = $this::REQUIREMENT_ERROR
;
213 $results['details'] = 'The following PHP variables are not set: ' . implode(', ', $missing);
222 public function checkJsonEncodeExists() {
224 'title' => 'CiviCRM JSON encoding support',
225 'severity' => $this::REQUIREMENT_OK
,
226 'details' => 'Function json_encode() found',
228 if (!function_exists('json_encode')) {
229 $results['severity'] = $this::REQUIREMENT_ERROR
;
230 $results['details'] = 'Function json_encode() does not exist';
237 * CHeck that PHP Multibyte functions are enabled.
240 public function checkMultibyteExists() {
242 'title' => 'CiviCRM MultiByte encoding support',
243 'severity' => $this::REQUIREMENT_OK
,
244 'details' => 'PHP Multibyte etension found',
246 if (!function_exists('mb_substr')) {
247 $results['severity'] = $this::REQUIREMENT_ERROR
;
248 $results['details'] = 'PHP Multibyte extension has not been installed and enabled';
257 public function checkMysqlConnectExists() {
259 'title' => 'CiviCRM MySQL check',
260 'severity' => $this::REQUIREMENT_OK
,
261 'details' => 'Function mysqli_connect() found',
263 if (!function_exists('mysqli_connect')) {
264 $results['severity'] = $this::REQUIREMENT_ERROR
;
265 $results['details'] = 'Function mysqli_connect() does not exist';
272 * @param array $db_config
276 public function checkMysqlConnection(array $db_config) {
278 'title' => 'CiviCRM MySQL connection',
279 'severity' => $this::REQUIREMENT_OK
,
280 'details' => "Connected",
283 $conn = $this->connect($db_config);
286 $results['details'] = mysqli_connect_error();
287 $results['severity'] = $this::REQUIREMENT_ERROR
;
291 if (!@mysqli_select_db
($conn, $db_config['database'])) {
292 $results['details'] = mysqli_error($conn);
293 $results['severity'] = $this::REQUIREMENT_ERROR
;
301 * @param array $db_config
305 public function checkMysqlVersion(array $db_config) {
306 if (!class_exists('\CRM_Upgrade_Incremental_General')) {
307 require_once dirname(__FILE__
) . '/../../CRM/Upgrade/Incremental/General.php';
309 $min = \CRM_Upgrade_Incremental_General
::MIN_INSTALL_MYSQL_VER
;
311 'title' => 'CiviCRM MySQL Version',
312 'severity' => $this::REQUIREMENT_OK
,
315 $conn = $this->connect($db_config);
316 if (!$conn ||
!($info = mysqli_get_server_info($conn))) {
317 $results['severity'] = $this::REQUIREMENT_WARNING
;
318 $results['details'] = "Cannot determine the version of MySQL installed. Please ensure at least version {$min} is installed.";
322 $versionDetails = mysqli_query($conn, 'SELECT version() as version')->fetch_assoc();
323 if (version_compare($versionDetails['version'], $min) == -1) {
324 $results['severity'] = $this::REQUIREMENT_ERROR
;
325 $results['details'] = "MySQL version is {$info}; minimum required is {$min}";
329 $results['details'] = "MySQL version is {$info}";
334 * @param array $db_config
338 public function checkMysqlInnodb(array $db_config) {
340 'title' => 'CiviCRM InnoDB support',
341 'severity' => $this::REQUIREMENT_ERROR
,
342 'details' => 'Could not determine if MySQL has InnoDB support. Assuming none.',
345 $conn = $this->connect($db_config);
350 $innodb_support = FALSE;
351 $result = mysqli_query($conn, "SHOW ENGINES");
352 while ($values = mysqli_fetch_array($result)) {
353 if ($values['Engine'] == 'InnoDB') {
354 if (strtolower($values['Support']) == 'yes' ||
strtolower($values['Support']) == 'default') {
355 $innodb_support = TRUE;
361 if ($innodb_support) {
362 $results['severity'] = $this::REQUIREMENT_OK
;
363 $results['details'] = 'MySQL supports InnoDB';
369 * @param array $db_config
373 public function checkMysqlTempTables(array $db_config) {
375 'title' => 'CiviCRM MySQL Temp Tables',
376 'severity' => $this::REQUIREMENT_OK
,
377 'details' => 'MySQL server supports temporary tables',
380 $conn = $this->connect($db_config);
382 $results['severity'] = $this::REQUIREMENT_ERROR
;
383 $results['details'] = "Could not connect to database";
387 if (!@mysqli_select_db
($conn, $db_config['database'])) {
388 $results['severity'] = $this::REQUIREMENT_ERROR
;
389 $results['details'] = "Could not select the database";
392 $temporaryTableName = \CRM_Utils_SQL_TempTable
::build()->setCategory('install')->getName();
393 $r = mysqli_query($conn, 'CREATE TEMPORARY TABLE ' . $temporaryTableName . ' (test text)');
395 $results['severity'] = $this::REQUIREMENT_ERROR
;
396 $results['details'] = "Database does not support creation of temporary tables";
400 mysqli_query($conn, 'DROP TEMPORARY TABLE ' . $temporaryTableName);
409 public function checkMysqlTrigger($db_config) {
411 'title' => 'CiviCRM MySQL Trigger',
412 'severity' => $this::REQUIREMENT_OK
,
413 'details' => 'Database supports MySQL triggers',
416 $conn = $this->connect($db_config);
418 $results['severity'] = $this::REQUIREMENT_ERROR
;
419 $results['details'] = 'Could not connect to database';
423 if (!@mysqli_select_db
($conn, $db_config['database'])) {
424 $results['severity'] = $this::REQUIREMENT_ERROR
;
425 $results['details'] = "Could not select the database";
429 $r = mysqli_query($conn, 'CREATE TABLE civicrm_install_temp_table_test (test text)');
431 $results['severity'] = $this::REQUIREMENT_ERROR
;
432 $results['details'] = 'Could not create a table to run test';
436 $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');
438 $results['severity'] = $this::REQUIREMENT_ERROR
;
439 $results['details'] = 'Database does not support creation of triggers';
442 mysqli_query($conn, 'DROP TRIGGER civicrm_install_temp_table_test_trigger');
445 mysqli_query($conn, 'DROP TABLE civicrm_install_temp_table_test');
450 * @param array $db_config
454 public function checkMySQLAutoIncrementIncrementOne(array $db_config) {
456 'title' => 'CiviCRM MySQL AutoIncrementIncrement',
457 'severity' => $this::REQUIREMENT_OK
,
458 'details' => 'MySQL server auto_increment_increment is 1',
461 $conn = $this->connect($db_config);
463 $results['severity'] = $this::REQUIREMENT_ERROR
;
464 $results['details'] = 'Could not connect to database';
468 $r = mysqli_query($conn, "SHOW variables like 'auto_increment_increment'");
470 $results['severity'] = $this::REQUIREMENT_ERROR
;
471 $results['details'] = 'Could not query database server variables';
475 $values = mysqli_fetch_row($r);
476 if ($values[1] != 1) {
477 $results['severity'] = $this::REQUIREMENT_ERROR
;
478 $results['details'] = 'MySQL server auto_increment_increment is not 1';
488 public function checkMysqlThreadStack($db_config) {
489 $min_thread_stack = 192;
492 'title' => 'CiviCRM Mysql thread stack',
493 'severity' => $this::REQUIREMENT_OK
,
494 'details' => 'MySQL thread_stack is OK',
497 $conn = $this->connect($db_config);
499 $results['severity'] = $this::REQUIREMENT_ERROR
;
500 $results['details'] = 'Could not connect to database';
504 if (!@mysqli_select_db
($conn, $db_config['database'])) {
505 $results['severity'] = $this::REQUIREMENT_ERROR
;
506 $results['details'] = 'Could not select the database';
511 $r = mysqli_query($conn, "SHOW VARIABLES LIKE 'thread_stack'");
513 $results['severity'] = $this::REQUIREMENT_ERROR
;
514 $results['details'] = 'Could not query thread_stack value';
517 $values = mysqli_fetch_row($r);
518 if ($values[1] < (1024 * $min_thread_stack)) {
519 $results['severity'] = $this::REQUIREMENT_ERROR
;
520 $results['details'] = 'MySQL thread_stack is ' . ($values[1] / 1024) . "kb (minimum required is {$min_thread_stack} kb";
532 public function checkMysqlLockTables($db_config) {
534 'title' => 'CiviCRM MySQL Lock Tables',
535 'severity' => $this::REQUIREMENT_OK
,
536 'details' => 'Can successfully lock and unlock tables',
539 $conn = $this->connect($db_config);
541 $results['severity'] = $this::REQUIREMENT_ERROR
;
542 $results['details'] = 'Could not connect to database';
546 if (!@mysqli_select_db
($conn, $db_config['database'])) {
547 $results['severity'] = $this::REQUIREMENT_ERROR
;
548 $results['details'] = 'Could not select the database';
553 $r = mysqli_query($conn, 'CREATE TEMPORARY TABLE civicrm_install_temp_table_test (test text)');
555 $results['severity'] = $this::REQUIREMENT_ERROR
;
556 $results['details'] = 'Could not create a table';
561 $r = mysqli_query($conn, 'LOCK TABLES civicrm_install_temp_table_test WRITE');
563 $results['severity'] = $this::REQUIREMENT_ERROR
;
564 $results['details'] = 'Could not obtain a write lock';
569 $r = mysqli_query($conn, 'UNLOCK TABLES');
571 $results['severity'] = $this::REQUIREMENT_ERROR
;
572 $results['details'] = 'Could not release table lock';
584 public function checkFilepathIsWritable($file_paths) {
586 'title' => 'CiviCRM directories are writable',
587 'severity' => $this::REQUIREMENT_OK
,
588 'details' => 'All required directories are writable: ' . implode(', ', $file_paths),
591 $unwritable_dirs = [];
592 foreach ($file_paths as $path) {
593 if (!is_writable($path)) {
594 $unwritable_dirs[] = $path;
598 if ($unwritable_dirs) {
599 $results['severity'] = $this::REQUIREMENT_ERROR
;
600 $results['details'] = "The following directories need to be made writable by the webserver: " . implode(', ', $unwritable_dirs);
611 public function checkMysqlUtf8mb4($db_config) {
613 'title' => 'CiviCRM MySQL utf8mb4 Support',
614 'severity' => $this::REQUIREMENT_OK
,
615 'details' => 'Your system supports the MySQL utf8mb4 character set.',
618 $conn = $this->connect($db_config);
620 $results['severity'] = $this::REQUIREMENT_ERROR
;
621 $results['details'] = 'Could not connect to database';
625 if (!@mysqli_select_db
($conn, $db_config['database'])) {
626 $results['severity'] = $this::REQUIREMENT_ERROR
;
627 $results['details'] = 'Could not select the database';
632 mysqli_query($conn, 'DROP TABLE IF EXISTS civicrm_utf8mb4_test');
633 $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');
635 $results['severity'] = $this::REQUIREMENT_WARNING
;
636 $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';
640 mysqli_query($conn, 'DROP TABLE civicrm_utf8mb4_test');
642 // Ensure that the MySQL driver supports utf8mb4 encoding.
643 $version = mysqli_get_client_info();
644 if (strpos($version, 'mysqlnd') !== FALSE) {
645 // The mysqlnd driver supports utf8mb4 starting at version 5.0.9.
646 $version = preg_replace('/^\D+([\d.]+).*/', '$1', $version);
647 if (version_compare($version, '5.0.9', '<')) {
648 $results['severity'] = $this::REQUIREMENT_WARNING
;
649 $results['details'] = 'It is recommended, though not yet required, to upgrade your PHP MySQL driver (mysqlnd) to >= 5.0.9 for utf8mb4 support.';
655 // The libmysqlclient driver supports utf8mb4 starting at version 5.5.3.
656 if (version_compare($version, '5.5.3', '<')) {
657 $results['severity'] = $this::REQUIREMENT_WARNING
;
658 $results['details'] = 'It is recommended, though not yet required, to upgrade your PHP MySQL driver (libmysqlclient) to >= 5.5.3 for utf8mb4 support.';