abstractdatabase.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Setup;
  25. use OCP\IConfig;
  26. use OCP\ILogger;
  27. use OCP\Security\ISecureRandom;
  28. abstract class AbstractDatabase {
  29. /** @var \OC_L10N */
  30. protected $trans;
  31. /** @var string */
  32. protected $dbDefinitionFile;
  33. /** @var string */
  34. protected $dbUser;
  35. /** @var string */
  36. protected $dbPassword;
  37. /** @var string */
  38. protected $dbName;
  39. /** @var string */
  40. protected $dbHost;
  41. /** @var string */
  42. protected $tablePrefix;
  43. /** @var IConfig */
  44. protected $config;
  45. /** @var ILogger */
  46. protected $logger;
  47. /** @var ISecureRandom */
  48. protected $random;
  49. public function __construct($trans, $dbDefinitionFile, IConfig $config, ILogger $logger, ISecureRandom $random) {
  50. $this->trans = $trans;
  51. $this->dbDefinitionFile = $dbDefinitionFile;
  52. $this->config = $config;
  53. $this->logger = $logger;
  54. $this->random = $random;
  55. }
  56. public function validate($config) {
  57. $errors = array();
  58. if(empty($config['dbuser'])) {
  59. $errors[] = $this->trans->t("%s enter the database username.", array($this->dbprettyname));
  60. }
  61. if(empty($config['dbname'])) {
  62. $errors[] = $this->trans->t("%s enter the database name.", array($this->dbprettyname));
  63. }
  64. if(substr_count($config['dbname'], '.') >= 1) {
  65. $errors[] = $this->trans->t("%s you may not use dots in the database name", array($this->dbprettyname));
  66. }
  67. return $errors;
  68. }
  69. public function initialize($config) {
  70. $dbUser = $config['dbuser'];
  71. $dbPass = $config['dbpass'];
  72. $dbName = $config['dbname'];
  73. $dbHost = !empty($config['dbhost']) ? $config['dbhost'] : 'localhost';
  74. $dbTablePrefix = isset($config['dbtableprefix']) ? $config['dbtableprefix'] : 'oc_';
  75. $this->config->setSystemValues([
  76. 'dbname' => $dbName,
  77. 'dbhost' => $dbHost,
  78. 'dbtableprefix' => $dbTablePrefix,
  79. ]);
  80. $this->dbUser = $dbUser;
  81. $this->dbPassword = $dbPass;
  82. $this->dbName = $dbName;
  83. $this->dbHost = $dbHost;
  84. $this->tablePrefix = $dbTablePrefix;
  85. }
  86. /**
  87. * @param string $userName
  88. */
  89. abstract public function setupDatabase($userName);
  90. }