2017-03-15 06:17:16 +01:00
|
|
|
<?php
|
|
|
|
use RedBeanPHP\Facade as RedBean;
|
|
|
|
|
2017-05-12 06:58:40 +02:00
|
|
|
/**
|
|
|
|
* @api {post} /system/init-database Init database
|
2021-10-19 03:06:20 +02:00
|
|
|
* @apiVersion 4.10.0
|
2017-05-12 06:58:40 +02:00
|
|
|
*
|
|
|
|
* @apiName Init database
|
|
|
|
*
|
|
|
|
* @apiGroup System
|
|
|
|
*
|
|
|
|
* @apiDescription This path sets the database settings. It can only be used once during installation.
|
|
|
|
*
|
|
|
|
* @apiPermission any
|
|
|
|
*
|
2018-04-26 22:42:59 +02:00
|
|
|
* @apiParam {String} dbHost Location of the database server.
|
|
|
|
* @apiParam {String} dbPort Port of the database server.
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiParam {String} dbName Name of the database. If not given, the system will try to create one.
|
|
|
|
* @apiParam {String} dbUser User of the database server.
|
|
|
|
* @apiParam {String} dbPassword Password of the database server.
|
|
|
|
*
|
|
|
|
* @apiUse DATABASE_CONNECTION
|
|
|
|
* @apiUse DATABASE_CREATION
|
|
|
|
* @apiUse INVALID_FILE
|
|
|
|
*
|
|
|
|
* @apiSuccess {Object} data Empty object
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-03-15 06:17:16 +01:00
|
|
|
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')) {
|
2018-11-20 23:41:00 +01:00
|
|
|
throw new RequestException(ERRORS::INIT_SETTINGS_DONE);
|
2017-03-29 23:46:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$dbHost = Controller::request('dbHost');
|
2018-03-28 04:30:18 +02:00
|
|
|
$dbPort = Controller::request('dbPort');
|
2017-03-29 23:46:26 +02:00
|
|
|
$dbName = Controller::request('dbName');
|
|
|
|
$dbUser = Controller::request('dbUser');
|
|
|
|
$dbPass = Controller::request('dbPassword');
|
|
|
|
|
2018-03-28 04:30:18 +02:00
|
|
|
RedBean::setup("mysql:host=$dbHost;port=$dbPort", $dbUser, $dbPass);
|
2017-03-29 23:46:26 +02:00
|
|
|
|
|
|
|
if($dbName) {
|
2018-03-28 04:30:18 +02:00
|
|
|
RedBean::addDatabase($dbName, "mysql:host=$dbHost;port=$dbPort;dbname=$dbName", $dbUser, $dbPass);
|
2017-03-29 23:46:26 +02:00
|
|
|
RedBean::selectDatabase($dbName);
|
|
|
|
|
|
|
|
if(!RedBean::testConnection()) {
|
2018-11-20 23:41:00 +01:00
|
|
|
throw new RequestException(ERRORS::DATABASE_CONNECTION);
|
2017-03-29 23:46:26 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$dbName = 'opensupports_' . Hashing::generateRandomNumber(100, 999);
|
|
|
|
RedBean::exec('CREATE DATABASE ' . $dbName);
|
2018-03-28 04:30:18 +02:00
|
|
|
RedBean::addDatabase($dbName, "mysql:host=$dbHost;port=$dbPort;dbname=$dbName", $dbUser, $dbPass);
|
2017-03-29 23:46:26 +02:00
|
|
|
RedBean::selectDatabase($dbName);
|
|
|
|
|
|
|
|
if(!RedBean::testConnection()) {
|
2018-11-20 23:41:00 +01:00
|
|
|
throw new RequestException(ERRORS::DATABASE_CREATION);
|
2017-03-29 23:46:26 +02:00
|
|
|
}
|
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;
|
2018-03-28 04:30:18 +02:00
|
|
|
$content .= 'define(\'MYSQL_PORT\', \'' . $dbPort . '\');' . PHP_EOL;
|
2017-03-29 23:46:26 +02:00
|
|
|
$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();
|
|
|
|
}
|
2018-01-11 22:51:07 +01:00
|
|
|
}
|