From e4f79f17bca4d3cd29ff486e98e615ce39d93003 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@freedombone.net>
Date: Fri, 10 Aug 2018 11:31:12 +0100
Subject: [PATCH] Conformation when installing an app on webadmin

---
 src/freedombone-utils-webadmin            |   8 ++
 webadmin/EN/add_app_confirm_template.html | 128 ++++++++++++++++++++++
 webadmin/EN/app_add_template.html         |   2 +-
 webadmin/installapp.php                   |  37 ++-----
 webadmin/installappconfirm.php            |  74 +++++++++++++
 5 files changed, 218 insertions(+), 31 deletions(-)
 create mode 100644 webadmin/EN/add_app_confirm_template.html
 create mode 100755 webadmin/installappconfirm.php

diff --git a/src/freedombone-utils-webadmin b/src/freedombone-utils-webadmin
index 0f5e448fb..14b4f080f 100755
--- a/src/freedombone-utils-webadmin
+++ b/src/freedombone-utils-webadmin
@@ -509,10 +509,18 @@ function web_admin_onion_only {
     # In onion only mode domain names or ddns codes
     # don't need to be provided
     web_admin_get_hostname
+
     # shellcheck disable=SC2154
     sed -i '/freedns_code/d' "$rootdir$webadmin_install_dir/app_add_template.html"
+    # shellcheck disable=SC2154
+    sed -i '/freedns_code/d' "$rootdir$webadmin_install_dir/app_add_confirm_template.html"
+
     sed -i '/install_domain/d' "$rootdir$webadmin_install_dir/app_add_template.html"
+    sed -i '/install_domain/d' "$rootdir$webadmin_install_dir/app_add_confirm_template.html"
+
     sed -i 's|onion_only=false;|onion_only=true;|g' "$rootdir$webadmin_install_dir/installapp.php"
+    sed -i 's|onion_only=false;|onion_only=true;|g' "$rootdir$webadmin_install_dir/installappconfirm.php"
+
     sed -i 's|setup_domain.html|setup_installing.html|g' "$rootdir$webadmin_install_dir/setupconfirm.php"
 }
 
diff --git a/webadmin/EN/add_app_confirm_template.html b/webadmin/EN/add_app_confirm_template.html
new file mode 100644
index 000000000..f893b6952
--- /dev/null
+++ b/webadmin/EN/add_app_confirm_template.html
@@ -0,0 +1,128 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <style>
+      #headerpic {
+          width: 60%;
+          height: auto;
+          margin-right : auto;
+          margin-left : auto;
+          min-width : 220px;
+      }
+
+      .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;
+      }
+
+      .proceedtext {
+          color: black;
+          font-size: 120%;
+      }
+
+      .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>
+        <br>
+        <p class="proceedtext">Install APPNAME now?</p>
+        <br>
+
+        <form action="installappconfirm.php" method="post">
+          <p>
+            <input type="radio" name="installconfirm" value="0" checked>No
+            <input type="radio" name="installconfirm" value="1">Yes
+            <input type="hidden" name="app_name" value="APPNAME">
+            <input type="hidden" name="install_domain" value="APPDOMAIN">
+            <input type="hidden" name="freedns_code" value="APPCODE">
+          </p>
+          <br><br>
+          <input type="submit" name="installconfirmsubmit" value="Continue" />
+        </form>
+        <br>
+      </div>
+    </div>
+  </body>
+</html>
diff --git a/webadmin/EN/app_add_template.html b/webadmin/EN/app_add_template.html
index 90c6cb2d5..ba2b7e061 100644
--- a/webadmin/EN/app_add_template.html
+++ b/webadmin/EN/app_add_template.html
@@ -100,7 +100,7 @@
       <h1>APPNAME</h1>
       <p class="appdesc">APPDESCRIPTION</p>
 
-      <form action="installapp.php" method="post">
+      <form action="installappconfirm.php" method="post">
         <p><label>Domain:</label><input type="text" name="install_domain" value="APPNAME.HOSTNAME"></p>
         <p><label><a href="https://freedns.afraid.org/dynamic">FreeDNS code</a>:</label><input type="text" name="freedns_code"></p>
         <input type="hidden" name="app_name" value="APPNAME">
