OCI.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Setup;
  31. class OCI extends AbstractDatabase {
  32. public $dbprettyname = 'Oracle';
  33. protected $dbtablespace;
  34. public function initialize($config) {
  35. parent::initialize($config);
  36. if (array_key_exists('dbtablespace', $config)) {
  37. $this->dbtablespace = $config['dbtablespace'];
  38. } else {
  39. $this->dbtablespace = 'USERS';
  40. }
  41. // allow empty hostname for oracle
  42. $this->dbHost = $config['dbhost'];
  43. $this->config->setValues([
  44. 'dbhost' => $this->dbHost,
  45. 'dbtablespace' => $this->dbtablespace,
  46. ]);
  47. }
  48. public function validate($config) {
  49. $errors = [];
  50. if (empty($config['dbuser']) && empty($config['dbname'])) {
  51. $errors[] = $this->trans->t("Enter the database username and name for %s", [$this->dbprettyname]);
  52. } elseif (empty($config['dbuser'])) {
  53. $errors[] = $this->trans->t("Enter the database username for %s", [$this->dbprettyname]);
  54. } elseif (empty($config['dbname'])) {
  55. $errors[] = $this->trans->t("Enter the database name for %s", [$this->dbprettyname]);
  56. }
  57. return $errors;
  58. }
  59. public function setupDatabase($username) {
  60. try {
  61. $this->connect();
  62. } catch (\Exception $e) {
  63. $errorMessage = $this->getLastError();
  64. if ($errorMessage) {
  65. throw new \OC\DatabaseSetupException($this->trans->t('Oracle connection could not be established'),
  66. $errorMessage . ' Check environment: ORACLE_HOME=' . getenv('ORACLE_HOME')
  67. . ' ORACLE_SID=' . getenv('ORACLE_SID')
  68. . ' LD_LIBRARY_PATH=' . getenv('LD_LIBRARY_PATH')
  69. . ' NLS_LANG=' . getenv('NLS_LANG')
  70. . ' tnsnames.ora is ' . (is_readable(getenv('ORACLE_HOME') . '/network/admin/tnsnames.ora') ? '' : 'not ') . 'readable', 0, $e);
  71. }
  72. throw new \OC\DatabaseSetupException($this->trans->t('Oracle username and/or password not valid'),
  73. 'Check environment: ORACLE_HOME=' . getenv('ORACLE_HOME')
  74. . ' ORACLE_SID=' . getenv('ORACLE_SID')
  75. . ' LD_LIBRARY_PATH=' . getenv('LD_LIBRARY_PATH')
  76. . ' NLS_LANG=' . getenv('NLS_LANG')
  77. . ' tnsnames.ora is ' . (is_readable(getenv('ORACLE_HOME') . '/network/admin/tnsnames.ora') ? '' : 'not ') . 'readable', 0, $e);
  78. }
  79. $this->config->setValues([
  80. 'dbuser' => $this->dbUser,
  81. 'dbname' => $this->dbName,
  82. 'dbpassword' => $this->dbPassword,
  83. ]);
  84. }
  85. /**
  86. * @param resource $connection
  87. * @return string
  88. */
  89. protected function getLastError($connection = null) {
  90. if ($connection) {
  91. $error = oci_error($connection);
  92. } else {
  93. $error = oci_error();
  94. }
  95. foreach (['message', 'code'] as $key) {
  96. if (isset($error[$key])) {
  97. return $error[$key];
  98. }
  99. }
  100. return '';
  101. }
  102. }