e283c1400c79560d8c408618f3cc3cd90314000f
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / SystemEnvironment / DatabaseCheck.php
1 <?php
2 namespace TYPO3\CMS\Install\SystemEnvironment;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Install\Status;
19
20 /**
21 * Check database configuration status
22 *
23 * This class is a hardcoded requirement check for the database server.
24 *
25 * The status messages and title *must not* include HTML, use plain
26 * text only. The return values of this class are not bound to HTML
27 * and can be used in different scopes (eg. as json array).
28 */
29 class DatabaseCheck
30 {
31 /**
32 * List of MySQL modes that are incompatible with TYPO3 CMS
33 *
34 * @var array
35 */
36 protected $incompatibleSqlModes = array(
37 'NO_BACKSLASH_ESCAPES'
38 );
39
40 /**
41 * Get all status information as array with status objects
42 *
43 * @return array<\TYPO3\CMS\Install\Status\StatusInterface>
44 */
45 public function getStatus()
46 {
47 $statusArray = array();
48 if ($this->isDbalEnabled() || !$this->getDatabaseConnection()) {
49 return $statusArray;
50 }
51 $statusArray[] = $this->checkMysqlVersion();
52 $statusArray[] = $this->checkInvalidSqlModes();
53 return $statusArray;
54 }
55
56 /**
57 * Check if any SQL mode is set which is not compatible with TYPO3
58 *
59 * @return Status\StatusInterface
60 */
61 protected function checkInvalidSqlModes()
62 {
63 $detectedIncompatibleSqlModes = $this->getIncompatibleSqlModes();
64 if (!empty($detectedIncompatibleSqlModes)) {
65 $status = new Status\ErrorStatus();
66 $status->setTitle('Incompatible SQL modes found!');
67 $status->setMessage(
68 'Incompatible SQL modes have been detected:' .
69 ' ' . implode(', ', $detectedIncompatibleSqlModes) . '.' .
70 ' The listed modes are not compatible with TYPO3 CMS.' .
71 ' You have to change that setting in your MySQL environment' .
72 ' or in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'setDBinit\']'
73 );
74 } else {
75 $status = new Status\OkStatus();
76 $status->setTitle('No incompatible SQL modes found.');
77 }
78
79 return $status;
80 }
81
82 /**
83 * Check minimum MySQL version
84 *
85 * @return Status\StatusInterface
86 */
87 protected function checkMysqlVersion()
88 {
89 $minimumMysqlVersion = '5.5.0';
90 $currentMysqlVersion = '';
91 $resource = $this->getDatabaseConnection()->sql_query('SHOW VARIABLES LIKE \'version\';');
92 if ($resource !== false) {
93 $result = $this->getDatabaseConnection()->sql_fetch_row($resource);
94 if (isset($result[1])) {
95 $currentMysqlVersion = $result[1];
96 }
97 }
98 if (version_compare($currentMysqlVersion, $minimumMysqlVersion) < 0) {
99 $status = new Status\ErrorStatus();
100 $status->setTitle('MySQL version too low');
101 $status->setMessage(
102 'Your MySQL version ' . $currentMysqlVersion . ' is too old. TYPO3 CMS does not run' .
103 ' with this version. Update to at least MySQL ' . $minimumMysqlVersion
104 );
105 } else {
106 $status = new Status\OkStatus();
107 $status->setTitle('MySQL version is fine');
108 }
109
110 return $status;
111 }
112
113 /**
114 * Returns an array with the current sql mode settings
115 *
116 * @return array Contains all configured SQL modes that are incompatible
117 */
118 protected function getIncompatibleSqlModes()
119 {
120 $sqlModes = array();
121 $resource = $this->getDatabaseConnection()->sql_query('SELECT @@SESSION.sql_mode;');
122 if ($resource !== false) {
123 $result = $this->getDatabaseConnection()->sql_fetch_row($resource);
124 if (isset($result[0])) {
125 $sqlModes = explode(',', $result[0]);
126 }
127 }
128 return array_intersect($this->incompatibleSqlModes, $sqlModes);
129 }
130
131 /**
132 * Get database instance.
133 * Will be initialized if it does not exist yet.
134 *
135 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
136 */
137 protected function getDatabaseConnection()
138 {
139 static $database;
140 if (!is_object($database)) {
141 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
142 $database = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
143 $database->setDatabaseUsername($GLOBALS['TYPO3_CONF_VARS']['DB']['username']);
144 $database->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
145 $database->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
146 $database->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
147 $database->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
148 $database->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
149 $database->initialize();
150 $database->connectDB();
151 }
152 return $database;
153 }
154
155 /**
156 * Checks if DBAL is enabled for the database connection
157 *
158 * @return bool
159 */
160 protected function isDbalEnabled()
161 {
162 return \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('dbal');
163 }
164 }