From a0695d960f0716b825569e578a63a8d6329a3607 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@freedombone.net>
Date: Mon, 30 Jul 2018 12:07:00 +0100
Subject: [PATCH] Don't continue installing if no domain is provided

---
 src/freedombone-installer      |  6 ++---
 src/freedombone-utils-webadmin | 18 ++++++++++++++-
 webadmin/installapp.php        | 41 +++++++++++++++++++++++-----------
 webadmin/removeapp.php         |  2 +-
 4 files changed, 49 insertions(+), 18 deletions(-)

diff --git a/src/freedombone-installer b/src/freedombone-installer
index fee63fd02..8ac89cb53 100755
--- a/src/freedombone-installer
+++ b/src/freedombone-installer
@@ -46,11 +46,11 @@ do
         if [ -f "$pending_installs" ]; then
             linestr=$(head -n 1 "$pending_installs")
             if [[ "$linestr" == "install_"* ]]; then
-                app_name=$(echo "$linestr" | awk -F '_' '{print $2}' | awk -F ' ' '{print $1}')
+                app_name=$(echo "$linestr" | awk -F '_' '{print $2}' | awk -F ',' '{print $1}')
                 if [ -f "/usr/share/${PROJECT_NAME}/apps/${PROJECT_NAME}-app-${app_name}" ]; then
-                    app_domain=$(echo "$linestr" | awk -F ' ' '{print $2}')
+                    app_domain=$(echo "$linestr" | awk -F ',' '{print $2}')
                     app_name_upper=$(echo "$app_name" | awk '{print toupper($0)}')
-                    freedns_code=$(echo "$linestr" | awk -F ' ' '{print $3}')
+                    freedns_code=$(echo "$linestr" | awk -F ',' '{print $3}')
 
                     # indicate that we are installing
                     if [[ "$linestr" != *'_running'* ]]; then
diff --git a/src/freedombone-utils-webadmin b/src/freedombone-utils-webadmin
index 3733c5781..48009aaa8 100755
--- a/src/freedombone-utils-webadmin
+++ b/src/freedombone-utils-webadmin
@@ -253,7 +253,9 @@ function web_admin_create_add_apps {
 
                 # remove freedns if necessary
                 if [[ "$DDNS_PROVIDER" != *"freedns"* ]]; then
-                    sed -i '/freedns_code/d' "$filename"
+                    if grep -q 'freedns_code' "$filename"; then
+                        sed -i '/freedns_code/d' "$filename"
+                    fi
                 fi
 
                 available_apps_ctr=$((available_apps_ctr+1))
@@ -429,6 +431,14 @@ function web_admin_get_language_subdir {
     echo "$lang_lower" | awk '{print toupper($0)}'
 }
 
+function web_admin_onion_only {
+    # In onion only mode domain names or ddns codes
+    # don't need to be provided
+    sed -i '/freedns_code/d' "/var/www/${local_hostname}/htdocs/admin/app_add_template.html"
+    sed -i '/install_domain/d' "/var/www/${local_hostname}/htdocs/admin/app_add_template.html"
+    sed -i 's|onion_only=false;|onion_only=true;|g' "/var/www/${local_hostname}/htdocs/admin/installapp.php"
+}
+
 function install_web_admin {
     # This is intended as an admin web user interface
     # similar to Plinth or the yunohost
@@ -446,6 +456,12 @@ function install_web_admin {
         if [ -d "/usr/share/${PROJECT_NAME}/webadmin/${language_subdir}" ]; then
             cp -r "/usr/share/${PROJECT_NAME}/webadmin"/* "/var/www/${local_hostname}/htdocs/admin"
             cp "/usr/share/${PROJECT_NAME}/webadmin/${language_subdir}"/*.html "/var/www/${local_hostname}/htdocs/admin"
+
+            read_config_param ONION_ONLY
+            if [[ "$ONION_ONLY" != 'no' ]]; then
+                web_admin_onion_only
+            fi
+
             web_admin_installed=1
         else
             echo $"No web admin language subdirectory for ${language_subdir}"
diff --git a/webadmin/installapp.php b/webadmin/installapp.php
index 33ecc01e0..4b4e40659 100755
--- a/webadmin/installapp.php
+++ b/webadmin/installapp.php
@@ -4,20 +4,35 @@ $output_filename = "apps_add.html";
 
 if (isset($_POST['install'])) {
     $app_name = htmlspecialchars($_POST['app_name']);
-    $install_domain = $_POST['install_domain'];
-    $freedns_code = $_POST['freedns_code'];
+    $install_domain = '';
+    $freedns_code = '';
+
+    // Note that this value can be changed by install_web_admin
+    $onion_only=false;
 
     $continue_install=true;
-    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');
-            }
-            else {
-                // Removing so don't continue
-                $continue_install=false;
+
+    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;
+                }
             }
         }
     }
@@ -30,7 +45,7 @@ if (isset($_POST['install'])) {
 
         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");
+            fwrite($pending_installs, "install_".$app_name.",".$install_domain.",".$freedns_code."\n");
             fclose($pending_installs);
             $output_filename = "app_installing.html";
         }
diff --git a/webadmin/removeapp.php b/webadmin/removeapp.php
index 61a068f99..bc1b50078 100755
--- a/webadmin/removeapp.php
+++ b/webadmin/removeapp.php
@@ -11,7 +11,7 @@ if (isset($_POST['uninstall'])) {
         if(exec('grep '.escapeshellarg("install_".$app_name).' ./pending_installs.txt')) {
             if(! exec('grep '.escapeshellarg("install_".$app_name).'_running ./pending_installs.txt')) {
                 // Not installing yet so remove from schedule
-                exec('sed -i "/'.escapeshellarg("install_".$app_name).'/d');
+                exec('sed -i "/'.escapeshellarg("install_".$app_name).'/d ./pending_installs.txt');
             }
             else {
                 // Installing so don't continue
-- 
GitLab