#!/usr/bin/env bash DEBUG_LOG_FILE="/var/log/hestia_php_selector.log" debug_out_function(){ pattern=">>$DEBUG_LOG_FILE DEBUG" res="$@" dt="[$(date -Is)]" res1=${res#*debug_out_function} result="${res1%%$pattern*}" echo "$dt + ${result# }" } exec 3>&1 1> >(tee -a "$DEBUG_LOG_FILE") 2>&1 trap "debug_out_function $BASH_COMMAND>>$DEBUG_LOG_FILE" DEBUG function restore_system() { 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" -o "$php_sys" == "/usr/bin/hestiacp-php-selector" ]; then inst_tp="/usr/bin/php82" if [ -e /usr/local/hestia/conf/hestia.conf ]; then res=$(cat /usr/local/hestia/conf/hestia.conf | grep "LOCAL_PHP" | grep "yes") if [ -n "$res" ]; then inst_tp="/opt/brepo/php82/bin/php" fi fi update-alternatives --display php | grep "$inst_tp" 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 } uid=$(id -u) if [ "$uid" != "0" ]; then echo "Command must be executed as privileged user" exit 0 fi case "$1" in install) echo "Try to find php-selector for hestiacp" current_path_to_php="" if [ -e /usr/bin/php ]; then current_path_to_php=$(readlink -f /usr/bin/php) fi update-alternatives --display php | grep hestiacp-php-selector if [ $? -ne 0 ]; then echo "Register php-selector" update-alternatives --install /usr/bin/php php /usr/bin/hestiacp-php-selector 1 if [ ! -e /etc/hestia_php_selector/system/php.path ]; then mkdir -p /etc/hestia_php_selector/system/ echo "$current_path_to_php" >/etc/hestia_php_selector/system/php.path chmod 644 /etc/hestia_php_selector/system/php.path fi /usr/bin/hestiacp-php-admin add fi ;; delete) restore_system update-alternatives --display php | grep hestiacp-php-selector if [ $? -eq 0 ]; then update-alternatives --remove php /usr/bin/hestiacp-php-selector fi /usr/bin/hestiacp-php-admin remove-all ;; off) restore_system ;; *) echo "Unknown command" ;; esac