Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
freedombone-app-gnusocial 33.57 KiB
#!/bin/bash
#  _____               _           _
# |   __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# |   __|  _| -_| -_| . | . |     | . | . |   | -_|
# |__|  |_| |___|___|___|___|_|_|_|___|___|_|_|___|
#
#                              Freedom in the Cloud
#
# gnusocial application
#
# License
# =======
#
# Copyright (C) 2014-2019 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 social'

APP_CATEGORY=social

IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=1
NOT_ON_ONION=1

GNUSOCIAL_DOMAIN_NAME=
GNUSOCIAL_CODE=
GNUSOCIAL_ONION_PORT=8087
GNUSOCIAL_REPO="https://git.gnu.io/gnu/gnu-social.git"
GNUSOCIAL_COMMIT='67a9c0415c395d92adeb784413bb9a88fba7347f'
GNUSOCIAL_ADMIN_PASSWORD=

GNUSOCIAL_BACKGROUND_IMAGE_URL=

GNUSOCIAL_TITLE='Pleroma FE'

# Number of months after which posts expire
GNUSOCIAL_EXPIRE_MONTHS=3

GNUSOCIAL_SHORT_DESCRIPTION=$'Federated microblogging'
GNUSOCIAL_DESCRIPTION=$'Federated microblogging'
GNUSOCIAL_MOBILE_APP_URL='https://f-droid.org/packages/org.mariotaku.twidere/'

gnusocial_variables=(ONION_ONLY
                     GNUSOCIAL_DOMAIN_NAME
                     GNUSOCIAL_CODE
                     GNUSOCIAL_WELCOME_MESSAGE
                     GNUSOCIAL_BACKGROUND_IMAGE_URL
                     DDNS_PROVIDER
                     GNUSOCIAL_TITLE
                     GNUSOCIAL_EXPIRE_MONTHS
                     DEFAULT_DOMAIN_NAME
                     MY_USERNAME)

function change_default_domain_name_gnusocial {
    new_default_domain_name="$1"

    read_config_param GNUSOCIAL_DOMAIN_NAME
    gnusocial_config_file="/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/config.php"
    sed -i "s|\$config['mail']['domain'] =.*|\$config['mail']['domain'] = '${new_default_domain_name}';|g" "$gnusocial_config_file"
}

function logging_on_gnusocial {
    echo -n ''
}

function logging_off_gnusocial {
    echo -n ''
}

function gnusocial_fix_endless_reloads {
    # This fixes a bug introduced with commit 5f7032dfee1fd202c14e76a9f8b37af35d584901
    # and which causes OrFox to endlessly reload the page
    sed -i 's| && common_local_referer()||g' "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/lib/apiauthaction.php"
}

function remove_user_gnusocial {
    remove_username="$1"

    "${PROJECT_NAME}-pass" -u "$remove_username" --rmapp gnusocial

    function_check get_completion_param
    GNUSOCIAL_DOMAIN_NAME=$(get_completion_param "gnusocial domain")
    if [ -d "/var/www/$GNUSOCIAL_DOMAIN_NAME" ]; then
        cd "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs" || exit 23
        php scripts/deleteprofile.php -n "$remove_username" -y
    fi
}

function add_user_gnusocial {
    new_username="$1"
    new_user_password="$2"

    "${PROJECT_NAME}-pass" -u "$new_username" -a gnusocial -p "$new_user_password"

    GNUSOCIAL_DOMAIN_NAME=$(get_completion_param "gnusocial domain")
    if [ -d "/var/www/${GNUSOCIAL_DOMAIN_NAME}/htdocs" ]; then
        cd "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs" || exit 27
        php scripts/registeruser.php -n "$new_username" -w "$new_user_password" -e "$new_username@$HOSTNAME"
        "${PROJECT_NAME}-addemail" -u "$new_username" -e "noreply@$GNUSOCIAL_DOMAIN_NAME" -g gnusocial --public no
        echo '0'
    else
        echo '1'
    fi
}

