From 72523843b69d45b9193331d08c65ab1f54321d36 Mon Sep 17 00:00:00 2001 From: Sergio Brighenti Date: Sun, 11 Nov 2018 19:18:27 +0100 Subject: [PATCH] Working on install wizard --- app/Controllers/DashboardController.php | 4 +- app/Web/Session.php | 14 +-- bootstrap/app.php | 3 + index.php | 2 +- install/index.php | 139 ++++++++++++++++++++++++ install/templates/alert.twig | 8 ++ install/templates/footer.twig | 7 ++ install/templates/install.twig | 124 +++++++++++++++++++++ resources/templates/comp/navbar.twig | 6 +- 9 files changed, 291 insertions(+), 16 deletions(-) create mode 100644 install/index.php create mode 100644 install/templates/alert.twig create mode 100644 install/templates/footer.twig create mode 100644 install/templates/install.twig diff --git a/app/Controllers/DashboardController.php b/app/Controllers/DashboardController.php index d0d3437..d76aed3 100644 --- a/app/Controllers/DashboardController.php +++ b/app/Controllers/DashboardController.php @@ -91,9 +91,7 @@ class DashboardController extends Controller $totalSize += $filesystem->getSize($media->storage_path); } - return $this->view->render( - $response, - 'dashboard/system.twig', [ + return $this->view->render($response, 'dashboard/system.twig', [ 'usersCount' => $usersCount, 'mediasCount' => $mediasCount, 'orphanFilesCount' => $orphanFilesCount, diff --git a/app/Web/Session.php b/app/Web/Session.php index bfaf956..f33962b 100644 --- a/app/Web/Session.php +++ b/app/Web/Session.php @@ -8,13 +8,15 @@ class Session /** * Start a session if is not already started in the current context + * @param string $name + * @param string $path */ - public static function init(): void + public static function init(string $name, $path = ''): void { if (session_status() === PHP_SESSION_NONE) { session_start([ - 'name' => 'xbackbone_session', - 'save_path' => 'resources/sessions' + 'name' => $name, + 'save_path' => $path ]); } } @@ -33,7 +35,6 @@ class Session */ public static function clear(): void { - self::init(); $_SESSION = []; } @@ -44,7 +45,6 @@ class Session */ public static function has($key): bool { - self::init(); return isset($_SESSION[$key]); } @@ -54,7 +54,6 @@ class Session */ public static function all(): array { - self::init(); return $_SESSION; } @@ -66,7 +65,6 @@ class Session */ public static function get($key, $default = null) { - self::init(); return self::has($key) ? $_SESSION[$key] : $default; } @@ -77,7 +75,6 @@ class Session */ public static function set($key, $value): void { - self::init(); $_SESSION[$key] = $value; } @@ -88,7 +85,6 @@ class Session */ public static function alert($message, string $type = 'info'): void { - self::init(); $_SESSION['_flash'] = [$type => $message]; } diff --git a/bootstrap/app.php b/bootstrap/app.php index 28ba294..c9f6a95 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -35,6 +35,8 @@ $container['logger'] = function ($container) { return $logger; }; +// Session init +Session::init('xbackbone_session', 'resources/sessions'); // Set the database dsn DB::setDsn($config['db']['connection'] . ':' . $config['db']['dsn'], $config['db']['username'], $config['db']['password']); @@ -76,6 +78,7 @@ $container['errorHandler'] = function ($container) { return $container->view->render($response->withStatus(500), 'errors/500.twig', ['exception' => $exception]); }; }; + $container['notFoundHandler'] = function ($container) { return function (\Slim\Http\Request $request, \Slim\Http\Response $response) use (&$container) { $response->withStatus(404)->withHeader('Content-Type', 'text/html'); diff --git a/index.php b/index.php index 5dfb172..b382b22 100644 --- a/index.php +++ b/index.php @@ -1,5 +1,5 @@ version); diff --git a/install/index.php b/install/index.php new file mode 100644 index 0000000..5b8737d --- /dev/null +++ b/install/index.php @@ -0,0 +1,139 @@ +version); + +$config = [ + 'app_name' => 'XBackBone', + 'base_url' => isset($_SERVER['HTTPS']) ? 'https://' . $_SERVER['HTTP_HOST'] : 'http://' . $_SERVER['HTTP_HOST'], + 'storage_dir' => 'storage', + 'displayErrorDetails' => false, + 'db' => [ + 'connection' => 'sqlite', + 'dsn' => 'resources/database/xbackbone.db', + 'username' => null, + 'password' => null, + ], +]; + +$container = new Container(['settings' => $config]); + +Session::init('xbackbone_session'); + +$container['view'] = function ($container) use (&$config) { + $view = new \Slim\Views\Twig('templates', [ + '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')); + $view->getEnvironment()->addGlobal('alerts', Session::getAlert()); + $view->getEnvironment()->addGlobal('session', Session::all()); + $view->getEnvironment()->addGlobal('PLATFORM_VERSION', PLATFORM_VERSION); + return $view; +}; + +$app = new App($container); + +$app->get('/', function (Request $request, Response $response) { + return $this->view->render($response, 'install.twig'); +}); + +$app->post('/', function (Request $request, Response $response) use (&$config) { + $config['base_url'] = $request->getParam('base_url'); + $config['storage_dir'] = $request->getParam('storage_dir'); + $config['db']['connection'] = $request->getParam('connection'); + $config['db']['dsn'] = $request->getParam('dsn'); + $config['db']['username'] = null; + $config['db']['password'] = null; + + + file_put_contents(__DIR__ . '/../config.php', '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) . '?'; + + $inMigrationsTable = DB::query("SELECT * FROM `migrations` WHERE `name` IN ($in)", $names)->fetchAll(); + + + foreach ($files as $file) { + + $continue = false; + $exists = false; + + foreach ($inMigrationsTable as $migration) { + if (basename($file) === $migration->name && $migration->migrated) { + $continue = true; + break; + } elseif (basename($file) === $migration->name && !$migration->migrated) { + $exists = true; + break; + } + } + if ($continue) continue; + + $sql = file_get_contents($file); + try { + DB::raw()->exec($sql); + if (!$exists) { + DB::query('INSERT INTO `migrations` VALUES (?,?)', [basename($file), 1]); + } else { + DB::query('UPDATE `migrations` SET `migrated`=? WHERE `name`=?', [1, basename($file)]); + } + } catch (PDOException $exception) { + if (!$exists) { + DB::query('INSERT INTO `migrations` VALUES (?,?)', [basename($file), 0]); + } + throw $exception; + } + } + + DB::query("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)]); + + Session::alert('Installation completed successfully!', 'success'); + return $response->withRedirect('../?afterInstall=true'); +}); + +$app->run(); \ No newline at end of file diff --git a/install/templates/alert.twig b/install/templates/alert.twig new file mode 100644 index 0000000..3a103aa --- /dev/null +++ b/install/templates/alert.twig @@ -0,0 +1,8 @@ +{% for type, message in alerts %} + +{% endfor %} \ No newline at end of file diff --git a/install/templates/footer.twig b/install/templates/footer.twig new file mode 100644 index 0000000..88065c5 --- /dev/null +++ b/install/templates/footer.twig @@ -0,0 +1,7 @@ + \ No newline at end of file diff --git a/install/templates/install.twig b/install/templates/install.twig new file mode 100644 index 0000000..ca6685e --- /dev/null +++ b/install/templates/install.twig @@ -0,0 +1,124 @@ + + + + Installing XBackBone | {{ config.app_name }} + + + + + + + + + + + + + + + + + +
+ {% include 'alert.twig' %} +
+
+
+
Install XBackBone
+
+
+ +
+ +
+ +
+
+
+
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+
+
+ +
+ +
+
+
+
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+
+ +
+
+
+
+
+
+
+
+{% include 'footer.twig' %} + + diff --git a/resources/templates/comp/navbar.twig b/resources/templates/comp/navbar.twig index 8fe6a1b..bcb1ff6 100644 --- a/resources/templates/comp/navbar.twig +++ b/resources/templates/comp/navbar.twig @@ -7,18 +7,18 @@