Adapter
3 years ago
Schema
3 years ago
Adapter.php
4 years ago
AdapterInterface.php
4 years ago
BatchInsert.php
4 years ago
Schema.php
5 years ago
SchemaInterface.php
5 years ago
Settings.php
4 years ago
TransactionLevel.php
4 years ago
Adapter.php
161 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 | ); |
| 114 | |
| 115 | $adapters = array(); |
| 116 | |
| 117 | foreach ($adapterNames as $adapterName) { |
| 118 | $className = '\Piwik\Db\Adapter\\' . $adapterName; |
| 119 | if (call_user_func(array($className, 'isEnabled'))) { |
| 120 | $adapters[strtoupper($adapterName)] = call_user_func(array($className, 'getDefaultPort')); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return $adapters; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Checks if the available adapters are recommended by Piwik or not. |
| 129 | * @param string $adapterName |
| 130 | * @return bool |
| 131 | */ |
| 132 | public static function isRecommendedAdapter($adapterName) |
| 133 | { |
| 134 | return strtolower($adapterName) === 'pdo/mysql'; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Intercepts certain exception messages and replaces leaky ones with ones that don't reveal too much info |
| 139 | * @param string $message |
| 140 | * @return string |
| 141 | */ |
| 142 | public static function overriddenExceptionMessage($message) |
| 143 | { |
| 144 | $safeMessageMap = array( |
| 145 | // add any exception search terms and their replacement message here |
| 146 | '[2006]' => Piwik::translate('General_ExceptionDatabaseUnavailable'), |
| 147 | 'MySQL server has gone away' => Piwik::translate('General_ExceptionDatabaseUnavailable'), |
| 148 | '[1698]' => Piwik::translate('General_ExceptionDatabaseAccess'), |
| 149 | 'Access denied' => Piwik::translate('General_ExceptionDatabaseAccess') |
| 150 | ); |
| 151 | |
| 152 | foreach ($safeMessageMap as $search_term => $safeMessage) { |
| 153 | if (strpos($message, $search_term) !== false) { |
| 154 | return $safeMessage; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return ''; |
| 159 | } |
| 160 | } |
| 161 |