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',
33 protected $database_checks = array(
34 'checkMysqlConnection',
37 'checkMysqlTempTables',
38 'checkMySQLAutoIncrementIncrementOne',
40 'checkMysqlThreadStack',
41 'checkMysqlLockTables',
45 * Run all requirements tests.
47 * @param array $config
48 * An array with two keys:
53 * An array of check summaries. Each array contains the keys 'title', 'severity', and 'details'.
55 public function checkAll(array $config) {
56 return array_merge($this->checkSystem($config['file_paths']), $this->checkDatabase($config['db_config']));
60 * Check system requirements are met, such as sufficient memory,
61 * necessary file paths are writable and required php extensions
64 * @param array $file_paths
65 * An array of file paths that will be checked to confirm they
70 public function checkSystem(array $file_paths) {
73 $errors[] = $this->checkFilepathIsWritable($file_paths);
74 foreach ($this->system_checks
as $check) {
75 $errors[] = $this->$check();
82 * Check database connection, database version and other
83 * database requirements are met.
85 * @param array $db_config
87 * - host (with optional port specified eg. localhost:12345)
88 * = database (name of database to select)
94 public function checkDatabase(array $db_config) {
97 foreach ($this->database_checks
as $check) {
98 $errors[] = $this->$check($db_config);
105 * Check configured php Memory
108 public function checkMemory() {
109 $min = 1024 * 1024 * 32;
110 $recommended = 1024 * 1024 * 64;
112 $mem = $this->getPHPMemory();
113 $mem_string = ini_get('memory_limit');
116 'title' => 'CiviCRM memory check',
117 'severity' => $this::REQUIREMENT_OK
,
118 'details' => "You have $mem_string allocated (minimum 32Mb, recommended 64Mb)",
121 if ($mem < $min && $mem > 0) {
122 $results['severity'] = $this::REQUIREMENT_ERROR
;
124 elseif ($mem < $recommended && $mem != 0) {
125 $results['severity'] = $this::REQUIREMENT_WARNING
;
128 $results['details'] = "Cannot determine PHP memory allocation. Install only if you're sure you've allocated at least 32 MB.";
129 $results['severity'] = $this::REQUIREMENT_WARNING
;
136 * Get Configured PHP memory
139 protected function getPHPMemory() {
140 $memString = ini_get("memory_limit");
142 switch (strtolower(substr($memString, -1))) {
144 return round(substr($memString, 0, -1) * 1024);
146 return round(substr($memString, 0, -1) * 1024 * 1024);
148 return round(substr($memString, 0, -1) * 1024 * 1024 * 1024);
150 return round($memString);
157 public function checkServerVariables() {
159 'title' => 'CiviCRM PHP server variables',
160 'severity' => $this::REQUIREMENT_OK
,
161 'details' => 'The required $_SERVER variables are set',
164 $required_variables = array('SCRIPT_NAME', 'HTTP_HOST', 'SCRIPT_FILENAME');
167 foreach ($required_variables as $required_variable) {
168 if (empty($_SERVER[$required_variable])) {
169 $missing[] = '$_SERVER[' . $required_variable . ']';
174 $results['severity'] = $this::REQUIREMENT_ERROR
;
175 $results['details'] = 'The following PHP variables are not set: ' . implode(', ', $missing);
184 public function checkJsonEncodeExists() {
186 'title' => 'CiviCRM JSON encoding support',
187 'severity' => $this::REQUIREMENT_OK
,
188 'details' => 'Function json_encode() found',
190 if (!function_exists('json_encode')) {
191 $results['severity'] = $this::REQUIREMENT_ERROR
;
192 $results['details'] = 'Function json_encode() does not exist';
201 public function checkMysqlConnectExists() {
203 'title' => 'CiviCRM MySQL check',
204 'severity' => $this::REQUIREMENT_OK
,
205 'details' => 'Function mysql_connect() found',
207 if (!function_exists('mysql_connect')) {
208 $results['severity'] = $this::REQUIREMENT_ERROR
;
209 $results['details'] = 'Function mysql_connect() does not exist';
216 * @param array $db_config
220 public function checkMysqlConnection(array $db_config) {
222 'title' => 'CiviCRM MySQL connection',
223 'severity' => $this::REQUIREMENT_OK
,
224 'details' => "Connected",
227 $conn = @mysql_connect
($db_config['host'], $db_config['username'], $db_config['password']);
230 $results['details'] = mysql_error();
231 $results['severity'] = $this::REQUIREMENT_ERROR
;
235 if (!@mysql_select_db
($db_config['database'], $conn)) {
236 $results['details'] = mysql_error();
237 $results['severity'] = $this::REQUIREMENT_ERROR
;
245 * @param array $db_config
249 public function checkMysqlVersion(array $db_config) {
252 'title' => 'CiviCRM MySQL Version',
253 'severity' => $this::REQUIREMENT_OK
,
256 $conn = @mysql_connect
($db_config['host'], $db_config['username'], $db_config['password']);
257 if (!$conn ||
!($info = mysql_get_server_info($conn))) {
258 $results['severity'] = $this::REQUIREMENT_WARNING
;
259 $results['details'] = "Cannot determine the version of MySQL installed. Please ensure at least version {$min} is installed.";
263 if (version_compare($info, $min) == -1) {
264 $results['severity'] = $this::REQUIREMENT_ERROR
;
265 $results['details'] = "MySQL version is {$info}; minimum required is {$min}";
269 $results['details'] = "MySQL version is {$info}";
274 * @param array $db_config
278 public function checkMysqlInnodb(array $db_config) {
280 'title' => 'CiviCRM InnoDB support',
281 'severity' => $this::REQUIREMENT_ERROR
,
282 'details' => 'Could not determine if MySQL has InnoDB support. Assuming none.'
285 $conn = @mysql_connect
($db_config['host'], $db_config['username'], $db_config['password']);
290 $innodb_support = FALSE;
291 $result = mysql_query("SHOW ENGINES", $conn);
292 while ($values = mysql_fetch_array($result)) {
293 if ($values['Engine'] == 'InnoDB') {
294 if (strtolower($values['Support']) == 'yes' ||
strtolower($values['Support']) == 'default') {
295 $innodb_support = TRUE;
301 if ($innodb_support) {
302 $results['severity'] = $this::REQUIREMENT_OK
;
303 $results['details'] = 'MySQL supports InnoDB';
309 * @param array $db_config
313 public function checkMysqlTempTables(array $db_config) {
315 'title' => 'CiviCRM MySQL Temp Tables',
316 'severity' => $this::REQUIREMENT_OK
,
317 'details' => 'MySQL server supports temporary tables',
320 $conn = @mysql_connect
($db_config['host'], $db_config['username'], $db_config['password']);
322 $results['severity'] = $this::REQUIREMENT_ERROR
;
323 $results['details'] = "Could not connect to database";
327 if (!@mysql_select_db
($db_config['database'], $conn)) {
328 $results['severity'] = $this::REQUIREMENT_ERROR
;
329 $results['details'] = "Could not select the database";
333 $r = mysql_query('CREATE TEMPORARY TABLE civicrm_install_temp_table_test (test text)', $conn);
335 $results['severity'] = $this::REQUIREMENT_ERROR
;
336 $results['details'] = "Database does not support creation of temporary tables";
340 mysql_query('DROP TEMPORARY TABLE civicrm_install_temp_table_test');
349 public function checkMysqlTrigger($db_config) {
351 'title' => 'CiviCRM MySQL Trigger',
352 'severity' => $this::REQUIREMENT_OK
,
353 'details' => 'Database supports MySQL triggers',
356 $conn = @mysql_connect
($db_config['host'], $db_config['username'], $db_config['password']);
358 $results['severity'] = $this::REQUIREMENT_ERROR
;
359 $results['details'] = 'Could not connect to database';
363 if (!@mysql_select_db
($db_config['database'], $conn)) {
364 $results['severity'] = $this::REQUIREMENT_ERROR
;
365 $results['details'] = "Could not select the database";
369 $r = mysql_query('CREATE TABLE civicrm_install_temp_table_test (test text)', $conn);
371 $results['severity'] = $this::REQUIREMENT_ERROR
;
372 $results['details'] = 'Could not create a table to run test';
376 $r = mysql_query('CREATE TRIGGER civicrm_install_temp_table_test_trigger BEFORE INSERT ON civicrm_install_temp_table_test FOR EACH ROW BEGIN END');
378 $results['severity'] = $this::REQUIREMENT_ERROR
;
379 $results['details'] = 'Database does not support creation of triggers';
382 mysql_query('DROP TRIGGER civicrm_install_temp_table_test_trigger');
385 mysql_query('DROP TABLE civicrm_install_temp_table_test');
390 * @param array $db_config
394 public function checkMySQLAutoIncrementIncrementOne(array $db_config) {
396 'title' => 'CiviCRM MySQL AutoIncrementIncrement',
397 'severity' => $this::REQUIREMENT_OK
,
398 'details' => 'MySQL server auto_increment_increment is 1',
401 $conn = @mysql_connect
($db_config['host'], $db_config['username'], $db_config['password']);
403 $results['severity'] = $this::REQUIREMENT_ERROR
;
404 $results['details'] = 'Could not connect to database';
408 $r = mysql_query("SHOW variables like 'auto_increment_increment'", $conn);
410 $results['severity'] = $this::REQUIREMENT_ERROR
;
411 $results['details'] = 'Could not query database server variables';
415 $values = mysql_fetch_row($r);
416 if ($values[1] != 1) {
417 $results['severity'] = $this::REQUIREMENT_ERROR
;
418 $results['details'] = 'MySQL server auto_increment_increment is not 1';
428 public function checkMysqlThreadStack($db_config) {
429 $min_thread_stack = 192;
432 'title' => 'CiviCRM Mysql thread stack',
433 'severity' => $this::REQUIREMENT_OK
,
434 'details' => 'MySQL thread_stack is OK',
437 $conn = @mysql_connect
($db_config['server'], $db_config['username'], $db_config['password']);
439 $results['severity'] = $this::REQUIREMENT_ERROR
;
440 $results['details'] = 'Could not connect to database';
444 if (!@mysql_select_db
($db_config['database'], $conn)) {
445 $results['severity'] = $this::REQUIREMENT_ERROR
;
446 $results['details'] = 'Could not select the database';
450 $r = mysql_query("SHOW VARIABLES LIKE 'thread_stack'", $conn); // bytes => kb
452 $results['severity'] = $this::REQUIREMENT_ERROR
;
453 $results['details'] = 'Could not query thread_stack value';
456 $values = mysql_fetch_row($r);
457 if ($values[1] < (1024 * $min_thread_stack)) {
458 $results['severity'] = $this::REQUIREMENT_ERROR
;
459 $results['details'] = 'MySQL thread_stack is ' . ($values[1] / 1024) . "kb (minimum required is {$min_thread_stack} kb";
471 public function checkMysqlLockTables($db_config) {
473 'title' => 'CiviCRM MySQL Lock Tables',
474 'severity' => $this::REQUIREMENT_OK
,
475 'details' => 'Can successfully lock and unlock tables',
478 $conn = @mysql_connect
($db_config['server'], $db_config['username'], $db_config['password']);
480 $results['severity'] = $this::REQUIREMENT_ERROR
;
481 $results['details'] = 'Could not connect to database';
485 if (!@mysql_select_db
($db_config['database'], $conn)) {
486 $results['severity'] = $this::REQUIREMENT_ERROR
;
487 $results['details'] = 'Could not select the database';
492 $r = mysql_query('CREATE TEMPORARY TABLE civicrm_install_temp_table_test (test text)', $conn);
494 $results['severity'] = $this::REQUIREMENT_ERROR
;
495 $results['details'] = 'Could not create a table';
500 $r = mysql_query('LOCK TABLES civicrm_install_temp_table_test WRITE', $conn);
502 $results['severity'] = $this::REQUIREMENT_ERROR
;
503 $results['details'] = 'Could not obtain a write lock';
508 $r = mysql_query('UNLOCK TABLES', $conn);
510 $results['severity'] = $this::REQUIREMENT_ERROR
;
511 $results['details'] = 'Could not release table lock';
523 public function checkFilepathIsWritable($file_paths) {
525 'title' => 'CiviCRM directories are writable',
526 'severity' => $this::REQUIREMENT_OK
,
527 'details' => 'All required directories are writable: ' . implode(', ', $file_paths),
530 $unwritable_dirs = array();
531 foreach ($file_paths as $path) {
532 if (!is_writable($path)) {
533 $unwritable_dirs[] = $path;
537 if ($unwritable_dirs) {
538 $results['severity'] = $this::REQUIREMENT_ERROR
;
539 $results['details'] = "The following directories need to be made writable by the webserver: " . implode(', ', $unwritable_dirs);