| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954 | #!/bin/bash
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# postactiv application
#
# License
# =======
#
# Copyright (C) 2016-2017 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
VARIANTS='full full-vim media'
IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=1
POSTACTIV_DOMAIN_NAME=
POSTACTIV_CODE=
POSTACTIV_ONION_PORT=8100
POSTACTIV_REPO="https://git.postactiv.com/postActiv/postActiv.git"
POSTACTIV_COMMIT='0531c469b44aab6a71230778ab4492eca889bb2c'
POSTACTIV_ADMIN_PASSWORD=
POSTACTIV_BACKGROUND_IMAGE_URL=
POSTACTIV_TITLE='PostActiv'
# Number of months after which posts expire
POSTACTIV_EXPIRE_MONTHS=3
postactiv_variables=(ONION_ONLY
                     POSTACTIV_DOMAIN_NAME
                     POSTACTIV_CODE
                     DDNS_PROVIDER
                     MY_USERNAME
                     POSTACTIV_BACKGROUND_IMAGE_URL
                     POSTACTIV_EXPIRE_MONTHS
                     POSTACTIV_TITLE)
function logging_on_postactiv {
    echo -n ''
}
function logging_off_postactiv {
    echo -n ''
}
function postactiv_customise_logo {
    domain_name=$1
    if [ -f /var/www/${domain_name}/htdocs/static/logo.png ]; then
        if [ -f ~/freedombone/img/postactiv.png ]; then
            cp ~/freedombone/img/postactiv.png /var/www/${domain_name}/htdocs/static/logo.png
        else
            if [ -f /home/$MY_USERNAME/freedombone/img/postactiv.png ]; then
                cp /home/$MY_USERNAME/freedombone/img/postactiv.png /var/www/${domain_name}/htdocs/static/logo.png
            fi
        fi
    fi
}
function remove_user_postactiv {
    remove_username="$1"
    ${PROJECT_NAME}-pass -u $remove_username --rmapp postactiv
    function_check get_completion_param
    POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
    if [ -d /var/www/$POSTACTIV_DOMAIN_NAME ]; then
        cd /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
        php scripts/deleteprofile.php -n $remove_username -y
    fi
}
function add_user_postactiv {
    new_username="$1"
    new_user_password="$2"
    ${PROJECT_NAME}-pass -u $new_username -a postactiv -p "$new_user_password"
    POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
    if [ -d /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs ]; then
        cd /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
        php scripts/registeruser.php -n $new_username -w "$new_user_password" -e "$new_username@$HOSTNAME"
        ${PROJECT_NAME}-addemail -u $new_username -e "noreply@$POSTACTIV_DOMAIN_NAME" -g postactiv --public no
        echo '0'
    else
        echo '1'
    fi
}
function install_interactive_postactiv {
    if [ ! $ONION_ONLY ]; then
        ONION_ONLY='no'
    fi
    if [[ $ONION_ONLY != "no" ]]; then
        POSTACTIV_DOMAIN_NAME='postactiv.local'
    else
        POSTACTIV_DETAILS_COMPLETE=
        while [ ! $POSTACTIV_DETAILS_COMPLETE ]
        do
            data=$(tempfile 2>/dev/null)
            trap "rm -f $data" 0 1 2 5 15
            if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
                dialog --backtitle $"Freedombone Configuration" \
                       --title $"PostActiv Configuration" \
                       --form $"\nPlease enter your PostActiv details. The background image URL can be left blank.\n\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 16 65 4 \
                       $"Domain:" 1 1 "$(grep 'POSTACTIV_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
                       $"Title:" 2 1 "$(grep '$POSTACTIV_TITLE' temp.cfg | awk -F '=' '{print $2}')" 2 25 255 255 \
                       $"Background image URL:" 3 1 "$(grep '$POSTACTIV_BACKGROUND_IMAGE_URL' temp.cfg | awk -F '=' '{print $2}')" 3 25 255 255 \
                       $"Code:" 4 1 "$(grep 'POSTACTIV_CODE' temp.cfg | awk -F '=' '{print $2}')" 4 25 33 255 \
                       2> $data
            else
                dialog --backtitle $"Freedombone Configuration" \
                       --title $"PostActiv Configuration" \
                       --form $"\nPlease enter your PostActiv details. The background image URL can be left blank.\n\nIMPORTANT: This should be a domain name which is supported by Let's Encrypt:" 16 65 4 \
                       $"Domain:" 1 1 "$(grep 'POSTACTIV_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
                       $"Title:" 2 1 "$(grep '$POSTACTIV_TITLE' temp.cfg | awk -F '=' '{print $2}')" 2 25 255 255 \
                       $"Background image URL:" 3 1 "$(grep '$POSTACTIV_BACKGROUND_IMAGE_URL' temp.cfg | awk -F '=' '{print $2}')" 3 25 255 255 \
                       2> $data
            fi
            sel=$?
            case $sel in
                1) exit 1;;
                255) exit 1;;
            esac
            POSTACTIV_DOMAIN_NAME=$(cat $data | sed -n 1p)
            title=$(cat $data | sed -n 2p)
            if [ ${#title} -gt 1 ]; then
                POSTACTIV_TITLE=$welcome_msg
            fi
            img_url=$(cat $data | sed -n 3p)
            if [ ${#img_url} -gt 1 ]; then
                POSTACTIV_BACKGROUND_IMAGE_URL=$img_url
            fi
            if [ $POSTACTIV_DOMAIN_NAME ]; then
                if [[ $POSTACTIV_DOMAIN_NAME == "$HUBZILLA_DOMAIN_NAME" ]]; then
                    POSTACTIV_DOMAIN_NAME=""
                fi
                TEST_DOMAIN_NAME=$POSTACTIV_DOMAIN_NAME
                validate_domain_name
                if [[ $TEST_DOMAIN_NAME != $POSTACTIV_DOMAIN_NAME ]]; then
                    POSTACTIV_DOMAIN_NAME=
                    dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
                else
                    if [[ $DDNS_PROVIDER == "default@freedns.afraid.org" ]]; then
                        POSTACTIV_CODE=$(cat $data | sed -n 4p)
                        validate_freedns_code "$POSTACTIV_CODE"
                        if [ ! $VALID_CODE ]; then
                            POSTACTIV_DOMAIN_NAME=
                        fi
                    fi
                fi
            fi
            if [ $POSTACTIV_DOMAIN_NAME ]; then
                POSTACTIV_DETAILS_COMPLETE="yes"
            fi
        done
        # remove any invalid characters
        if [ ${#POSTACTIV_TITLE} -gt 0 ]; then
            new_title=$(echo "$POSTACTIV_TITLE" | sed "s|'||g")
            POSTACTIV_TITLE="$new_title"
        fi
        # save the results in the config file
        write_config_param "POSTACTIV_CODE" "$POSTACTIV_CODE"
        write_config_param "POSTACTIV_TITLE" "$POSTACTIV_TITLE"
        write_config_param "POSTACTIV_BACKGROUND_IMAGE_URL" "$POSTACTIV_BACKGROUND_IMAGE_URL"
    fi
    write_config_param "POSTACTIV_DOMAIN_NAME" "$POSTACTIV_DOMAIN_NAME"
    APP_INSTALLED=1
}
function change_password_postactiv {
    curr_username="$1"
    new_user_password="$2"
    read_config_param 'POSTACTIV_DOMAIN_NAME'
    cd /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/scripts
    php setpassword.php "$curr_username" "$new_user_password"
    ${PROJECT_NAME}-pass -u "$curr_username" -a postactiv -p "$new_user_password"
}
function postactiv_create_database {
    if [ -f $IMAGE_PASSWORD_FILE ]; then
        POSTACTIV_ADMIN_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
    else
        if [ ! $POSTACTIV_ADMIN_PASSWORD ]; then
            POSTACTIV_ADMIN_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
        fi
    fi
    if [ ! $POSTACTIV_ADMIN_PASSWORD ]; then
        return
    fi
    function_check create_database
    create_database postactiv "$POSTACTIV_ADMIN_PASSWORD" $MY_USERNAME
}
function postactiv_running_script {
    gnusocial_hourly_script postactiv $POSTACTIV_DOMAIN_NAME
    # This was a hack to fix a bug in master, but may no longer be needed
    #echo "if ! grep -q \"//define('POSTACTIV',true);\" scripts/commandline.inc; then" >> /etc/cron.hourly/postactiv-daemons
    #echo "  sed -i \"s|define('POSTACTIV',true);|//define('POSTACTIV',true);|g\" scripts/commandline.inc" >> /etc/cron.hourly/postactiv-daemons
    #echo 'fi' >> /etc/cron.hourly/postactiv-daemons
}
function reconfigure_postactiv {
    echo -n ''
}
function postactiv_set_background_image {
    POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
    data=$(tempfile 2>/dev/null)
    trap "rm -f $data" 0 1 2 5 15
    dialog --title $"PostActiv" \
           --backtitle $"Freedombone Control Panel" \
           --inputbox $'Set a background image URL' 10 60 2>$data
    sel=$?
    case $sel in
        0)
            temp_background=$(<$data)
            if [ ${#temp_background} -gt 0 ]; then
                POSTACTIV_BACKGROUND_IMAGE_URL="$temp_background"
                write_config_param "POSTACTIV_BACKGROUND_IMAGE_URL" "$POSTACTIV_BACKGROUND_IMAGE_URL"
                if [[ $(pleroma_set_background_image_from_url /var/www/$POSTACTIV_DOMAIN_NAME/htdocs "$POSTACTIV_DOMAIN_NAME" "$POSTACTIV_BACKGROUND_IMAGE_URL" "$POSTACTIV_TITLE" | tail -n 1) == "0" ]]; then
                    dialog --title $"Set PostActiv background" \
                           --msgbox $"The background image has been set" 6 60
                fi
            fi
           ;;
    esac
}
function postactiv_set_title {
    data=$(tempfile 2>/dev/null)
    trap "rm -f $data" 0 1 2 5 15
    dialog --title $"PostActiv" \
           --backtitle $"Freedombone Control Panel" \
           --inputbox $'Set a title' 10 60 2>$data
    sel=$?
    case $sel in
        0)
            new_title=$(<$data)
            if [ ${#new_title} -gt 0 ]; then
                POSTACTIV_TITLE="$new_title"
                POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
                write_config_param "POSTACTIV_TITLE" "$POSTACTIV_TITLE"
                cd /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/static
                sed -i "s|\"name\":.*|\"name\": \"${POSTACTIV_TITLE}\",|g" config.json
                dialog --title $"Set PostActiv title" \
                       --msgbox $"The title has been set" 6 60
            fi
           ;;
    esac
}
function postactiv_set_expire_months {
    POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
    read_config_param "POSTACTIV_EXPIRE_MONTHS"
    data=$(tempfile 2>/dev/null)
    trap "rm -f $data" 0 1 2 5 15
    dialog --title $"PostActiv" \
           --backtitle $"Freedombone Control Panel" \
           --inputbox $'Set an expiry period for posts in months. Anything older will be deleted. Lower values help to keep the database size small and as fast as possible.' 12 60 "$POSTACTIV_EXPIRE_MONTHS" 2>$data
    sel=$?
    case $sel in
        0)
            new_expiry_months=$(<$data)
            if [ ${#new_expiry_months} -gt 0 ]; then
                # should contain no spaces
                if [[ "$new_expiry_months" == *" "* ]]; then
                    return
                fi
                # should be a number
                re='^[0-9]+$'
                if ! [[ $new_expiry_months =~ $re ]] ; then
                    return
                fi
                # set the new value
                POSTACTIV_EXPIRE_MONTHS=$new_expiry_months
                write_config_param "POSTACTIV_EXPIRE_MONTHS" "$POSTACTIV_EXPIRE_MONTHS"
                expire_gnusocial_posts "$POSTACTIV_DOMAIN_NAME" "postactiv" "$POSTACTIV_EXPIRE_MONTHS"
                dialog --title $"Set PostActiv post expiry period" \
                       --msgbox $"Expiry period set to $POSTACTIV_EXPIRE_MONTHS months" 6 60
            fi
           ;;
    esac
}
function configure_interactive_postactiv {
    read_config_param "POSTACTIV_EXPIRE_MONTHS"
    while true
    do
        data=$(tempfile 2>/dev/null)
        trap "rm -f $data" 0 1 2 5 15
        dialog --backtitle $"Freedombone Control Panel" \
               --title $"PostActiv" \
               --radiolist $"Choose an operation:" 16 70 7 \
               1 $"Set a background image" off \
               2 $"Set the title" off \
               3 $"Set post expiry period (currently $POSTACTIV_EXPIRE_MONTHS months)" off \
               4 $"Select Qvitter user interface" off \
               5 $"Select Pleroma user interface" off \
               6 $"Select Classic user interface" off \
               7 $"Exit" on 2> $data
        sel=$?
        case $sel in
            1) return;;
            255) return;;
        esac
        case $(cat $data) in
            1) postactiv_set_background_image;;
            2) postactiv_set_title;;
            3) postactiv_set_expire_months;;
            4) gnusocial_use_qvitter postactiv;;
            5) gnusocial_use_pleroma postactiv;;
            6) gnusocial_use_classic postactiv;;
            7) break;;
        esac
    done
}
function upgrade_postactiv {
    CURR_POSTACTIV_COMMIT=$(get_completion_param "postactiv commit")
    if [[ "$CURR_POSTACTIV_COMMIT" == "$POSTACTIV_COMMIT" ]]; then
        return
    fi
    if grep -q "postactiv domain" $COMPLETION_FILE; then
        POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
    fi
    # update to the next commit
    function_check set_repo_commit
    set_repo_commit /var/www/$POSTACTIV_DOMAIN_NAME/htdocs "postactiv commit" "$POSTACTIV_COMMIT" $POSTACTIV_REPO
    # Ensure that installation script is removed
    if [ -f /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/install.php ]; then
        rm /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/install.php
    fi
    if [ -f /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/index_qvitter.php ]; then
        if [ -f /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/index.php ]; then
            mv /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/index.php /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/index_qvitter.php
        fi
    fi
    function_check postactiv_running_script
    postactiv_running_script
    gnusocial_block_user_script
    gnusocial_block_domain_script postactiv "$POSTACTIV_DOMAIN_NAME"
    upgrade_pleroma_frontend "$POSTACTIV_DOMAIN_NAME" "postactiv" "$POSTACTIV_BACKGROUND_IMAGE_URL" "$POSTACTIV_TITLE"
    postactiv_customise_logo
    install_gnusocial_default_background "postactiv" "$POSTACTIV_DOMAIN_NAME"
    chown -R www-data:www-data /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs
    systemctl restart mariadb
}
function backup_local_postactiv {
    POSTACTIV_DOMAIN_NAME='postactiv'
    if grep -q "postactiv domain" $COMPLETION_FILE; then
        POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
    fi
    # don't backup more data than we need to
    /etc/cron.daily/postactiv-expire
    source_directory=/var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/backup
    if [ ! -d $source_directory ]; then
        mkdir $source_directory
    fi
    cp -p /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/config.php $source_directory
    if [ -d /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/static ]; then
        cp -rp /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/static $source_directory
    fi
    function_check suspend_site
    suspend_site ${POSTACTIV_DOMAIN_NAME}
    function_check backup_directory_to_usb
    dest_directory=postactivconfig
    backup_directory_to_usb $source_directory $dest_directory
    source_directory=/var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/file
    dest_directory=postactivfile
    backup_directory_to_usb $source_directory $dest_directory
    function_check backup_database_to_usb
    backup_database_to_usb postactiv
    function_check restart_site
    restart_site
}
function restore_local_postactiv {
    if ! grep -q "postactiv domain" $COMPLETION_FILE; then
        return
    fi
    POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
    if [ $POSTACTIV_DOMAIN_NAME ]; then
        echo $"Restoring postactiv"
        temp_restore_dir=/root/temppostactiv
        postactiv_dir=/var/www/${POSTACTIV_DOMAIN_NAME}/htdocs
        # stop the daemons
        cd $postactiv_dir
        scripts/stopdaemons.sh
        function_check postactiv_create_database
        postactiv_create_database
        restore_database postactiv
        if [ -d $temp_restore_dir ]; then
            rm -rf $temp_restore_dir
        fi
        function_check restore_directory_from_usb
        restore_directory_from_usb $temp_restore_dir postactivconfig
        if [ -d $temp_restore_dir ]; then
            if [ -d $temp_restore_dir$postactiv_dir ]; then
                cp $temp_restore_dir$postactiv_dir/backup/config.php $postactiv_dir/
                cp -rp $temp_restore_dir$postactiv_dir/static $postactiv_dir/
            else
                cp $temp_restore_dir/backup/config.php $postactiv_dir/
                if [ ! -d $postactiv_dir/static ]; then
                    mkdir $postactiv_dir/static
                fi
                cp -rp $temp_restore_dir/static/* $postactiv_dir/static/
            fi
            chown www-data:www-data $postactiv_dir/config.php
            chown -R www-data:www-data $postactiv_dir/static
            rm -rf $temp_restore_dir
        fi
        restore_directory_from_usb $temp_restore_dir postactivfile
        if [ -d $temp_restore_dir ]; then
            if [ -d $temp_restore_dir$postactiv_dir/file ]; then
                cp -rp $temp_restore_dir$postactiv_dir/file $postactiv_dir/
            else
                if [ ! -d $postactiv_dir/file ]; then
                    mkdir $postactiv_dir/file
                fi
                cp -rp $temp_restore_dir/* $postactiv_dir/file/
            fi
            chown -R www-data:www-data $postactiv_dir/file
            rm -rf $temp_restore_dir
        fi
        gnusocial_update_after_restore postactiv ${POSTACTIV_DOMAIN_NAME}
        echo $"Restore of postactiv complete"
    fi
}
function backup_remote_postactiv {
    POSTACTIV_DOMAIN_NAME='postactiv'
    if grep -q "postactiv domain" $COMPLETION_FILE; then
        POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
    fi
    # don't backup more data than we need to
    /etc/cron.daily/postactiv-expire
    source_directory=/var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/backup
    if [ ! -d $source_directory ]; then
        mkdir $source_directory
    fi
    cp -p /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/config.php $source_directory
    if [ -d /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/static ]; then
        cp -rp /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/static $source_directory
    fi
    function_check suspend_site
    suspend_site ${POSTACTIV_DOMAIN_NAME}
    function_check backup_directory_to_friend
    dest_directory=postactivconfig
    backup_directory_to_friend $source_directory $dest_directory
    source_directory=/var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/file
    dest_directory=postactivfile
    backup_directory_to_friend $source_directory $dest_directory
    function_check backup_database_to_friend
    backup_database_to_friend postactiv
    function_check restart_site
    restart_site
}
function restore_remote_postactiv {
    if ! grep -q "postactiv domain" $COMPLETION_FILE; then
        return
    fi
    POSTACTIV_DOMAIN_NAME=$(get_completion_param "postactiv domain")
    if [ $POSTACTIV_DOMAIN_NAME ]; then
        echo $"Restoring postactiv"
        temp_restore_dir=/root/temppostactiv
        postactiv_dir=/var/www/${POSTACTIV_DOMAIN_NAME}/htdocs
        # stop the daemons
        cd $postactiv_dir
        scripts/stopdaemons.sh
        function_check postactiv_create_database
        postactiv_create_database
        function_check restore_database_from_friend
        restore_database_from_friend postactiv
        if [ -d $temp_restore_dir ]; then
            rm -rf $temp_restore_dir
        fi
        function_check restore_directory_from_friend
        restore_directory_from_friend $temp_restore_dir postactivconfig
        if [ -d $temp_restore_dir ]; then
            if [ -d $temp_restore_dir$postactiv_dir ]; then
                cp $temp_restore_dir$postactiv_dir/backup/config.php $postactiv_dir/
                cp -rp $temp_restore_dir$postactiv_dir/static $postactiv_dir/
            else
                cp $temp_restore_dir/config.php $postactiv_dir/
                if [ ! -d $postactiv_dir/static ]; then
                    mkdir $postactiv_dir/static
                fi
                cp -rp $temp_restore_dir/static/* $postactiv_dir/static/
            fi
            chown www-data:www-data $postactiv_dir/config.php
            chown -R www-data:www-data $postactiv_dir/static
            rm -rf $temp_restore_dir
        fi
        restore_directory_from_friend $temp_restore_dir postactivfile
        if [ -d $temp_restore_dir ]; then
            if [ -d $temp_restore_dir$postactiv_dir/file ]; then
                cp -rp $temp_restore_dir$postactiv_dir/file $postactiv_dir/
            else
                if [ ! -d $postactiv_dir/file ]; then
                    mkdir $postactiv_dir/file
                fi
                cp -rp $temp_restore_dir/* $postactiv_dir/file/
            fi
            chown -R www-data:www-data $postactiv_dir/file
            rm -rf $temp_restore_dir
        fi
        gnusocial_update_after_restore postactiv ${POSTACTIV_DOMAIN_NAME}
        echo $"Restore of postactiv complete"
    fi
}
function remove_postactiv {
    if [ ${#POSTACTIV_DOMAIN_NAME} -eq 0 ]; then
        return
    fi
    function_check remove_nodejs
    remove_nodejs pleroma-postactiv
    read_config_param "POSTACTIV_DOMAIN_NAME"
    read_config_param "MY_USERNAME"
    echo "Removing $POSTACTIV_DOMAIN_NAME"
    nginx_dissite $POSTACTIV_DOMAIN_NAME
    remove_certs $POSTACTIV_DOMAIN_NAME
    if [ -f /etc/cron.hourly/postactiv-daemons ]; then
        rm /etc/cron.hourly/postactiv-daemons
    fi
    if [ -f /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/scripts/stopdaemons.sh ]; then
        cd /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
        scripts/stopdaemons.sh
    fi
    kill_pid=$(ps aux | grep /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/scripts/queuedaemon.php | awk -F ' ' '{print $2}' | head -n 1)
    kill -9 $kill_pid
    if [ -d /var/www/$POSTACTIV_DOMAIN_NAME ]; then
        rm -rf /var/www/$POSTACTIV_DOMAIN_NAME
    fi
    if [ -f /etc/nginx/sites-available/$POSTACTIV_DOMAIN_NAME ]; then
        rm /etc/nginx/sites-available/$POSTACTIV_DOMAIN_NAME
    fi
    function_check drop_database
    drop_database postactiv
    function_check remove_onion_service
    remove_onion_service postactiv ${POSTACTIV_ONION_PORT}
    if grep -q "postactiv" /etc/crontab; then
        sed -i "/postactiv/d" /etc/crontab
    fi
    remove_app postactiv
    remove_completion_param install_postactiv
    sed -i '/postactiv/d' $COMPLETION_FILE
    remove_backup_database_local postactiv
    sed -i '/postactiv-firewall/d' /etc/crontab
    function_check remove_ddns_domain
    remove_ddns_domain $POSTACTIV_DOMAIN_NAME
}
function install_postactiv_main {
    if [ ! $POSTACTIV_DOMAIN_NAME ]; then
        echo $'No domain name was given for postactiv'
        exit 7359
    fi
    if [[ $(app_is_installed postactiv_main) == "1" ]]; then
        return
    fi
    function_check postactiv_running_script
    postactiv_running_script
    function_check install_mariadb
    install_mariadb
    function_check get_mariadb_password
    get_mariadb_password
    function_check repair_databases_script
    repair_databases_script
    apt-get -yq install php-gettext php-curl php-gd php-mysql git curl
    apt-get -yq install memcached php-memcached php-intl exiftool libfcgi0ldbl
    if [ ! -d /var/www/$POSTACTIV_DOMAIN_NAME ]; then
        mkdir /var/www/$POSTACTIV_DOMAIN_NAME
    fi
    if [ ! -d /var/www/$POSTACTIV_DOMAIN_NAME/htdocs ]; then
        if [ -d /repos/postactiv ]; then
            mkdir /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
            cp -r -p /repos/postactiv/. /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
            cd /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
            git pull
        else
            function_check git_clone
            git_clone $POSTACTIV_REPO /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
        fi
        if [ ! -d /var/www/$POSTACTIV_DOMAIN_NAME/htdocs ]; then
            echo $'Unable to clone postactiv repo'
            exit 87525
        fi
    fi
    cd /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
    git checkout $POSTACTIV_COMMIT -b $POSTACTIV_COMMIT
    set_completion_param "postactiv commit" "$POSTACTIV_COMMIT"
    chmod g+w /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
    chmod a+w /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/avatar
    chmod a+w /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/file
    chown -R www-data:www-data /var/www/$POSTACTIV_DOMAIN_NAME/htdocs
    chmod +x /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/scripts/maildaemon.php
    chmod 755 /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/extlib/HTMLPurifier/HTMLPurifier/DefinitionCache/Serializer.php
    chmod -x /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/extlib/HTMLPurifier/HTMLPurifier/DefinitionCache/Serializer.php
    function_check postactiv_create_database
    postactiv_create_database
    if [ ! -f "/etc/aliases" ]; then
        touch /etc/aliases
    fi
    if ! grep -q "www-data: root" /etc/aliases; then
        echo 'www-data: root' >> /etc/aliases
    fi
    if ! grep -q "/var/www/$POSTACTIV_DOMAIN_NAME/htdocs/scripts/maildaemon.php" /etc/aliases; then
        echo "*: /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/scripts/maildaemon.php" >> /etc/aliases
    fi
    function_check add_ddns_domain
    add_ddns_domain $POSTACTIV_DOMAIN_NAME
    POSTACTIV_ONION_HOSTNAME=$(add_onion_service postactiv 80 ${POSTACTIV_ONION_PORT})
    postactiv_nginx_site=/etc/nginx/sites-available/$POSTACTIV_DOMAIN_NAME
    if [[ $ONION_ONLY == "no" ]]; then
        function_check nginx_http_redirect
        nginx_http_redirect $POSTACTIV_DOMAIN_NAME "index index.php"
        echo 'server {' >> $postactiv_nginx_site
        echo '  listen 443 ssl;' >> $postactiv_nginx_site
        echo '  listen [::]:443 ssl;' >> $postactiv_nginx_site
        echo "  server_name $POSTACTIV_DOMAIN_NAME;" >> $postactiv_nginx_site
        echo '' >> $postactiv_nginx_site
        function_check nginx_compress
        nginx_compress $POSTACTIV_DOMAIN_NAME
        echo '' >> $postactiv_nginx_site
        echo '  # Security' >> $postactiv_nginx_site
        function_check nginx_ssl
        nginx_ssl $POSTACTIV_DOMAIN_NAME
        function_check nginx_disable_sniffing
        nginx_disable_sniffing $POSTACTIV_DOMAIN_NAME
        echo '  add_header Strict-Transport-Security max-age=15768000;' >> $postactiv_nginx_site
        echo '' >> $postactiv_nginx_site
        echo '  # Logs' >> $postactiv_nginx_site
        echo '  access_log /dev/null;' >> $postactiv_nginx_site
        echo '  error_log /dev/null;' >> $postactiv_nginx_site
        echo '' >> $postactiv_nginx_site
        echo '  # Root' >> $postactiv_nginx_site
        echo "  root /var/www/$POSTACTIV_DOMAIN_NAME/htdocs;" >> $postactiv_nginx_site
        echo '' >> $postactiv_nginx_site
        echo '  # Index' >> $postactiv_nginx_site
        echo '  index index.php;' >> $postactiv_nginx_site
        echo '' >> $postactiv_nginx_site
        echo '  # PHP' >> $postactiv_nginx_site
        echo '  location ~ \.php {' >> $postactiv_nginx_site
        echo '    include snippets/fastcgi-php.conf;' >> $postactiv_nginx_site
        echo '    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;' >> $postactiv_nginx_site
        echo '    fastcgi_read_timeout 30;' >> $postactiv_nginx_site
        echo '  }' >> $postactiv_nginx_site
        echo '' >> $postactiv_nginx_site
        echo '  # Location' >> $postactiv_nginx_site
        echo '  location / {' >> $postactiv_nginx_site
        function_check nginx_limits
        nginx_limits $POSTACTIV_DOMAIN_NAME '15m'
        echo '    try_files $uri $uri/ @postactiv;' >> $postactiv_nginx_site
        echo '  }' >> $postactiv_nginx_site
        echo '' >> $postactiv_nginx_site
        echo '  # Fancy URLs' >> $postactiv_nginx_site
        echo '  location @postactiv {' >> $postactiv_nginx_site
        echo '    rewrite ^(.*)$ /index.php?p=$1 last;' >> $postactiv_nginx_site
        echo '  }' >> $postactiv_nginx_site
        echo '' >> $postactiv_nginx_site
        echo '  # Restrict access that is unnecessary anyway' >> $postactiv_nginx_site
        echo '  location ~ /\.(ht|git) {' >> $postactiv_nginx_site
        echo '    deny all;' >> $postactiv_nginx_site
        echo '  }' >> $postactiv_nginx_site
        echo '' >> $postactiv_nginx_site
        # DO NOT ENABLE KEYBASE. postactiv really doesn't like having a .well-known directory
        echo '}' >> $postactiv_nginx_site
    else
        echo -n '' > $postactiv_nginx_site
    fi
    echo 'server {' >> $postactiv_nginx_site
    echo "    listen 127.0.0.1:$POSTACTIV_ONION_PORT default_server;" >> $postactiv_nginx_site
    echo "    server_name $POSTACTIV_ONION_HOSTNAME;" >> $postactiv_nginx_site
    echo '' >> $postactiv_nginx_site
    function_check nginx_disable_sniffing
    nginx_disable_sniffing $POSTACTIV_DOMAIN_NAME
    echo '' >> $postactiv_nginx_site
    function_check nginx_compress
    nginx_compress $POSTACTIV_DOMAIN_NAME
    echo '' >> $postactiv_nginx_site
    echo '  # Logs' >> $postactiv_nginx_site
    echo '  access_log /dev/null;' >> $postactiv_nginx_site
    echo '  error_log /dev/null;' >> $postactiv_nginx_site
    echo '' >> $postactiv_nginx_site
    echo '  # Root' >> $postactiv_nginx_site
    echo "  root /var/www/$POSTACTIV_DOMAIN_NAME/htdocs;" >> $postactiv_nginx_site
    echo '' >> $postactiv_nginx_site
    echo '  # Index' >> $postactiv_nginx_site
    echo '  index index.php;' >> $postactiv_nginx_site
    echo '' >> $postactiv_nginx_site
    echo '  # PHP' >> $postactiv_nginx_site
    echo '  location ~ \.php {' >> $postactiv_nginx_site
    echo '    include snippets/fastcgi-php.conf;' >> $postactiv_nginx_site
    echo '    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;' >> $postactiv_nginx_site
    echo '    fastcgi_read_timeout 30;' >> $postactiv_nginx_site
    echo '  }' >> $postactiv_nginx_site
    echo '' >> $postactiv_nginx_site
    echo '  # Location' >> $postactiv_nginx_site
    echo '  location / {' >> $postactiv_nginx_site
    function_check nginx_limits
    nginx_limits $POSTACTIV_DOMAIN_NAME '15m'
    echo '    try_files $uri $uri/ @postactiv;' >> $postactiv_nginx_site
    echo '  }' >> $postactiv_nginx_site
    echo '' >> $postactiv_nginx_site
    echo '  # Fancy URLs' >> $postactiv_nginx_site
    echo '  location @postactiv {' >> $postactiv_nginx_site
    echo '    rewrite ^(.*)$ /index.php?p=$1 last;' >> $postactiv_nginx_site
    echo '  }' >> $postactiv_nginx_site
    echo '' >> $postactiv_nginx_site
    echo '  # Restrict access that is unnecessary anyway' >> $postactiv_nginx_site
    echo '  location ~ /\.(ht|git) {' >> $postactiv_nginx_site
    echo '    deny all;' >> $postactiv_nginx_site
    echo '  }' >> $postactiv_nginx_site
    echo '' >> $postactiv_nginx_site
    # DO NOT ENABLE KEYBASE. postactiv really doesn't like having a .well-known directory
    echo '}' >> $postactiv_nginx_site
    function_check configure_php
    configure_php
    function_check create_site_certificate
    create_site_certificate $POSTACTIV_DOMAIN_NAME 'yes'
    # Ensure that the database gets backed up locally, if remote
    # backups are not being used
    function_check backup_databases_script_header
    backup_databases_script_header
    function_check backup_database_local
    backup_database_local postactiv
    function_check nginx_ensite
    nginx_ensite $POSTACTIV_DOMAIN_NAME
    # NOTE: For the typical case always enable SSL and only
    # disable it if in onion only mode. This is due to complexities
    # with the way URLs are generated by postactiv
    postactiv_ssl='always'
    if [[ $ONION_ONLY != 'no' ]]; then
        postactiv_ssl='never'
    fi
    POSTACTIV_SERVER=${POSTACTIV_DOMAIN_NAME}
    if [[ $ONION_ONLY != 'no' ]]; then
        POSTACTIV_SERVER=${POSTACTIV_ONION_HOSTNAME}
    fi
    # Create the configuration
    postactiv_installer=/var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/scripts/install_cli.php
    if [ ! -f $postactiv_installer ]; then
        rm -rf /var/www/$POSTACTIV_DOMAIN_NAME
        echo $'No postactiv commandline installer found'
        exit 53026
    fi
    ${postactiv_installer} --server "${POSTACTIV_SERVER}" \
                           --host="localhost" --database="postactiv" \
                           --dbtype=mysql --username="root" -v \
                           --password="$MARIADB_PASSWORD" \
                           --sitename=$"postactiv" --fancy='yes' \
                           --admin-nick="$MY_USERNAME" \
                           --admin-pass="$POSTACTIV_ADMIN_PASSWORD" \
                           --site-profile="community" \
                           --ssl=${postactiv_ssl}
    # There can be a lot of warnings here so the return value check is disabled
    #if [ ! "$?" = "0" ]; then
    #    # failed to install
    #    echo $'Could not install postactiv'
    #    exit 72357
    #fi
    # check postactiv has a config file
    postactiv_config_file=/var/www/$POSTACTIV_DOMAIN_NAME/htdocs/config.php
    if [ ! -f $postactiv_config_file ]; then
        drop_database postactiv
        rm -rf /var/www/$POSTACTIV_DOMAIN_NAME
        echo $'Postactiv config.php not found'
        exit 87586
    fi
    # Some useful settings
    if ! grep -q "Recommended postactiv settings" $postactiv_config_file; then
        echo "" >> $postactiv_config_file
        echo "// Recommended postactiv settings" >> $postactiv_config_file
        echo "\$config['thumbnail']['maxsize'] = 3000;" >> $postactiv_config_file
        echo "\$config['profile']['delete'] = true;" >> $postactiv_config_file
        echo "\$config['profile']['changenick'] = true;" >> $postactiv_config_file
        echo "\$config['public']['localonly'] = false;" >> $postactiv_config_file
        echo "addPlugin('StoreRemoteMedia');" >> $postactiv_config_file
        echo "\$config['queue']['enabled'] = true;" >> $postactiv_config_file
        echo "\$config['queue']['daemon'] = true;" >> $postactiv_config_file
        echo "\$config['ostatus']['hub_retries'] = 3;" >> $postactiv_config_file
    fi
    echo "\$config['mail']['domain'] = '${DEFAULT_DOMAIN_NAME}';" >> $postactiv_config_file
    # This improves performance
    sed -i "s|//\$config\['db'\]\['schemacheck'\].*|\$config\['db'\]\['schemacheck'\] = 'script';|g" $postactiv_config_file
    # remove the install script
    if [ -f /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/install.php ]; then
        rm /var/www/$POSTACTIV_DOMAIN_NAME/htdocs/install.php
    fi
    systemctl restart mariadb
    systemctl restart php7.0-fpm
    systemctl restart nginx
    ${PROJECT_NAME}-addemail -u $MY_USERNAME -e "noreply@$POSTACTIV_DOMAIN_NAME" -g postactiv --public no
    ${PROJECT_NAME}-pass -u $MY_USERNAME -a postactiv -p "$POSTACTIV_ADMIN_PASSWORD"
    gnusocial_block_user_script
    gnusocial_block_domain_script postactiv "$POSTACTIV_DOMAIN_NAME"
    set_completion_param "postactiv domain" "$POSTACTIV_DOMAIN_NAME"
    install_completed postactiv_main
}
function install_postactiv {
    if [ ! $ONION_ONLY ]; then
        ONION_ONLY='no'
    fi
    install_postactiv_main
    install_qvitter "$POSTACTIV_DOMAIN_NAME" "postactiv"
    install_gnusocial_markdown "$POSTACTIV_DOMAIN_NAME" "postactiv"
    install_gnusocial_plugin_sharings "$POSTACTIV_DOMAIN_NAME" "postactiv"
    install_gnusocial_plugin_sharings_theme "$POSTACTIV_DOMAIN_NAME" "postactiv"
    # Currently Pleroma won't install on ARM systems
    # because it uses node-sass which doesn't support ARM
    if [[ "$(arch)" == "arm"* || "$(arch)" == "aarch"* ]]; then
        echo -m $'WARNING: Pleroma currently does not support ARM '
        echo $'architecture, so it will not be installed'
    else
        function_check install_nodejs
        install_nodejs pleroma-gnusocial
        install_pleroma_front_end "postactiv" "$POSTACTIV_DOMAIN_NAME" "$POSTACTIV_BACKGROUND_IMAGE_URL" "$POSTACTIV_TITLE"
        install_gnusocial_default_background "postactiv" "$POSTACTIV_DOMAIN_NAME"
        postactiv_customise_logo
        sed -i 's|"theme":.*|"theme": "base16-harmonic16-light.css",|g' /var/www/${POSTACTIV_DOMAIN_NAME}/htdocs/static/config.json
    fi
    expire_gnusocial_posts "$POSTACTIV_DOMAIN_NAME" "postactiv" "$POSTACTIV_EXPIRE_MONTHS"
    # unleash the daemons!
    /etc/cron.hourly/postactiv-daemons
    systemctl restart nginx
    # Set qvitter to be the default UI. It's probably the most stable.
    # And doesn't forget logins
    gnusocial_use_qvitter postactiv
    if [ $POSTACTIV_BACKGROUND_IMAGE_URL ]; then
        pleroma_set_background_image_from_url /var/www/$POSTACTIV_DOMAIN_NAME/htdocs "$POSTACTIV_DOMAIN_NAME" "$POSTACTIV_BACKGROUND_IMAGE_URL" "$POSTACTIV_TITLE"
    fi
    APP_INSTALLED=1
}
# NOTE: deliberately there is no "exit 0"
 |