Files
SODEW/sodew-bash-main/libs/modules/unigma.sh
T
2026-08-18 09:40:08 +02:00

117 lines
3.7 KiB
Bash

function unigmaGetTxt() {
local domain="$1"
local raw
raw=$(dig +short +time=2 +tries=1 TXT "$domain.settings.wedos-int.dev" 2>/dev/null)
[[ -n "$raw" ]] || return 1
printf '%s\n' "$raw" | tr -d '"' | tr '\n' ' '
}
function unigmaParse() {
local raw="$1"
UNIGMA_PHP="" UNIGMA_MEM="" UNIGMA_EXEC=""
local IFS=';' pair key value
for pair in $raw; do
pair="$(sed 's/^[[:space:]]*//;s/[[:space:]]*$//' <<< "$pair")"
[[ -z "$pair" || "$pair" != *"="* ]] && continue
key="$(sed 's/^[[:space:]]*//;s/[[:space:]]*$//' <<< "${pair%%=*}")"
value="$(sed 's/^[[:space:]]*//;s/[[:space:]]*$//' <<< "${pair#*=}")"
case "$key" in
php) UNIGMA_PHP="$value" ;;
mem|memory_limit) UNIGMA_MEM="$value" ;;
exec|max_execution_time) UNIGMA_EXEC="$value" ;;
esac
done
}
function unigmaIniSet() {
local domain="$1"
local mem="$2"
local exec="$3"
local file content="" existing uid gid
[[ -n "$mem" || -n "$exec" ]] || return 0
uid=$(domainToUid "$domain")
[[ -n "$uid" ]] || { appError "Cannot resolve UID for: $domain"; return 1; }
gid=$(domainToUid "$domain")
[[ -n "$gid" ]] || { appError "Cannot resolve GID for: $domain"; return 1; }
[[ -n "$mem" ]] && content+="memory_limit = ${mem}"$'\n'
[[ -n "$exec" ]] && content+="max_execution_time = ${exec}"$'\n'
file="$olsPrivatePath/$domain/php/01-ols-private.ini"
# no change
existing="$(cat "$file" 2>/dev/null || true)"
[[ "${content%$'\n'}" == "$existing" ]] && return 0
fileAtomicWrite "$file" "$content"
chown -- "$uid:$gid" "$file" || { appError "Change owner of file: $file"; return 1; }
return 2
}
function unigmaPhpSet() {
local domain="$1"
local php="$2"
local path="$olsVhostsPath/$domain/www"
[[ -d "$path" ]] || { appError "Directory not found: $path"; return 1; }
local uid gid
uid=$(domainToUid "$domain")
[[ -n "$uid" ]] || { appError "Cannot resolve UID for: $domain"; return 1; }
gid=$(domainToGid "$domain")
[[ -n "$gid" ]] || { appError "Cannot resolve GID for: $domain"; return 1; }
if [[ -z "$php" ]]; then
find "$path" -maxdepth 1 -type f -name '.php[1-9][0-9]' -delete
return 0
fi
local suffix="${php//./}"
[[ "$suffix" =~ ^[1-9][0-9]$ ]] || { appError "Incorrect PHP version: $php"; return 1; }
# target
local target="$path/.php$suffix"
# find files .phpXX
local -a current
mapfile -t current < <(find "$path" -maxdepth 1 -type f -name '.php[1-9][0-9]')
if [[ ${#current[@]} -eq 0 ]]; then
touch "$target"
chown -- "$uid:$gid" "$target" || true
return 0
fi
# no change
if [[ ${#current[@]} -eq 1 && "${current[0]}" == "$target" ]]; then
return 0
fi
# rename (atomic substitution)
mv -f "${current[0]}" "$target"
chown -- "$uid:$gid" "$target" || true
# remove other files .phpXX
if [[ ${#current[@]} -gt 1 ]]; then
local i
for ((i = 1; i < ${#current[@]}; i++)); do
rm -f "${current[i]}"
done
fi
}
function unigmaUpdate() {
local domain="$1"
local raw
raw="$(unigmaGetTxt "$domain")" || return 0
unigmaParse "$raw"
unigmaIniSet "$domain" "$UNIGMA_MEM" "$UNIGMA_EXEC"
unigmaPhpSet "$domain" "$UNIGMA_PHP"
}