HEX
Server: Apache/2.4.65 (Unix) OpenSSL/1.1.1k
System: Linux vps109042.inmotionhosting.com 4.18.0 #1 SMP Mon Sep 30 15:36:27 MSK 2024 x86_64
User: cisa (1010)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: //opt/cwprads/docroot
#!/usr/bin/env bash
# docroot: Find the document root for domains on a CWP server
# Nathan P. <code@tchbnl.net>
# 0.1d
set -euo pipefail

# This script is meant to be used by root or a user with sudo access to it
# The silent exit is intentional
current_user="$(whoami)"
if [[ "${current_user}" != 'root' ]]; then
    exit 1
fi

# Version information
VERSION='0.1d'

# Nice text formatting options
TEXT_BOLD='\e[1m'
TEXT_UNSET='\e[0m'

# Help message
show_help() {
    cat << END_OF_HELP
$(echo -e "${TEXT_BOLD}")docroot:$(echo -e "${TEXT_UNSET}") Find the document root for domains on a CWP server

USAGE: docroot DOMAIN
    --help -h           Show this message
    --version -v        Show version information
END_OF_HELP
}

# Newer versions of MariaDB complain about using 'mysql'
if command -v mariadb >/dev/null 2>&1; then
    MYSQL_COMMAND='mariadb'
else
    MYSQL_COMMAND='mysql'
fi

# We run our domain through all three tables to see if there's a match. If
# there is, we're good and stop there. If not, we move on to the next one.
find_docroot() {
    # Primary domain
    result="$(${MYSQL_COMMAND} -u root root_cwp -Nse "SELECT username FROM user WHERE domain = '${domain}';")"
    if [[ -n "${result}" ]]; then
        echo "/home/${result}/public_html"
        return 0
    fi

    # Addon domain
    result="$(${MYSQL_COMMAND} -u root root_cwp -Nse "SELECT path FROM domains WHERE domain = '${domain}';")"
    if [[ -n "${result}" ]]; then
        echo "${result}"
        return 0
    fi

    # Subdomain
    result="$(${MYSQL_COMMAND} -u root root_cwp -Nse "SELECT path FROM subdomains WHERE CONCAT(subdomain, '.', domain) = '${domain}';")"
    if [[ -n "${result}" ]]; then
        echo "${result}"
        return 0
    fi

    # And if no results were found at all...
    echo "${domain} wasn't found in the CWP database." >&2
    return 1
}

# Command options
while [[ "${#}" -gt 0 ]]; do
    case "${1}" in
        --help | -h)
            show_help
            exit 0
            ;;

        --version | -v)
            echo -e "${TEXT_BOLD}docroot${TEXT_UNSET} ${VERSION}"
            exit 0
            ;;

        -*)
            echo -e "Not sure what ${1} is supposed to mean..." >&2
            echo
            show_help
            exit 1
            ;;

        *)
            break
            ;;
    esac
done

# Show help message and exit if no arguments (like a domain) were passed
if [[ "${#}" -eq 0 ]]; then
    show_help
    exit 0
fi

# First removing any trailing period
domain=${1%.}

# Second convert the user's input into lowercase (domains and subdomains are
# lowercase according to Google) and capture it
domain=${domain,,}

# And then check if it's valid (not strict)
# allows domain to end in a period and punycode domains such as xn--j6h.com
if [[ ! "${domain}" =~ ^([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$ ]]; then
    echo "That doesn't look like a valid domain or subdomain." >&2
    exit 1
fi

# And then pass the valid input to find_docroot if it's valid. Valid.
find_docroot "${domain}"