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;
26 protected $system_checks = array(
28 'checkServerVariables',
29 'checkMysqlConnectExists',
30 'checkJsonEncodeExists',
31 'checkMultibyteExists',
34 protected $database_checks = array(
35 'checkMysqlConnection',
38 'checkMysqlTempTables',
39 'checkMySQLAutoIncrementIncrementOne',
41 'checkMysqlThreadStack',
42 'checkMysqlLockTables',
47 * Run all requirements tests.
49 * @param array $config
50 * An array with two keys:
55 * An array of check summaries. Each array contains the keys 'title', 'severity', and 'details'.
57 public function checkAll(array $config) {
58 return array_merge($this->checkSystem($config['file_paths']), $this->checkDatabase($config['db_config']));
62 * Check system requirements are met, such as sufficient memory,
63 * necessary file paths are writable and required php extensions
66 * @param array $file_paths
67 * An array of file paths that will be checked to confirm they
72 public function checkSystem(array $file_paths) {
75 $errors[] = $this->checkFilepathIsWritable($file_paths);
76 foreach ($this->system_checks
as $check) {
77 $errors[] = $this->$check();
84 * Check database connection, database version and other
85 * database requirements are met.
87 * @param array $db_config
89 * - host (with optional port specified eg. localhost:12345)
90 * - database (name of database to select)
96 public function checkDatabase(array $db_config) {
99 foreach ($this->database_checks
as $check) {
100 $errors[] = $this->$check($db_config);
107 * Generates a mysql connection
109 * @param $db_confic array
110 * @return object mysqli connection
112 protected function connect($db_config) {
114 if (!empty($db_config['host'])) {
115 $host = $db_config['host'];
117 elseif (!empty($db_config['server'])) {
118 $host = $db_config['server'];
120 $conn = @mysqli_connect
($host, $db_config['username'], $db_config['password'], $db_config['database'], !empty($db_config['port']) ?
$db_config['port'] : NULL);
125 * Check configured php Memory.
128 public function checkMemory() {
129 $min = 1024 * 1024 * 32;
130 $recommended = 1024 * 1024 * 64;
132 $mem = $this->getPHPMemory();
133 $mem_string = ini_get('memory_limit');
136 'title' => 'CiviCRM memory check',
137 'severity' => $this::REQUIREMENT_OK
,
138 'details' => "You have $mem_string allocated (minimum 32Mb, recommended 64Mb)",
141 if ($mem < $min && $mem > 0) {
142 $results['severity'] = $this::REQUIREMENT_ERROR
;
144 elseif ($mem < $recommended && $mem != 0) {
145 $results['severity'] = $this::REQUIREMENT_WARNING
;
148 $results['details'] = "Cannot determine PHP memory allocation. Install only if you're sure you've allocated at least 32 MB.";
149 $results['severity'] = $this::REQUIREMENT_WARNING
;
156 * Get Configured PHP memory.
159 protected function getPHPMemory() {
160 $memString = ini_get("memory_limit");
162 switch (strtolower(substr($memString, -1))) {
164 return round(substr($memString, 0, -1) * 1024);
167 return round(substr($memString, 0, -1) * 1024 * 1024);
170 return round(substr($memString, 0, -1) * 1024 * 1024 * 1024);
173 return round($memString);
180 public function checkServerVariables() {
182 'title' => 'CiviCRM PHP server variables',
183 'severity' => $this::REQUIREMENT_OK
,
184 'details' => 'The required $_SERVER variables are set',
187 $required_variables = array('SCRIPT_NAME', 'HTTP_HOST', 'SCRIPT_FILENAME');
190 foreach ($required_variables as $required_variable) {
191 if (empty($_SERVER[$required_variable])) {
192 $missing[] = '$_SERVER[' . $required_variable . ']';
197 $results['severity'] = $this::REQUIREMENT_ERROR
;
198 $results['details'] = 'The following PHP variables are not set: ' . implode(', ', $missing);
207 public function checkJsonEncodeExists() {
209 'title' => 'CiviCRM JSON encoding support',
210 'severity' => $this::REQUIREMENT_OK
,
211 'details' => 'Function json_encode() found',
213 if (!function_exists('json_encode')) {
214 $results['severity'] = $this::REQUIREMENT_ERROR
;
215 $results['details'] = 'Function json_encode() does not exist';
222 * CHeck that PHP Multibyte functions are enabled.
225 public function checkMultibyteExists() {
227 'title' => 'CiviCRM MultiByte encoding support',
228 'severity' => $this::REQUIREMENT_OK
,
229 'details' => 'PHP Multibyte etension found',
231 if (!function_exists('mb_substr')) {
232 $results['severity'] = $this::REQUIREMENT_ERROR
;
233 $results['details'] = 'PHP Multibyte extension has not been installed and enabled';
242 public function checkMysqlConnectExists() {
244 'title' => 'CiviCRM MySQL check',
245 'severity' => $this::REQUIREMENT_OK
,
246 'details' => 'Function mysqli_connect() found',
248 if (!function_exists('mysqli_connect')) {
249 $results['severity'] = $this::REQUIREMENT_ERROR
;
250 $results['details'] = 'Function mysqli_connect() does not exist';
257 * @param array $db_config
261 public function checkMysqlConnection(array $db_config) {
263 'title' => 'CiviCRM MySQL connection',
264 'severity' => $this::REQUIREMENT_OK
,
265 'details' => "Connected",
268 $conn = $this->connect($db_config);
271 $results['details'] = mysqli_connect_error();
272 $results['severity'] = $this::REQUIREMENT_ERROR
;
276 if (!@mysqli_select_db
($conn, $db_config['database'])) {
277 $results['details'] = mysqli_error($conn);
278 $results['severity'] = $this::REQUIREMENT_ERROR
;
286 * @param array $db_config
290 public function checkMysqlVersion(array $db_config) {
293 'title' => 'CiviCRM MySQL Version',
294 'severity' => $this::REQUIREMENT_OK
,
297 $conn = $this->connect($db_config);
298 if (!$conn ||
!($info = mysqli_get_server_info($conn))) {
299 $results['severity'] = $this::REQUIREMENT_WARNING
;
300 $results['details'] = "Cannot determine the version of MySQL installed. Please ensure at least version {$min} is installed.";
304 if (version_compare($info, $min) == -1) {
305 $results['severity'] = $this::REQUIREMENT_ERROR
;
306 $results['details'] = "MySQL version is {$info}; minimum required is {$min}";
310 $results['details'] = "MySQL version is {$info}";
315 * @param array $db_config
319 public function checkMysqlInnodb(array $db_config) {
321 'title' => 'CiviCRM InnoDB support',
322 'severity' => $this::REQUIREMENT_ERROR
,
323 'details' => 'Could not determine if MySQL has InnoDB support. Assuming none.',
326 $conn = $this->connect($db_config);
331 $innodb_support = FALSE;
332 $result = mysqli_query($conn, "SHOW ENGINES");
333 while ($values = mysqli_fetch_array($result)) {
334 if ($values['Engine'] == 'InnoDB') {
335 if (strtolower($values['Support']) == 'yes' ||
strtolower($values['Support']) == 'default') {
336 $innodb_support = TRUE;
342 if ($innodb_support) {
343 $results['severity'] = $this::REQUIREMENT_OK
;
344 $results['details'] = 'MySQL supports InnoDB';
350 * @param array $db_config
354 public function checkMysqlTempTables(array $db_config) {
356 'title' => 'CiviCRM MySQL Temp Tables',
357 'severity' => $this::REQUIREMENT_OK
,
358 'details' => 'MySQL server supports temporary tables',
361 $conn = $this->connect($db_config);
363 $results['severity'] = $this::REQUIREMENT_ERROR
;
364 $results['details'] = "Could not connect to database";
368 if (!@mysqli_select_db
($conn, $db_config['database'])) {
369 $results['severity'] = $this::REQUIREMENT_ERROR
;
370 $results['details'] = "Could not select the database";
374 $r = mysqli_query($conn, 'CREATE TEMPORARY TABLE civicrm_install_temp_table_test (test text)');
376 $results['severity'] = $this::REQUIREMENT_ERROR
;
377 $results['details'] = "Database does not support creation of temporary tables";
381 mysqli_query($conn, 'DROP TEMPORARY TABLE civicrm_install_temp_table_test');
390 public function checkMysqlTrigger($db_config) {
392 'title' => 'CiviCRM MySQL Trigger',
393 'severity' => $this::REQUIREMENT_OK
,
394 'details' => 'Database supports MySQL triggers',
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";
410 $r = mysqli_query($conn, 'CREATE TABLE civicrm_install_temp_table_test (test text)');
412 $results['severity'] = $this::REQUIREMENT_ERROR
;
413 $results['details'] = 'Could not create a table to run test';
417 $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');
419 $results['severity'] = $this::REQUIREMENT_ERROR
;
420 $results['details'] = 'Database does not support creation of triggers';
423 mysqli_query($conn, 'DROP TRIGGER civicrm_install_temp_table_test_trigger');
426 mysqli_query($conn, 'DROP TABLE civicrm_install_temp_table_test');
431 * @param array $db_config
435 public function checkMySQLAutoIncrementIncrementOne(array $db_config) {
437 'title' => 'CiviCRM MySQL AutoIncrementIncrement',
438 'severity' => $this::REQUIREMENT_OK
,
439 'details' => 'MySQL server auto_increment_increment is 1',
442 $conn = $this->connect($db_config);
444 $results['severity'] = $this::REQUIREMENT_ERROR
;
445 $results['details'] = 'Could not connect to database';
449 $r = mysqli_query($conn, "SHOW variables like 'auto_increment_increment'");
451 $results['severity'] = $this::REQUIREMENT_ERROR
;
452 $results['details'] = 'Could not query database server variables';
456 $values = mysqli_fetch_row($r);
457 if ($values[1] != 1) {
458 $results['severity'] = $this::REQUIREMENT_ERROR
;
459 $results['details'] = 'MySQL server auto_increment_increment is not 1';
469 public function checkMysqlThreadStack($db_config) {
470 $min_thread_stack = 192;
473 'title' => 'CiviCRM Mysql thread stack',
474 'severity' => $this::REQUIREMENT_OK
,
475 'details' => 'MySQL thread_stack is OK',
478 $conn = $this->connect($db_config);
480 $results['severity'] = $this::REQUIREMENT_ERROR
;
481 $results['details'] = 'Could not connect to database';
485 if (!@mysqli_select_db
($conn, $db_config['database'])) {
486 $results['severity'] = $this::REQUIREMENT_ERROR
;
487 $results['details'] = 'Could not select the database';
491 $r = mysqli_query($conn, "SHOW VARIABLES LIKE 'thread_stack'"); // bytes => kb
493 $results['severity'] = $this::REQUIREMENT_ERROR
;
494 $results['details'] = 'Could not query thread_stack value';
497 $values = mysqli_fetch_row($r);
498 if ($values[1] < (1024 * $min_thread_stack)) {
499 $results['severity'] = $this::REQUIREMENT_ERROR
;
500 $results['details'] = 'MySQL thread_stack is ' . ($values[1] / 1024) . "kb (minimum required is {$min_thread_stack} kb";
512 public function checkMysqlLockTables($db_config) {
514 'title' => 'CiviCRM MySQL Lock Tables',
515 'severity' => $this::REQUIREMENT_OK
,
516 'details' => 'Can successfully lock and unlock tables',
519 $conn = $this->connect($db_config);
521 $results['severity'] = $this::REQUIREMENT_ERROR
;
522 $results['details'] = 'Could not connect to database';
526 if (!@mysqli_select_db
($conn, $db_config['database'])) {
527 $results['severity'] = $this::REQUIREMENT_ERROR
;
528 $results['details'] = 'Could not select the database';
533 $r = mysqli_query($conn, 'CREATE TEMPORARY TABLE civicrm_install_temp_table_test (test text)');
535 $results['severity'] = $this::REQUIREMENT_ERROR
;
536 $results['details'] = 'Could not create a table';
541 $r = mysqli_query($conn, 'LOCK TABLES civicrm_install_temp_table_test WRITE');
543 $results['severity'] = $this::REQUIREMENT_ERROR
;
544 $results['details'] = 'Could not obtain a write lock';
549 $r = mysqli_query($conn, 'UNLOCK TABLES');
551 $results['severity'] = $this::REQUIREMENT_ERROR
;
552 $results['details'] = 'Could not release table lock';
564 public function checkFilepathIsWritable($file_paths) {
566 'title' => 'CiviCRM directories are writable',
567 'severity' => $this::REQUIREMENT_OK
,
568 'details' => 'All required directories are writable: ' . implode(', ', $file_paths),
571 $unwritable_dirs = array();
572 foreach ($file_paths as $path) {
573 if (!is_writable($path)) {
574 $unwritable_dirs[] = $path;
578 if ($unwritable_dirs) {
579 $results['severity'] = $this::REQUIREMENT_ERROR
;
580 $results['details'] = "The following directories need to be made writable by the webserver: " . implode(', ', $unwritable_dirs);
591 public function checkMysqlUtf8mb4($db_config) {
593 'title' => 'CiviCRM MySQL utf8mb4 Support',
594 'severity' => $this::REQUIREMENT_OK
,
595 'details' => 'Your system supports the MySQL utf8mb4 character set.',
598 $conn = $this->connect($db_config);
600 $results['severity'] = $this::REQUIREMENT_ERROR
;
601 $results['details'] = 'Could not connect to database';
605 if (!@mysqli_select_db
($conn, $db_config['database'])) {
606 $results['severity'] = $this::REQUIREMENT_ERROR
;
607 $results['details'] = 'Could not select the database';
612 $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');
614 $results['severity'] = $this::REQUIREMENT_WARNING
;
615 $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';
619 mysqli_query('DROP TABLE civicrm_utf8mb4_test');
621 // Ensure that the MySQL driver supports utf8mb4 encoding.
622 $version = mysqli_get_client_info($conn);
623 if (strpos($version, 'mysqlnd') !== FALSE) {
624 // The mysqlnd driver supports utf8mb4 starting at version 5.0.9.
625 $version = preg_replace('/^\D+([\d.]+).*/', '$1', $version);
626 if (version_compare($version, '5.0.9', '<')) {
627 $results['severity'] = $this::REQUIREMENT_WARNING
;
628 $results['details'] = 'It is recommended, though not yet required, to upgrade your PHP MySQL driver (mysqlnd) to >= 5.0.9 for utf8mb4 support.';
634 // The libmysqlclient driver supports utf8mb4 starting at version 5.5.3.
635 if (version_compare($version, '5.5.3', '<')) {
636 $results['severity'] = $this::REQUIREMENT_WARNING
;
637 $results['details'] = 'It is recommended, though not yet required, to upgrade your PHP MySQL driver (libmysqlclient) to >= 5.5.3 for utf8mb4 support.';