From ac863f5c8a4e8dec7dbe0ef6948431d6aa59970e Mon Sep 17 00:00:00 2001
From: billz <billzimmerman@gmail.com>
Date: Sun, 15 Oct 2023 21:29:34 +0200
Subject: [PATCH] Status message when CLI binary not found, fetch provider
 countries, status, user account

---
 app/css/custom.php    |  6 ++++
 includes/provider.php | 72 +++++++++++++++++++++++++++++++++----------
 2 files changed, 62 insertions(+), 16 deletions(-)

diff --git a/app/css/custom.php b/app/css/custom.php
index ff87b3b2..33a59c58 100644
--- a/app/css/custom.php
+++ b/app/css/custom.php
@@ -42,6 +42,12 @@ body {
   background-color: #fff;
 }
 
+.btn-primary.disabled {
+  color: <?php echo $color; ?> !important;
+  border-color: <?php echo $color; ?> !important;
+  background-color: #fff !important;
+}
+
 .card-footer, .modal-footer {
   background-color: #f2f1f0;
 }
diff --git a/includes/provider.php b/includes/provider.php
index ee2b8008..f178810e 100755
--- a/includes/provider.php
+++ b/includes/provider.php
@@ -1,11 +1,8 @@
 <?php
 
 require_once 'includes/config.php';
-require_once 'includes/wifi_functions.php';
 
-getWifiInterface();
-
-/**
+/*
  * Manage VPN provider configuration
  */
 function DisplayProviderConfig()
@@ -13,6 +10,38 @@ function DisplayProviderConfig()
     $status = new \RaspAP\Messages\StatusMessage;
     $providerName = getProviderValue($_SESSION["providerID"], "name");
     $binPath = getProviderValue($_SESSION["providerID"], "bin_path");
+    $public_ip = get_public_ip();
+
+    if (!file_exists($binPath)) {
+        $installPage = getProviderValue($_SESSION["providerID"], "install_page");
+        $status->addMessage('Expected '.$providerName.' binary not found at: '.$binPath, 'warning');
+        $status->addMessage('Visit the <a href="'.$installPage.'" target="_blank">installation instructions</a> for '.$providerName.'\'s Linux CLI.', 'warning');
+        $ctlState = 'disabled';
+        $providerVersion = 'not found';
+    } else {
+        // fetch provider status
+        $output = shell_exec("sudo $binPath status");
+        $serviceStatus = strtolower($output) == 0 ? "inactive" : "active";
+        $result = strtolower(($lastSpacePos = strrpos($output, ' ')) ? substr($output, $lastSpacePos + 1) : $output);
+        $providerLog = stripArtifacts($output);
+        //echo '<br>status = '.$result;
+
+        // fetch provider version
+        $providerVersion = shell_exec("sudo $binPath -v");
+
+        // fetch account info
+        exec("sudo $binPath account", $output);
+        $accountInfo = stripArtifacts($output);
+
+        // fetch available countries
+        $output = shell_exec("sudo $binPath countries");
+        $output = stripArtifacts($output, '\s');
+        $arrTmp = explode(",", $output);
+        $countries = array_combine($arrTmp, $arrTmp);
+        foreach ($countries as $key => $value) {
+            $countries[$key] = str_replace("_", " ", $value);
+        }
+    }
 
     if (!RASPI_MONITOR_ENABLED) {
         if (isset($_POST['SaveProviderSettings'])) {
@@ -21,34 +50,31 @@ function DisplayProviderConfig()
             }
             $return = SaveProviderConfig($status, $someVar);
         } elseif (isset($_POST['StartProviderVPN'])) {
-            $status->addMessage('Attempting to connect provider VPN', 'info');
-            exec('sudo '.$binPath.' connect', $return);
+            $status->addMessage('Attempting to connect VPN provider', 'info');
+            exec("sudo $binPath connect", $return);
             foreach ($return as $line) {
                 $status->addMessage($line, 'info');
             }
         } elseif (isset($_POST['StopProviderVPN'])) {
-            $status->addMessage('Attempting to disconnect provider VPN', 'info');
-            exec('sudo '.$binPath.' disconnect', $return);
+            $status->addMessage('Attempting to disconnect VPN provider', 'info');
+            exec("sudo $binPath disconnect", $return);
             foreach ($return as $line) {
                 $status->addMessage($line, 'info');
             }
         }
     }
 
-    exec("sudo $binPath status", $result);
-    $serviceStatus = strtolower($output[1]) == 0 ? "disconnected" : "connected";
-    $public_ip = get_public_ip();
-
-    exec("sudo $binPath status > /tmp/provider.log");
-    $providerLog = file_get_contents('/tmp/provider.log');
-
     echo renderTemplate(
         "provider", compact(
             "status",
             "serviceStatus",
             "providerName",
+            "providerVersion",
+            "accountInfo",
+            "countries",
             "providerLog",
-            "public_ip"
+            "public_ip",
+            "ctlState"
         )
     );
 }
@@ -63,3 +89,17 @@ function SaveProviderConfig($status, $someVar)
 {
 
 }
+
+/**
+ * Removes artifacts from shell_exec string values
+ *
+ * @param string $output
+ * @param string $pattern
+ * @return string $result
+ */
+function stripArtifacts($output, $pattern = null)
+{
+    $result = preg_replace('/[-\/\n\t\\\\'.$pattern.'|]/', '', $output);
+    return $result;
+}
+