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