mysql.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace OC\Setup;
  3. class MySQL extends AbstractDatabase {
  4. public $dbprettyname = 'MySQL';
  5. public function setupDatabase($username) {
  6. //check if the database user has admin right
  7. $connection = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword);
  8. if(!$connection) {
  9. throw new \DatabaseSetupException($this->trans->t('MySQL username and/or password not valid'),
  10. $this->trans->t('You need to enter either an existing account or the administrator.'));
  11. }
  12. $oldUser=\OC_Config::getValue('dbuser', false);
  13. //this should be enough to check for admin rights in mysql
  14. $query="SELECT user FROM mysql.user WHERE user='$this->dbuser'";
  15. if(mysql_query($query, $connection)) {
  16. //use the admin login data for the new database user
  17. //add prefix to the mysql user name to prevent collisions
  18. $this->dbuser=substr('oc_'.$username, 0, 16);
  19. if($this->dbuser!=$oldUser) {
  20. //hash the password so we don't need to store the admin config in the config file
  21. $this->dbpassword=\OC_Util::generateRandomBytes(30);
  22. $this->createDBUser($connection);
  23. \OC_Config::setValue('dbuser', $this->dbuser);
  24. \OC_Config::setValue('dbpassword', $this->dbpassword);
  25. }
  26. //create the database
  27. $this->createDatabase($connection);
  28. }
  29. else {
  30. if($this->dbuser!=$oldUser) {
  31. \OC_Config::setValue('dbuser', $this->dbuser);
  32. \OC_Config::setValue('dbpassword', $this->dbpassword);
  33. }
  34. //create the database
  35. $this->createDatabase($connection);
  36. }
  37. //fill the database if needed
  38. $query='select count(*) from information_schema.tables'
  39. ." where table_schema='".$this->dbname."' AND table_name = '".$this->tableprefix."users';";
  40. $result = mysql_query($query, $connection);
  41. if($result) {
  42. $row=mysql_fetch_row($result);
  43. }
  44. if(!$result or $row[0]==0) {
  45. \OC_DB::createDbFromStructure($this->dbDefinitionFile);
  46. }
  47. mysql_close($connection);
  48. }
  49. private function createDatabase($connection) {
  50. $name = $this->dbname;
  51. $user = $this->dbuser;
  52. //we cant use OC_BD functions here because we need to connect as the administrative user.
  53. $query = "CREATE DATABASE IF NOT EXISTS `$name`";
  54. $result = mysql_query($query, $connection);
  55. if(!$result) {
  56. $entry = $this->trans->t('DB Error: "%s"', array(mysql_error($connection))) . '<br />';
  57. $entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
  58. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  59. }
  60. $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'";
  61. //this query will fail if there aren't the right permissions, ignore the error
  62. mysql_query($query, $connection);
  63. }
  64. private function createDBUser($connection) {
  65. $name = $this->dbuser;
  66. $password = $this->dbpassword;
  67. // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one,
  68. // the anonymous user would take precedence when there is one.
  69. $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'";
  70. $result = mysql_query($query, $connection);
  71. if (!$result) {
  72. throw new \DatabaseSetupException($this->trans->t("MySQL user '%s'@'localhost' exists already.", array($name)),
  73. $this->trans->t("Drop this user from MySQL", array($name)));
  74. }
  75. $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'";
  76. $result = mysql_query($query, $connection);
  77. if (!$result) {
  78. throw new \DatabaseSetupException($this->trans->t("MySQL user '%s'@'%%' already exists", array($name)),
  79. $this->trans->t("Drop this user from MySQL."));
  80. }
  81. }
  82. }