public function createDatabase()
{
if (!Helper::isInstallAvailable()) {
return sprintf(__('Please remove content from %s file.', 'Install', false), 'config.php');
}
$db = ipRequest()->getPost('db');
if (!isset($_SESSION['db_errors'])) {
$_SESSION['db_errors'] = array();
}
foreach (array('hostname', 'username', 'database') as $key) {
if (empty($db[$key])) {
$_SESSION['db_errors'][] = 'Required fields';
return \Ip\Response\JsonRpc::error(__('Please fill in required fields.', 'Install', false));
}
}
if (empty($db['tablePrefix'])) {
$db['tablePrefix'] = '';
}
if (strlen($db['tablePrefix']) > 7) {
$_SESSION['db_errors'][] = 'Prefix too long';
return \Ip\Response\JsonRpc::error(__("Prefix can't be longer than 7 symbols.", 'Install', false));
}
if ($db['tablePrefix'] != '' && !preg_match('/^([A-Za-z_][A-Za-z0-9_]*)$/', $db['tablePrefix'])) {
$_SESSION['db_errors'][] = 'Prefix is bad';
return \Ip\Response\JsonRpc::error(__("Prefix can't contain any special characters and should start with a letter.", 'Install', false));
}
$dbConfig = array('hostname' => $db['hostname'], 'username' => $db['username'], 'password' => $db['password'], 'tablePrefix' => $db['tablePrefix'], 'database' => '', 'charset' => 'utf8');
ipConfig()->set('db', $dbConfig);
try {
ipDb()->getConnection();
} catch (\Exception $e) {
$_SESSION['db_errors'][] = 'Cannot connect';
return \Ip\Response\JsonRpc::error(__("Can't connect to database.", 'Install'), false);
}
try {
Model::createAndUseDatabase($db['database']);
} catch (\Ip\Exception $e) {
$_SESSION['db_errors'][] = 'DB cannot be created';
return \Ip\Response\JsonRpc::error(__('Specified database does not exists and cannot be created.', 'Install', false));
}
if (Helper::testDBTables($db['tablePrefix']) && empty($db['replaceTables'])) {
$_SESSION['db_errors'][] = 'Replace tables';
return \Ip\Response\JsonRpc::error(__('Do you like to replace existing tables in the database?', 'Install', false), 'table_exists');
}
$errors = Model::createDatabaseStructure($db['database'], $db['tablePrefix']);
if (!$errors) {
$errors = Model::importData($dbConfig['tablePrefix']);
}
if ($errors) {
$_SESSION['db_errors'][] = 'Failed install';
return \Ip\Response\JsonRpc::error(__('There were errors while executing install queries. ' . serialize($errors), 'Install', false));
}
$dbConfig['database'] = $db['database'];
$_SESSION['db'] = $dbConfig;
$configToFile = array();
$configToFile['sessionName'] = 'ses' . rand();
$configToFile['db'] = $_SESSION['db'];
$configToFile['timezone'] = $_SESSION['config']['timezone'];
if (Helper::checkModRewrite() != 'success') {
$configToFile['rewritesDisabled'] = true;
}
$admin = ipRequest()->getPost('admin');
if ($admin) {
$adminUsername = $admin['username'];
$adminEmail = $admin['email'];
$adminPassword = $admin['password'];
}
$cachedBaseUrl = substr(rtrim(ipConfig()->baseUrl(), "/"), 0, -strlen('install'));
try {
ipConfig()->set('db', $dbConfig);
// if admin data is posted then user will be created
if ($admin) {
Model::insertAdmin($adminUsername, $adminEmail, $adminPassword);
}
ipSetOptionLang('Config.websiteTitle', $_SESSION['config']['websiteName'], 'en');
ipSetOptionLang('Config.websiteEmail', $_SESSION['config']['websiteEmail'], 'en');
Model::generateCronPassword();
ipStorage()->set('Ip', 'cachedBaseUrl', $cachedBaseUrl);
ipStorage()->set('Ip', 'websiteId', $_SESSION['websiteId']);
ipStorage()->set('Ip', 'getImpressPagesSupport', $_SESSION['config']['support']);
} catch (\Exception $e) {
$_SESSION['db_errors'][] = $e->getTraceAsString();
return \Ip\Response\JsonRpc::error($e->getTraceAsString());
}
try {
Model::writeConfigFile($configToFile, ipFile('config.php'));
} catch (\Exception $e) {
$_SESSION['db_errors'][] = 'Cannot write config file';
return \Ip\Response\JsonRpc::error(__('Can\'t write configuration "/config.php"', 'Install', false));
}
// Send usage statistics
$usageStatistics = Helper::setUsageStatistics('Install.database', $_SESSION['db_errors']);
\Ip\Internal\System\Model::sendUsageStatistics($usageStatistics);
$redirect = $cachedBaseUrl . 'admin';
return \Ip\Response\JsonRpc::result(array('redirect' => $redirect));
}