Newer
Older
#!/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.git"
QVITTER_THEME_COMMIT='a7f82628402db3a7579bb9b2877da3c5737da77b'
PLEROMA_REPO="https://gitgud.io/lambadalambda/pleroma-fe"
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="gif"
fi
fi
if [ ${#ext} -gt 0 ]; then
if [ -d /var/www/${domain_name}/htdocs/static ]; then
cd /var/www/${domain_name}/htdocs/static
# remove any existing image
if [ -f bg_custom.${ext} ]; then
rm bg_custom.${ext}
fi
# get the new image
if [ ! -f bg_custom.${ext} ]; then
echo "$url"
echo $'Custom background image for pleroma could not be downloaded'
echo "1"
return
fi
sed -i "s|\"background\":.*|\"background\": \"/static/bg_custom.${ext}\",|g" config.json
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
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 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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_pleroma {
app_name="$1"
pleroma_domain="$2"
background_url="$3"
title="$4"
git_clone $PLEROMA_REPO $INSTALL_DIR/pleroma
if [ ! -d $INSTALL_DIR/pleroma ]; then
echo $'Unable to clone pleroma repo'
exit 682252
fi
fi
git checkout $PLEROMA_COMMIT -b $PLEROMA_COMMIT
set_completion_param "${app_name} pleroma commit" "$PLEROMA_COMMIT"
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
echo 'Unable to build pleroma'
exit 7629352
fi
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"
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
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
echo 'Unable to build pleroma'
exit 268362
fi
cp -r $INSTALL_DIR/pleroma/dist/* /var/www/${domain_name}/htdocs/
pleroma_set_background_image_from_url "$domain_name" "$background_url" "$title"
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 "${app_name}" "${domain_name}" "${background_url}" "${title}"
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 ' scripts/startdaemons.sh' >> /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
}
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
function expire_gnusocial_posts {
domain_name=$1
gnusocial_type=$2
expire_months=$3
if [ ! $expire_months ]; then
expire_months=3
fi
# 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_script=/usr/bin/${gnusocial_type}-expire
echo '<?php' > $gnusocial_expire_script
echo '' >> $gnusocial_expire_script
echo "// ${gnusocial_type} post expiry script, based on StatExpire by Tony Baldwin" >> $gnusocial_expire_script
echo '// https://github.com/tonybaldwin/statexpire' >> $gnusocial_expire_script
echo '' >> $gnusocial_expire_script
echo "\$oldate=date((\"Y-m-d\"), strtotime(\"-${expire_months} months\"));" >> $gnusocial_expire_script
echo '$username="root";' >> $gnusocial_expire_script
echo "\$password=shell_exec('${PROJECT_NAME}-pass -u root -a mariadb');" >> $gnusocial_expire_script
echo "\$database=\"${gnusocial_type}\";" >> $gnusocial_expire_script
echo '' >> $gnusocial_expire_script
echo 'if (!$link = mysql_connect("localhost", $username, $password)) {' >> $gnusocial_expire_script
echo ' echo "Could not connect to mariadb";' >> $gnusocial_expire_script
echo ' exit;' >> $gnusocial_expire_script
echo '}' >> $gnusocial_expire_script
echo '' >> $gnusocial_expire_script
echo 'if (!mysql_select_db($database, $link)) {' >> $gnusocial_expire_script
echo " echo \"Could not select ${gnusocial_type} database\";" >> $gnusocial_expire_script
echo ' exit;' >> $gnusocial_expire_script
echo '}' >> $gnusocial_expire_script
echo '' >> $gnusocial_expire_script
echo "\$notice_query=\"DELETE FROM notice WHERE created <= '\$oldate 01:01:01'\";" >> $gnusocial_expire_script
echo "\$conversation_query=\"DELETE FROM conversation WHERE created <= '$oldate 01:01:01'\";" >> $gnusocial_expire_script
echo "\$reply_query=\"DELETE FROM reply WHERE modified <= '\$oldate 01:01:01'\";" >> $gnusocial_expire_script
echo '' >> $gnusocial_expire_script
echo 'mysql_query($notice_query);' >> $gnusocial_expire_script
echo '$rowaff1=mysql_affected_rows();' >> $gnusocial_expire_script
echo 'mysql_query($conversation_query);' >> $gnusocial_expire_script
echo '$rowaff2=mysql_affected_rows();' >> $gnusocial_expire_script
echo 'mysql_query($reply_query);' >> $gnusocial_expire_script
echo '$rowaff3=mysql_affected_rows();' >> $gnusocial_expire_script
echo 'mysql_close();' >> $gnusocial_expire_script
echo '' >> $gnusocial_expire_script
echo -n "echo \"Expire ${gnusocial_type} posts: " >> $gnusocial_expire_script
echo '$rowaff1 notices, $rowaff2 conversations, and $rowaff3 replies deleted from database.\n";' >> $gnusocial_expire_script
chmod +x $gnusocial_expire_script
# Add a cron job
if ! grep -q "${gnusocial_type}_expire_script" /etc/crontab; then
echo "10 3 5 * * root /usr/bin/timeout 500 /usr/bin/php ${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
}
# NOTE: deliberately there is no "exit 0"