PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.2
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.php
matomo / app / core / Db Last commit date
Adapter 6 months ago Schema 3 months ago Adapter.php 6 months ago AdapterInterface.php 3 months ago BatchInsert.php 6 months ago Schema.php 3 months ago SchemaInterface.php 3 months ago Settings.php 1 year ago TransactionLevel.php 1 year ago TransactionalDatabaseDynamicTrait.php 1 year ago TransactionalDatabaseInterface.php 1 year ago TransactionalDatabaseStaticTrait.php 1 year ago
Adapter.php
143 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;
10
11 use Zend_Db_Table;
12 use Piwik\Piwik;
13 /**
14 */
15 class Adapter
16 {
17 /**
18 * Create adapter
19 *
20 * @param string $adapterName database adapter name
21 * @param array $dbInfos database connection info
22 * @param bool $connect
23 * @return AdapterInterface
24 */
25 public static function factory($adapterName,
26 #[\SensitiveParameter]
27 &$dbInfos, $connect = \true)
28 {
29 if ($connect) {
30 if (isset($dbInfos['port']) && is_string($dbInfos['port']) && $dbInfos['port'][0] === '/') {
31 $dbInfos['unix_socket'] = $dbInfos['port'];
32 unset($dbInfos['host']);
33 unset($dbInfos['port']);
34 }
35 // not used by Zend Framework
36 unset($dbInfos['tables_prefix']);
37 unset($dbInfos['adapter']);
38 unset($dbInfos['schema']);
39 }
40 $className = self::getAdapterClassName($adapterName);
41 // make sure not to pass any references otherwise they will modify $dbInfos
42 $infos = array();
43 foreach ($dbInfos as $key => $val) {
44 $infos[$key] = $val;
45 }
46 $adapter = new $className($infos);
47 if ($connect) {
48 try {
49 $adapter->getConnection();
50 Zend_Db_Table::setDefaultAdapter($adapter);
51 // we don't want the connection information to appear in the logs
52 $adapter->resetConfig();
53 } catch (\Exception $e) {
54 // we don't want certain exceptions to leak information
55 $msg = self::overriddenExceptionMessage($e->getMessage());
56 if ('' !== $msg) {
57 throw new \Exception($msg);
58 }
59 throw $e;
60 }
61 }
62 return $adapter;
63 }
64 /**
65 * Get adapter class name
66 *
67 * @param string $adapterName
68 * @return string
69 * @throws \Exception
70 */
71 private static function getAdapterClassName($adapterName)
72 {
73 $className = 'Piwik\\Db\\Adapter\\' . str_replace(' ', '\\', ucwords(str_replace(array('_', '\\'), ' ', strtolower($adapterName))));
74 if (!class_exists($className)) {
75 throw new \Exception(sprintf("Adapter '%s' is not valid. Maybe check that your Matomo configuration files in config/*.ini.php are readable by the webserver.", $adapterName));
76 }
77 return $className;
78 }
79 /**
80 * Get default port for named adapter
81 *
82 * @deprecated use Schema::getDefaultPortForSchema instead
83 * @param string $adapterName
84 * @return int
85 */
86 public static function getDefaultPortForAdapter($adapterName)
87 {
88 $className = self::getAdapterClassName($adapterName);
89 return call_user_func(array($className, 'getDefaultPort'));
90 }
91 /**
92 * Get list of adapters
93 *
94 * @return array
95 */
96 public static function getAdapters()
97 {
98 static $adapterNames = array(
99 // currently supported by Piwik
100 'Pdo\\Mysql',
101 'Mysqli',
102 );
103 $adapters = array();
104 foreach ($adapterNames as $adapterName) {
105 $className = '\\Piwik\\Db\\Adapter\\' . $adapterName;
106 if (call_user_func(array($className, 'isEnabled'))) {
107 $adapters[] = strtoupper($adapterName);
108 }
109 }
110 return $adapters;
111 }
112 /**
113 * Checks if the available adapters are recommended by Piwik or not.
114 * @param string $adapterName
115 * @return bool
116 */
117 public static function isRecommendedAdapter($adapterName)
118 {
119 return strtolower($adapterName) === 'pdo/mysql';
120 }
121 /**
122 * Intercepts certain exception messages and replaces leaky ones with ones that don't reveal too much info
123 * @param string $message
124 * @return string
125 */
126 public static function overriddenExceptionMessage($message)
127 {
128 $safeMessageMap = array(
129 // add any exception search terms and their replacement message here
130 '[2006]' => Piwik::translate('General_ExceptionDatabaseUnavailable'),
131 'MySQL server has gone away' => Piwik::translate('General_ExceptionDatabaseUnavailable'),
132 '[1698]' => Piwik::translate('General_ExceptionDatabaseAccess'),
133 'Access denied' => Piwik::translate('General_ExceptionDatabaseAccess'),
134 );
135 foreach ($safeMessageMap as $search_term => $safeMessage) {
136 if (strpos($message, $search_term) !== \false) {
137 return $safeMessage;
138 }
139 }
140 return '';
141 }
142 }
143