AbstractDatabase.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. public function __construct(IL10N $trans, SystemConfig $config, LoggerInterface $logger, ISecureRandom $random) {
  59. $this->trans = $trans;
  60. $this->config = $config;
  61. $this->logger = $logger;
  62. $this->random = $random;
  63. }
  64. public function validate($config) {
  65. $errors = [];
  66. if (empty($config['dbuser']) && empty($config['dbname'])) {
  67. $errors[] = $this->trans->t("Enter the database username and name for %s", [$this->dbprettyname]);
  68. } elseif (empty($config['dbuser'])) {
  69. $errors[] = $this->trans->t("Enter the database username for %s", [$this->dbprettyname]);
  70. } elseif (empty($config['dbname'])) {
  71. $errors[] = $this->trans->t("Enter the database name for %s", [$this->dbprettyname]);
  72. }
  73. if (substr_count($config['dbname'], '.') >= 1) {
  74. $errors[] = $this->trans->t("You cannot use dots in the database name %s", [$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 = []): Connection {
  103. $connectionParams = [
  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. } elseif (strpos($this->dbHost, ':')) {
  118. // Host variable may carry a port or socket.
  119. [$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. public function runMigrations() {
  136. if (!is_dir(\OC::$SERVERROOT."/core/Migrations")) {
  137. return;
  138. }
  139. $ms = new MigrationService('core', \OC::$server->get(Connection::class));
  140. $ms->migrate('latest', true);
  141. }
  142. }