From 1dc0438a1872de09a6c40abc8486f95d083b68c3 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 11 May 2019 12:45:02 +0100 Subject: [PATCH] Work more on the first-run installer, but it's not finished yet --- modules/feature-firstrun.php | 67 +++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/modules/feature-firstrun.php b/modules/feature-firstrun.php index d44de5f..89bd5d8 100644 --- a/modules/feature-firstrun.php +++ b/modules/feature-firstrun.php @@ -12,8 +12,10 @@ register_module([ return true; // Stop this module from actually being executed - it's not ready yet! // TODO: Figure out how to detect pre-existing wikis here - // Perhaps this could be a setting instead? We'd need to update the settings logic a bit - $firstrun_complete = file_exists("._peppermint_installed"); + + if(!$firstrun_complete && count(glob("._peppermint_secret_*")) == 0) { + + } /** * @api {get} ?action=firstrun Display the firstrun page @@ -30,14 +32,20 @@ register_module([ * ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ * ██ ██ ██ ██ ███████ ██ ██ ██ ██████ ██ ████ */ - add_action("firstrun", function() use($firstrun_complete) { + add_action("firstrun", function() { global $settings; - if($firstrun_complete) { + if($settings->firstrun_complete) { http_response_code(400); exit(page_renderer::render_main("Setup complete - Error - $settings->sitename", "

Oops! Looks like $settings->sitename is already setup and ready to go! Go to the " . htmlentities($settings->defaultpage)." to get started!

")); } + if(!module_exists("page-login")) { + http_response_code(503); + exit(page_renderer::render_main("Build error - Pepperminty Wiki", "

The page-login wasn't included in this build of Pepperminty Wiki, so the first-run installation wizard will not work correctly.

+

You can still complete the setup manually, however! Once done, set firstrun_complete in peppermint.json to true.

")); + } + // TODO: Check the environment here first // - Make sure peppermint.json isn't accessible // - Check for required modules? @@ -54,6 +62,9 @@ register_module([
+ + +

Longer is better! Aim for at least 14 characters.

@@ -74,41 +85,71 @@ register_module([ "; + + exit(page_renderer::render_main("Welcome! - Pepperminty Wiki", $result)); }); add_action("firstrun-complete", function() { - global $version, $commit; + global $version, $commit, $settings; - if($firstrun_complete) { + if($settings->firstrun_complete) { http_response_code(400); exit(page_renderer::render_main("Setup complete - Error - $settings->sitename", "

Oops! Looks like $settings->sitename is already setup and ready to go! Go to the " . htmlentities($settings->defaultpage)." to get started!

")); } - // $_POST: username, password, password-again, wiki-name, data-dir + // $_POST: username, email-address, password, password-again, wiki-name, data-dir if(empty($_POST["username"])) { - http_response_code(422); + http_response_code(400); exit(page_renderer::render_main("Missing information - Error - Pepperminty Wiki", "

Oops! Looks like you forgot to enter a username. Try going back in your browser and filling one in.

")); } + if(empty($_POST["email-address"])) { + http_response_code(400); + exit(page_renderer::render_main("Missing information - Error - Pepperminty Wiki", "

Oops! Looks like you forgot to enter an email address. Try going back in your browser and filling one in.

")); + } + if(!filter_var($_POST["email-address"], FILTER_VALIDATE_EMAIL)) { + http_response_code(400); + exit(page_renderer::render_main("Invalid email address - Error - Pepperminty Wiki", "

Oops! Looks like that email address isn't valid. Try going back in your browser and correcting it.

")); + } if(empty($_POST["password"]) || empty($_POST["password-again"])) { - http_response_code(422); + http_response_code(400); exit(page_renderer::render_main("Missing information - Error - Pepperminty Wiki", "

Oops! Looks like you forgot to enter a password. Try going back in your browser and filling one in.

")); } - if(empty($_POST["wiki-name"])) { + if($_POST["password"] !== $_POST["password-again"]) { http_response_code(422); + exit(page_renderer::render_main("Password mismatch - Error - Pepperminty Wiki", "

Oops! Looks like the passwords you entered aren't the same. Try going back in your browser and entering it again.

")); + } + if(empty($_POST["wiki-name"])) { + http_response_code(400); exit(page_renderer::render_main("Missing information - Error - Pepperminty Wiki", "

Oops! Looks like you forgot to enter a name for your wiki. Try going back in your browser and filling one in.

")); } if(empty($_POST["data-dir"])) { - http_response_code(422); + http_response_code(400); exit(page_renderer::render_main("Missing information - Error - Pepperminty Wiki", "

Oops! Looks like you forgot to enter a directory on the server to store the wiki's data in. Try going back in your browser and filling one in. Relative paths are ok - the default is . (i.e. the current directory).

")); } + // Generate the user data object & replace the pre-generated users + $user_data = new stdClass(); + $user_data->password = hash_password($_POST["password"]); + $user_data->emailAddress = $_POST["email-address"]; + $settings->users = new stdClass(); + $settings->users->{$_POST["username"]} = $user_data; + // Apply the settings + $settings->firstrun_complete = true; + $settings->sitename = $_POST["wiki-name"]; + $settings->data_storage_dir = $_POST["data-dir"]; - // ---------------------------------------------------------------- + if(!save_settings()) { + http_response_code(500); + exit(page_renderer::render_main("Server Error - Pepperminty Wiki", "

Oops! Pepperminty Wiki was unable to save your settings back to disk. This can happen if Pepperminty Wiki does not have write permissions on it's own directory and the files contained within (except index.php of course).

+

Try contacting your server owner and ask them to correct it. If you are the server owner, you may need to run sudo chown -R WEBSERVER_USERNAME:WEBSERVER_USERNAME PATH/TO/WIKI/DIRECTORY, replacing the bits in UPPERCASE.

")); + } - file_put_contents("._peppermint_installed", "Install complete at " . date("c") . "with Pepperminty Wiki v$version-$commit"); + http_response_code(201); + exit(page_renderer::render_main("Setup complete! - Pepperminty Wiki", "

Congratulations! You've completed the Pepperminty Wiki setup.

+

Click here to start using $settings->sitename, your new wiki!

")); }); } ]);