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 |