OCI.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Setup;
  8. class OCI extends AbstractDatabase {
  9. public $dbprettyname = 'Oracle';
  10. protected $dbtablespace;
  11. public function initialize($config) {
  12. parent::initialize($config);
  13. if (array_key_exists('dbtablespace', $config)) {
  14. $this->dbtablespace = $config['dbtablespace'];
  15. } else {
  16. $this->dbtablespace = 'USERS';
  17. }
  18. // allow empty hostname for oracle
  19. $this->dbHost = $config['dbhost'];
  20. $this->config->setValues([
  21. 'dbhost' => $this->dbHost,
  22. 'dbtablespace' => $this->dbtablespace,
  23. ]);
  24. }
  25. public function validate($config) {
  26. $errors = [];
  27. if (empty($config['dbuser']) && empty($config['dbname'])) {
  28. $errors[] = $this->trans->t('Enter the database Login and name for %s', [$this->dbprettyname]);
  29. } elseif (empty($config['dbuser'])) {
  30. $errors[] = $this->trans->t('Enter the database Login for %s', [$this->dbprettyname]);
  31. } elseif (empty($config['dbname'])) {
  32. $errors[] = $this->trans->t('Enter the database name for %s', [$this->dbprettyname]);
  33. }
  34. return $errors;
  35. }
  36. public function setupDatabase($username) {
  37. try {
  38. $this->connect();
  39. } catch (\Exception $e) {
  40. $errorMessage = $this->getLastError();
  41. if ($errorMessage) {
  42. throw new \OC\DatabaseSetupException($this->trans->t('Oracle connection could not be established'),
  43. $errorMessage . ' Check environment: ORACLE_HOME=' . getenv('ORACLE_HOME')
  44. . ' ORACLE_SID=' . getenv('ORACLE_SID')
  45. . ' LD_LIBRARY_PATH=' . getenv('LD_LIBRARY_PATH')
  46. . ' NLS_LANG=' . getenv('NLS_LANG')
  47. . ' tnsnames.ora is ' . (is_readable(getenv('ORACLE_HOME') . '/network/admin/tnsnames.ora') ? '' : 'not ') . 'readable', 0, $e);
  48. }
  49. throw new \OC\DatabaseSetupException($this->trans->t('Oracle Login and/or password not valid'),
  50. 'Check environment: ORACLE_HOME=' . getenv('ORACLE_HOME')
  51. . ' ORACLE_SID=' . getenv('ORACLE_SID')
  52. . ' LD_LIBRARY_PATH=' . getenv('LD_LIBRARY_PATH')
  53. . ' NLS_LANG=' . getenv('NLS_LANG')
  54. . ' tnsnames.ora is ' . (is_readable(getenv('ORACLE_HOME') . '/network/admin/tnsnames.ora') ? '' : 'not ') . 'readable', 0, $e);
  55. }
  56. $this->config->setValues([
  57. 'dbuser' => $this->dbUser,
  58. 'dbname' => $this->dbName,
  59. 'dbpassword' => $this->dbPassword,
  60. ]);
  61. }
  62. /**
  63. * @param resource $connection
  64. * @return string
  65. */
  66. protected function getLastError($connection = null) {
  67. if ($connection) {
  68. $error = oci_error($connection);
  69. } else {
  70. $error = oci_error();
  71. }
  72. foreach (['message', 'code'] as $key) {
  73. if (isset($error[$key])) {
  74. return $error[$key];
  75. }
  76. }
  77. return '';
  78. }
  79. }