diff --git a/webadmin/installapp.php b/webadmin/installapp.php
index 4b4e40659..3cc5143df 100755
--- a/webadmin/installapp.php
+++ b/webadmin/installapp.php
@@ -22,36 +22,13 @@ if (isset($_POST['install'])) {
     }
 
     if($continue_install) {
-        if(file_exists("pending_removes.txt")) {
-            // Is this app in the pending_removes list?
-            if(exec('grep '.escapeshellarg("remove_".$app_name).' ./pending_removes.txt')) {
-                if(! exec('grep '.escapeshellarg("remove_".$app_name).'_running ./pending_removes.txt')) {
-                    // Not Removing yet so remove from schedule
-                    exec('sed -i "/'.escapeshellarg("remove_".$app_name).'/d ./pending_removes.txt');
-                }
-                else {
-                    // Removing so don't continue
-                    $continue_install=false;
-                }
-            }
-        }
-    }
-
-    if($continue_install) {
-        if(! file_exists("pending_installs.txt")) {
-            $pending_installs = fopen("pending_installs.txt", "w") or die("Unable to create installs file");
-            fclose($pending_installs);
-        }
-
-        if(! exec('grep '.escapeshellarg("install_".$app_name).' ./pending_installs.txt')) {
-            $pending_installs = fopen("pending_installs.txt", "a") or die("Unable to append to installs file");
-            fwrite($pending_installs, "install_".$app_name.",".$install_domain.",".$freedns_code."\n");
-            fclose($pending_installs);
-            $output_filename = "app_installing.html";
-        }
-        else {
-            // The app is already scheduled for installation
-            $output_filename = "app_scheduled.html";
+        // create the confirm screen populated with details for the app
+        exec('cp add_app_confirm_template.html add_app_confirm.html');
+        if(file_exists("add_app_confirm.html")) {
+            exec('sed -i "s|APPNAME|'.$app_name.'|g" add_app_confirm.html');
+            exec('sed -i "s|APPDOMAIN|'.$install_domain.'|g" add_app_confirm.html');
+            exec('sed -i "s|APPCODE|'.$freedns_code.'|g" add_app_confirm.html');
+            $output_filename = "add_app_confirm.html";
         }
     }
 }
diff --git a/webadmin/installappconfirm.php b/webadmin/installappconfirm.php
new file mode 100755
index 000000000..a7fd82904
--- /dev/null
+++ b/webadmin/installappconfirm.php
@@ -0,0 +1,74 @@
+<?php
+
+$output_filename = "apps_add.html";
+
+if (isset($_POST['installconfirmsubmit'])) {
+    if(isset($_POST['installconfirm'])) {
+        $confirm = htmlspecialchars($_POST['installconfirm']);
+
+        if($confirm == "1") {
+            $app_name = htmlspecialchars($_POST['app_name']);
+            $install_domain = '';
+            $freedns_code = '';
+
+            // Note that this value can be changed by install_web_admin
+            $onion_only=false;
+
+            $continue_install=true;
+
+            if(! $onion_only) {
+                $install_domain = $_POST['install_domain'];
+                if (!strpos($install_domain, '.')) {
+                    // No domain was provided
+                    $continue_install=false;
+                }
+                $freedns_code = $_POST['freedns_code'];
+            }
+
+            if($continue_install) {
+                if(file_exists("pending_removes.txt")) {
+                    // Is this app in the pending_removes list?
+                    if(exec('grep '.escapeshellarg("remove_".$app_name).' ./pending_removes.txt')) {
+                        if(! exec('grep '.escapeshellarg("remove_".$app_name).'_running ./pending_removes.txt')) {
+                            // Not Removing yet so remove from schedule
+                            exec('sed -i "/'.escapeshellarg("remove_".$app_name).'/d ./pending_removes.txt');
+                        }
+                        else {
+                            // Removing so don't continue
+                            $continue_install=false;
+                        }
+                    }
+                }
+            }
+
+            if($continue_install) {
+                if(! file_exists("pending_installs.txt")) {
+                    $pending_installs = fopen("pending_installs.txt", "w") or die("Unable to create installs file");
+                    fclose($pending_installs);
+                }
+
+                if(! exec('grep '.escapeshellarg("install_".$app_name).' ./pending_installs.txt')) {
+                    $pending_installs = fopen("pending_installs.txt", "a") or die("Unable to append to installs file");
+                    fwrite($pending_installs, "install_".$app_name.",".$install_domain.",".$freedns_code."\n");
+                    fclose($pending_installs);
+                    $output_filename = "app_installing.html";
+                }
+                else {
+                    // The app is already scheduled for installation
+                    $output_filename = "app_scheduled.html";
+                }
+            }
+        }
+    }
+}
+
+$htmlfile = fopen("$output_filename", "r") or die("Unable to open $output_filename");
+echo fread($htmlfile,filesize("$output_filename"));
+fclose($htmlfile);
+
+// remove confirm screen
+if(file_exists("add_app_confirm.html")) {
+    exec('rm add_app_confirm.html');
+}
+
+?>
-- 
GitLab