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.

155 lines
4.6 KiB

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