You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
13 years ago
13 years ago
10 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Manish Bisht <manish.bisht490@gmail.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Thomas Pulzer <t.pulzer@kniel.de>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Setup;
  28. use OC\AllConfig;
  29. use OC\DB\ConnectionFactory;
  30. use OC\SystemConfig;
  31. use OCP\IL10N;
  32. use OCP\ILogger;
  33. use OCP\Security\ISecureRandom;
  34. abstract class AbstractDatabase {
  35. /** @var IL10N */
  36. protected $trans;
  37. /** @var string */
  38. protected $dbDefinitionFile;
  39. /** @var string */
  40. protected $dbUser;
  41. /** @var string */
  42. protected $dbPassword;
  43. /** @var string */
  44. protected $dbName;
  45. /** @var string */
  46. protected $dbHost;
  47. /** @var string */
  48. protected $dbPort;
  49. /** @var string */
  50. protected $tablePrefix;
  51. /** @var SystemConfig */
  52. protected $config;
  53. /** @var ILogger */
  54. protected $logger;
  55. /** @var ISecureRandom */
  56. protected $random;
  57. public function __construct(IL10N $trans, $dbDefinitionFile, SystemConfig $config, ILogger $logger, ISecureRandom $random) {
  58. $this->trans = $trans;
  59. $this->dbDefinitionFile = $dbDefinitionFile;
  60. $this->config = $config;
  61. $this->logger = $logger;
  62. $this->random = $random;
  63. }
  64. public function validate($config) {
  65. $errors = array();
  66. if(empty($config['dbuser']) && empty($config['dbname'])) {
  67. $errors[] = $this->trans->t("%s enter the database username and name.", array($this->dbprettyname));
  68. } else if(empty($config['dbuser'])) {
  69. $errors[] = $this->trans->t("%s enter the database username.", array($this->dbprettyname));
  70. } else if(empty($config['dbname'])) {
  71. $errors[] = $this->trans->t("%s enter the database name.", array($this->dbprettyname));
  72. }
  73. if(substr_count($config['dbname'], '.') >= 1) {
  74. $errors[] = $this->trans->t("%s you may not use dots in the database name", array($this->dbprettyname));
  75. }
  76. return $errors;
  77. }
  78. public function initialize($config) {
  79. $dbUser = $config['dbuser'];
  80. $dbPass = $config['dbpass'];
  81. $dbName = $config['dbname'];
  82. $dbHost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost';
  83. $dbPort = !empty($config['dbport']) ? $config['dbport'] : '';
  84. $dbTablePrefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_';
  85. $this->config->setValues([
  86. 'dbname' => $dbName,
  87. 'dbhost' => $dbHost,
  88. 'dbport' => $dbPort,
  89. 'dbtableprefix' => $dbTablePrefix,
  90. ]);
  91. $this->dbUser = $dbUser;
  92. $this->dbPassword = $dbPass;
  93. $this->dbName = $dbName;
  94. $this->dbHost = $dbHost;
  95. $this->dbPort = $dbPort;
  96. $this->tablePrefix = $dbTablePrefix;
  97. }
  98. /**
  99. * @param array $configOverwrite
  100. * @return \OC\DB\Connection
  101. */
  102. protected function connect(array $configOverwrite = []) {
  103. $connectionParams = array(
  104. 'host' => $this->dbHost,
  105. 'user' => $this->dbUser,
  106. 'password' => $this->dbPassword,
  107. 'tablePrefix' => $this->tablePrefix,
  108. 'dbname' => $this->dbName
  109. );
  110. // adding port support through installer
  111. if (!empty($this->dbPort)) {
  112. if (ctype_digit($this->dbPort)) {
  113. $connectionParams['port'] = $this->dbPort;
  114. } else {
  115. $connectionParams['unix_socket'] = $this->dbPort;
  116. }
  117. } else if (strpos($this->dbHost, ':')) {
  118. // Host variable may carry a port or socket.
  119. list($host, $portOrSocket) = explode(':', $this->dbHost, 2);
  120. if (ctype_digit($portOrSocket)) {
  121. $connectionParams['port'] = $portOrSocket;
  122. } else {
  123. $connectionParams['unix_socket'] = $portOrSocket;
  124. }
  125. $connectionParams['host'] = $host;
  126. }
  127. $connectionParams = array_merge($connectionParams, $configOverwrite);
  128. $cf = new ConnectionFactory($this->config);
  129. return $cf->getConnection($this->config->getValue('dbtype', 'sqlite'), $connectionParams);
  130. }
  131. /**
  132. * @param string $userName
  133. */
  134. abstract public function setupDatabase($userName);
  135. }