abstractdatabase.php 2.7 KB

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