2018-11-15 15:08:11 +00:00
|
|
|
<?php
|
2019-05-09 10:51:33 +00:00
|
|
|
(PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 1) ?: die('Sorry, PHP 7.1 or above is required to run XBackBone.');
|
2018-11-15 15:08:11 +00:00
|
|
|
require __DIR__ . '/../vendor/autoload.php';
|
|
|
|
|
|
|
|
use App\Database\DB;
|
|
|
|
use App\Web\Session;
|
2019-05-19 13:39:42 +00:00
|
|
|
use Aws\S3\S3Client;
|
|
|
|
use Google\Cloud\Storage\StorageClient;
|
|
|
|
use League\Flysystem\Adapter\Local;
|
|
|
|
use League\Flysystem\AwsS3v3\AwsS3Adapter;
|
|
|
|
use League\Flysystem\Adapter\Ftp as FtpAdapter;
|
|
|
|
use Spatie\Dropbox\Client as DropboxClient;
|
|
|
|
use League\Flysystem\Filesystem;
|
2018-11-15 15:08:11 +00:00
|
|
|
use Slim\App;
|
|
|
|
use Slim\Container;
|
2019-05-06 22:49:24 +00:00
|
|
|
use Slim\Http\Environment;
|
2018-11-15 15:08:11 +00:00
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
2019-05-06 22:49:24 +00:00
|
|
|
use Slim\Http\Uri;
|
|
|
|
use Slim\Views\Twig;
|
2019-05-19 13:39:42 +00:00
|
|
|
use Spatie\FlysystemDropbox\DropboxAdapter;
|
|
|
|
use Superbalist\Flysystem\GoogleStorage\GoogleStorageAdapter;
|
2018-11-15 15:08:11 +00:00
|
|
|
|
|
|
|
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'],
|
2019-05-23 19:24:04 +00:00
|
|
|
'displayErrorDetails' => true,
|
2018-11-15 15:08:11 +00:00
|
|
|
'db' => [
|
|
|
|
'connection' => 'sqlite',
|
|
|
|
'dsn' => 'resources/database/xbackbone.db',
|
|
|
|
'username' => null,
|
|
|
|
'password' => null,
|
|
|
|
],
|
2019-05-19 13:39:42 +00:00
|
|
|
'storage' => [
|
|
|
|
'driver' => 'local',
|
|
|
|
],
|
2018-11-15 15:08:11 +00:00
|
|
|
];
|
|
|
|
|
2019-05-19 13:39:42 +00:00
|
|
|
if (file_exists(__DIR__ . '/../config.php')) {
|
|
|
|
$config = array_replace_recursive($config, require __DIR__ . '/../config.php');
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:08:11 +00:00
|
|
|
$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) {
|
2019-05-06 22:49:24 +00:00
|
|
|
$view = new 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');
|
2019-05-06 22:49:24 +00:00
|
|
|
$uri = Uri::createFromEnvironment(new Environment($_SERVER));
|
2018-11-15 15:08:11 +00:00
|
|
|
$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;
|
|
|
|
};
|
|
|
|
|
2019-05-19 13:39:42 +00:00
|
|
|
$container['storage'] = function ($container) use (&$config) {
|
|
|
|
|
|
|
|
switch ($config['storage']['driver']) {
|
|
|
|
case 'local':
|
|
|
|
return new Filesystem(new Local($config['storage']['path']));
|
|
|
|
case 's3':
|
|
|
|
$client = new S3Client([
|
|
|
|
'credentials' => [
|
|
|
|
'key' => $config['storage']['key'],
|
|
|
|
'secret' => $config['storage']['secret'],
|
|
|
|
],
|
|
|
|
'region' => $config['storage']['region'],
|
|
|
|
'version' => 'latest',
|
|
|
|
]);
|
|
|
|
|
|
|
|
return new Filesystem(new AwsS3Adapter($client, $config['storage']['bucket'], $config['storage']['path']));
|
|
|
|
case 'dropbox':
|
|
|
|
$client = new DropboxClient($config['storage']['token']);
|
|
|
|
return new Filesystem(new DropboxAdapter($client), ['case_sensitive' => false]);
|
|
|
|
case 'ftp':
|
|
|
|
return new Filesystem(new FtpAdapter([
|
|
|
|
'host' => $config['storage']['host'],
|
|
|
|
'username' => $config['storage']['username'],
|
|
|
|
'password' => $config['storage']['password'],
|
|
|
|
'port' => $config['storage']['port'],
|
|
|
|
'root' => $config['storage']['path'],
|
|
|
|
'passive' => $config['storage']['passive'],
|
|
|
|
'ssl' => $config['storage']['ssl'],
|
|
|
|
'timeout' => 30,
|
|
|
|
]));
|
|
|
|
case 'google-cloud':
|
|
|
|
$client = new StorageClient([
|
|
|
|
'projectId' => $config['storage']['project_id'],
|
|
|
|
'keyFilePath' => $config['storage']['key_path'],
|
|
|
|
]);
|
|
|
|
return new Filesystem(new GoogleStorageAdapter($client, $client->bucket($config['storage']['bucket'])));
|
|
|
|
default:
|
|
|
|
throw new InvalidArgumentException('The driver specified is not supported.');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-15 15:08:11 +00:00
|
|
|
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) {
|
2019-05-19 13:39:42 +00:00
|
|
|
|
|
|
|
// Check if there is a previous installation, if not, setup the config file
|
2018-11-15 15:08:11 +00:00
|
|
|
$installed = true;
|
|
|
|
if (!file_exists(__DIR__ . '/../config.php')) {
|
|
|
|
$installed = false;
|
|
|
|
|
2019-05-19 13:39:42 +00:00
|
|
|
// config file setup
|
2018-11-15 15:08:11 +00:00
|
|
|
$config['base_url'] = $request->getParam('base_url');
|
2019-05-19 13:39:42 +00:00
|
|
|
$config['storage']['driver'] = $request->getParam('storage_driver');
|
2019-02-07 16:34:19 +00:00
|
|
|
unset($config['displayErrorDetails']);
|
2018-11-15 15:08:11 +00:00
|
|
|
$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');
|
|
|
|
|
2019-05-19 13:39:42 +00:00
|
|
|
|
|
|
|
// setup storage configuration
|
|
|
|
switch ($config['storage']['driver']) {
|
|
|
|
case 's3':
|
|
|
|
$config['storage']['key'] = $request->getParam('storage_key');
|
|
|
|
$config['storage']['secret'] = $request->getParam('storage_secret');
|
|
|
|
$config['storage']['region'] = $request->getParam('storage_region');
|
|
|
|
$config['storage']['bucket'] = $request->getParam('storage_bucket');
|
|
|
|
$config['storage']['path'] = $request->getParam('storage_path');
|
|
|
|
break;
|
|
|
|
case 'dropbox':
|
|
|
|
$config['storage']['token'] = $request->getParam('storage_token');
|
|
|
|
break;
|
|
|
|
case 'ftp':
|
|
|
|
$config['storage']['host'] = $request->getParam('storage_host');
|
|
|
|
$config['storage']['username'] = $request->getParam('storage_username');
|
|
|
|
$config['storage']['password'] = $request->getParam('storage_password');
|
|
|
|
$config['storage']['port'] = $request->getParam('storage_port');
|
|
|
|
$config['storage']['path'] = $request->getParam('storage_path');
|
2019-05-23 19:24:04 +00:00
|
|
|
$config['storage']['passive'] = $request->getParam('storage_passive') === '1';
|
|
|
|
$config['storage']['ssl'] = $request->getParam('storage_ssl') === '1';
|
2019-05-19 13:39:42 +00:00
|
|
|
break;
|
|
|
|
case 'google-cloud':
|
|
|
|
$config['storage']['project_id'] = $request->getParam('storage_project_id');
|
|
|
|
$config['storage']['key_path'] = $request->getParam('storage_key_path');
|
|
|
|
$config['storage']['bucket'] = $request->getParam('storage_bucket');
|
|
|
|
break;
|
|
|
|
case 'local':
|
|
|
|
default:
|
|
|
|
$config['storage']['path'] = $request->getParam('storage_path');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the storage is valid
|
2018-12-09 13:42:50 +00:00
|
|
|
try {
|
2019-05-23 19:24:04 +00:00
|
|
|
$success = $this->storage->write('storage_test.xbackbone.txt', 'XBACKBONE_TEST_FILE');
|
2019-05-19 13:39:42 +00:00
|
|
|
if (!$success) {
|
|
|
|
throw new Exception('The storage is not writable.');
|
2018-12-09 13:42:50 +00:00
|
|
|
}
|
2019-05-23 19:24:04 +00:00
|
|
|
$this->storage->readAndDelete('test.install.txt');
|
2019-05-19 13:39:42 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->session->alert("Storage setup error: {$e->getMessage()} [{$e->getTraceAsString()}]", 'danger');
|
2019-05-23 19:24:04 +00:00
|
|
|
return redirect($response, $request->getUri());
|
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');
|
2019-05-23 19:24:04 +00:00
|
|
|
return redirect($response, $request->getUri());
|
2018-12-07 09:17:45 +00:00
|
|
|
}
|
2018-11-15 15:08:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 12:56:41 +00:00
|
|
|
// if from older installations with no support of other than local driver
|
|
|
|
// update the config
|
|
|
|
if ($installed && isset($config['storage_dir'])) {
|
|
|
|
$config['storage']['driver'] = 'local';
|
|
|
|
$config['storage']['path'] = $config['storage_dir'];
|
|
|
|
unset($config['storage_dir']);
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:08:11 +00:00
|
|
|
|
2019-05-19 13:39:42 +00:00
|
|
|
// Build the dns string and run the migrations
|
2018-12-06 20:20:50 +00:00
|
|
|
try {
|
2019-05-19 13:39:42 +00:00
|
|
|
|
|
|
|
$dsn = $config['db']['connection'] === 'sqlite' ? __DIR__ . '/../' . $config['db']['dsn'] : $config['db']['dsn'];
|
2018-12-06 20:20:50 +00:00
|
|
|
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');
|
2019-05-23 19:24:04 +00:00
|
|
|
return redirect($response, $request->getUri());
|
2018-12-06 20:20:50 +00:00
|
|
|
}
|
2018-11-15 15:08:11 +00:00
|
|
|
|
2019-05-19 13:39:42 +00:00
|
|
|
// if not installed, create the default admin account
|
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
|
|
|
}
|
|
|
|
|
2019-05-19 13:39:42 +00:00
|
|
|
// post install cleanup
|
2018-11-15 15:08:11 +00:00
|
|
|
cleanDirectory(__DIR__ . '/../resources/cache');
|
|
|
|
cleanDirectory(__DIR__ . '/../resources/sessions');
|
|
|
|
|
2019-08-20 12:56:41 +00:00
|
|
|
removeDirectory(__DIR__ . '/../install');
|
2019-01-31 19:53:49 +00:00
|
|
|
|
2019-05-19 13:39:42 +00:00
|
|
|
// if is upgrading and existing installation, put it out maintenance
|
2019-01-31 19:53:49 +00:00
|
|
|
if ($installed) {
|
2019-02-07 16:34:19 +00:00
|
|
|
unset($config['maintenance']);
|
2019-01-31 19:53:49 +00:00
|
|
|
|
|
|
|
$ret = file_put_contents(__DIR__ . '/../config.php', '<?php' . PHP_EOL . 'return ' . var_export($config, true) . ';');
|
|
|
|
if ($ret === false) {
|
|
|
|
$this->session->alert('The config folder is not writable (' . __DIR__ . '/../config.php' . ')', 'danger');
|
2019-05-23 19:24:04 +00:00
|
|
|
return redirect($response, $request->getUri());
|
2019-01-31 19:53:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-15 15:08:11 +00:00
|
|
|
return $response->withRedirect('../?afterInstall=true');
|
|
|
|
});
|
|
|
|
|
|
|
|
$app->run();
|