[BUGFIX] PHP7 always_populate_raw_post_data is removed.
[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 * List of MySQL modes that are incompatible with TYPO3 CMS
32 *
33 * @var array
34 */
35 protected $incompatibleSqlModes = array(
36 'STRICT_ALL_TABLES',
37 'STRICT_TRANS_TABLES',
38 'NO_BACKSLASH_ESCAPES'
39 );
40
41 /**
42 * Get all status information as array with status objects
43 *
44 * @return array<\TYPO3\CMS\Install\Status\StatusInterface>
45 */
46 public function getStatus() {
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 $detectedIncompatibleSqlModes = $this->getIncompatibleSqlModes();
63 if (!empty($detectedIncompatibleSqlModes)) {
64 $status = new Status\ErrorStatus();
65 $status->setTitle('Incompatible SQL modes found!');
66 $status->setMessage(
67 'Incompatible SQL modes have been detected:' .
68 ' ' . implode(', ', $detectedIncompatibleSqlModes) . '.' .
69 ' The listed modes are not compatible with TYPO3 CMS.' .
70 ' You have to change that setting in your MySQL environment' .
71 ' or in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'setDBinit\']'
72 );
73 } else {
74 $status = new Status\OkStatus();
75 $status->setTitle('No incompatible SQL modes found.');
76 }
77
78 return $status;
79 }
80
81 /**
82 * Check minimum MySQL version
83 *
84 * @return Status\StatusInterface
85 */
86 protected function checkMysqlVersion() {
87 $minimumMysqlVersion = '5.5.0';
88 $currentMysqlVersion = '';
89 $resource = $this->getDatabaseConnection()->sql_query('SHOW VARIABLES LIKE \'version\';');
90 if ($resource !== FALSE) {
91 $result = $this->getDatabaseConnection()->sql_fetch_row($resource);
92 if (isset($result[1])) {
93 $currentMysqlVersion = $result[1];
94 }
95 }
96 if (version_compare($currentMysqlVersion, $minimumMysqlVersion) < 0) {
97 $status = new Status\ErrorStatus();
98 $status->setTitle('MySQL version too low');
99 $status->setMessage(
100 'Your MySQL version ' . $currentMysqlVersion . ' is too old. TYPO3 CMS does not run' .
101 ' with this version. Update to at least MySQL ' . $minimumMysqlVersion
102 );
103 } else {
104 $status = new Status\OkStatus();
105 $status->setTitle('MySQL version is fine');
106 }
107
108 return $status;
109 }
110
111 /**
112 * Returns an array with the current sql mode settings
113 *
114 * @return array Contains all configured SQL modes that are incompatible
115 */
116 protected function getIncompatibleSqlModes() {
117 $sqlModes = array();
118 $resource = $this->getDatabaseConnection()->sql_query('SELECT @@SESSION.sql_mode;');
119 if ($resource !== FALSE) {
120 $result = $this->getDatabaseConnection()->sql_fetch_row($resource);
121 if (isset($result[0])) {
122 $sqlModes = explode(',', $result[0]);
123 }
124 }
125 return array_intersect($this->incompatibleSqlModes, $sqlModes);
126 }
127
128 /**
129 * Get database instance.
130 * Will be initialized if it does not exist yet.
131 *
132 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
133 */
134 protected function getDatabaseConnection() {
135 static $database;
136 if (!is_object($database)) {
137 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
138 $database = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
139 $database->setDatabaseUsername($GLOBALS['TYPO3_CONF_VARS']['DB']['username']);
140 $database->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
141 $database->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
142 $database->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
143 $database->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
144 $database->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
145 $database->initialize();
146 $database->connectDB();
147 }
148 return $database;
149 }
150
151 /**
152 * Checks if DBAL is enabled for the database connection
153 *
154 * @return bool
155 */
156 protected function isDbalEnabled() {
157 return \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('dbal');
158 }
159 }