function install_interactive_gnusocial {
    if [ ! "$ONION_ONLY" ]; then
        ONION_ONLY='no'
    fi

    if [[ $ONION_ONLY != "no" ]]; then
        GNUSOCIAL_DOMAIN_NAME='gnusocial.local'
    else
        GNUSOCIAL_DETAILS_COMPLETE=
        while [ ! $GNUSOCIAL_DETAILS_COMPLETE ]
        do
            data=$(mktemp 2>/dev/null)
            if [[ $DDNS_PROVIDER == *"freedns"* ]]; then
                dialog --backtitle $"Freedombone Configuration" \
                       --title $"GNU Social Configuration" \
                       --form $"\\nPlease enter your GNU Social 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 'GNUSOCIAL_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
                       $"Title:" 2 1 "$(grep "$GNUSOCIAL_TITLE" temp.cfg | awk -F '=' '{print $2}')" 2 25 255 255 \
                       $"Background image URL:" 3 1 "$(grep "$GNUSOCIAL_BACKGROUND_IMAGE_URL" temp.cfg | awk -F '=' '{print $2}')" 3 25 255 255 \
                       $"Code:" 4 1 "$(grep 'GNUSOCIAL_CODE' temp.cfg | awk -F '=' '{print $2}')" 4 25 33 255 \
                       2> "$data"
            else
                dialog --backtitle $"Freedombone Configuration" \
                       --title $"GNU Social Configuration" \
                       --form $"\\nPlease enter your GNU Social 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 'GNUSOCIAL_DOMAIN_NAME' temp.cfg | awk -F '=' '{print $2}')" 1 25 33 40 \
                       $"Title:" 2 1 "$(grep "$GNUSOCIAL_TITLE" temp.cfg | awk -F '=' '{print $2}')" 2 25 255 255 \
                       $"Background image URL:" 3 1 "$(grep "$GNUSOCIAL_BACKGROUND_IMAGE_URL" temp.cfg | awk -F '=' '{print $2}')" 3 25 255 255 \
                       2> "$data"
            fi
            sel=$?
            case $sel in
                1) rm -f "$data"
                   exit 1;;
                255) rm -f "$data"
                     exit 1;;
            esac
            GNUSOCIAL_DOMAIN_NAME=$(sed -n 1p < "$data")
            title=$(sed -n 2p < "$data")
            if [ ${#title} -gt 1 ]; then
                GNUSOCIAL_TITLE="$title"
            fi
            img_url=$(sed -n 3p < "$data")
            if [ ${#img_url} -gt 1 ]; then
                GNUSOCIAL_BACKGROUND_IMAGE_URL=$img_url
            fi
            if [ "$GNUSOCIAL_DOMAIN_NAME" ]; then
                if [[ "$GNUSOCIAL_DOMAIN_NAME" == "$HUBZILLA_DOMAIN_NAME" ]]; then
                    GNUSOCIAL_DOMAIN_NAME=""
                fi
                TEST_DOMAIN_NAME=$GNUSOCIAL_DOMAIN_NAME
                validate_domain_name
                if [[ "$TEST_DOMAIN_NAME" != "$GNUSOCIAL_DOMAIN_NAME" ]]; then
                    GNUSOCIAL_DOMAIN_NAME=
                    dialog --title $"Domain name validation" --msgbox "$TEST_DOMAIN_NAME" 15 50
                else
                    if [[ $DDNS_PROVIDER == *"freedns"* ]]; then
                        GNUSOCIAL_CODE=$(sed -n 4p < "$data")
                        validate_freedns_code "$GNUSOCIAL_CODE"
                        if [ ! "$VALID_CODE" ]; then
                            GNUSOCIAL_DOMAIN_NAME=
                        fi
                    fi
                fi
            fi
            if [ $GNUSOCIAL_DOMAIN_NAME ]; then
                GNUSOCIAL_DETAILS_COMPLETE="yes"
            fi
            rm -f "$data"
        done

        # remove any invalid characters
        if [ ${#GNUSOCIAL_TITLE} -gt 0 ]; then
            new_title=${GNUSOCIAL_TITLE//\'/}
            GNUSOCIAL_TITLE="$new_title"
        fi

        # save the results in the config file
        write_config_param "GNUSOCIAL_CODE" "$GNUSOCIAL_CODE"
        write_config_param "GNUSOCIAL_TITLE" "$GNUSOCIAL_TITLE"
        write_config_param "GNUSOCIAL_BACKGROUND_IMAGE_URL" "$GNUSOCIAL_BACKGROUND_IMAGE_URL"
    fi
    write_config_param "GNUSOCIAL_DOMAIN_NAME" "$GNUSOCIAL_DOMAIN_NAME"
    APP_INSTALLED=1
}

function change_password_gnusocial {
    curr_username="$1"
    new_user_password="$2"

    read_config_param 'GNUSOCIAL_DOMAIN_NAME'

    cd "/var/www/${GNUSOCIAL_DOMAIN_NAME}/htdocs/scripts" || exit 24
    php setpassword.php "$curr_username" "$new_user_password"

    "${PROJECT_NAME}-pass" -u "$curr_username" -a gnusocial -p "$new_user_password"
}

function gnusocial_create_database {
    if [ -f "$IMAGE_PASSWORD_FILE" ]; then
        GNUSOCIAL_ADMIN_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
    else
        if [ ! "$GNUSOCIAL_ADMIN_PASSWORD" ]; then
            GNUSOCIAL_ADMIN_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
        fi
    fi
    if [ ! "$GNUSOCIAL_ADMIN_PASSWORD" ]; then
        return
    fi

    function_check create_database
    create_database gnusocial "$GNUSOCIAL_ADMIN_PASSWORD" "$MY_USERNAME"
}

function reconfigure_gnusocial {
    echo -n ''
}

function gnusocial_set_background_image {
    GNUSOCIAL_DOMAIN_NAME=$(get_completion_param "gnusocial domain")

    data=$(mktemp 2>/dev/null)
    dialog --title $"GNU Social" \
           --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
                GNUSOCIAL_BACKGROUND_IMAGE_URL="$temp_background"
                write_config_param "GNUSOCIAL_BACKGROUND_IMAGE_URL" "$GNUSOCIAL_BACKGROUND_IMAGE_URL"
                if [[ $(pleroma_set_background_image_from_url "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs" "$GNUSOCIAL_DOMAIN_NAME" "$GNUSOCIAL_BACKGROUND_IMAGE_URL" "$GNUSOCIAL_TITLE" | tail -n 1) == "0" ]]; then
                    dialog --title $"Set GNU Social login background" \
                           --msgbox $"The background image has been set" 6 60
                fi
            fi
           ;;
    esac
    rm -f "$data"
}

function gnusocial_set_title {
    data=$(mktemp 2>/dev/null)
    dialog --title $"GNU Social" \
           --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
                GNUSOCIAL_TITLE="$new_title"
                GNUSOCIAL_DOMAIN_NAME=$(get_completion_param "gnusocial domain")
                write_config_param "GNUSOCIAL_TITLE" "$GNUSOCIAL_TITLE"
                cd "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/static" || exit 24
                sed -i "s|\"name\":.*|\"name\": \"${GNUSOCIAL_TITLE}\",|g" config.json
                dialog --title $"Set GNU Social title" \
                       --msgbox $"The title has been set" 6 60
            fi
           ;;
    esac
    rm -f "$data"
}

function gnusocial_set_expire_months {
    GNUSOCIAL_DOMAIN_NAME=$(get_completion_param "gnusocial domain")
    read_config_param "GNUSOCIAL_EXPIRE_MONTHS"

    data=$(mktemp 2>/dev/null)
    dialog --title $"GNU Social" \
           --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 "$GNUSOCIAL_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
                    rm -f "$data"
                    return
                fi
                # should be a number
                re='^[0-9]+$'
                if ! [[ $new_expiry_months =~ $re ]] ; then
                    rm -f "$data"
                    return
                fi
                # set the new value
                GNUSOCIAL_EXPIRE_MONTHS=$new_expiry_months
                write_config_param "GNUSOCIAL_EXPIRE_MONTHS" "$GNUSOCIAL_EXPIRE_MONTHS"
                expire_gnusocial_posts "$GNUSOCIAL_DOMAIN_NAME" "gnusocial" "$GNUSOCIAL_EXPIRE_MONTHS"
                dialog --title $"Set GNU Social post expiry period" \
                       --msgbox $"Expiry period set to $GNUSOCIAL_EXPIRE_MONTHS months" 6 60
            fi
           ;;
    esac
    rm -f "$data"
}

function configure_interactive_gnusocial {
    read_config_param GNUSOCIAL_EXPIRE_MONTHS
    while true
    do
        W=(1 $"Set a background image"
           2 $"Set the title"
           3 $"Set post expiry period (currently $GNUSOCIAL_EXPIRE_MONTHS months)"
           4 $"Select Qvitter user interface"
           5 $"Select Pleroma user interface"
           6 $"Select Classic user interface")

        # shellcheck disable=SC2068
        selection=$(dialog --backtitle $"Freedombone Administrator Control Panel" --title $"GNU Social" --menu $"Choose an operation, or ESC to exit:" 15 60 6 "${W[@]}" 3>&2 2>&1 1>&3)

        if [ ! "$selection" ]; then
            break
        fi

        case $selection in
            1) gnusocial_set_background_image;;
            2) gnusocial_set_title;;
            3) gnusocial_set_expire_months;;
            4) gnusocial_use_qvitter gnusocial;;
            5) gnusocial_use_pleroma gnusocial;;
            6) gnusocial_use_classic gnusocial;;
        esac
    done
}

