PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / trunk
Matomo Analytics – Powerful, Privacy-First Insights for WordPress vtrunk
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 1 month ago Schema 2 weeks ago Adapter.php 1 month ago AdapterInterface.php 1 month ago BatchInsert.php 6 months ago Schema.php 1 month ago SchemaInterface.php 1 month 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
141 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 class Adapter
14 {
15 /**
16 * Create adapter
17 *
18 * @param string $adapterName database adapter name
19 * @param array $dbInfos database connection info
20 * @param bool $connect
21 * @return AdapterInterface
22 */
23 public static function factory($adapterName,
24 #[\SensitiveParameter]
25 &$dbInfos, $connect = \true)
26 {
27 if ($connect) {
28 if (isset($dbInfos['port']) && is_string($dbInfos['port']) && $dbInfos['port'][0] === '/') {
29 $dbInfos['unix_socket'] = $dbInfos['port'];
30 unset($dbInfos['host']);
31 unset($dbInfos['port']);
32 }
33 // not used by Zend Framework
34 unset($dbInfos['tables_prefix']);
35 unset($dbInfos['adapter']);
36 unset($dbInfos['schema']);
37 }
38 $className = self::getAdapterClassName($adapterName);
39 // make sure not to pass any references otherwise they will modify $dbInfos
40 $infos = array();
41 foreach ($dbInfos as $key => $val) {
42 $infos[$key] = $val;
43 }
44 $adapter = new $className($infos);
45 if ($connect) {
46 try {
47 $adapter->getConnection();
48 Zend_Db_Table::setDefaultAdapter($adapter);
49 // we don't want the connection information to appear in the logs
50 $adapter->resetConfig();
51 } catch (\Exception $e) {
52 // we don't want certain exceptions to leak information
53 $msg = self::overriddenExceptionMessage($e->getMessage());
54 if ('' !== $msg) {
55 throw new \Exception($msg);
56 }
57 throw $e;
58 }
59 }
60 return $adapter;
61 }
62 /**
63 * Get adapter class name
64 *
65 * @param string $adapterName
66 * @return string
67 * @throws \Exception
68 */
69 private static function getAdapterClassName($adapterName)
70 {
71 $className = 'Piwik\\Db\\Adapter\\' . str_replace(' ', '\\', ucwords(str_replace(array('_', '\\'), ' ', strtolower($adapterName))));
72 if (!class_exists($className)) {
73 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));
74 }
75 return $className;
76 }
77 /**
78 * Get default port for named adapter
79 *
80 * @deprecated use Schema::getDefaultPortForSchema instead
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);
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