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