function upgrade_gnusocial {
    CURR_GNUSOCIAL_COMMIT=$(get_completion_param "gnusocial commit")
    if [[ "$CURR_GNUSOCIAL_COMMIT" == "$GNUSOCIAL_COMMIT" ]]; then
        return
    fi

    if grep -q "gnusocial domain" "$COMPLETION_FILE"; then
        GNUSOCIAL_DOMAIN_NAME=$(get_completion_param "gnusocial domain")
    fi

    # update to the next commit
    function_check set_repo_commit
    set_repo_commit "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs" "gnusocial commit" "$GNUSOCIAL_COMMIT" "$GNUSOCIAL_REPO"

    gnusocial_fix_endless_reloads

    # Ensure that installation script is removed
    if [ -f "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/install.php" ]; then
        rm "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/install.php"
    fi

    if [ -f "/var/www/${GNUSOCIAL_DOMAIN_NAME}/htdocs/index_qvitter.php" ]; then
        if [ -f "/var/www/${GNUSOCIAL_DOMAIN_NAME}/htdocs/index.php" ]; then
            # shellcheck disable=SC2086
            mv /var/www/${GNUSOCIAL_DOMAIN_NAME}/htdocs/index.php /var/www/${GNUSOCIAL_DOMAIN_NAME}/htdocs/index_qvitter.php
        fi
    fi

    gnusocial_block_user_script
    gnusocial_block_domain_script gnusocial "$GNUSOCIAL_DOMAIN_NAME"

    gnusocial_hourly_script gnusocial "$GNUSOCIAL_DOMAIN_NAME"
    if [ -d "$INSTALL_DIR/pleroma" ]; then
        upgrade_pleroma_frontend "$GNUSOCIAL_DOMAIN_NAME" "gnusocial" "$GNUSOCIAL_BACKGROUND_IMAGE_URL" "$GNUSOCIAL_TITLE"
    fi
    install_gnusocial_default_background "gnusocial" "$GNUSOCIAL_DOMAIN_NAME"
    chown -R www-data:www-data "/var/www/${GNUSOCIAL_DOMAIN_NAME}/htdocs"
}


