Mysqli.php
225 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Db\Adapter; |
| 10 | |
| 11 | use Exception; |
| 12 | use Piwik\Config; |
| 13 | use Piwik\Db; |
| 14 | use Piwik\Db\AdapterInterface; |
| 15 | use Piwik\Db\Schema; |
| 16 | use Piwik\Piwik; |
| 17 | use Zend_Config; |
| 18 | use Zend_Db_Adapter_Mysqli; |
| 19 | /** |
| 20 | */ |
| 21 | class Mysqli extends Zend_Db_Adapter_Mysqli implements AdapterInterface |
| 22 | { |
| 23 | use Db\TransactionalDatabaseDynamicTrait; |
| 24 | /** |
| 25 | * Constructor |
| 26 | * |
| 27 | * @param array|Zend_Config $config database configuration |
| 28 | */ |
| 29 | public function __construct($config) |
| 30 | { |
| 31 | // Enable LOAD DATA INFILE |
| 32 | $config['driver_options'][\MYSQLI_OPT_LOCAL_INFILE] = \true; |
| 33 | if ($config['enable_ssl']) { |
| 34 | if (!empty($config['ssl_key'])) { |
| 35 | $config['driver_options']['ssl_key'] = $config['ssl_key']; |
| 36 | } |
| 37 | if (!empty($config['ssl_cert'])) { |
| 38 | $config['driver_options']['ssl_cert'] = $config['ssl_cert']; |
| 39 | } |
| 40 | if (!empty($config['ssl_ca'])) { |
| 41 | $config['driver_options']['ssl_ca'] = $config['ssl_ca']; |
| 42 | } |
| 43 | if (!empty($config['ssl_ca_path'])) { |
| 44 | $config['driver_options']['ssl_ca_path'] = $config['ssl_ca_path']; |
| 45 | } |
| 46 | if (!empty($config['ssl_cipher'])) { |
| 47 | $config['driver_options']['ssl_cipher'] = $config['ssl_cipher']; |
| 48 | } |
| 49 | if (!empty($config['ssl_no_verify'])) { |
| 50 | $config['driver_options']['ssl_no_verify'] = $config['ssl_no_verify']; |
| 51 | } |
| 52 | } |
| 53 | parent::__construct($config); |
| 54 | } |
| 55 | /** |
| 56 | * Reset the configuration variables in this adapter. |
| 57 | */ |
| 58 | public function resetConfig() |
| 59 | { |
| 60 | $this->_config = array(); |
| 61 | } |
| 62 | /** |
| 63 | * Return default port. |
| 64 | * |
| 65 | * @deprecated Use Schema::getDefaultPortForSchema instead |
| 66 | * @return int |
| 67 | */ |
| 68 | public static function getDefaultPort() |
| 69 | { |
| 70 | return 3306; |
| 71 | } |
| 72 | protected function _connect() |
| 73 | { |
| 74 | if ($this->_connection) { |
| 75 | return; |
| 76 | } |
| 77 | // The default error reporting of mysqli changed in PHP 8.1. To circumvent problems in our error handling we set |
| 78 | // the erroring reporting to the default that was used prior PHP 8.1 |
| 79 | // See https://php.watch/versions/8.1/mysqli-error-mode for more details |
| 80 | mysqli_report(\MYSQLI_REPORT_OFF); |
| 81 | parent::_connect(); |
| 82 | $this->_connection->query('SET sql_mode = "' . Db::SQL_MODE . '"'); |
| 83 | } |
| 84 | /** |
| 85 | * Check MySQL version |
| 86 | * |
| 87 | * @throws Exception |
| 88 | */ |
| 89 | public function checkServerVersion() |
| 90 | { |
| 91 | $requiredVersion = Schema::getInstance()->getMinimumSupportedVersion(); |
| 92 | $serverVersion = $this->getServerVersion(); |
| 93 | if (version_compare($serverVersion, $requiredVersion) === -1) { |
| 94 | throw new Exception(Piwik::translate('General_ExceptionDatabaseVersion', array('MySQL', $serverVersion, $requiredVersion))); |
| 95 | } |
| 96 | } |
| 97 | /** |
| 98 | * Returns the MySQL server version |
| 99 | * |
| 100 | * @return null|string |
| 101 | */ |
| 102 | public function getServerVersion() |
| 103 | { |
| 104 | // prioritizing SELECT @@VERSION in case the connection version string is incorrect (which can |
| 105 | // occur on Azure) |
| 106 | $versionInfo = $this->fetchAll('SELECT @@VERSION'); |
| 107 | if (count($versionInfo)) { |
| 108 | return $versionInfo[0]['@@VERSION']; |
| 109 | } |
| 110 | return parent::getServerVersion(); |
| 111 | } |
| 112 | /** |
| 113 | * Check client version compatibility against database server |
| 114 | * |
| 115 | * @throws Exception |
| 116 | */ |
| 117 | public function checkClientVersion() |
| 118 | { |
| 119 | $serverVersion = $this->getServerVersion(); |
| 120 | $clientVersion = $this->getClientVersion(); |
| 121 | // incompatible change to DECIMAL implementation in 5.0.3 |
| 122 | if (version_compare($serverVersion, '5.0.3') >= 0 && version_compare($clientVersion, '5.0.3') < 0) { |
| 123 | throw new Exception(Piwik::translate('General_ExceptionIncompatibleClientServerVersions', array('MySQL', $clientVersion, $serverVersion))); |
| 124 | } |
| 125 | } |
| 126 | /** |
| 127 | * Return number of affected rows in last query |
| 128 | * |
| 129 | * @param mixed $queryResult Result from query() |
| 130 | * @return int |
| 131 | */ |
| 132 | public function rowCount($queryResult) |
| 133 | { |
| 134 | return mysqli_affected_rows($this->_connection); |
| 135 | } |
| 136 | /** |
| 137 | * Returns true if this adapter's required extensions are enabled |
| 138 | * |
| 139 | * @return bool |
| 140 | */ |
| 141 | public static function isEnabled() |
| 142 | { |
| 143 | $extensions = @get_loaded_extensions(); |
| 144 | return in_array('mysqli', $extensions); |
| 145 | } |
| 146 | /** |
| 147 | * Returns true if this adapter supports blobs as fields |
| 148 | * |
| 149 | * @return bool |
| 150 | */ |
| 151 | public function hasBlobDataType() |
| 152 | { |
| 153 | return \true; |
| 154 | } |
| 155 | /** |
| 156 | * Returns true if this adapter supports bulk loading |
| 157 | * |
| 158 | * @return bool |
| 159 | */ |
| 160 | public function hasBulkLoader() |
| 161 | { |
| 162 | return \true; |
| 163 | } |
| 164 | /** |
| 165 | * Test error number |
| 166 | * |
| 167 | * @param Exception $e |
| 168 | * @param string $errno |
| 169 | * @return bool |
| 170 | */ |
| 171 | public function isErrNo($e, $errno) |
| 172 | { |
| 173 | return self::isMysqliErrorNumber($e, $this->_connection, $errno); |
| 174 | } |
| 175 | /** |
| 176 | * Test error number |
| 177 | * |
| 178 | * @param Exception $e |
| 179 | * @param string $errno |
| 180 | * @return bool |
| 181 | */ |
| 182 | public static function isMysqliErrorNumber($e, $connection, $errno) |
| 183 | { |
| 184 | if (is_null($connection)) { |
| 185 | if (preg_match('/(?:\\[|\\s)([0-9]{4})(?:\\]|\\s)/', $e->getMessage(), $match)) { |
| 186 | return $match[1] == $errno; |
| 187 | } |
| 188 | return mysqli_connect_errno() == $errno; |
| 189 | } |
| 190 | return mysqli_errno($connection) == $errno; |
| 191 | } |
| 192 | /** |
| 193 | * Execute unprepared SQL query and throw away the result |
| 194 | * |
| 195 | * Workaround some SQL statements not compatible with prepare(). |
| 196 | * See https://framework.zend.com/issues/browse/ZF-1398 |
| 197 | * |
| 198 | * @param string $sqlQuery |
| 199 | * @return int Number of rows affected (SELECT/INSERT/UPDATE/DELETE) |
| 200 | */ |
| 201 | public function exec($sqlQuery) |
| 202 | { |
| 203 | $rc = mysqli_query($this->_connection, $sqlQuery); |
| 204 | $rowsAffected = mysqli_affected_rows($this->_connection); |
| 205 | if (!is_bool($rc)) { |
| 206 | mysqli_free_result($rc); |
| 207 | } |
| 208 | return $rowsAffected; |
| 209 | } |
| 210 | /** |
| 211 | * Get client version |
| 212 | * |
| 213 | * @return string |
| 214 | */ |
| 215 | public function getClientVersion() |
| 216 | { |
| 217 | $this->_connect(); |
| 218 | $version = $this->_connection->server_version; |
| 219 | $major = (int) ($version / 10000); |
| 220 | $minor = (int) ($version % 10000 / 100); |
| 221 | $revision = (int) ($version % 100); |
| 222 | return $major . '.' . $minor . '.' . $revision; |
| 223 | } |
| 224 | } |
| 225 |