From 55480ed0036917ee8d92080d0d49950222737d18 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@freedombone.net>
Date: Tue, 31 Jul 2018 19:17:15 +0100
Subject: [PATCH] Add confirm screen for initial setup via web admin

---
 src/freedombone-installer               |   5 +
 src/freedombone-utils-webadmin          |   2 +
 webadmin/EN/setup_confirm_template.html | 137 ++++++++++++++++++++++++
 webadmin/setup.php                      |   4 +-
 webadmin/setupconfirm.php               |  24 +++++
 5 files changed, 170 insertions(+), 2 deletions(-)
 create mode 100644 webadmin/EN/setup_confirm_template.html
 create mode 100644 webadmin/setupconfirm.php

diff --git a/src/freedombone-installer b/src/freedombone-installer
index 4d922223b..9255756c5 100755
--- a/src/freedombone-installer
+++ b/src/freedombone-installer
@@ -112,6 +112,11 @@ do
                     cp "$webadmin_install_dir/index.prev" "$webadmin_install_dir/setup_installing.html"
                 fi
 
+                # Replace the setup confirm screen with the main index
+                if [ -f "$webadmin_install_dir/setup_confirm.html" ]; then
+                    cp "$webadmin_install_dir/index.prev" "$webadmin_install_dir/setup_confirm.html"
+                fi
+
                 # set permissions for web admin site at freedombone.local
                 chown www-data:www-data "$webadmin_install_dir/index.html"
 
diff --git a/src/freedombone-utils-webadmin b/src/freedombone-utils-webadmin
index df95a25e6..ceb1ad906 100755
--- a/src/freedombone-utils-webadmin
+++ b/src/freedombone-utils-webadmin
@@ -539,7 +539,9 @@ function web_admin_setup_login {
         # and copy it to the index
         cp "$webadmin_install_dir/index.html" "$webadmin_install_dir/index.prev"
         cp "$webadmin_install_dir/setup.html" "$webadmin_install_dir/setup.prev"
+        cp "$webadmin_install_dir/setup_confirm_template.html" "$webadmin_install_dir/setup_confirm.html"
         sed -i "s|WEBADMINPASSWORD|${webadmin_password}|g" "$webadmin_install_dir/setup.prev"
+        sed -i "s|WEBADMINPASSWORD|${webadmin_password}|g" "$webadmin_install_dir/setup_confirm.html"
         cp "$webadmin_install_dir/setup.prev" "$webadmin_install_dir/index.html"
 
         # if initial setup has not yet happened then create
diff --git a/webadmin/EN/setup_confirm_template.html b/webadmin/EN/setup_confirm_template.html
new file mode 100644
index 000000000..70f4b750d
--- /dev/null
+++ b/webadmin/EN/setup_confirm_template.html
@@ -0,0 +1,137 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <meta http-equiv="Refresh" content="30">
+    <style>
+      #headerpic {
+          width: 60%;
+          height: auto;
+          margin-right : auto;
+          margin-left : auto;
+          min-width : 220px;
+      }
+
+      #welcomeicon {
+          width: 20%;
+          height: auto;
+          margin-right : auto;
+          margin-left : auto;
+          min-width : 100px;
+      }
+
+      .header {
+          text-align: center;
+          padding: 32px;
+      }
+
+      #iconpic {
+          width: 20%;
+          height: auto;
+          margin-right : auto;
+          margin-left : auto;
+          min-width : 120px;
+      }
+
+      .card {
+          box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
+          max-width: 600px;
+          margin: auto;
+          text-align: center;
+          font-family: arial;
+          clear: both;
+      }
+
+      .card input[type=text] {
+          width: 90%;
+          clear: both;
+          text-align: center;
+      }
+
+      .appurl {
+          color: grey;
+          font-size: 100%;
+      }
+
+      .welcomeheader {
+          color: black;
+          font-size: 200%;
+          font-weight: bold;
+      }
+
+      .welcometext {
+          color: black;
+          font-size: 90%;
+      }
+
+      .logintext {
+          color: black;
+          font-size: 120%;
+          font-weight: bold;
+          color: #981737;
+      }
+
+      button {
+          border: none;
+          outline: 0;
+          display: inline-block;
+          padding: 8px;
+          color: white;
+          background-color: #000;
+          text-align: center;
+          cursor: pointer;
+          width: 100%;
+          font-size: 18px;
+      }
+
+      a {
+          text-decoration: none;
+          color: black;
+      }
+
+      button:hover, a:hover {
+          opacity: 0.7;
+      }
+
+      .chip {
+          display: inline-block;
+          padding: 0 25px;
+          height: 50px;
+          font-size: 70%;
+          line-height: 50px;
+          border-radius: 25px;
+          background-color: #f1f1f1;
+      }
+
+      .chip img {
+          float: left;
+          margin: 0 10px 0 -25px;
+          height: 50px;
+          width: 50px;
+          border-radius: 50%;
+      }
+    </style>
+  </head>
+  <body>
+    <div class="card">
+      <div class="header">
+        <img id="headerpic" class="img-responsive" src="images/logo.png"><br>
+        <img id="welcomeicon" class="img-responsive" src="images/login.png">
+        <p class="welcometext">Have you written down or saved the login credentials?</p>
+
+        <p class="logintext">Username: admin<br>
+          Password: WEBADMINPASSWORD
+        </p>
+
+        <br>
+
+        <form action="setupconfirm.php" method="post">
+          <p><input type="radio" name="setupconfirm" value="0" checked>No
+            <input type="radio" name="setupconfirm" value="1">Yes</p><br><br>
+          <input type="submit" name="setupconfirmsubmit" value="Continue" />
+        </form>
+
+        <br>
+      </div>
+    </div>
+  </body>
+</html>
diff --git a/webadmin/setup.php b/webadmin/setup.php
index 8facfcf45..960a03465 100755
--- a/webadmin/setup.php
+++ b/webadmin/setup.php
@@ -4,8 +4,8 @@ if (isset($_POST['setup'])) {
     $my_username = htmlspecialchars($_POST['my_username']);
     $default_domain_name = htmlspecialchars($_POST['default_domain_name']);
 
-    if(!file_exists("setup.txt")) {
-        $setup_file = fopen("setup.txt", "w") or die("Unable to create setup file");
+    if(!file_exists(".temp_setup.txt")) {
+        $setup_file = fopen(".temp_setup.txt", "w") or die("Unable to create setup file");
         fwrite($setup_file, $my_username.",".$default_domain_name."\n");
         fclose($);
     }
diff --git a/webadmin/setupconfirm.php b/webadmin/setupconfirm.php
new file mode 100644
index 000000000..168f49145
--- /dev/null
+++ b/webadmin/setupconfirm.php
@@ -0,0 +1,24 @@
+<?php
+
+$output_filename = "index.html";
+
+if (isset($_POST['setupconfirmsubmit'])) {
+    if(isset($_POST['setupconfirm'])) {
+        $confirm = htmlspecialchars($_POST['setupconfirm']);
+
+        if($confirm == "1") {
+            if(file_exists(".temp_setup.txt")) {
+                exec('mv .temp_setup.txt setup.txt');
+            }
+            if(file_exists("setup.txt")) {
+                $output_filename = "setup_installing.html";
+            }
+        }
+    }
+}
+
+$htmlfile = fopen("$output_filename", "r") or die("Unable to open $output_filename");
+echo fread($htmlfile,filesize("$output_filename"));
+fclose($htmlfile);
+
+?>
-- 
GitLab