From 9ba459325ff71eed096e745b881b7294a0383936 Mon Sep 17 00:00:00 2001
From: Alexey Berezhok <a@bayrepo.ru>
Date: Tue, 27 Aug 2024 16:08:31 +0300
Subject: [PATCH] Added config of php path

---
 hestiacp-php-selector.spec            |  3 +++
 installer.sh                          | 27 ++++++++++++++++++++++-----
 src/converter/hestiacp-php-set-ver.go | 21 +++++++++++++++++++--
 3 files changed, 44 insertions(+), 7 deletions(-)

diff --git a/hestiacp-php-selector.spec b/hestiacp-php-selector.spec
index 463c36b..4dfc4eb 100644
--- a/hestiacp-php-selector.spec
+++ b/hestiacp-php-selector.spec
@@ -29,6 +29,7 @@ make DESTDIR=$RPM_BUILD_ROOT install
 touch "$RPM_BUILD_ROOT"%{_sysconfdir}/hestia_php_selector/system/lock
 %{__mkdir} -p "$RPM_BUILD_ROOT"/usr/local/hestia_php_selector/
 %{__install} -D -m 755 installer.sh "$RPM_BUILD_ROOT"/usr/local/hestia_php_selector/hestiacp_php_selector_installer
+echo "/usr/bin/php%s" > "$RPM_BUILD_ROOT"%{_sysconfdir}/hestia_php_selector/php_sel_path.conf
 
 %check
 make check
@@ -50,6 +51,8 @@ fi
 %files
 %defattr(-,root,root)
 %doc LICENSE
+%config(noreplace) %{_sysconfdir}/hestia_php_selector/php_sel_path.conf
+%attr(0644, root, root) %{_sysconfdir}/hestia_php_selector/php_sel_path.conf
 %attr(0755, root, root) %{_bindir}/hestiacp-php-selector
 %attr(0755, root, root) %{_bindir}/hestiacp-php-admin
 %{_sysconfdir}/hestia_php_selector/*
diff --git a/installer.sh b/installer.sh
index fb796db..fbf6714 100644
--- a/installer.sh
+++ b/installer.sh
@@ -22,14 +22,31 @@ install)
     fi
 ;;
 delete)
-    if [ -e /etc/hestia_php_selector/system/php.path ]; then
-        php_sys=$(cat /etc/hestia_php_selector/system/php.path)
-        if [ -n "$php_sys" ]; then
-            update-alternatives --set php "$php_sys"
+    current_php=$(readlink -f /usr/bin/php)
+    if [ "$current_php" == "/usr/bin/hestiacp-php-selector" ]; then
+        if [ -e /etc/hestia_php_selector/system/php.path ]; then
+            php_sys=$(cat /etc/hestia_php_selector/system/php.path)
+            if [ -n "$php_sys" ]; then
+                update-alternatives --display php | grep "/usr/bin/php82"
+                if [ $? -eq 0 ]; then
+                    update-alternatives --set php "$php_sys"
+                else
+                    php_ver=$(echo "$php_sys" | grep -o -P "php\d+.?\d+")
+                    is_sim_php=`update-alternatives --display php | tail -n +3 | grep -Po "^(.+?)(?= -)" | grep "$php_ver" | head -n1`
+                    fst_php=`update-alternatives --display php | tail -n +3 | grep -Po "^(.+?)(?= -)" | head -n1`
+                    if [ -n "$is_sim_php" ]; then
+                        update-alternatives --set php "$is_sim_php"
+                    else
+                        if [ -n "$fst_php" ]; then
+                            update-alternatives --set php "$fst_php"
+                        fi
+                    fi
+                fi
+            fi
         fi
     fi
     update-alternatives --display php | grep hestiacp-php-selector
-    if [ $? -ne 0 ]; then
+    if [ $? -eq 0 ]; then
         update-alternatives --remove php /usr/bin/hestiacp-php-selector
     fi
     /usr/bin/hestiacp-php-admin remove-all
diff --git a/src/converter/hestiacp-php-set-ver.go b/src/converter/hestiacp-php-set-ver.go
index d7c6f5e..6e3fe1c 100644
--- a/src/converter/hestiacp-php-set-ver.go
+++ b/src/converter/hestiacp-php-set-ver.go
@@ -25,6 +25,8 @@ const (
 	PATH_TO_LOCAL_PHP_ROOT   = "php_sel"
 	PATH_TO_LOCAL_PHP        = PATH_TO_LOCAL_PHP_ROOT + PATH_TO_CONFIG_NAME
 	LOCK_PATH                = "lock"
+	PHP_SEL_CONF             = "/etc/hestia_php_selector/php_sel_path.conf"
+	PHP_TEMPLATE             = "/usr/bin/php%s"
 )
 
 func escapeUserName(username string) string {
@@ -41,6 +43,21 @@ func isExecOther(mode os.FileMode) bool {
 	return mode&0001 != 0
 }
 
+func getPathtpPHP(phpVer string) string {
+	cont, err := os.ReadFile(PHP_SEL_CONF)
+	if err == nil {
+		result := strings.Split(strings.TrimSpace(string(cont)), "\n")
+		if len(result) > 0 {
+			php_check := strings.TrimSpace(result[0])
+			if (len(php_check) > 0) && (strings.Contains(php_check, "%s")) {
+				return fmt.Sprintf(php_check, phpVer)
+			}
+		}
+	}
+	return fmt.Sprintf(PHP_TEMPLATE, phpVer)
+
+}
+
 func getPHPVerFromConf(path string) (string, error) {
 	f, err := os.Open(path)
 	if err != nil {
@@ -77,7 +94,7 @@ func getUserPHPVerHestia(username string) (string, string, error) {
 	if phpVer == "" {
 		return "", "", nil
 	}
-	phpPath := "/usr/bin/php" + phpVer
+	phpPath := getPathtpPHP(phpVer)
 	if finfo, err := os.Stat(phpPath); err != nil {
 		return "", "", err
 	} else {
@@ -97,7 +114,7 @@ func setUserPHPVer(username string, php_ver string) error {
 	matches := reIsVer.FindStringSubmatch(php_ver)
 	if len(matches) > 1 {
 		phpver := strings.TrimSpace(matches[1])
-		phpPath := "/usr/bin/php" + phpver
+		phpPath := getPathtpPHP(phpver)
 		if finfo, err := os.Stat(phpPath); err != nil {
 			return err
 		} else {