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);
147 return round(substr($memString, 0, -1) * 1024 * 1024);
150 return round(substr($memString, 0, -1) * 1024 * 1024 * 1024);
153 return round($memString);
160 public function checkServerVariables() {
162 'title' => 'CiviCRM PHP server variables',
163 'severity' => $this::REQUIREMENT_OK
,
164 'details' => 'The required $_SERVER variables are set',
167 $required_variables = array('SCRIPT_NAME', 'HTTP_HOST', 'SCRIPT_FILENAME');
170 foreach ($required_variables as $required_variable) {
171 if (empty($_SERVER[$required_variable])) {
172 $missing[] = '$_SERVER[' . $required_variable . ']';
177 $results['severity'] = $this::REQUIREMENT_ERROR
;
178 $results['details'] = 'The following PHP variables are not set: ' . implode(', ', $missing);
187 public function checkJsonEncodeExists() {
189 'title' => 'CiviCRM JSON encoding support',
190 'severity' => $this::REQUIREMENT_OK
,
191 'details' => 'Function json_encode() found',
193 if (!function_exists('json_encode')) {
194 $results['severity'] = $this::REQUIREMENT_ERROR
;
195 $results['details'] = 'Function json_encode() does not exist';
204 public function checkMysqlConnectExists() {
206 'title' => 'CiviCRM MySQL check',
207 'severity' => $this::REQUIREMENT_OK
,
208 'details' => 'Function mysql_connect() found',
210 if (!function_exists('mysql_connect')) {
211 $results['severity'] = $this::REQUIREMENT_ERROR
;
212 $results['details'] = 'Function mysql_connect() does not exist';
219 * @param array $db_config
223 public function checkMysqlConnection(array $db_config) {
225 'title' => 'CiviCRM MySQL connection',
226 'severity' => $this::REQUIREMENT_OK
,
227 'details' => "Connected",
230 $conn = @mysql_connect
($db_config['host'], $db_config['username'], $db_config['password']);
233 $results['details'] = mysql_error();
234 $results['severity'] = $this::REQUIREMENT_ERROR
;
238 if (!@mysql_select_db
($db_config['database'], $conn)) {
239 $results['details'] = mysql_error();
240 $results['severity'] = $this::REQUIREMENT_ERROR
;
248 * @param array $db_config
252 public function checkMysqlVersion(array $db_config) {
255 'title' => 'CiviCRM MySQL Version',
256 'severity' => $this::REQUIREMENT_OK
,
259 $conn = @mysql_connect
($db_config['host'], $db_config['username'], $db_config['password']);
260 if (!$conn ||
!($info = mysql_get_server_info($conn))) {
261 $results['severity'] = $this::REQUIREMENT_WARNING
;
262 $results['details'] = "Cannot determine the version of MySQL installed. Please ensure at least version {$min} is installed.";
266 if (version_compare($info, $min) == -1) {
267 $results['severity'] = $this::REQUIREMENT_ERROR
;
268 $results['details'] = "MySQL version is {$info}; minimum required is {$min}";
272 $results['details'] = "MySQL version is {$info}";
277 * @param array $db_config
281 public function checkMysqlInnodb(array $db_config) {
283 'title' => 'CiviCRM InnoDB support',
284 'severity' => $this::REQUIREMENT_ERROR
,
285 'details' => 'Could not determine if MySQL has InnoDB support. Assuming none.',
288 $conn = @mysql_connect
($db_config['host'], $db_config['username'], $db_config['password']);
293 $innodb_support = FALSE;
294 $result = mysql_query("SHOW ENGINES", $conn);
295 while ($values = mysql_fetch_array($result)) {
296 if ($values['Engine'] == 'InnoDB') {
297 if (strtolower($values['Support']) == 'yes' ||
strtolower($values['Support']) == 'default') {
298 $innodb_support = TRUE;
304 if ($innodb_support) {
305 $results['severity'] = $this::REQUIREMENT_OK
;
306 $results['details'] = 'MySQL supports InnoDB';
312 * @param array $db_config
316 public function checkMysqlTempTables(array $db_config) {
318 'title' => 'CiviCRM MySQL Temp Tables',
319 'severity' => $this::REQUIREMENT_OK
,
320 'details' => 'MySQL server supports temporary tables',
323 $conn = @mysql_connect
($db_config['host'], $db_config['username'], $db_config['password']);
325 $results['severity'] = $this::REQUIREMENT_ERROR
;
326 $results['details'] = "Could not connect to database";
330 if (!@mysql_select_db
($db_config['database'], $conn)) {
331 $results['severity'] = $this::REQUIREMENT_ERROR
;
332 $results['details'] = "Could not select the database";
336 $r = mysql_query('CREATE TEMPORARY TABLE civicrm_install_temp_table_test (test text)', $conn);
338 $results['severity'] = $this::REQUIREMENT_ERROR
;
339 $results['details'] = "Database does not support creation of temporary tables";
343 mysql_query('DROP TEMPORARY TABLE civicrm_install_temp_table_test');
352 public function checkMysqlTrigger($db_config) {
354 'title' => 'CiviCRM MySQL Trigger',
355 'severity' => $this::REQUIREMENT_OK
,
356 'details' => 'Database supports MySQL triggers',
359 $conn = @mysql_connect
($db_config['host'], $db_config['username'], $db_config['password']);
361 $results['severity'] = $this::REQUIREMENT_ERROR
;
362 $results['details'] = 'Could not connect to database';
366 if (!@mysql_select_db
($db_config['database'], $conn)) {
367 $results['severity'] = $this::REQUIREMENT_ERROR
;
368 $results['details'] = "Could not select the database";
372 $r = mysql_query('CREATE TABLE civicrm_install_temp_table_test (test text)', $conn);
374 $results['severity'] = $this::REQUIREMENT_ERROR
;
375 $results['details'] = 'Could not create a table to run test';
379 $r = mysql_query('CREATE TRIGGER civicrm_install_temp_table_test_trigger BEFORE INSERT ON civicrm_install_temp_table_test FOR EACH ROW BEGIN END');
381 $results['severity'] = $this::REQUIREMENT_ERROR
;
382 $results['details'] = 'Database does not support creation of triggers';
385 mysql_query('DROP TRIGGER civicrm_install_temp_table_test_trigger');
388 mysql_query('DROP TABLE civicrm_install_temp_table_test');
393 * @param array $db_config
397 public function checkMySQLAutoIncrementIncrementOne(array $db_config) {
399 'title' => 'CiviCRM MySQL AutoIncrementIncrement',
400 'severity' => $this::REQUIREMENT_OK
,
401 'details' => 'MySQL server auto_increment_increment is 1',
404 $conn = @mysql_connect
($db_config['host'], $db_config['username'], $db_config['password']);
406 $results['severity'] = $this::REQUIREMENT_ERROR
;
407 $results['details'] = 'Could not connect to database';
411 $r = mysql_query("SHOW variables like 'auto_increment_increment'", $conn);
413 $results['severity'] = $this::REQUIREMENT_ERROR
;
414 $results['details'] = 'Could not query database server variables';
418 $values = mysql_fetch_row($r);
419 if ($values[1] != 1) {
420 $results['severity'] = $this::REQUIREMENT_ERROR
;
421 $results['details'] = 'MySQL server auto_increment_increment is not 1';
431 public function checkMysqlThreadStack($db_config) {
432 $min_thread_stack = 192;
435 'title' => 'CiviCRM Mysql thread stack',
436 'severity' => $this::REQUIREMENT_OK
,
437 'details' => 'MySQL thread_stack is OK',
440 $conn = @mysql_connect
($db_config['server'], $db_config['username'], $db_config['password']);
442 $results['severity'] = $this::REQUIREMENT_ERROR
;
443 $results['details'] = 'Could not connect to database';
447 if (!@mysql_select_db
($db_config['database'], $conn)) {
448 $results['severity'] = $this::REQUIREMENT_ERROR
;
449 $results['details'] = 'Could not select the database';
453 $r = mysql_query("SHOW VARIABLES LIKE 'thread_stack'", $conn); // bytes => kb
455 $results['severity'] = $this::REQUIREMENT_ERROR
;
456 $results['details'] = 'Could not query thread_stack value';
459 $values = mysql_fetch_row($r);
460 if ($values[1] < (1024 * $min_thread_stack)) {
461 $results['severity'] = $this::REQUIREMENT_ERROR
;
462 $results['details'] = 'MySQL thread_stack is ' . ($values[1] / 1024) . "kb (minimum required is {$min_thread_stack} kb";
474 public function checkMysqlLockTables($db_config) {
476 'title' => 'CiviCRM MySQL Lock Tables',
477 'severity' => $this::REQUIREMENT_OK
,
478 'details' => 'Can successfully lock and unlock tables',
481 $conn = @mysql_connect
($db_config['server'], $db_config['username'], $db_config['password']);
483 $results['severity'] = $this::REQUIREMENT_ERROR
;
484 $results['details'] = 'Could not connect to database';
488 if (!@mysql_select_db
($db_config['database'], $conn)) {
489 $results['severity'] = $this::REQUIREMENT_ERROR
;
490 $results['details'] = 'Could not select the database';
495 $r = mysql_query('CREATE TEMPORARY TABLE civicrm_install_temp_table_test (test text)', $conn);
497 $results['severity'] = $this::REQUIREMENT_ERROR
;
498 $results['details'] = 'Could not create a table';
503 $r = mysql_query('LOCK TABLES civicrm_install_temp_table_test WRITE', $conn);
505 $results['severity'] = $this::REQUIREMENT_ERROR
;
506 $results['details'] = 'Could not obtain a write lock';
511 $r = mysql_query('UNLOCK TABLES', $conn);
513 $results['severity'] = $this::REQUIREMENT_ERROR
;
514 $results['details'] = 'Could not release table lock';
526 public function checkFilepathIsWritable($file_paths) {
528 'title' => 'CiviCRM directories are writable',
529 'severity' => $this::REQUIREMENT_OK
,
530 'details' => 'All required directories are writable: ' . implode(', ', $file_paths),
533 $unwritable_dirs = array();
534 foreach ($file_paths as $path) {
535 if (!is_writable($path)) {
536 $unwritable_dirs[] = $path;
540 if ($unwritable_dirs) {
541 $results['severity'] = $this::REQUIREMENT_ERROR
;
542 $results['details'] = "The following directories need to be made writable by the webserver: " . implode(', ', $unwritable_dirs);