freedombone/src/freedombone-utils-gnusocial...

863 lines
41 KiB
Bash
Executable File

#!/bin/bash
#
# .---. . .
# | | |
# |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
# | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
# ' ' --' --' -' - -' ' ' -' -' -' ' - --'
#
# Freedom in the Cloud
#
# functions common to GNU Social server varieties
#
# License
# =======
#
# Copyright (C) 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/>.
QVITTER_THEME_REPO="https://git.gnu.io/h2p/Qvitter"
QVITTER_THEME_COMMIT='0eff0c80ffe32013a0cd2016b8ffbee768338b69'
PLEROMA_REPO="https://gitgud.io/lambadalambda/pleroma-fe"
PLEROMA_COMMIT='76624c9e4ba2dd648af0effdfe14c24b4e858f8f'
ARMADILLO_REPO="https://git.postactiv.com/maiya/Armadillo"
ARMADILLO_COMMIT='ec3938a678f373156c4cbf37926c9a5ab68222c4'
function qvitter_update_background {
domain_name="$1"
ext=$2
if [ -d /var/www/${domain_name}/htdocs/local/plugins/Qvitter/img ]; then
cp bg_custom.${ext} /var/www/${domain_name}/htdocs/local/plugins/Qvitter/img
if ! grep -q "bg_custom.${ext}" /var/www/${domain_name}/htdocs/config.php; then
if grep -q 'sitebackground' /var/www/${domain_name}/htdocs/config.php; then
sed -i '/sitebackground/d' /var/www/${domain_name}/htdocs/config.php
fi
echo "\$config['site']['qvitter']['sitebackground'] = 'img/bg_custom.${ext}';" >> /var/www/${domain_name}/htdocs/config.php
fi
fi
}
function pleroma_set_background_image_from_url {
domain_name="$1"
url="$2"
title="$3"
if [ ${#domain_name} -eq 0 ]; then
echo "1"
return
fi
ext=
if [ ${#url} -gt 0 ]; then
if [[ "$url" == *".jpeg" || "$url" == *".jpg" ]]; then
ext="jpg"
fi
if [[ "$url" == *".png" ]]; then
ext="png"
fi
if [[ "$url" == *".gif" ]]; then
ext="gif"
fi
fi
if [ ${#ext} -gt 0 ]; then
cd /var/www/${domain_name}/htdocs
# remove any existing image
if [ -f bg_custom.${ext} ]; then
rm bg_custom.${ext}
fi
# get the new image
wget "$url" -O bg_custom.${ext}
if [ ! -f bg_custom.${ext} ]; then
echo "$url"
echo $'Custom background image for pleroma could not be downloaded'
echo "1"
return
fi
if [ -d /var/www/${domain_name}/htdocs/static ]; then
cd /var/www/${domain_name}/htdocs/static
cp ../bg_custom.${ext} bg_custom.${ext}
sed -i "s|\"background\":.*|\"background\": \"/static/bg_custom.${ext}\",|g" config.json
fi
qvitter_update_background ${domain_name} ${ext}
rm bg_custom.${ext}
else
echo "2"
return
fi
# customise the logo
if [ -f /var/www/${domain_name}/htdocs/static/logo.png ]; then
if [ -f ~/freedombone/img/logo_fbone3.png ]; then
cp ~/freedombone/img/logo_fbone3.png /var/www/${domain_name}/htdocs/static/logo.png
else
if [ -f /home/$MY_USERNAME/freedombone/img/logo_fbone3.png ]; then
cp /home/$MY_USERNAME/freedombone/img/logo_fbone3.png /var/www/${domain_name}/htdocs/static/logo.png
fi
fi
fi
# customise the title
if [ -f /var/www/${domain_name}/htdocs/static/config.json ]; then
sed -i "s|\"name\":.*|\"name\": \"${title}\",|g" /var/www/${domain_name}/htdocs/static/config.json
fi
echo "0"
}
function armadillo_set_background_image_from_url {
domain_name="$1"
url="$2"
if [ ${#domain_name} -eq 0 ]; then
echo "1"
return
fi
ext=
if [ ${#url} -gt 0 ]; then
if [[ "$url" == *".jpeg" || "$url" == *".jpg" ]]; then
ext="jpg"
fi
if [[ "$url" == *".png" ]]; then
ext="png"
fi
if [[ "$url" == *".gif" ]]; then
ext="gif"
fi
fi
if [ ${#ext} -gt 0 ]; then
cd /var/www/${domain_name}/htdocs
# remove any existing image
if [ -f bg_custom.${ext} ]; then
rm bg_custom.${ext}
fi
# get the new image
wget "$url" -O bg_custom.${ext}
if [ ! -f bg_custom.${ext} ]; then
echo "$url"
echo $'Custom background image for armadillo could not be downloaded'
echo "1"
return
fi
if [ -d /var/www/${domain_name}/htdocs/armadillo ]; then
cd /var/www/${domain_name}/htdocs/armadillo
cp ../bg_custom.${ext} media/img/bg_custom.${ext}
sed -i "s|\"background\":.*|\"background\": \"armadillo/media/img/background.jpeg\"," config.json
fi
qvitter_update_background ${domain_name} ${ext}
rm bg_custom.${ext}
else
echo "2"
return
fi
echo "0"
}
function install_qvitter {
domain_name=$1
app_name=$2
# update to the next commit
function_check set_repo_commit
set_repo_commit /var/www/${domain_name}/htdocs/local/plugins/Qvitter "${app_name} theme commit" "$QVITTER_THEME_COMMIT" $QVITTER_THEME_REPO
# customise with project logo
if [ -f /var/www/${domain_name}/htdocs/local/plugins/Qvitter/img/sprite.png ]; then
if [ -f ~/freedombone/img/logo_fbone3.png ]; then
cp ~/freedombone/img/logo_fbone3.png /var/www/${domain_name}/htdocs/local/plugins/Qvitter/img/sprite.png
else
if [ -f /home/$MY_USERNAME/freedombone/img/logo_fbone3.png ]; then
cp /home/$MY_USERNAME/freedombone/img/logo_fbone3.png /var/www/${domain_name}/htdocs/local/plugins/Qvitter/img/sprite.png
fi
fi
fi
if [[ $(app_is_installed "${app_name}_theme") == "1" ]]; then
return
fi
apt-get -yq install wget
if [ ! -d /var/www/${domain_name}/htdocs/local/plugins ]; then
mkdir -p /var/www/${domain_name}/htdocs/local/plugins
fi
cd /var/www/${domain_name}/htdocs/local/plugins
function_check git_clone
git_clone $QVITTER_THEME_REPO Qvitter
cd /var/www/${domain_name}/htdocs/local/plugins/Qvitter
git checkout $QVITTER_THEME_COMMIT -b $QVITTER_THEME_COMMIT
config_file=/var/www/${domain_name}/htdocs/config.php
if ! grep -q "addPlugin('Qvitter')" $config_file; then
echo "" >> $config_file
echo "// Qvitter settings" >> $config_file
echo "addPlugin('Qvitter');" >> $config_file
echo "\$config['site']['qvitter']['enabledbydefault'] = true;" >> $config_file
echo "\$config['site']['qvitter']['defaultbackgroundcolor'] = '#f4f4f4';" >> $config_file
echo "\$config['site']['qvitter']['defaultlinkcolor'] = '#0084B4';" >> $config_file
echo "\$config['site']['qvitter']['timebetweenpolling'] = 30000; // 30 secs" >> $config_file
if [[ $ONION_ONLY == 'no' ]]; then
echo "\$config['site']['qvitter']['urlshortenerapiurl'] = 'http://qttr.at/shortener.php';" >> $config_file
echo "\$config['site']['qvitter']['urlshortenersignature'] = 'b6afeec983';" >> $config_file
fi
echo "\$config['site']['qvitter']['favicon'] = 'img/favicon.ico?v=4';" >> $config_file
echo "\$config['site']['qvitter']['sprite'] = Plugin::staticPath('Qvitter', '').'img/sprite.png?v=40';" >> $config_file
echo "\$config['site']['qvitter']['enablewelcometext'] = false;" >> $config_file
echo "\$config['site']['qvitter']['blocked_ips'] = array();" >> $config_file
fi
# customise with project logo
if [ -f /var/www/${domain_name}/htdocs/local/plugins/Qvitter/img/sprite.png ]; then
if [ -f ~/freedombone/img/gnusocial_sprite.png ]; then
cp ~/freedombone/img/gnusocial_sprite.png /var/www/${domain_name}/htdocs/local/plugins/Qvitter/img/sprite.png
fi
fi
chown -R www-data:www-data /var/www/${domain_name}/htdocs
cd /var/www/${domain_name}/htdocs
php scripts/upgrade.php
php scripts/checkschema.php
chown -R www-data:www-data /var/www/${domain_name}/htdocs
set_completion_param "${app_name} theme commit" "$QVITTER_THEME_COMMIT"
install_completed ${app_name}_theme
}
function install_gnusocial_default_background {
gnusocial_type=$1
domain_name=$2
# customise the logo
if [ -f /var/www/${domain_name}/htdocs/static/logo.png ]; then
if [ -f ~/freedombone/img/logo_fbone3.png ]; then
cp ~/freedombone/img/logo_fbone3.png /var/www/${domain_name}/htdocs/static/logo.png
else
if [ -f /home/$MY_USERNAME/freedombone/img/logo_fbone3.png ]; then
cp /home/$MY_USERNAME/freedombone/img/logo_fbone3.png /var/www/${domain_name}/htdocs/static/logo.png
fi
fi
fi
if [ ! -f /var/www/${domain_name}/htdocs/static/bg.jpg ]; then
return
fi
if [ -f ~/freedombone/img/backgrounds/${gnusocial_type}.jpg ]; then
cp ~/freedombone/img/backgrounds/${gnusocial_type}.jpg /var/www/${domain_name}/htdocs/static/bg.jpg
else
if [ -f /home/$MY_USERNAME/freedombone/img/backgrounds/${gnusocial_type}.jpg ]; then
cp /home/$MY_USERNAME/freedombone/img/backgrounds/${gnusocial_type}.jpg /var/www/${domain_name}/htdocs/static/bg.jpg
fi
fi
}
function install_armadillo_front_end {
app_name="$1"
armadillo_domain="$2"
background_url="$3"
if [ ! -d $INSTALL_DIR/armadillo ]; then
function_check git_clone
git_clone $ARMADILLO_REPO $INSTALL_DIR/armadillo
if [ ! -d $INSTALL_DIR/armadillo ]; then
echo $'Unable to clone armadillo repo'
exit 35722
fi
fi
cd $INSTALL_DIR/armadillo
git checkout $ARMADILLO_COMMIT -b $ARMADILLO_COMMIT
set_completion_param "${app_name} armadillo commit" "$ARMADILLO_COMMIT"
cp armadillo/config.json.example armadillo/config.json
if [[ $ONION_ONLY == 'no' ]]; then
sed -i "s|\"server\".*|\"server\": \"https://${armadillo_domain}\",|g" armadillo/config.json
else
sed -i "s|\"server\".*|\"server\": \"http://${armadillo_domain}\",|g" armadillo/config.json
fi
owner='<a href=\\"'"https://${armadillo_domain}/$MY_USERNAME"'\\">@'"$MY_USERNAME@${armadillo_domain}"'</a>'
sed -i "s|\"owner\".*|\"owner\": \"$owner\",|g" armadillo/config.json
cp -r $INSTALL_DIR/armadillo/armadillo /var/www/${armadillo_domain}/htdocs/
cp $INSTALL_DIR/armadillo/armadillo.html /var/www/${armadillo_domain}/htdocs/index.html
cp $INSTALL_DIR/armadillo/dash.html /var/www/${armadillo_domain}/htdocs/dash.html
armadillo_set_background_image_from_url "$armadillo_domain" "$background_url"
nginx_site=/etc/nginx/sites-available/${armadillo_domain}
sed -i 's|index index.php;|index index.html;|g' $nginx_site
if [ -f /var/www/${armadillo_domain}/htdocs/index.php ]; then
mv /var/www/${armadillo_domain}/htdocs/index.php /var/www/${armadillo_domain}/htdocs/index_qvitter.php
fi
sed -i 's|index.php|index_qvitter.php|g' $nginx_site
chown -R www-data:www-data /var/www/${armadillo_domain}/htdocs
}
function install_pleroma_front_end {
app_name="$1"
pleroma_domain="$2"
background_url="$3"
title="$4"
if [ ! -d $INSTALL_DIR/pleroma ]; then
function_check git_clone
git_clone $PLEROMA_REPO $INSTALL_DIR/pleroma
if [ ! -d $INSTALL_DIR/pleroma ]; then
echo $'Unable to clone pleroma repo'
exit 682252
fi
fi
cd $INSTALL_DIR/pleroma
git checkout $PLEROMA_COMMIT -b $PLEROMA_COMMIT
set_completion_param "${app_name} pleroma commit" "$PLEROMA_COMMIT"
if [ -d $INSTALL_DIR/pleroma/node_modules ]; then
rm -rf $INSTALL_DIR/pleroma/node_modules
fi
npm install -g yarn
npm install -g eslint@3.19.0
npm install -g eslint-plugin-promise@3.5.0
npm install -g moment@2.18.1
npm install -g node-sass@4.5.2
yarn
if [ -f $INSTALL_DIR/pleroma/dist/index.html ]; then
rm -rf $INSTALL_DIR/pleroma/dist/*
fi
npm run build
if [ ! -d $INSTALL_DIR/pleroma/dist ]; then
echo 'Unable to build pleroma'
exit 7629352
fi
if [ ! -f $INSTALL_DIR/pleroma/dist/index.html ]; then
echo $'Unable to build pleroma index.html'
exit 5282682
fi
cp -r $INSTALL_DIR/pleroma/dist/* /var/www/${pleroma_domain}/htdocs/
pleroma_set_background_image_from_url "$pleroma_domain" "$background_url" "$title"
nginx_site=/etc/nginx/sites-available/${pleroma_domain}
sed -i 's|index index.php;|index index.html;|g' $nginx_site
if [ -f /var/www/${pleroma_domain}/htdocs/index.php ]; then
mv /var/www/${pleroma_domain}/htdocs/index.php /var/www/${pleroma_domain}/htdocs/index_qvitter.php
fi
sed -i 's|index.php|index_qvitter.php|g' $nginx_site
# remove any cached yarn files
if [ -d /root/.cache/yarn ]; then
rm -rf /root/.cache/yarn
fi
chown -R www-data:www-data /var/www/${pleroma_domain}/htdocs
}
function upgrade_pleroma {
domain_name="$1"
app_name="$2"
background_url="$3"
title="$4"
if [ -d $INSTALL_DIR/pleroma ]; then
set_repo_commit $INSTALL_DIR/pleroma "${app_name} pleroma commit" "$PLEROMA_COMMIT" $PLEROMA_REPO
cd $INSTALL_DIR/pleroma
npm run build
if [ ! -d $INSTALL_DIR/pleroma/dist ]; then
echo 'Unable to build pleroma'
exit 268362
fi
if [ -f /var/www/${domain_name}/htdocs/static/logo.png ]; then
cp /var/www/${domain_name}/htdocs/static/logo.png /var/www/${domain_name}/htdocs/static/logo.png.old
fi
if [ -f /var/www/${domain_name}/htdocs/static/bg.jpg ]; then
cp /var/www/${domain_name}/htdocs/static/bg.jpg /var/www/${domain_name}/htdocs/static/bg.jpg.old
fi
if [ -f /var/www/${domain_name}/htdocs/static/config.json ]; then
cp /var/www/${domain_name}/htdocs/static/config.json /var/www/${domain_name}/htdocs/static/config.json.old
fi
cp -r $INSTALL_DIR/pleroma/dist/* /var/www/${domain_name}/htdocs/
if [ -f /var/www/${domain_name}/htdocs/static/config.json.old ]; then
mv /var/www/${domain_name}/htdocs/static/config.json.old /var/www/${domain_name}/htdocs/static/config.json
fi
sed -i "s|\"name\":.*|\"name\": \"${title}\",|g" /var/www/${domain_name}/htdocs/static/config.json
if [ -f /var/www/${domain_name}/htdocs/static/logo.png.old ]; then
cp /var/www/${domain_name}/htdocs/static/logo.png.old /var/www/${domain_name}/htdocs/static/logo.png
fi
if [ -f /var/www/${domain_name}/htdocs/static/bg.jpg.old ]; then
cp /var/www/${domain_name}/htdocs/static/bg.jpg.old /var/www/${domain_name}/htdocs/static/bg.jpg
fi
if [ -f /var/www/${domain_name}/htdocs/index.php ]; then
mv /var/www/${domain_name}/htdocs/index.php /var/www/${domain_name}/htdocs/index_qvitter.php
fi
chown -R www-data:www-data /var/www/${domain_name}/htdocs
else
install_pleroma_front_end "${app_name}" "${domain_name}" "${background_url}" "${title}"
fi
}
function upgrade_armadillo {
domain_name="$1"
app_name="$2"
background_url="$3"
if [ -d $INSTALL_DIR/armadillo ]; then
set_repo_commit $INSTALL_DIR/armadillo "${app_name} armadillo commit" "$ARMADILLO_COMMIT" $ARMADILLO_REPO
cd $INSTALL_DIR/armadillo
if [ -f $INSTALL_DIR/armadillo/armadillo/config.json ]; then
rm $INSTALL_DIR/armadillo/armadillo/config.json
fi
cp -r $INSTALL_DIR/armadillo/armadillo /var/www/${armadillo_domain}/htdocs/
cp $INSTALL_DIR/armadillo/armadillo.html /var/www/${armadillo_domain}/htdocs/index.html
cp $INSTALL_DIR/armadillo/dash.html /var/www/${armadillo_domain}/htdocs/dash.html
chown -R www-data:www-data /var/www/${domain_name}/htdocs
else
install_armadillo_front_end "${app_name}" "${domain_name}" "${background_url}"
fi
}
function gnusocial_hourly_script {
gnusocial_type=$1
domain_name=$2
# check that the daemon is running
echo '#!/bin/bash' > /etc/cron.hourly/${gnusocial_type}-daemons
echo -n 'daemon_lines=$(ps aux | grep "' >> /etc/cron.hourly/${gnusocial_type}-daemons
echo -n "${domain_name}" >> /etc/cron.hourly/${gnusocial_type}-daemons
echo '/htdocs/scripts/queuedaemon.php" | grep "/var/www")' >> /etc/cron.hourly/${gnusocial_type}-daemons
echo "cd /var/www/${domain_name}/htdocs" >> /etc/cron.hourly/${gnusocial_type}-daemons
echo 'if [[ $daemon_lines != *"/var/www/"* ]]; then' >> /etc/cron.hourly/${gnusocial_type}-daemons
echo " ADMIN_USER=\$(cat $COMPLETION_FILE | grep 'Admin user' | awk -F ':' '{print \$2}')" >> /etc/cron.hourly/${gnusocial_type}-daemons
echo ' MY_EMAIL_ADDRESS=$ADMIN_USER@$HOSTNAME' >> /etc/cron.hourly/${gnusocial_type}-daemons
echo -n " echo \"Restarting ${gnusocial_type} daemons\" | mail -s \"${gnusocial_type} " >> /etc/cron.hourly/${gnusocial_type}-daemons
echo 'daemons not found" $MY_EMAIL_ADDRESS' >> /etc/cron.hourly/${gnusocial_type}-daemons
echo ' su -c "sh scripts/startdaemons.sh" -s /bin/sh www-data' >> /etc/cron.hourly/${gnusocial_type}-daemons
echo 'fi' >> /etc/cron.hourly/${gnusocial_type}-daemons
echo 'php scripts/delete_orphan_files.php > /dev/null' >> /etc/cron.hourly/${gnusocial_type}-daemons
echo 'php scripts/clean_thumbnails.php -y > /dev/null' >> /etc/cron.hourly/${gnusocial_type}-daemons
echo 'php scripts/clean_file_table.php -y > /dev/null' >> /etc/cron.hourly/${gnusocial_type}-daemons
echo 'php scripts/upgrade.php > /dev/null' >> /etc/cron.hourly/${gnusocial_type}-daemons
chmod +x /etc/cron.hourly/${gnusocial_type}-daemons
}
function gnusocial_block_user_script {
echo '#!/bin/bash' > /usr/bin/gnusocial-delete-user
echo '' >> /usr/bin/gnusocial-delete-user
echo 'database_name=$1' >> /usr/bin/gnusocial-delete-user
echo 'remove_user=$2' >> /usr/bin/gnusocial-delete-user
echo 'domain_name=$3' >> /usr/bin/gnusocial-delete-user
echo 'MARIADB_PASSWORD=$(freedombone-pass -u root -a mariadb)' >> /usr/bin/gnusocial-delete-user
echo '' >> /usr/bin/gnusocial-delete-user
echo 'if [ ${#remove_user} -lt 2 ]; then' >> /usr/bin/gnusocial-delete-user
echo ' echo $"No user was specified"' >> /usr/bin/gnusocial-delete-user
echo ' exit 1' >> /usr/bin/gnusocial-delete-user
echo 'fi' >> /usr/bin/gnusocial-delete-user
echo '' >> /usr/bin/gnusocial-delete-user
echo 'if [[ "$remove_user" != *"."* || "$remove_user" != *"@"* ]]; then' >> /usr/bin/gnusocial-delete-user
echo ' echo $"This doesnt look like a user"' >> /usr/bin/gnusocial-delete-user
echo ' exit 2' >> /usr/bin/gnusocial-delete-user
echo 'fi' >> /usr/bin/gnusocial-delete-user
echo '' >> /usr/bin/gnusocial-delete-user
echo 'if [ ${#database_name} -lt 2 ]; then' >> /usr/bin/gnusocial-delete-user
echo ' echo $"No database was specified"' >> /usr/bin/gnusocial-delete-user
echo ' exit 3' >> /usr/bin/gnusocial-delete-user
echo 'fi' >> /usr/bin/gnusocial-delete-user
echo '' >> /usr/bin/gnusocial-delete-user
echo "nickname=\$(echo \"\$remove_user\" | awk -F '@' '{print \$1}')" >> /usr/bin/gnusocial-delete-user
echo "remove_domain=\$(echo \"\$remove_user\" | awk -F '@' '{print \$2}')" >> /usr/bin/gnusocial-delete-user
echo '' >> /usr/bin/gnusocial-delete-user
echo 'database_query="select profile.id from profile where nickname = '"'"'${nickname}'"'"' and profileurl like '"'"'%${remove_domain}%'"'"';"' >> /usr/bin/gnusocial-delete-user
echo 'declare -a ids=$(mysql -u root --password="$MARIADB_PASSWORD" << EOF' >> /usr/bin/gnusocial-delete-user
echo 'use $database_name;' >> /usr/bin/gnusocial-delete-user
echo '$database_query' >> /usr/bin/gnusocial-delete-user
echo 'EOF' >> /usr/bin/gnusocial-delete-user
echo ')' >> /usr/bin/gnusocial-delete-user
echo '' >> /usr/bin/gnusocial-delete-user
echo 'ctr=0' >> /usr/bin/gnusocial-delete-user
echo 'for id in $ids' >> /usr/bin/gnusocial-delete-user
echo 'do' >> /usr/bin/gnusocial-delete-user
echo ' if [ $ctr -gt 0 ]; then' >> /usr/bin/gnusocial-delete-user
echo ' #echo "Removing user ID $id on $remove_domain"' >> /usr/bin/gnusocial-delete-user
echo '' >> /usr/bin/gnusocial-delete-user
echo ' database_query="delete from reply where profile_id=${id};"' >> /usr/bin/gnusocial-delete-user
echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-user
echo '' >> /usr/bin/gnusocial-delete-user
echo ' database_query="delete from reply where replied_id=${id};"' >> /usr/bin/gnusocial-delete-user
echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-user
echo '' >> /usr/bin/gnusocial-delete-user
echo ' if [ $domain_name ]; then' >> /usr/bin/gnusocial-delete-user
echo ' database_query="select rendered from notice where profile_id=${id} and rendered like '"'"'%/file/%'"'"';"' >> /usr/bin/gnusocial-delete-user
echo ' declare -a stored_files=$(mysql -u root --password="$MARIADB_PASSWORD" << EOF' >> /usr/bin/gnusocial-delete-user
echo 'use $database_name;' >> /usr/bin/gnusocial-delete-user
echo '$database_query' >> /usr/bin/gnusocial-delete-user
echo 'EOF' >> /usr/bin/gnusocial-delete-user
echo ')' >> /usr/bin/gnusocial-delete-user
echo -n " files_to_delete=\$(echo \"\$stored_files\" | " >> /usr/bin/gnusocial-delete-user
echo 'sed -n '"'"'s/.*href="\([^"]*\).*/\1/p'"'"' | awk -F '"'"'file/'"'"' '"'"'{print $2}'"'"')' >> /usr/bin/gnusocial-delete-user
echo ' filectr=0' >> /usr/bin/gnusocial-delete-user
echo ' for f in $files_to_delete' >> /usr/bin/gnusocial-delete-user
echo ' do' >> /usr/bin/gnusocial-delete-user
echo ' if [ $filectr -gt 0 ]; then' >> /usr/bin/gnusocial-delete-user
echo ' if $(ls /var/www/${domain_name}/htdocs/file/*${f}* 1> /dev/null 2>&1); then' >> /usr/bin/gnusocial-delete-user
echo ' rm /var/www/${domain_name}/htdocs/file/*${f}*' >> /usr/bin/gnusocial-delete-user
echo ' fi' >> /usr/bin/gnusocial-delete-user
echo ' fi' >> /usr/bin/gnusocial-delete-user
echo ' filectr=$((filectr + 1))' >> /usr/bin/gnusocial-delete-user
echo ' done' >> /usr/bin/gnusocial-delete-user
echo ' fi' >> /usr/bin/gnusocial-delete-user
echo '' >> /usr/bin/gnusocial-delete-user
echo ' database_query="delete from notice where profile_id=${id};"' >> /usr/bin/gnusocial-delete-user
echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-user
echo '' >> /usr/bin/gnusocial-delete-user
echo ' database_query="delete from profile where id=${id};"' >> /usr/bin/gnusocial-delete-user
echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-user
echo '' >> /usr/bin/gnusocial-delete-user
echo ' database_query="delete from qvitternotification where to_profile_id=${id} or from_profile_id=${id};"' >> /usr/bin/gnusocial-delete-user
echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-user
echo ' fi' >> /usr/bin/gnusocial-delete-user
echo ' ctr=$((ctr + 1))' >> /usr/bin/gnusocial-delete-user
echo 'done' >> /usr/bin/gnusocial-delete-user
chmod +x /usr/bin/gnusocial-delete-user
}
function gnusocial_block_domain_script {
database_name=$1
gnusoc_domain=$2
echo '#!/bin/bash' > /usr/bin/gnusocial-delete-domain
echo '' >> /usr/bin/gnusocial-delete-domain
echo 'database_name=$1' >> /usr/bin/gnusocial-delete-domain
echo 'remove_domain=$2' >> /usr/bin/gnusocial-delete-domain
echo 'domain_name=$3' >> /usr/bin/gnusocial-delete-domain
echo "MARIADB_PASSWORD=\$(${PROJECT_NAME}-pass -u root -a mariadb)" >> /usr/bin/gnusocial-delete-domain
echo '' >> /usr/bin/gnusocial-delete-domain
echo 'if [ ${#remove_domain} -lt 2 ]; then' >> /usr/bin/gnusocial-delete-domain
echo ' echo $"No domain was specified"' >> /usr/bin/gnusocial-delete-domain
echo ' exit 1' >> /usr/bin/gnusocial-delete-domain
echo 'fi' >> /usr/bin/gnusocial-delete-domain
echo '' >> /usr/bin/gnusocial-delete-domain
echo 'if [[ "$remove_domain" != *"."* ]]; then' >> /usr/bin/gnusocial-delete-domain
echo ' echo $"This doesnt look like a domain"' >> /usr/bin/gnusocial-delete-domain
echo ' exit 2' >> /usr/bin/gnusocial-delete-domain
echo 'fi' >> /usr/bin/gnusocial-delete-domain
echo '' >> /usr/bin/gnusocial-delete-domain
echo 'if [ ${#database_name} -lt 2 ]; then' >> /usr/bin/gnusocial-delete-domain
echo ' echo $"No database was specified"' >> /usr/bin/gnusocial-delete-domain
echo ' exit 3' >> /usr/bin/gnusocial-delete-domain
echo 'fi' >> /usr/bin/gnusocial-delete-domain
echo '' >> /usr/bin/gnusocial-delete-domain
echo "database_query=\"select profile.id from profile where profileurl like '%\${remove_domain}%';\"" >> /usr/bin/gnusocial-delete-domain
echo 'declare -a ids=$(mysql -u root --password="$MARIADB_PASSWORD" << EOF' >> /usr/bin/gnusocial-delete-domain
echo 'use $database_name;' >> /usr/bin/gnusocial-delete-domain
echo '$database_query' >> /usr/bin/gnusocial-delete-domain
echo 'EOF' >> /usr/bin/gnusocial-delete-domain
echo ')' >> /usr/bin/gnusocial-delete-domain
echo '' >> /usr/bin/gnusocial-delete-domain
echo 'ctr=0' >> /usr/bin/gnusocial-delete-domain
echo 'for id in $ids' >> /usr/bin/gnusocial-delete-domain
echo 'do' >> /usr/bin/gnusocial-delete-domain
echo ' if [ $ctr -gt 0 ]; then' >> /usr/bin/gnusocial-delete-domain
echo ' #echo "Removing user ID $id on $remove_domain"' >> /usr/bin/gnusocial-delete-domain
echo '' >> /usr/bin/gnusocial-delete-domain
echo " database_query=\"delete from conversation where uri like '%\${remove_domain}%';\"" >> /usr/bin/gnusocial-delete-domain
echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-domain
echo '' >> /usr/bin/gnusocial-delete-domain
echo ' database_query="delete from reply where profile_id=${id};"' >> /usr/bin/gnusocial-delete-domain
echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-domain
echo '' >> /usr/bin/gnusocial-delete-domain
echo ' database_query="delete from reply where replied_id=${id};"' >> /usr/bin/gnusocial-delete-domain
echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-domain
echo '' >> /usr/bin/gnusocial-delete-domain
echo ' if [ $domain_name ]; then' >> /usr/bin/gnusocial-delete-domain
echo ' database_query="select rendered from notice where profile_id=${id} and rendered like '"'"'%/file/%'"'"';"' >> /usr/bin/gnusocial-delete-domain
echo ' declare -a stored_files=$(mysql -u root --password="$MARIADB_PASSWORD" << EOF' >> /usr/bin/gnusocial-delete-domain
echo 'use $database_name;' >> /usr/bin/gnusocial-delete-domain
echo '$database_query' >> /usr/bin/gnusocial-delete-domain
echo 'EOF' >> /usr/bin/gnusocial-delete-domain
echo ')' >> /usr/bin/gnusocial-delete-domain
echo -n " files_to_delete=\$(echo \"\$stored_files\" | " >> /usr/bin/gnusocial-delete-domain
echo 'sed -n '"'"'s/.*href="\([^"]*\).*/\1/p'"'"' | awk -F '"'"'file/'"'"' '"'"'{print $2}'"'"')' >> /usr/bin/gnusocial-delete-domain
echo ' filectr=0' >> /usr/bin/gnusocial-delete-domain
echo ' for f in $files_to_delete' >> /usr/bin/gnusocial-delete-domain
echo ' do' >> /usr/bin/gnusocial-delete-domain
echo ' if [ $filectr -gt 0 ]; then' >> /usr/bin/gnusocial-delete-domain
echo ' if $(ls /var/www/${domain_name}/htdocs/file/*${f}* 1> /dev/null 2>&1); then' >> /usr/bin/gnusocial-delete-domain
echo ' rm /var/www/${domain_name}/htdocs/file/*${f}*' >> /usr/bin/gnusocial-delete-domain
echo ' fi' >> /usr/bin/gnusocial-delete-domain
echo ' fi' >> /usr/bin/gnusocial-delete-domain
echo ' filectr=$((filectr + 1))' >> /usr/bin/gnusocial-delete-domain
echo ' done' >> /usr/bin/gnusocial-delete-domain
echo ' fi' >> /usr/bin/gnusocial-delete-domain
echo '' >> /usr/bin/gnusocial-delete-domain
echo ' database_query="delete from notice where profile_id=${id};"' >> /usr/bin/gnusocial-delete-domain
echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-domain
echo '' >> /usr/bin/gnusocial-delete-domain
echo ' database_query="delete from profile where id=${id};"' >> /usr/bin/gnusocial-delete-domain
echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-domain
echo '' >> /usr/bin/gnusocial-delete-domain
echo ' database_query="delete from qvitternotification where to_profile_id=${id} or from_profile_id=${id};"' >> /usr/bin/gnusocial-delete-domain
echo ' mysql -u root --password="$MARIADB_PASSWORD" -e "$database_query" $database_name' >> /usr/bin/gnusocial-delete-domain
echo ' fi' >> /usr/bin/gnusocial-delete-domain
echo ' ctr=$((ctr + 1))' >> /usr/bin/gnusocial-delete-domain
echo 'done' >> /usr/bin/gnusocial-delete-domain
chmod +x /usr/bin/gnusocial-delete-domain
echo '#!/bin/bash' > /usr/bin/${database_name}-firewall
echo '' >> /usr/bin/${database_name}-firewall
echo "PROJECT_NAME=$PROJECT_NAME" >> /usr/bin/${database_name}-firewall
echo "GNUSOCIAL_DOMAIN_NAME=$gnusoc_domain" >> /usr/bin/${database_name}-firewall
echo "database_name=$database_name" >> /usr/bin/${database_name}-firewall
echo 'FIREWALL_DOMAINS=$HOME/${PROJECT_NAME}-firewall-domains.cfg' >> /usr/bin/${database_name}-firewall
echo '' >> /usr/bin/${database_name}-firewall
echo 'while read block; do' >> /usr/bin/${database_name}-firewall
echo ' if [[ "$block" != *"@"* ]]; then' >> /usr/bin/${database_name}-firewall
echo ' /usr/bin/gnusocial-delete-domain "$database_name" "$block" "$GNUSOCIAL_DOMAIN_NAME"' >> /usr/bin/${database_name}-firewall
echo ' else' >> /usr/bin/${database_name}-firewall
echo ' /usr/bin/gnusocial-delete-user "$database_name" "$block" "$GNUSOCIAL_DOMAIN_NAME"' >> /usr/bin/${database_name}-firewall
echo ' fi' >> /usr/bin/${database_name}-firewall
echo 'done <$FIREWALL_DOMAINS' >> /usr/bin/${database_name}-firewall
echo '' >> /usr/bin/${database_name}-firewall
echo '# remove any metadata from images uploaded in the last 2 minutes' >> /usr/bin/${database_name}-firewall
echo 'if [ -d /var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs ]; then' >> /usr/bin/${database_name}-firewall
echo ' cd /var/www/$GNUSOCIAL_DOMAIN_NAME/htdocs' >> /usr/bin/${database_name}-firewall
echo ' declare -a recent_image=$(find file ! -name thumb-* -mmin -2 -type f -exec ls -l {} + | awk -F '"'"'file/'"'"' '"'"'{print $2}'"'"')' >> /usr/bin/${database_name}-firewall
echo ' for f in ${recent_image[@]}' >> /usr/bin/${database_name}-firewall
echo ' do' >> /usr/bin/${database_name}-firewall
echo ' if [ ${#f} -gt 1 ]; then' >> /usr/bin/${database_name}-firewall
echo ' exiftool -q -all= "file/${f}"' >> /usr/bin/${database_name}-firewall
echo ' fi' >> /usr/bin/${database_name}-firewall
echo ' done' >> /usr/bin/${database_name}-firewall
echo 'fi' >> /usr/bin/${database_name}-firewall
chmod +x /usr/bin/${database_name}-firewall
if ! grep -q "${database_name}-firewall" /etc/crontab; then
echo "*/1 * * * * root /usr/bin/${database_name}-firewall" >> /etc/crontab
fi
}
function expire_gnusocial_posts {
domain_name=$1
gnusocial_type=$2
expire_months=$3
if [ ! $expire_months ]; then
expire_months=3
fi
expire_days=$((expire_months * 30))
# To prevent the database size from growing endlessly this script expires posts
# after a number of months
if [ ! -d /var/www/${domain_name}/htdocs ]; then
return
fi
gnusocial_expire_posts_script=/usr/bin/${gnusocial_type}-expire-posts
echo '<?php' > $gnusocial_expire_posts_script
echo '' >> $gnusocial_expire_posts_script
echo "// ${gnusocial_type} post expiry script, based on StatExpire by Tony Baldwin" >> $gnusocial_expire_posts_script
echo '// https://github.com/tonybaldwin/statexpire' >> $gnusocial_expire_posts_script
echo '' >> $gnusocial_expire_posts_script
echo "\$oldate=date((\"Y-m-d\"), strtotime(\"-${expire_months} months\"));" >> $gnusocial_expire_posts_script
echo '$username="root";' >> $gnusocial_expire_posts_script
echo "\$password=shell_exec('${PROJECT_NAME}-pass -u root -a mariadb');" >> $gnusocial_expire_posts_script
echo "\$database=\"${gnusocial_type}\";" >> $gnusocial_expire_posts_script
echo '' >> $gnusocial_expire_posts_script
echo 'if (!$link = mysql_connect("localhost", $username, $password)) {' >> $gnusocial_expire_posts_script
echo ' echo "Could not connect to mariadb";' >> $gnusocial_expire_posts_script
echo ' exit;' >> $gnusocial_expire_posts_script
echo '}' >> $gnusocial_expire_posts_script
echo '' >> $gnusocial_expire_posts_script
echo 'if (!mysql_select_db($database, $link)) {' >> $gnusocial_expire_posts_script
echo " echo \"Could not select ${gnusocial_type} database\";" >> $gnusocial_expire_posts_script
echo ' exit;' >> $gnusocial_expire_posts_script
echo '}' >> $gnusocial_expire_posts_script
echo '' >> $gnusocial_expire_posts_script
echo "\$notice_query=\"DELETE FROM notice WHERE created <= '\$oldate 01:01:01'\";" >> $gnusocial_expire_posts_script
echo "\$conversation_query=\"DELETE FROM conversation WHERE created <= '$oldate 01:01:01'\";" >> $gnusocial_expire_posts_script
echo "\$reply_query=\"DELETE FROM reply WHERE modified <= '\$oldate 01:01:01'\";" >> $gnusocial_expire_posts_script
echo "\$notification_query=\"DELETE FROM qvitternotification WHERE created <= '\$oldate 01:01:01'\";" >> $gnusocial_expire_posts_script
echo '' >> $gnusocial_expire_posts_script
echo 'mysql_query($notice_query);' >> $gnusocial_expire_posts_script
echo '$rowaff1=mysql_affected_rows();' >> $gnusocial_expire_posts_script
echo 'mysql_query($conversation_query);' >> $gnusocial_expire_posts_script
echo '$rowaff2=mysql_affected_rows();' >> $gnusocial_expire_posts_script
echo 'mysql_query($reply_query);' >> $gnusocial_expire_posts_script
echo '$rowaff3=mysql_affected_rows();' >> $gnusocial_expire_posts_script
echo 'mysql_query($notification_query);' >> $gnusocial_expire_posts_script
echo '$rowaff4=mysql_affected_rows();' >> $gnusocial_expire_posts_script
echo 'mysql_close();' >> $gnusocial_expire_posts_script
echo '' >> $gnusocial_expire_posts_script
echo -n "echo \"Expire ${gnusocial_type} posts: " >> $gnusocial_expire_posts_script
echo '$rowaff1 notices, $rowaff2 conversations, $rowaff3 replies, and $rowaff4 qvitter notifications deleted from database.\n";' >> $gnusocial_expire_posts_script
chmod +x $gnusocial_expire_posts_script
gnusocial_expire_script=/usr/bin/${gnusocial_type}-expire
echo '#!/bin/bash' > $gnusocial_expire_script
echo "/usr/bin/php $gnusocial_expire_posts_script" >> $gnusocial_expire_script
echo "find /var/www/${domain_name}/htdocs/file/* -mtime +${expire_days} -exec rm {} +" >> $gnusocial_expire_script
chmod +x $gnusocial_expire_script
# Add a cron job
if ! grep -q "${gnusocial_expire_script}" /etc/crontab; then
echo "10 3 5 * * root /usr/bin/timeout 500 ${gnusocial_expire_script}" >> /etc/crontab
fi
# remove old expire script
if [ -f /etc/cron.weekly/clear-${gnusocial_type}-database ]; then
rm /etc/cron.weekly/clear-${gnusocial_type}-database
fi
}
function gnusocial_use_classic {
database_name=$1
domain_name=$(get_completion_param "$database_name domain")
if [ -f /var/www/$domain_name/htdocs/index_qvitter.php ]; then
mv /var/www/$domain_name/htdocs/index_qvitter.php /var/www/$domain_name/htdocs/index.php
fi
if [ -f /etc/nginx/sites-available/$domain_name ]; then
sed -i 's|index_qvitter.php|index.php|g' /etc/nginx/sites-available/$domain_name
sed -i 's|index.html|index.php|g' /etc/nginx/sites-available/$domain_name
fi
if ! grep -q "//addPlugin('Qvitter')" /var/www/$domain_name/htdocs/config.php; then
sed -i "s|// addPlugin('Qvitter')|addPlugin('Qvitter')|g" /var/www/$domain_name/htdocs/config.php
sed -i "s|addPlugin('Qvitter')|//addPlugin('Qvitter')|g" /var/www/$domain_name/htdocs/config.php
fi
systemctl restart nginx
}
function gnusocial_use_qvitter {
database_name=$1
domain_name=$(get_completion_param "$database_name domain")
if [ -f /var/www/$domain_name/htdocs/index_qvitter.php ]; then
mv /var/www/$domain_name/htdocs/index_qvitter.php /var/www/$domain_name/htdocs/index.php
fi
if [ -f /etc/nginx/sites-available/$domain_name ]; then
sed -i 's|index_qvitter.php|index.php|g' /etc/nginx/sites-available/$domain_name
sed -i 's|index.html|index.php|g' /etc/nginx/sites-available/$domain_name
fi
if grep -q "//addPlugin('Qvitter')" /var/www/$domain_name/htdocs/config.php; then
sed -i "s|//addPlugin('Qvitter')|addPlugin('Qvitter')|g" /var/www/$domain_name/htdocs/config.php
fi
if grep -q "// addPlugin('Qvitter')" /var/www/$domain_name/htdocs/config.php; then
sed -i "s|// addPlugin('Qvitter')|addPlugin('Qvitter')|g" /var/www/$domain_name/htdocs/config.php
fi
systemctl restart nginx
}
function gnusocial_use_pleroma {
database_name=$1
domain_name=$(get_completion_param "$database_name domain")
if [ ! -d /var/www/$domain_name/htdocs/static ]; then
return
fi
if [ -f /var/www/$domain_name/htdocs/index.php ]; then
mv /var/www/$domain_name/htdocs/index.php /var/www/$domain_name/htdocs/index_qvitter.php
fi
if [ -f /etc/nginx/sites-available/$domain_name ]; then
sed -i 's|index.php|index_qvitter.php|g' /etc/nginx/sites-available/$domain_name
sed -i 's|index index_qvitter.php|index index.html|g' /etc/nginx/sites-available/$domain_name
fi
if grep -q "//addPlugin('Qvitter')" /var/www/$domain_name/htdocs/config.php; then
sed -i "s|//addPlugin('Qvitter')|addPlugin('Qvitter')|g" /var/www/$domain_name/htdocs/config.php
fi
if grep -q "// addPlugin('Qvitter')" /var/www/$domain_name/htdocs/config.php; then
sed -i "s|// addPlugin('Qvitter')|addPlugin('Qvitter')|g" /var/www/$domain_name/htdocs/config.php
fi
cp $INSTALL_DIR/pleroma/dist/index.html /var/www/${domain_name}/htdocs/index.html
systemctl restart nginx
}
function gnusocial_use_armadillo {
database_name=$1
domain_name=$(get_completion_param "$database_name domain")
if [ ! -d /var/www/$domain_name/htdocs/armadillo ]; then
return
fi
if [ -f /var/www/$domain_name/htdocs/index.php ]; then
mv /var/www/$domain_name/htdocs/index.php /var/www/$domain_name/htdocs/index_qvitter.php
fi
if [ -f /etc/nginx/sites-available/$domain_name ]; then
sed -i 's|index.php|index_qvitter.php|g' /etc/nginx/sites-available/$domain_name
sed -i 's|index index_qvitter.php|index index.html|g' /etc/nginx/sites-available/$domain_name
fi
if grep -q "//addPlugin('Qvitter')" /var/www/$domain_name/htdocs/config.php; then
sed -i "s|//addPlugin('Qvitter')|addPlugin('Qvitter')|g" /var/www/$domain_name/htdocs/config.php
fi
if grep -q "// addPlugin('Qvitter')" /var/www/$domain_name/htdocs/config.php; then
sed -i "s|// addPlugin('Qvitter')|addPlugin('Qvitter')|g" /var/www/$domain_name/htdocs/config.php
fi
if [ -f $INSTALL_DIR/armadillo/armadillo/config.json ]; then
rm $INSTALL_DIR/armadillo/armadillo/config.json
fi
cp -r $INSTALL_DIR/armadillo/armadillo/* /var/www/${domain_name}/htdocs/armadillo
cp $INSTALL_DIR/armadillo/armadillo.html /var/www/${domain_name}/htdocs/index.html
cp $INSTALL_DIR/armadillo/dash.html /var/www/${domain_name}/htdocs/dash.html
systemctl restart nginx
}
# NOTE: deliberately there is no "exit 0"