mssql.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace OC\Setup;
  3. class MSSQL extends AbstractDatabase {
  4. public $dbprettyname = 'MS SQL Server';
  5. public function setupDatabase() {
  6. //check if the database user has admin right
  7. $masterConnectionInfo = array( "Database" => "master", "UID" => $this->dbuser, "PWD" => $this->dbpassword);
  8. $masterConnection = @sqlsrv_connect($this->dbhost, $masterConnectionInfo);
  9. if(!$masterConnection) {
  10. $entry = null;
  11. if( ($errors = sqlsrv_errors() ) != null) {
  12. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  13. } else {
  14. $entry = '';
  15. }
  16. throw new \DatabaseSetupException($this->trans->t('MS SQL username and/or password not valid: %s', array($entry)),
  17. $this->trans->t('You need to enter either an existing account or the administrator.'));
  18. }
  19. \OC_Config::setValue('dbuser', $this->dbuser);
  20. \OC_Config::setValue('dbpassword', $this->dbpassword);
  21. $this->createDBLogin($masterConnection);
  22. $this->createDatabase($masterConnection);
  23. $this->createDBUser($masterConnection);
  24. sqlsrv_close($masterConnection);
  25. $this->createDatabaseStructure();
  26. }
  27. private function createDBLogin($connection) {
  28. $query = "SELECT * FROM master.sys.server_principals WHERE name = '".$this->dbuser."';";
  29. $result = sqlsrv_query($connection, $query);
  30. if ($result === false) {
  31. if ( ($errors = sqlsrv_errors() ) != null) {
  32. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  33. } else {
  34. $entry = '';
  35. }
  36. $entry.='Offending command was: '.$query.'<br />';
  37. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  38. } else {
  39. $row = sqlsrv_fetch_array($result);
  40. if ($row === false) {
  41. if ( ($errors = sqlsrv_errors() ) != null) {
  42. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  43. } else {
  44. $entry = '';
  45. }
  46. $entry.='Offending command was: '.$query.'<br />';
  47. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  48. } else {
  49. if ($row == null) {
  50. $query = "CREATE LOGIN [".$this->dbuser."] WITH PASSWORD = '".$this->dbpassword."';";
  51. $result = sqlsrv_query($connection, $query);
  52. if (!$result or $result === false) {
  53. if ( ($errors = sqlsrv_errors() ) != null) {
  54. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  55. } else {
  56. $entry = '';
  57. }
  58. $entry.='Offending command was: '.$query.'<br />';
  59. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. private function createDBUser($connection) {
  66. $query = "SELECT * FROM [".$this->dbname."].sys.database_principals WHERE name = '".$this->dbuser."';";
  67. $result = sqlsrv_query($connection, $query);
  68. if ($result === false) {
  69. if ( ($errors = sqlsrv_errors() ) != null) {
  70. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  71. } else {
  72. $entry = '';
  73. }
  74. $entry.='Offending command was: '.$query.'<br />';
  75. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  76. } else {
  77. $row = sqlsrv_fetch_array($result);
  78. if ($row === false) {
  79. if ( ($errors = sqlsrv_errors() ) != null) {
  80. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  81. } else {
  82. $entry = '';
  83. }
  84. $entry.='Offending command was: '.$query.'<br />';
  85. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  86. } else {
  87. if ($row == null) {
  88. $query = "USE [".$this->dbname."]; CREATE USER [".$this->dbuser."] FOR LOGIN [".$this->dbuser."];";
  89. $result = sqlsrv_query($connection, $query);
  90. if (!$result || $result === false) {
  91. if ( ($errors = sqlsrv_errors() ) != null) {
  92. $entry = 'DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  93. } else {
  94. $entry = '';
  95. }
  96. $entry.='Offending command was: '.$query.'<br />';
  97. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  98. }
  99. }
  100. $query = "USE [".$this->dbname."]; EXEC sp_addrolemember 'db_owner', '".$this->dbuser."';";
  101. $result = sqlsrv_query($connection, $query);
  102. if (!$result || $result === false) {
  103. if ( ($errors = sqlsrv_errors() ) != null) {
  104. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  105. } else {
  106. $entry = '';
  107. }
  108. $entry.='Offending command was: '.$query.'<br />';
  109. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  110. }
  111. }
  112. }
  113. }
  114. private function createDatabase($connection) {
  115. $query = "CREATE DATABASE [".$this->dbname."];";
  116. $result = sqlsrv_query($connection, $query);
  117. if (!$result || $result === false) {
  118. if ( ($errors = sqlsrv_errors() ) != null) {
  119. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  120. } else {
  121. $entry = '';
  122. }
  123. $entry.='Offending command was: '.$query.'<br />';
  124. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  125. }
  126. }
  127. private function createDatabaseStructure() {
  128. $connectionInfo = array( "Database" => $this->dbname, "UID" => $this->dbuser, "PWD" => $this->dbpassword);
  129. $connection = @sqlsrv_connect($this->dbhost, $connectionInfo);
  130. //fill the database if needed
  131. $query = "SELECT * FROM INFORMATION_SCHEMA.TABLES"
  132. ." WHERE TABLE_SCHEMA = '".$this->dbname."'"
  133. ." AND TABLE_NAME = '".$this->tableprefix."users'";
  134. $result = sqlsrv_query($connection, $query);
  135. if ($result === false) {
  136. if ( ($errors = sqlsrv_errors() ) != null) {
  137. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  138. } else {
  139. $entry = '';
  140. }
  141. $entry.='Offending command was: '.$query.'<br />';
  142. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  143. } else {
  144. $row = sqlsrv_fetch_array($result);
  145. if ($row === false) {
  146. if ( ($errors = sqlsrv_errors() ) != null) {
  147. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  148. } else {
  149. $entry = '';
  150. }
  151. $entry.='Offending command was: '.$query.'<br />';
  152. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  153. } else {
  154. if ($row == null) {
  155. \OC_DB::createDbFromStructure($this->dbDefinitionFile);
  156. }
  157. }
  158. }
  159. sqlsrv_close($connection);
  160. }
  161. }