PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.2.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.2.0
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Db / Adapter / Mysqli.php
matomo / app / core / Db / Adapter Last commit date
Pdo 1 year ago Mysqli.php 1 year ago
Mysqli.php
224 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\Piwik;
16 use Zend_Config;
17 use Zend_Db_Adapter_Mysqli;
18 /**
19 */
20 class Mysqli extends Zend_Db_Adapter_Mysqli implements AdapterInterface
21 {
22 use Db\TransactionalDatabaseDynamicTrait;
23 /**
24 * Constructor
25 *
26 * @param array|Zend_Config $config database configuration
27 */
28 public function __construct($config)
29 {
30 // Enable LOAD DATA INFILE
31 $config['driver_options'][\MYSQLI_OPT_LOCAL_INFILE] = \true;
32 if ($config['enable_ssl']) {
33 if (!empty($config['ssl_key'])) {
34 $config['driver_options']['ssl_key'] = $config['ssl_key'];
35 }
36 if (!empty($config['ssl_cert'])) {
37 $config['driver_options']['ssl_cert'] = $config['ssl_cert'];
38 }
39 if (!empty($config['ssl_ca'])) {
40 $config['driver_options']['ssl_ca'] = $config['ssl_ca'];
41 }
42 if (!empty($config['ssl_ca_path'])) {
43 $config['driver_options']['ssl_ca_path'] = $config['ssl_ca_path'];
44 }
45 if (!empty($config['ssl_cipher'])) {
46 $config['driver_options']['ssl_cipher'] = $config['ssl_cipher'];
47 }
48 if (!empty($config['ssl_no_verify'])) {
49 $config['driver_options']['ssl_no_verify'] = $config['ssl_no_verify'];
50 }
51 }
52 parent::__construct($config);
53 }
54 /**
55 * Reset the configuration variables in this adapter.
56 */
57 public function resetConfig()
58 {
59 $this->_config = array();
60 }
61 /**
62 * Return default port.
63 *
64 * @deprecated Use Schema::getDefaultPortForSchema instead
65 * @return int
66 */
67 public static function getDefaultPort()
68 {
69 return 3306;
70 }
71 protected function _connect()
72 {
73 if ($this->_connection) {
74 return;
75 }
76 // The default error reporting of mysqli changed in PHP 8.1. To circumvent problems in our error handling we set
77 // the erroring reporting to the default that was used prior PHP 8.1
78 // See https://php.watch/versions/8.1/mysqli-error-mode for more details
79 mysqli_report(\MYSQLI_REPORT_OFF);
80 parent::_connect();
81 $this->_connection->query('SET sql_mode = "' . Db::SQL_MODE . '"');
82 }
83 /**
84 * Check MySQL version
85 *
86 * @throws Exception
87 */
88 public function checkServerVersion()
89 {
90 $serverVersion = $this->getServerVersion();
91 $requiredVersion = Config::getInstance()->General['minimum_mysql_version'];
92 if (version_compare($serverVersion, $requiredVersion) === -1) {
93 throw new Exception(Piwik::translate('General_ExceptionDatabaseVersion', array('MySQL', $serverVersion, $requiredVersion)));
94 }
95 }
96 /**
97 * Returns the MySQL server version
98 *
99 * @return null|string
100 */
101 public function getServerVersion()
102 {
103 // prioritizing SELECT @@VERSION in case the connection version string is incorrect (which can
104 // occur on Azure)
105 $versionInfo = $this->fetchAll('SELECT @@VERSION');
106 if (count($versionInfo)) {
107 return $versionInfo[0]['@@VERSION'];
108 }
109 return parent::getServerVersion();
110 }
111 /**
112 * Check client version compatibility against database server
113 *
114 * @throws Exception
115 */
116 public function checkClientVersion()
117 {
118 $serverVersion = $this->getServerVersion();
119 $clientVersion = $this->getClientVersion();
120 // incompatible change to DECIMAL implementation in 5.0.3
121 if (version_compare($serverVersion, '5.0.3') >= 0 && version_compare($clientVersion, '5.0.3') < 0) {
122 throw new Exception(Piwik::translate('General_ExceptionIncompatibleClientServerVersions', array('MySQL', $clientVersion, $serverVersion)));
123 }
124 }
125 /**
126 * Return number of affected rows in last query
127 *
128 * @param mixed $queryResult Result from query()
129 * @return int
130 */
131 public function rowCount($queryResult)
132 {
133 return mysqli_affected_rows($this->_connection);
134 }
135 /**
136 * Returns true if this adapter's required extensions are enabled
137 *
138 * @return bool
139 */
140 public static function isEnabled()
141 {
142 $extensions = @get_loaded_extensions();
143 return in_array('mysqli', $extensions);
144 }
145 /**
146 * Returns true if this adapter supports blobs as fields
147 *
148 * @return bool
149 */
150 public function hasBlobDataType()
151 {
152 return \true;
153 }
154 /**
155 * Returns true if this adapter supports bulk loading
156 *
157 * @return bool
158 */
159 public function hasBulkLoader()
160 {
161 return \true;
162 }
163 /**
164 * Test error number
165 *
166 * @param Exception $e
167 * @param string $errno
168 * @return bool
169 */
170 public function isErrNo($e, $errno)
171 {
172 return self::isMysqliErrorNumber($e, $this->_connection, $errno);
173 }
174 /**
175 * Test error number
176 *
177 * @param Exception $e
178 * @param string $errno
179 * @return bool
180 */
181 public static function isMysqliErrorNumber($e, $connection, $errno)
182 {
183 if (is_null($connection)) {
184 if (preg_match('/(?:\\[|\\s)([0-9]{4})(?:\\]|\\s)/', $e->getMessage(), $match)) {
185 return $match[1] == $errno;
186 }
187 return mysqli_connect_errno() == $errno;
188 }
189 return mysqli_errno($connection) == $errno;
190 }
191 /**
192 * Execute unprepared SQL query and throw away the result
193 *
194 * Workaround some SQL statements not compatible with prepare().
195 * See http://framework.zend.com/issues/browse/ZF-1398
196 *
197 * @param string $sqlQuery
198 * @return int Number of rows affected (SELECT/INSERT/UPDATE/DELETE)
199 */
200 public function exec($sqlQuery)
201 {
202 $rc = mysqli_query($this->_connection, $sqlQuery);
203 $rowsAffected = mysqli_affected_rows($this->_connection);
204 if (!is_bool($rc)) {
205 mysqli_free_result($rc);
206 }
207 return $rowsAffected;
208 }
209 /**
210 * Get client version
211 *
212 * @return string
213 */
214 public function getClientVersion()
215 {
216 $this->_connect();
217 $version = $this->_connection->server_version;
218 $major = (int) ($version / 10000);
219 $minor = (int) ($version % 10000 / 100);
220 $revision = (int) ($version % 100);
221 return $major . '.' . $minor . '.' . $revision;
222 }
223 }
224