TYPO3  7.6
DatabaseCheck.php
Go to the documentation of this file.
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 
18 use TYPO3\CMS\Install\Status;
19 
30 {
36  protected $incompatibleSqlModes = array(
37  'STRICT_ALL_TABLES',
38  'STRICT_TRANS_TABLES',
39  'NO_BACKSLASH_ESCAPES'
40  );
41 
47  public function getStatus()
48  {
49  $statusArray = array();
50  if ($this->isDbalEnabled() || !$this->getDatabaseConnection()) {
51  return $statusArray;
52  }
53  $statusArray[] = $this->checkMysqlVersion();
54  $statusArray[] = $this->checkInvalidSqlModes();
55  return $statusArray;
56  }
57 
63  protected function checkInvalidSqlModes()
64  {
65  $detectedIncompatibleSqlModes = $this->getIncompatibleSqlModes();
66  if (!empty($detectedIncompatibleSqlModes)) {
67  $status = new Status\ErrorStatus();
68  $status->setTitle('Incompatible SQL modes found!');
69  $status->setMessage(
70  'Incompatible SQL modes have been detected:' .
71  ' ' . implode(', ', $detectedIncompatibleSqlModes) . '.' .
72  ' The listed modes are not compatible with TYPO3 CMS.' .
73  ' You have to change that setting in your MySQL environment' .
74  ' or in $GLOBALS[\'TYPO3_CONF_VARS\'][\'SYS\'][\'setDBinit\']'
75  );
76  } else {
77  $status = new Status\OkStatus();
78  $status->setTitle('No incompatible SQL modes found.');
79  }
80 
81  return $status;
82  }
83 
89  protected function checkMysqlVersion()
90  {
91  $minimumMysqlVersion = '5.5.0';
92  $currentMysqlVersion = '';
93  $resource = $this->getDatabaseConnection()->sql_query('SHOW VARIABLES LIKE \'version\';');
94  if ($resource !== false) {
95  $result = $this->getDatabaseConnection()->sql_fetch_row($resource);
96  if (isset($result[1])) {
97  $currentMysqlVersion = $result[1];
98  }
99  }
100  if (version_compare($currentMysqlVersion, $minimumMysqlVersion) < 0) {
101  $status = new Status\ErrorStatus();
102  $status->setTitle('MySQL version too low');
103  $status->setMessage(
104  'Your MySQL version ' . $currentMysqlVersion . ' is too old. TYPO3 CMS does not run' .
105  ' with this version. Update to at least MySQL ' . $minimumMysqlVersion
106  );
107  } else {
108  $status = new Status\OkStatus();
109  $status->setTitle('MySQL version is fine');
110  }
111 
112  return $status;
113  }
114 
120  protected function getIncompatibleSqlModes()
121  {
122  $sqlModes = array();
123  $resource = $this->getDatabaseConnection()->sql_query('SELECT @@SESSION.sql_mode;');
124  if ($resource !== false) {
125  $result = $this->getDatabaseConnection()->sql_fetch_row($resource);
126  if (isset($result[0])) {
127  $sqlModes = explode(',', $result[0]);
128  }
129  }
130  return array_intersect($this->incompatibleSqlModes, $sqlModes);
131  }
132 
139  protected function getDatabaseConnection()
140  {
141  static $database;
142  if (!is_object($database)) {
144  $database = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
145  $database->setDatabaseUsername($GLOBALS['TYPO3_CONF_VARS']['DB']['username']);
146  $database->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
147  $database->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
148  $database->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
149  $database->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
150  $database->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
151  $database->initialize();
152  $database->connectDB();
153  }
154  return $database;
155  }
156 
162  protected function isDbalEnabled()
163  {
164  return \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('dbal');
165  }
166 }