function backup_local_gnusocial {
    GNUSOCIAL_DOMAIN_NAME='gnusocial'
    if grep -q "gnusocial domain" "$COMPLETION_FILE"; then
        GNUSOCIAL_DOMAIN_NAME=$(get_completion_param "gnusocial domain")
    fi

    # don't backup more data than we need to
    /etc/cron.daily/gnusocial-expire

    source_directory="/var/www/${GNUSOCIAL_DOMAIN_NAME}/htdocs/backup"
    if [ ! -d "$source_directory" ]; then
        mkdir "$source_directory"
    fi
    cp -p "/var/www/${GNUSOCIAL_DOMAIN_NAME}/htdocs/config.php" "$source_directory"
    if [ -d "/var/www/${GNUSOCIAL_DOMAIN_NAME}/htdocs/static" ]; then
        cp -rp "/var/www/${GNUSOCIAL_DOMAIN_NAME}/htdocs/static" "$source_directory"
    fi

    function_check suspend_site
    suspend_site "${GNUSOCIAL_DOMAIN_NAME}"

    function_check backup_directory_to_usb
    dest_directory=gnusocialconfig
    backup_directory_to_usb "$source_directory" "$dest_directory"

    source_directory="/var/www/${GNUSOCIAL_DOMAIN_NAME}/htdocs/file"
    dest_directory=gnusocialfile
    backup_directory_to_usb "$source_directory" "$dest_directory"

    function_check backup_database_to_usb
    backup_database_to_usb gnusocial

    function_check restart_site
    restart_site
}

