AbstractDatabase.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 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 = array();
  65. if(empty($config['dbuser']) && empty($config['dbname'])) {
  66. $errors[] = $this->trans->t("%s enter the database username and name.", array($this->dbprettyname));
  67. } else if(empty($config['dbuser'])) {
  68. $errors[] = $this->trans->t("%s enter the database username.", array($this->dbprettyname));
  69. } else if(empty($config['dbname'])) {
  70. $errors[] = $this->trans->t("%s enter the database name.", array($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", array($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 = array(
  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. } else if (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. }