Commit | Line | Data |
---|---|---|
d7dea16c EM |
1 | <?php |
2 | ||
3 | namespace Civi\Install; | |
4 | ||
5 | /** | |
6 | * Class Requirements | |
7 | * @package Civi\Install | |
8 | */ | |
9 | class Requirements { | |
10 | ||
11 | /** | |
12 | * Requirement severity -- Requirement successfully met. | |
13 | */ | |
14 | const REQUIREMENT_OK = 0; | |
15 | ||
16 | /** | |
17 | * Requirement severity -- Warning condition; proceed but flag warning. | |
18 | */ | |
19 | const REQUIREMENT_WARNING = 1; | |
20 | ||
21 | /** | |
22 | * Requirement severity -- Error condition; abort installation. | |
23 | */ | |
24 | const REQUIREMENT_ERROR = 2; | |
25 | ||
34f3bbd9 SL |
26 | /** |
27 | * @var array | |
28 | */ | |
c64f69d9 | 29 | protected $system_checks = [ |
d7dea16c | 30 | 'checkMemory', |
d7dea16c EM |
31 | 'checkMysqlConnectExists', |
32 | 'checkJsonEncodeExists', | |
9ad2ba41 | 33 | 'checkMultibyteExists', |
c64f69d9 | 34 | ]; |
d7dea16c | 35 | |
8a0e49bc MT |
36 | protected $system_checks_web = [ |
37 | 'checkServerVariables', | |
38 | ]; | |
39 | ||
c64f69d9 | 40 | protected $database_checks = [ |
d7dea16c EM |
41 | 'checkMysqlConnection', |
42 | 'checkMysqlVersion', | |
43 | 'checkMysqlInnodb', | |
44 | 'checkMysqlTempTables', | |
45 | 'checkMySQLAutoIncrementIncrementOne', | |
46 | 'checkMysqlTrigger', | |
47 | 'checkMysqlThreadStack', | |
48 | 'checkMysqlLockTables', | |
75615982 | 49 | 'checkMysqlUtf8mb4', |
c64f69d9 | 50 | ]; |
d7dea16c EM |
51 | |
52 | /** | |
53 | * Run all requirements tests. | |
54 | * | |
55 | * @param array $config | |
56 | * An array with two keys: | |
57 | * - file_paths | |
58 | * - db_config | |
59 | * | |
a6c01b45 CW |
60 | * @return array |
61 | * An array of check summaries. Each array contains the keys 'title', 'severity', and 'details'. | |
d7dea16c EM |
62 | */ |
63 | public function checkAll(array $config) { | |
6ef58b18 SL |
64 | if (!class_exists('\CRM_Utils_SQL_TempTable')) { |
65 | require_once dirname(__FILE__) . '/../../CRM/Utils/SQL/TempTable.php'; | |
66 | } | |
d7dea16c EM |
67 | return array_merge($this->checkSystem($config['file_paths']), $this->checkDatabase($config['db_config'])); |
68 | } | |
69 | ||
70 | /** | |
71 | * Check system requirements are met, such as sufficient memory, | |
72 | * necessary file paths are writable and required php extensions | |
73 | * are available. | |
74 | * | |
75 | * @param array $file_paths | |
76 | * An array of file paths that will be checked to confirm they | |
77 | * are writable. | |
78 | * | |
79 | * @return array | |
80 | */ | |
81 | public function checkSystem(array $file_paths) { | |
c64f69d9 | 82 | $errors = []; |
d7dea16c EM |
83 | |
84 | $errors[] = $this->checkFilepathIsWritable($file_paths); | |
85 | foreach ($this->system_checks as $check) { | |
86 | $errors[] = $this->$check(); | |
87 | } | |
88 | ||
8a0e49bc MT |
89 | if (PHP_SAPI !== 'cli') { |
90 | foreach ($this->system_checks_web as $check) { | |
91 | $errors[] = $this->$check(); | |
92 | } | |
93 | } | |
94 | ||
d7dea16c EM |
95 | return $errors; |
96 | } | |
97 | ||
98 | /** | |
99 | * Check database connection, database version and other | |
100 | * database requirements are met. | |
101 | * | |
102 | * @param array $db_config | |
103 | * An array with keys: | |
104 | * - host (with optional port specified eg. localhost:12345) | |
16b10e64 | 105 | * - database (name of database to select) |
d7dea16c EM |
106 | * - username |
107 | * - password | |
108 | * | |
109 | * @return array | |
110 | */ | |
111 | public function checkDatabase(array $db_config) { | |
c64f69d9 | 112 | $errors = []; |
d7dea16c EM |
113 | |
114 | foreach ($this->database_checks as $check) { | |
115 | $errors[] = $this->$check($db_config); | |
116 | } | |
117 | ||
118 | return $errors; | |
119 | } | |
120 | ||
9043c4bf SL |
121 | /** |
122 | * Generates a mysql connection | |
123 | * | |
34f3bbd9 | 124 | * @param $db_config array |
9043c4bf SL |
125 | * @return object mysqli connection |
126 | */ | |
127 | protected function connect($db_config) { | |
3916ca43 SL |
128 | $host = NULL; |
129 | if (!empty($db_config['host'])) { | |
130 | $host = $db_config['host']; | |
131 | } | |
132 | elseif (!empty($db_config['server'])) { | |
133 | $host = $db_config['server']; | |
134 | } | |
04b1abad | 135 | if (empty($db_config['ssl_params'])) { |
136 | $conn = @mysqli_connect($host, $db_config['username'], $db_config['password'], $db_config['database'], !empty($db_config['port']) ? $db_config['port'] : NULL); | |
137 | } | |
138 | else { | |
139 | $conn = NULL; | |
140 | $init = mysqli_init(); | |
141 | mysqli_ssl_set( | |
142 | $init, | |
143 | $db_config['ssl_params']['key'] ?? NULL, | |
144 | $db_config['ssl_params']['cert'] ?? NULL, | |
145 | $db_config['ssl_params']['ca'] ?? NULL, | |
146 | $db_config['ssl_params']['capath'] ?? NULL, | |
147 | $db_config['ssl_params']['cipher'] ?? NULL | |
148 | ); | |
149 | if (@mysqli_real_connect($init, $host, $db_config['username'], $db_config['password'], $db_config['database'], (!empty($db_config['port']) ? $db_config['port'] : NULL), NULL, MYSQLI_CLIENT_SSL)) { | |
150 | $conn = $init; | |
151 | } | |
152 | } | |
3916ca43 | 153 | return $conn; |
9043c4bf SL |
154 | } |
155 | ||
d7dea16c | 156 | /** |
fe482240 | 157 | * Check configured php Memory. |
d7dea16c EM |
158 | * @return array |
159 | */ | |
160 | public function checkMemory() { | |
161 | $min = 1024 * 1024 * 32; | |
162 | $recommended = 1024 * 1024 * 64; | |
163 | ||
164 | $mem = $this->getPHPMemory(); | |
165 | $mem_string = ini_get('memory_limit'); | |
166 | ||
c64f69d9 | 167 | $results = [ |
d7dea16c EM |
168 | 'title' => 'CiviCRM memory check', |
169 | 'severity' => $this::REQUIREMENT_OK, | |
170 | 'details' => "You have $mem_string allocated (minimum 32Mb, recommended 64Mb)", | |
c64f69d9 | 171 | ]; |
d7dea16c EM |
172 | |
173 | if ($mem < $min && $mem > 0) { | |
174 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
175 | } | |
4c9b6178 | 176 | elseif ($mem < $recommended && $mem != 0) { |
d7dea16c EM |
177 | $results['severity'] = $this::REQUIREMENT_WARNING; |
178 | } | |
4c9b6178 | 179 | elseif ($mem == 0) { |
d7dea16c EM |
180 | $results['details'] = "Cannot determine PHP memory allocation. Install only if you're sure you've allocated at least 32 MB."; |
181 | $results['severity'] = $this::REQUIREMENT_WARNING; | |
182 | } | |
183 | ||
184 | return $results; | |
185 | } | |
186 | ||
187 | /** | |
fe482240 | 188 | * Get Configured PHP memory. |
d7dea16c EM |
189 | * @return float |
190 | */ | |
191 | protected function getPHPMemory() { | |
192 | $memString = ini_get("memory_limit"); | |
193 | ||
194 | switch (strtolower(substr($memString, -1))) { | |
195 | case "k": | |
196 | return round(substr($memString, 0, -1) * 1024); | |
ea100cb5 | 197 | |
d7dea16c EM |
198 | case "m": |
199 | return round(substr($memString, 0, -1) * 1024 * 1024); | |
ea100cb5 | 200 | |
d7dea16c EM |
201 | case "g": |
202 | return round(substr($memString, 0, -1) * 1024 * 1024 * 1024); | |
ea100cb5 | 203 | |
d7dea16c EM |
204 | default: |
205 | return round($memString); | |
206 | } | |
207 | } | |
208 | ||
209 | /** | |
210 | * @return array | |
211 | */ | |
00be9182 | 212 | public function checkServerVariables() { |
c64f69d9 | 213 | $results = [ |
d7dea16c EM |
214 | 'title' => 'CiviCRM PHP server variables', |
215 | 'severity' => $this::REQUIREMENT_OK, | |
216 | 'details' => 'The required $_SERVER variables are set', | |
c64f69d9 | 217 | ]; |
d7dea16c | 218 | |
c64f69d9 CW |
219 | $required_variables = ['SCRIPT_NAME', 'HTTP_HOST', 'SCRIPT_FILENAME']; |
220 | $missing = []; | |
d7dea16c EM |
221 | |
222 | foreach ($required_variables as $required_variable) { | |
223 | if (empty($_SERVER[$required_variable])) { | |
224 | $missing[] = '$_SERVER[' . $required_variable . ']'; | |
225 | } | |
226 | } | |
227 | ||
228 | if ($missing) { | |
229 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
230 | $results['details'] = 'The following PHP variables are not set: ' . implode(', ', $missing); | |
231 | } | |
232 | ||
233 | return $results; | |
234 | } | |
235 | ||
236 | /** | |
237 | * @return array | |
238 | */ | |
239 | public function checkJsonEncodeExists() { | |
c64f69d9 | 240 | $results = [ |
d7dea16c EM |
241 | 'title' => 'CiviCRM JSON encoding support', |
242 | 'severity' => $this::REQUIREMENT_OK, | |
243 | 'details' => 'Function json_encode() found', | |
c64f69d9 | 244 | ]; |
d7dea16c EM |
245 | if (!function_exists('json_encode')) { |
246 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
247 | $results['details'] = 'Function json_encode() does not exist'; | |
248 | } | |
249 | ||
250 | return $results; | |
251 | } | |
252 | ||
9ad2ba41 SL |
253 | /** |
254 | * CHeck that PHP Multibyte functions are enabled. | |
255 | * @return array | |
256 | */ | |
257 | public function checkMultibyteExists() { | |
c64f69d9 | 258 | $results = [ |
9ad2ba41 SL |
259 | 'title' => 'CiviCRM MultiByte encoding support', |
260 | 'severity' => $this::REQUIREMENT_OK, | |
261 | 'details' => 'PHP Multibyte etension found', | |
c64f69d9 | 262 | ]; |
9ad2ba41 SL |
263 | if (!function_exists('mb_substr')) { |
264 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
265 | $results['details'] = 'PHP Multibyte extension has not been installed and enabled'; | |
266 | } | |
267 | ||
268 | return $results; | |
269 | } | |
270 | ||
d7dea16c EM |
271 | /** |
272 | * @return array | |
273 | */ | |
274 | public function checkMysqlConnectExists() { | |
c64f69d9 | 275 | $results = [ |
d7dea16c EM |
276 | 'title' => 'CiviCRM MySQL check', |
277 | 'severity' => $this::REQUIREMENT_OK, | |
fcf908c6 | 278 | 'details' => 'Function mysqli_connect() found', |
c64f69d9 | 279 | ]; |
fcf908c6 | 280 | if (!function_exists('mysqli_connect')) { |
d7dea16c | 281 | $results['severity'] = $this::REQUIREMENT_ERROR; |
fcf908c6 | 282 | $results['details'] = 'Function mysqli_connect() does not exist'; |
d7dea16c EM |
283 | } |
284 | ||
285 | return $results; | |
286 | } | |
287 | ||
288 | /** | |
289 | * @param array $db_config | |
290 | * | |
291 | * @return array | |
292 | */ | |
293 | public function checkMysqlConnection(array $db_config) { | |
c64f69d9 | 294 | $results = [ |
d7dea16c EM |
295 | 'title' => 'CiviCRM MySQL connection', |
296 | 'severity' => $this::REQUIREMENT_OK, | |
297 | 'details' => "Connected", | |
c64f69d9 | 298 | ]; |
d7dea16c | 299 | |
9043c4bf | 300 | $conn = $this->connect($db_config); |
d7dea16c EM |
301 | |
302 | if (!$conn) { | |
fcf908c6 | 303 | $results['details'] = mysqli_connect_error(); |
d7dea16c EM |
304 | $results['severity'] = $this::REQUIREMENT_ERROR; |
305 | return $results; | |
306 | } | |
307 | ||
fcf908c6 | 308 | if (!@mysqli_select_db($conn, $db_config['database'])) { |
309 | $results['details'] = mysqli_error($conn); | |
d7dea16c EM |
310 | $results['severity'] = $this::REQUIREMENT_ERROR; |
311 | return $results; | |
312 | } | |
313 | ||
314 | return $results; | |
315 | } | |
316 | ||
317 | /** | |
318 | * @param array $db_config | |
319 | * | |
320 | * @return array | |
321 | */ | |
322 | public function checkMysqlVersion(array $db_config) { | |
edaacca6 SL |
323 | if (!class_exists('\CRM_Upgrade_Incremental_General')) { |
324 | require_once dirname(__FILE__) . '/../../CRM/Upgrade/Incremental/General.php'; | |
325 | } | |
010e8b6c | 326 | $min = \CRM_Upgrade_Incremental_General::MIN_INSTALL_MYSQL_VER; |
c64f69d9 | 327 | $results = [ |
d7dea16c EM |
328 | 'title' => 'CiviCRM MySQL Version', |
329 | 'severity' => $this::REQUIREMENT_OK, | |
c64f69d9 | 330 | ]; |
d7dea16c | 331 | |
9043c4bf | 332 | $conn = $this->connect($db_config); |
fcf908c6 | 333 | if (!$conn || !($info = mysqli_get_server_info($conn))) { |
d7dea16c EM |
334 | $results['severity'] = $this::REQUIREMENT_WARNING; |
335 | $results['details'] = "Cannot determine the version of MySQL installed. Please ensure at least version {$min} is installed."; | |
336 | return $results; | |
337 | } | |
338 | ||
66c0d0bd SL |
339 | $versionDetails = mysqli_query($conn, 'SELECT version() as version')->fetch_assoc(); |
340 | if (version_compare($versionDetails['version'], $min) == -1) { | |
d7dea16c EM |
341 | $results['severity'] = $this::REQUIREMENT_ERROR; |
342 | $results['details'] = "MySQL version is {$info}; minimum required is {$min}"; | |
343 | return $results; | |
344 | } | |
345 | ||
346 | $results['details'] = "MySQL version is {$info}"; | |
347 | return $results; | |
348 | } | |
349 | ||
350 | /** | |
351 | * @param array $db_config | |
352 | * | |
353 | * @return array | |
354 | */ | |
355 | public function checkMysqlInnodb(array $db_config) { | |
c64f69d9 | 356 | $results = [ |
d7dea16c EM |
357 | 'title' => 'CiviCRM InnoDB support', |
358 | 'severity' => $this::REQUIREMENT_ERROR, | |
87a890cc | 359 | 'details' => 'Could not determine if MySQL has InnoDB support. Assuming none.', |
c64f69d9 | 360 | ]; |
d7dea16c | 361 | |
9043c4bf | 362 | $conn = $this->connect($db_config); |
d7dea16c EM |
363 | if (!$conn) { |
364 | return $results; | |
365 | } | |
366 | ||
367 | $innodb_support = FALSE; | |
fcf908c6 | 368 | $result = mysqli_query($conn, "SHOW ENGINES"); |
369 | while ($values = mysqli_fetch_array($result)) { | |
d7dea16c EM |
370 | if ($values['Engine'] == 'InnoDB') { |
371 | if (strtolower($values['Support']) == 'yes' || strtolower($values['Support']) == 'default') { | |
372 | $innodb_support = TRUE; | |
373 | break; | |
374 | } | |
375 | } | |
376 | } | |
377 | ||
378 | if ($innodb_support) { | |
379 | $results['severity'] = $this::REQUIREMENT_OK; | |
380 | $results['details'] = 'MySQL supports InnoDB'; | |
381 | } | |
382 | return $results; | |
383 | } | |
384 | ||
385 | /** | |
386 | * @param array $db_config | |
387 | * | |
388 | * @return array | |
389 | */ | |
390 | public function checkMysqlTempTables(array $db_config) { | |
c64f69d9 | 391 | $results = [ |
d7dea16c EM |
392 | 'title' => 'CiviCRM MySQL Temp Tables', |
393 | 'severity' => $this::REQUIREMENT_OK, | |
394 | 'details' => 'MySQL server supports temporary tables', | |
c64f69d9 | 395 | ]; |
d7dea16c | 396 | |
9043c4bf | 397 | $conn = $this->connect($db_config); |
d7dea16c EM |
398 | if (!$conn) { |
399 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
400 | $results['details'] = "Could not connect to database"; | |
401 | return $results; | |
402 | } | |
403 | ||
fcf908c6 | 404 | if (!@mysqli_select_db($conn, $db_config['database'])) { |
d7dea16c EM |
405 | $results['severity'] = $this::REQUIREMENT_ERROR; |
406 | $results['details'] = "Could not select the database"; | |
407 | return $results; | |
408 | } | |
6ef58b18 SL |
409 | $temporaryTableName = \CRM_Utils_SQL_TempTable::build()->setCategory('install')->getName(); |
410 | $r = mysqli_query($conn, 'CREATE TEMPORARY TABLE ' . $temporaryTableName . ' (test text)'); | |
d7dea16c EM |
411 | if (!$r) { |
412 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
413 | $results['details'] = "Database does not support creation of temporary tables"; | |
414 | return $results; | |
415 | } | |
416 | ||
6ef58b18 | 417 | mysqli_query($conn, 'DROP TEMPORARY TABLE ' . $temporaryTableName); |
d7dea16c EM |
418 | return $results; |
419 | } | |
420 | ||
421 | /** | |
422 | * @param $db_config | |
423 | * | |
424 | * @return array | |
425 | */ | |
426 | public function checkMysqlTrigger($db_config) { | |
c64f69d9 | 427 | $results = [ |
d7dea16c EM |
428 | 'title' => 'CiviCRM MySQL Trigger', |
429 | 'severity' => $this::REQUIREMENT_OK, | |
430 | 'details' => 'Database supports MySQL triggers', | |
c64f69d9 | 431 | ]; |
d7dea16c | 432 | |
9043c4bf | 433 | $conn = $this->connect($db_config); |
d7dea16c EM |
434 | if (!$conn) { |
435 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
436 | $results['details'] = 'Could not connect to database'; | |
437 | return $results; | |
438 | } | |
439 | ||
fcf908c6 | 440 | if (!@mysqli_select_db($conn, $db_config['database'])) { |
d7dea16c EM |
441 | $results['severity'] = $this::REQUIREMENT_ERROR; |
442 | $results['details'] = "Could not select the database"; | |
443 | return $results; | |
444 | } | |
445 | ||
fcf908c6 | 446 | $r = mysqli_query($conn, 'CREATE TABLE civicrm_install_temp_table_test (test text)'); |
d7dea16c EM |
447 | if (!$r) { |
448 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
449 | $results['details'] = 'Could not create a table to run test'; | |
450 | return $results; | |
451 | } | |
452 | ||
fcf908c6 | 453 | $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'); |
d7dea16c EM |
454 | if (!$r) { |
455 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
456 | $results['details'] = 'Database does not support creation of triggers'; | |
457 | } | |
458 | else { | |
fcf908c6 | 459 | mysqli_query($conn, 'DROP TRIGGER civicrm_install_temp_table_test_trigger'); |
d7dea16c EM |
460 | } |
461 | ||
fcf908c6 | 462 | mysqli_query($conn, 'DROP TABLE civicrm_install_temp_table_test'); |
d7dea16c EM |
463 | return $results; |
464 | } | |
465 | ||
466 | /** | |
467 | * @param array $db_config | |
468 | * | |
469 | * @return array | |
470 | */ | |
471 | public function checkMySQLAutoIncrementIncrementOne(array $db_config) { | |
c64f69d9 | 472 | $results = [ |
d7dea16c EM |
473 | 'title' => 'CiviCRM MySQL AutoIncrementIncrement', |
474 | 'severity' => $this::REQUIREMENT_OK, | |
475 | 'details' => 'MySQL server auto_increment_increment is 1', | |
c64f69d9 | 476 | ]; |
d7dea16c | 477 | |
9043c4bf | 478 | $conn = $this->connect($db_config); |
d7dea16c EM |
479 | if (!$conn) { |
480 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
481 | $results['details'] = 'Could not connect to database'; | |
482 | return $results; | |
483 | } | |
484 | ||
fcf908c6 | 485 | $r = mysqli_query($conn, "SHOW variables like 'auto_increment_increment'"); |
d7dea16c EM |
486 | if (!$r) { |
487 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
488 | $results['details'] = 'Could not query database server variables'; | |
489 | return $results; | |
490 | } | |
491 | ||
fcf908c6 | 492 | $values = mysqli_fetch_row($r); |
d7dea16c EM |
493 | if ($values[1] != 1) { |
494 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
495 | $results['details'] = 'MySQL server auto_increment_increment is not 1'; | |
496 | } | |
497 | return $results; | |
498 | } | |
499 | ||
500 | /** | |
501 | * @param $db_config | |
502 | * | |
503 | * @return array | |
504 | */ | |
505 | public function checkMysqlThreadStack($db_config) { | |
506 | $min_thread_stack = 192; | |
507 | ||
c64f69d9 | 508 | $results = [ |
d7dea16c EM |
509 | 'title' => 'CiviCRM Mysql thread stack', |
510 | 'severity' => $this::REQUIREMENT_OK, | |
511 | 'details' => 'MySQL thread_stack is OK', | |
c64f69d9 | 512 | ]; |
d7dea16c | 513 | |
9043c4bf | 514 | $conn = $this->connect($db_config); |
d7dea16c EM |
515 | if (!$conn) { |
516 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
517 | $results['details'] = 'Could not connect to database'; | |
518 | return $results; | |
519 | } | |
520 | ||
fcf908c6 | 521 | if (!@mysqli_select_db($conn, $db_config['database'])) { |
d7dea16c EM |
522 | $results['severity'] = $this::REQUIREMENT_ERROR; |
523 | $results['details'] = 'Could not select the database'; | |
524 | return $results; | |
525 | } | |
526 | ||
34f3bbd9 SL |
527 | // bytes => kb |
528 | $r = mysqli_query($conn, "SHOW VARIABLES LIKE 'thread_stack'"); | |
d7dea16c EM |
529 | if (!$r) { |
530 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
531 | $results['details'] = 'Could not query thread_stack value'; | |
532 | } | |
533 | else { | |
fcf908c6 | 534 | $values = mysqli_fetch_row($r); |
d7dea16c EM |
535 | if ($values[1] < (1024 * $min_thread_stack)) { |
536 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
537 | $results['details'] = 'MySQL thread_stack is ' . ($values[1] / 1024) . "kb (minimum required is {$min_thread_stack} kb"; | |
538 | } | |
539 | } | |
540 | ||
541 | return $results; | |
542 | } | |
543 | ||
544 | /** | |
545 | * @param $db_config | |
546 | * | |
547 | * @return array | |
548 | */ | |
00be9182 | 549 | public function checkMysqlLockTables($db_config) { |
c64f69d9 | 550 | $results = [ |
d7dea16c EM |
551 | 'title' => 'CiviCRM MySQL Lock Tables', |
552 | 'severity' => $this::REQUIREMENT_OK, | |
553 | 'details' => 'Can successfully lock and unlock tables', | |
c64f69d9 | 554 | ]; |
d7dea16c | 555 | |
9043c4bf | 556 | $conn = $this->connect($db_config); |
d7dea16c EM |
557 | if (!$conn) { |
558 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
46bcf597 | 559 | $results['details'] = 'Could not connect to database'; |
d7dea16c EM |
560 | return $results; |
561 | } | |
562 | ||
fcf908c6 | 563 | if (!@mysqli_select_db($conn, $db_config['database'])) { |
d7dea16c EM |
564 | $results['severity'] = $this::REQUIREMENT_ERROR; |
565 | $results['details'] = 'Could not select the database'; | |
fcf908c6 | 566 | mysqli_close($conn); |
d7dea16c EM |
567 | return $results; |
568 | } | |
569 | ||
fcf908c6 | 570 | $r = mysqli_query($conn, 'CREATE TEMPORARY TABLE civicrm_install_temp_table_test (test text)'); |
d7dea16c EM |
571 | if (!$r) { |
572 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
573 | $results['details'] = 'Could not create a table'; | |
fcf908c6 | 574 | mysqli_close($conn); |
d7dea16c EM |
575 | return $results; |
576 | } | |
577 | ||
fcf908c6 | 578 | $r = mysqli_query($conn, 'LOCK TABLES civicrm_install_temp_table_test WRITE'); |
d7dea16c EM |
579 | if (!$r) { |
580 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
581 | $results['details'] = 'Could not obtain a write lock'; | |
fcf908c6 | 582 | mysqli_close($conn); |
d7dea16c EM |
583 | return $results; |
584 | } | |
585 | ||
fcf908c6 | 586 | $r = mysqli_query($conn, 'UNLOCK TABLES'); |
d7dea16c EM |
587 | if (!$r) { |
588 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
589 | $results['details'] = 'Could not release table lock'; | |
590 | } | |
591 | ||
fcf908c6 | 592 | mysqli_close($conn); |
d7dea16c EM |
593 | return $results; |
594 | } | |
595 | ||
596 | /** | |
597 | * @param $file_paths | |
598 | * | |
599 | * @return array | |
600 | */ | |
601 | public function checkFilepathIsWritable($file_paths) { | |
c64f69d9 | 602 | $results = [ |
d7dea16c EM |
603 | 'title' => 'CiviCRM directories are writable', |
604 | 'severity' => $this::REQUIREMENT_OK, | |
605 | 'details' => 'All required directories are writable: ' . implode(', ', $file_paths), | |
c64f69d9 | 606 | ]; |
d7dea16c | 607 | |
c64f69d9 | 608 | $unwritable_dirs = []; |
d7dea16c EM |
609 | foreach ($file_paths as $path) { |
610 | if (!is_writable($path)) { | |
611 | $unwritable_dirs[] = $path; | |
612 | } | |
613 | } | |
614 | ||
615 | if ($unwritable_dirs) { | |
616 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
617 | $results['details'] = "The following directories need to be made writable by the webserver: " . implode(', ', $unwritable_dirs); | |
618 | } | |
619 | ||
620 | return $results; | |
621 | } | |
96025800 | 622 | |
75615982 | 623 | /** |
624 | * @param $db_config | |
625 | * | |
626 | * @return array | |
627 | */ | |
628 | public function checkMysqlUtf8mb4($db_config) { | |
c64f69d9 | 629 | $results = [ |
75615982 | 630 | 'title' => 'CiviCRM MySQL utf8mb4 Support', |
631 | 'severity' => $this::REQUIREMENT_OK, | |
632 | 'details' => 'Your system supports the MySQL utf8mb4 character set.', | |
c64f69d9 | 633 | ]; |
75615982 | 634 | |
635 | $conn = $this->connect($db_config); | |
636 | if (!$conn) { | |
637 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
638 | $results['details'] = 'Could not connect to database'; | |
639 | return $results; | |
640 | } | |
641 | ||
642 | if (!@mysqli_select_db($conn, $db_config['database'])) { | |
643 | $results['severity'] = $this::REQUIREMENT_ERROR; | |
644 | $results['details'] = 'Could not select the database'; | |
645 | mysqli_close($conn); | |
646 | return $results; | |
647 | } | |
648 | ||
50169eb4 | 649 | mysqli_query($conn, 'DROP TABLE IF EXISTS civicrm_utf8mb4_test'); |
75615982 | 650 | $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'); |
651 | if (!$r) { | |
652 | $results['severity'] = $this::REQUIREMENT_WARNING; | |
653 | $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'; | |
654 | mysqli_close($conn); | |
655 | return $results; | |
656 | } | |
50169eb4 | 657 | mysqli_query($conn, 'DROP TABLE civicrm_utf8mb4_test'); |
75615982 | 658 | |
659 | // Ensure that the MySQL driver supports utf8mb4 encoding. | |
e479ac61 | 660 | $version = mysqli_get_client_info(); |
75615982 | 661 | if (strpos($version, 'mysqlnd') !== FALSE) { |
662 | // The mysqlnd driver supports utf8mb4 starting at version 5.0.9. | |
663 | $version = preg_replace('/^\D+([\d.]+).*/', '$1', $version); | |
664 | if (version_compare($version, '5.0.9', '<')) { | |
665 | $results['severity'] = $this::REQUIREMENT_WARNING; | |
666 | $results['details'] = 'It is recommended, though not yet required, to upgrade your PHP MySQL driver (mysqlnd) to >= 5.0.9 for utf8mb4 support.'; | |
667 | mysqli_close($conn); | |
668 | return $results; | |
669 | } | |
670 | } | |
671 | else { | |
672 | // The libmysqlclient driver supports utf8mb4 starting at version 5.5.3. | |
673 | if (version_compare($version, '5.5.3', '<')) { | |
674 | $results['severity'] = $this::REQUIREMENT_WARNING; | |
675 | $results['details'] = 'It is recommended, though not yet required, to upgrade your PHP MySQL driver (libmysqlclient) to >= 5.5.3 for utf8mb4 support.'; | |
676 | mysqli_close($conn); | |
677 | return $results; | |
678 | } | |
679 | } | |
680 | ||
681 | mysqli_close($conn); | |
682 | return $results; | |
683 | } | |
684 | ||
d7dea16c | 685 | } |