#!/bin/bash

function welcome() { cat <<EOF
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#                                                                         #
# TeTeOS.Net Website Manager (or Nginx Manager) (TSite)                   #
#                                                                         #
# by TeTeOS.Net ( teteos.net )                                            #
# Copyright (c) 2024. TeTeOS.Net                                          #
#                                                                         #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
EOF
}
version="v8.0"

# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# Before progress
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =


# Check permissions (Admin Required)
if ! [[ `whoami` = 'root' ]]; then
    echo "[!] Admin permissions are required."
    exit 1
fi


# PiluX-Based OS variables
if [[ -f "/etc/teteosnet/os/is_pilux_based" ]]; then
    source /etc/teteosnet/scripts/pilux_variables.sh &>/dev/null
fi
        

# Parameter check function
ParameterIs() {
    local parameter="$1"
    local valid_parameters=("$2" "-$2" "--$2" "/$2")
    for valid_param in "${valid_parameters[@]}"; do
        if [ "$parameter" = "$valid_param" ]; then
            return 0 # true
        fi
    done
    return 1 # false
}


# Check Nginx is installed?
if ! ParameterIs "$1" "install" && ! ParameterIs "$1" "copybin"; then
    if ! [[ -d "/etc/nginx" && -f "/usr/sbin/nginx" ]]; then
        echo "[!] Nginx not installed! Please install Nginx before using TSite."
        if [[ $0 = "/usr/sbin/teteosnet/tsite" || $0 = "/usr/sbin/tsite" ]]; then
            echo "[i] Type \"tsite install\" to start installation by TSite."
        else
            echo "[i] Type \"$0 install\" to start installation by TSite."
        fi
        exit 1
    elif ! [[ -d "/etc/nginx/sites-available" ]]; then
        echo "[!] Nginx sites-available folder does not exist! Please install Nginx before using TSite."
        if [[ $0 = "/usr/sbin/teteosnet/tsite" || $0 = "/usr/sbin/tsite" ]]; then
            echo "[i] Type \"tsite install\" to start installation by TSite."
        else
            echo "[i] Type \"$0 install\" to start installation by TSite."
        fi
        exit 1
    elif ! [[ -f "/etc/nginx/installed_by_tsite" ]]; then
        echo ""
        echo "[!] Warning Nginx not installed by TSite!."
        echo "[i] If you plan use TSite for manage your Nginx more easy, we really highly"
        echo " recommend backup your all data and reinstall Nginx, PHP, Mysql with TSite."
        echo ""
        echo " For remove Nginx, PHP, Mysql (with ALL DATA!) and reinstall with TSite," 
        if [[ $0 = "/usr/sbin/teteosnet/tsite" || $0 = "/usr/sbin/tsite" ]]; then
            echo " just type \"tsite tsite\"."
        else
            echo " just type \"$0 install\"."
        fi
        echo ""
    fi
fi


# Check PHP is installed?
php_version="0"
if [[ -f "/usr/bin/php" ]]; then
    php_version=$(php -r "echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION;")
    if [[ $php_version < "8" ]]; then
        echo "[!] PHP version unsupported."
        exit 1
    fi
else
    if ! ParameterIs "$1" "install" && ! ParameterIs "$1" "copybin"; then
        echo "[!] PHP not installed! Please install Nginx before using TSite."
        if [[ $0 = "/usr/sbin/teteosnet/tsite" || $0 = "/usr/sbin/tsite" ]]; then
            echo "[i] Type \"tsite install\" to start installation by TSite."
        else
            echo "[i] Type \"$0 install\" to start installation by TSite."
        fi
        exit 1
    fi
fi


# Get "tsite" location
if [[ $0 = "/usr/sbin/teteosnet/tsite" || $0 = "/usr/sbin/tsite" ]]; then
    tsite="tsite"
else
    tsite=$0
fi


# Create some required folders if not exist.
mkdir -p /var/run/mysqld
chown mysql:mysql /var/run/mysqld
mkdir -p /var/log/mysql
chown mysql:mysql /var/log/mysql

mkdir -p /var/run/sshd

mkdir -p /var/log/nginx/
chown -R www-data:www-data /var/log/nginx/
find /var/log/nginx -type d -exec chmod 755 {} \;
find /var/log/nginx -type f -exec chmod 644 {} \;
mkdir -p /var/run/php/
chown -R www-data:www-data /var/run/php
find /var/run/php -type d -exec chmod 755 {} \;
find /var/run/php -type f -exec chmod 644 {} \;



# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# Functions
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Help(){
    echo ""
    welcome
    echo ""

    echo " TeTeOS.Net Website Manager (or Nginx Manager) (TSite)
 Version: $version (Beta)

 Website: teteos.net/tsite

 Usage:
  - Domain managing:
  add [domain]                    Add domain.
  add [domain] fast               Add domain, enable it, add to hosts file.
  del [domain]                    Delete domain.
  del [domain] all                Delete domain with folder and from hosts file.
  list                            List domains.
  list names-only                 List domains, showing only domain names.
  enable [domain]                 Enable domain.
  disable [domain]                Disable domain.

  - Subdomain managing:
  sub-add [domain] [su            Add subdomain to domain.
  sub-del [domain] [su            Delete subdomain from domain.
  sub-list [domain]               List subdomains of domain.

  - Server managing:"
    if ! [[ $0 = "/usr/sbin/teteosnet/tsite" || $0 = "/usr/sbin/tsite" ]]; then
    echo "  copybin                         Install as a command in this system (copy to bin)."
    fi
    echo "  install                         (Re)install Nginx,Php,Mysql,Certbot,Composer.
  restart                         Restart Nginx, Php, Mysql services.
  stop                            Stop Nginx, Php, Mysql services.
  firewall                        Attempt to allow ports 80&443 in firewalls.
  hosts-add [domain]              Add domain to the /etc/hosts file.
  hosts-del [domain]              Delete domain from /etc/hosts file.

  - SSL
  https-on [domain]               Enable SSL for domain.
  https-off [domain]              Disable SSL for domain.
  ssl-signself                    Create and configure nginx self-signed SSL.
  ssl-signself-renew              Renew signed SSL certificate (365 days).
  ssl-signself-remove             Remove self-sign certificate (!).
  ssl-signself-showcrt            Show self-sign certificate.
  ssl-signself-showcsr            Show self-sign certificate request.
  ssl-signself-showkey            Show self-sign certificate (!)private(!) key.

  - File manager:
  phpini                          Try to open php.ini file with text editor.
  cd                              Try change directory to domain folder.
   "
    if ! [[ -f "/etc/teteosnet-data/regs/policies/tsite/block_mysql_cmd" && `cat /etc/teteosnet-data/regs/policies/tsite/block_mysql_cmd` == "true" ]]; then
        Help_MYSQL
    fi

}


CopyToBin(){
    if ! [[ $0 = "/usr/sbin/teteosnet/tsite" || $0 = "/usr/sbin/tsite" ]]; then
        # Check parameter is set?
        if [[ $1 = "" ]]; then
            return 1 # false
        fi


        # Copy to sbin now.
        if grep -q "/usr/sbin/teteosnet/bin" "/etc/environment"; then
            rm -f "/usr/sbin/teteosnet/$1"
            cp "$0" "/usr/sbin/teteosnet/$1" &>/dev/null
            chmod +x "/usr/sbin/teteosnet/$1" &>/dev/null
        else
            rm -f "/usr/sbin/$1"
            cp "$0" "/usr/sbin/$1" &>/dev/null
            chmod +x "/usr/sbin/$1" &>/dev/null
        fi


        # Check copy is failed?
        if [[ ! -f "/usr/sbin/teteosnet/tsite" && ! -f "/usr/sbin/tsite" ]]; then
            echo "[!] Cannot copyed to '/usr/sbin'. (Write or permission error.)"
            return 1 # false
        fi


        # Return Finish
        return 0

    fi
}


apt_install(){
    # Check parameter is set?
    if [[ $1 = "" ]]; then
        return 1 # false
    fi


    # Install package(s) now.
    for package in "$@"; do
        apt install -y --no-install-recommends "$package" &>/dev/null

        # Install package is failed?
        if ! dpkg -s "$package" >/dev/null 2>&1; then
            echo "[!] Failed to install a package: '$package'."
            return 1 # false
        fi

    done


    # Return Finish
    return 0
}


apt_reinstall(){
    # Check parameter is set?
    if [[ $1 = "" ]]; then
        return 1 # false
    fi


    # Install package(s) now.
    for package in "$@"; do
        apt install -y --no-install-recommends --reinstall "$package" &>/dev/null

        # Install package is failed?
        if ! dpkg -s "$package" >/dev/null 2>&1; then
            echo "[!] Failed to install a package: '$package'."
            return 1 # false
        fi

    done


    # Return Finish
    return 0
}


apt_install_nocheck(){
    # Check parameter is set?
    if [[ $1 = "" ]]; then
        return 1 # false
    fi


    # Install package(s) now.
    for package in "$@"; do
        apt install -y --no-install-recommends "$package" &>/dev/null
    done


    # Return Finish
    return 0
}


apt_reinstall_nocheck(){
    # Check parameter is set?
    if [[ $1 = "" ]]; then
        return 1 # false
    fi


    # Install package(s) now.
    for package in "$@"; do
        apt install -y --no-install-recommends --reinstall "$package" &>/dev/null
    done


    # Return Finish
    return 0
}


is_alphanumeric() {
    if [[ $1 = "" ]]; then
        return 1 # false
    elif [[ "$1" =~ ^[a-zA-Z0-9iI]+$ ]]; then # TR language bug: iI is special character.
        return 0 # true
    else
        return 1 # false
    fi
}


domain_check(){
    # Check parameter is set?
    if [[ $1 = "" ]]; then
        return 1 # false
    fi


    # $domain
    domain_check_domain=$(echo "$1" | tr '[:upper:]' '[:lower:]')


    # Disallow 000-default name.
    if [[ $domain_check_domain = "000-default" ]]; then
        if ! [[ $2 = "silent" ]]; then
            echo "[!] The name '000-default' is reserved for the system."
        fi
        return 1 # false
    fi


    # Disallow start-ends with dot.
    if [[ $3 != "dotallow" ]]; then
        if [[ $domain_check_domain == .* || $domain_check_domain == *. ]]; then
            if ! [[ $2 = "silent" ]]; then
                echo "[!] Domain names cannot start or end with a dot."
            fi
            return 1 # false
        fi
    else
        if [[ $domain_check_domain == ..* || $domain_check_domain == *.. ]]; then
            if ! [[ $2 = "silent" ]]; then
                echo "[!] Domain names cannot start or end with double dots."
            fi
            return 1 # false
        fi
    fi



    # Disallow ends with .backup.
    if [[ $domain_check_domain == *.backup ]]; then
        if ! [[ $2 = "silent" ]]; then
            echo "[!] Domain names cannot end with '.backup'."
        fi
        return 1 # false
    fi



    # Disallow protocol in domain name.
    if [[ $domain_check_domain == *://* ]]; then
        if ! [[ $2 = "silent" ]]; then
            echo "[!] Domain names cannot contain protocols like http:// or https://..."
        fi
        return 1 # false
    fi


    # Disallow start with www.
    if [[ $domain_check_domain == www.* ]]; then
        if ! [[ $2 = "silent" ]]; then
            echo "[!] Domain names cannot start with 'www'."
        fi
        return 1 # false
    fi


    # 1-253 length accepted.
    if (( ${#domain_check_domain} < 1 )) || (( ${#domain_check_domain} > 253 )); then
        if ! [[ $2 = "silent" ]]; then
            echo "[!] Domain names must have a length of 1-253 characters."
        fi
        return 1 # false
    fi


    # Check domain regex.
    if ! [[ "$domain_check_domain" =~ ^[a-zA-Z0-9\.\i\I\-]*$ ]]; then
        if ! [[ $2 = "silent" ]]; then
            echo "[!] Domain name does not match the required regex pattern."
        fi
        echo "[i] The domain may contain unsupported special characters."
        return 1 # false
    fi


    # Return Finish
    return 0 # true
}


domain_add() {
    # Check parameter is set?
    if [[ $1 = "" ]]; then
        return 1 # false
    fi


    # $domain
    domain_add_domain=$(echo "$1" | tr '[:upper:]' '[:lower:]')


    # Create teteosnet.conf if not exist.
    if ! teteosnetconf; then
        exit 1
    fi
    

    # Create /etc/nginx/sites-available/domainname file.
    mkdir -p /etc/nginx/sites-available/
    mkdir -p /etc/nginx/sites-enabled/
    if [[ -f "/etc/nginx/sites-available/000-default" && $domain_add_domain != "000-default" ]]; then
        cp "/etc/nginx/sites-available/000-default" "/etc/nginx/sites-available/$domain_add_domain" &>/dev/null
        sed -i "s/000-default/$domain_add_domain/g" "/etc/nginx/sites-available/$domain_add_domain" &>/dev/null
    else
        SITEFILE_CONTENT='# Domain file generated by tsite tool. ( teteos.net/tsite )

        # "www." subdomain
        server {

            # Listen ports
            listen 80;

            # Domain
            server_name www.'$domain_add_domain' '$domain_add_domain';
            server_tokens off;

            # Folder
            root "/var/www/'$domain_add_domain'/_www/";
            include /var/www/'$domain_add_domain'/_www/.nginx[.]conf;
            error_page 403 404 410 =200 /error_404.php;

            # Using PHP'$php_version'
            index index.html index.htm index.php;
            location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php'$php_version'-fpm.sock;
            }

        }
        #tsite:add_sub_domain_section #dont remove this line!
        # Other subdomains
        server {

            # Listen ports
            listen 80;

            # Domain
            server_name *.'$domain_add_domain';
            server_tokens off;

            # Folder
            root "/var/www/'$domain_add_domain'/_404/";
            include /var/www/'$domain_add_domain'/_404/.nginx[.]conf;
            error_page 403 404 410 =200 /error_404.php;

            # Using PHP'$php_version'
            index index.html index.htm index.php;
            location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php'$php_version'-fpm.sock;
            }

            # TSite <3 config
            include snippets/teteosnet.conf;

        }'
        echo "$SITEFILE_CONTENT" > "/etc/nginx/sites-available/$domain_add_domain"
    fi


    # Check is failed?
    if ! [ -f "/etc/nginx/sites-available/$domain_add_domain" ]; then
        echo "[!] Failed to creating domain file. (Write or permission error.)"
        return 1 # false
    fi


    # Enable SSL if self-signed.
    if [[ -f "/etc/nginx/snippets/ssl-params.conf" && -f "/etc/nginx/snippets/self-signed.conf" ]]; then
        ssl_on $domain_add_domain "silent"
    else
        ssl_off $domain_add_domain "silent"
    fi


    # Set permission to file.
    chmod 644 "/etc/nginx/sites-available/$domain_add_domain" &>/dev/null


    # Return Finish
    return 0 # true
}


teteosnetconf(){
    if [[ -f "/etc/nginx/snippets/teteosnet.conf" ]]; then
        return 0 # return true, file exist.
    fi

    mkdir -p /etc/nginx/snippets/
    SDAUISDA7Y8SDA='
    #
    # Rules for all domains created using tsite tool by TeTeOS.Net
    #

    #
    # Security
    #

    # Block access files start with .ht (for protect .htaccess .htpassword etc...)
    location ~ /\.ht {
        deny all;
    }

    add_header X-Frame-Options SAMEORIGIN;
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";'
    echo "$SDAUISDA7Y8SDA" > /etc/nginx/snippets/teteosnet.conf && unset SDAUISDA7Y8SDA
    if [[ ! -f "/etc/nginx/snippets/teteosnet.conf" ]]; then
        echo "[!] Cannot created '/etc/nginx/snippets/teteosnet.conf' file!"
        return 1 # false
    fi
}

domain_add_folder() {
    # Check parameter is set?
    if [[ $1 = "" ]]; then
        return 1 # false
    fi


    # $domain
    domain_add_folder_domain=$(echo "$1" | tr '[:upper:]' '[:lower:]')


    # Create domain folder.
    mkdir -p "/var/www/$domain_add_folder_domain/_www/"
    mkdir -p "/var/www/$domain_add_folder_domain/_404/"


    if [[ -d "/var/www/000-default/_www/" && $domain_add_folder_domain != "000-default" ]]; then
        cp -r "/var/www/000-default/_www/." "/var/www/$domain_add_folder_domain/_www/" &>/dev/null
        cp -r "/var/www/000-default/_404/." "/var/www/$domain_add_folder_domain/_404/" &>/dev/null
    else
        index_php "/var/www/$domain_add_folder_domain/_www/" "index.php" "Congratulations! The website is up and running!" "This host is utilized for hosting websites."
        index_php "/var/www/$domain_add_folder_domain/_www/" "error_404.php" "Oops! Page not found :(" "There is no page available at this location. (404)"
        index_php "/var/www/$domain_add_folder_domain/_404/" "index.php" "Oops! Sub domain not found :(" "This subdomain does not exists."
        index_php "/var/www/$domain_add_folder_domain/_404/" "error_404.php" "Oops! Page not found :(" "There is no page available at this location. (404)"
    fi 


    # Check is failed?
    if [[ ! -d "/var/www/$domain_add_folder_domain/_www" ]]; then
        echo "[!] Failed to create domain folder. (Write or permission error.)"
        return 1 # false
    fi


    # Set permissions
    chown -R www-data:www-data "/var/www/$domain_add_folder_domain" &>/dev/null
    chmod -R 750 "/var/www/$domain_add_folder_domain" &>/dev/null


    # Return Finish
    return 0 # true
}


subdomain_add_folder() {
    # Check parameter is set?
    if [[ $1 = "" ]]; then
        return 1 # false
    fi


    # $domain and $subdomain
    subdomain_add_folder_domain=$(echo "$1" | tr '[:upper:]' '[:lower:]')
    subdomain_add_folder_subdomain=$(echo "$2" | tr '[:upper:]' '[:lower:]')


    # Create domain folder.
    mkdir -p "/var/www/$subdomain_add_folder_domain/$subdomain_add_folder_subdomain/"
    if [[ -d "/var/www/000-default/_www/" ]]; then
        cp -r "/var/www/000-default/_www/." "/var/www/$subdomain_add_folder_domain/$subdomain_add_folder_subdomain/" &>/dev/null
    else
        index_php "/var/www/$subdomain_add_folder_domain/$subdomain_add_folder_subdomain/" "index.php" "Congratulations! The website is up and running!" "This host is utilized for hosting websites."
        index_php "/var/www/$subdomain_add_folder_domain/$subdomain_add_folder_subdomain/" "error_404.php" "Oops! Page not found :(" "There is no page available at this location. (404)"
    fi 


    # Check is failed?
    if [[ ! -d "/var/www/$subdomain_add_folder_domain/$subdomain_add_folder_subdomain/" ]]; then
        echo "[!] Failed to create subdomain folder. (Write or permission error.)"
        return 1 # false
    fi


    # Set permissions
    chown -R www-data:www-data "/var/www/$subdomain_add_folder_domain/$subdomain_add_folder_subdomain" &>/dev/null
    chmod -R 750 "/var/www/$subdomain_add_folder_domain/$subdomain_add_folder_subdomain" &>/dev/null


    # Return Finish
    return 0 # true
}


index_php() {
    # Check parameter is set?
    if [[ $1 = "" ]]; then
        return 1 # false
    fi


    # Get messages.
    if [[ $3 = "" ]]; then
        message1="Congratulations! The website is up and running!"
    else
        message1="$3"
    fi
    if [[ $3 = "" ]]; then
        message2="This host is utilized for hosting websites."
    else
        message2="$4"
    fi


    # Get PiluX Based OS name.
    if [[ -f "/etc/teteosnet/os/is_pilux_based" ]]; then
        love_with=$PILUX_OS_NAME
    else
        love_with="Nginx"
    fi


    INDEX_PHP='<!DOCTYPE html>
    <!DOCTYPE html>
    <html oncontextmenu="return false;">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>A TSite <3</title>
            <style>
                html {
                    -webkit-touch-callout: none;
                    -webkit-user-select: none;
                    -khtml-user-select: none;
                    -moz-user-select: none;
                    -ms-user-select: none;
                    user-select: none;
                }
                body {
                    margin: 0;
                    padding: 0;
                    background-color: #FFF;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    height: 100vh;
                    font-family: Consolas, monospace;
                    font-size: 18px;
                }
                .container {
                    background-color: #f1f1f1;
                    border-radius: 10px;
                    padding: 40px;
                    box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
                    max-width: 70%;
                }

                @media (min-width: 420px) {
                    .container {
                        min-width: 400px;
                    }
                }
            </style>
        </head>
        <body>
            <div class="container">
                <center>
                <h2 style="color:#FF5500;">TSite <3</h2>
                    <div style="word-wrap: break-word;">
                        <b>'$message1'</b><br>
                        '$message2'
                        <br><br><a target="_blank" href="https://www.teteos.net/tsite">TSite</a> 💕 '$love_with'<br>
                    </div>
                </center>
            </div>
        </body>
    </html>
    <style>
        body {
            background: url(background.svg);
        }
    </style>'
    echo "$INDEX_PHP" > "$1/$2"
    if [[ ! -f "$1/background.svg" ]]; then
        SVG_BACKGROUND='
        <svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg" version="1.1">
        <defs>
            <pattern id="backgroundPattern" x="0" y="0" width="40" height="40" patternUnits="userSpaceOnUse">
            <rect x="0" y="0" width="40" height="40" fill="#ffffff" />
            <polygon points="0,0 20,0 10,20" fill="#f0f0f0" />
            <polygon points="20,0 40,0 30,20" fill="#f0f0f0" />
            <polygon points="10,20 30,20 20,40" fill="#f0f0f0" />
            </pattern>
        </defs>
        <rect width="100%" height="100%" fill="url(#backgroundPattern)" />
        </svg>'
        echo "$SVG_BACKGROUND" > "$1/background.svg"
    fi


    # Check is failed?
    if ! [[ -f "$1/$2" ]]; then
        echo "[!] Failed to creating $1/$2 file. (Write or permission error.)"
        return 1 # false
    fi


    # Return Finish.
    return 0 # true
}


domain_enable(){
    # Check parameter is set?
    if [[ $1 = "" ]]; then
        return 1 # false
    fi


    # $domain
    domain_enable_domain=$(echo "$1" | tr '[:upper:]' '[:lower:]')


    # Enable domain.
    rm -f "/etc/nginx/sites-enabled/$domain_enable_domain" &>/dev/null
    ln -s /etc/nginx/sites-available/$domain_enable_domain "/etc/nginx/sites-enabled/$domain_enable_domain" &>/dev/null
        

    # Check is failed?
    if ! [[ -f "/etc/nginx/sites-enabled/$domain_enable_domain" ]]; then
        if ! [[ $2 = "silent" ]]; then
            echo "[!] Failed to enable domain. (Write or permission error.)"
        fi
        return 1 # false
    fi


    # Return Finish
    if ! [[ $2 = "silent" ]]; then
        echo "[√] Domain enabled successfully."
    fi
    return 0 # true
}


ssl_on(){
    # Check parameter is set?
    if [[ $1 = "" ]]; then
        return 1 # false
    fi


    # $domain
    ssl_on_domain=$(echo "$1" | tr '[:upper:]' '[:lower:]')


    # Check SSL is already enabled?
    if grep -q "listen 443 ssl" "/etc/nginx/sites-available/$ssl_on_domain"; then
        if ! [[ $2 = "silent" ]]; then
            echo "[!] SSL is already on in this domain."
        fi
        return 1 # false
    fi
    

    # Add "listen 443 ssl;" line to domain file.
    if [[ -f "/etc/nginx/snippets/self-signed.conf" && -f "/etc/nginx/snippets/ssl-params.conf" ]]; then
        sed -i "s/listen 80;/listen 80;\n            listen 443 ssl;\n            include snippets\/self-signed.conf;\n            include snippets\/ssl-params.conf;/g" "/etc/nginx/sites-available/$1"
    else
        sed -i "s/listen 80;/listen 80;\n            listen 443 ssl;/g" "/etc/nginx/sites-available/$ssl_on_domain"
    fi


    # Check is failed?
    if ! grep -q "listen 443 ssl" /etc/nginx/sites-available/$ssl_on_domain; then
        if ! [[ $2 = "silent" ]]; then
            echo "[!] SSL cannot added to domain. (Write or permission error.)"
        fi
        return 1 # false
    fi


    # Return Finish
    if ! [[ $2 = "silent" ]]; then
        echo "[√] SSL added to domain successfully."
    fi
    service_restart_nginx
    return 0 # true
}


ssl_off(){
    # Check parameter is set?
    if [[ $1 = "" ]]; then
        return 1 # false
    fi


    # $domain
    ssl_off_domain=$(echo "$1" | tr '[:upper:]' '[:lower:]')


    # Check SSL is already disabled?
    if ! grep -q "listen 443 ssl" "/etc/nginx/sites-available/$ssl_off_domain"; then
        if ! [[ $2 = "silent" ]]; then
            echo "[!] SSL is already off in this domain."
        fi
        return 1 # false
    fi


    # Remove SSL from domain file.
    sed -i "/listen 443 ssl/d" "/etc/nginx/sites-available/$ssl_off_domain"
    sed -i "/include snippets\/self-signed.conf/d" "/etc/nginx/sites-available/$ssl_off_domain"
    sed -i "/include snippets\/ssl-params.conf/d" "/etc/nginx/sites-available/$ssl_off_domain"


    # Check is failed?
    if grep -q "listen 443 ssl" /etc/nginx/sites-available/$ssl_off_domain; then
        echo "[!] SSL cannot removed to site. (Write or permission error.)"
        return 1 # false
    fi


    # Return Finish
    if ! [[ $2 = "silent" ]]; then
        echo "[√] SSL removed from domain successfully."
    fi
    service_restart_nginx
    return 0 # true
}

hosts_add() {
    # Check parameter is set?
    if [[ $1 = "" ]]; then
        return 1 # false
    fi


    # $domain
    hosts_add_domain=$(echo "$1" | tr '[:upper:]' '[:lower:]')

    # Create /etc/hosts file if not exist.
    if [[ ! -f "/etc/hosts" ]]; then
        echo -e "127.0.0.1       localhost\n127.0.0.1       $(hostname)\n\n# IPv6 loopback\n::1             localhost ip6-localhost ip6-loopback\nff02::1         ip6-allnodes\nff02::2         ip6-allrouters" > /etc/hosts
    fi


    # Check if domain is already added to hosts file
    if grep -Eq "127\.0\.0\.1[[:space:]]*$hosts_add_domain\$" /etc/hosts; then
        if ! [[ $2 = "silent" ]]; then
            echo "[!] Domain found in /etc/hosts. (May be already added)"
        fi
        return 1 # false
    fi


    # Add domain to hosts file now.
    # Check if localhost line exists, if yes, add below it
    if grep -Eq "127\.0\.0\.1[[:space:]]*localhost" /etc/hosts; then
        sed -i "/127\.0\.0\.1[[:space:]]*localhost/a 127.0.0.1       $hosts_add_domain" /etc/hosts
    else
        echo "127.0.0.1 $hosts_add_domain" >> /etc/hosts
    fi


    # Check is failed?
    if ! grep -Eq "127\.0\.0\.1[[:space:]]*$hosts_add_domain\$" /etc/hosts; then
        if ! [[ $2 = "silent" ]]; then
            echo "[!] Domain cannot be added to hosts file. (Write or permission error.)"
        fi
        return 1 # false
    fi


    # Return Finish
    if ! [[ $2 = "silent" ]]; then
        echo "[√] Domain added to hosts file successfully."
    fi
    resolvectl flush-caches &>/dev/null
    return 0 # true
}


hosts_del() {
    # Check parameter is set?
    if [[ $1 = "" ]]; then
        return 1 # false
    fi


    # $domain
    hosts_del_domain=$(echo "$1" | tr '[:upper:]' '[:lower:]')

    # Check hosts file is exist?
    if [[ ! -f "/etc/hosts" ]]; then
        return 0 # true # for nothing do.
    fi


    # Check domain is already not found in hosts file?
    if ! grep -Eq "^[[:space:]]*127\.0\.0\.1[[:space:]]+$hosts_del_domain([[:space:]]|$)" /etc/hosts; then
        if ! [[ $2 = "silent" ]]; then
            echo "[!] Domain not found in /etc/hosts. (May be already deleted)"
        fi
        return 1 # false
    fi


    # Remove domain in hosts file now.
    sed -i "/^[[:space:]]*127\.0\.0\.1[[:space:]]\+$hosts_del_domain[[:space:]]*$/d" /etc/hosts


    # Check is failed?
    if grep -Eq "^[[:space:]]*127\.0\.0\.1[[:space:]]+$hosts_del_domain([[:space:]]|$)" /etc/hosts; then
        if ! [[ $2 = "silent" ]]; then
            echo "[!] Domain cannot be removed in hosts file. (Write or permission error.)"
        fi
        return 1 # false
    fi


    # Return Finish
    if ! [[ $2 = "silent" ]]; then
        echo "[√] Domain deleted in hosts file successfully."
    fi
    resolvectl flush-caches &>/dev/null
    return 0 # true
}


service_firewall_configure(){
    ufw allow 'Nginx Full' &>/dev/null
    ufw delete allow 'Nginx HTTP' &>/dev/null
    IPTABLESS=`iptables -S | grep "\-\-dport 80" | wc -l`
    if [[ $IPTABLESS = "0" ]]; then
        iptables -A INPUT -p tcp --dport 80 -j ACCEPT &>/dev/null
    fi
    IPTABLESS=`iptables -S | grep "\-\-dport 443" | wc -l`
    if [[ $IPTABLESS = "0" ]]; then
        iptables -A INPUT -p tcp --dport 443 -j ACCEPT &>/dev/null
    fi
    unset IPTABLESS
}


service_restart_nginx(){
    # Restart Nginx
    if nginx -t > /dev/null 2>&1; then # Test before restart.
        systemctl restart nginx.service &>/dev/null
        if ! systemctl is-active --quiet nginx; then
            echo "[!] Critical!: Nginx service not active!"
            echo "[i] Executing \"systemctl status nginx\" for see error."
            getstatus=$(systemctl status nginx)
            echo "===================================================================================="
            echo "$getstatus"
            echo "===================================================================================="
            return 1 # false
        fi
    else
        echo "[!] Warning: Restart canceled. Nginx test failed."
        echo "[i] Executing \"nginx -t\" for see error."
        echo "===================================================================================="
        nginx -t
        echo "===================================================================================="
        return 1 # false
    fi


    # Return Finish.
    return 0 # true
}


service_restart_php(){
    # Check PHP directory.
    if [[ ! -d  /etc/php/$php_version/ ]]; then
        echo "[!] Restart canceled. PHP directory not found."
        return 1 #false
    fi


    # Check /etc/php/$php_version/fpm/pool.d/ is empty?
    if [[ -z $(ls -A /etc/php/$php_version/fpm/pool.d/) ]]; then
        echo "[!] Warning: Restart canceled. /etc/php/$php_version/fpm/pool.d/ is empty."
        return 1 # false
    fi


    # Restart PHP.
    systemctl restart php$php_version-fpm &>/dev/null


    # Check is failed?
    if ! systemctl is-active --quiet php$php_version-fpm; then
        echo "[!] Critical!: PHP service not active!"
        echo "[i] Executing \"systemctl status php$php_version-fpm\" for see error."
        getstatus=$(systemctl status php$php_version-fpm)
        echo "===================================================================================="
        echo "$getstatus"
        echo "===================================================================================="
        return 1 # false
    fi


    # Return Finish.
    return 0 # true
}


service_restart_all(){
    # Restart Nginx
    if ! service_restart_nginx; then
        exit 1
    fi
    # Restart PHP
    if ! service_restart_php; then
        exit 1
    fi
    # Restart MYSQL
    systemctl restart mysql &>/dev/null
    # Return success.
    return 0 # true
}


service_stop_all(){
    # Nginx
    systemctl stop nginx.service &>/dev/null
    # PHP
    systemctl stop php$php_version-fpm &>/dev/null
    # Mysql
    systemctl stop mysql &>/dev/null
}


# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - mysql [MANAGER]
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Help_MYSQL(){
    echo "  - MYSQL managing:
  mysql user-add                        Add MYSQL user. (mysql -u root -p)
  mysql user-del                        Del MYSQL user. (mysql -u root -p)
  mysql user-list                       List MYSQL users. (mysql -u root -p)
  mysql user-setpass [user][old][new]   Set (!)password(!) to MYSQL user.
  mysql db-add [db]                     data
  mysql db-del [db]                     data
  mysql db-list [db]                    data
  mysql db-export [user][db][path]      Backup database to .sql file.
  mysql db-import [user][db][path]     Restore database from .sql file.
  mysql db-cancreate [user]             Grand permission user to create new database.
  mysql db-cannotcreate [user]          Remove permission user to create new database.
  mysql db-allow [user][db]                Grand all permissions to user for specified database.
  mysql db-disallow [user][db]             Remove all permissions from user for specified database."
echo ""
}


if ParameterIs "$1" "mysql" ; then
    # Check TReg Policy
    if [[ -f "/etc/teteosnet-data/regs/policies/tsite/block_mysql_cmd" && `cat /etc/teteosnet-data/regs/policies/tsite/block_mysql_cmd` == "true" ]]; then
        echo "[!] TSite MYSQL commands blocked."
        exit 1 # false
    fi


    # Check any mysql parameter is set?
    if [[ $2 = "" ]]; then
        echo ""
        echo " Usage:"
        Help_MYSQL
        exit
    fi

    # --- --- --- ---
    # mysql user-add
    # --- --- --- ---
    if ParameterIs "$2" "user-add" || ParameterIs "$2" "useradd" || ParameterIs "$2" "add-user" || ParameterIs "$2" "adduser" ; then
        # Check any mysql parameter is set?
        if [[ $3 = "" ]]; then
            echo "[!] Please enter mysql user name to add."
            echo "[i] Usage: $tsite $1 $2 [user] [password]"
            exit 1 # false
        fi

        # Check username is valid?
        if [[ $3 = "debian-sys-maint" || $3 = "mysql.infoschema" || $3 = "mysql.session" || $3 = "mysql.sys" || $3 = "root" ]]; then
            echo "[!] The name '$3' is reserved for the system."
            exit 1 # false
        fi

        # Check user password parameter is set?
        if [[ $4 = "" ]]; then
            echo "[!] Please enter mysql user name to add."
            echo "[i] Usage: $tsite $1 $2 $3 [password]"
            exit 1 # false
        fi

        # Create user now!
        echo "[?] Please enter MYSQL root password for creating user."
        mysql -u root -p -e "CREATE USER '$3'@'localhost' IDENTIFIED BY '$4'; REVOKE SHOW DATABASES ON *.* FROM '$3';"
    
    exit 0
    # --- --- --- ---
    # mysql user-del
    # --- --- --- ---
    elif ParameterIs "$2" "user-del" || ParameterIs "$2" "userdel" || ParameterIs "$2" "del-user" || ParameterIs "$2" "deluser"; then
        # Check any mysql parameter is set?
        if [[ $3 = "" ]]; then
            echo "[!] Please enter mysql user name to delete."
            echo "[i] Usage: $tsite $1 $2 [user]"
            exit 1 # false
        fi

        # Check username is valid?
        if [[ $3 = "debian-sys-maint" || $3 = "mysql.infoschema" || $3 = "mysql.session" || $3 = "mysql.sys" || $3 = "root" ]]; then
            echo "[!] The name '$3' is reserved for the system."
            exit 1 # false
        fi

        # Delete user now!
        echo "[?] Please enter MYSQL root password for delete(drop) user."
        mysql -u root -p -e "DROP USER '$3'@'localhost';"

    exit 0
    # --- --- --- ---
    # mysql user-list
    # --- --- --- ---
    elif ParameterIs "$2" "user-list" || ParameterIs "$2" "userlist" || ParameterIs "$2" "list-user" || ParameterIs "$2" "listuser" ; then

        # Get user list
        mysql_output=$(mysql -u root -p -e "SELECT user FROM mysql.user;")
        user_list=$(echo "$mysql_output" | cut -f2- -d$'\n')
        while read -r line; do
            user=$(echo "$line" | awk '{print $1}')
            if [[ "$user" != "debian-sys-maint" && "$user" != "mysql.infoschema" && "$user" != "mysql.session" && "$user" != "mysql.sys" ]]; then
                echo "$line"
            fi
        done <<< "$user_list"

    exit 0
    # --- --- --- ---
    # mysql user-setpass
    # --- --- --- ---
    elif ParameterIs "$2" "user-setpass" ; then
        # Check user is set?
        if [[ $3 = "" ]]; then
            echo "[!] Please enter mysql user to change password."
            echo "[i] Usage: $tsite $1 $2 [user] [new password]"
            exit 1 # false
        fi

        # Check passowrd is set?
        if [[ $3 = "" ]]; then
            echo "[!] Please enter new password."
            echo "[i] Usage: $tsite $1 $2 $3 [new password]"
            exit 1 # false
        fi

        # Change password now!
        mysql -u root -p -e "UPDATE mysql.user SET authentication_string=null WHERE User='$3'; FLUSH PRIVILEGES; ALTER USER '$3'@'localhost' IDENTIFIED WITH caching_sha2_password BY '$4';";

    exit 0
    # --- --- --- ---
    # mysql db-add
    # --- --- --- ---
    elif ParameterIs "$2" "db-add" || ParameterIs "$2" "add-db" ; then
    
        # Check database is set?
        if [[ $3 = "" ]]; then
            echo "[!] Please enter database name to create."
            echo "[i] Usage: $tsite $1 $2 [db name]"
            exit 1
        fi

        # Check database name is available?
        if [[ $3 = "mysql" || $3 = "information_schema" || $3 = "performance_schema" || $3 = "sys" ]]; then
            echo "[!] The name '$3' is reserved for the system."
            exit 1
        fi

        # Create database now!
        mysql -u root -p -e "CREATE DATABASE $3;"

    exit 0
    # --- --- --- ---
    # mysql db-del
    # --- --- --- ---
    elif ParameterIs "$2" "db-del" || ParameterIs "$2" "del-db" ; then
    
        # Check database is set?
        if [[ $3 = "" ]]; then
            echo "[!] Please enter database name to delete."
            echo "[i] Usage: $tsite $1 $2 [db name]"
            exit 1
        fi

        # Check database name is available?
        if [[ $3 = "mysql" || $3 = "information_schema" || $3 = "performance_schema" || $3 = "sys" ]]; then
            echo "[!] The name '$3' is reserved for the system."
            exit 1
        fi

        # Create database now!
        mysql -u root -p -e "DROP DATABASE $3;"

    exit 0
    # --- --- --- ---
    # mysql db-list
    # --- --- --- ---
    elif ParameterIs "$2" "db-list" || ParameterIs "$2" "list-db"; then

        # Get user list
        mysql_output=$(mysql -u root -p -e "SHOW DATABASES;")
        database_list=$(echo "$mysql_output" | cut -f2- -d$'\n')
        while read -r line; do
            database=$(echo "$line" | awk '{print $1}')
            if [[ "$database" != "mysql" && "$database" != "information_schema" && "$database" != "performance_schema" && "$database" != "sys" ]]; then
                echo "$line"
            fi
        done <<< "$database_list"

    exit 0
    # --- --- --- ---
    # mysql db-export
    # --- --- --- ---
    elif ParameterIs "$2" "db-export" ; then
        # Check database name is set?
        if [[ $3 = "" ]]; then
            echo "[!] Please enter mysql database name."
            echo "[i] Usage: $tsite $1 $2 [db] [path]"
            exit 1 # false
        fi

        # Check file path is set?
        if [[ $4 = "" ]]; then
            echo "[!] Please enter file path."
            echo "[i] Usage: $tsite $1 $2 $3 [path]"
            exit 1 # false
        fi

        # Backup as SQL file now!
        mysqldump -u root -p "$3" > "$4"

    exit 0
    # --- --- --- ---
    # mysql db-import
    # --- --- --- ---
    elif ParameterIs "$2" "db-import" ; then
        # Check database name is set?
        if [[ $3 = "" ]]; then
            echo "[!] Please enter mysql database name."
            echo "[i] Usage: $tsite $1 $2 [db] [path]"
            exit 1 # false
        fi

        # Check file path is set?
        if [[ $4 = "" ]]; then
            echo "[!] Please enter file path."
            echo "[i] Usage: $tsite $1 $2 $3 [path]"
            exit 1 # false
        fi

        # $path
        if [[ ! -f $4 ]]; then
            echo "[!] '$4' file not found!"
            exit 1 # false
        fi

        # Backup as SQL file now!
        mysql -u root -p "$3" < "$4"

    exit 0
    # --- --- --- ---
    # mysql db-cancreate
    # --- --- --- ---
    elif ParameterIs "$2" "db-cancreate" ; then
        # Check user is set?
        if [[ $3 = "" ]]; then
            echo "[!] Please enter mysql user."
            echo "[i] Usage: $tsite $1 $2 [user]"
            exit 1 # false
        fi

        # Set permission now!
        mysql -u root -p -e "GRANT CREATE DATABASE ON *.* TO '$3'@'localhost'; FLUSH PRIVILEGES;"

    exit 0
    # --- --- --- ---
    # mysql db-cannotcreate
    # --- --- --- ---
    elif ParameterIs "$2" "db-cannotcreate" ; then
        # Check user is set?
        if [[ $3 = "" ]]; then
            echo "[!] Please enter mysql user."
            echo "[i] Usage: $tsite $1 $2 [user]"
            exit 1 # false
        fi

        # Set permission now!
        mysql -u root -p -e "REVOKE CREATE DATABASE ON *.* TO '$3'@'localhost'; FLUSH PRIVILEGES;"

    exit 0
    # --- --- --- ---
    # mysql db-allow
    # --- --- --- ---
    elif ParameterIs "$2" "db-allow" ; then
        # Check user is set?
        if [[ $3 = "" ]]; then
            echo "[!] Please enter mysql user."
            echo "[i] Usage: $tsite $1 $2 [user] [db]"
            exit 1 # false
        fi

        # Check database is set?
        if [[ $4 = "" ]]; then
            echo "[!] Please enter mysql database."
            echo "[i] Usage: $tsite $1 $2 $3 [db]"
            exit 1 # false
        fi

        # Set permission now!
        mysql -u root -p -e "GRANT ALL PRIVILEGES ON $4.* TO '$3'@'localhost'; FLUSH PRIVILEGES;"

    exit 0
    # --- --- --- ---
    # mysql db-disallow
    # --- --- --- ---
    elif ParameterIs "$2" "db-disallow" ; then
        # Check user is set?
        if [[ $3 = "" ]]; then
            echo "[!] Please enter mysql user."
            echo "[i] Usage: $tsite $1 $2 [user] [db]"
            exit 1 # false
        fi

        # Check database is set?
        if [[ $4 = "" ]]; then
            echo "[!] Please enter mysql database."
            echo "[i] Usage: $tsite $1 $2 $3 [db]"
            exit 1 # false
        fi

        # Set permission now!
        mysql -u root -p -e "REVOKE ALL PRIVILEGES ON $4.* FROM '$3'@'localhost'; FLUSH PRIVILEGES;"

    exit 0
    # --- --- --- ---
    # mysql [Unknown command]
    # --- --- --- ---
    else
        echo "[!] Unknown parameter for MYSQL: $2"
        echo "[i] Type \"$tsite mysql\" to see available MYSQL parameters."
        exit 1
    fi
    # --- --- --- ---

# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - help
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "help" || ParameterIs "$1" "" ; then
    Help
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - add
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "add"; then
    # Check domain is set?
    if [[ $2 = "" ]]; then
        echo "[!] Please enter domain name."
        echo "[i] Usage: $tsite add [new domain] (optional: fast)"
        exit 1 # false
    fi


    # $domain
    domain=$(echo "$2" | tr '[:upper:]' '[:lower:]')


    # Check domain name is acceptable?
    if ! domain_check "$domain"; then
        exit 1
    fi


    # Check if domain exists?
    if [[ -f "/etc/nginx/sites-available/$domain" ]]; then
        first_line=$(head -n 1 "/etc/nginx/sites-available/$domain")
        if [[ "$first_line" != "# Domain file generated by tsite tool. ( teteos.net/tsite )" ]]; then
            echo "[!] '/etc/nginx/sites-available/$domain' is exist but not created by TSite."
            unset first_line
            exit 1
        else
            echo "[!] Domain already exist."
            unset first_line
            exit 1
        fi
    fi


    # Add domain now!
    domain_add $domain


    # Check is success?
    if [ -f "/etc/nginx/sites-available/$domain" ]; then
        echo "[√] Domain added successfully." # Yes, don't wait "Return Finish".
    else
        echo "[!] Failed to creating domain file. (Write or permission error.)"
        exit 1
    fi


    # Create domain folder
    if [[ ! -d "/var/www/$domain/" ]]; then
        if ! domain_add_folder $domain; then
            exit 1
        fi
    fi


    # Fast mode
    if ParameterIs "$3" "fast"; then
        # Enable domain.
        ln -s /etc/nginx/sites-available/$domain "/etc/nginx/sites-enabled/$domain" &>/dev/null
        

        # Check is enabled?
        if [[ -f "/etc/nginx/sites-enabled/$domain" ]]; then
            echo "[√] Domain enabled successfully."
            #service_restart_all # Do not restart service here!
            #exit 0 # Do not exit here!
        else
            echo "[!] Domain cannot enabled. (Write or permission error.)"
            #exit 1 # Do not exit here!
        fi


        # Add to /etc/hosts now
        hosts_add $domain
    else
        # Domain not enabled note.
        echo "[i] Note: Domain is not enabled. Enable command: $tsite enable $domain"
    fi


    # Return Finish
    if [[ -f /etc/teteosnet-data/autoexec/tsite/add-after.sh ]]; then
        sleep 0.001 && bash /etc/teteosnet-data/autoexec/tsite/add-after.sh $domain
    fi
    #service_restart_nginx
    service_restart_all
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - del
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "del"; then
    # Check domain is set?
    if [[ $2 = "" ]]; then
        echo "[!] Please enter domain name."
        echo "[i] Usage: $tsite del [domain] (optional: all)"
        exit 1 # false
    fi


    # $domain
    domain=$(echo "$2" | tr '[:upper:]' '[:lower:]')


    # Check domain name is acceptable?
    if ! domain_check "$domain" "silent" "dotallow"; then
        echo "[!] Domain does not exist."
        exit 1
    fi


    # Check domain is exist?
    if [[ -f "/etc/nginx/sites-available/$domain" ]]; then
        first_line=$(head -n 1 "/etc/nginx/sites-available/$domain")
        if [[ "$first_line" != "# Domain file generated by tsite tool. ( teteos.net/tsite )" ]]; then
            echo "[!] Domain does not exist or not created by TSite."
            unset first_line
            exit 1
        fi
        unset first_line
    else
        # Domain file not exist, check folder if "all" parameter is set.
        if [[ $3 = "all" ]]; then
            if ! [[ -d "/var/www/$domain/" ]]; then
                echo "[!] Domain does not exist."
                exit 1 # false
            fi
        else
            echo "[!] Domain does not exist."
            exit 1 # false
        fi
    fi


    # Delete now
    rm -f "/etc/nginx/sites-available/$domain" &>/dev/null
    rm -f "/etc/nginx/sites-enabled/$domain" &>/dev/null
    if [[ -f "/etc/hosts" ]]; then
        if grep -q "$domain" /etc/hosts; then
            hosts_del "$domain" "silent"
        fi
        if [[ $3 = "all" ]]; then
            rm -rf /var/www/$domain && sleep 0.05
        fi
    fi


    # Check is failed?
    if ! [[ -f "/etc/nginx/sites-available/$domain" ]]; then
        if [[ $3 = "all" ]]; then
            if [[ -d "/var/www/$domain" ]]; then
                echo "[!] Domain folder cannot deleted. (Write or permission error.)"
                exit 1
            fi
        fi
    else
        echo "[!] Domain cannot deleted. (Write or permission error.)"
        exit 1
    fi


    # Return Finish
    echo "[√] Site deleted successfully."
    if [[ -f /etc/teteosnet-data/autoexec/tsite/del-after.sh ]]; then
        bash /etc/teteosnet-data/autoexec/tsite/del-after.sh $domain $3
    fi
    service_restart_nginx
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - list
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "list"; then
    # Get domain files list
    ListFiles=$(grep -l "^# Domain file generated by tsite tool. ( teteos.net/tsite )" /etc/nginx/sites-available/* | grep -v "/000-default")


    # Check any domain found?
    if [[ -z "$ListFiles" ]]; then
        echo "[i] No any domain found."
        exit 0
    fi


    # List domains now.
    if [[ "$2" == "names-only" || "$2" == "only-names" ]]; then
        
        for file in $ListFiles; do
            echo $(basename $file)
        done

    else
        echo ""
        echo "Domain$(printf '%*s' 40)Enabled?"
        echo "---------------------------------------------------------"
            
            
            for file in $ListFiles; do

                domain="$(basename $file)"
                enabled="no"
                
                if [[ -e "/etc/nginx/sites-enabled/$(basename $file)" ]]; then
                    enabled="yes"
                fi
                
                printf "%-45s %s\n" "$domain" "$enabled"

            done
        
        echo "---------------------------------------------------------"
        echo ""
    fi

    # Return Finish
    if [[ -f /etc/teteosnet-data/autoexec/tsite/list-after.sh ]]; then
        bash /etc/teteosnet-data/autoexec/tsite/list-after.sh $2
    fi
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - enable
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "enable"; then
    # Check domain is set?
    if [[ $2 = "" ]]; then
        echo "[!] Please enter domain name."
        echo "[i] Usage: $tsite enable [domain]"
        exit 1 # false
    fi


    # $domain
    domain=$(echo "$2" | tr '[:upper:]' '[:lower:]')


    # Check domain name is acceptable?
    if ! domain_check "$domain" "silent" "dotallow"; then
        echo "[!] Domain does not exist."
        exit 1
    fi


    # Check if domain exists?
    if [[ ! -f "/etc/nginx/sites-available/$domain" ]]; then
        echo "[!] Domain does not exist."
        exit 1
    fi
    first_line=$(head -n 1 "/etc/nginx/sites-available/$domain")
    if [[ "$first_line" != "# Domain file generated by tsite tool. ( teteos.net/tsite )" ]]; then
        echo "[!] Domain does not exist or not created by TSite."
        exit 1
    fi
    unset first_line


    # Check domain is already enabled?
    if [[ -f "/etc/nginx/sites-enabled/$2" ]]; then
        echo "[!] Domain is already enabled."
        exit 1
    fi


    # Enable now
    if ! domain_enable $domain; then
        exit 1
    fi


    # Return
    service_restart_nginx
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - disable
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "disable"; then
    # Check domain is set?
    if [[ $2 = "" ]]; then
        echo "[!] Please enter domain name."
        echo "[i] Usage: $tsite disable [domain]"
        exit 1 # false
    fi


    # $domain
    domain=$(echo "$2" | tr '[:upper:]' '[:lower:]')


    # Check domain name is acceptable?
    if ! domain_check "$domain" "silent" "dotallow"; then
        echo "[!] Domain does not exist."
        exit 1
    fi


    # Check if domain exists?
    if [[ ! -f "/etc/nginx/sites-available/$domain" ]]; then
        echo "[!] Domain does not exist."
        exit 1
    fi
    first_line=$(head -n 1 "/etc/nginx/sites-available/$domain")
    if [[ "$first_line" != "# Domain file generated by tsite tool. ( teteos.net/tsite )" ]]; then
        echo "[!] Domain does not exist or not created by TSite."
        exit 1
    fi
    unset first_line


    # Check domain is already disabled?
    if [[ ! -f "/etc/nginx/sites-enabled/$2" ]]; then
        echo "[!] Domain is already disabled."
        exit 1
    fi


    # Disable now
    unlink "/etc/nginx/sites-enabled/$2" && sleep 0.05


    # Check is success?
    if [[ -f "/etc/nginx/sites-enabled/$2" ]]; then
        echo "[!] Domain cannot disabled. (Write or permission error)"
        exit 1
    fi


    # Return
    echo "[√] Domain disabled successfully."
    #service_restart_nginx
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - sub-add
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "sub-add" || ParameterIs "$1" "add-sub"; then
    # Check domain is set?
    if [[ $2 = "" ]]; then
        echo "[!] Please enter domain name."
        echo "[i] Usage: $tsite $1 [domain] [new subdomain]"
        exit 1 # false
    fi


    # $domain
    domain=$(echo "$2" | tr '[:upper:]' '[:lower:]')


    # Check domain name is acceptable?
    if ! domain_check "$domain" "silent" "dotallow"; then
        echo "[!] Domain does not exist."
        exit 1
    fi


    # Check if domain exists?
    if [[ ! -f "/etc/nginx/sites-available/$domain" ]]; then
        echo "[!] Domain does not exist."
        exit 1
    fi
    first_line=$(head -n 1 "/etc/nginx/sites-available/$domain")
    if [[ "$first_line" != "# Domain file generated by tsite tool. ( teteos.net/tsite )" ]]; then
        echo "[!] Domain does not exist or not created by TSite."
        exit 1
    fi
    unset first_line


    # Check subdomain is set?
    if [[ $3 = "" ]]; then
        echo "[!] Please enter new subdomain name."
        echo "[i] Usage: $tsite $1 $domain [new subdomain]"
        exit 1
    fi


    # $subdomain and $fulldomain
    subdomain=$(echo "$3" | tr '[:upper:]' '[:lower:]')
    fulldomain="$subdomain.$domain"


    # Check subdomain is acceptable?
    if ! is_alphanumeric $subdomain; then
        echo "[!] Only alphanumeric accepted for subdomain name."
        exit 1
    elif [[ $subdomain = "www" ]]; then
        echo "[!] 'www' is already working and you can't modify."
        exit 1
    elif [[ $subdomain = $domain ]]; then
        echo "[!] You cannot add subdomain same with domain name."
        exit 1
    fi


    # Check subdomain is exists?
    if grep -q "server_name $subdomain.$domain;" "/etc/nginx/sites-available/$domain"; then
        echo "[!] Subdomain already exists."
        exit 1
    fi


    # Check #tsite:add_sub_domain_section line is set?
    if ! grep -q "#tsite:add_sub_domain_section" "/etc/nginx/sites-available/$domain"; then
        echo "[!] Domain file look like modified by another program or user!"
        echo "[i] Subdomain section not found. Make sure the '#tsite:add_sub_domain_section' line is present."
        exit 1
    fi

    # Start add subdomain now!
        # Get the template for "www." subdomain
        www_subdomain_template=$(sed -n '/# "www." subdomain/,/#tsite:add_sub_domain_section/p' "/etc/nginx/sites-available/$domain")

        # Replace "www." with the new subdomain name
        new_subdomain_template=$(echo "$www_subdomain_template" | sed "s/www\./$subdomain\./g")
        
        # Replace "_www" with the new subdomain name
        new_subdomain_template=$(echo "$new_subdomain_template" | sed "s/_www/$subdomain/g")
        
        
        # Remove the domain from server_name if it's the same as the subdomain
        new_subdomain_template=$(echo "$new_subdomain_template" | sed "s/ $domain;/;/g")

        # Escape slashes and newlines for the sed command
        new_subdomain_template_escaped=$(echo "$new_subdomain_template" | sed ':a;N;$!ba;s/\n/\\n/g;s/\//\\\//g')

        # Add new subdomain section
        sed -i "/#tsite:add_sub_domain_section/ s/.*/$new_subdomain_template_escaped/" "/etc/nginx/sites-available/$domain"


        # Add subdomain to hosts file (if standard domain added.)
        if [[ -f "/etc/hosts" ]]; then
            if grep -q "$domain" /etc/hosts; then
                hosts_add "$fulldomain" "silent"
            fi
        fi


        # Check if failed
        if ! grep -q "server_name $fulldomain;" "/etc/nginx/sites-available/$domain"; then
            echo "[!] Cannot add subdomain. (Write or permission error.)"
            exit 1
        fi


    # Create subdomain folder.
    subdomain_add_folder $domain $subdomain


    # Return Finish
    if [[ -f /etc/teteosnet-data/autoexec/tsite/sub-add-after.sh ]]; then
        sleep 0.001 && bash /etc/teteosnet-data/autoexec/tsite/sub-add-after.sh $domain $subdomain
    fi
    echo "[√] Subdomain added successfuly."
    service_restart_nginx
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - sub-del
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "sub-del" || ParameterIs "$1" "del-sub"; then
    # Check domain is set?
    if [[ $2 = "" ]]; then
        echo "[!] Please enter domain name."
        echo "[i] Usage: $tsite $1 [domain] [subdomain]"
        exit 1 # false
    fi


    # $domain
    domain=$(echo "$2" | tr '[:upper:]' '[:lower:]')


    # Check domain name is acceptable?
    if ! domain_check "$domain" "silent" "dotallow"; then
        echo "[!] Domain does not exist."
        exit 1
    fi


    # Check if domain exists?
    if [[ ! -f "/etc/nginx/sites-available/$domain" ]]; then
        echo "[!] Domain does not exist."
        exit 1
    fi
    first_line=$(head -n 1 "/etc/nginx/sites-available/$domain")
    if [[ "$first_line" != "# Domain file generated by tsite tool. ( teteos.net/tsite )" ]]; then
        echo "[!] Domain does not exist or not created by TSite."
        exit 1
    fi
    unset first_line


    # Check subdomain is set?
    if [[ $3 = "" ]]; then
        echo "[!] Please enter subdomain name."
        echo "[i] Usage: $tsite $1 $domain [subdomain]"
        return 1 # false
    fi


    # $subdomain
    subdomain=$(echo "$3" | tr '[:upper:]' '[:lower:]')


    # Check subdomain is acceptable?
    if ! is_alphanumeric $subdomain; then # and protecting "*" subdomain <3
        echo "[!] Subdomain does not exist." # custom error msg
        exit 1
    elif [[ $subdomain = "www" ]]; then
        echo "[!] 'www' is already working and you can't modify."
        exit 1
    elif [[ $subdomain = $domain ]]; then
        echo "[!] Subdomain does not exist." # custom error msg
        exit 1
    fi


    # Check subdomain is exists?
    if ! grep -q "server_name $subdomain.$domain;" "/etc/nginx/sites-available/$domain"; then
        if [[ $4 == "all" ]]; then
            if [[ ! -d "/var/www/$domain/$subdomain/" ]]; then
                echo "[!] Subdomain does not exist."
                exit 1
            fi
        else
            echo "[!] Subdomain does not exist."
            exit 1
        fi
        exit 1
    fi


    # Delete subdomain section now.
    sed -i "/# \"$subdomain.\" subdomain/,/^        \}/d" "/etc/nginx/sites-available/$domain"
    if [[ $4 == "all" ]]; then
        rm -rf "/var/www/$domain/$subdomain/" &>/dev/null
    fi

    # Check subdomain is exists?
    if grep -q "server_name $subdomain.$domain;" "/etc/nginx/sites-available/$domain"; then
        echo "[!] Cannot remove subdomain. (Write or permission error.)"
        exit 1
    fi


    # Remove subdomain from hosts file
    if grep -q "$subdomain.$domain" /etc/hosts; then
        hosts_del "$subdomain.$domain" "silent"
    fi
    

    # Return Finish
    if [[ -f /etc/teteosnet-data/autoexec/tsite/sub-del-after.sh ]]; then
        sleep 0.001 && bash /etc/teteosnet-data/autoexec/tsite/sub-del-after.sh $domain $subdomain $4
    fi
    echo "[√] Subdomain removed successfuly."
    service_restart_nginx
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - sub-list
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "sub-list" || ParameterIs "$1" "list-sub"; then
    # Check domain is set?
    if [[ $2 = "" ]]; then
        echo "[!] Please enter domain name."
        echo "[i] Usage: $tsite $1 [domain]"
        exit 1 # false
    fi

    # $domain
    domain=$(echo "$2" | tr '[:upper:]' '[:lower:]')

    # Check domain name is acceptable?
    if ! domain_check "$domain" "silent" "dotallow"; then
        echo "[!] Domain does not exist."
        exit 1
    fi

    # Check if domain exists?
    if [[ ! -f "/etc/nginx/sites-available/$domain" ]]; then
        echo "[!] Domain does not exist."
        exit 1
    fi
    first_line=$(head -n 1 "/etc/nginx/sites-available/$domain")
    if [[ "$first_line" != "# Domain file generated by tsite tool. ( teteos.net/tsite )" ]]; then
        echo "[!] Domain does not exist or not created by TSite."
        exit 1
    fi
    unset first_line


    echo ""
    echo "Subdomains for $domain"
    echo "---------------------------------------------------------"

    # Extract subdomains from nginx configuration
    subdomains=$(grep "server_name" "/etc/nginx/sites-available/$domain" | sed -n -E 's/^\s*server_name\s+(.*);$/\1/p')

    for subdomain in $subdomains; do
        if [[ "$subdomain" != "*" && "$subdomain" != "$domain" ]]; then
            subdomain_name="${subdomain%%.$domain}"
            echo "$subdomain_name"
        fi
    done

    echo "---------------------------------------------------------"
    echo ""
    
    # PLANNN: names-only

    # Return Finish
    if [[ -f /etc/teteosnet-data/autoexec/tsite/sub-list-after.sh ]]; then
        sleep 0.001 && bash /etc/teteosnet-data/autoexec/tsite/sub-list-after.sh $domain # add $3 after adding names-only
    fi

exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - copybin
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "copybin"; then
    # Copy "tsite" to sbin.
    if ! CopyToBin "tsite"; then
        exit 1
    fi
    

    # Return Finish
    if [[ -f "/etc/teteosnet/os/is_pilux_based" ]]; then
        echo "[√] Script added to this $PILUX_OS_NAME as command."
    else
        echo "[√] Script added to this system as command."
    fi
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - install
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "install" || ParameterIs "$1" "reinstall"; then
    # Check system is Ubuntu or PiluX right?
    distrib_id=`cat /etc/lsb-release | grep "DISTRIB_ID=" | sed "s/DISTRIB_ID=//g"`
    if ! [[ $distrib_id = "Ubuntu" || -f /etc/teteosnet/os/is_pilux_based && -d "/etc/apt" ]]; then
        echo "[!] Your OS is not supported. Only PiluX Based Distros and Ubuntu supported!"
        exit 1
    fi
    distrib_code=`cat /etc/lsb-release | grep "DISTRIB_CODENAME=" | sed "s/DISTRIB_CODENAME=//g"`


    # Warn user and wait 10 seconds before installation.
    echo ""
    welcome
    echo ""
    echo "--------------------------------------------------------------------"
    echo ""
    echo " ⚠️  Warning! If you currently have Nginx, PHP, and MySQL installed, "
    echo " all of YOUR DATA will be permanently DELETED during the process! "
    echo " Make sure to back up your data before proceeding!"
    echo ""
    echo " 🛠️  Initiating the reinstallation of Nginx, PHP, and MySQL!"
    echo ""
    echo "--------------------------------------------------------------------"
    echo ""
    for i in {2..1}; do
        echo -ne " 🚨 You have $i seconds to cancel by pressing CTRL + C.\r"
        sleep 1
    done
    echo " 🚨 You have 0 seconds to cancel by pressing CTRL + C."
    
    echo "" && echo "[i] Cleaning system..."
        service_stop_all


        echo " -  Removing Nginx (if exist)..."
        apt remove -y nginx* --purge &>/dev/null
        rm -rf "/etc/nginx"
        rm -f "/etc/ssl/certs/nginx-selfsigned.crt" &>/dev/null
        rm -f "/etc/ssl/certs/nginx-selfsigned.csr" &>/dev/null
        rm -f "/etc/ssl/private/nginx-selfsigned.key" &>/dev/null
        rm -rf "/var/www" &>/dev/null


        echo " -  Removing PHP (if exist)..."
        apt remove -y php* --purge &>/dev/null
        apt remove -y php-cli --purge &>/dev/null
        apt remove -y php-fpm --purge &>/dev/null
        rm -rf "/etc/php"


        echo " -  Removing Mysql (if exist)..."
        apt remove -y mysql* --purge &>/dev/null
        apt remove -y mysql-server --purge &>/dev/null
        deluser mysql &>/dev/null
        if id "mysql" &>/dev/null; then
            systemctl stop mysql &>/dev/null
            deluser mysql &>/dev/null
            if id "mysql" &>/dev/null; then
                echo "[!] Failed to remove linux user 'mysql'."
                exit 1
            fi
        fi
        delgroup mysql &>/dev/null
        rm -rf /etc/mysql/
        rm -rf /var/lib/mysql/
        

        echo " -  Executing APT autoremove and autoclean..."
        apt autoremove -y --purge &>/dev/null
        apt autoclean -y &>/dev/null


    echo "" && echo "[i] Configuring APT..."
        echo " -  Running dpkg --configure -a..."
        dpkg --configure -a &>/dev/null


        if ! [[ -f "/usr/bin/add-apt-repository" ]]; then
            echo " -  Installing add-apt-repository command"
            apt_install software-properties-common || exit 1
        fi


        # Ondrej repository
        repofile="/etc/apt/sources.list.d/ondrej-ubuntu"
        if ! [[ ( -f "$repofile-nginx-$distrib_code.sources" && -f "$repofile-php-$distrib_code.sources" ) || ( -f "$repofile-nginx-$distrib_code.list" && -f "$repofile-php-$distrib_code.list" ) ]]; then
            echo " -  Adding Repositorys"
            check_distro_supported_repo=$(curl -Is "https://ppa.launchpadcontent.net/ondrej/php/ubuntu/dists/$distrib_code" | head -n 1 | awk '{print $2}')
            if [ "$check_distro_supported_repo" != "404" ]; then
                if ! [[ -f "$repofile-nginx-$distrib_code.source" || -f "$repofile-nginx-$distrib_code.list" ]]; then
                    echo " - -- Adding Nginx repo (ondrej/nginx)"
                    add-apt-repository ppa:ondrej/nginx -y &>/dev/null
                fi
                if ! [[ -f "$repofile-php-$distrib_code.source" || -f "$repofile-php-$distrib_code.list" ]]; then
                    echo " - -- Adding PHP repo (ondrej/php)"
                    add-apt-repository ppa:ondrej/php -y &>/dev/null
                fi
            else
                echo " - -- Ignored. May be Ondrej repo is not support your OS :("
            fi
        fi


        echo " -  Updating APT cache"
        apt update -y &>/dev/null


    echo "" && echo "[i] Installing Web Server..."
        echo " -  Installing Nginx"
        apt_reinstall nginx || exit 1
        #apt_reinstall_nocheck nginx-extras || exit 1
        if [[ ! -f "/etc/nginx/nginx.conf" || ! -d "/var/www" ]]; then
            echo "[!] Failed to install Nginx."
            exit 1
        fi
        sed -i 's/# server_tokens off;/server_tokens off;/' /etc/nginx/nginx.conf
        rm -rf "/etc/nginx/sites-enabled/"
        rm -rf "/etc/nginx/sites-available/"
        service_restart_nginx # Restart nginx here! For hide Nginx version if server is live in public!
        chown -R www-data:www-data "/var/www" &>/dev/null
        find "/var/www" -type d -exec chmod 750 {} \; &>/dev/null
        find "/var/www" -type f -exec chmod 640 {} \; &>/dev/null
        rm -rf "/var/www/html/"


        echo " -  Installing PHP"
        apt_reinstall php-fpm php-cli || exit 1
        if [[ ! -f "/usr/bin/php" ]]; then
            echo "[!] Failed to install a package: php."
            exit 1
        fi
        php_version=$(php -r "echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION;")
        if [[ $php_version < "8" ]]; then
            echo "[!] PHP version unsupported. TSite requires PHP 8+."
            echo "[i] Make sure your distro(or apt repos) supporting latest PHP version."
            exit 1
        fi
        sed -i "s/expose_php.*/expose_php = off/g" /etc/php/$php_version/cli/php.ini &>/dev/null
        service_restart_php # Restart php here! For hide Nginx version if server is live in public!

        domain_add "000-default"
        domain_add_folder "000-default"
        domain_enable "000-default" "silent"
        service_restart_nginx # Restart nginx here! For start index default site!


        echo " -  Installing PHP Modules"
        echo " - -- mysql"
        apt_install php-mysql || exit 1
        echo " - -- json"
        apt_install php-json || exit 1
        echo " - -- xml (dom)"
        apt_install php-xml || exit 1
        echo " - -- curl"
        apt_install php-curl || exit 1
        echo " - -- mbstring"
        apt_install php-mbstring || exit 1
        echo " - -- gd"
        apt_install php-gd || exit 1
        echo " - -- gmp"
        apt_install php-gmp || exit 1
        echo " - -- imap"
        apt_install php-imap || exit 1
        echo " - -- zip"
        apt_install php-zip || exit 1


        echo " -  Installing MySQL Server"
        apt_reinstall mysql-server || exit 1
        while true; do
            echo -n "[?] Please enter new password for 'mysql root user': "
            read -s mysql_new_password

            if [[ -z "$mysql_new_password" ]]; then
                echo "The password cannot be empty. Please try again."
            else
                break
            fi
        done
        command="UPDATE mysql.user SET authentication_string=null WHERE User='root'; FLUSH PRIVILEGES; ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '$mysql_new_password';"
        if mysql -e "$command"; then
            echo " -- Pass update ok."
        else
            echo " -- Pass update fail. Please try execute 'mysql_secure_installation' after installation."
        fi
    
    echo "" && echo "[i] Installing Tools..."
        #if [[ ! -f "/usr/bin/composer" ]]; then
            echo " -  Installing Composer"
            php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" &>/dev/null
            php composer-setup.php &>/dev/null
            php -r "unlink('composer-setup.php');" &>/dev/null
            rm -f "/usr/bin/composer"
            mv composer.phar "/usr/bin/composer" &>/dev/null
        #fi


        echo " -  Installing Pip"
        if [[ ! -f "/usr/bin/pip" ]]; then
            apt_install_nocheck pip || exit 1
        fi
        python3 -m pip install --upgrade pip &>/dev/null


        if [[ ! -f "/usr/bin/git" ]]; then
            echo " -  Installing Git"
            apt_install git || exit 1
        fi


        if ! [[ -f "/usr/bin/certbot" ]]; then
            echo " -  Installing Certbot"
            apt_install certbot python3-certbot-nginx python3-acme || exit 1
            echo " - -- dns-cloudflare"
            apt_install python3-certbot-dns-cloudflare || exit 1
            echo " - -- dns-digitalocean"
            apt_install python3-certbot-dns-digitalocean || exit 1
            echo " - -- dns-google"
            apt_install python3-certbot-dns-google || exit 1
        fi


    echo "" && echo "[i] Copying TSite to system..."
        CopyToBin "tsite"
        mkdir -p /etc/teteosnet-data/autoexec/tsite/
        if [[ ! -f /etc/teteosnet-data/autoexec/tsite/add-after.sh ]]; then
        	if [[ -f autoexec/add-after.sh ]]; then
        		cp "autoexec/add-after.sh" "/etc/teteosnet-data/autoexec/tsite/add-after.sh"
        	else
        	        echo '#!/bin/bash' > /etc/teteosnet-data/autoexec/tsite/add-after.sh
			echo '# executed by TSite after adding site(domain)' >> /etc/teteosnet-data/autoexec/tsite/add-after.sh
			echo "" >>/etc/teteosnet-data/autoexec/tsite/add-after.sh
			echo 'if [[ -f "/etc/teteosnet-data/regs/devmode" && `cat /etc/teteosnet-data/regs/devmode` == "true" ]]; then' >> /etc/teteosnet-data/autoexec/tsite/add-after.sh
			echo 'echo "[d] Executing /etc/teteosnet-data/autoexec/tsite/add-after.sh"' >> /etc/teteosnet-data/autoexec/tsite/add-after.sh
			echo 'fi' >> /etc/teteosnet-data/autoexec/tsite/add-after.sh
			echo "" >>/etc/teteosnet-data/autoexec/tsite/add-after.sh
        	fi

        fi
        if [[ ! -f /etc/teteosnet-data/autoexec/tsite/del-after.sh ]]; then
        	if [[ -f autoexec/del-after.sh ]]; then
        		cp "autoexec/del-after.sh" "/etc/teteosnet-data/autoexec/tsite/del-after.sh"
        	else
        	        echo '#!/bin/bash' > /etc/teteosnet-data/autoexec/tsite/del-after.sh
			echo '# executed by TSite after deleting site(domain)' >> /etc/teteosnet-data/autoexec/tsite/del-after.sh
			echo "" >>/etc/teteosnet-data/autoexec/tsite/del-after.sh
			echo 'if [[ -f "/etc/teteosnet-data/regs/devmode" && `cat /etc/teteosnet-data/regs/devmode` == "true" ]]; then' >> /etc/teteosnet-data/autoexec/tsite/del-after.sh
			echo 'echo "[d] Executing /etc/teteosnet-data/autoexec/tsite/del-after.sh"' >> /etc/teteosnet-data/autoexec/tsite/del-after.sh
			echo 'fi' >> /etc/teteosnet-data/autoexec/tsite/del-after.sh
			echo "" >>/etc/teteosnet-data/autoexec/tsite/del-after.sh
        	fi
        fi
        if [[ ! -f /etc/teteosnet-data/autoexec/tsite/list-after.sh ]]; then
        	if [[ -f autoexec/list-after.sh ]]; then
        		cp "autoexec/list-after.sh" "/etc/teteosnet-data/autoexec/tsite/list-after.sh"
        	else
        	        echo '#!/bin/bash' > /etc/teteosnet-data/autoexec/tsite/list-after.sh
			echo '# executed by TSite after listing sites(domains)' >> /etc/teteosnet-data/autoexec/tsite/list-after.sh
			echo "" >>/etc/teteosnet-data/autoexec/tsite/list-after.sh
			echo 'if [[ -f "/etc/teteosnet-data/regs/devmode" && `cat /etc/teteosnet-data/regs/devmode` == "true" ]]; then' >> /etc/teteosnet-data/autoexec/tsite/list-after.sh
			echo 'echo "[d] Executing /etc/teteosnet-data/autoexec/tsite/list-after.sh"' >> /etc/teteosnet-data/autoexec/tsite/list-after.sh
			echo 'fi' >> /etc/teteosnet-data/autoexec/tsite/list-after.sh
			echo "" >>/etc/teteosnet-data/autoexec/tsite/list-after.sh
        	fi
        fi
        if [[ ! -f /etc/teteosnet-data/autoexec/tsite/sub-add-after.sh ]]; then
        	if [[ -f autoexec/sub-add-after.sh ]]; then
        		cp "autoexec/sub-add-after.sh" "/etc/teteosnet-data/autoexec/tsite/sub-add-after.sh"
        	else
        	        echo '#!/bin/bash' > /etc/teteosnet-data/autoexec/tsite/sub-add-after.sh
			echo '# executed by TSite after adding subdomain' >> /etc/teteosnet-data/autoexec/tsite/sub-add-after.sh
			echo "" >>/etc/teteosnet-data/autoexec/tsite/sub-add-after.sh
			echo 'if [[ -f "/etc/teteosnet-data/regs/devmode" && `cat /etc/teteosnet-data/regs/devmode` == "true" ]]; then' >> /etc/teteosnet-data/autoexec/tsite/sub-add-after.sh
			echo 'echo "[d] Executing /etc/teteosnet-data/autoexec/tsite/sub-add-after.sh"' >> /etc/teteosnet-data/autoexec/tsite/sub-add-after.sh
			echo 'fi' >> /etc/teteosnet-data/autoexec/tsite/sub-add-after.sh
			echo "" >>/etc/teteosnet-data/autoexec/tsite/sub-add-after.sh
        	fi

        fi
        if [[ ! -f /etc/teteosnet-data/autoexec/tsite/sub-del-after.sh ]]; then
        	if [[ -f autoexec/sub-del-after.sh ]]; then
        		cp "autoexec/sub-del-after.sh" "/etc/teteosnet-data/autoexec/tsite/sub-del-after.sh"
        	else
        	        echo '#!/bin/bash' > /etc/teteosnet-data/autoexec/tsite/sub-del-after.sh
			echo '# executed by TSite after deleting subdomain' >> /etc/teteosnet-data/autoexec/tsite/sub-del-after.sh
			echo "" >>/etc/teteosnet-data/autoexec/tsite/sub-del-after.sh
			echo 'if [[ -f "/etc/teteosnet-data/regs/devmode" && `cat /etc/teteosnet-data/regs/devmode` == "true" ]]; then' >> /etc/teteosnet-data/autoexec/tsite/sub-del-after.sh
			echo 'echo "[d] Executing /etc/teteosnet-data/autoexec/tsite/sub-del-after.sh"' >> /etc/teteosnet-data/autoexec/tsite/sub-del-after.sh
			echo 'fi' >> /etc/teteosnet-data/autoexec/tsite/sub-del-after.sh
			echo "" >>/etc/teteosnet-data/autoexec/tsite/sub-del-after.sh
        	fi

        fi
        if [[ ! -f /etc/teteosnet-data/autoexec/tsite/sub-list-after.sh ]]; then
        	if [[ -f autoexec/sub-list-after.sh ]]; then
        		cp "autoexec/sub-list-after.sh" "/etc/teteosnet-data/autoexec/tsite/sub-list-after.sh"
        	else
        	        echo '#!/bin/bash' > /etc/teteosnet-data/autoexec/tsite/sub-list-after.sh
			echo '# executed by TSite after listing subdomains' >> /etc/teteosnet-data/autoexec/tsite/sub-list-after.sh
			echo "" >>/etc/teteosnet-data/autoexec/tsite/sub-list-after.sh
			echo 'if [[ -f "/etc/teteosnet-data/regs/devmode" && `cat /etc/teteosnet-data/regs/devmode` == "true" ]]; then' >> /etc/teteosnet-data/autoexec/tsite/sub-list-after.sh
			echo 'echo "[d] Executing /etc/teteosnet-data/autoexec/tsite/sub-list-after.sh"' >> /etc/teteosnet-data/autoexec/tsite/sub-list-after.sh
			echo 'fi' >> /etc/teteosnet-data/autoexec/tsite/sub-list-after.sh
			echo "" >>/etc/teteosnet-data/autoexec/tsite/sub-list-after.sh
        	fi

        fi
        echo "true">/etc/nginx/installed_by_tsite

    echo "" && echo "[i] Done! [Service startup commands executed.]"
    service_firewall_configure
    service_restart_all
    echo ""
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - restart
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "restart" || ParameterIs "$1" "start" ; then
    if [[ $2 = "" ]]; then
        echo "[i] Restarting services..."
        if ! service_restart_all; then
            exit 1 # false
        fi
    fi
    if [[ $2 = "n" || $2 = "nginx" ]]; then
        echo "[i] Restarting Nginx."
        if ! service_restart_nginx; then
            exit 1 # false
        fi
    fi
    if [[ $2 = "p" || $2 = "php" ]]; then
        echo "[i] Restarting PHP."
        if ! service_restart_php; then
            exit 1 # false
        fi
    fi
    if [[ $2 = "m" || $2 = "mysql" ]]; then
        echo "[i] Restarting Mysql."
        systemctl restart mysql &>/dev/null
    fi
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - stop
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "stop" || ParameterIs "$1" "kill" ; then
    echo "[i] Stopping Services..."
    echo "[i] Note: For start services again, start command: '$tsite restart'"
    service_stop_all
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - firewall
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "firewall"; then
    service_firewall_configure
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - hosts-add
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "hosts-add" || ParameterIs "$1" "host-add" || ParameterIs "$1" "hostsadd" || ParameterIs "$1" "hostadd" ; then
    # Check domain is set?
    if [[ $2 = "" ]]; then
        echo "[!] Please enter domain name."
        echo "[i] Usage: $tsite $1 [domain]"
        exit 1 # false
    fi


    # $domain
    domain=$(echo "$2" | tr '[:upper:]' '[:lower:]')


    # Check domain name is acceptable?
    if ! domain_check "$domain" "silent" "dotallow"; then
        echo "[!] Domain does not exist."
        exit 1
    fi


    # Check if domain exists?
    if [[ ! -f "/etc/nginx/sites-available/$domain" ]]; then
        echo "[!] Domain does not exist."
        exit 1
    fi
    first_line=$(head -n 1 "/etc/nginx/sites-available/$domain")
    if [[ "$first_line" != "# Domain file generated by tsite tool. ( teteos.net/tsite )" ]]; then
        echo "[!] Domain does not exist or not created by TSite."
        exit 1
    fi
    unset first_line


    # Add domain from hosts file now. --- If failed, exit with code 1.
    if ! hosts_add "$domain"; then
        exit 1
    fi
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - hosts-del
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "hosts-del" || ParameterIs "$1" "host-del" || ParameterIs "$1" "hostsdel" || ParameterIs "$1" "hostdel" ; then
    # Check domain is set?
    if [[ $2 = "" ]]; then
        echo "[!] Please enter domain name."
        echo "[i] Usage: $tsite $1 [domain]"
        exit 1 # false
    fi


    # $domain
    domain=$(echo "$2" | tr '[:upper:]' '[:lower:]')


    # Check domain name is acceptable?
    if ! domain_check "$domain" "silent" "dotallow"; then
        echo "[!] Domain does not exist."
        exit 1
    fi


    # Check if domain exists?
    if [[ ! -f "/etc/nginx/sites-available/$domain" ]]; then
        echo "[!] Domain does not exist."
        exit 1
    fi
    first_line=$(head -n 1 "/etc/nginx/sites-available/$domain")
    if [[ "$first_line" != "# Domain file generated by tsite tool. ( teteos.net/tsite )" ]]; then
        echo "[!] Domain does not exist or not created by TSite."
        exit 1
    fi
    unset first_line
    

    # Delete domain from hosts file now. --- If failed, exit with code 1.
    if ! hosts_del "$2"; then
        exit 1
    fi
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - https-on
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "https-on" || ParameterIs "$1" "ssl-on" || ParameterIs "$1" "httpson" || ParameterIs "$1" "sslon"; then
    # Check domain is set?
    if [[ $2 = "" ]]; then
        echo "[!] Please enter domain name."
        echo "[i] Usage: $tsite $1 [domain]"
        exit 1 # false
    fi


    # $domain
    domain=$(echo "$2" | tr '[:upper:]' '[:lower:]')


    # Check domain name is acceptable?
    if ! domain_check "$domain" "silent" "dotallow"; then
        echo "[!] Domain does not exist."
        exit 1
    fi


    # Check if domain exists?
    if [[ ! -f "/etc/nginx/sites-available/$domain" ]]; then
        echo "[!] Domain does not exist."
        exit 1
    fi
    first_line=$(head -n 1 "/etc/nginx/sites-available/$domain")
    if [[ "$first_line" != "# Domain file generated by tsite tool. ( teteos.net/tsite )" ]]; then
        echo "[!] Domain does not exist or not created by TSite."
        exit 1
    fi
    unset first_line


    # SSL mode on now. --- If failed, exit with code 1.
    if ! ssl_on "$domain"; then
        exit 1
    fi

    # Restart service
    service_restart_nginx
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - https-off
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "https-off" || ParameterIs "$1" "ssl-off" || ParameterIs "$1" "httpsoff" || ParameterIs "$1" "ssloff" ; then
    # Check domain is set?
    if [[ $2 = "" ]]; then
        echo "[!] Please enter domain name."
        echo "[i] Usage: $tsite $1 [domain]"
        exit 1 # false
    fi


    # $domain
    domain=$(echo "$2" | tr '[:upper:]' '[:lower:]')


    # Check domain name is acceptable?
    if ! domain_check "$domain" "silent" "dotallow"; then
        echo "[!] Domain does not exist."
        exit 1
    fi


    # Check if domain exists?
    if [[ ! -f "/etc/nginx/sites-available/$domain" ]]; then
        echo "[!] Domain does not exist."
        exit 1
    fi
    first_line=$(head -n 1 "/etc/nginx/sites-available/$domain")
    if [[ "$first_line" != "# Domain file generated by tsite tool. ( teteos.net/tsite )" ]]; then
        echo "[!] Domain does not exist or not created by TSite."
        exit 1
    fi
    unset first_line


    # SSL mode off now. --- If failed, exit with code 1.
    if ! ssl_off "$2"; then
        exit 1
    fi


    # Restart service
    service_restart_nginx
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - ssl-signself
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "ssl-signself" || ParameterIs "$1" "ssl-selfsign" ; then
    echo " -  Generating self-signed key and certificate pair with OpenSSL (365 day, rsa:2048)..."
    openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt
    if ! [[ -f "/etc/ssl/private/nginx-selfsigned.key" && -f "/etc/ssl/certs/nginx-selfsigned.crt" ]]; then
        echo "[!] Cannot generated self-sign key."
        exit 1
    fi


    echo " -  Generating CSR file..."
    openssl x509 -x509toreq -in /etc/ssl/certs/nginx-selfsigned.crt -signkey /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.csr &>/dev/null
    if ! [[ -f "/etc/ssl/certs/nginx-selfsigned.csr" ]]; then
        echo "[!] Cannot generate CSR file."
        rm -rf /etc/ssl/certs/nginx-selfsigned.crt
        rm -rf /etc/ssl/private/nginx-selfsigned.key
        exit 1
    fi


    echo " -  Generating DH parameters..."
    openssl dhparam -dsaparam -out /etc/nginx/dhparam.pem 4096 &>/dev/null
    if ! [[ -f "/etc/nginx/dhparam.pem" ]]; then
        echo "[!] Cannot generated self-sign key."
        rm -rf /etc/ssl/private/nginx-selfsigned.crt
        rm -rf /etc/ssl/private/nginx-selfsigned.key
        exit 1
    fi


    echo " -  Creating self-signed.conf and ssl-params.conf..."
    echo "ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;">/etc/nginx/snippets/self-signed.conf
    echo "ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;">>/etc/nginx/snippets/self-signed.conf
    echo "ssl_protocols TLSv1.3;">/etc/nginx/snippets/ssl-params.conf
    echo "ssl_prefer_server_ciphers on;">>/etc/nginx/snippets/ssl-params.conf
    echo "ssl_dhparam /etc/nginx/dhparam.pem;">>/etc/nginx/snippets/ssl-params.conf
    echo "ssl_ciphers EECDH+AESGCM:EDH+AESGCM;">>/etc/nginx/snippets/ssl-params.conf
    echo "ssl_ecdh_curve secp384r1;">>/etc/nginx/snippets/ssl-params.conf
    echo "ssl_session_timeout  10m;">>/etc/nginx/snippets/ssl-params.conf
    echo "ssl_session_cache shared:SSL:10m;">>/etc/nginx/snippets/ssl-params.conf
    echo "ssl_session_tickets off;">>/etc/nginx/snippets/ssl-params.conf
    echo "ssl_stapling on;">>/etc/nginx/snippets/ssl-params.conf
    echo "ssl_stapling_verify on;">>/etc/nginx/snippets/ssl-params.conf
    echo "resolver 1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4 valid=300s;">>/etc/nginx/snippets/ssl-params.conf
    echo "resolver_timeout 5s;">>/etc/nginx/snippets/ssl-params.conf
    echo "# Disable strict transport security for now. You can uncomment the following">>/etc/nginx/snippets/ssl-params.conf
    echo "# line if you understand the implications.">>/etc/nginx/snippets/ssl-params.conf
    echo "#add_header Strict-Transport-Security \"max-age=63072000; includeSubDomains; preload\";">>/etc/nginx/snippets/ssl-params.conf
    echo "add_header X-Frame-Options DENY;">>/etc/nginx/snippets/ssl-params.conf
    echo "add_header X-Content-Type-Options nosniff;">>/etc/nginx/snippets/ssl-params.conf
    echo "add_header X-XSS-Protection \"1; mode=block\";">>/etc/nginx/snippets/ssl-params.conf
    if ! [[ -f "/etc/nginx/snippets/ssl-params.conf" && -f "/etc/nginx/snippets/self-signed.conf" ]]; then
        echo "[!] Cannot generated self-sign key."
        exit 1
    fi


    echo " -  Enabling SSL for default site."
    if [[ -f "/etc/nginx/sites-available/000-default" ]]; then
        ssl_on 000-default
    fi


    # Return Finish
    service_firewall_configure
    echo "[√] Generating self-signed key successfully."
    echo "[i] - Generated files:"
    echo "      /etc/ssl/private/nginx-selfsigned.key (ssl-signself-showkey)"
    echo "      /etc/ssl/certs/nginx-selfsigned.crt (ssl-signself-showcrt)"
    echo "      /etc/ssl/certs/nginx-selfsigned.csr (ssl-signself-showcsr)"
    echo "      /etc/nginx/dhparam.pem"
    service_restart_nginx
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - ssl-signself-renew
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "ssl-signself-renew" || ParameterIs "$1" "ssl-selfsign-renew" ; then
    # Check signself files is exist?
    if ! [ -f "/etc/ssl/certs/nginx-selfsigned.crt" ]; then
        echo "[!] nginx-selfsigned.crt not found."
        exit 1
    fi
    if ! [ -f "/etc/ssl/certs/nginx-selfsigned.csr" ]; then
        echo "[!] nginx-selfsigned.csr not found."
        exit 1
    fi
    if ! [ -f "/etc/ssl/private/nginx-selfsigned.key" ]; then
        echo "[!] /etc/ssl/private/nginx-selfsigned.key not found."
        exit 1
    fi


    # Backup current nginx-selfsigned.crt
    CRT_OLD_MD5=`md5sum /etc/ssl/certs/nginx-selfsigned.crt`
    cp /etc/ssl/certs/nginx-selfsigned.crt /etc/ssl/certs/nginx-selfsigned.backup.crt &>/dev/null
    cp /etc/ssl/certs/nginx-selfsigned.csr /etc/ssl/certs/nginx-selfsigned.backup.csr &>/dev/null
    if ! [ -f "/etc/ssl/certs/nginx-selfsigned.backup.crt" && "/etc/ssl/certs/nginx-selfsigned.backup.csr" ]; then
        echo "[!] Backup failed for nginx-selfsigned.crt (Write or permission error..)"
        exit 1
    fi


    # Renew current signself now.
    openssl x509 -x509toreq -in /etc/ssl/certs/nginx-selfsigned.crt -signkey /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.csr &>/dev/null
    if ! [ -f "/etc/ssl/certs/nginx-selfsigned.csr" ]; then
        echo "[!] Failed to renew Self-Sign SSL. File cannot created. (Write or permission error..)"
        exit 1
    fi
    openssl x509 -req -days 365 -in /etc/ssl/certs/nginx-selfsigned.csr -signkey /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt &>/dev/null
    CRT_NEW_MD5=`md5sum /etc/ssl/certs/nginx-selfsigned.crt`
    if [[ $CRT_NEW_MD5 = $CRT_OLD_MD5 ]]; then
        echo "[!] Failed to renew Self-Sign SSL."
        rm -f /etc/ssl/certs/nginx-selfsigned.crt || mv /etc/ssl/certs/nginx-selfsigned.backup.crt /etc/ssl/certs/nginx-selfsigned.crt ||
        rm -f /etc/ssl/certs/nginx-selfsigned.csr || mv /etc/ssl/certs/nginx-selfsigned.backup.csr /etc/ssl/certs/nginx-selfsigned.csr ||
        exit 1
    fi


    # Return Finish
    echo "[√] Renewing self-signed key successfully."
    service_restart_nginx
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - ssl-signself-remove
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "ssl-signself-remove" || ParameterIs "$1" "ssl-selfsign-remove" ; then
    rm -f /etc/ssl/certs/nginx-selfsigned.crt
    rm -f /etc/ssl/certs/nginx-selfsigned.csr
    rm -f /etc/ssl/private/nginx-selfsigned.key
    rm -f /etc/nginx/snippets/self-signed.conf
    for file in "/etc/nginx/sites-available"/*; do
        if [ -f "$file" ]; then
            if grep -q "self-signed.conf" "$file"; then
                ssl_off "$(basename "$file")" "silent"
            fi
        fi
    done
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - ssl-signself-showcrt
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "ssl-signself-showcrt" || ParameterIs "$1" "ssl-selfsign-showcrt" ; then
    if ! [ -f "/etc/ssl/certs/nginx-selfsigned.crt" ]; then
        echo "[!] nginx-selfsigned.crt not found."
        exit 1
    fi
    cat /etc/ssl/certs/nginx-selfsigned.crt
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - ssl-signself-showcsr
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "ssl-signself-showcst" || ParameterIs "$1" "ssl-selfsign-showcst" ; then
    if ! [ -f "/etc/ssl/certs/nginx-selfsigned.csr" ]; then
        echo "[!] nginx-selfsigned.csr not found."
        exit 1
    fi
    cat /etc/ssl/certs/nginx-selfsigned.csr
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - ssl-signself-showkey
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "ssl-signself-showkey" || ParameterIs "$1" "ssl-selfsign-showkey" ; then
    if ! [ -f "/etc/ssl/private/nginx-selfsigned.key" ]; then
        echo "[!] nginx-selfsigned.key not found."
        exit 1
    fi
    cat /etc/ssl/private/nginx-selfsigned.key
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - phpini
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "phpini" || ParameterIs "$1" "php.ini"; then
    php_ini_path="/etc/php/$php_version/cli/php.ini"
    if [[ -f "$php_ini_path" ]]; then
        if [[ -n "$XDG_CURRENT_DESKTOP" ]]; then
            xdg-open "$php_ini_path" &>/dev/null
            exit 0
        else
            if hash nano 2>/dev/null; then
                nano "$php_ini_path"
                exit 0
            else
                echo "[!] 'nano' is not installed!"
                exit 1
            fi
        fi
    else
        echo "[!] $php_ini_path file not found!"
        exit 1
    fi
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - cd
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
elif ParameterIs "$1" "cd"; then
    # Check domain is set?
    if [[ $2 = "" ]]; then
        echo "[!] Please enter domain name."
        echo "[i] Usage: $tsite cd [domain]"
        exit 1 # false
    fi


    # $domain
    domain=$(echo "$2" | tr '[:upper:]' '[:lower:]')


    # Check domain name is acceptable?
    if ! domain_check "$domain" "silent" "dotallow"; then
        echo "[!] Domain does not exist."
        exit 1
    fi


    # Check if domain exists?
    if [[ ! -f "/etc/nginx/sites-available/$domain" ]]; then
        echo "[!] Domain does not exist."
        exit 1
    fi
    first_line=$(head -n 1 "/etc/nginx/sites-available/$domain")
    if [[ "$first_line" != "# Domain file generated by tsite tool. ( teteos.net/tsite )" ]]; then
        echo "[!] Domain does not exist or not created by TSite."
        exit 1
    fi
    unset first_line
    

    # cd
    random_string=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 8 ; echo '')
    mkdir -p "/tmp/tsite_cd_$random_string/"
    if [[ ! -d "/tmp/tsite_cd_$random_string/" ]]; then
        echo "[!] Failed to change directory. Write to /tmp permission required."
        exit 1
    fi
    echo "cd /var/www/$2">/tmp/tsite_cd_$random_string/cd.sh
    echo "\$SHELL">>/tmp/tsite_cd_$random_string/cd.sh
    chmod +x /tmp/tsite_cd_$random_string/cd.sh
    exec /tmp/tsite_cd_$random_string/cd.sh
    unset randomstring
    rm -rf /tmp/tsite_cd_*
exit 0
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# - [Unknown command]
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
else
    echo "[!] Unknown parameter: $1"
    echo "[i] Type \"$tsite help\" to see available parameters."
    exit 1
fi
# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
# Happy ending <3 Code by @hasanmerkit ( teteos.net/u/hasanmerkit )
