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',
46 * Run all requirements tests.
48 * @param array $config
49 * An array with two keys:
54 * An array of check summaries. Each array contains the keys 'title', 'severity', and 'details'.
56 public function checkAll(array $config) {
57 return array_merge($this->checkSystem($config['file_paths']), $this->checkDatabase($config['db_config']));
61 * Check system requirements are met, such as sufficient memory,
62 * necessary file paths are writable and required php extensions
65 * @param array $file_paths
66 * An array of file paths that will be checked to confirm they
71 public function checkSystem(array $file_paths) {
74 $errors[] = $this->checkFilepathIsWritable($file_paths);
75 foreach ($this->system_checks
as $check) {
76 $errors[] = $this->$check();
83 * Check database connection, database version and other
84 * database requirements are met.
86 * @param array $db_config
88 * - host (with optional port specified eg. localhost:12345)
89 * - database (name of database to select)
95 public function checkDatabase(array $db_config) {
98 foreach ($this->database_checks
as $check) {
99 $errors[] = $this->$check($db_config);
106 * Generates a mysql connection
108 * @param $db_confic array
109 * @return object mysqli connection
111 protected function connect($db_config) {
113 if (!empty($db_config['host'])) {
114 $host = $db_config['host'];
116 elseif (!empty($db_config['server'])) {
117 $host = $db_config['server'];
119 $conn = @mysqli_connect
($host, $db_config['username'], $db_config['password'], $db_config['database'], !empty($db_config['port']) ?
$db_config['port'] : NULL);
124 * Check configured php Memory.
127 public function checkMemory() {
128 $min = 1024 * 1024 * 32;
129 $recommended = 1024 * 1024 * 64;
131 $mem = $this->getPHPMemory();
132 $mem_string = ini_get('memory_limit');
135 'title' => 'CiviCRM memory check',
136 'severity' => $this::REQUIREMENT_OK
,
137 'details' => "You have $mem_string allocated (minimum 32Mb, recommended 64Mb)",
140 if ($mem < $min && $mem > 0) {
141 $results['severity'] = $this::REQUIREMENT_ERROR
;
143 elseif ($mem < $recommended && $mem != 0) {
144 $results['severity'] = $this::REQUIREMENT_WARNING
;
147 $results['details'] = "Cannot determine PHP memory allocation. Install only if you're sure you've allocated at least 32 MB.";
148 $results['severity'] = $this::REQUIREMENT_WARNING
;
155 * Get Configured PHP memory.
158 protected function getPHPMemory() {
159 $memString = ini_get("memory_limit");
161 switch (strtolower(substr($memString, -1))) {
163 return round(substr($memString, 0, -1) * 1024);
166 return round(substr($memString, 0, -1) * 1024 * 1024);
169 return round(substr($memString, 0, -1) * 1024 * 1024 * 1024);
172 return round($memString);
179 public function checkServerVariables() {
181 'title' => 'CiviCRM PHP server variables',
182 'severity' => $this::REQUIREMENT_OK
,
183 'details' => 'The required $_SERVER variables are set',
186 $required_variables = array('SCRIPT_NAME', 'HTTP_HOST', 'SCRIPT_FILENAME');
189 foreach ($required_variables as $required_variable) {
190 if (empty($_SERVER[$required_variable])) {
191 $missing[] = '$_SERVER[' . $required_variable . ']';
196 $results['severity'] = $this::REQUIREMENT_ERROR
;
197 $results['details'] = 'The following PHP variables are not set: ' . implode(', ', $missing);
206 public function checkJsonEncodeExists() {
208 'title' => 'CiviCRM JSON encoding support',
209 'severity' => $this::REQUIREMENT_OK
,
210 'details' => 'Function json_encode() found',
212 if (!function_exists('json_encode')) {
213 $results['severity'] = $this::REQUIREMENT_ERROR
;
214 $results['details'] = 'Function json_encode() does not exist';
221 * CHeck that PHP Multibyte functions are enabled.
224 public function checkMultibyteExists() {
226 'title' => 'CiviCRM MultiByte encoding support',
227 'severity' => $this::REQUIREMENT_OK
,
228 'details' => 'PHP Multibyte etension found',
230 if (!function_exists('mb_substr')) {
231 $results['severity'] = $this::REQUIREMENT_ERROR
;
232 $results['details'] = 'PHP Multibyte extension has not been installed and enabled';
241 public function checkMysqlConnectExists() {
243 'title' => 'CiviCRM MySQL check',
244 'severity' => $this::REQUIREMENT_OK
,
245 'details' => 'Function mysqli_connect() found',
247 if (!function_exists('mysqli_connect')) {
248 $results['severity'] = $this::REQUIREMENT_ERROR
;
249 $results['details'] = 'Function mysqli_connect() does not exist';
256 * @param array $db_config
260 public function checkMysqlConnection(array $db_config) {
262 'title' => 'CiviCRM MySQL connection',
263 'severity' => $this::REQUIREMENT_OK
,
264 'details' => "Connected",
267 $conn = $this->connect($db_config);
270 $results['details'] = mysqli_connect_error();
271 $results['severity'] = $this::REQUIREMENT_ERROR
;
275 if (!@mysqli_select_db
($conn, $db_config['database'])) {
276 $results['details'] = mysqli_error($conn);
277 $results['severity'] = $this::REQUIREMENT_ERROR
;
285 * @param array $db_config
289 public function checkMysqlVersion(array $db_config) {
292 'title' => 'CiviCRM MySQL Version',
293 'severity' => $this::REQUIREMENT_OK
,
296 $conn = $this->connect($db_config);
297 if (!$conn ||
!($info = mysqli_get_server_info($conn))) {
298 $results['severity'] = $this::REQUIREMENT_WARNING
;
299 $results['details'] = "Cannot determine the version of MySQL installed. Please ensure at least version {$min} is installed.";
303 if (version_compare($info, $min) == -1) {
304 $results['severity'] = $this::REQUIREMENT_ERROR
;
305 $results['details'] = "MySQL version is {$info}; minimum required is {$min}";
309 $results['details'] = "MySQL version is {$info}";
314 * @param array $db_config
318 public function checkMysqlInnodb(array $db_config) {
320 'title' => 'CiviCRM InnoDB support',
321 'severity' => $this::REQUIREMENT_ERROR
,
322 'details' => 'Could not determine if MySQL has InnoDB support. Assuming none.',
325 $conn = $this->connect($db_config);
330 $innodb_support = FALSE;
331 $result = mysqli_query($conn, "SHOW ENGINES");
332 while ($values = mysqli_fetch_array($result)) {
333 if ($values['Engine'] == 'InnoDB') {
334 if (strtolower($values['Support']) == 'yes' ||
strtolower($values['Support']) == 'default') {
335 $innodb_support = TRUE;
341 if ($innodb_support) {
342 $results['severity'] = $this::REQUIREMENT_OK
;
343 $results['details'] = 'MySQL supports InnoDB';
349 * @param array $db_config
353 public function checkMysqlTempTables(array $db_config) {
355 'title' => 'CiviCRM MySQL Temp Tables',
356 'severity' => $this::REQUIREMENT_OK
,
357 'details' => 'MySQL server supports temporary tables',
360 $conn = $this->connect($db_config);
362 $results['severity'] = $this::REQUIREMENT_ERROR
;
363 $results['details'] = "Could not connect to database";
367 if (!@mysqli_select_db
($conn, $db_config['database'])) {
368 $results['severity'] = $this::REQUIREMENT_ERROR
;
369 $results['details'] = "Could not select the database";
373 $r = mysqli_query($conn, 'CREATE TEMPORARY TABLE civicrm_install_temp_table_test (test text)');
375 $results['severity'] = $this::REQUIREMENT_ERROR
;
376 $results['details'] = "Database does not support creation of temporary tables";
380 mysqli_query($conn, 'DROP TEMPORARY TABLE civicrm_install_temp_table_test');
389 public function checkMysqlTrigger($db_config) {
391 'title' => 'CiviCRM MySQL Trigger',
392 'severity' => $this::REQUIREMENT_OK
,
393 'details' => 'Database supports MySQL triggers',
396 $conn = $this->connect($db_config);
398 $results['severity'] = $this::REQUIREMENT_ERROR
;
399 $results['details'] = 'Could not connect to database';
403 if (!@mysqli_select_db
($conn, $db_config['database'])) {
404 $results['severity'] = $this::REQUIREMENT_ERROR
;
405 $results['details'] = "Could not select the database";
409 $r = mysqli_query($conn, 'CREATE TABLE civicrm_install_temp_table_test (test text)');
411 $results['severity'] = $this::REQUIREMENT_ERROR
;
412 $results['details'] = 'Could not create a table to run test';
416 $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');
418 $results['severity'] = $this::REQUIREMENT_ERROR
;
419 $results['details'] = 'Database does not support creation of triggers';
422 mysqli_query($conn, 'DROP TRIGGER civicrm_install_temp_table_test_trigger');
425 mysqli_query($conn, 'DROP TABLE civicrm_install_temp_table_test');
430 * @param array $db_config
434 public function checkMySQLAutoIncrementIncrementOne(array $db_config) {
436 'title' => 'CiviCRM MySQL AutoIncrementIncrement',
437 'severity' => $this::REQUIREMENT_OK
,
438 'details' => 'MySQL server auto_increment_increment is 1',
441 $conn = $this->connect($db_config);
443 $results['severity'] = $this::REQUIREMENT_ERROR
;
444 $results['details'] = 'Could not connect to database';
448 $r = mysqli_query($conn, "SHOW variables like 'auto_increment_increment'");
450 $results['severity'] = $this::REQUIREMENT_ERROR
;
451 $results['details'] = 'Could not query database server variables';
455 $values = mysqli_fetch_row($r);
456 if ($values[1] != 1) {
457 $results['severity'] = $this::REQUIREMENT_ERROR
;
458 $results['details'] = 'MySQL server auto_increment_increment is not 1';
468 public function checkMysqlThreadStack($db_config) {
469 $min_thread_stack = 192;
472 'title' => 'CiviCRM Mysql thread stack',
473 'severity' => $this::REQUIREMENT_OK
,
474 'details' => 'MySQL thread_stack is OK',
477 $conn = $this->connect($db_config);
479 $results['severity'] = $this::REQUIREMENT_ERROR
;
480 $results['details'] = 'Could not connect to database';
484 if (!@mysqli_select_db
($conn, $db_config['database'])) {
485 $results['severity'] = $this::REQUIREMENT_ERROR
;
486 $results['details'] = 'Could not select the database';
490 $r = mysqli_query($conn, "SHOW VARIABLES LIKE 'thread_stack'"); // bytes => kb
492 $results['severity'] = $this::REQUIREMENT_ERROR
;
493 $results['details'] = 'Could not query thread_stack value';
496 $values = mysqli_fetch_row($r);
497 if ($values[1] < (1024 * $min_thread_stack)) {
498 $results['severity'] = $this::REQUIREMENT_ERROR
;
499 $results['details'] = 'MySQL thread_stack is ' . ($values[1] / 1024) . "kb (minimum required is {$min_thread_stack} kb";
511 public function checkMysqlLockTables($db_config) {
513 'title' => 'CiviCRM MySQL Lock Tables',
514 'severity' => $this::REQUIREMENT_OK
,
515 'details' => 'Can successfully lock and unlock tables',
518 $conn = $this->connect($db_config);
520 $results['severity'] = $this::REQUIREMENT_ERROR
;
521 $results['details'] = 'Could not connect to database';
525 if (!@mysqli_select_db
($conn, $db_config['database'])) {
526 $results['severity'] = $this::REQUIREMENT_ERROR
;
527 $results['details'] = 'Could not select the database';
532 $r = mysqli_query($conn, 'CREATE TEMPORARY TABLE civicrm_install_temp_table_test (test text)');
534 $results['severity'] = $this::REQUIREMENT_ERROR
;
535 $results['details'] = 'Could not create a table';
540 $r = mysqli_query($conn, 'LOCK TABLES civicrm_install_temp_table_test WRITE');
542 $results['severity'] = $this::REQUIREMENT_ERROR
;
543 $results['details'] = 'Could not obtain a write lock';
548 $r = mysqli_query($conn, 'UNLOCK TABLES');
550 $results['severity'] = $this::REQUIREMENT_ERROR
;
551 $results['details'] = 'Could not release table lock';
563 public function checkFilepathIsWritable($file_paths) {
565 'title' => 'CiviCRM directories are writable',
566 'severity' => $this::REQUIREMENT_OK
,
567 'details' => 'All required directories are writable: ' . implode(', ', $file_paths),
570 $unwritable_dirs = array();
571 foreach ($file_paths as $path) {
572 if (!is_writable($path)) {
573 $unwritable_dirs[] = $path;
577 if ($unwritable_dirs) {
578 $results['severity'] = $this::REQUIREMENT_ERROR
;
579 $results['details'] = "The following directories need to be made writable by the webserver: " . implode(', ', $unwritable_dirs);