function restore_local_gnusocial {
    if ! grep -q "gnusocial domain" "$COMPLETION_FILE"; then
        return
    fi
    GNUSOCIAL_DOMAIN_NAME=$(get_completion_param "gnusocial domain")
    if [ "$GNUSOCIAL_DOMAIN_NAME" ]; then
        echo $"Restoring gnusocial"
        temp_restore_dir=/root/tempgnusocial
        gnusocial_dir="/var/www/${GNUSOCIAL_DOMAIN_NAME}/htdocs"
        # stop the daemons
        cd "$gnusocial_dir" || exit 24
        scripts/stopdaemons.sh

        function_check gnusocial_create_database
        gnusocial_create_database

        restore_database gnusocial
        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 gnusocialconfig
        if [ -d $temp_restore_dir ]; then
            if [ -d "$temp_restore_dir$gnusocial_dir" ]; then
                cp "$temp_restore_dir$gnusocial_dir/backup/config.php" "$gnusocial_dir/"
                cp -rp "$temp_restore_dir$gnusocial_dir/static" "$gnusocial_dir/"
            else
                cp "$temp_restore_dir/backup/config.php" "$gnusocial_dir/"
                if [ ! -d "$gnusocial_dir/static" ]; then
                    mkdir "$gnusocial_dir/static"
                fi
                cp -rp "$temp_restore_dir/static/"* "$gnusocial_dir/static/"
            fi
            chown www-data:www-data "$gnusocial_dir/config.php"
            chown -R www-data:www-data "$gnusocial_dir/static"
            rm -rf $temp_restore_dir
        fi

        restore_directory_from_usb $temp_restore_dir gnusocialfile
        if [ -d "$temp_restore_dir" ]; then
            if [ -d "$temp_restore_dir$gnusocial_dir/file" ]; then
                cp -rp "$temp_restore_dir$gnusocial_dir/file" "$gnusocial_dir/"
            else
                if [ ! -d "$gnusocial_dir/file" ]; then
                    mkdir "$gnusocial_dir/file"
                fi
                cp -rp "$temp_restore_dir/"* "$gnusocial_dir/file"
            fi
            chown -R www-data:www-data "$gnusocial_dir/file"
            rm -rf "$temp_restore_dir"
        fi

        gnusocial_update_after_restore gnusocial "${GNUSOCIAL_DOMAIN_NAME}"

        echo $"Restore of gnusocial complete"
    fi
}

function backup_remote_gnusocial {
    echo -n ''
}

function restore_remote_gnusocial {
    echo -n ''
}

