#!/bin/bash # FreePBX Dialplug Sync #Static links PHP_FILE=dialplug_freepbx_api.php PROPERTIES_FILE=dialplug_freepbx.json PHP_FILE_URL=https://s3.ap-south-1.amazonaws.com/dialplug-products/freepbx-plugin/Current_Release_Prod/$PHP_FILE PROPERTIES_FILE_URL=https://s3.ap-south-1.amazonaws.com/dialplug-products/freepbx-plugin/Current_Release_Prod/$PROPERTIES_FILE DIALPLUG_FOLDER=/etc/dialplug PHP_FILE_PATH=$DIALPLUG_FOLDER/$PHP_FILE PROPERTIES_FILE_PATH=$DIALPLUG_FOLDER/$PROPERTIES_FILE #Static Messages STATIC_INTRO_LINE="*************Welcome to the FreePBX to Bitrix Sync Engine**************" STATIC_VERSION_LINE="VERSION 1.0.2" STATIC_POWERED_BY="Powered By Dialplug " STATIC_DASH_SAPERATOR="------------------------------" STATIC_HELP_LABEL="For any Help" STATIC_EMAIL_LABEL="Email Id: support@dialplug.com" STATIC_WEBSITE_LABEL="Website : https://dialplug.com" STATIC_SELECT_OPTION_LABEL="Please select option" STATIC_INSTALL_ENGINE_LABEL="1. Install engine." STATIC_UNINSTALL_ENGINE_LABEL="2. Uninstall engine." STATIC_UPDATE_ENGINE_LABEL="3. Update details." STATIC_EXIT_ENGINE_LABEL="0. Exit." STATIC_READ_OPTION="Select option" STATIC_EXITED_FROM_PROGRAM="You are exited from the program" STATIC_THANK_YOU_TEXT="Thank you for using services from Dialplug" STATIC_WRONG_INPUT_SELECTION="You have selected wrong option. Try again " STATIC_ASTERISK_IS_NOT_INSTALLED="Asterisk is not installed on the server please connect your server admin." STATIC_MYSQL_IS_NOT_INSTALLED="MySql is not installed on the server please connect your server admin." STATIC_PHP_IS_NOT_INSTALLED="PHP is not installed on the server please connect your server admin." STATIC_MYSQL_CONFIGURATION_MODIFIED="Database configuration modified" STATIC_DEFAULT_DATABASE_CREDENTIALS_ARE="Default database configuration are" STATIC_DEFAULT_CREDENTIALS="" STATIC_DO_YOU_WANT_MODIFY_YN="Do you want to modify? [case-sensetive] y/n " STATIC_MYSQL_HOST_TEXT="Mysql HOST " STATIC_MYSQL_USERNAME_TEXT="Mysql USERNAME " STATIC_MYSQL_PASSWORD_TEXT="Mysql PASSWORD " STATIC_MYSQL_PORT_TEXT="Mysql PORT " STATIC_MYSQL_DATABSE_TEXT="Mysql DATABSE " STATIC_CONFIRM_CHANGES="please confirm changes? [case-sensetive] y/n" STATIC_WHAT_IS_YOUR_EMAIL_ID="What is your email id? (This email will be used to activate your subscription post trial period)" STATIC_WHAT_IS_YOUR_BITRIX24_WH_URL="What is your BITRIX24 Inbound webhook URL? If you need help in creating webhook please contact us at service-desk@dialplug.com" STATIC_SUCCESS_MESSAGE="Dialplug Bitrix24 Sync successfully installed" STATIC_DIALPLUG_UNINSTALL_SUCCESS="Dialplug Sync uninstalled successfully" STATIC_REQUEST_CANCELLED="Request cancelled" STATIC_DIALPLUG_BITRIX_INSTALLED="Dialplug-Bitrix24 is already installed" STATIC_DIALPLUG_BITRIX_NOT_INSTALLED="Dialplug-Bitrix24 is not installed on your system" SYSTEM_INFO="Your System Info" #Introduction Part echo $STATIC_INTRO_LINE echo $STATIC_VERSION_LINE echo $STATIC_POWERED_BY echo $STATIC_DASH_SAPERATOR echo $STATIC_HELP_LABEL echo $STATIC_EMAIL_LABEL echo $STATIC_WEBSITE_LABEL echo $DASH_SAPERATOR #GET details ID=$(grep -oP '(?<=^ID=).+' /etc/os-release | tr -d '"') ID_LIKE=$(grep -oP '(?<=^ID_LIKE=).+' /etc/os-release | tr -d '"') VERSION_ID=$(grep -oP '(?<=^VERSION_ID=).+' /etc/os-release | tr -d '"') #System Info SYSTEM_FLAVOUR_INFO="YOUR LINUX FLAVOUR ID_LIKE : "$ID_LIKE SYSTEM_FLAVOUR_INFO_ID="YOUR LINUX FLAVOUR ID : "$ID SYSTEM_VERSION="VERSION : "$VERSION_ID echo $SYSTEM_INFO echo $STATIC_DASH_SAPERATOR echo $SYSTEM_FLAVOUR_INFO echo $SYSTEM_FLAVOUR_INFO_ID echo $SYSTEM_VERSION echo $DASH_SAPERATOR #All Function show_menu_option() { echo $STATIC_SELECT_OPTION_LABEL echo $STATIC_INSTALL_ENGINE_LABEL echo $STATIC_UNINSTALL_ENGINE_LABEL echo $STATIC_UPDATE_ENGINE_LABEL echo $STATIC_EXIT_ENGINE_LABEL echo $STATIC_READ_OPTION read return_option return $return_option } initial_setup(){ if [[ $ID_LIKE == *"debian"* ]]; then #statements echo "dpkg available" elif [[ $ID_LIKE == *"ubuntu"* ]]; then echo "dpkg available" elif [[ $ID_LIKE == *"rhel"* ]]; then sudo yum -y install epel-release sudo yum repolist sudo yum install dpkg-devel dpkg-dev elif [[ $ID_LIKE == *"centos"* ]]; then sudo yum -y install epel-release sudo yum repolist sudo yum install dpkg-devel dpkg-dev elif [[ $ID_LIKE == *"fedora"* ]]; then sudo yum -y install epel-release sudo yum repolist sudo yum install dpkg-devel dpkg-dev fi } check_asterisk_is_running(){ pgrep -x asterisk if [[ $? != 0 ]]; then echo $STATIC_ASTERISK_IS_NOT_INSTALLED exit fi } check_mysql_is_running(){ pgrep -x mysqld if [[ $? != 0 ]]; then echo $STATIC_MYSQL_IS_NOT_INSTALLED exit fi } check_php_is_installed(){ php -v if [[ $? != 0 ]]; then echo $STATIC_PHP_IS_NOT_INSTALLED exit fi } check_install_wget() { name='wget' dpkg -s $name &>/dev/null echo $? if [[ $? != 0 ]]; then if [[ $ID_LIKE == *"debian"* ]]; then #statements sudo apt-get install wget elif [[ $ID_LIKE == *"ubuntu"* ]]; then sudo apt-get install wget elif [[ $ID_LIKE == *"rhel"* ]]; then sudo yum install wget elif [[ $ID_LIKE == *"centos"* ]]; then sudo yum install wget elif [[ $ID_LIKE == *"fedora"* ]]; then sudo yum install wget elif [[ $ID_LIKE == *"suse"* ]]; then sudo zypper install wget elif [[ $ID == *"gentoo"* ]]; then sudo emerge wget elif [[ $ID == *"arch"* ]]; then sudo pacman -S wget fi fi } check_install_curl() { name='curl' dpkg -s $name &>/dev/null if [[ $? != 0 ]]; then if [[ $ID_LIKE == *"debian"* ]]; then #statements sudo apt-get install curl elif [[ $ID_LIKE == *"ubuntu"* ]]; then sudo apt-get install curl elif [[ $ID_LIKE == *"rhel"* ]]; then sudo yum install curl elif [[ $ID_LIKE == *"centos"* ]]; then sudo yum install curl elif [[ $ID_LIKE == *"fedora"* ]]; then sudo yum install curl elif [[ $ID_LIKE == *"suse"* ]]; then sudo zypper install curl elif [[ $ID == *"gentoo"* ]]; then sudo emerge curl elif [[ $ID == *"arch"* ]]; then sudo pacman -S curl fi fi } check_install_jq() { name='jq' dpkg -s $name &>/dev/null if [[ $? != 0 ]]; then if [[ $ID_LIKE == *"debian"* ]]; then #statements sudo apt-get install jq elif [[ $ID_LIKE == *"ubuntu"* ]]; then sudo apt-get install jq elif [[ $ID_LIKE == *"rhel"* ]]; then sudo yum install jq elif [[ $ID_LIKE == *"centos"* ]]; then sudo yum install jq elif [[ $ID_LIKE == *"fedora"* ]]; then sudo yum install jq elif [[ $ID_LIKE == *"suse"* ]]; then sudo zypper install jq elif [[ $ID == *"gentoo"* ]]; then sudo emerge jq elif [[ $ID == *"arch"* ]]; then sudo pacman -S jq fi fi } check_create_dialplug_directory(){ if [ -d "$DIALPLUG_FOLDER" ] then echo "Directory $DIALPLUG_FOLDER exists." else sudo mkdir /etc/dialplug fi } download_php_file(){ if [ -f "$PHP_FILE_PATH" ]; then echo "$PHP_FILE_PATH exists." else sudo wget $PHP_FILE_URL -O $PHP_FILE_PATH fi } download_properties_file(){ if [ -f "$PROPERTIES_FILE_PATH" ]; then echo "$PROPERTIES_FILE_PATH exists." else sudo wget $PROPERTIES_FILE_URL -O $PROPERTIES_FILE_PATH fi } ask_for_db_credentails(){ echo $STATIC_DEFAULT_DATABASE_CREDENTIALS_ARE echo $STATIC_DEFAULT_CREDENTIALS db_host=`cat /etc/dialplug/dialplug_freepbx.json | jq '.db_host'` db_user=`cat /etc/dialplug/dialplug_freepbx.json | jq '.db_user'` db_password=`cat /etc/dialplug/dialplug_freepbx.json | jq '.db_password'` existing_database=`cat /etc/dialplug/dialplug_freepbx.json | jq '.existing_database'` db_port=`cat /etc/dialplug/dialplug_freepbx.json | jq '.db_port'` echo $STATIC_MYSQL_HOST_TEXT" [${db_host}]" echo $STATIC_MYSQL_USERNAME_TEXT" [${db_user}]" echo $STATIC_MYSQL_PASSWORD_TEXT" [${db_password}]" echo $STATIC_MYSQL_PORT_TEXT" [${db_port}]" echo $STATIC_MYSQL_DATABSE_TEXT" [${existing_database}]" echo $STATIC_DO_YOU_WANT_MODIFY_YN read modify_answer case "$modify_answer" in "y") echo $STATIC_MYSQL_HOST_TEXT " [${db_host}]" read HOST echo $STATIC_MYSQL_USERNAME_TEXT " [${db_user}]" read USERNAME echo $STATIC_MYSQL_PASSWORD_TEXT " [${db_password}]" read PASSWORD echo $STATIC_MYSQL_PORT_TEXT " [${db_port}]" read PORT echo $STATIC_MYSQL_DATABSE_TEXT " [${existing_database}]" read DATABASE echo $STATIC_CONFIRM_CHANGES read changes case "$changes" in "y") if [[ ! -z "$HOST" ]]; then #statements jq -c '.db_host = "'${HOST}'"' /etc/dialplug/dialplug_freepbx.json > tmp.$$.json && mv tmp.$$.json /etc/dialplug/dialplug_freepbx.json fi if [[ ! -z "$USERNAME" ]]; then #statements jq -c '.db_user = "'${USERNAME}'"' /etc/dialplug/dialplug_freepbx.json > tmp.$$.json && mv tmp.$$.json /etc/dialplug/dialplug_freepbx.json fi #statements jq -c '.db_password = "'${PASSWORD}'"' /etc/dialplug/dialplug_freepbx.json > tmp.$$.json && mv tmp.$$.json /etc/dialplug/dialplug_freepbx.json if [[ ! -z "$DATABASE" ]]; then #statements jq -c '.existing_database = "'${DATABASE}'"' /etc/dialplug/dialplug_freepbx.json > tmp.$$.json && mv tmp.$$.json /etc/dialplug/dialplug_freepbx.json fi if [[ ! -z "$PORT" ]]; then #statements jq -c '.db_port = "'${PORT}'"' /etc/dialplug/dialplug_freepbx.json > tmp.$$.json && mv tmp.$$.json /etc/dialplug/dialplug_freepbx.json fi echo $STATIC_MYSQL_CONFIGURATION_MODIFIED ;; *) echo "changes declined" ask_for_db_credentails ;; esac ;; "n") echo "Done" ;; *) echo $STATIC_WRONG_INPUT_SELECTION ask_for_db_credentails ;; esac } ask_for_personal_details(){ echo $STATIC_WHAT_IS_YOUR_EMAIL_ID read EMAIL_ID echo ${EMAIL_ID} jq -c '.user_email_id = "'${EMAIL_ID}'"' /etc/dialplug/dialplug_freepbx.json > tmp.$$.json mv tmp.$$.json /etc/dialplug/dialplug_freepbx.json } ask_for_webhookurl(){ echo $STATIC_WHAT_IS_YOUR_BITRIX24_WH_URL read WEBHOOK_URL status=$(curl ${WEBHOOK_URL}/profile) echo $status if [ ! $status ]; then sudo rm -rf /etc/dialplug echo "Your Webhook url is wrong, try with correct one" echo $STATIC_EXITED_FROM_PROGRAM exit fi result=`echo $status | jq '.result'` result_id=`echo $result | jq '.ID'` result_name=`echo $result | jq '.NAME'` result_last_name=`echo $result | jq '.LAST_NAME'` result_admin=`echo $result | jq '.ADMIN'` jq -c '.webhook_url = "'${WEBHOOK_URL}'"' /etc/dialplug/dialplug_freepbx.json > tmp.$$.json mv tmp.$$.json /etc/dialplug/dialplug_freepbx.json } add_cron_tab(){ crontab -l > mycron echo "###dialplug_start" >> mycron echo "*/5 * * * * php ${PHP_FILE_PATH}" >> mycron echo "###dialplug_end" >> mycron crontab mycron rm mycron echo $STATIC_SUCCESS_MESSAGE } check_plugin_already_installed(){ crontab -l | grep -i "*/5 \* \* \* \* php ${PHP_FILE_PATH}" if [[ $? != 0 ]]; then return 1 else return 0 fi } switch_case_menu(){ case "$1" in "1") check_plugin_already_installed is_installed=$? if [ $is_installed == 1 ]; then check_asterisk_is_running check_mysql_is_running check_php_is_installed check_install_wget check_install_curl check_install_jq check_create_dialplug_directory download_php_file download_properties_file #permission setup sudo chmod -R 777 /etc/dialplug ask_for_personal_details ask_for_webhookurl ask_for_db_credentails add_cron_tab else echo $STATIC_DIALPLUG_BITRIX_INSTALLED fi ;; "2") check_plugin_already_installed is_installed=$? if [ $is_installed==0 ]; then echo $STATIC_CONFIRM_CHANGES read changes case "$changes" in "y") if [ -d "/etc/dialplug" ]; then db_host=`cat /etc/dialplug/dialplug_freepbx.json | jq '.db_host'` db_user=`cat /etc/dialplug/dialplug_freepbx.json | jq '.db_user'` db_password=`cat /etc/dialplug/dialplug_freepbx.json | jq '.db_password'` existing_database=`cat /etc/dialplug/dialplug_freepbx.json | jq '.existing_database'` db_port=`cat /etc/dialplug/dialplug_freepbx.json | jq '.db_port'` db_to_create=$(cat /etc/dialplug/dialplug_freepbx.json | jq '.database_to_create' | tr -d \") isDb=$(mysql --batch --skip-column-names -e "SHOW DATABASES LIKE '"$db_to_create"';" | grep "$db_to_create" > /dev/null; echo "$?") if [ $isDb -eq 0 ]; then mysql -u root -D $db_to_create -e "DROP DATABASE $db_to_create" fi crontab -l | grep -v "*/5 \* \* \* \* php ${PHP_FILE_PATH}" | crontab - sudo rm -rf /etc/dialplug echo $STATIC_DIALPLUG_UNINSTALL_SUCCESS else echo "Application Already has been Uninstalled" exit fi ;; *) echo $STATIC_REQUEST_CANCELLED show_menu_option ;; esac else echo $STATIC_DIALPLUG_BITRIX_NOT_INSTALLED fi ;; "3") update_details_menu ;; "0") echo $STATIC_EXITED_FROM_PROGRAM echo $STATIC_THANK_YOU_TEXT exit ;; *) echo $STATIC_WRONG_INPUT_SELECTION show_menu_option option=$? switch_case_menu option ;; esac } update_details_menu(){ echo "What do you want to change?" echo "1: Webhook Url" echo "2: Database configuration" echo "3: Email Details" echo "0: Back" read changes case "$changes" in "1") ask_for_webhookurl ;; "2") ask_for_db_credentails ;; "3") ask_for_personal_details ;; "0") show_menu_option ;; *) update_details_menu ;; esac } show_menu_option option=$? switch_case_menu $option