2018-11-15 15:08:11 +00:00
|
|
|
<?php
|
|
|
|
require __DIR__ . '/../vendor/autoload.php';
|
|
|
|
|
|
|
|
use App\Database\DB;
|
|
|
|
use App\Web\Session;
|
|
|
|
use Slim\App;
|
|
|
|
use Slim\Container;
|
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
|
|
|
|
|
|
|
define('PLATFORM_VERSION', json_decode(file_get_contents(__DIR__ . '/../composer.json'))->version);
|
|
|
|
|
|
|
|
$config = [
|
|
|
|
'base_url' => isset($_SERVER['HTTPS']) ? 'https://' . $_SERVER['HTTP_HOST'] : 'http://' . $_SERVER['HTTP_HOST'],
|
|
|
|
'storage_dir' => 'storage',
|
|
|
|
'displayErrorDetails' => true,
|
2019-01-31 10:48:39 +00:00
|
|
|
'maintenance' => false,
|
2018-11-15 15:08:11 +00:00
|
|
|
'db' => [
|
|
|
|
'connection' => 'sqlite',
|
|
|
|
'dsn' => 'resources/database/xbackbone.db',
|
|
|
|
'username' => null,
|
|
|
|
'password' => null,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$container = new Container(['settings' => $config]);
|
|
|
|
|
2019-01-10 22:22:19 +00:00
|
|
|
$container['session'] = function ($container) {
|
|
|
|
return new Session('xbackbone_session');
|
|
|
|
};
|
2018-11-15 15:08:11 +00:00
|
|
|
|
|
|
|
$container['view'] = function ($container) use (&$config) {
|
2018-12-06 20:20:50 +00:00
|
|
|
$view = new \Slim\Views\Twig([__DIR__ . '/templates', __DIR__ . '/../resources/templates'], [
|
2018-11-15 15:08:11 +00:00
|
|
|
'cache' => false,
|
|
|
|
'autoescape' => 'html',
|
|
|
|
'debug' => $config['displayErrorDetails'],
|
|
|
|
'auto_reload' => $config['displayErrorDetails'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Instantiate and add Slim specific extension
|
|
|
|
$router = $container->get('router');
|
|
|
|
$uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER));
|
|
|
|
$view->addExtension(new Slim\Views\TwigExtension($router, $uri));
|
|
|
|
|
|
|
|
$view->getEnvironment()->addGlobal('config', $config);
|
|
|
|
$view->getEnvironment()->addGlobal('request', $container->get('request'));
|
2019-01-10 22:22:19 +00:00
|
|
|
$view->getEnvironment()->addGlobal('alerts', $container->get('session')->getAlert());
|
|
|
|
$view->getEnvironment()->addGlobal('session', $container->get('session')->all());
|
2018-11-15 15:08:11 +00:00
|
|
|
$view->getEnvironment()->addGlobal('PLATFORM_VERSION', PLATFORM_VERSION);
|
|
|
|
return $view;
|
|
|
|
};
|
|
|
|
|
|
|
|
function migrate($config)
|
|
|
|
{
|
|
|
|
$firstMigrate = false;
|
|
|
|
if ($config['db']['connection'] === 'sqlite' && !file_exists(__DIR__ . '/../' . $config['db']['dsn'])) {
|
|
|
|
touch(__DIR__ . '/../' . $config['db']['dsn']);
|
|
|
|
$firstMigrate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-01-10 22:22:19 +00:00
|
|
|
DB::doQuery('SELECT 1 FROM `migrations` LIMIT 1');
|
2018-11-15 15:08:11 +00:00
|
|
|
} catch (PDOException $exception) {
|
|
|
|
$firstMigrate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($firstMigrate) {
|
|
|
|
DB::raw()->exec(file_get_contents(__DIR__ . '/../resources/schemas/migrations.sql'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$files = glob(__DIR__ . '/../resources/schemas/' . DB::driver() . '/*.sql');
|
|
|
|
|
|
|
|
$names = array_map(function ($path) {
|
|
|
|
return basename($path);
|
|
|
|
}, $files);
|
|
|
|
|
|
|
|
$in = str_repeat('?, ', count($names) - 1) . '?';
|
|
|
|
|
2019-01-10 22:22:19 +00:00
|
|
|
$inMigrationsTable = DB::doQuery("SELECT * FROM `migrations` WHERE `name` IN ($in)", $names)->fetchAll();
|
2018-11-15 15:08:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
|
|
|
|
$continue = false;
|
|
|
|
$exists = false;
|
|
|
|
|
|
|
|
foreach ($inMigrationsTable as $migration) {
|
|
|
|
if (basename($file) === $migration->name && $migration->migrated) {
|
|
|
|
$continue = true;
|
|
|
|
break;
|
|
|
|
} else if (basename($file) === $migration->name && !$migration->migrated) {
|
|
|
|
$exists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($continue) continue;
|
|
|
|
|
|
|
|
$sql = file_get_contents($file);
|
|
|
|
try {
|
|
|
|
DB::raw()->exec($sql);
|
|
|
|
if (!$exists) {
|
2019-01-10 22:22:19 +00:00
|
|
|
DB::doQuery('INSERT INTO `migrations` VALUES (?,?)', [basename($file), 1]);
|
2018-11-15 15:08:11 +00:00
|
|
|
} else {
|
2019-01-10 22:22:19 +00:00
|
|
|
DB::doQuery('UPDATE `migrations` SET `migrated`=? WHERE `name`=?', [1, basename($file)]);
|
2018-11-15 15:08:11 +00:00
|
|
|
}
|
|
|
|
} catch (PDOException $exception) {
|
|
|
|
if (!$exists) {
|
2019-01-10 22:22:19 +00:00
|
|
|
DB::doQuery('INSERT INTO `migrations` VALUES (?,?)', [basename($file), 0]);
|
2018-11-15 15:08:11 +00:00
|
|
|
}
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$app = new App($container);
|
|
|
|
|
|
|
|
$app->get('/', function (Request $request, Response $response) {
|
|
|
|
|
2018-12-09 13:42:50 +00:00
|
|
|
if (!is_writable(__DIR__ . '/../resources/cache')) {
|
2019-01-10 22:22:19 +00:00
|
|
|
$this->session->alert('The cache folder is not writable (' . __DIR__ . '/../resources/cache' . ')', 'danger');
|
2018-12-09 13:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_writable(__DIR__ . '/../resources/database')) {
|
2019-01-10 22:22:19 +00:00
|
|
|
$this->session->alert('The database folder is not writable (' . __DIR__ . '/../resources/database' . ')', 'danger');
|
2018-12-09 13:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_writable(__DIR__ . '/../resources/sessions')) {
|
2019-01-10 22:22:19 +00:00
|
|
|
$this->session->alert('The sessions folder is not writable (' . __DIR__ . '/../resources/sessions' . ')', 'danger');
|
2018-12-09 13:42:50 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:08:11 +00:00
|
|
|
$installed = file_exists(__DIR__ . '/../config.php');
|
|
|
|
|
|
|
|
return $this->view->render($response, 'install.twig', ['installed' => $installed]);
|
|
|
|
});
|
|
|
|
|
|
|
|
$app->post('/', function (Request $request, Response $response) use (&$config) {
|
|
|
|
$installed = true;
|
|
|
|
if (!file_exists(__DIR__ . '/../config.php')) {
|
|
|
|
$installed = false;
|
|
|
|
|
|
|
|
$config['base_url'] = $request->getParam('base_url');
|
|
|
|
$config['storage_dir'] = $request->getParam('storage_dir');
|
|
|
|
$config['displayErrorDetails'] = false;
|
|
|
|
$config['db']['connection'] = $request->getParam('connection');
|
|
|
|
$config['db']['dsn'] = $request->getParam('dsn');
|
|
|
|
$config['db']['username'] = $request->getParam('db_user');
|
|
|
|
$config['db']['password'] = $request->getParam('db_password');
|
|
|
|
|
2018-12-09 13:42:50 +00:00
|
|
|
try {
|
|
|
|
storage($config['storage_dir']);
|
|
|
|
} catch (LogicException $exception) {
|
2019-01-10 22:22:19 +00:00
|
|
|
$this->session->alert('The storage folder is not readable (' . $config['storage_dir'] . ')', 'danger');
|
2018-12-09 13:42:50 +00:00
|
|
|
return redirect($response, './');
|
|
|
|
} finally {
|
|
|
|
if (!is_writable($config['storage_dir'])) {
|
2019-01-10 22:22:19 +00:00
|
|
|
$this->session->alert('The storage folder is not writable (' . $config['storage_dir'] . ')', 'danger');
|
2018-12-09 13:42:50 +00:00
|
|
|
return redirect($response, './');
|
|
|
|
}
|
2018-12-07 09:17:45 +00:00
|
|
|
}
|
2018-11-15 15:08:11 +00:00
|
|
|
|
2018-12-07 09:17:45 +00:00
|
|
|
$ret = file_put_contents(__DIR__ . '/../config.php', '<?php' . PHP_EOL . 'return ' . var_export($config, true) . ';');
|
|
|
|
if ($ret === false) {
|
2019-01-10 22:22:19 +00:00
|
|
|
$this->session->alert('The config folder is not writable (' . __DIR__ . '/../config.php' . ')', 'danger');
|
2018-12-09 13:42:50 +00:00
|
|
|
return redirect($response, './');
|
2018-12-07 09:17:45 +00:00
|
|
|
}
|
2018-11-15 15:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$dsn = $config['db']['connection'] === 'sqlite' ? __DIR__ . '/../' . $config['db']['dsn'] : $config['db']['dsn'];
|
|
|
|
|
2018-12-06 20:20:50 +00:00
|
|
|
try {
|
|
|
|
DB::setDsn($config['db']['connection'] . ':' . $dsn, $config['db']['username'], $config['db']['password']);
|
|
|
|
|
|
|
|
migrate($config);
|
|
|
|
} catch (PDOException $exception) {
|
2019-01-10 22:22:19 +00:00
|
|
|
$this->session->alert("Cannot connect to the database: {$exception->getMessage()} [{$exception->getCode()}]", 'danger');
|
2018-12-09 13:42:50 +00:00
|
|
|
return redirect($response, './');
|
2018-12-06 20:20:50 +00:00
|
|
|
}
|
2018-11-15 15:08:11 +00:00
|
|
|
|
|
|
|
if (!$installed) {
|
2019-01-10 22:22:19 +00:00
|
|
|
DB::doQuery("INSERT INTO `users` (`email`, `username`, `password`, `is_admin`, `user_code`) VALUES (?, 'admin', ?, 1, ?)", [$request->getParam('email'), password_hash($request->getParam('password'), PASSWORD_DEFAULT), substr(md5(microtime()), rand(0, 26), 5)]);
|
2018-11-15 15:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanDirectory(__DIR__ . '/../resources/cache');
|
|
|
|
cleanDirectory(__DIR__ . '/../resources/sessions');
|
|
|
|
|
|
|
|
return $response->withRedirect('../?afterInstall=true');
|
|
|
|
});
|
|
|
|
|
|
|
|
$app->run();
|