2017-03-15 06:17:16 +01:00
|
|
|
<?php
|
|
|
|
//include '../../config.php';
|
|
|
|
use RedBeanPHP\Facade as RedBean;
|
|
|
|
|
|
|
|
class InitDatabaseController extends Controller {
|
|
|
|
const PATH = '/init-database';
|
|
|
|
const METHOD = 'POST';
|
|
|
|
|
|
|
|
public function validations() {
|
|
|
|
return [
|
|
|
|
'permission' => 'any',
|
|
|
|
'requestData' => []
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handler() {
|
2017-03-31 01:37:55 +02:00
|
|
|
if(defined('MYSQL_HOST')) {
|
2017-03-15 06:17:16 +01:00
|
|
|
throw new Exception(ERRORS::INIT_SETTINGS_DONE);
|
2017-03-29 23:46:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$dbHost = Controller::request('dbHost');
|
|
|
|
$dbName = Controller::request('dbName');
|
|
|
|
$dbUser = Controller::request('dbUser');
|
|
|
|
$dbPass = Controller::request('dbPassword');
|
|
|
|
|
|
|
|
if(!defined('MYSQL_HOST')) {
|
|
|
|
RedBean::setup('mysql:host=' . $dbHost, $dbUser, $dbPass);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($dbName) {
|
|
|
|
RedBean::addDatabase($dbName, 'mysql:host='. $dbHost . ';dbname=' . $dbName, $dbUser, $dbPass);
|
|
|
|
RedBean::selectDatabase($dbName);
|
|
|
|
|
|
|
|
if(!RedBean::testConnection()) {
|
|
|
|
throw new Exception(ERRORS::DATABASE_CONNECTION);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$dbName = 'opensupports_' . Hashing::generateRandomNumber(100, 999);
|
|
|
|
RedBean::exec('CREATE DATABASE ' . $dbName);
|
|
|
|
RedBean::addDatabase($dbName, 'mysql:host='. $dbHost . ';dbname=' . $dbName, $dbUser, $dbPass);
|
|
|
|
RedBean::selectDatabase($dbName);
|
|
|
|
|
|
|
|
if(!RedBean::testConnection()) {
|
|
|
|
throw new Exception(ERRORS::DATABASE_CREATION);
|
|
|
|
}
|
2017-03-15 06:17:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$configFile = fopen('config.php', 'w+') or die(ERRORS::INVALID_FILE);
|
|
|
|
$content = '<?php' . PHP_EOL;
|
2017-03-29 23:46:26 +02:00
|
|
|
$content .= 'define(\'MYSQL_HOST\', \'' . $dbHost . '\');' . PHP_EOL;
|
|
|
|
$content .= 'define(\'MYSQL_USER\', \'' . $dbUser . '\');' . PHP_EOL;
|
|
|
|
$content .= 'define(\'MYSQL_PASSWORD\', \'' . $dbPass . '\');' . PHP_EOL;
|
|
|
|
$content .= 'define(\'MYSQL_DATABASE\', \'' . $dbName . '\');' . PHP_EOL;
|
2017-03-15 06:17:16 +01:00
|
|
|
|
|
|
|
fwrite($configFile, $content);
|
|
|
|
fclose($configFile);
|
|
|
|
Response::respondSuccess();
|
|
|
|
}
|
|
|
|
}
|