2013-03-29 15:28:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace OC\Setup;
|
|
|
|
|
2013-04-02 20:01:23 +00:00
|
|
|
class MySQL extends AbstractDatabase {
|
2014-02-07 15:09:31 +00:00
|
|
|
public $dbprettyname = 'MySQL/MariaDB';
|
2013-04-03 15:52:18 +00:00
|
|
|
|
2013-04-02 20:01:23 +00:00
|
|
|
public function setupDatabase($username) {
|
2013-03-29 15:28:48 +00:00
|
|
|
//check if the database user has admin right
|
2013-04-02 20:01:23 +00:00
|
|
|
$connection = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword);
|
2013-03-29 15:28:48 +00:00
|
|
|
if(!$connection) {
|
2014-02-07 15:09:31 +00:00
|
|
|
throw new \DatabaseSetupException($this->trans->t('MySQL/MariaDB username and/or password not valid'),
|
2013-04-02 20:01:23 +00:00
|
|
|
$this->trans->t('You need to enter either an existing account or the administrator.'));
|
2013-03-29 15:28:48 +00:00
|
|
|
}
|
|
|
|
$oldUser=\OC_Config::getValue('dbuser', false);
|
|
|
|
|
|
|
|
//this should be enough to check for admin rights in mysql
|
2013-04-02 20:01:23 +00:00
|
|
|
$query="SELECT user FROM mysql.user WHERE user='$this->dbuser'";
|
2013-03-29 15:28:48 +00:00
|
|
|
if(mysql_query($query, $connection)) {
|
|
|
|
//use the admin login data for the new database user
|
|
|
|
|
|
|
|
//add prefix to the mysql user name to prevent collisions
|
2013-04-02 20:01:23 +00:00
|
|
|
$this->dbuser=substr('oc_'.$username, 0, 16);
|
|
|
|
if($this->dbuser!=$oldUser) {
|
2013-03-29 15:28:48 +00:00
|
|
|
//hash the password so we don't need to store the admin config in the config file
|
2013-08-15 06:49:19 +00:00
|
|
|
$this->dbpassword=\OC_Util::generateRandomBytes(30);
|
2013-03-29 15:28:48 +00:00
|
|
|
|
2013-04-02 20:01:23 +00:00
|
|
|
$this->createDBUser($connection);
|
2013-03-29 15:28:48 +00:00
|
|
|
|
2013-04-02 20:01:23 +00:00
|
|
|
\OC_Config::setValue('dbuser', $this->dbuser);
|
|
|
|
\OC_Config::setValue('dbpassword', $this->dbpassword);
|
2013-03-29 15:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//create the database
|
2013-04-02 20:01:23 +00:00
|
|
|
$this->createDatabase($connection);
|
2013-03-29 15:28:48 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-04-02 20:01:23 +00:00
|
|
|
if($this->dbuser!=$oldUser) {
|
|
|
|
\OC_Config::setValue('dbuser', $this->dbuser);
|
|
|
|
\OC_Config::setValue('dbpassword', $this->dbpassword);
|
2013-03-29 15:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//create the database
|
2013-04-02 20:01:23 +00:00
|
|
|
$this->createDatabase($connection);
|
2013-03-29 15:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//fill the database if needed
|
|
|
|
$query='select count(*) from information_schema.tables'
|
2013-04-10 22:08:53 +00:00
|
|
|
." where table_schema='".$this->dbname."' AND table_name = '".$this->tableprefix."users';";
|
2013-03-29 15:28:48 +00:00
|
|
|
$result = mysql_query($query, $connection);
|
|
|
|
if($result) {
|
|
|
|
$row=mysql_fetch_row($result);
|
|
|
|
}
|
|
|
|
if(!$result or $row[0]==0) {
|
2013-06-27 18:19:51 +00:00
|
|
|
\OC_DB::createDbFromStructure($this->dbDefinitionFile);
|
2013-03-29 15:28:48 +00:00
|
|
|
}
|
|
|
|
mysql_close($connection);
|
|
|
|
}
|
|
|
|
|
2013-04-02 20:01:23 +00:00
|
|
|
private function createDatabase($connection) {
|
|
|
|
$name = $this->dbname;
|
|
|
|
$user = $this->dbuser;
|
2013-03-29 15:28:48 +00:00
|
|
|
//we cant use OC_BD functions here because we need to connect as the administrative user.
|
2014-07-07 15:37:35 +00:00
|
|
|
$query = "CREATE DATABASE IF NOT EXISTS `$name` CHARACTER SET utf8 COLLATE utf8_bin;";
|
2013-03-29 15:28:48 +00:00
|
|
|
$result = mysql_query($query, $connection);
|
|
|
|
if(!$result) {
|
2013-04-02 20:01:23 +00:00
|
|
|
$entry = $this->trans->t('DB Error: "%s"', array(mysql_error($connection))) . '<br />';
|
|
|
|
$entry .= $this->trans->t('Offending command was: "%s"', array($query)) . '<br />';
|
2013-03-29 15:28:48 +00:00
|
|
|
\OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
|
|
|
|
}
|
2013-04-02 20:01:23 +00:00
|
|
|
$query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'";
|
2013-03-29 15:28:48 +00:00
|
|
|
|
|
|
|
//this query will fail if there aren't the right permissions, ignore the error
|
|
|
|
mysql_query($query, $connection);
|
|
|
|
}
|
|
|
|
|
2013-04-02 20:01:23 +00:00
|
|
|
private function createDBUser($connection) {
|
|
|
|
$name = $this->dbuser;
|
|
|
|
$password = $this->dbpassword;
|
2013-03-29 15:28:48 +00:00
|
|
|
// we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one,
|
|
|
|
// the anonymous user would take precedence when there is one.
|
|
|
|
$query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'";
|
|
|
|
$result = mysql_query($query, $connection);
|
|
|
|
if (!$result) {
|
2014-02-07 15:09:31 +00:00
|
|
|
throw new \DatabaseSetupException($this->trans->t("MySQL/MariaDB user '%s'@'localhost' exists already.", array($name)),
|
|
|
|
$this->trans->t("Drop this user from MySQL/MariaDB", array($name)));
|
2013-03-29 15:28:48 +00:00
|
|
|
}
|
|
|
|
$query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'";
|
|
|
|
$result = mysql_query($query, $connection);
|
|
|
|
if (!$result) {
|
2014-02-07 15:09:31 +00:00
|
|
|
throw new \DatabaseSetupException($this->trans->t("MySQL/MariaDB user '%s'@'%%' already exists", array($name)),
|
|
|
|
$this->trans->t("Drop this user from MySQL/MariaDB."));
|
2013-03-29 15:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|