PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.4.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.4.2
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.php
matomo / app / core / Db Last commit date
Adapter 5 years ago Schema 5 years ago Adapter.php 5 years ago AdapterInterface.php 5 years ago BatchInsert.php 5 years ago Schema.php 5 years ago SchemaInterface.php 5 years ago Settings.php 5 years ago TransactionLevel.php 6 years ago
Adapter.php
168 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9 namespace Piwik\Db;
10
11 use Zend_Db_Table;
12 use Piwik\Piwik;
13
14 /**
15 */
16 class Adapter
17 {
18 /**
19 * Create adapter
20 *
21 * @param string $adapterName database adapter name
22 * @param array $dbInfos database connection info
23 * @param bool $connect
24 * @return AdapterInterface
25 */
26 public static function factory($adapterName, & $dbInfos, $connect = true)
27 {
28 if ($connect) {
29 if (isset($dbInfos['port']) && is_string($dbInfos['port']) && $dbInfos['port'][0] === '/') {
30 $dbInfos['unix_socket'] = $dbInfos['port'];
31 unset($dbInfos['host']);
32 unset($dbInfos['port']);
33 }
34
35 // not used by Zend Framework
36 unset($dbInfos['tables_prefix']);
37 unset($dbInfos['adapter']);
38 unset($dbInfos['schema']);
39 }
40
41 $className = self::getAdapterClassName($adapterName);
42
43 // make sure not to pass any references otherwise they will modify $dbInfos
44 $infos = array();
45 foreach ($dbInfos as $key => $val) {
46 $infos[$key] = $val;
47 }
48
49 $adapter = new $className($infos);
50
51 if ($connect) {
52 try {
53 $adapter->getConnection();
54
55 Zend_Db_Table::setDefaultAdapter($adapter);
56 // we don't want the connection information to appear in the logs
57 $adapter->resetConfig();
58 } catch(\Exception $e) {
59 // we don't want certain exceptions to leak information
60 $msg = self::overriddenExceptionMessage($e->getMessage());
61 if ('' !== $msg) {
62 throw new \Exception($msg);
63 }
64
65 throw $e;
66 }
67 }
68
69 return $adapter;
70 }
71
72 /**
73 * Get adapter class name
74 *
75 * @param string $adapterName
76 * @return string
77 * @throws \Exception
78 */
79 private static function getAdapterClassName($adapterName)
80 {
81 $className = 'Piwik\Db\Adapter\\' . str_replace(' ', '\\', ucwords(str_replace(array('_', '\\'), ' ', strtolower($adapterName))));
82 if (!class_exists($className)) {
83 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));
84 }
85 return $className;
86 }
87
88 /**
89 * Get default port for named adapter
90 *
91 * @param string $adapterName
92 * @return int
93 */
94 public static function getDefaultPortForAdapter($adapterName)
95 {
96 $className = self::getAdapterClassName($adapterName);
97 return call_user_func(array($className, 'getDefaultPort'));
98 }
99
100 /**
101 * Get list of adapters
102 *
103 * @return array
104 */
105 public static function getAdapters()
106 {
107 static $adapterNames = array(
108 // currently supported by Piwik
109 'Pdo\Mysql',
110 'Mysqli',
111
112 // other adapters supported by Zend_Db
113 // 'Pdo_Pgsql',
114 // 'Pdo_Mssql',
115 // 'Sqlsrv',
116 // 'Pdo_Ibm',
117 // 'Db2',
118 // 'Pdo_Oci',
119 // 'Oracle',
120 );
121
122 $adapters = array();
123
124 foreach ($adapterNames as $adapterName) {
125 $className = '\Piwik\Db\Adapter\\' . $adapterName;
126 if (call_user_func(array($className, 'isEnabled'))) {
127 $adapters[strtoupper($adapterName)] = call_user_func(array($className, 'getDefaultPort'));
128 }
129 }
130
131 return $adapters;
132 }
133
134 /**
135 * Checks if the available adapters are recommended by Piwik or not.
136 * @param string $adapterName
137 * @return bool
138 */
139 public static function isRecommendedAdapter($adapterName)
140 {
141 return strtolower($adapterName) === 'pdo/mysql';
142 }
143
144 /**
145 * Intercepts certain exception messages and replaces leaky ones with ones that don't reveal too much info
146 * @param string $message
147 * @return string
148 */
149 public static function overriddenExceptionMessage($message)
150 {
151 $safeMessageMap = array(
152 // add any exception search terms and their replacement message here
153 '[2006]' => Piwik::translate('General_ExceptionDatabaseUnavailable'),
154 'MySQL server has gone away' => Piwik::translate('General_ExceptionDatabaseUnavailable'),
155 '[1698]' => Piwik::translate('General_ExceptionDatabaseAccess'),
156 'Access denied' => Piwik::translate('General_ExceptionDatabaseAccess')
157 );
158
159 foreach ($safeMessageMap as $search_term => $safeMessage) {
160 if (strpos($message, $search_term) !== false) {
161 return $safeMessage;
162 }
163 }
164
165 return '';
166 }
167 }
168