PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.0.7
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.0.7
5.13.0 5.12.1 5.12.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 2 years ago Mysqli.php 2 years ago
Mysqli.php
225 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 *
9 */
10 namespace Piwik\Db\Adapter;
11
12 use Exception;
13 use Piwik\Config;
14 use Piwik\Db;
15 use Piwik\Db\AdapterInterface;
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 /**
24 * Constructor
25 *
26 * @param array|Zend_Config $config database configuration
27 */
28 // this is used for indicate TransactionLevel Cache
29 public $supportsUncommitted;
30 public function __construct($config)
31 {
32 // Enable LOAD DATA INFILE
33 $config['driver_options'][MYSQLI_OPT_LOCAL_INFILE] = true;
34 if ($config['enable_ssl']) {
35 if (!empty($config['ssl_key'])) {
36 $config['driver_options']['ssl_key'] = $config['ssl_key'];
37 }
38 if (!empty($config['ssl_cert'])) {
39 $config['driver_options']['ssl_cert'] = $config['ssl_cert'];
40 }
41 if (!empty($config['ssl_ca'])) {
42 $config['driver_options']['ssl_ca'] = $config['ssl_ca'];
43 }
44 if (!empty($config['ssl_ca_path'])) {
45 $config['driver_options']['ssl_ca_path'] = $config['ssl_ca_path'];
46 }
47 if (!empty($config['ssl_cipher'])) {
48 $config['driver_options']['ssl_cipher'] = $config['ssl_cipher'];
49 }
50 if (!empty($config['ssl_no_verify'])) {
51 $config['driver_options']['ssl_no_verify'] = $config['ssl_no_verify'];
52 }
53 }
54 parent::__construct($config);
55 }
56 /**
57 * Reset the configuration variables in this adapter.
58 */
59 public function resetConfig()
60 {
61 $this->_config = array();
62 }
63 /**
64 * Return default port.
65 *
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 $serverVersion = $this->getServerVersion();
92 $requiredVersion = Config::getInstance()->General['minimum_mysql_version'];
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 http://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