AbstractDatabase.php 5.0 KB

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