1
0

AbstractDatabase.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\Connection;
  31. use OC\DB\ConnectionFactory;
  32. use OC\DB\MigrationService;
  33. use OC\SystemConfig;
  34. use OCP\IL10N;
  35. use OCP\Migration\IOutput;
  36. use OCP\Security\ISecureRandom;
  37. use Psr\Log\LoggerInterface;
  38. abstract class AbstractDatabase {
  39. /** @var IL10N */
  40. protected $trans;
  41. /** @var string */
  42. protected $dbUser;
  43. /** @var string */
  44. protected $dbPassword;
  45. /** @var string */
  46. protected $dbName;
  47. /** @var string */
  48. protected $dbHost;
  49. /** @var string */
  50. protected $dbPort;
  51. /** @var string */
  52. protected $tablePrefix;
  53. /** @var SystemConfig */
  54. protected $config;
  55. /** @var LoggerInterface */
  56. protected $logger;
  57. /** @var ISecureRandom */
  58. protected $random;
  59. /** @var bool */
  60. protected $tryCreateDbUser;
  61. public function __construct(IL10N $trans, SystemConfig $config, LoggerInterface $logger, ISecureRandom $random) {
  62. $this->trans = $trans;
  63. $this->config = $config;
  64. $this->logger = $logger;
  65. $this->random = $random;
  66. }
  67. public function validate($config) {
  68. $errors = [];
  69. if (empty($config['dbuser']) && empty($config['dbname'])) {
  70. $errors[] = $this->trans->t("Enter the database Login and name for %s", [$this->dbprettyname]);
  71. } elseif (empty($config['dbuser'])) {
  72. $errors[] = $this->trans->t("Enter the database Login for %s", [$this->dbprettyname]);
  73. } elseif (empty($config['dbname'])) {
  74. $errors[] = $this->trans->t("Enter the database name for %s", [$this->dbprettyname]);
  75. }
  76. if (substr_count($config['dbname'], '.') >= 1) {
  77. $errors[] = $this->trans->t("You cannot use dots in the database name %s", [$this->dbprettyname]);
  78. }
  79. return $errors;
  80. }
  81. public function initialize($config) {
  82. $dbUser = $config['dbuser'];
  83. $dbPass = $config['dbpass'];
  84. $dbName = $config['dbname'];
  85. $dbHost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost';
  86. $dbPort = !empty($config['dbport']) ? $config['dbport'] : '';
  87. $dbTablePrefix = $config['dbtableprefix'] ?? 'oc_';
  88. $createUserConfig = $this->config->getValue("setup_create_db_user", true);
  89. // accept `false` both as bool and string, since setting config values from env will result in a string
  90. $this->tryCreateDbUser = $createUserConfig !== false && $createUserConfig !== "false";
  91. $this->config->setValues([
  92. 'dbname' => $dbName,
  93. 'dbhost' => $dbHost,
  94. 'dbport' => $dbPort,
  95. 'dbtableprefix' => $dbTablePrefix,
  96. ]);
  97. $this->dbUser = $dbUser;
  98. $this->dbPassword = $dbPass;
  99. $this->dbName = $dbName;
  100. $this->dbHost = $dbHost;
  101. $this->dbPort = $dbPort;
  102. $this->tablePrefix = $dbTablePrefix;
  103. }
  104. /**
  105. * @param array $configOverwrite
  106. * @return \OC\DB\Connection
  107. */
  108. protected function connect(array $configOverwrite = []): Connection {
  109. $connectionParams = [
  110. 'host' => $this->dbHost,
  111. 'user' => $this->dbUser,
  112. 'password' => $this->dbPassword,
  113. 'tablePrefix' => $this->tablePrefix,
  114. 'dbname' => $this->dbName
  115. ];
  116. // adding port support through installer
  117. if (!empty($this->dbPort)) {
  118. if (ctype_digit($this->dbPort)) {
  119. $connectionParams['port'] = $this->dbPort;
  120. } else {
  121. $connectionParams['unix_socket'] = $this->dbPort;
  122. }
  123. } elseif (strpos($this->dbHost, ':')) {
  124. // Host variable may carry a port or socket.
  125. [$host, $portOrSocket] = explode(':', $this->dbHost, 2);
  126. if (ctype_digit($portOrSocket)) {
  127. $connectionParams['port'] = $portOrSocket;
  128. } else {
  129. $connectionParams['unix_socket'] = $portOrSocket;
  130. }
  131. $connectionParams['host'] = $host;
  132. }
  133. $connectionParams = array_merge($connectionParams, $configOverwrite);
  134. $connectionParams = array_merge($connectionParams, ['primary' => $connectionParams, 'replica' => [$connectionParams]]);
  135. $cf = new ConnectionFactory($this->config);
  136. $connection = $cf->getConnection($this->config->getValue('dbtype', 'sqlite'), $connectionParams);
  137. $connection->ensureConnectedToPrimary();
  138. return $connection;
  139. }
  140. /**
  141. * @param string $username
  142. */
  143. abstract public function setupDatabase($username);
  144. public function runMigrations(?IOutput $output = null) {
  145. if (!is_dir(\OC::$SERVERROOT."/core/Migrations")) {
  146. return;
  147. }
  148. $ms = new MigrationService('core', \OC::$server->get(Connection::class), $output);
  149. $ms->migrate('latest', true);
  150. }
  151. }