#!/bin/sh
set -f
#################################################################################
#
#   Flexy
# ------------------
#
# Copyright 2012-2021, Ganapathi Chidambaram
# Copyright 2007-2021, Buzzworks Business Services Pvt Ltd
#
# Website  : https://buzzworks.com
# Blog     : http://linux-audit.com
# GitHub   : https://github.com/buzzworks
#
# Flexy comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
# welcome to redistribute it under the terms of the GNU General Public License.
# See LICENSE file for usage of this software.
#
#################################################################################

getVirtualHostDir(){

    APACHECTLBIN=$(which apachectl 2> /dev/null)
    if [ $? -eq 0 ]; then
       VHOSTFILES=$(${APACHECTLBIN} -S 2> /dev/null |  ${EGREPBINARY} '(80|443) namevhost' | ${CUTBINARY} -f2 -d\( | ${CUTBINARY} -f1 -d\: | ${SORTBINARY} | ${UNIQBINARY})
    else
        ExitClean
    fi
    local VHOST=()
    for f in ${VHOSTFILES}; do
        VHOST+=($(${GREPBINARY} "<Directory" $f | ${GREPBINARY} -v "cgi-bin" | ${AWKBINARY} '{print $2}' | ${TRBINARY} -d '"'  | ${AWKBINARY} -F\> '{print $1}' ))
    done
    VHOST_DIR=($(echo "${VHOST[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
    return;
}

clearDialLines()
{
    GetAsteriskChannels

    if [  -z "${CHANNELS// }" ] ; then return; fi

    ${MYSQLCLIENTBINARY} -h $DB_HOST -u$DB_USERNAME -p$DB_PASSWORD $DB_DATABASE \
        -e "UPDATE diallines SET status = 'Free',conf='',number='',uniqueid='',src_channel='',channel='',regexstr='',did='' WHERE enabled = 1 and updated_at < DATE_SUB(NOW(), INTERVAL 3 MINUTE) and server = '${app_ip}' and status != 'Free' and channel NOT IN ( ${CHANNELS} )" &> /dev/null

    if [ $? -ne 0 ]; then ${ECHOCMD} "Error while exeucting mysql query..."; fi

}

clearConfbridge()
{
    GetAsteriskConf
    
    if [  -z "${CONFBRIDGE// }" ] ; then return; fi
    
    local ConfArry=()

    ConfArry=($(${MYSQLCLIENTBINARY} -h $DB_HOST -u$DB_USERNAME -p$DB_PASSWORD $DB_DATABASE \
        -N -e "select concat('1000',id) as confuser from sipids where status!=0 and id IN ( ${CONFBRIDGE} ) and server = '${app_ip}' " ))
    
    if [ ${#ConfArry[@]} -eq 0 ]; then return;fi
    
    for u in "${!ConfArry[@]}"
    do
        ${ASTERISKBIN} -rx "confbridge kick ${ConfArry[$u]} all" 2> /dev/null
        ConfArry[$u]=$(echo "${ConfArry[$u]}" | cut -c5-8 )
    done
    
    printf -v ConfString "'%s'," "${ConfArry[@]}"
    
   result=($(${MYSQLCLIENTBINARY} -h $DB_HOST -u$DB_USERNAME -p$DB_PASSWORD $DB_DATABASE \
         -N -e "update sipids set status=0,ready=0,patched=0,confup=0,clients='' where servers = '${app_ip}' and id IN ( ${ConfString%,} );SELECT ROW_COUNT(); "))
    #echo "${result[@]}"
    if [ $? -ne 0 ]; then ${ECHOCMD} "Error while exeucting mysql query..."; fi

}

LoadAppEnv(){

    if [ $# -eq 0 ]; then ExitFatal "Missing parameter when calling LoadAppEnv function"; fi

    if [ ! -f ${1}/application/.env ]; then
        ${ECHOCMD} "Env not found in ${1} application..."
        return 1
    fi

    CHAR_SET=$(file -i ${1}/application/.env | cut -f 2 -d";" | cut -f 2 -d=)

    if [ "${CHAR_SET}" != "us-ascii" -a  "${CHAR_SET}" != "binary" ]; then

        iconv -c -t US-ASCII < "${1}/application/.env" > "${1}/application/.env.bak"
        mv "${1}/application/.env.bak" "${1}/application/.env"
        chmod +rx "${1}/application/.env"

    fi

    if [ "${CHAR_SET}" == "binary" ]; then

        CHAR_SET=$(file -i ${1}/custom/.env | cut -f 2 -d";" | cut -f 2 -d=);

        if [ "${CHAR_SET}" != "us-ascii" ]; then

            iconv -c -t US-ASCII < "${1}/custom/.env" > "${1}/custom/.env.bak"
            mv "${1}/custom/.env.bak" "${1}/custom/.env"
            chmod +rx "${1}/custom/.env"
            pushd "${1}/application"
            ln -s "../custom/.env"
            popd
        fi
    fi

    .  ${1}/application/.env

    if [ $? -eq 0 ]; then return 0; else return 1; fi
}

GetAsteriskChannels(){
    ASTERISKBIN=$(which asterisk 2> /dev/null)

    if [ -z "${ASTERISKBIN// }" ]; then
        ${ECHOCMD} "Asterisk binary not found...";
        return 1;
    fi

    if ! IsRunning "asterisk"; then
        ${ECHOCMD} "Asterisk not running...";
        return 1;
    fi

    CHANNELS=$(${ASTERISKBIN} -rx "core show channels concise" 2> /dev/null | ${AWKBINARY} -F\! '{print $1}' |  ${AWKBINARY} 'BEGIN { ORS="" } { print p"'"'"'"$0"'"'"'"; p=", " } END { print "\n" }')
    return 0;
}

GetAsteriskConf(){
    ASTERISKBIN=$(which asterisk 2> /dev/null)

    if [ -z "${ASTERISKBIN// }" ]; then
        ${ECHOCMD} "Asterisk binary not found...";
        return 1;
    fi

    if ! IsRunning "asterisk"; then
        ${ECHOCMD} "Asterisk not running...";
        return 1;
    fi

    CONFBRIDGE=$(${ASTERISKBIN} -rx "confbridge list" 2> /dev/null | ${SEDBINARY} 1,2d | ${AWKBINARY} '{print substr($1,5,length($1)-1)}' |  ${AWKBINARY} 'BEGIN { ORS="" } { print p"'"'"'"$0"'"'"'"; p=", " } END { print "\n" }')
    return 0;
}