function remove_gnusocial {
    if [ ${#GNUSOCIAL_DOMAIN_NAME} -eq 0 ]; then
        return
    fi
    function_check remove_nodejs
    remove_nodejs pleroma-gnusocial

    read_config_param "GNUSOCIAL_DOMAIN_NAME"
    read_config_param "MY_USERNAME"
    echo "Removing $GNUSOCIAL_DOMAIN_NAME"
    nginx_dissite "$GNUSOCIAL_DOMAIN_NAME"
    remove_certs "$GNUSOCIAL_DOMAIN_NAME"
    if [ -f /etc/cron.hourly/gnusocial-daemons ]; then
        rm /etc/cron.hourly/gnusocial-daemons
    fi
    if [ -f "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/scripts/stopdaemons.sh" ]; then
        cd "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs" || exit 26
        scripts/stopdaemons.sh
    fi
    kill_pid=$(pgrep "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/scripts/queuedaemon.php" | head -n 1)
    kill -9 "$kill_pid"
    pkill "$GNUSOCIAL_DOMAIN_NAME/htdocs/scripts/queuedaemon.php"

    if [ -d "/var/www/$GNUSOCIAL_DOMAIN_NAME" ]; then
        rm -rf "/var/www/$GNUSOCIAL_DOMAIN_NAME"
    fi
    if [ -f "/etc/nginx/sites-available/$GNUSOCIAL_DOMAIN_NAME" ]; then
        rm "/etc/nginx/sites-available/$GNUSOCIAL_DOMAIN_NAME"
    fi
    function_check drop_database
    drop_database gnusocial
    function_check remove_onion_service
    remove_onion_service gnusocial ${GNUSOCIAL_ONION_PORT}
    if grep -q "gnusocial" /etc/crontab; then
        sed -i "/gnusocial/d" /etc/crontab
    fi
    remove_app gnusocial
    remove_completion_param install_gnusocial
    sed -i '/gnusocial/d' "$COMPLETION_FILE"
    remove_backup_database_local gnusocial

    if grep -q 'gnusocial-firewall' /etc/crontab; then
        sed -i '/gnusocial-firewall/d' /etc/crontab
    fi
    sed -i 's|mysqli.allow_persistent.*|mysqli.allow_persistent = On|g' "/etc/php/${PHP_VERSION}/cli/php.ini"
    sed -i 's|mysqli.reconnect.*|mysqli.reconnect = Off|g' "/etc/php/${PHP_VERSION}/cli/php.ini"

    if [ -f /usr/bin/gnusocial-firewall ]; then
        rm /usr/bin/gnusocial-firewall
    fi

    if [ -f /etc/cron.daily/gnusocial-expire ]; then
        rm /etc/cron.daily/gnusocial-expire
    fi
    if [ -f /usr/bin/gnusocial-expire-posts ]; then
        rm /usr/bin/gnusocial-expire-posts
    fi

    function_check remove_ddns_domain
    remove_ddns_domain "$GNUSOCIAL_DOMAIN_NAME"
}

function install_gnusocial_main {
    if [ ! "$GNUSOCIAL_DOMAIN_NAME" ]; then
        echo $'No domain name was given for gnusocial'
        exit 73
    fi

    if [[ $(app_is_installed gnusocial_main) == "1" ]]; then
        return
    fi

    gnusocial_hourly_script gnusocial "$GNUSOCIAL_DOMAIN_NAME"

    function_check install_mariadb
    install_mariadb

    function_check get_mariadb_password
    get_mariadb_password

    function_check repair_databases_script
    repair_databases_script

    $INSTALL_PACKAGES php-gettext php-curl php-gd php-mysql git curl
    $INSTALL_PACKAGES memcached php-memcached php-intl exiftool libfcgi0ldbl

    if [ ! -d "/var/www/$GNUSOCIAL_DOMAIN_NAME" ]; then
        mkdir "/var/www/$GNUSOCIAL_DOMAIN_NAME"
    fi
    if [ ! -d "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs" ]; then

        if [ -d /repos/gnusocial ]; then
            mkdir -p "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs"
            cp -r -p /repos/gnusocial/. "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs"
            cd "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs" || exit 24
            git pull
        else
            function_check git_clone
            git_clone "$GNUSOCIAL_REPO" "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs"
        fi

        if [ ! -d "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs" ]; then
            echo $'Unable to clone gnusocial repo'
            exit 87
        fi
    fi

    cd "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs" || exit 26
    git checkout "$GNUSOCIAL_COMMIT" -b "$GNUSOCIAL_COMMIT"
    set_completion_param "gnusocial commit" "$GNUSOCIAL_COMMIT"

    gnusocial_fix_endless_reloads

    chmod g+w "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs"
    chmod a+w "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/avatar"
    chmod a+w "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/file"
    chown -R www-data:www-data "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs"
    chmod +x "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/scripts/maildaemon.php"

    function_check gnusocial_create_database
    gnusocial_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/$GNUSOCIAL_DOMAIN_NAME/htdocs/scripts/maildaemon.php" /etc/aliases; then
        echo "*: /var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/scripts/maildaemon.php" >> /etc/aliases
    fi

    function_check add_ddns_domain
    add_ddns_domain "$GNUSOCIAL_DOMAIN_NAME"

    GNUSOCIAL_ONION_HOSTNAME=$(add_onion_service gnusocial 80 ${GNUSOCIAL_ONION_PORT})

    gnusocial_nginx_site=/etc/nginx/sites-available/$GNUSOCIAL_DOMAIN_NAME
    if [[ $ONION_ONLY == "no" ]]; then
        function_check nginx_http_redirect
        nginx_http_redirect "$GNUSOCIAL_DOMAIN_NAME" "index index.php"
        { echo 'server {';
          echo '  listen 443 ssl;';
          echo '  #listen [::]:443 ssl;';
          echo "  server_name $GNUSOCIAL_DOMAIN_NAME;";
          echo ''; } >> "$gnusocial_nginx_site"
        function_check nginx_compress
        nginx_compress "$GNUSOCIAL_DOMAIN_NAME"
        echo '' >> "$gnusocial_nginx_site"
        echo '  # Security' >> "$gnusocial_nginx_site"
        function_check nginx_ssl
        nginx_ssl "$GNUSOCIAL_DOMAIN_NAME"

        function_check nginx_security_options
        nginx_security_options "$GNUSOCIAL_DOMAIN_NAME"
        nginx_robots "$GNUSOCIAL_DOMAIN_NAME"

        { echo '  add_header Strict-Transport-Security max-age=15768000;';
          echo '';
          echo '  # Logs';
          echo '  access_log /dev/null;';
          echo '  error_log /dev/null;';
          echo '';
          echo '  # Root';
          echo "  root /var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs;";
          echo '';
          echo '  # Index';
          echo '  index index.php;';
          echo '';
          echo '  # PHP';
          echo '  location ~ \.php {';
          echo '    client_max_body_size 50m;';
          echo '    client_body_buffer_size 50m;';
          echo '    include snippets/fastcgi-php.conf;';
          echo "    fastcgi_pass unix:/var/run/php/php${PHP_VERSION}-fpm.sock;";
          echo '    fastcgi_read_timeout 30;';
          echo '  }';
          echo '';
          echo '  # Location';
          echo '  location / {'; } >> "$gnusocial_nginx_site"
        function_check nginx_limits
        nginx_limits "$GNUSOCIAL_DOMAIN_NAME" '15m'
        { echo "    try_files \$uri \$uri/ @gnusocial;";
          echo '  }';
          echo '';
          echo '  # Fancy URLs';
          echo '  location @gnusocial {';
          echo '    client_max_body_size 50m;';
          echo '    client_body_buffer_size 50m;';
          echo "    rewrite ^(.*)\$ /index.php?p=\$1 last;";
          echo '  }';
          echo '';
          echo '  # Restrict access that is unnecessary anyway';
          echo '  location ~ /\.(ht|git) {';
          echo '    deny all;';
          echo '  }';
          echo '}'; } >> "$gnusocial_nginx_site"
    else
        echo -n '' > "$gnusocial_nginx_site"
    fi
    { echo 'server {';
      echo "    listen 127.0.0.1:$GNUSOCIAL_ONION_PORT default_server;";
      echo '    port_in_redirect off;';
      echo "    server_name $GNUSOCIAL_ONION_HOSTNAME;";
      echo ''; } >> "$gnusocial_nginx_site"
    function_check nginx_compress
    nginx_compress "$GNUSOCIAL_DOMAIN_NAME"
    echo '' >> "$gnusocial_nginx_site"
    function_check nginx_security_options
    nginx_security_options "$GNUSOCIAL_DOMAIN_NAME"
    { echo '';
      echo '  # Logs';
      echo '  access_log /dev/null;';
      echo '  error_log /dev/null;';
      echo '';
      echo '  # Root';
      echo "  root /var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs;";
      echo '';
      echo '  # Index';
      echo '  index index.php;';
      echo '';
      echo '  # PHP';
      echo '  location ~ \.php {';
      echo '    client_max_body_size 50m;';
      echo '    client_body_buffer_size 50m;';
      echo '    include snippets/fastcgi-php.conf;';
      echo "    fastcgi_pass unix:/var/run/php/php${PHP_VERSION}-fpm.sock;";
      echo '    fastcgi_read_timeout 30;';
      echo '  }';
      echo '';
      echo '  # Location';
      echo '  location / {'; } >> "$gnusocial_nginx_site"
    function_check nginx_limits
    nginx_limits "$GNUSOCIAL_DOMAIN_NAME" '15m'
    { echo "    try_files \$uri \$uri/ @gnusocial;";
      echo '  }';
      echo '';
      echo '  # Fancy URLs';
      echo '  location @gnusocial {';
      echo '    client_max_body_size 50m;';
      echo '    client_body_buffer_size 50m;';
      echo "    rewrite ^(.*)\$ /index.php?p=\$1 last;";
      echo '  }';
      echo '';
      echo '  # Restrict access that is unnecessary anyway';
      echo '  location ~ /\.(ht|git) {';
      echo '    deny all;';
      echo '  }';
      echo '}'; } >> "$gnusocial_nginx_site"

    function_check gnusocial_set_limits
    gnusocial_set_limits "$gnusocial_nginx_site"

    function_check configure_php
    configure_php

    function_check create_site_certificate
    create_site_certificate "$GNUSOCIAL_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 gnusocial

    function_check nginx_ensite
    nginx_ensite "$GNUSOCIAL_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 gnusocial
    gnusocial_ssl='always'
    if [[ $ONION_ONLY != 'no' ]]; then
        gnusocial_ssl='never'
    fi

    GNUSOCIAL_SERVER=${GNUSOCIAL_DOMAIN_NAME}
    if [[ $ONION_ONLY != 'no' ]]; then
        GNUSOCIAL_SERVER=${GNUSOCIAL_ONION_HOSTNAME}
    fi

    # Create the configuration
    gnusocial_installer="/var/www/${GNUSOCIAL_DOMAIN_NAME}/htdocs/scripts/install_cli.php"
    if [ ! -f "$gnusocial_installer" ]; then
        rm -rf "/var/www/$GNUSOCIAL_DOMAIN_NAME"
        echo $'No gnusocial commandline installer found'
        exit 53
    fi
    ${gnusocial_installer} --server "${GNUSOCIAL_SERVER}" \
                           --host="localhost" --database="gnusocial" \
                           --dbtype=mysql --username="root" -v \
                           --password="$MARIADB_PASSWORD" \
                           --sitename=$"GNU Social" --fancy='yes' \
                           --admin-nick="$MY_USERNAME" \
                           --admin-pass="$GNUSOCIAL_ADMIN_PASSWORD" \
                           --site-profile="community" \
                           --ssl=${gnusocial_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 GNU Social'
    #    exit 72
    #fi

    # check gnusocial has a config file
    gnusocial_config_file="/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/config.php"
    if [ ! -f "$gnusocial_config_file" ]; then
        drop_database gnusocial
        rm -rf "/var/www/$GNUSOCIAL_DOMAIN_NAME"
        echo $'Gnusocial config.php not found'
        exit 87
    fi

    # Some useful settings
    if ! grep -q "Recommended GNU social settings" "$gnusocial_config_file"; then
        { echo "";
          echo "// Recommended GNU social settings";
          echo "\$config['thumbnail']['maxsize'] = 3000;";
          echo "\$config['profile']['delete'] = true;";
          echo "\$config['profile']['changenick'] = true;";
          echo "\$config['public']['localonly'] = false;";
          echo "addPlugin('StoreRemoteMedia');";
          echo "\$config['queue']['enabled'] = true;";
          echo "\$config['queue']['daemon'] = true;";
          echo "\$config['ostatus']['hub_retries'] = 3;"; } >> "$gnusocial_config_file"
    fi

    # turn off logging
    echo "\$config['site']['logfile'] = '/dev/null';" >> "$gnusocial_config_file"
    echo "\$config['mail']['domain'] = '${DEFAULT_DOMAIN_NAME}';" >> "$gnusocial_config_file"

    # This improves performance
    sed -i "s|//\$config\\['db'\\]\\['schemacheck'\\].*|\$config\\['db'\\]\\['schemacheck'\\] = 'script';|g" "$gnusocial_config_file"

    # remove the install script
    if [ -f "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/install.php" ]; then
        rm "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs/install.php"
    fi

    # php configuration
    sed -i 's|mysqli.allow_persistent.*|mysqli.allow_persistent = On|g' "/etc/php/${PHP_VERSION}/cli/php.ini"
    sed -i 's|mysqli.reconnect.*|mysqli.reconnect = On|g' "/etc/php/${PHP_VERSION}/cli/php.ini"

    systemctl restart mariadb
    # shellcheck disable=SC2086
    systemctl restart php${PHP_VERSION}-fpm
    systemctl restart nginx

    "${PROJECT_NAME}-addemail" -u "$MY_USERNAME" -e "noreply@$GNUSOCIAL_DOMAIN_NAME" -g gnusocial --public no

    "${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a gnusocial -p "$GNUSOCIAL_ADMIN_PASSWORD"
    gnusocial_block_user_script
    gnusocial_block_domain_script gnusocial "$GNUSOCIAL_DOMAIN_NAME"

    set_completion_param "gnusocial domain" "$GNUSOCIAL_DOMAIN_NAME"

    install_completed gnusocial_main
}

function install_gnusocial {
    if [[ "$ONION_ONLY" != 'no' ]]; then
        return
    fi

    increment_app_install_progress

    install_gnusocial_main

    increment_app_install_progress

    expire_gnusocial_posts "$GNUSOCIAL_DOMAIN_NAME" "gnusocial" "$GNUSOCIAL_EXPIRE_MONTHS"

    increment_app_install_progress

    install_qvitter "$GNUSOCIAL_DOMAIN_NAME" "gnusocial"

    increment_app_install_progress

    install_gnusocial_markdown "$GNUSOCIAL_DOMAIN_NAME" "gnusocial"

    increment_app_install_progress

    install_gnusocial_plugin_sharings "$GNUSOCIAL_DOMAIN_NAME" "gnusocial"

    increment_app_install_progress

    install_gnusocial_plugin_sharings_theme "$GNUSOCIAL_DOMAIN_NAME" "gnusocial"

    increment_app_install_progress

    # 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 "gnusocial" "$GNUSOCIAL_DOMAIN_NAME" "$GNUSOCIAL_BACKGROUND_IMAGE_URL" "$GNUSOCIAL_TITLE"
        install_gnusocial_default_background "gnusocial" "$GNUSOCIAL_DOMAIN_NAME"
        sed -i 's|"theme":.*|"theme": "base16-apathy.css",|g' "/var/www/${GNUSOCIAL_DOMAIN_NAME}/htdocs/static/config.json"
    fi

    # this has to be run as root initially, otherwise database tables
    # don't get created
    cd "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs" || exit 23
    php scripts/checkschema.php
    sh scripts/startdaemons.sh

    increment_app_install_progress

    /etc/cron.hourly/gnusocial-daemons

    increment_app_install_progress

    systemctl restart nginx

    # Set qvitter to be the default UI. It's probably the most stable.
    # And doesn't forget logins
    gnusocial_use_qvitter gnusocial

    increment_app_install_progress

    if [ "$GNUSOCIAL_BACKGROUND_IMAGE_URL" ]; then
        pleroma_set_background_image_from_url "/var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs" "$GNUSOCIAL_DOMAIN_NAME" "$GNUSOCIAL_BACKGROUND_IMAGE_URL" "$GNUSOCIAL_TITLE"
    fi

    APP_INSTALLED=1
}

# NOTE: deliberately there is no "exit 0"